[SFN] introduce statement frontier notes, still disabled
[official-gcc.git] / gcc / c / c-parser.c
blobf1bae8abdf380962fd4c29bf646d28a8780e107e
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #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)
1331 cpp_opts->cpp_pedantic = pedantic = 0;
1332 warn_pointer_arith = 0;
1333 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1334 flag_iso = 0;
1335 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1336 warn_cxx_compat = 0;
1337 warn_overlength_strings = 0;
1338 warn_c90_c99_compat = 0;
1339 warn_c99_c11_compat = 0;
1340 return ret;
1343 /* Restore the warning flags which are controlled by __extension__.
1344 FLAGS is the return value from disable_extension_diagnostics. */
1346 static inline void
1347 restore_extension_diagnostics (int flags)
1349 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1350 warn_pointer_arith = (flags >> 1) & 1;
1351 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1352 flag_iso = (flags >> 3) & 1;
1353 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1354 warn_cxx_compat = (flags >> 5) & 1;
1355 warn_overlength_strings = (flags >> 6) & 1;
1356 /* See above for why is this needed. */
1357 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1358 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1361 /* Helper data structure for parsing #pragma acc routine. */
1362 struct oacc_routine_data {
1363 bool error_seen; /* Set if error has been reported. */
1364 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1365 tree clauses;
1366 location_t loc;
1369 static void c_parser_external_declaration (c_parser *);
1370 static void c_parser_asm_definition (c_parser *);
1371 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1372 bool, bool, tree *, vec<c_token>,
1373 struct oacc_routine_data * = NULL,
1374 bool * = NULL);
1375 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1376 static void c_parser_static_assert_declaration (c_parser *);
1377 static struct c_typespec c_parser_enum_specifier (c_parser *);
1378 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1379 static tree c_parser_struct_declaration (c_parser *);
1380 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1381 static tree c_parser_alignas_specifier (c_parser *);
1382 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1383 c_dtr_syn, bool *);
1384 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1385 bool,
1386 struct c_declarator *);
1387 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1388 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1389 tree);
1390 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1391 static tree c_parser_simple_asm_expr (c_parser *);
1392 static tree c_parser_attributes (c_parser *);
1393 static struct c_expr c_parser_initializer (c_parser *);
1394 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1395 struct obstack *);
1396 static void c_parser_initelt (c_parser *, struct obstack *);
1397 static void c_parser_initval (c_parser *, struct c_expr *,
1398 struct obstack *);
1399 static tree c_parser_compound_statement (c_parser *);
1400 static void c_parser_compound_statement_nostart (c_parser *);
1401 static void c_parser_label (c_parser *);
1402 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1403 static void c_parser_statement_after_labels (c_parser *, bool *,
1404 vec<tree> * = NULL);
1405 static tree c_parser_c99_block_statement (c_parser *, bool *,
1406 location_t * = NULL);
1407 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1408 static void c_parser_switch_statement (c_parser *, bool *);
1409 static void c_parser_while_statement (c_parser *, bool, bool *);
1410 static void c_parser_do_statement (c_parser *, bool);
1411 static void c_parser_for_statement (c_parser *, bool, bool *);
1412 static tree c_parser_asm_statement (c_parser *);
1413 static tree c_parser_asm_operands (c_parser *);
1414 static tree c_parser_asm_goto_operands (c_parser *);
1415 static tree c_parser_asm_clobbers (c_parser *);
1416 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1417 tree = NULL_TREE);
1418 static struct c_expr c_parser_conditional_expression (c_parser *,
1419 struct c_expr *, tree);
1420 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1421 tree);
1422 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1423 static struct c_expr c_parser_unary_expression (c_parser *);
1424 static struct c_expr c_parser_sizeof_expression (c_parser *);
1425 static struct c_expr c_parser_alignof_expression (c_parser *);
1426 static struct c_expr c_parser_postfix_expression (c_parser *);
1427 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1428 struct c_type_name *,
1429 location_t);
1430 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1431 location_t loc,
1432 struct c_expr);
1433 static tree c_parser_transaction (c_parser *, enum rid);
1434 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1435 static tree c_parser_transaction_cancel (c_parser *);
1436 static struct c_expr c_parser_expression (c_parser *);
1437 static struct c_expr c_parser_expression_conv (c_parser *);
1438 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1439 vec<tree, va_gc> **, location_t *,
1440 tree *, vec<location_t> *,
1441 unsigned int * = NULL);
1442 static void c_parser_oacc_declare (c_parser *);
1443 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1444 static void c_parser_oacc_update (c_parser *);
1445 static void c_parser_omp_construct (c_parser *, bool *);
1446 static void c_parser_omp_threadprivate (c_parser *);
1447 static void c_parser_omp_barrier (c_parser *);
1448 static void c_parser_omp_flush (c_parser *);
1449 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1450 tree, tree *, bool *);
1451 static void c_parser_omp_taskwait (c_parser *);
1452 static void c_parser_omp_taskyield (c_parser *);
1453 static void c_parser_omp_cancel (c_parser *);
1455 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1456 pragma_stmt, pragma_compound };
1457 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1458 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1459 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1460 static void c_parser_omp_end_declare_target (c_parser *);
1461 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1462 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1463 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1465 /* These Objective-C parser functions are only ever called when
1466 compiling Objective-C. */
1467 static void c_parser_objc_class_definition (c_parser *, tree);
1468 static void c_parser_objc_class_instance_variables (c_parser *);
1469 static void c_parser_objc_class_declaration (c_parser *);
1470 static void c_parser_objc_alias_declaration (c_parser *);
1471 static void c_parser_objc_protocol_definition (c_parser *, tree);
1472 static bool c_parser_objc_method_type (c_parser *);
1473 static void c_parser_objc_method_definition (c_parser *);
1474 static void c_parser_objc_methodprotolist (c_parser *);
1475 static void c_parser_objc_methodproto (c_parser *);
1476 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1477 static tree c_parser_objc_type_name (c_parser *);
1478 static tree c_parser_objc_protocol_refs (c_parser *);
1479 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1480 static void c_parser_objc_synchronized_statement (c_parser *);
1481 static tree c_parser_objc_selector (c_parser *);
1482 static tree c_parser_objc_selector_arg (c_parser *);
1483 static tree c_parser_objc_receiver (c_parser *);
1484 static tree c_parser_objc_message_args (c_parser *);
1485 static tree c_parser_objc_keywordexpr (c_parser *);
1486 static void c_parser_objc_at_property_declaration (c_parser *);
1487 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1488 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1489 static bool c_parser_objc_diagnose_bad_element_prefix
1490 (c_parser *, struct c_declspecs *);
1492 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1494 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1496 translation-unit:
1497 external-declarations
1499 external-declarations:
1500 external-declaration
1501 external-declarations external-declaration
1503 GNU extensions:
1505 translation-unit:
1506 empty
1509 static void
1510 c_parser_translation_unit (c_parser *parser)
1512 if (c_parser_next_token_is (parser, CPP_EOF))
1514 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1515 "ISO C forbids an empty translation unit");
1517 else
1519 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1520 mark_valid_location_for_stdc_pragma (false);
1523 ggc_collect ();
1524 c_parser_external_declaration (parser);
1525 obstack_free (&parser_obstack, obstack_position);
1527 while (c_parser_next_token_is_not (parser, CPP_EOF));
1530 unsigned int i;
1531 tree decl;
1532 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1533 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1534 error ("storage size of %q+D isn%'t known", decl);
1537 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1539 external-declaration:
1540 function-definition
1541 declaration
1543 GNU extensions:
1545 external-declaration:
1546 asm-definition
1548 __extension__ external-declaration
1550 Objective-C:
1552 external-declaration:
1553 objc-class-definition
1554 objc-class-declaration
1555 objc-alias-declaration
1556 objc-protocol-definition
1557 objc-method-definition
1558 @end
1561 static void
1562 c_parser_external_declaration (c_parser *parser)
1564 int ext;
1565 switch (c_parser_peek_token (parser)->type)
1567 case CPP_KEYWORD:
1568 switch (c_parser_peek_token (parser)->keyword)
1570 case RID_EXTENSION:
1571 ext = disable_extension_diagnostics ();
1572 c_parser_consume_token (parser);
1573 c_parser_external_declaration (parser);
1574 restore_extension_diagnostics (ext);
1575 break;
1576 case RID_ASM:
1577 c_parser_asm_definition (parser);
1578 break;
1579 case RID_AT_INTERFACE:
1580 case RID_AT_IMPLEMENTATION:
1581 gcc_assert (c_dialect_objc ());
1582 c_parser_objc_class_definition (parser, NULL_TREE);
1583 break;
1584 case RID_AT_CLASS:
1585 gcc_assert (c_dialect_objc ());
1586 c_parser_objc_class_declaration (parser);
1587 break;
1588 case RID_AT_ALIAS:
1589 gcc_assert (c_dialect_objc ());
1590 c_parser_objc_alias_declaration (parser);
1591 break;
1592 case RID_AT_PROTOCOL:
1593 gcc_assert (c_dialect_objc ());
1594 c_parser_objc_protocol_definition (parser, NULL_TREE);
1595 break;
1596 case RID_AT_PROPERTY:
1597 gcc_assert (c_dialect_objc ());
1598 c_parser_objc_at_property_declaration (parser);
1599 break;
1600 case RID_AT_SYNTHESIZE:
1601 gcc_assert (c_dialect_objc ());
1602 c_parser_objc_at_synthesize_declaration (parser);
1603 break;
1604 case RID_AT_DYNAMIC:
1605 gcc_assert (c_dialect_objc ());
1606 c_parser_objc_at_dynamic_declaration (parser);
1607 break;
1608 case RID_AT_END:
1609 gcc_assert (c_dialect_objc ());
1610 c_parser_consume_token (parser);
1611 objc_finish_implementation ();
1612 break;
1613 default:
1614 goto decl_or_fndef;
1616 break;
1617 case CPP_SEMICOLON:
1618 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1619 "ISO C does not allow extra %<;%> outside of a function");
1620 c_parser_consume_token (parser);
1621 break;
1622 case CPP_PRAGMA:
1623 mark_valid_location_for_stdc_pragma (true);
1624 c_parser_pragma (parser, pragma_external, NULL);
1625 mark_valid_location_for_stdc_pragma (false);
1626 break;
1627 case CPP_PLUS:
1628 case CPP_MINUS:
1629 if (c_dialect_objc ())
1631 c_parser_objc_method_definition (parser);
1632 break;
1634 /* Else fall through, and yield a syntax error trying to parse
1635 as a declaration or function definition. */
1636 /* FALLTHRU */
1637 default:
1638 decl_or_fndef:
1639 /* A declaration or a function definition (or, in Objective-C,
1640 an @interface or @protocol with prefix attributes). We can
1641 only tell which after parsing the declaration specifiers, if
1642 any, and the first declarator. */
1643 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1644 NULL, vNULL);
1645 break;
1649 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1650 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1652 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
1654 static void
1655 add_debug_begin_stmt (location_t loc)
1657 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
1658 return;
1660 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1661 SET_EXPR_LOCATION (stmt, loc);
1662 add_stmt (stmt);
1665 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1666 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1667 is accepted; otherwise (old-style parameter declarations) only other
1668 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1669 assertion is accepted; otherwise (old-style parameter declarations)
1670 it is not. If NESTED is true, we are inside a function or parsing
1671 old-style parameter declarations; any functions encountered are
1672 nested functions and declaration specifiers are required; otherwise
1673 we are at top level and functions are normal functions and
1674 declaration specifiers may be optional. If EMPTY_OK is true, empty
1675 declarations are OK (subject to all other constraints); otherwise
1676 (old-style parameter declarations) they are diagnosed. If
1677 START_ATTR_OK is true, the declaration specifiers may start with
1678 attributes; otherwise they may not.
1679 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1680 declaration when parsing an Objective-C foreach statement.
1681 FALLTHRU_ATTR_P is used to signal whether this function parsed
1682 "__attribute__((fallthrough));".
1684 declaration:
1685 declaration-specifiers init-declarator-list[opt] ;
1686 static_assert-declaration
1688 function-definition:
1689 declaration-specifiers[opt] declarator declaration-list[opt]
1690 compound-statement
1692 declaration-list:
1693 declaration
1694 declaration-list declaration
1696 init-declarator-list:
1697 init-declarator
1698 init-declarator-list , init-declarator
1700 init-declarator:
1701 declarator simple-asm-expr[opt] attributes[opt]
1702 declarator simple-asm-expr[opt] attributes[opt] = initializer
1704 GNU extensions:
1706 nested-function-definition:
1707 declaration-specifiers declarator declaration-list[opt]
1708 compound-statement
1710 attribute ;
1712 Objective-C:
1713 attributes objc-class-definition
1714 attributes objc-category-definition
1715 attributes objc-protocol-definition
1717 The simple-asm-expr and attributes are GNU extensions.
1719 This function does not handle __extension__; that is handled in its
1720 callers. ??? Following the old parser, __extension__ may start
1721 external declarations, declarations in functions and declarations
1722 at the start of "for" loops, but not old-style parameter
1723 declarations.
1725 C99 requires declaration specifiers in a function definition; the
1726 absence is diagnosed through the diagnosis of implicit int. In GNU
1727 C we also allow but diagnose declarations without declaration
1728 specifiers, but only at top level (elsewhere they conflict with
1729 other syntax).
1731 In Objective-C, declarations of the looping variable in a foreach
1732 statement are exceptionally terminated by 'in' (for example, 'for
1733 (NSObject *object in array) { ... }').
1735 OpenMP:
1737 declaration:
1738 threadprivate-directive
1740 GIMPLE:
1742 gimple-function-definition:
1743 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1744 declaration-list[opt] compound-statement
1746 rtl-function-definition:
1747 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1748 declaration-list[opt] compound-statement */
1750 static void
1751 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1752 bool static_assert_ok, bool empty_ok,
1753 bool nested, bool start_attr_ok,
1754 tree *objc_foreach_object_declaration,
1755 vec<c_token> omp_declare_simd_clauses,
1756 struct oacc_routine_data *oacc_routine_data,
1757 bool *fallthru_attr_p)
1759 struct c_declspecs *specs;
1760 tree prefix_attrs;
1761 tree all_prefix_attrs;
1762 bool diagnosed_no_specs = false;
1763 location_t here = c_parser_peek_token (parser)->location;
1765 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1767 if (static_assert_ok
1768 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1770 c_parser_static_assert_declaration (parser);
1771 return;
1773 specs = build_null_declspecs ();
1775 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1776 if (c_parser_peek_token (parser)->type == CPP_NAME
1777 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1778 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1779 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1780 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1782 tree name = c_parser_peek_token (parser)->value;
1784 /* Issue a warning about NAME being an unknown type name, perhaps
1785 with some kind of hint.
1786 If the user forgot a "struct" etc, suggest inserting
1787 it. Otherwise, attempt to look for misspellings. */
1788 gcc_rich_location richloc (here);
1789 if (tag_exists_p (RECORD_TYPE, name))
1791 /* This is not C++ with its implicit typedef. */
1792 richloc.add_fixit_insert_before ("struct ");
1793 error_at (&richloc,
1794 "unknown type name %qE;"
1795 " use %<struct%> keyword to refer to the type",
1796 name);
1798 else if (tag_exists_p (UNION_TYPE, name))
1800 richloc.add_fixit_insert_before ("union ");
1801 error_at (&richloc,
1802 "unknown type name %qE;"
1803 " use %<union%> keyword to refer to the type",
1804 name);
1806 else if (tag_exists_p (ENUMERAL_TYPE, name))
1808 richloc.add_fixit_insert_before ("enum ");
1809 error_at (&richloc,
1810 "unknown type name %qE;"
1811 " use %<enum%> keyword to refer to the type",
1812 name);
1814 else
1816 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1817 here);
1818 if (hint)
1820 richloc.add_fixit_replace (hint.suggestion ());
1821 error_at (&richloc,
1822 "unknown type name %qE; did you mean %qs?",
1823 name, hint.suggestion ());
1825 else
1826 error_at (here, "unknown type name %qE", name);
1829 /* Parse declspecs normally to get a correct pointer type, but avoid
1830 a further "fails to be a type name" error. Refuse nested functions
1831 since it is not how the user likely wants us to recover. */
1832 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1833 c_parser_peek_token (parser)->keyword = RID_VOID;
1834 c_parser_peek_token (parser)->value = error_mark_node;
1835 fndef_ok = !nested;
1838 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1839 true, true, cla_nonabstract_decl);
1840 if (parser->error)
1842 c_parser_skip_to_end_of_block_or_statement (parser);
1843 return;
1845 if (nested && !specs->declspecs_seen_p)
1847 c_parser_error (parser, "expected declaration specifiers");
1848 c_parser_skip_to_end_of_block_or_statement (parser);
1849 return;
1852 finish_declspecs (specs);
1853 bool auto_type_p = specs->typespec_word == cts_auto_type;
1854 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1856 if (auto_type_p)
1857 error_at (here, "%<__auto_type%> in empty declaration");
1858 else if (specs->typespec_kind == ctsk_none
1859 && attribute_fallthrough_p (specs->attrs))
1861 if (fallthru_attr_p != NULL)
1862 *fallthru_attr_p = true;
1863 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1864 void_type_node, 0);
1865 add_stmt (fn);
1867 else if (empty_ok)
1868 shadow_tag (specs);
1869 else
1871 shadow_tag_warned (specs, 1);
1872 pedwarn (here, 0, "empty declaration");
1874 c_parser_consume_token (parser);
1875 if (oacc_routine_data)
1876 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1877 return;
1880 /* Provide better error recovery. Note that a type name here is usually
1881 better diagnosed as a redeclaration. */
1882 if (empty_ok
1883 && specs->typespec_kind == ctsk_tagdef
1884 && c_parser_next_token_starts_declspecs (parser)
1885 && !c_parser_next_token_is (parser, CPP_NAME))
1887 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1888 parser->error = false;
1889 shadow_tag_warned (specs, 1);
1890 return;
1892 else if (c_dialect_objc () && !auto_type_p)
1894 /* Prefix attributes are an error on method decls. */
1895 switch (c_parser_peek_token (parser)->type)
1897 case CPP_PLUS:
1898 case CPP_MINUS:
1899 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1900 return;
1901 if (specs->attrs)
1903 warning_at (c_parser_peek_token (parser)->location,
1904 OPT_Wattributes,
1905 "prefix attributes are ignored for methods");
1906 specs->attrs = NULL_TREE;
1908 if (fndef_ok)
1909 c_parser_objc_method_definition (parser);
1910 else
1911 c_parser_objc_methodproto (parser);
1912 return;
1913 break;
1914 default:
1915 break;
1917 /* This is where we parse 'attributes @interface ...',
1918 'attributes @implementation ...', 'attributes @protocol ...'
1919 (where attributes could be, for example, __attribute__
1920 ((deprecated)).
1922 switch (c_parser_peek_token (parser)->keyword)
1924 case RID_AT_INTERFACE:
1926 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1927 return;
1928 c_parser_objc_class_definition (parser, specs->attrs);
1929 return;
1931 break;
1932 case RID_AT_IMPLEMENTATION:
1934 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1935 return;
1936 if (specs->attrs)
1938 warning_at (c_parser_peek_token (parser)->location,
1939 OPT_Wattributes,
1940 "prefix attributes are ignored for implementations");
1941 specs->attrs = NULL_TREE;
1943 c_parser_objc_class_definition (parser, NULL_TREE);
1944 return;
1946 break;
1947 case RID_AT_PROTOCOL:
1949 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1950 return;
1951 c_parser_objc_protocol_definition (parser, specs->attrs);
1952 return;
1954 break;
1955 case RID_AT_ALIAS:
1956 case RID_AT_CLASS:
1957 case RID_AT_END:
1958 case RID_AT_PROPERTY:
1959 if (specs->attrs)
1961 c_parser_error (parser, "unexpected attribute");
1962 specs->attrs = NULL;
1964 break;
1965 default:
1966 break;
1969 else if (attribute_fallthrough_p (specs->attrs))
1970 warning_at (here, OPT_Wattributes,
1971 "%<fallthrough%> attribute not followed by %<;%>");
1973 pending_xref_error ();
1974 prefix_attrs = specs->attrs;
1975 all_prefix_attrs = prefix_attrs;
1976 specs->attrs = NULL_TREE;
1977 while (true)
1979 struct c_declarator *declarator;
1980 bool dummy = false;
1981 timevar_id_t tv;
1982 tree fnbody = NULL_TREE;
1983 /* Declaring either one or more declarators (in which case we
1984 should diagnose if there were no declaration specifiers) or a
1985 function definition (in which case the diagnostic for
1986 implicit int suffices). */
1987 declarator = c_parser_declarator (parser,
1988 specs->typespec_kind != ctsk_none,
1989 C_DTR_NORMAL, &dummy);
1990 if (declarator == NULL)
1992 if (omp_declare_simd_clauses.exists ())
1993 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1994 omp_declare_simd_clauses);
1995 if (oacc_routine_data)
1996 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1997 c_parser_skip_to_end_of_block_or_statement (parser);
1998 return;
2000 if (auto_type_p && declarator->kind != cdk_id)
2002 error_at (here,
2003 "%<__auto_type%> requires a plain identifier"
2004 " as declarator");
2005 c_parser_skip_to_end_of_block_or_statement (parser);
2006 return;
2008 if (c_parser_next_token_is (parser, CPP_EQ)
2009 || c_parser_next_token_is (parser, CPP_COMMA)
2010 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2011 || c_parser_next_token_is_keyword (parser, RID_ASM)
2012 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2013 || c_parser_next_token_is_keyword (parser, RID_IN))
2015 tree asm_name = NULL_TREE;
2016 tree postfix_attrs = NULL_TREE;
2017 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2019 diagnosed_no_specs = true;
2020 pedwarn (here, 0, "data definition has no type or storage class");
2022 /* Having seen a data definition, there cannot now be a
2023 function definition. */
2024 fndef_ok = false;
2025 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2026 asm_name = c_parser_simple_asm_expr (parser);
2027 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2029 postfix_attrs = c_parser_attributes (parser);
2030 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2032 /* This means there is an attribute specifier after
2033 the declarator in a function definition. Provide
2034 some more information for the user. */
2035 error_at (here, "attributes should be specified before the "
2036 "declarator in a function definition");
2037 c_parser_skip_to_end_of_block_or_statement (parser);
2038 return;
2041 if (c_parser_next_token_is (parser, CPP_EQ))
2043 tree d;
2044 struct c_expr init;
2045 location_t init_loc;
2046 c_parser_consume_token (parser);
2047 if (auto_type_p)
2049 init_loc = c_parser_peek_token (parser)->location;
2050 rich_location richloc (line_table, init_loc);
2051 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2052 /* A parameter is initialized, which is invalid. Don't
2053 attempt to instrument the initializer. */
2054 int flag_sanitize_save = flag_sanitize;
2055 if (nested && !empty_ok)
2056 flag_sanitize = 0;
2057 init = c_parser_expr_no_commas (parser, NULL);
2058 flag_sanitize = flag_sanitize_save;
2059 if (TREE_CODE (init.value) == COMPONENT_REF
2060 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2061 error_at (here,
2062 "%<__auto_type%> used with a bit-field"
2063 " initializer");
2064 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2065 tree init_type = TREE_TYPE (init.value);
2066 /* As with typeof, remove all qualifiers from atomic types. */
2067 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2068 init_type
2069 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2070 bool vm_type = variably_modified_type_p (init_type,
2071 NULL_TREE);
2072 if (vm_type)
2073 init.value = save_expr (init.value);
2074 finish_init ();
2075 specs->typespec_kind = ctsk_typeof;
2076 specs->locations[cdw_typedef] = init_loc;
2077 specs->typedef_p = true;
2078 specs->type = init_type;
2079 if (vm_type)
2081 bool maybe_const = true;
2082 tree type_expr = c_fully_fold (init.value, false,
2083 &maybe_const);
2084 specs->expr_const_operands &= maybe_const;
2085 if (specs->expr)
2086 specs->expr = build2 (COMPOUND_EXPR,
2087 TREE_TYPE (type_expr),
2088 specs->expr, type_expr);
2089 else
2090 specs->expr = type_expr;
2092 d = start_decl (declarator, specs, true,
2093 chainon (postfix_attrs, all_prefix_attrs));
2094 if (!d)
2095 d = error_mark_node;
2096 if (omp_declare_simd_clauses.exists ())
2097 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2098 omp_declare_simd_clauses);
2100 else
2102 /* The declaration of the variable is in effect while
2103 its initializer is parsed. */
2104 d = start_decl (declarator, specs, true,
2105 chainon (postfix_attrs, all_prefix_attrs));
2106 if (!d)
2107 d = error_mark_node;
2108 if (omp_declare_simd_clauses.exists ())
2109 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2110 omp_declare_simd_clauses);
2111 init_loc = c_parser_peek_token (parser)->location;
2112 rich_location richloc (line_table, init_loc);
2113 start_init (d, asm_name, global_bindings_p (), &richloc);
2114 /* A parameter is initialized, which is invalid. Don't
2115 attempt to instrument the initializer. */
2116 int flag_sanitize_save = flag_sanitize;
2117 if (TREE_CODE (d) == PARM_DECL)
2118 flag_sanitize = 0;
2119 init = c_parser_initializer (parser);
2120 flag_sanitize = flag_sanitize_save;
2121 finish_init ();
2123 if (oacc_routine_data)
2124 c_finish_oacc_routine (oacc_routine_data, d, false);
2125 if (d != error_mark_node)
2127 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2128 finish_decl (d, init_loc, init.value,
2129 init.original_type, asm_name);
2132 else
2134 if (auto_type_p)
2136 error_at (here,
2137 "%<__auto_type%> requires an initialized "
2138 "data declaration");
2139 c_parser_skip_to_end_of_block_or_statement (parser);
2140 return;
2142 tree d = start_decl (declarator, specs, false,
2143 chainon (postfix_attrs,
2144 all_prefix_attrs));
2145 if (d && TREE_CODE (d) == FUNCTION_DECL)
2146 if (declarator->kind == cdk_function)
2147 if (DECL_ARGUMENTS (d) == NULL_TREE)
2148 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2149 if (omp_declare_simd_clauses.exists ())
2151 tree parms = NULL_TREE;
2152 if (d && TREE_CODE (d) == FUNCTION_DECL)
2154 struct c_declarator *ce = declarator;
2155 while (ce != NULL)
2156 if (ce->kind == cdk_function)
2158 parms = ce->u.arg_info->parms;
2159 break;
2161 else
2162 ce = ce->declarator;
2164 if (parms)
2165 temp_store_parm_decls (d, parms);
2166 c_finish_omp_declare_simd (parser, d, parms,
2167 omp_declare_simd_clauses);
2168 if (parms)
2169 temp_pop_parm_decls ();
2171 if (oacc_routine_data)
2172 c_finish_oacc_routine (oacc_routine_data, d, false);
2173 if (d)
2174 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2175 NULL_TREE, asm_name);
2177 if (c_parser_next_token_is_keyword (parser, RID_IN))
2179 if (d)
2180 *objc_foreach_object_declaration = d;
2181 else
2182 *objc_foreach_object_declaration = error_mark_node;
2185 if (c_parser_next_token_is (parser, CPP_COMMA))
2187 if (auto_type_p)
2189 error_at (here,
2190 "%<__auto_type%> may only be used with"
2191 " a single declarator");
2192 c_parser_skip_to_end_of_block_or_statement (parser);
2193 return;
2195 c_parser_consume_token (parser);
2196 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2197 all_prefix_attrs = chainon (c_parser_attributes (parser),
2198 prefix_attrs);
2199 else
2200 all_prefix_attrs = prefix_attrs;
2201 continue;
2203 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2205 c_parser_consume_token (parser);
2206 return;
2208 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2210 /* This can only happen in Objective-C: we found the
2211 'in' that terminates the declaration inside an
2212 Objective-C foreach statement. Do not consume the
2213 token, so that the caller can use it to determine
2214 that this indeed is a foreach context. */
2215 return;
2217 else
2219 c_parser_error (parser, "expected %<,%> or %<;%>");
2220 c_parser_skip_to_end_of_block_or_statement (parser);
2221 return;
2224 else if (auto_type_p)
2226 error_at (here,
2227 "%<__auto_type%> requires an initialized data declaration");
2228 c_parser_skip_to_end_of_block_or_statement (parser);
2229 return;
2231 else if (!fndef_ok)
2233 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2234 "%<asm%> or %<__attribute__%>");
2235 c_parser_skip_to_end_of_block_or_statement (parser);
2236 return;
2238 /* Function definition (nested or otherwise). */
2239 if (nested)
2241 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2242 c_push_function_context ();
2244 if (!start_function (specs, declarator, all_prefix_attrs))
2246 /* At this point we've consumed:
2247 declaration-specifiers declarator
2248 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2249 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2250 but the
2251 declaration-specifiers declarator
2252 aren't grokkable as a function definition, so we have
2253 an error. */
2254 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2255 if (c_parser_next_token_starts_declspecs (parser))
2257 /* If we have
2258 declaration-specifiers declarator decl-specs
2259 then assume we have a missing semicolon, which would
2260 give us:
2261 declaration-specifiers declarator decl-specs
2264 <~~~~~~~~~ declaration ~~~~~~~~~~>
2265 Use c_parser_require to get an error with a fix-it hint. */
2266 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2267 parser->error = false;
2269 else
2271 /* This can appear in many cases looking nothing like a
2272 function definition, so we don't give a more specific
2273 error suggesting there was one. */
2274 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2275 "or %<__attribute__%>");
2277 if (nested)
2278 c_pop_function_context ();
2279 break;
2282 if (DECL_DECLARED_INLINE_P (current_function_decl))
2283 tv = TV_PARSE_INLINE;
2284 else
2285 tv = TV_PARSE_FUNC;
2286 auto_timevar at (g_timer, tv);
2288 /* Parse old-style parameter declarations. ??? Attributes are
2289 not allowed to start declaration specifiers here because of a
2290 syntax conflict between a function declaration with attribute
2291 suffix and a function definition with an attribute prefix on
2292 first old-style parameter declaration. Following the old
2293 parser, they are not accepted on subsequent old-style
2294 parameter declarations either. However, there is no
2295 ambiguity after the first declaration, nor indeed on the
2296 first as long as we don't allow postfix attributes after a
2297 declarator with a nonempty identifier list in a definition;
2298 and postfix attributes have never been accepted here in
2299 function definitions either. */
2300 while (c_parser_next_token_is_not (parser, CPP_EOF)
2301 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2302 c_parser_declaration_or_fndef (parser, false, false, false,
2303 true, false, NULL, vNULL);
2304 store_parm_decls ();
2305 if (omp_declare_simd_clauses.exists ())
2306 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2307 omp_declare_simd_clauses);
2308 if (oacc_routine_data)
2309 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2310 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2311 = c_parser_peek_token (parser)->location;
2313 /* If the definition was marked with __GIMPLE then parse the
2314 function body as GIMPLE. */
2315 if (specs->gimple_p)
2317 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2318 bool saved = in_late_binary_op;
2319 in_late_binary_op = true;
2320 c_parser_parse_gimple_body (parser);
2321 in_late_binary_op = saved;
2323 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2324 consuming the function body. */
2325 else if (specs->rtl_p)
2327 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2329 /* Normally, store_parm_decls sets next_is_function_body,
2330 anticipating a function body. We need a push_scope/pop_scope
2331 pair to flush out this state, or subsequent function parsing
2332 will go wrong. */
2333 push_scope ();
2334 pop_scope ();
2336 finish_function ();
2337 return;
2339 else
2340 fnbody = c_parser_compound_statement (parser);
2341 tree fndecl = current_function_decl;
2342 if (nested)
2344 tree decl = current_function_decl;
2345 /* Mark nested functions as needing static-chain initially.
2346 lower_nested_functions will recompute it but the
2347 DECL_STATIC_CHAIN flag is also used before that happens,
2348 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2349 DECL_STATIC_CHAIN (decl) = 1;
2350 add_stmt (fnbody);
2351 finish_function ();
2352 c_pop_function_context ();
2353 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2355 else
2357 if (fnbody)
2358 add_stmt (fnbody);
2359 finish_function ();
2361 /* Get rid of the empty stmt list for GIMPLE. */
2362 if (specs->gimple_p)
2363 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2365 break;
2369 /* Parse an asm-definition (asm() outside a function body). This is a
2370 GNU extension.
2372 asm-definition:
2373 simple-asm-expr ;
2376 static void
2377 c_parser_asm_definition (c_parser *parser)
2379 tree asm_str = c_parser_simple_asm_expr (parser);
2380 if (asm_str)
2381 symtab->finalize_toplevel_asm (asm_str);
2382 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2385 /* Parse a static assertion (C11 6.7.10).
2387 static_assert-declaration:
2388 static_assert-declaration-no-semi ;
2391 static void
2392 c_parser_static_assert_declaration (c_parser *parser)
2394 c_parser_static_assert_declaration_no_semi (parser);
2395 if (parser->error
2396 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2397 c_parser_skip_to_end_of_block_or_statement (parser);
2400 /* Parse a static assertion (C11 6.7.10), without the trailing
2401 semicolon.
2403 static_assert-declaration-no-semi:
2404 _Static_assert ( constant-expression , string-literal )
2407 static void
2408 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2410 location_t assert_loc, value_loc;
2411 tree value;
2412 tree string;
2414 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2415 assert_loc = c_parser_peek_token (parser)->location;
2416 if (flag_isoc99)
2417 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2418 "ISO C99 does not support %<_Static_assert%>");
2419 else
2420 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2421 "ISO C90 does not support %<_Static_assert%>");
2422 c_parser_consume_token (parser);
2423 matching_parens parens;
2424 if (!parens.require_open (parser))
2425 return;
2426 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2427 value = c_parser_expr_no_commas (parser, NULL).value;
2428 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2429 parser->lex_untranslated_string = true;
2430 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2432 parser->lex_untranslated_string = false;
2433 return;
2435 switch (c_parser_peek_token (parser)->type)
2437 case CPP_STRING:
2438 case CPP_STRING16:
2439 case CPP_STRING32:
2440 case CPP_WSTRING:
2441 case CPP_UTF8STRING:
2442 string = c_parser_peek_token (parser)->value;
2443 c_parser_consume_token (parser);
2444 parser->lex_untranslated_string = false;
2445 break;
2446 default:
2447 c_parser_error (parser, "expected string literal");
2448 parser->lex_untranslated_string = false;
2449 return;
2451 parens.require_close (parser);
2453 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2455 error_at (value_loc, "expression in static assertion is not an integer");
2456 return;
2458 if (TREE_CODE (value) != INTEGER_CST)
2460 value = c_fully_fold (value, false, NULL);
2461 /* Strip no-op conversions. */
2462 STRIP_TYPE_NOPS (value);
2463 if (TREE_CODE (value) == INTEGER_CST)
2464 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2465 "is not an integer constant expression");
2467 if (TREE_CODE (value) != INTEGER_CST)
2469 error_at (value_loc, "expression in static assertion is not constant");
2470 return;
2472 constant_expression_warning (value);
2473 if (integer_zerop (value))
2474 error_at (assert_loc, "static assertion failed: %E", string);
2477 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2478 6.7, C11 6.7), adding them to SPECS (which may already include some).
2479 Storage class specifiers are accepted iff SCSPEC_OK; type
2480 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2481 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2482 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2484 declaration-specifiers:
2485 storage-class-specifier declaration-specifiers[opt]
2486 type-specifier declaration-specifiers[opt]
2487 type-qualifier declaration-specifiers[opt]
2488 function-specifier declaration-specifiers[opt]
2489 alignment-specifier declaration-specifiers[opt]
2491 Function specifiers (inline) are from C99, and are currently
2492 handled as storage class specifiers, as is __thread. Alignment
2493 specifiers are from C11.
2495 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2496 storage-class-specifier:
2497 typedef
2498 extern
2499 static
2500 auto
2501 register
2502 _Thread_local
2504 (_Thread_local is new in C11.)
2506 C99 6.7.4, C11 6.7.4:
2507 function-specifier:
2508 inline
2509 _Noreturn
2511 (_Noreturn is new in C11.)
2513 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2514 type-specifier:
2515 void
2516 char
2517 short
2519 long
2520 float
2521 double
2522 signed
2523 unsigned
2524 _Bool
2525 _Complex
2526 [_Imaginary removed in C99 TC2]
2527 struct-or-union-specifier
2528 enum-specifier
2529 typedef-name
2530 atomic-type-specifier
2532 (_Bool and _Complex are new in C99.)
2533 (atomic-type-specifier is new in C11.)
2535 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2537 type-qualifier:
2538 const
2539 restrict
2540 volatile
2541 address-space-qualifier
2542 _Atomic
2544 (restrict is new in C99.)
2545 (_Atomic is new in C11.)
2547 GNU extensions:
2549 declaration-specifiers:
2550 attributes declaration-specifiers[opt]
2552 type-qualifier:
2553 address-space
2555 address-space:
2556 identifier recognized by the target
2558 storage-class-specifier:
2559 __thread
2561 type-specifier:
2562 typeof-specifier
2563 __auto_type
2564 __intN
2565 _Decimal32
2566 _Decimal64
2567 _Decimal128
2568 _Fract
2569 _Accum
2570 _Sat
2572 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2573 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2575 atomic-type-specifier
2576 _Atomic ( type-name )
2578 Objective-C:
2580 type-specifier:
2581 class-name objc-protocol-refs[opt]
2582 typedef-name objc-protocol-refs
2583 objc-protocol-refs
2586 void
2587 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2588 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2589 bool alignspec_ok, bool auto_type_ok,
2590 enum c_lookahead_kind la)
2592 bool attrs_ok = start_attr_ok;
2593 bool seen_type = specs->typespec_kind != ctsk_none;
2595 if (!typespec_ok)
2596 gcc_assert (la == cla_prefer_id);
2598 while (c_parser_next_token_is (parser, CPP_NAME)
2599 || c_parser_next_token_is (parser, CPP_KEYWORD)
2600 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2602 struct c_typespec t;
2603 tree attrs;
2604 tree align;
2605 location_t loc = c_parser_peek_token (parser)->location;
2607 /* If we cannot accept a type, exit if the next token must start
2608 one. Also, if we already have seen a tagged definition,
2609 a typename would be an error anyway and likely the user
2610 has simply forgotten a semicolon, so we exit. */
2611 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2612 && c_parser_next_tokens_start_typename (parser, la)
2613 && !c_parser_next_token_is_qualifier (parser)
2614 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2615 break;
2617 if (c_parser_next_token_is (parser, CPP_NAME))
2619 c_token *name_token = c_parser_peek_token (parser);
2620 tree value = name_token->value;
2621 c_id_kind kind = name_token->id_kind;
2623 if (kind == C_ID_ADDRSPACE)
2625 addr_space_t as
2626 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2627 declspecs_add_addrspace (name_token->location, specs, as);
2628 c_parser_consume_token (parser);
2629 attrs_ok = true;
2630 continue;
2633 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2635 /* If we cannot accept a type, and the next token must start one,
2636 exit. Do the same if we already have seen a tagged definition,
2637 since it would be an error anyway and likely the user has simply
2638 forgotten a semicolon. */
2639 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2640 break;
2642 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2643 a C_ID_CLASSNAME. */
2644 c_parser_consume_token (parser);
2645 seen_type = true;
2646 attrs_ok = true;
2647 if (kind == C_ID_ID)
2649 error_at (loc, "unknown type name %qE", value);
2650 t.kind = ctsk_typedef;
2651 t.spec = error_mark_node;
2653 else if (kind == C_ID_TYPENAME
2654 && (!c_dialect_objc ()
2655 || c_parser_next_token_is_not (parser, CPP_LESS)))
2657 t.kind = ctsk_typedef;
2658 /* For a typedef name, record the meaning, not the name.
2659 In case of 'foo foo, bar;'. */
2660 t.spec = lookup_name (value);
2662 else
2664 tree proto = NULL_TREE;
2665 gcc_assert (c_dialect_objc ());
2666 t.kind = ctsk_objc;
2667 if (c_parser_next_token_is (parser, CPP_LESS))
2668 proto = c_parser_objc_protocol_refs (parser);
2669 t.spec = objc_get_protocol_qualified_type (value, proto);
2671 t.expr = NULL_TREE;
2672 t.expr_const_operands = true;
2673 declspecs_add_type (name_token->location, specs, t);
2674 continue;
2676 if (c_parser_next_token_is (parser, CPP_LESS))
2678 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2679 nisse@lysator.liu.se. */
2680 tree proto;
2681 gcc_assert (c_dialect_objc ());
2682 if (!typespec_ok || seen_type)
2683 break;
2684 proto = c_parser_objc_protocol_refs (parser);
2685 t.kind = ctsk_objc;
2686 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2687 t.expr = NULL_TREE;
2688 t.expr_const_operands = true;
2689 declspecs_add_type (loc, specs, t);
2690 continue;
2692 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2693 switch (c_parser_peek_token (parser)->keyword)
2695 case RID_STATIC:
2696 case RID_EXTERN:
2697 case RID_REGISTER:
2698 case RID_TYPEDEF:
2699 case RID_INLINE:
2700 case RID_NORETURN:
2701 case RID_AUTO:
2702 case RID_THREAD:
2703 if (!scspec_ok)
2704 goto out;
2705 attrs_ok = true;
2706 /* TODO: Distinguish between function specifiers (inline, noreturn)
2707 and storage class specifiers, either here or in
2708 declspecs_add_scspec. */
2709 declspecs_add_scspec (loc, specs,
2710 c_parser_peek_token (parser)->value);
2711 c_parser_consume_token (parser);
2712 break;
2713 case RID_AUTO_TYPE:
2714 if (!auto_type_ok)
2715 goto out;
2716 /* Fall through. */
2717 case RID_UNSIGNED:
2718 case RID_LONG:
2719 case RID_SHORT:
2720 case RID_SIGNED:
2721 case RID_COMPLEX:
2722 case RID_INT:
2723 case RID_CHAR:
2724 case RID_FLOAT:
2725 case RID_DOUBLE:
2726 case RID_VOID:
2727 case RID_DFLOAT32:
2728 case RID_DFLOAT64:
2729 case RID_DFLOAT128:
2730 CASE_RID_FLOATN_NX:
2731 case RID_BOOL:
2732 case RID_FRACT:
2733 case RID_ACCUM:
2734 case RID_SAT:
2735 case RID_INT_N_0:
2736 case RID_INT_N_1:
2737 case RID_INT_N_2:
2738 case RID_INT_N_3:
2739 if (!typespec_ok)
2740 goto out;
2741 attrs_ok = true;
2742 seen_type = true;
2743 if (c_dialect_objc ())
2744 parser->objc_need_raw_identifier = true;
2745 t.kind = ctsk_resword;
2746 t.spec = c_parser_peek_token (parser)->value;
2747 t.expr = NULL_TREE;
2748 t.expr_const_operands = true;
2749 declspecs_add_type (loc, specs, t);
2750 c_parser_consume_token (parser);
2751 break;
2752 case RID_ENUM:
2753 if (!typespec_ok)
2754 goto out;
2755 attrs_ok = true;
2756 seen_type = true;
2757 t = c_parser_enum_specifier (parser);
2758 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2759 declspecs_add_type (loc, specs, t);
2760 break;
2761 case RID_STRUCT:
2762 case RID_UNION:
2763 if (!typespec_ok)
2764 goto out;
2765 attrs_ok = true;
2766 seen_type = true;
2767 t = c_parser_struct_or_union_specifier (parser);
2768 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2769 declspecs_add_type (loc, specs, t);
2770 break;
2771 case RID_TYPEOF:
2772 /* ??? The old parser rejected typeof after other type
2773 specifiers, but is a syntax error the best way of
2774 handling this? */
2775 if (!typespec_ok || seen_type)
2776 goto out;
2777 attrs_ok = true;
2778 seen_type = true;
2779 t = c_parser_typeof_specifier (parser);
2780 declspecs_add_type (loc, specs, t);
2781 break;
2782 case RID_ATOMIC:
2783 /* C parser handling of Objective-C constructs needs
2784 checking for correct lvalue-to-rvalue conversions, and
2785 the code in build_modify_expr handling various
2786 Objective-C cases, and that in build_unary_op handling
2787 Objective-C cases for increment / decrement, also needs
2788 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2789 and objc_types_are_equivalent may also need updates. */
2790 if (c_dialect_objc ())
2791 sorry ("%<_Atomic%> in Objective-C");
2792 if (flag_isoc99)
2793 pedwarn_c99 (loc, OPT_Wpedantic,
2794 "ISO C99 does not support the %<_Atomic%> qualifier");
2795 else
2796 pedwarn_c99 (loc, OPT_Wpedantic,
2797 "ISO C90 does not support the %<_Atomic%> qualifier");
2798 attrs_ok = true;
2799 tree value;
2800 value = c_parser_peek_token (parser)->value;
2801 c_parser_consume_token (parser);
2802 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2804 /* _Atomic ( type-name ). */
2805 seen_type = true;
2806 c_parser_consume_token (parser);
2807 struct c_type_name *type = c_parser_type_name (parser);
2808 t.kind = ctsk_typeof;
2809 t.spec = error_mark_node;
2810 t.expr = NULL_TREE;
2811 t.expr_const_operands = true;
2812 if (type != NULL)
2813 t.spec = groktypename (type, &t.expr,
2814 &t.expr_const_operands);
2815 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2816 "expected %<)%>");
2817 if (t.spec != error_mark_node)
2819 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2820 error_at (loc, "%<_Atomic%>-qualified array type");
2821 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2822 error_at (loc, "%<_Atomic%>-qualified function type");
2823 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2824 error_at (loc, "%<_Atomic%> applied to a qualified type");
2825 else
2826 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2828 declspecs_add_type (loc, specs, t);
2830 else
2831 declspecs_add_qual (loc, specs, value);
2832 break;
2833 case RID_CONST:
2834 case RID_VOLATILE:
2835 case RID_RESTRICT:
2836 attrs_ok = true;
2837 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2838 c_parser_consume_token (parser);
2839 break;
2840 case RID_ATTRIBUTE:
2841 if (!attrs_ok)
2842 goto out;
2843 attrs = c_parser_attributes (parser);
2844 declspecs_add_attrs (loc, specs, attrs);
2845 break;
2846 case RID_ALIGNAS:
2847 if (!alignspec_ok)
2848 goto out;
2849 align = c_parser_alignas_specifier (parser);
2850 declspecs_add_alignas (loc, specs, align);
2851 break;
2852 case RID_GIMPLE:
2853 if (! flag_gimple)
2854 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2855 c_parser_consume_token (parser);
2856 specs->gimple_p = true;
2857 specs->locations[cdw_gimple] = loc;
2858 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2859 break;
2860 case RID_RTL:
2861 c_parser_consume_token (parser);
2862 specs->rtl_p = true;
2863 specs->locations[cdw_rtl] = loc;
2864 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2865 break;
2866 default:
2867 goto out;
2870 out: ;
2873 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2875 enum-specifier:
2876 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2877 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2878 enum attributes[opt] identifier
2880 The form with trailing comma is new in C99. The forms with
2881 attributes are GNU extensions. In GNU C, we accept any expression
2882 without commas in the syntax (assignment expressions, not just
2883 conditional expressions); assignment expressions will be diagnosed
2884 as non-constant.
2886 enumerator-list:
2887 enumerator
2888 enumerator-list , enumerator
2890 enumerator:
2891 enumeration-constant
2892 enumeration-constant = constant-expression
2894 GNU Extensions:
2896 enumerator:
2897 enumeration-constant attributes[opt]
2898 enumeration-constant attributes[opt] = constant-expression
2902 static struct c_typespec
2903 c_parser_enum_specifier (c_parser *parser)
2905 struct c_typespec ret;
2906 tree attrs;
2907 tree ident = NULL_TREE;
2908 location_t enum_loc;
2909 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2910 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2911 c_parser_consume_token (parser);
2912 attrs = c_parser_attributes (parser);
2913 enum_loc = c_parser_peek_token (parser)->location;
2914 /* Set the location in case we create a decl now. */
2915 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2916 if (c_parser_next_token_is (parser, CPP_NAME))
2918 ident = c_parser_peek_token (parser)->value;
2919 ident_loc = c_parser_peek_token (parser)->location;
2920 enum_loc = ident_loc;
2921 c_parser_consume_token (parser);
2923 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2925 /* Parse an enum definition. */
2926 struct c_enum_contents the_enum;
2927 tree type;
2928 tree postfix_attrs;
2929 /* We chain the enumerators in reverse order, then put them in
2930 forward order at the end. */
2931 tree values;
2932 timevar_push (TV_PARSE_ENUM);
2933 type = start_enum (enum_loc, &the_enum, ident);
2934 values = NULL_TREE;
2935 c_parser_consume_token (parser);
2936 while (true)
2938 tree enum_id;
2939 tree enum_value;
2940 tree enum_decl;
2941 bool seen_comma;
2942 c_token *token;
2943 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2944 location_t decl_loc, value_loc;
2945 if (c_parser_next_token_is_not (parser, CPP_NAME))
2947 /* Give a nicer error for "enum {}". */
2948 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2949 && !parser->error)
2951 error_at (c_parser_peek_token (parser)->location,
2952 "empty enum is invalid");
2953 parser->error = true;
2955 else
2956 c_parser_error (parser, "expected identifier");
2957 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2958 values = error_mark_node;
2959 break;
2961 token = c_parser_peek_token (parser);
2962 enum_id = token->value;
2963 /* Set the location in case we create a decl now. */
2964 c_parser_set_source_position_from_token (token);
2965 decl_loc = value_loc = token->location;
2966 c_parser_consume_token (parser);
2967 /* Parse any specified attributes. */
2968 tree enum_attrs = c_parser_attributes (parser);
2969 if (c_parser_next_token_is (parser, CPP_EQ))
2971 c_parser_consume_token (parser);
2972 value_loc = c_parser_peek_token (parser)->location;
2973 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2975 else
2976 enum_value = NULL_TREE;
2977 enum_decl = build_enumerator (decl_loc, value_loc,
2978 &the_enum, enum_id, enum_value);
2979 if (enum_attrs)
2980 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2981 TREE_CHAIN (enum_decl) = values;
2982 values = enum_decl;
2983 seen_comma = false;
2984 if (c_parser_next_token_is (parser, CPP_COMMA))
2986 comma_loc = c_parser_peek_token (parser)->location;
2987 seen_comma = true;
2988 c_parser_consume_token (parser);
2990 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2992 if (seen_comma)
2993 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2994 "comma at end of enumerator list");
2995 c_parser_consume_token (parser);
2996 break;
2998 if (!seen_comma)
3000 c_parser_error (parser, "expected %<,%> or %<}%>");
3001 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3002 values = error_mark_node;
3003 break;
3006 postfix_attrs = c_parser_attributes (parser);
3007 ret.spec = finish_enum (type, nreverse (values),
3008 chainon (attrs, postfix_attrs));
3009 ret.kind = ctsk_tagdef;
3010 ret.expr = NULL_TREE;
3011 ret.expr_const_operands = true;
3012 timevar_pop (TV_PARSE_ENUM);
3013 return ret;
3015 else if (!ident)
3017 c_parser_error (parser, "expected %<{%>");
3018 ret.spec = error_mark_node;
3019 ret.kind = ctsk_tagref;
3020 ret.expr = NULL_TREE;
3021 ret.expr_const_operands = true;
3022 return ret;
3024 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3025 /* In ISO C, enumerated types can be referred to only if already
3026 defined. */
3027 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3029 gcc_assert (ident);
3030 pedwarn (enum_loc, OPT_Wpedantic,
3031 "ISO C forbids forward references to %<enum%> types");
3033 return ret;
3036 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3038 struct-or-union-specifier:
3039 struct-or-union attributes[opt] identifier[opt]
3040 { struct-contents } attributes[opt]
3041 struct-or-union attributes[opt] identifier
3043 struct-contents:
3044 struct-declaration-list
3046 struct-declaration-list:
3047 struct-declaration ;
3048 struct-declaration-list struct-declaration ;
3050 GNU extensions:
3052 struct-contents:
3053 empty
3054 struct-declaration
3055 struct-declaration-list struct-declaration
3057 struct-declaration-list:
3058 struct-declaration-list ;
3061 (Note that in the syntax here, unlike that in ISO C, the semicolons
3062 are included here rather than in struct-declaration, in order to
3063 describe the syntax with extra semicolons and missing semicolon at
3064 end.)
3066 Objective-C:
3068 struct-declaration-list:
3069 @defs ( class-name )
3071 (Note this does not include a trailing semicolon, but can be
3072 followed by further declarations, and gets a pedwarn-if-pedantic
3073 when followed by a semicolon.) */
3075 static struct c_typespec
3076 c_parser_struct_or_union_specifier (c_parser *parser)
3078 struct c_typespec ret;
3079 tree attrs;
3080 tree ident = NULL_TREE;
3081 location_t struct_loc;
3082 location_t ident_loc = UNKNOWN_LOCATION;
3083 enum tree_code code;
3084 switch (c_parser_peek_token (parser)->keyword)
3086 case RID_STRUCT:
3087 code = RECORD_TYPE;
3088 break;
3089 case RID_UNION:
3090 code = UNION_TYPE;
3091 break;
3092 default:
3093 gcc_unreachable ();
3095 struct_loc = c_parser_peek_token (parser)->location;
3096 c_parser_consume_token (parser);
3097 attrs = c_parser_attributes (parser);
3099 /* Set the location in case we create a decl now. */
3100 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3102 if (c_parser_next_token_is (parser, CPP_NAME))
3104 ident = c_parser_peek_token (parser)->value;
3105 ident_loc = c_parser_peek_token (parser)->location;
3106 struct_loc = ident_loc;
3107 c_parser_consume_token (parser);
3109 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3111 /* Parse a struct or union definition. Start the scope of the
3112 tag before parsing components. */
3113 struct c_struct_parse_info *struct_info;
3114 tree type = start_struct (struct_loc, code, ident, &struct_info);
3115 tree postfix_attrs;
3116 /* We chain the components in reverse order, then put them in
3117 forward order at the end. Each struct-declaration may
3118 declare multiple components (comma-separated), so we must use
3119 chainon to join them, although when parsing each
3120 struct-declaration we can use TREE_CHAIN directly.
3122 The theory behind all this is that there will be more
3123 semicolon separated fields than comma separated fields, and
3124 so we'll be minimizing the number of node traversals required
3125 by chainon. */
3126 tree contents;
3127 timevar_push (TV_PARSE_STRUCT);
3128 contents = NULL_TREE;
3129 c_parser_consume_token (parser);
3130 /* Handle the Objective-C @defs construct,
3131 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3132 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3134 tree name;
3135 gcc_assert (c_dialect_objc ());
3136 c_parser_consume_token (parser);
3137 matching_parens parens;
3138 if (!parens.require_open (parser))
3139 goto end_at_defs;
3140 if (c_parser_next_token_is (parser, CPP_NAME)
3141 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3143 name = c_parser_peek_token (parser)->value;
3144 c_parser_consume_token (parser);
3146 else
3148 c_parser_error (parser, "expected class name");
3149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3150 goto end_at_defs;
3152 parens.skip_until_found_close (parser);
3153 contents = nreverse (objc_get_class_ivars (name));
3155 end_at_defs:
3156 /* Parse the struct-declarations and semicolons. Problems with
3157 semicolons are diagnosed here; empty structures are diagnosed
3158 elsewhere. */
3159 while (true)
3161 tree decls;
3162 /* Parse any stray semicolon. */
3163 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3165 location_t semicolon_loc
3166 = c_parser_peek_token (parser)->location;
3167 gcc_rich_location richloc (semicolon_loc);
3168 richloc.add_fixit_remove ();
3169 pedwarn (&richloc, OPT_Wpedantic,
3170 "extra semicolon in struct or union specified");
3171 c_parser_consume_token (parser);
3172 continue;
3174 /* Stop if at the end of the struct or union contents. */
3175 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3177 c_parser_consume_token (parser);
3178 break;
3180 /* Accept #pragmas at struct scope. */
3181 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3183 c_parser_pragma (parser, pragma_struct, NULL);
3184 continue;
3186 /* Parse some comma-separated declarations, but not the
3187 trailing semicolon if any. */
3188 decls = c_parser_struct_declaration (parser);
3189 contents = chainon (decls, contents);
3190 /* If no semicolon follows, either we have a parse error or
3191 are at the end of the struct or union and should
3192 pedwarn. */
3193 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3194 c_parser_consume_token (parser);
3195 else
3197 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3198 pedwarn (c_parser_peek_token (parser)->location, 0,
3199 "no semicolon at end of struct or union");
3200 else if (parser->error
3201 || !c_parser_next_token_starts_declspecs (parser))
3203 c_parser_error (parser, "expected %<;%>");
3204 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3205 break;
3208 /* If we come here, we have already emitted an error
3209 for an expected `;', identifier or `(', and we also
3210 recovered already. Go on with the next field. */
3213 postfix_attrs = c_parser_attributes (parser);
3214 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3215 chainon (attrs, postfix_attrs), struct_info);
3216 ret.kind = ctsk_tagdef;
3217 ret.expr = NULL_TREE;
3218 ret.expr_const_operands = true;
3219 timevar_pop (TV_PARSE_STRUCT);
3220 return ret;
3222 else if (!ident)
3224 c_parser_error (parser, "expected %<{%>");
3225 ret.spec = error_mark_node;
3226 ret.kind = ctsk_tagref;
3227 ret.expr = NULL_TREE;
3228 ret.expr_const_operands = true;
3229 return ret;
3231 ret = parser_xref_tag (ident_loc, code, ident);
3232 return ret;
3235 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3236 *without* the trailing semicolon.
3238 struct-declaration:
3239 specifier-qualifier-list struct-declarator-list
3240 static_assert-declaration-no-semi
3242 specifier-qualifier-list:
3243 type-specifier specifier-qualifier-list[opt]
3244 type-qualifier specifier-qualifier-list[opt]
3245 alignment-specifier specifier-qualifier-list[opt]
3246 attributes specifier-qualifier-list[opt]
3248 struct-declarator-list:
3249 struct-declarator
3250 struct-declarator-list , attributes[opt] struct-declarator
3252 struct-declarator:
3253 declarator attributes[opt]
3254 declarator[opt] : constant-expression attributes[opt]
3256 GNU extensions:
3258 struct-declaration:
3259 __extension__ struct-declaration
3260 specifier-qualifier-list
3262 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3263 of attributes where shown is a GNU extension. In GNU C, we accept
3264 any expression without commas in the syntax (assignment
3265 expressions, not just conditional expressions); assignment
3266 expressions will be diagnosed as non-constant. */
3268 static tree
3269 c_parser_struct_declaration (c_parser *parser)
3271 struct c_declspecs *specs;
3272 tree prefix_attrs;
3273 tree all_prefix_attrs;
3274 tree decls;
3275 location_t decl_loc;
3276 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3278 int ext;
3279 tree decl;
3280 ext = disable_extension_diagnostics ();
3281 c_parser_consume_token (parser);
3282 decl = c_parser_struct_declaration (parser);
3283 restore_extension_diagnostics (ext);
3284 return decl;
3286 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3288 c_parser_static_assert_declaration_no_semi (parser);
3289 return NULL_TREE;
3291 specs = build_null_declspecs ();
3292 decl_loc = c_parser_peek_token (parser)->location;
3293 /* Strictly by the standard, we shouldn't allow _Alignas here,
3294 but it appears to have been intended to allow it there, so
3295 we're keeping it as it is until WG14 reaches a conclusion
3296 of N1731.
3297 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3298 c_parser_declspecs (parser, specs, false, true, true,
3299 true, false, cla_nonabstract_decl);
3300 if (parser->error)
3301 return NULL_TREE;
3302 if (!specs->declspecs_seen_p)
3304 c_parser_error (parser, "expected specifier-qualifier-list");
3305 return NULL_TREE;
3307 finish_declspecs (specs);
3308 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3309 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3311 tree ret;
3312 if (specs->typespec_kind == ctsk_none)
3314 pedwarn (decl_loc, OPT_Wpedantic,
3315 "ISO C forbids member declarations with no members");
3316 shadow_tag_warned (specs, pedantic);
3317 ret = NULL_TREE;
3319 else
3321 /* Support for unnamed structs or unions as members of
3322 structs or unions (which is [a] useful and [b] supports
3323 MS P-SDK). */
3324 tree attrs = NULL;
3326 ret = grokfield (c_parser_peek_token (parser)->location,
3327 build_id_declarator (NULL_TREE), specs,
3328 NULL_TREE, &attrs);
3329 if (ret)
3330 decl_attributes (&ret, attrs, 0);
3332 return ret;
3335 /* Provide better error recovery. Note that a type name here is valid,
3336 and will be treated as a field name. */
3337 if (specs->typespec_kind == ctsk_tagdef
3338 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3339 && c_parser_next_token_starts_declspecs (parser)
3340 && !c_parser_next_token_is (parser, CPP_NAME))
3342 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3343 parser->error = false;
3344 return NULL_TREE;
3347 pending_xref_error ();
3348 prefix_attrs = specs->attrs;
3349 all_prefix_attrs = prefix_attrs;
3350 specs->attrs = NULL_TREE;
3351 decls = NULL_TREE;
3352 while (true)
3354 /* Declaring one or more declarators or un-named bit-fields. */
3355 struct c_declarator *declarator;
3356 bool dummy = false;
3357 if (c_parser_next_token_is (parser, CPP_COLON))
3358 declarator = build_id_declarator (NULL_TREE);
3359 else
3360 declarator = c_parser_declarator (parser,
3361 specs->typespec_kind != ctsk_none,
3362 C_DTR_NORMAL, &dummy);
3363 if (declarator == NULL)
3365 c_parser_skip_to_end_of_block_or_statement (parser);
3366 break;
3368 if (c_parser_next_token_is (parser, CPP_COLON)
3369 || c_parser_next_token_is (parser, CPP_COMMA)
3370 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3371 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3372 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3374 tree postfix_attrs = NULL_TREE;
3375 tree width = NULL_TREE;
3376 tree d;
3377 if (c_parser_next_token_is (parser, CPP_COLON))
3379 c_parser_consume_token (parser);
3380 width = c_parser_expr_no_commas (parser, NULL).value;
3382 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3383 postfix_attrs = c_parser_attributes (parser);
3384 d = grokfield (c_parser_peek_token (parser)->location,
3385 declarator, specs, width, &all_prefix_attrs);
3386 decl_attributes (&d, chainon (postfix_attrs,
3387 all_prefix_attrs), 0);
3388 DECL_CHAIN (d) = decls;
3389 decls = d;
3390 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3391 all_prefix_attrs = chainon (c_parser_attributes (parser),
3392 prefix_attrs);
3393 else
3394 all_prefix_attrs = prefix_attrs;
3395 if (c_parser_next_token_is (parser, CPP_COMMA))
3396 c_parser_consume_token (parser);
3397 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3398 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3400 /* Semicolon consumed in caller. */
3401 break;
3403 else
3405 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3406 break;
3409 else
3411 c_parser_error (parser,
3412 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3413 "%<__attribute__%>");
3414 break;
3417 return decls;
3420 /* Parse a typeof specifier (a GNU extension).
3422 typeof-specifier:
3423 typeof ( expression )
3424 typeof ( type-name )
3427 static struct c_typespec
3428 c_parser_typeof_specifier (c_parser *parser)
3430 struct c_typespec ret;
3431 ret.kind = ctsk_typeof;
3432 ret.spec = error_mark_node;
3433 ret.expr = NULL_TREE;
3434 ret.expr_const_operands = true;
3435 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3436 c_parser_consume_token (parser);
3437 c_inhibit_evaluation_warnings++;
3438 in_typeof++;
3439 matching_parens parens;
3440 if (!parens.require_open (parser))
3442 c_inhibit_evaluation_warnings--;
3443 in_typeof--;
3444 return ret;
3446 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3448 struct c_type_name *type = c_parser_type_name (parser);
3449 c_inhibit_evaluation_warnings--;
3450 in_typeof--;
3451 if (type != NULL)
3453 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3454 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3457 else
3459 bool was_vm;
3460 location_t here = c_parser_peek_token (parser)->location;
3461 struct c_expr expr = c_parser_expression (parser);
3462 c_inhibit_evaluation_warnings--;
3463 in_typeof--;
3464 if (TREE_CODE (expr.value) == COMPONENT_REF
3465 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3466 error_at (here, "%<typeof%> applied to a bit-field");
3467 mark_exp_read (expr.value);
3468 ret.spec = TREE_TYPE (expr.value);
3469 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3470 /* This is returned with the type so that when the type is
3471 evaluated, this can be evaluated. */
3472 if (was_vm)
3473 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3474 pop_maybe_used (was_vm);
3475 /* For use in macros such as those in <stdatomic.h>, remove all
3476 qualifiers from atomic types. (const can be an issue for more macros
3477 using typeof than just the <stdatomic.h> ones.) */
3478 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3479 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3481 parens.skip_until_found_close (parser);
3482 return ret;
3485 /* Parse an alignment-specifier.
3487 C11 6.7.5:
3489 alignment-specifier:
3490 _Alignas ( type-name )
3491 _Alignas ( constant-expression )
3494 static tree
3495 c_parser_alignas_specifier (c_parser * parser)
3497 tree ret = error_mark_node;
3498 location_t loc = c_parser_peek_token (parser)->location;
3499 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3500 c_parser_consume_token (parser);
3501 if (flag_isoc99)
3502 pedwarn_c99 (loc, OPT_Wpedantic,
3503 "ISO C99 does not support %<_Alignas%>");
3504 else
3505 pedwarn_c99 (loc, OPT_Wpedantic,
3506 "ISO C90 does not support %<_Alignas%>");
3507 matching_parens parens;
3508 if (!parens.require_open (parser))
3509 return ret;
3510 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3512 struct c_type_name *type = c_parser_type_name (parser);
3513 if (type != NULL)
3514 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3515 false, true, 1);
3517 else
3518 ret = c_parser_expr_no_commas (parser, NULL).value;
3519 parens.skip_until_found_close (parser);
3520 return ret;
3523 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3524 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3525 a typedef name may be redeclared; otherwise it may not. KIND
3526 indicates which kind of declarator is wanted. Returns a valid
3527 declarator except in the case of a syntax error in which case NULL is
3528 returned. *SEEN_ID is set to true if an identifier being declared is
3529 seen; this is used to diagnose bad forms of abstract array declarators
3530 and to determine whether an identifier list is syntactically permitted.
3532 declarator:
3533 pointer[opt] direct-declarator
3535 direct-declarator:
3536 identifier
3537 ( attributes[opt] declarator )
3538 direct-declarator array-declarator
3539 direct-declarator ( parameter-type-list )
3540 direct-declarator ( identifier-list[opt] )
3542 pointer:
3543 * type-qualifier-list[opt]
3544 * type-qualifier-list[opt] pointer
3546 type-qualifier-list:
3547 type-qualifier
3548 attributes
3549 type-qualifier-list type-qualifier
3550 type-qualifier-list attributes
3552 array-declarator:
3553 [ type-qualifier-list[opt] assignment-expression[opt] ]
3554 [ static type-qualifier-list[opt] assignment-expression ]
3555 [ type-qualifier-list static assignment-expression ]
3556 [ type-qualifier-list[opt] * ]
3558 parameter-type-list:
3559 parameter-list
3560 parameter-list , ...
3562 parameter-list:
3563 parameter-declaration
3564 parameter-list , parameter-declaration
3566 parameter-declaration:
3567 declaration-specifiers declarator attributes[opt]
3568 declaration-specifiers abstract-declarator[opt] attributes[opt]
3570 identifier-list:
3571 identifier
3572 identifier-list , identifier
3574 abstract-declarator:
3575 pointer
3576 pointer[opt] direct-abstract-declarator
3578 direct-abstract-declarator:
3579 ( attributes[opt] abstract-declarator )
3580 direct-abstract-declarator[opt] array-declarator
3581 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3583 GNU extensions:
3585 direct-declarator:
3586 direct-declarator ( parameter-forward-declarations
3587 parameter-type-list[opt] )
3589 direct-abstract-declarator:
3590 direct-abstract-declarator[opt] ( parameter-forward-declarations
3591 parameter-type-list[opt] )
3593 parameter-forward-declarations:
3594 parameter-list ;
3595 parameter-forward-declarations parameter-list ;
3597 The uses of attributes shown above are GNU extensions.
3599 Some forms of array declarator are not included in C99 in the
3600 syntax for abstract declarators; these are disallowed elsewhere.
3601 This may be a defect (DR#289).
3603 This function also accepts an omitted abstract declarator as being
3604 an abstract declarator, although not part of the formal syntax. */
3606 struct c_declarator *
3607 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3608 bool *seen_id)
3610 /* Parse any initial pointer part. */
3611 if (c_parser_next_token_is (parser, CPP_MULT))
3613 struct c_declspecs *quals_attrs = build_null_declspecs ();
3614 struct c_declarator *inner;
3615 c_parser_consume_token (parser);
3616 c_parser_declspecs (parser, quals_attrs, false, false, true,
3617 false, false, cla_prefer_id);
3618 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3619 if (inner == NULL)
3620 return NULL;
3621 else
3622 return make_pointer_declarator (quals_attrs, inner);
3624 /* Now we have a direct declarator, direct abstract declarator or
3625 nothing (which counts as a direct abstract declarator here). */
3626 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3629 /* Parse a direct declarator or direct abstract declarator; arguments
3630 as c_parser_declarator. */
3632 static struct c_declarator *
3633 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3634 bool *seen_id)
3636 /* The direct declarator must start with an identifier (possibly
3637 omitted) or a parenthesized declarator (possibly abstract). In
3638 an ordinary declarator, initial parentheses must start a
3639 parenthesized declarator. In an abstract declarator or parameter
3640 declarator, they could start a parenthesized declarator or a
3641 parameter list. To tell which, the open parenthesis and any
3642 following attributes must be read. If a declaration specifier
3643 follows, then it is a parameter list; if the specifier is a
3644 typedef name, there might be an ambiguity about redeclaring it,
3645 which is resolved in the direction of treating it as a typedef
3646 name. If a close parenthesis follows, it is also an empty
3647 parameter list, as the syntax does not permit empty abstract
3648 declarators. Otherwise, it is a parenthesized declarator (in
3649 which case the analysis may be repeated inside it, recursively).
3651 ??? There is an ambiguity in a parameter declaration "int
3652 (__attribute__((foo)) x)", where x is not a typedef name: it
3653 could be an abstract declarator for a function, or declare x with
3654 parentheses. The proper resolution of this ambiguity needs
3655 documenting. At present we follow an accident of the old
3656 parser's implementation, whereby the first parameter must have
3657 some declaration specifiers other than just attributes. Thus as
3658 a parameter declaration it is treated as a parenthesized
3659 parameter named x, and as an abstract declarator it is
3660 rejected.
3662 ??? Also following the old parser, attributes inside an empty
3663 parameter list are ignored, making it a list not yielding a
3664 prototype, rather than giving an error or making it have one
3665 parameter with implicit type int.
3667 ??? Also following the old parser, typedef names may be
3668 redeclared in declarators, but not Objective-C class names. */
3670 if (kind != C_DTR_ABSTRACT
3671 && c_parser_next_token_is (parser, CPP_NAME)
3672 && ((type_seen_p
3673 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3674 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3675 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3677 struct c_declarator *inner
3678 = build_id_declarator (c_parser_peek_token (parser)->value);
3679 *seen_id = true;
3680 inner->id_loc = c_parser_peek_token (parser)->location;
3681 c_parser_consume_token (parser);
3682 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3685 if (kind != C_DTR_NORMAL
3686 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3688 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3689 inner->id_loc = c_parser_peek_token (parser)->location;
3690 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3693 /* Either we are at the end of an abstract declarator, or we have
3694 parentheses. */
3696 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3698 tree attrs;
3699 struct c_declarator *inner;
3700 c_parser_consume_token (parser);
3701 attrs = c_parser_attributes (parser);
3702 if (kind != C_DTR_NORMAL
3703 && (c_parser_next_token_starts_declspecs (parser)
3704 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3706 struct c_arg_info *args
3707 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3708 attrs);
3709 if (args == NULL)
3710 return NULL;
3711 else
3713 inner
3714 = build_function_declarator (args,
3715 build_id_declarator (NULL_TREE));
3716 return c_parser_direct_declarator_inner (parser, *seen_id,
3717 inner);
3720 /* A parenthesized declarator. */
3721 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3722 if (inner != NULL && attrs != NULL)
3723 inner = build_attrs_declarator (attrs, inner);
3724 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3726 c_parser_consume_token (parser);
3727 if (inner == NULL)
3728 return NULL;
3729 else
3730 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3732 else
3734 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3735 "expected %<)%>");
3736 return NULL;
3739 else
3741 if (kind == C_DTR_NORMAL)
3743 c_parser_error (parser, "expected identifier or %<(%>");
3744 return NULL;
3746 else
3747 return build_id_declarator (NULL_TREE);
3751 /* Parse part of a direct declarator or direct abstract declarator,
3752 given that some (in INNER) has already been parsed; ID_PRESENT is
3753 true if an identifier is present, false for an abstract
3754 declarator. */
3756 static struct c_declarator *
3757 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3758 struct c_declarator *inner)
3760 /* Parse a sequence of array declarators and parameter lists. */
3761 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3763 location_t brace_loc = c_parser_peek_token (parser)->location;
3764 struct c_declarator *declarator;
3765 struct c_declspecs *quals_attrs = build_null_declspecs ();
3766 bool static_seen;
3767 bool star_seen;
3768 struct c_expr dimen;
3769 dimen.value = NULL_TREE;
3770 dimen.original_code = ERROR_MARK;
3771 dimen.original_type = NULL_TREE;
3772 c_parser_consume_token (parser);
3773 c_parser_declspecs (parser, quals_attrs, false, false, true,
3774 false, false, cla_prefer_id);
3775 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3776 if (static_seen)
3777 c_parser_consume_token (parser);
3778 if (static_seen && !quals_attrs->declspecs_seen_p)
3779 c_parser_declspecs (parser, quals_attrs, false, false, true,
3780 false, false, cla_prefer_id);
3781 if (!quals_attrs->declspecs_seen_p)
3782 quals_attrs = NULL;
3783 /* If "static" is present, there must be an array dimension.
3784 Otherwise, there may be a dimension, "*", or no
3785 dimension. */
3786 if (static_seen)
3788 star_seen = false;
3789 dimen = c_parser_expr_no_commas (parser, NULL);
3791 else
3793 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3795 dimen.value = NULL_TREE;
3796 star_seen = false;
3798 else if (c_parser_next_token_is (parser, CPP_MULT))
3800 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3802 dimen.value = NULL_TREE;
3803 star_seen = true;
3804 c_parser_consume_token (parser);
3806 else
3808 star_seen = false;
3809 dimen = c_parser_expr_no_commas (parser, NULL);
3812 else
3814 star_seen = false;
3815 dimen = c_parser_expr_no_commas (parser, NULL);
3818 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3819 c_parser_consume_token (parser);
3820 else
3822 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3823 "expected %<]%>");
3824 return NULL;
3826 if (dimen.value)
3827 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3828 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3829 static_seen, star_seen);
3830 if (declarator == NULL)
3831 return NULL;
3832 inner = set_array_declarator_inner (declarator, inner);
3833 return c_parser_direct_declarator_inner (parser, id_present, inner);
3835 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3837 tree attrs;
3838 struct c_arg_info *args;
3839 c_parser_consume_token (parser);
3840 attrs = c_parser_attributes (parser);
3841 args = c_parser_parms_declarator (parser, id_present, attrs);
3842 if (args == NULL)
3843 return NULL;
3844 else
3846 inner = build_function_declarator (args, inner);
3847 return c_parser_direct_declarator_inner (parser, id_present, inner);
3850 return inner;
3853 /* Parse a parameter list or identifier list, including the closing
3854 parenthesis but not the opening one. ATTRS are the attributes at
3855 the start of the list. ID_LIST_OK is true if an identifier list is
3856 acceptable; such a list must not have attributes at the start. */
3858 static struct c_arg_info *
3859 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3861 push_scope ();
3862 declare_parm_level ();
3863 /* If the list starts with an identifier, it is an identifier list.
3864 Otherwise, it is either a prototype list or an empty list. */
3865 if (id_list_ok
3866 && !attrs
3867 && c_parser_next_token_is (parser, CPP_NAME)
3868 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3870 /* Look ahead to detect typos in type names. */
3871 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3872 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3873 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3874 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3875 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3877 tree list = NULL_TREE, *nextp = &list;
3878 while (c_parser_next_token_is (parser, CPP_NAME)
3879 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3881 *nextp = build_tree_list (NULL_TREE,
3882 c_parser_peek_token (parser)->value);
3883 nextp = & TREE_CHAIN (*nextp);
3884 c_parser_consume_token (parser);
3885 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3886 break;
3887 c_parser_consume_token (parser);
3888 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3890 c_parser_error (parser, "expected identifier");
3891 break;
3894 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3896 struct c_arg_info *ret = build_arg_info ();
3897 ret->types = list;
3898 c_parser_consume_token (parser);
3899 pop_scope ();
3900 return ret;
3902 else
3904 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3905 "expected %<)%>");
3906 pop_scope ();
3907 return NULL;
3910 else
3912 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3913 NULL);
3914 pop_scope ();
3915 return ret;
3919 /* Parse a parameter list (possibly empty), including the closing
3920 parenthesis but not the opening one. ATTRS are the attributes at
3921 the start of the list. EXPR is NULL or an expression that needs to
3922 be evaluated for the side effects of array size expressions in the
3923 parameters. */
3925 static struct c_arg_info *
3926 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3928 bool bad_parm = false;
3930 /* ??? Following the old parser, forward parameter declarations may
3931 use abstract declarators, and if no real parameter declarations
3932 follow the forward declarations then this is not diagnosed. Also
3933 note as above that attributes are ignored as the only contents of
3934 the parentheses, or as the only contents after forward
3935 declarations. */
3936 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3938 struct c_arg_info *ret = build_arg_info ();
3939 c_parser_consume_token (parser);
3940 return ret;
3942 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3944 struct c_arg_info *ret = build_arg_info ();
3946 if (flag_allow_parameterless_variadic_functions)
3948 /* F (...) is allowed. */
3949 ret->types = NULL_TREE;
3951 else
3953 /* Suppress -Wold-style-definition for this case. */
3954 ret->types = error_mark_node;
3955 error_at (c_parser_peek_token (parser)->location,
3956 "ISO C requires a named argument before %<...%>");
3958 c_parser_consume_token (parser);
3959 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3961 c_parser_consume_token (parser);
3962 return ret;
3964 else
3966 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3967 "expected %<)%>");
3968 return NULL;
3971 /* Nonempty list of parameters, either terminated with semicolon
3972 (forward declarations; recurse) or with close parenthesis (normal
3973 function) or with ", ... )" (variadic function). */
3974 while (true)
3976 /* Parse a parameter. */
3977 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3978 attrs = NULL_TREE;
3979 if (parm == NULL)
3980 bad_parm = true;
3981 else
3982 push_parm_decl (parm, &expr);
3983 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3985 tree new_attrs;
3986 c_parser_consume_token (parser);
3987 mark_forward_parm_decls ();
3988 new_attrs = c_parser_attributes (parser);
3989 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3991 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3993 c_parser_consume_token (parser);
3994 if (bad_parm)
3995 return NULL;
3996 else
3997 return get_parm_info (false, expr);
3999 if (!c_parser_require (parser, CPP_COMMA,
4000 "expected %<;%>, %<,%> or %<)%>",
4001 UNKNOWN_LOCATION, false))
4003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4004 return NULL;
4006 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4008 c_parser_consume_token (parser);
4009 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4011 c_parser_consume_token (parser);
4012 if (bad_parm)
4013 return NULL;
4014 else
4015 return get_parm_info (true, expr);
4017 else
4019 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4020 "expected %<)%>");
4021 return NULL;
4027 /* Parse a parameter declaration. ATTRS are the attributes at the
4028 start of the declaration if it is the first parameter. */
4030 static struct c_parm *
4031 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4033 struct c_declspecs *specs;
4034 struct c_declarator *declarator;
4035 tree prefix_attrs;
4036 tree postfix_attrs = NULL_TREE;
4037 bool dummy = false;
4039 /* Accept #pragmas between parameter declarations. */
4040 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4041 c_parser_pragma (parser, pragma_param, NULL);
4043 if (!c_parser_next_token_starts_declspecs (parser))
4045 c_token *token = c_parser_peek_token (parser);
4046 if (parser->error)
4047 return NULL;
4048 c_parser_set_source_position_from_token (token);
4049 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4051 name_hint hint = lookup_name_fuzzy (token->value,
4052 FUZZY_LOOKUP_TYPENAME,
4053 token->location);
4054 if (hint)
4056 gcc_rich_location richloc (token->location);
4057 richloc.add_fixit_replace (hint.suggestion ());
4058 error_at (&richloc,
4059 "unknown type name %qE; did you mean %qs?",
4060 token->value, hint.suggestion ());
4062 else
4063 error_at (token->location, "unknown type name %qE", token->value);
4064 parser->error = true;
4066 /* ??? In some Objective-C cases '...' isn't applicable so there
4067 should be a different message. */
4068 else
4069 c_parser_error (parser,
4070 "expected declaration specifiers or %<...%>");
4071 c_parser_skip_to_end_of_parameter (parser);
4072 return NULL;
4075 location_t start_loc = c_parser_peek_token (parser)->location;
4077 specs = build_null_declspecs ();
4078 if (attrs)
4080 declspecs_add_attrs (input_location, specs, attrs);
4081 attrs = NULL_TREE;
4083 c_parser_declspecs (parser, specs, true, true, true, true, false,
4084 cla_nonabstract_decl);
4085 finish_declspecs (specs);
4086 pending_xref_error ();
4087 prefix_attrs = specs->attrs;
4088 specs->attrs = NULL_TREE;
4089 declarator = c_parser_declarator (parser,
4090 specs->typespec_kind != ctsk_none,
4091 C_DTR_PARM, &dummy);
4092 if (declarator == NULL)
4094 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4095 return NULL;
4097 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4098 postfix_attrs = c_parser_attributes (parser);
4100 /* Generate a location for the parameter, ranging from the start of the
4101 initial token to the end of the final token.
4103 If we have a identifier, then use it for the caret location, e.g.
4105 extern int callee (int one, int (*two)(int, int), float three);
4106 ~~~~~~^~~~~~~~~~~~~~
4108 otherwise, reuse the start location for the caret location e.g.:
4110 extern int callee (int one, int (*)(int, int), float three);
4111 ^~~~~~~~~~~~~~~~~
4113 location_t end_loc = parser->last_token_location;
4115 /* Find any cdk_id declarator; determine if we have an identifier. */
4116 c_declarator *id_declarator = declarator;
4117 while (id_declarator && id_declarator->kind != cdk_id)
4118 id_declarator = id_declarator->declarator;
4119 location_t caret_loc = (id_declarator->u.id
4120 ? id_declarator->id_loc
4121 : start_loc);
4122 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4124 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4125 declarator, param_loc);
4128 /* Parse a string literal in an asm expression. It should not be
4129 translated, and wide string literals are an error although
4130 permitted by the syntax. This is a GNU extension.
4132 asm-string-literal:
4133 string-literal
4135 ??? At present, following the old parser, the caller needs to have
4136 set lex_untranslated_string to 1. It would be better to follow the
4137 C++ parser rather than using this kludge. */
4139 static tree
4140 c_parser_asm_string_literal (c_parser *parser)
4142 tree str;
4143 int save_flag = warn_overlength_strings;
4144 warn_overlength_strings = 0;
4145 if (c_parser_next_token_is (parser, CPP_STRING))
4147 str = c_parser_peek_token (parser)->value;
4148 c_parser_consume_token (parser);
4150 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4152 error_at (c_parser_peek_token (parser)->location,
4153 "wide string literal in %<asm%>");
4154 str = build_string (1, "");
4155 c_parser_consume_token (parser);
4157 else
4159 c_parser_error (parser, "expected string literal");
4160 str = NULL_TREE;
4162 warn_overlength_strings = save_flag;
4163 return str;
4166 /* Parse a simple asm expression. This is used in restricted
4167 contexts, where a full expression with inputs and outputs does not
4168 make sense. This is a GNU extension.
4170 simple-asm-expr:
4171 asm ( asm-string-literal )
4174 static tree
4175 c_parser_simple_asm_expr (c_parser *parser)
4177 tree str;
4178 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4179 /* ??? Follow the C++ parser rather than using the
4180 lex_untranslated_string kludge. */
4181 parser->lex_untranslated_string = true;
4182 c_parser_consume_token (parser);
4183 matching_parens parens;
4184 if (!parens.require_open (parser))
4186 parser->lex_untranslated_string = false;
4187 return NULL_TREE;
4189 str = c_parser_asm_string_literal (parser);
4190 parser->lex_untranslated_string = false;
4191 if (!parens.require_close (parser))
4193 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4194 return NULL_TREE;
4196 return str;
4199 static tree
4200 c_parser_attribute_any_word (c_parser *parser)
4202 tree attr_name = NULL_TREE;
4204 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4206 /* ??? See comment above about what keywords are accepted here. */
4207 bool ok;
4208 switch (c_parser_peek_token (parser)->keyword)
4210 case RID_STATIC:
4211 case RID_UNSIGNED:
4212 case RID_LONG:
4213 case RID_CONST:
4214 case RID_EXTERN:
4215 case RID_REGISTER:
4216 case RID_TYPEDEF:
4217 case RID_SHORT:
4218 case RID_INLINE:
4219 case RID_NORETURN:
4220 case RID_VOLATILE:
4221 case RID_SIGNED:
4222 case RID_AUTO:
4223 case RID_RESTRICT:
4224 case RID_COMPLEX:
4225 case RID_THREAD:
4226 case RID_INT:
4227 case RID_CHAR:
4228 case RID_FLOAT:
4229 case RID_DOUBLE:
4230 case RID_VOID:
4231 case RID_DFLOAT32:
4232 case RID_DFLOAT64:
4233 case RID_DFLOAT128:
4234 CASE_RID_FLOATN_NX:
4235 case RID_BOOL:
4236 case RID_FRACT:
4237 case RID_ACCUM:
4238 case RID_SAT:
4239 case RID_TRANSACTION_ATOMIC:
4240 case RID_TRANSACTION_CANCEL:
4241 case RID_ATOMIC:
4242 case RID_AUTO_TYPE:
4243 case RID_INT_N_0:
4244 case RID_INT_N_1:
4245 case RID_INT_N_2:
4246 case RID_INT_N_3:
4247 ok = true;
4248 break;
4249 default:
4250 ok = false;
4251 break;
4253 if (!ok)
4254 return NULL_TREE;
4256 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4257 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4259 else if (c_parser_next_token_is (parser, CPP_NAME))
4260 attr_name = c_parser_peek_token (parser)->value;
4262 return attr_name;
4265 /* Parse (possibly empty) attributes. This is a GNU extension.
4267 attributes:
4268 empty
4269 attributes attribute
4271 attribute:
4272 __attribute__ ( ( attribute-list ) )
4274 attribute-list:
4275 attrib
4276 attribute_list , attrib
4278 attrib:
4279 empty
4280 any-word
4281 any-word ( identifier )
4282 any-word ( identifier , nonempty-expr-list )
4283 any-word ( expr-list )
4285 where the "identifier" must not be declared as a type, and
4286 "any-word" may be any identifier (including one declared as a
4287 type), a reserved word storage class specifier, type specifier or
4288 type qualifier. ??? This still leaves out most reserved keywords
4289 (following the old parser), shouldn't we include them, and why not
4290 allow identifiers declared as types to start the arguments? */
4292 static tree
4293 c_parser_attributes (c_parser *parser)
4295 tree attrs = NULL_TREE;
4296 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4298 /* ??? Follow the C++ parser rather than using the
4299 lex_untranslated_string kludge. */
4300 parser->lex_untranslated_string = true;
4301 /* Consume the `__attribute__' keyword. */
4302 c_parser_consume_token (parser);
4303 /* Look for the two `(' tokens. */
4304 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4306 parser->lex_untranslated_string = false;
4307 return attrs;
4309 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4311 parser->lex_untranslated_string = false;
4312 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4313 return attrs;
4315 /* Parse the attribute list. */
4316 while (c_parser_next_token_is (parser, CPP_COMMA)
4317 || c_parser_next_token_is (parser, CPP_NAME)
4318 || c_parser_next_token_is (parser, CPP_KEYWORD))
4320 tree attr, attr_name, attr_args;
4321 vec<tree, va_gc> *expr_list;
4322 if (c_parser_next_token_is (parser, CPP_COMMA))
4324 c_parser_consume_token (parser);
4325 continue;
4328 attr_name = c_parser_attribute_any_word (parser);
4329 if (attr_name == NULL)
4330 break;
4331 attr_name = canonicalize_attr_name (attr_name);
4332 c_parser_consume_token (parser);
4333 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4335 attr = build_tree_list (attr_name, NULL_TREE);
4336 /* Add this attribute to the list. */
4337 attrs = chainon (attrs, attr);
4338 /* If the next token isn't a comma, we're done. */
4339 if (!c_parser_next_token_is (parser, CPP_COMMA))
4340 break;
4341 continue;
4343 c_parser_consume_token (parser);
4344 /* Parse the attribute contents. If they start with an
4345 identifier which is followed by a comma or close
4346 parenthesis, then the arguments start with that
4347 identifier; otherwise they are an expression list.
4348 In objective-c the identifier may be a classname. */
4349 if (c_parser_next_token_is (parser, CPP_NAME)
4350 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4351 || (c_dialect_objc ()
4352 && c_parser_peek_token (parser)->id_kind
4353 == C_ID_CLASSNAME))
4354 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4355 || (c_parser_peek_2nd_token (parser)->type
4356 == CPP_CLOSE_PAREN))
4357 && (attribute_takes_identifier_p (attr_name)
4358 || (c_dialect_objc ()
4359 && c_parser_peek_token (parser)->id_kind
4360 == C_ID_CLASSNAME)))
4362 tree arg1 = c_parser_peek_token (parser)->value;
4363 c_parser_consume_token (parser);
4364 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4365 attr_args = build_tree_list (NULL_TREE, arg1);
4366 else
4368 tree tree_list;
4369 c_parser_consume_token (parser);
4370 expr_list = c_parser_expr_list (parser, false, true,
4371 NULL, NULL, NULL, NULL);
4372 tree_list = build_tree_list_vec (expr_list);
4373 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4374 release_tree_vector (expr_list);
4377 else
4379 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4380 attr_args = NULL_TREE;
4381 else
4383 expr_list = c_parser_expr_list (parser, false, true,
4384 NULL, NULL, NULL, NULL);
4385 attr_args = build_tree_list_vec (expr_list);
4386 release_tree_vector (expr_list);
4390 attr = build_tree_list (attr_name, attr_args);
4391 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4392 c_parser_consume_token (parser);
4393 else
4395 parser->lex_untranslated_string = false;
4396 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4397 "expected %<)%>");
4398 return attrs;
4400 /* Add this attribute to the list. */
4401 attrs = chainon (attrs, attr);
4402 /* If the next token isn't a comma, we're done. */
4403 if (!c_parser_next_token_is (parser, CPP_COMMA))
4404 break;
4406 /* Look for the two `)' tokens. */
4407 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4408 c_parser_consume_token (parser);
4409 else
4411 parser->lex_untranslated_string = false;
4412 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4413 "expected %<)%>");
4414 return attrs;
4416 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4417 c_parser_consume_token (parser);
4418 else
4420 parser->lex_untranslated_string = false;
4421 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4422 "expected %<)%>");
4423 return attrs;
4425 parser->lex_untranslated_string = false;
4428 return attrs;
4431 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4432 says whether alignment specifiers are OK (only in cases that might
4433 be the type name of a compound literal).
4435 type-name:
4436 specifier-qualifier-list abstract-declarator[opt]
4439 struct c_type_name *
4440 c_parser_type_name (c_parser *parser, bool alignas_ok)
4442 struct c_declspecs *specs = build_null_declspecs ();
4443 struct c_declarator *declarator;
4444 struct c_type_name *ret;
4445 bool dummy = false;
4446 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4447 cla_prefer_type);
4448 if (!specs->declspecs_seen_p)
4450 c_parser_error (parser, "expected specifier-qualifier-list");
4451 return NULL;
4453 if (specs->type != error_mark_node)
4455 pending_xref_error ();
4456 finish_declspecs (specs);
4458 declarator = c_parser_declarator (parser,
4459 specs->typespec_kind != ctsk_none,
4460 C_DTR_ABSTRACT, &dummy);
4461 if (declarator == NULL)
4462 return NULL;
4463 ret = XOBNEW (&parser_obstack, struct c_type_name);
4464 ret->specs = specs;
4465 ret->declarator = declarator;
4466 return ret;
4469 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4471 initializer:
4472 assignment-expression
4473 { initializer-list }
4474 { initializer-list , }
4476 initializer-list:
4477 designation[opt] initializer
4478 initializer-list , designation[opt] initializer
4480 designation:
4481 designator-list =
4483 designator-list:
4484 designator
4485 designator-list designator
4487 designator:
4488 array-designator
4489 . identifier
4491 array-designator:
4492 [ constant-expression ]
4494 GNU extensions:
4496 initializer:
4499 designation:
4500 array-designator
4501 identifier :
4503 array-designator:
4504 [ constant-expression ... constant-expression ]
4506 Any expression without commas is accepted in the syntax for the
4507 constant-expressions, with non-constant expressions rejected later.
4509 This function is only used for top-level initializers; for nested
4510 ones, see c_parser_initval. */
4512 static struct c_expr
4513 c_parser_initializer (c_parser *parser)
4515 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4516 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4517 else
4519 struct c_expr ret;
4520 location_t loc = c_parser_peek_token (parser)->location;
4521 ret = c_parser_expr_no_commas (parser, NULL);
4522 if (TREE_CODE (ret.value) != STRING_CST
4523 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4524 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4525 return ret;
4529 /* The location of the last comma within the current initializer list,
4530 or UNKNOWN_LOCATION if not within one. */
4532 location_t last_init_list_comma;
4534 /* Parse a braced initializer list. TYPE is the type specified for a
4535 compound literal, and NULL_TREE for other initializers and for
4536 nested braced lists. NESTED_P is true for nested braced lists,
4537 false for the list of a compound literal or the list that is the
4538 top-level initializer in a declaration. */
4540 static struct c_expr
4541 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4542 struct obstack *outer_obstack)
4544 struct c_expr ret;
4545 struct obstack braced_init_obstack;
4546 location_t brace_loc = c_parser_peek_token (parser)->location;
4547 gcc_obstack_init (&braced_init_obstack);
4548 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4549 matching_braces braces;
4550 braces.consume_open (parser);
4551 if (nested_p)
4553 finish_implicit_inits (brace_loc, outer_obstack);
4554 push_init_level (brace_loc, 0, &braced_init_obstack);
4556 else
4557 really_start_incremental_init (type);
4558 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4560 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4562 else
4564 /* Parse a non-empty initializer list, possibly with a trailing
4565 comma. */
4566 while (true)
4568 c_parser_initelt (parser, &braced_init_obstack);
4569 if (parser->error)
4570 break;
4571 if (c_parser_next_token_is (parser, CPP_COMMA))
4573 last_init_list_comma = c_parser_peek_token (parser)->location;
4574 c_parser_consume_token (parser);
4576 else
4577 break;
4578 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4579 break;
4582 c_token *next_tok = c_parser_peek_token (parser);
4583 if (next_tok->type != CPP_CLOSE_BRACE)
4585 ret.value = error_mark_node;
4586 ret.original_code = ERROR_MARK;
4587 ret.original_type = NULL;
4588 braces.skip_until_found_close (parser);
4589 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4590 obstack_free (&braced_init_obstack, NULL);
4591 return ret;
4593 location_t close_loc = next_tok->location;
4594 c_parser_consume_token (parser);
4595 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4596 obstack_free (&braced_init_obstack, NULL);
4597 set_c_expr_source_range (&ret, brace_loc, close_loc);
4598 return ret;
4601 /* Parse a nested initializer, including designators. */
4603 static void
4604 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4606 /* Parse any designator or designator list. A single array
4607 designator may have the subsequent "=" omitted in GNU C, but a
4608 longer list or a structure member designator may not. */
4609 if (c_parser_next_token_is (parser, CPP_NAME)
4610 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4612 /* Old-style structure member designator. */
4613 set_init_label (c_parser_peek_token (parser)->location,
4614 c_parser_peek_token (parser)->value,
4615 c_parser_peek_token (parser)->location,
4616 braced_init_obstack);
4617 /* Use the colon as the error location. */
4618 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4619 "obsolete use of designated initializer with %<:%>");
4620 c_parser_consume_token (parser);
4621 c_parser_consume_token (parser);
4623 else
4625 /* des_seen is 0 if there have been no designators, 1 if there
4626 has been a single array designator and 2 otherwise. */
4627 int des_seen = 0;
4628 /* Location of a designator. */
4629 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4630 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4631 || c_parser_next_token_is (parser, CPP_DOT))
4633 int des_prev = des_seen;
4634 if (!des_seen)
4635 des_loc = c_parser_peek_token (parser)->location;
4636 if (des_seen < 2)
4637 des_seen++;
4638 if (c_parser_next_token_is (parser, CPP_DOT))
4640 des_seen = 2;
4641 c_parser_consume_token (parser);
4642 if (c_parser_next_token_is (parser, CPP_NAME))
4644 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4645 c_parser_peek_token (parser)->location,
4646 braced_init_obstack);
4647 c_parser_consume_token (parser);
4649 else
4651 struct c_expr init;
4652 init.value = error_mark_node;
4653 init.original_code = ERROR_MARK;
4654 init.original_type = NULL;
4655 c_parser_error (parser, "expected identifier");
4656 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4657 process_init_element (input_location, init, false,
4658 braced_init_obstack);
4659 return;
4662 else
4664 tree first, second;
4665 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4666 location_t array_index_loc = UNKNOWN_LOCATION;
4667 /* ??? Following the old parser, [ objc-receiver
4668 objc-message-args ] is accepted as an initializer,
4669 being distinguished from a designator by what follows
4670 the first assignment expression inside the square
4671 brackets, but after a first array designator a
4672 subsequent square bracket is for Objective-C taken to
4673 start an expression, using the obsolete form of
4674 designated initializer without '=', rather than
4675 possibly being a second level of designation: in LALR
4676 terms, the '[' is shifted rather than reducing
4677 designator to designator-list. */
4678 if (des_prev == 1 && c_dialect_objc ())
4680 des_seen = des_prev;
4681 break;
4683 if (des_prev == 0 && c_dialect_objc ())
4685 /* This might be an array designator or an
4686 Objective-C message expression. If the former,
4687 continue parsing here; if the latter, parse the
4688 remainder of the initializer given the starting
4689 primary-expression. ??? It might make sense to
4690 distinguish when des_prev == 1 as well; see
4691 previous comment. */
4692 tree rec, args;
4693 struct c_expr mexpr;
4694 c_parser_consume_token (parser);
4695 if (c_parser_peek_token (parser)->type == CPP_NAME
4696 && ((c_parser_peek_token (parser)->id_kind
4697 == C_ID_TYPENAME)
4698 || (c_parser_peek_token (parser)->id_kind
4699 == C_ID_CLASSNAME)))
4701 /* Type name receiver. */
4702 tree id = c_parser_peek_token (parser)->value;
4703 c_parser_consume_token (parser);
4704 rec = objc_get_class_reference (id);
4705 goto parse_message_args;
4707 first = c_parser_expr_no_commas (parser, NULL).value;
4708 mark_exp_read (first);
4709 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4710 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4711 goto array_desig_after_first;
4712 /* Expression receiver. So far only one part
4713 without commas has been parsed; there might be
4714 more of the expression. */
4715 rec = first;
4716 while (c_parser_next_token_is (parser, CPP_COMMA))
4718 struct c_expr next;
4719 location_t comma_loc, exp_loc;
4720 comma_loc = c_parser_peek_token (parser)->location;
4721 c_parser_consume_token (parser);
4722 exp_loc = c_parser_peek_token (parser)->location;
4723 next = c_parser_expr_no_commas (parser, NULL);
4724 next = convert_lvalue_to_rvalue (exp_loc, next,
4725 true, true);
4726 rec = build_compound_expr (comma_loc, rec, next.value);
4728 parse_message_args:
4729 /* Now parse the objc-message-args. */
4730 args = c_parser_objc_message_args (parser);
4731 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4732 "expected %<]%>");
4733 mexpr.value
4734 = objc_build_message_expr (rec, args);
4735 mexpr.original_code = ERROR_MARK;
4736 mexpr.original_type = NULL;
4737 /* Now parse and process the remainder of the
4738 initializer, starting with this message
4739 expression as a primary-expression. */
4740 c_parser_initval (parser, &mexpr, braced_init_obstack);
4741 return;
4743 c_parser_consume_token (parser);
4744 array_index_loc = c_parser_peek_token (parser)->location;
4745 first = c_parser_expr_no_commas (parser, NULL).value;
4746 mark_exp_read (first);
4747 array_desig_after_first:
4748 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4750 ellipsis_loc = c_parser_peek_token (parser)->location;
4751 c_parser_consume_token (parser);
4752 second = c_parser_expr_no_commas (parser, NULL).value;
4753 mark_exp_read (second);
4755 else
4756 second = NULL_TREE;
4757 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4759 c_parser_consume_token (parser);
4760 set_init_index (array_index_loc, first, second,
4761 braced_init_obstack);
4762 if (second)
4763 pedwarn (ellipsis_loc, OPT_Wpedantic,
4764 "ISO C forbids specifying range of elements to initialize");
4766 else
4767 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4768 "expected %<]%>");
4771 if (des_seen >= 1)
4773 if (c_parser_next_token_is (parser, CPP_EQ))
4775 pedwarn_c90 (des_loc, OPT_Wpedantic,
4776 "ISO C90 forbids specifying subobject "
4777 "to initialize");
4778 c_parser_consume_token (parser);
4780 else
4782 if (des_seen == 1)
4783 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4784 "obsolete use of designated initializer without %<=%>");
4785 else
4787 struct c_expr init;
4788 init.value = error_mark_node;
4789 init.original_code = ERROR_MARK;
4790 init.original_type = NULL;
4791 c_parser_error (parser, "expected %<=%>");
4792 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4793 process_init_element (input_location, init, false,
4794 braced_init_obstack);
4795 return;
4800 c_parser_initval (parser, NULL, braced_init_obstack);
4803 /* Parse a nested initializer; as c_parser_initializer but parses
4804 initializers within braced lists, after any designators have been
4805 applied. If AFTER is not NULL then it is an Objective-C message
4806 expression which is the primary-expression starting the
4807 initializer. */
4809 static void
4810 c_parser_initval (c_parser *parser, struct c_expr *after,
4811 struct obstack * braced_init_obstack)
4813 struct c_expr init;
4814 gcc_assert (!after || c_dialect_objc ());
4815 location_t loc = c_parser_peek_token (parser)->location;
4817 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4818 init = c_parser_braced_init (parser, NULL_TREE, true,
4819 braced_init_obstack);
4820 else
4822 init = c_parser_expr_no_commas (parser, after);
4823 if (init.value != NULL_TREE
4824 && TREE_CODE (init.value) != STRING_CST
4825 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4826 init = convert_lvalue_to_rvalue (loc, init, true, true);
4828 process_init_element (loc, init, false, braced_init_obstack);
4831 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4832 C99 6.8.2, C11 6.8.2).
4834 compound-statement:
4835 { block-item-list[opt] }
4836 { label-declarations block-item-list }
4838 block-item-list:
4839 block-item
4840 block-item-list block-item
4842 block-item:
4843 nested-declaration
4844 statement
4846 nested-declaration:
4847 declaration
4849 GNU extensions:
4851 compound-statement:
4852 { label-declarations block-item-list }
4854 nested-declaration:
4855 __extension__ nested-declaration
4856 nested-function-definition
4858 label-declarations:
4859 label-declaration
4860 label-declarations label-declaration
4862 label-declaration:
4863 __label__ identifier-list ;
4865 Allowing the mixing of declarations and code is new in C99. The
4866 GNU syntax also permits (not shown above) labels at the end of
4867 compound statements, which yield an error. We don't allow labels
4868 on declarations; this might seem like a natural extension, but
4869 there would be a conflict between attributes on the label and
4870 prefix attributes on the declaration. ??? The syntax follows the
4871 old parser in requiring something after label declarations.
4872 Although they are erroneous if the labels declared aren't defined,
4873 is it useful for the syntax to be this way?
4875 OpenACC:
4877 block-item:
4878 openacc-directive
4880 openacc-directive:
4881 update-directive
4883 OpenMP:
4885 block-item:
4886 openmp-directive
4888 openmp-directive:
4889 barrier-directive
4890 flush-directive
4891 taskwait-directive
4892 taskyield-directive
4893 cancel-directive
4894 cancellation-point-directive */
4896 static tree
4897 c_parser_compound_statement (c_parser *parser)
4899 tree stmt;
4900 location_t brace_loc;
4901 brace_loc = c_parser_peek_token (parser)->location;
4902 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4904 /* Ensure a scope is entered and left anyway to avoid confusion
4905 if we have just prepared to enter a function body. */
4906 stmt = c_begin_compound_stmt (true);
4907 c_end_compound_stmt (brace_loc, stmt, true);
4908 return error_mark_node;
4910 stmt = c_begin_compound_stmt (true);
4911 c_parser_compound_statement_nostart (parser);
4913 return c_end_compound_stmt (brace_loc, stmt, true);
4916 /* Parse a compound statement except for the opening brace. This is
4917 used for parsing both compound statements and statement expressions
4918 (which follow different paths to handling the opening). */
4920 static void
4921 c_parser_compound_statement_nostart (c_parser *parser)
4923 bool last_stmt = false;
4924 bool last_label = false;
4925 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4926 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4927 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4929 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4930 c_parser_consume_token (parser);
4931 return;
4933 mark_valid_location_for_stdc_pragma (true);
4934 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4936 /* Read zero or more forward-declarations for labels that nested
4937 functions can jump to. */
4938 mark_valid_location_for_stdc_pragma (false);
4939 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4941 label_loc = c_parser_peek_token (parser)->location;
4942 c_parser_consume_token (parser);
4943 /* Any identifiers, including those declared as type names,
4944 are OK here. */
4945 while (true)
4947 tree label;
4948 if (c_parser_next_token_is_not (parser, CPP_NAME))
4950 c_parser_error (parser, "expected identifier");
4951 break;
4953 label
4954 = declare_label (c_parser_peek_token (parser)->value);
4955 C_DECLARED_LABEL_FLAG (label) = 1;
4956 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4957 c_parser_consume_token (parser);
4958 if (c_parser_next_token_is (parser, CPP_COMMA))
4959 c_parser_consume_token (parser);
4960 else
4961 break;
4963 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4965 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4967 /* We must now have at least one statement, label or declaration. */
4968 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4970 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4971 c_parser_error (parser, "expected declaration or statement");
4972 c_parser_consume_token (parser);
4973 return;
4975 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4977 location_t loc = c_parser_peek_token (parser)->location;
4978 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4979 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4980 || (c_parser_next_token_is (parser, CPP_NAME)
4981 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4983 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4984 label_loc = c_parser_peek_2nd_token (parser)->location;
4985 else
4986 label_loc = c_parser_peek_token (parser)->location;
4987 last_label = true;
4988 last_stmt = false;
4989 mark_valid_location_for_stdc_pragma (false);
4990 c_parser_label (parser);
4992 else if (!last_label
4993 && c_parser_next_tokens_start_declaration (parser))
4995 last_label = false;
4996 mark_valid_location_for_stdc_pragma (false);
4997 bool fallthru_attr_p = false;
4998 c_parser_declaration_or_fndef (parser, true, true, true, true,
4999 true, NULL, vNULL, NULL,
5000 &fallthru_attr_p);
5001 if (last_stmt && !fallthru_attr_p)
5002 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5003 "ISO C90 forbids mixed declarations and code");
5004 last_stmt = fallthru_attr_p;
5006 else if (!last_label
5007 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5009 /* __extension__ can start a declaration, but is also an
5010 unary operator that can start an expression. Consume all
5011 but the last of a possible series of __extension__ to
5012 determine which. */
5013 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5014 && (c_parser_peek_2nd_token (parser)->keyword
5015 == RID_EXTENSION))
5016 c_parser_consume_token (parser);
5017 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5019 int ext;
5020 ext = disable_extension_diagnostics ();
5021 c_parser_consume_token (parser);
5022 last_label = false;
5023 mark_valid_location_for_stdc_pragma (false);
5024 c_parser_declaration_or_fndef (parser, true, true, true, true,
5025 true, NULL, vNULL);
5026 /* Following the old parser, __extension__ does not
5027 disable this diagnostic. */
5028 restore_extension_diagnostics (ext);
5029 if (last_stmt)
5030 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5031 "ISO C90 forbids mixed declarations and code");
5032 last_stmt = false;
5034 else
5035 goto statement;
5037 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5039 /* External pragmas, and some omp pragmas, are not associated
5040 with regular c code, and so are not to be considered statements
5041 syntactically. This ensures that the user doesn't put them
5042 places that would turn into syntax errors if the directive
5043 were ignored. */
5044 if (c_parser_pragma (parser,
5045 last_label ? pragma_stmt : pragma_compound,
5046 NULL))
5047 last_label = false, last_stmt = true;
5049 else if (c_parser_next_token_is (parser, CPP_EOF))
5051 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5052 c_parser_error (parser, "expected declaration or statement");
5053 return;
5055 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5057 if (parser->in_if_block)
5059 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5060 error_at (loc, "expected %<}%> before %<else%>");
5061 return;
5063 else
5065 error_at (loc, "%<else%> without a previous %<if%>");
5066 c_parser_consume_token (parser);
5067 continue;
5070 else
5072 statement:
5073 last_label = false;
5074 last_stmt = true;
5075 mark_valid_location_for_stdc_pragma (false);
5076 c_parser_statement_after_labels (parser, NULL);
5079 parser->error = false;
5081 if (last_label)
5082 error_at (label_loc, "label at end of compound statement");
5083 c_parser_consume_token (parser);
5084 /* Restore the value we started with. */
5085 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5088 /* Parse all consecutive labels. */
5090 static void
5091 c_parser_all_labels (c_parser *parser)
5093 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5094 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5095 || (c_parser_next_token_is (parser, CPP_NAME)
5096 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5097 c_parser_label (parser);
5100 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5102 label:
5103 identifier : attributes[opt]
5104 case constant-expression :
5105 default :
5107 GNU extensions:
5109 label:
5110 case constant-expression ... constant-expression :
5112 The use of attributes on labels is a GNU extension. The syntax in
5113 GNU C accepts any expressions without commas, non-constant
5114 expressions being rejected later. */
5116 static void
5117 c_parser_label (c_parser *parser)
5119 location_t loc1 = c_parser_peek_token (parser)->location;
5120 tree label = NULL_TREE;
5122 /* Remember whether this case or a user-defined label is allowed to fall
5123 through to. */
5124 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5126 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5128 tree exp1, exp2;
5129 c_parser_consume_token (parser);
5130 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5131 if (c_parser_next_token_is (parser, CPP_COLON))
5133 c_parser_consume_token (parser);
5134 label = do_case (loc1, exp1, NULL_TREE);
5136 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5138 c_parser_consume_token (parser);
5139 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5140 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5141 label = do_case (loc1, exp1, exp2);
5143 else
5144 c_parser_error (parser, "expected %<:%> or %<...%>");
5146 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5148 c_parser_consume_token (parser);
5149 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5150 label = do_case (loc1, NULL_TREE, NULL_TREE);
5152 else
5154 tree name = c_parser_peek_token (parser)->value;
5155 tree tlab;
5156 tree attrs;
5157 location_t loc2 = c_parser_peek_token (parser)->location;
5158 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5159 c_parser_consume_token (parser);
5160 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5161 c_parser_consume_token (parser);
5162 attrs = c_parser_attributes (parser);
5163 tlab = define_label (loc2, name);
5164 if (tlab)
5166 decl_attributes (&tlab, attrs, 0);
5167 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5170 if (label)
5172 if (TREE_CODE (label) == LABEL_EXPR)
5173 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5174 else
5175 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5177 /* Allow '__attribute__((fallthrough));'. */
5178 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5180 location_t loc = c_parser_peek_token (parser)->location;
5181 tree attrs = c_parser_attributes (parser);
5182 if (attribute_fallthrough_p (attrs))
5184 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5186 tree fn = build_call_expr_internal_loc (loc,
5187 IFN_FALLTHROUGH,
5188 void_type_node, 0);
5189 add_stmt (fn);
5191 else
5192 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5193 "not followed by %<;%>");
5195 else if (attrs != NULL_TREE)
5196 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5197 " can be applied to a null statement");
5199 if (c_parser_next_tokens_start_declaration (parser))
5201 error_at (c_parser_peek_token (parser)->location,
5202 "a label can only be part of a statement and "
5203 "a declaration is not a statement");
5204 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5205 /*static_assert_ok*/ true,
5206 /*empty_ok*/ true, /*nested*/ true,
5207 /*start_attr_ok*/ true, NULL,
5208 vNULL);
5213 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5215 statement:
5216 labeled-statement
5217 compound-statement
5218 expression-statement
5219 selection-statement
5220 iteration-statement
5221 jump-statement
5223 labeled-statement:
5224 label statement
5226 expression-statement:
5227 expression[opt] ;
5229 selection-statement:
5230 if-statement
5231 switch-statement
5233 iteration-statement:
5234 while-statement
5235 do-statement
5236 for-statement
5238 jump-statement:
5239 goto identifier ;
5240 continue ;
5241 break ;
5242 return expression[opt] ;
5244 GNU extensions:
5246 statement:
5247 asm-statement
5249 jump-statement:
5250 goto * expression ;
5252 expression-statement:
5253 attributes ;
5255 Objective-C:
5257 statement:
5258 objc-throw-statement
5259 objc-try-catch-statement
5260 objc-synchronized-statement
5262 objc-throw-statement:
5263 @throw expression ;
5264 @throw ;
5266 OpenACC:
5268 statement:
5269 openacc-construct
5271 openacc-construct:
5272 parallel-construct
5273 kernels-construct
5274 data-construct
5275 loop-construct
5277 parallel-construct:
5278 parallel-directive structured-block
5280 kernels-construct:
5281 kernels-directive structured-block
5283 data-construct:
5284 data-directive structured-block
5286 loop-construct:
5287 loop-directive structured-block
5289 OpenMP:
5291 statement:
5292 openmp-construct
5294 openmp-construct:
5295 parallel-construct
5296 for-construct
5297 simd-construct
5298 for-simd-construct
5299 sections-construct
5300 single-construct
5301 parallel-for-construct
5302 parallel-for-simd-construct
5303 parallel-sections-construct
5304 master-construct
5305 critical-construct
5306 atomic-construct
5307 ordered-construct
5309 parallel-construct:
5310 parallel-directive structured-block
5312 for-construct:
5313 for-directive iteration-statement
5315 simd-construct:
5316 simd-directive iteration-statements
5318 for-simd-construct:
5319 for-simd-directive iteration-statements
5321 sections-construct:
5322 sections-directive section-scope
5324 single-construct:
5325 single-directive structured-block
5327 parallel-for-construct:
5328 parallel-for-directive iteration-statement
5330 parallel-for-simd-construct:
5331 parallel-for-simd-directive iteration-statement
5333 parallel-sections-construct:
5334 parallel-sections-directive section-scope
5336 master-construct:
5337 master-directive structured-block
5339 critical-construct:
5340 critical-directive structured-block
5342 atomic-construct:
5343 atomic-directive expression-statement
5345 ordered-construct:
5346 ordered-directive structured-block
5348 Transactional Memory:
5350 statement:
5351 transaction-statement
5352 transaction-cancel-statement
5354 IF_P is used to track whether there's a (possibly labeled) if statement
5355 which is not enclosed in braces and has an else clause. This is used to
5356 implement -Wparentheses. */
5358 static void
5359 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5361 c_parser_all_labels (parser);
5362 if (loc_after_labels)
5363 *loc_after_labels = c_parser_peek_token (parser)->location;
5364 c_parser_statement_after_labels (parser, if_p, NULL);
5367 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5368 of if-else-if conditions.
5370 IF_P is used to track whether there's a (possibly labeled) if statement
5371 which is not enclosed in braces and has an else clause. This is used to
5372 implement -Wparentheses. */
5374 static void
5375 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5376 vec<tree> *chain)
5378 location_t loc = c_parser_peek_token (parser)->location;
5379 tree stmt = NULL_TREE;
5380 bool in_if_block = parser->in_if_block;
5381 parser->in_if_block = false;
5382 if (if_p != NULL)
5383 *if_p = false;
5385 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5386 add_debug_begin_stmt (loc);
5388 switch (c_parser_peek_token (parser)->type)
5390 case CPP_OPEN_BRACE:
5391 add_stmt (c_parser_compound_statement (parser));
5392 break;
5393 case CPP_KEYWORD:
5394 switch (c_parser_peek_token (parser)->keyword)
5396 case RID_IF:
5397 c_parser_if_statement (parser, if_p, chain);
5398 break;
5399 case RID_SWITCH:
5400 c_parser_switch_statement (parser, if_p);
5401 break;
5402 case RID_WHILE:
5403 c_parser_while_statement (parser, false, if_p);
5404 break;
5405 case RID_DO:
5406 c_parser_do_statement (parser, false);
5407 break;
5408 case RID_FOR:
5409 c_parser_for_statement (parser, false, if_p);
5410 break;
5411 case RID_GOTO:
5412 c_parser_consume_token (parser);
5413 if (c_parser_next_token_is (parser, CPP_NAME))
5415 stmt = c_finish_goto_label (loc,
5416 c_parser_peek_token (parser)->value);
5417 c_parser_consume_token (parser);
5419 else if (c_parser_next_token_is (parser, CPP_MULT))
5421 struct c_expr val;
5423 c_parser_consume_token (parser);
5424 val = c_parser_expression (parser);
5425 val = convert_lvalue_to_rvalue (loc, val, false, true);
5426 stmt = c_finish_goto_ptr (loc, val.value);
5428 else
5429 c_parser_error (parser, "expected identifier or %<*%>");
5430 goto expect_semicolon;
5431 case RID_CONTINUE:
5432 c_parser_consume_token (parser);
5433 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5434 goto expect_semicolon;
5435 case RID_BREAK:
5436 c_parser_consume_token (parser);
5437 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5438 goto expect_semicolon;
5439 case RID_RETURN:
5440 c_parser_consume_token (parser);
5441 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5443 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5444 c_parser_consume_token (parser);
5446 else
5448 location_t xloc = c_parser_peek_token (parser)->location;
5449 struct c_expr expr = c_parser_expression_conv (parser);
5450 mark_exp_read (expr.value);
5451 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5452 expr.value, expr.original_type);
5453 goto expect_semicolon;
5455 break;
5456 case RID_ASM:
5457 stmt = c_parser_asm_statement (parser);
5458 break;
5459 case RID_TRANSACTION_ATOMIC:
5460 case RID_TRANSACTION_RELAXED:
5461 stmt = c_parser_transaction (parser,
5462 c_parser_peek_token (parser)->keyword);
5463 break;
5464 case RID_TRANSACTION_CANCEL:
5465 stmt = c_parser_transaction_cancel (parser);
5466 goto expect_semicolon;
5467 case RID_AT_THROW:
5468 gcc_assert (c_dialect_objc ());
5469 c_parser_consume_token (parser);
5470 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5472 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5473 c_parser_consume_token (parser);
5475 else
5477 struct c_expr expr = c_parser_expression (parser);
5478 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5479 expr.value = c_fully_fold (expr.value, false, NULL);
5480 stmt = objc_build_throw_stmt (loc, expr.value);
5481 goto expect_semicolon;
5483 break;
5484 case RID_AT_TRY:
5485 gcc_assert (c_dialect_objc ());
5486 c_parser_objc_try_catch_finally_statement (parser);
5487 break;
5488 case RID_AT_SYNCHRONIZED:
5489 gcc_assert (c_dialect_objc ());
5490 c_parser_objc_synchronized_statement (parser);
5491 break;
5492 case RID_ATTRIBUTE:
5494 /* Allow '__attribute__((fallthrough));'. */
5495 tree attrs = c_parser_attributes (parser);
5496 if (attribute_fallthrough_p (attrs))
5498 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5500 tree fn = build_call_expr_internal_loc (loc,
5501 IFN_FALLTHROUGH,
5502 void_type_node, 0);
5503 add_stmt (fn);
5504 /* Eat the ';'. */
5505 c_parser_consume_token (parser);
5507 else
5508 warning_at (loc, OPT_Wattributes,
5509 "%<fallthrough%> attribute not followed "
5510 "by %<;%>");
5512 else if (attrs != NULL_TREE)
5513 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5514 " can be applied to a null statement");
5515 break;
5517 default:
5518 goto expr_stmt;
5520 break;
5521 case CPP_SEMICOLON:
5522 c_parser_consume_token (parser);
5523 break;
5524 case CPP_CLOSE_PAREN:
5525 case CPP_CLOSE_SQUARE:
5526 /* Avoid infinite loop in error recovery:
5527 c_parser_skip_until_found stops at a closing nesting
5528 delimiter without consuming it, but here we need to consume
5529 it to proceed further. */
5530 c_parser_error (parser, "expected statement");
5531 c_parser_consume_token (parser);
5532 break;
5533 case CPP_PRAGMA:
5534 c_parser_pragma (parser, pragma_stmt, if_p);
5535 break;
5536 default:
5537 expr_stmt:
5538 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5539 expect_semicolon:
5540 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5541 break;
5543 /* Two cases cannot and do not have line numbers associated: If stmt
5544 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5545 cannot hold line numbers. But that's OK because the statement
5546 will either be changed to a MODIFY_EXPR during gimplification of
5547 the statement expr, or discarded. If stmt was compound, but
5548 without new variables, we will have skipped the creation of a
5549 BIND and will have a bare STATEMENT_LIST. But that's OK because
5550 (recursively) all of the component statements should already have
5551 line numbers assigned. ??? Can we discard no-op statements
5552 earlier? */
5553 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5554 protected_set_expr_location (stmt, loc);
5556 parser->in_if_block = in_if_block;
5559 /* Parse the condition from an if, do, while or for statements. */
5561 static tree
5562 c_parser_condition (c_parser *parser)
5564 location_t loc = c_parser_peek_token (parser)->location;
5565 tree cond;
5566 cond = c_parser_expression_conv (parser).value;
5567 cond = c_objc_common_truthvalue_conversion (loc, cond);
5568 cond = c_fully_fold (cond, false, NULL);
5569 if (warn_sequence_point)
5570 verify_sequence_points (cond);
5571 return cond;
5574 /* Parse a parenthesized condition from an if, do or while statement.
5576 condition:
5577 ( expression )
5579 static tree
5580 c_parser_paren_condition (c_parser *parser)
5582 tree cond;
5583 matching_parens parens;
5584 if (!parens.require_open (parser))
5585 return error_mark_node;
5586 cond = c_parser_condition (parser);
5587 parens.skip_until_found_close (parser);
5588 return cond;
5591 /* Parse a statement which is a block in C99.
5593 IF_P is used to track whether there's a (possibly labeled) if statement
5594 which is not enclosed in braces and has an else clause. This is used to
5595 implement -Wparentheses. */
5597 static tree
5598 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5599 location_t *loc_after_labels)
5601 tree block = c_begin_compound_stmt (flag_isoc99);
5602 location_t loc = c_parser_peek_token (parser)->location;
5603 c_parser_statement (parser, if_p, loc_after_labels);
5604 return c_end_compound_stmt (loc, block, flag_isoc99);
5607 /* Parse the body of an if statement. This is just parsing a
5608 statement but (a) it is a block in C99, (b) we track whether the
5609 body is an if statement for the sake of -Wparentheses warnings, (c)
5610 we handle an empty body specially for the sake of -Wempty-body
5611 warnings, and (d) we call parser_compound_statement directly
5612 because c_parser_statement_after_labels resets
5613 parser->in_if_block.
5615 IF_P is used to track whether there's a (possibly labeled) if statement
5616 which is not enclosed in braces and has an else clause. This is used to
5617 implement -Wparentheses. */
5619 static tree
5620 c_parser_if_body (c_parser *parser, bool *if_p,
5621 const token_indent_info &if_tinfo)
5623 tree block = c_begin_compound_stmt (flag_isoc99);
5624 location_t body_loc = c_parser_peek_token (parser)->location;
5625 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5626 token_indent_info body_tinfo
5627 = get_token_indent_info (c_parser_peek_token (parser));
5629 c_parser_all_labels (parser);
5630 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5632 location_t loc = c_parser_peek_token (parser)->location;
5633 add_stmt (build_empty_stmt (loc));
5634 c_parser_consume_token (parser);
5635 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5636 warning_at (loc, OPT_Wempty_body,
5637 "suggest braces around empty body in an %<if%> statement");
5639 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5640 add_stmt (c_parser_compound_statement (parser));
5641 else
5643 body_loc_after_labels = c_parser_peek_token (parser)->location;
5644 c_parser_statement_after_labels (parser, if_p);
5647 token_indent_info next_tinfo
5648 = get_token_indent_info (c_parser_peek_token (parser));
5649 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5650 if (body_loc_after_labels != UNKNOWN_LOCATION
5651 && next_tinfo.type != CPP_SEMICOLON)
5652 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5653 if_tinfo.location, RID_IF);
5655 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5658 /* Parse the else body of an if statement. This is just parsing a
5659 statement but (a) it is a block in C99, (b) we handle an empty body
5660 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5661 of if-else-if conditions. */
5663 static tree
5664 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5665 vec<tree> *chain)
5667 location_t body_loc = c_parser_peek_token (parser)->location;
5668 tree block = c_begin_compound_stmt (flag_isoc99);
5669 token_indent_info body_tinfo
5670 = get_token_indent_info (c_parser_peek_token (parser));
5671 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5673 c_parser_all_labels (parser);
5674 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5676 location_t loc = c_parser_peek_token (parser)->location;
5677 warning_at (loc,
5678 OPT_Wempty_body,
5679 "suggest braces around empty body in an %<else%> statement");
5680 add_stmt (build_empty_stmt (loc));
5681 c_parser_consume_token (parser);
5683 else
5685 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5686 body_loc_after_labels = c_parser_peek_token (parser)->location;
5687 c_parser_statement_after_labels (parser, NULL, chain);
5690 token_indent_info next_tinfo
5691 = get_token_indent_info (c_parser_peek_token (parser));
5692 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5693 if (body_loc_after_labels != UNKNOWN_LOCATION
5694 && next_tinfo.type != CPP_SEMICOLON)
5695 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5696 else_tinfo.location, RID_ELSE);
5698 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5701 /* We might need to reclassify any previously-lexed identifier, e.g.
5702 when we've left a for loop with an if-statement without else in the
5703 body - we might have used a wrong scope for the token. See PR67784. */
5705 static void
5706 c_parser_maybe_reclassify_token (c_parser *parser)
5708 if (c_parser_next_token_is (parser, CPP_NAME))
5710 c_token *token = c_parser_peek_token (parser);
5712 if (token->id_kind != C_ID_CLASSNAME)
5714 tree decl = lookup_name (token->value);
5716 token->id_kind = C_ID_ID;
5717 if (decl)
5719 if (TREE_CODE (decl) == TYPE_DECL)
5720 token->id_kind = C_ID_TYPENAME;
5722 else if (c_dialect_objc ())
5724 tree objc_interface_decl = objc_is_class_name (token->value);
5725 /* Objective-C class names are in the same namespace as
5726 variables and typedefs, and hence are shadowed by local
5727 declarations. */
5728 if (objc_interface_decl)
5730 token->value = objc_interface_decl;
5731 token->id_kind = C_ID_CLASSNAME;
5738 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5740 if-statement:
5741 if ( expression ) statement
5742 if ( expression ) statement else statement
5744 CHAIN is a vector of if-else-if conditions.
5745 IF_P is used to track whether there's a (possibly labeled) if statement
5746 which is not enclosed in braces and has an else clause. This is used to
5747 implement -Wparentheses. */
5749 static void
5750 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5752 tree block;
5753 location_t loc;
5754 tree cond;
5755 bool nested_if = false;
5756 tree first_body, second_body;
5757 bool in_if_block;
5759 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5760 token_indent_info if_tinfo
5761 = get_token_indent_info (c_parser_peek_token (parser));
5762 c_parser_consume_token (parser);
5763 block = c_begin_compound_stmt (flag_isoc99);
5764 loc = c_parser_peek_token (parser)->location;
5765 cond = c_parser_paren_condition (parser);
5766 in_if_block = parser->in_if_block;
5767 parser->in_if_block = true;
5768 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5769 parser->in_if_block = in_if_block;
5771 if (warn_duplicated_cond)
5772 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5774 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5776 token_indent_info else_tinfo
5777 = get_token_indent_info (c_parser_peek_token (parser));
5778 c_parser_consume_token (parser);
5779 if (warn_duplicated_cond)
5781 if (c_parser_next_token_is_keyword (parser, RID_IF)
5782 && chain == NULL)
5784 /* We've got "if (COND) else if (COND2)". Start the
5785 condition chain and add COND as the first element. */
5786 chain = new vec<tree> ();
5787 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5788 chain->safe_push (cond);
5790 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5792 /* This is if-else without subsequent if. Zap the condition
5793 chain; we would have already warned at this point. */
5794 delete chain;
5795 chain = NULL;
5798 second_body = c_parser_else_body (parser, else_tinfo, chain);
5799 /* Set IF_P to true to indicate that this if statement has an
5800 else clause. This may trigger the Wparentheses warning
5801 below when we get back up to the parent if statement. */
5802 if (if_p != NULL)
5803 *if_p = true;
5805 else
5807 second_body = NULL_TREE;
5809 /* Diagnose an ambiguous else if if-then-else is nested inside
5810 if-then. */
5811 if (nested_if)
5812 warning_at (loc, OPT_Wdangling_else,
5813 "suggest explicit braces to avoid ambiguous %<else%>");
5815 if (warn_duplicated_cond)
5817 /* This if statement does not have an else clause. We don't
5818 need the condition chain anymore. */
5819 delete chain;
5820 chain = NULL;
5823 c_finish_if_stmt (loc, cond, first_body, second_body);
5824 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5826 c_parser_maybe_reclassify_token (parser);
5829 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5831 switch-statement:
5832 switch (expression) statement
5835 static void
5836 c_parser_switch_statement (c_parser *parser, bool *if_p)
5838 struct c_expr ce;
5839 tree block, expr, body, save_break;
5840 location_t switch_loc = c_parser_peek_token (parser)->location;
5841 location_t switch_cond_loc;
5842 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5843 c_parser_consume_token (parser);
5844 block = c_begin_compound_stmt (flag_isoc99);
5845 bool explicit_cast_p = false;
5846 matching_parens parens;
5847 if (parens.require_open (parser))
5849 switch_cond_loc = c_parser_peek_token (parser)->location;
5850 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5851 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5852 explicit_cast_p = true;
5853 ce = c_parser_expression (parser);
5854 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5855 expr = ce.value;
5856 /* ??? expr has no valid location? */
5857 parens.skip_until_found_close (parser);
5859 else
5861 switch_cond_loc = UNKNOWN_LOCATION;
5862 expr = error_mark_node;
5863 ce.original_type = error_mark_node;
5865 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5866 save_break = c_break_label;
5867 c_break_label = NULL_TREE;
5868 location_t loc_after_labels;
5869 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5870 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5871 location_t next_loc = c_parser_peek_token (parser)->location;
5872 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5873 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5874 RID_SWITCH);
5875 if (c_break_label)
5877 location_t here = c_parser_peek_token (parser)->location;
5878 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5879 SET_EXPR_LOCATION (t, here);
5880 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5881 append_to_statement_list_force (t, &body);
5883 c_finish_case (body, ce.original_type);
5884 c_break_label = save_break;
5885 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5886 c_parser_maybe_reclassify_token (parser);
5889 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5891 while-statement:
5892 while (expression) statement
5894 IF_P is used to track whether there's a (possibly labeled) if statement
5895 which is not enclosed in braces and has an else clause. This is used to
5896 implement -Wparentheses. */
5898 static void
5899 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5901 tree block, cond, body, save_break, save_cont;
5902 location_t loc;
5903 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5904 token_indent_info while_tinfo
5905 = get_token_indent_info (c_parser_peek_token (parser));
5906 c_parser_consume_token (parser);
5907 block = c_begin_compound_stmt (flag_isoc99);
5908 loc = c_parser_peek_token (parser)->location;
5909 cond = c_parser_paren_condition (parser);
5910 if (ivdep && cond != error_mark_node)
5911 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5912 build_int_cst (integer_type_node,
5913 annot_expr_ivdep_kind),
5914 integer_zero_node);
5915 save_break = c_break_label;
5916 c_break_label = NULL_TREE;
5917 save_cont = c_cont_label;
5918 c_cont_label = NULL_TREE;
5920 token_indent_info body_tinfo
5921 = get_token_indent_info (c_parser_peek_token (parser));
5923 location_t loc_after_labels;
5924 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5925 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5926 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5927 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5928 c_parser_maybe_reclassify_token (parser);
5930 token_indent_info next_tinfo
5931 = get_token_indent_info (c_parser_peek_token (parser));
5932 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5934 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5935 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5936 while_tinfo.location, RID_WHILE);
5938 c_break_label = save_break;
5939 c_cont_label = save_cont;
5942 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5944 do-statement:
5945 do statement while ( expression ) ;
5948 static void
5949 c_parser_do_statement (c_parser *parser, bool ivdep)
5951 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5952 location_t loc;
5953 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5954 c_parser_consume_token (parser);
5955 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5956 warning_at (c_parser_peek_token (parser)->location,
5957 OPT_Wempty_body,
5958 "suggest braces around empty body in %<do%> statement");
5959 block = c_begin_compound_stmt (flag_isoc99);
5960 loc = c_parser_peek_token (parser)->location;
5961 save_break = c_break_label;
5962 c_break_label = NULL_TREE;
5963 save_cont = c_cont_label;
5964 c_cont_label = NULL_TREE;
5965 body = c_parser_c99_block_statement (parser, NULL);
5966 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5967 new_break = c_break_label;
5968 c_break_label = save_break;
5969 new_cont = c_cont_label;
5970 c_cont_label = save_cont;
5971 cond = c_parser_paren_condition (parser);
5972 if (ivdep && cond != error_mark_node)
5973 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5974 build_int_cst (integer_type_node,
5975 annot_expr_ivdep_kind),
5976 integer_zero_node);
5977 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5978 c_parser_skip_to_end_of_block_or_statement (parser);
5979 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5980 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5983 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5985 for-statement:
5986 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5987 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5989 The form with a declaration is new in C99.
5991 ??? In accordance with the old parser, the declaration may be a
5992 nested function, which is then rejected in check_for_loop_decls,
5993 but does it make any sense for this to be included in the grammar?
5994 Note in particular that the nested function does not include a
5995 trailing ';', whereas the "declaration" production includes one.
5996 Also, can we reject bad declarations earlier and cheaper than
5997 check_for_loop_decls?
5999 In Objective-C, there are two additional variants:
6001 foreach-statement:
6002 for ( expression in expresssion ) statement
6003 for ( declaration in expression ) statement
6005 This is inconsistent with C, because the second variant is allowed
6006 even if c99 is not enabled.
6008 The rest of the comment documents these Objective-C foreach-statement.
6010 Here is the canonical example of the first variant:
6011 for (object in array) { do something with object }
6012 we call the first expression ("object") the "object_expression" and
6013 the second expression ("array") the "collection_expression".
6014 object_expression must be an lvalue of type "id" (a generic Objective-C
6015 object) because the loop works by assigning to object_expression the
6016 various objects from the collection_expression. collection_expression
6017 must evaluate to something of type "id" which responds to the method
6018 countByEnumeratingWithState:objects:count:.
6020 The canonical example of the second variant is:
6021 for (id object in array) { do something with object }
6022 which is completely equivalent to
6024 id object;
6025 for (object in array) { do something with object }
6027 Note that initizializing 'object' in some way (eg, "for ((object =
6028 xxx) in array) { do something with object }") is possibly
6029 technically valid, but completely pointless as 'object' will be
6030 assigned to something else as soon as the loop starts. We should
6031 most likely reject it (TODO).
6033 The beginning of the Objective-C foreach-statement looks exactly
6034 like the beginning of the for-statement, and we can tell it is a
6035 foreach-statement only because the initial declaration or
6036 expression is terminated by 'in' instead of ';'.
6038 IF_P is used to track whether there's a (possibly labeled) if statement
6039 which is not enclosed in braces and has an else clause. This is used to
6040 implement -Wparentheses. */
6042 static void
6043 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
6045 tree block, cond, incr, save_break, save_cont, body;
6046 /* The following are only used when parsing an ObjC foreach statement. */
6047 tree object_expression;
6048 /* Silence the bogus uninitialized warning. */
6049 tree collection_expression = NULL;
6050 location_t loc = c_parser_peek_token (parser)->location;
6051 location_t for_loc = c_parser_peek_token (parser)->location;
6052 bool is_foreach_statement = false;
6053 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6054 token_indent_info for_tinfo
6055 = get_token_indent_info (c_parser_peek_token (parser));
6056 c_parser_consume_token (parser);
6057 /* Open a compound statement in Objective-C as well, just in case this is
6058 as foreach expression. */
6059 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6060 cond = error_mark_node;
6061 incr = error_mark_node;
6062 matching_parens parens;
6063 if (parens.require_open (parser))
6065 /* Parse the initialization declaration or expression. */
6066 object_expression = error_mark_node;
6067 parser->objc_could_be_foreach_context = c_dialect_objc ();
6068 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6070 parser->objc_could_be_foreach_context = false;
6071 c_parser_consume_token (parser);
6072 c_finish_expr_stmt (loc, NULL_TREE);
6074 else if (c_parser_next_tokens_start_declaration (parser))
6076 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6077 &object_expression, vNULL);
6078 parser->objc_could_be_foreach_context = false;
6080 if (c_parser_next_token_is_keyword (parser, RID_IN))
6082 c_parser_consume_token (parser);
6083 is_foreach_statement = true;
6084 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6085 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6087 else
6088 check_for_loop_decls (for_loc, flag_isoc99);
6090 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6092 /* __extension__ can start a declaration, but is also an
6093 unary operator that can start an expression. Consume all
6094 but the last of a possible series of __extension__ to
6095 determine which. */
6096 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6097 && (c_parser_peek_2nd_token (parser)->keyword
6098 == RID_EXTENSION))
6099 c_parser_consume_token (parser);
6100 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6102 int ext;
6103 ext = disable_extension_diagnostics ();
6104 c_parser_consume_token (parser);
6105 c_parser_declaration_or_fndef (parser, true, true, true, true,
6106 true, &object_expression, vNULL);
6107 parser->objc_could_be_foreach_context = false;
6109 restore_extension_diagnostics (ext);
6110 if (c_parser_next_token_is_keyword (parser, RID_IN))
6112 c_parser_consume_token (parser);
6113 is_foreach_statement = true;
6114 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6115 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6117 else
6118 check_for_loop_decls (for_loc, flag_isoc99);
6120 else
6121 goto init_expr;
6123 else
6125 init_expr:
6127 struct c_expr ce;
6128 tree init_expression;
6129 ce = c_parser_expression (parser);
6130 init_expression = ce.value;
6131 parser->objc_could_be_foreach_context = false;
6132 if (c_parser_next_token_is_keyword (parser, RID_IN))
6134 c_parser_consume_token (parser);
6135 is_foreach_statement = true;
6136 if (! lvalue_p (init_expression))
6137 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6138 object_expression = c_fully_fold (init_expression, false, NULL);
6140 else
6142 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6143 init_expression = ce.value;
6144 c_finish_expr_stmt (loc, init_expression);
6145 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6149 /* Parse the loop condition. In the case of a foreach
6150 statement, there is no loop condition. */
6151 gcc_assert (!parser->objc_could_be_foreach_context);
6152 if (!is_foreach_statement)
6154 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6156 if (ivdep)
6158 c_parser_error (parser, "missing loop condition in loop with "
6159 "%<GCC ivdep%> pragma");
6160 cond = error_mark_node;
6162 else
6164 c_parser_consume_token (parser);
6165 cond = NULL_TREE;
6168 else
6170 cond = c_parser_condition (parser);
6171 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6172 "expected %<;%>");
6174 if (ivdep && cond != error_mark_node)
6175 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6176 build_int_cst (integer_type_node,
6177 annot_expr_ivdep_kind),
6178 integer_zero_node);
6180 /* Parse the increment expression (the third expression in a
6181 for-statement). In the case of a foreach-statement, this is
6182 the expression that follows the 'in'. */
6183 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6185 if (is_foreach_statement)
6187 c_parser_error (parser, "missing collection in fast enumeration");
6188 collection_expression = error_mark_node;
6190 else
6191 incr = c_process_expr_stmt (loc, NULL_TREE);
6193 else
6195 if (is_foreach_statement)
6196 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6197 false, NULL);
6198 else
6200 struct c_expr ce = c_parser_expression (parser);
6201 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6202 incr = c_process_expr_stmt (loc, ce.value);
6205 parens.skip_until_found_close (parser);
6207 save_break = c_break_label;
6208 c_break_label = NULL_TREE;
6209 save_cont = c_cont_label;
6210 c_cont_label = NULL_TREE;
6212 token_indent_info body_tinfo
6213 = get_token_indent_info (c_parser_peek_token (parser));
6215 location_t loc_after_labels;
6216 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6217 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6219 if (is_foreach_statement)
6220 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6221 else
6222 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6223 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6224 c_parser_maybe_reclassify_token (parser);
6226 token_indent_info next_tinfo
6227 = get_token_indent_info (c_parser_peek_token (parser));
6228 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6230 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6231 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6232 for_tinfo.location, RID_FOR);
6234 c_break_label = save_break;
6235 c_cont_label = save_cont;
6238 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6239 statement with inputs, outputs, clobbers, and volatile tag
6240 allowed.
6242 asm-statement:
6243 asm type-qualifier[opt] ( asm-argument ) ;
6244 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6246 asm-argument:
6247 asm-string-literal
6248 asm-string-literal : asm-operands[opt]
6249 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6250 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6252 asm-goto-argument:
6253 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6254 : asm-goto-operands
6256 Qualifiers other than volatile are accepted in the syntax but
6257 warned for. */
6259 static tree
6260 c_parser_asm_statement (c_parser *parser)
6262 tree quals, str, outputs, inputs, clobbers, labels, ret;
6263 bool simple, is_goto;
6264 location_t asm_loc = c_parser_peek_token (parser)->location;
6265 int section, nsections;
6267 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6268 c_parser_consume_token (parser);
6269 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6271 quals = c_parser_peek_token (parser)->value;
6272 c_parser_consume_token (parser);
6274 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6275 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6277 warning_at (c_parser_peek_token (parser)->location,
6279 "%E qualifier ignored on asm",
6280 c_parser_peek_token (parser)->value);
6281 quals = NULL_TREE;
6282 c_parser_consume_token (parser);
6284 else
6285 quals = NULL_TREE;
6287 is_goto = false;
6288 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6290 c_parser_consume_token (parser);
6291 is_goto = true;
6294 /* ??? Follow the C++ parser rather than using the
6295 lex_untranslated_string kludge. */
6296 parser->lex_untranslated_string = true;
6297 ret = NULL;
6299 matching_parens parens;
6300 if (!parens.require_open (parser))
6301 goto error;
6303 str = c_parser_asm_string_literal (parser);
6304 if (str == NULL_TREE)
6305 goto error_close_paren;
6307 simple = true;
6308 outputs = NULL_TREE;
6309 inputs = NULL_TREE;
6310 clobbers = NULL_TREE;
6311 labels = NULL_TREE;
6313 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6314 goto done_asm;
6316 /* Parse each colon-delimited section of operands. */
6317 nsections = 3 + is_goto;
6318 for (section = 0; section < nsections; ++section)
6320 if (!c_parser_require (parser, CPP_COLON,
6321 is_goto
6322 ? G_("expected %<:%>")
6323 : G_("expected %<:%> or %<)%>"),
6324 UNKNOWN_LOCATION, is_goto))
6325 goto error_close_paren;
6327 /* Once past any colon, we're no longer a simple asm. */
6328 simple = false;
6330 if ((!c_parser_next_token_is (parser, CPP_COLON)
6331 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6332 || section == 3)
6333 switch (section)
6335 case 0:
6336 /* For asm goto, we don't allow output operands, but reserve
6337 the slot for a future extension that does allow them. */
6338 if (!is_goto)
6339 outputs = c_parser_asm_operands (parser);
6340 break;
6341 case 1:
6342 inputs = c_parser_asm_operands (parser);
6343 break;
6344 case 2:
6345 clobbers = c_parser_asm_clobbers (parser);
6346 break;
6347 case 3:
6348 labels = c_parser_asm_goto_operands (parser);
6349 break;
6350 default:
6351 gcc_unreachable ();
6354 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6355 goto done_asm;
6358 done_asm:
6359 if (!parens.require_close (parser))
6361 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6362 goto error;
6365 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6366 c_parser_skip_to_end_of_block_or_statement (parser);
6368 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6369 clobbers, labels, simple));
6371 error:
6372 parser->lex_untranslated_string = false;
6373 return ret;
6375 error_close_paren:
6376 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6377 goto error;
6380 /* Parse asm operands, a GNU extension.
6382 asm-operands:
6383 asm-operand
6384 asm-operands , asm-operand
6386 asm-operand:
6387 asm-string-literal ( expression )
6388 [ identifier ] asm-string-literal ( expression )
6391 static tree
6392 c_parser_asm_operands (c_parser *parser)
6394 tree list = NULL_TREE;
6395 while (true)
6397 tree name, str;
6398 struct c_expr expr;
6399 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6401 c_parser_consume_token (parser);
6402 if (c_parser_next_token_is (parser, CPP_NAME))
6404 tree id = c_parser_peek_token (parser)->value;
6405 c_parser_consume_token (parser);
6406 name = build_string (IDENTIFIER_LENGTH (id),
6407 IDENTIFIER_POINTER (id));
6409 else
6411 c_parser_error (parser, "expected identifier");
6412 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6413 return NULL_TREE;
6415 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6416 "expected %<]%>");
6418 else
6419 name = NULL_TREE;
6420 str = c_parser_asm_string_literal (parser);
6421 if (str == NULL_TREE)
6422 return NULL_TREE;
6423 parser->lex_untranslated_string = false;
6424 matching_parens parens;
6425 if (!parens.require_open (parser))
6427 parser->lex_untranslated_string = true;
6428 return NULL_TREE;
6430 expr = c_parser_expression (parser);
6431 mark_exp_read (expr.value);
6432 parser->lex_untranslated_string = true;
6433 if (!parens.require_close (parser))
6435 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6436 return NULL_TREE;
6438 list = chainon (list, build_tree_list (build_tree_list (name, str),
6439 expr.value));
6440 if (c_parser_next_token_is (parser, CPP_COMMA))
6441 c_parser_consume_token (parser);
6442 else
6443 break;
6445 return list;
6448 /* Parse asm clobbers, a GNU extension.
6450 asm-clobbers:
6451 asm-string-literal
6452 asm-clobbers , asm-string-literal
6455 static tree
6456 c_parser_asm_clobbers (c_parser *parser)
6458 tree list = NULL_TREE;
6459 while (true)
6461 tree str = c_parser_asm_string_literal (parser);
6462 if (str)
6463 list = tree_cons (NULL_TREE, str, list);
6464 else
6465 return NULL_TREE;
6466 if (c_parser_next_token_is (parser, CPP_COMMA))
6467 c_parser_consume_token (parser);
6468 else
6469 break;
6471 return list;
6474 /* Parse asm goto labels, a GNU extension.
6476 asm-goto-operands:
6477 identifier
6478 asm-goto-operands , identifier
6481 static tree
6482 c_parser_asm_goto_operands (c_parser *parser)
6484 tree list = NULL_TREE;
6485 while (true)
6487 tree name, label;
6489 if (c_parser_next_token_is (parser, CPP_NAME))
6491 c_token *tok = c_parser_peek_token (parser);
6492 name = tok->value;
6493 label = lookup_label_for_goto (tok->location, name);
6494 c_parser_consume_token (parser);
6495 TREE_USED (label) = 1;
6497 else
6499 c_parser_error (parser, "expected identifier");
6500 return NULL_TREE;
6503 name = build_string (IDENTIFIER_LENGTH (name),
6504 IDENTIFIER_POINTER (name));
6505 list = tree_cons (name, label, list);
6506 if (c_parser_next_token_is (parser, CPP_COMMA))
6507 c_parser_consume_token (parser);
6508 else
6509 return nreverse (list);
6513 /* Parse an expression other than a compound expression; that is, an
6514 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6515 AFTER is not NULL then it is an Objective-C message expression which
6516 is the primary-expression starting the expression as an initializer.
6518 assignment-expression:
6519 conditional-expression
6520 unary-expression assignment-operator assignment-expression
6522 assignment-operator: one of
6523 = *= /= %= += -= <<= >>= &= ^= |=
6525 In GNU C we accept any conditional expression on the LHS and
6526 diagnose the invalid lvalue rather than producing a syntax
6527 error. */
6529 static struct c_expr
6530 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6531 tree omp_atomic_lhs)
6533 struct c_expr lhs, rhs, ret;
6534 enum tree_code code;
6535 location_t op_location, exp_location;
6536 gcc_assert (!after || c_dialect_objc ());
6537 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6538 op_location = c_parser_peek_token (parser)->location;
6539 switch (c_parser_peek_token (parser)->type)
6541 case CPP_EQ:
6542 code = NOP_EXPR;
6543 break;
6544 case CPP_MULT_EQ:
6545 code = MULT_EXPR;
6546 break;
6547 case CPP_DIV_EQ:
6548 code = TRUNC_DIV_EXPR;
6549 break;
6550 case CPP_MOD_EQ:
6551 code = TRUNC_MOD_EXPR;
6552 break;
6553 case CPP_PLUS_EQ:
6554 code = PLUS_EXPR;
6555 break;
6556 case CPP_MINUS_EQ:
6557 code = MINUS_EXPR;
6558 break;
6559 case CPP_LSHIFT_EQ:
6560 code = LSHIFT_EXPR;
6561 break;
6562 case CPP_RSHIFT_EQ:
6563 code = RSHIFT_EXPR;
6564 break;
6565 case CPP_AND_EQ:
6566 code = BIT_AND_EXPR;
6567 break;
6568 case CPP_XOR_EQ:
6569 code = BIT_XOR_EXPR;
6570 break;
6571 case CPP_OR_EQ:
6572 code = BIT_IOR_EXPR;
6573 break;
6574 default:
6575 return lhs;
6577 c_parser_consume_token (parser);
6578 exp_location = c_parser_peek_token (parser)->location;
6579 rhs = c_parser_expr_no_commas (parser, NULL);
6580 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6582 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6583 code, exp_location, rhs.value,
6584 rhs.original_type);
6585 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6586 if (code == NOP_EXPR)
6587 ret.original_code = MODIFY_EXPR;
6588 else
6590 TREE_NO_WARNING (ret.value) = 1;
6591 ret.original_code = ERROR_MARK;
6593 ret.original_type = NULL;
6594 return ret;
6597 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6598 AFTER is not NULL then it is an Objective-C message expression which is
6599 the primary-expression starting the expression as an initializer.
6601 conditional-expression:
6602 logical-OR-expression
6603 logical-OR-expression ? expression : conditional-expression
6605 GNU extensions:
6607 conditional-expression:
6608 logical-OR-expression ? : conditional-expression
6611 static struct c_expr
6612 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6613 tree omp_atomic_lhs)
6615 struct c_expr cond, exp1, exp2, ret;
6616 location_t start, cond_loc, colon_loc;
6618 gcc_assert (!after || c_dialect_objc ());
6620 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6622 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6623 return cond;
6624 if (cond.value != error_mark_node)
6625 start = cond.get_start ();
6626 else
6627 start = UNKNOWN_LOCATION;
6628 cond_loc = c_parser_peek_token (parser)->location;
6629 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6630 c_parser_consume_token (parser);
6631 if (c_parser_next_token_is (parser, CPP_COLON))
6633 tree eptype = NULL_TREE;
6635 location_t middle_loc = c_parser_peek_token (parser)->location;
6636 pedwarn (middle_loc, OPT_Wpedantic,
6637 "ISO C forbids omitting the middle term of a ?: expression");
6638 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6640 eptype = TREE_TYPE (cond.value);
6641 cond.value = TREE_OPERAND (cond.value, 0);
6643 tree e = cond.value;
6644 while (TREE_CODE (e) == COMPOUND_EXPR)
6645 e = TREE_OPERAND (e, 1);
6646 warn_for_omitted_condop (middle_loc, e);
6647 /* Make sure first operand is calculated only once. */
6648 exp1.value = save_expr (default_conversion (cond.value));
6649 if (eptype)
6650 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6651 exp1.original_type = NULL;
6652 exp1.src_range = cond.src_range;
6653 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6654 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6656 else
6658 cond.value
6659 = c_objc_common_truthvalue_conversion
6660 (cond_loc, default_conversion (cond.value));
6661 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6662 exp1 = c_parser_expression_conv (parser);
6663 mark_exp_read (exp1.value);
6664 c_inhibit_evaluation_warnings +=
6665 ((cond.value == truthvalue_true_node)
6666 - (cond.value == truthvalue_false_node));
6669 colon_loc = c_parser_peek_token (parser)->location;
6670 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6672 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6673 ret.value = error_mark_node;
6674 ret.original_code = ERROR_MARK;
6675 ret.original_type = NULL;
6676 return ret;
6679 location_t exp2_loc = c_parser_peek_token (parser)->location;
6680 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6681 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6683 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6684 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6685 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6686 ret.value = build_conditional_expr (colon_loc, cond.value,
6687 cond.original_code == C_MAYBE_CONST_EXPR,
6688 exp1.value, exp1.original_type, loc1,
6689 exp2.value, exp2.original_type, loc2);
6690 ret.original_code = ERROR_MARK;
6691 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6692 ret.original_type = NULL;
6693 else
6695 tree t1, t2;
6697 /* If both sides are enum type, the default conversion will have
6698 made the type of the result be an integer type. We want to
6699 remember the enum types we started with. */
6700 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6701 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6702 ret.original_type = ((t1 != error_mark_node
6703 && t2 != error_mark_node
6704 && (TYPE_MAIN_VARIANT (t1)
6705 == TYPE_MAIN_VARIANT (t2)))
6706 ? t1
6707 : NULL);
6709 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6710 return ret;
6713 /* Parse a binary expression; that is, a logical-OR-expression (C90
6714 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6715 NULL then it is an Objective-C message expression which is the
6716 primary-expression starting the expression as an initializer.
6718 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6719 when it should be the unfolded lhs. In a valid OpenMP source,
6720 one of the operands of the toplevel binary expression must be equal
6721 to it. In that case, just return a build2 created binary operation
6722 rather than result of parser_build_binary_op.
6724 multiplicative-expression:
6725 cast-expression
6726 multiplicative-expression * cast-expression
6727 multiplicative-expression / cast-expression
6728 multiplicative-expression % cast-expression
6730 additive-expression:
6731 multiplicative-expression
6732 additive-expression + multiplicative-expression
6733 additive-expression - multiplicative-expression
6735 shift-expression:
6736 additive-expression
6737 shift-expression << additive-expression
6738 shift-expression >> additive-expression
6740 relational-expression:
6741 shift-expression
6742 relational-expression < shift-expression
6743 relational-expression > shift-expression
6744 relational-expression <= shift-expression
6745 relational-expression >= shift-expression
6747 equality-expression:
6748 relational-expression
6749 equality-expression == relational-expression
6750 equality-expression != relational-expression
6752 AND-expression:
6753 equality-expression
6754 AND-expression & equality-expression
6756 exclusive-OR-expression:
6757 AND-expression
6758 exclusive-OR-expression ^ AND-expression
6760 inclusive-OR-expression:
6761 exclusive-OR-expression
6762 inclusive-OR-expression | exclusive-OR-expression
6764 logical-AND-expression:
6765 inclusive-OR-expression
6766 logical-AND-expression && inclusive-OR-expression
6768 logical-OR-expression:
6769 logical-AND-expression
6770 logical-OR-expression || logical-AND-expression
6773 static struct c_expr
6774 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6775 tree omp_atomic_lhs)
6777 /* A binary expression is parsed using operator-precedence parsing,
6778 with the operands being cast expressions. All the binary
6779 operators are left-associative. Thus a binary expression is of
6780 form:
6782 E0 op1 E1 op2 E2 ...
6784 which we represent on a stack. On the stack, the precedence
6785 levels are strictly increasing. When a new operator is
6786 encountered of higher precedence than that at the top of the
6787 stack, it is pushed; its LHS is the top expression, and its RHS
6788 is everything parsed until it is popped. When a new operator is
6789 encountered with precedence less than or equal to that at the top
6790 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6791 by the result of the operation until the operator at the top of
6792 the stack has lower precedence than the new operator or there is
6793 only one element on the stack; then the top expression is the LHS
6794 of the new operator. In the case of logical AND and OR
6795 expressions, we also need to adjust c_inhibit_evaluation_warnings
6796 as appropriate when the operators are pushed and popped. */
6798 struct {
6799 /* The expression at this stack level. */
6800 struct c_expr expr;
6801 /* The precedence of the operator on its left, PREC_NONE at the
6802 bottom of the stack. */
6803 enum c_parser_prec prec;
6804 /* The operation on its left. */
6805 enum tree_code op;
6806 /* The source location of this operation. */
6807 location_t loc;
6808 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6809 tree sizeof_arg;
6810 } stack[NUM_PRECS];
6811 int sp;
6812 /* Location of the binary operator. */
6813 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6814 #define POP \
6815 do { \
6816 switch (stack[sp].op) \
6818 case TRUTH_ANDIF_EXPR: \
6819 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6820 == truthvalue_false_node); \
6821 break; \
6822 case TRUTH_ORIF_EXPR: \
6823 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6824 == truthvalue_true_node); \
6825 break; \
6826 case TRUNC_DIV_EXPR: \
6827 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6828 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6830 tree type0 = stack[sp - 1].sizeof_arg; \
6831 tree type1 = stack[sp].sizeof_arg; \
6832 tree first_arg = type0; \
6833 if (!TYPE_P (type0)) \
6834 type0 = TREE_TYPE (type0); \
6835 if (!TYPE_P (type1)) \
6836 type1 = TREE_TYPE (type1); \
6837 if (POINTER_TYPE_P (type0) \
6838 && comptypes (TREE_TYPE (type0), type1) \
6839 && !(TREE_CODE (first_arg) == PARM_DECL \
6840 && C_ARRAY_PARAMETER (first_arg) \
6841 && warn_sizeof_array_argument)) \
6842 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6843 "division %<sizeof (%T) / sizeof (%T)%> does " \
6844 "not compute the number of array elements", \
6845 type0, type1)) \
6846 if (DECL_P (first_arg)) \
6847 inform (DECL_SOURCE_LOCATION (first_arg), \
6848 "first %<sizeof%> operand was declared here"); \
6850 break; \
6851 default: \
6852 break; \
6854 stack[sp - 1].expr \
6855 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6856 stack[sp - 1].expr, true, true); \
6857 stack[sp].expr \
6858 = convert_lvalue_to_rvalue (stack[sp].loc, \
6859 stack[sp].expr, true, true); \
6860 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6861 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6862 && ((1 << stack[sp].prec) \
6863 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6864 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6865 && stack[sp].op != TRUNC_MOD_EXPR \
6866 && stack[0].expr.value != error_mark_node \
6867 && stack[1].expr.value != error_mark_node \
6868 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6869 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6870 stack[0].expr.value \
6871 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6872 stack[0].expr.value, stack[1].expr.value); \
6873 else \
6874 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6875 stack[sp].op, \
6876 stack[sp - 1].expr, \
6877 stack[sp].expr); \
6878 sp--; \
6879 } while (0)
6880 gcc_assert (!after || c_dialect_objc ());
6881 stack[0].loc = c_parser_peek_token (parser)->location;
6882 stack[0].expr = c_parser_cast_expression (parser, after);
6883 stack[0].prec = PREC_NONE;
6884 stack[0].sizeof_arg = c_last_sizeof_arg;
6885 sp = 0;
6886 while (true)
6888 enum c_parser_prec oprec;
6889 enum tree_code ocode;
6890 source_range src_range;
6891 if (parser->error)
6892 goto out;
6893 switch (c_parser_peek_token (parser)->type)
6895 case CPP_MULT:
6896 oprec = PREC_MULT;
6897 ocode = MULT_EXPR;
6898 break;
6899 case CPP_DIV:
6900 oprec = PREC_MULT;
6901 ocode = TRUNC_DIV_EXPR;
6902 break;
6903 case CPP_MOD:
6904 oprec = PREC_MULT;
6905 ocode = TRUNC_MOD_EXPR;
6906 break;
6907 case CPP_PLUS:
6908 oprec = PREC_ADD;
6909 ocode = PLUS_EXPR;
6910 break;
6911 case CPP_MINUS:
6912 oprec = PREC_ADD;
6913 ocode = MINUS_EXPR;
6914 break;
6915 case CPP_LSHIFT:
6916 oprec = PREC_SHIFT;
6917 ocode = LSHIFT_EXPR;
6918 break;
6919 case CPP_RSHIFT:
6920 oprec = PREC_SHIFT;
6921 ocode = RSHIFT_EXPR;
6922 break;
6923 case CPP_LESS:
6924 oprec = PREC_REL;
6925 ocode = LT_EXPR;
6926 break;
6927 case CPP_GREATER:
6928 oprec = PREC_REL;
6929 ocode = GT_EXPR;
6930 break;
6931 case CPP_LESS_EQ:
6932 oprec = PREC_REL;
6933 ocode = LE_EXPR;
6934 break;
6935 case CPP_GREATER_EQ:
6936 oprec = PREC_REL;
6937 ocode = GE_EXPR;
6938 break;
6939 case CPP_EQ_EQ:
6940 oprec = PREC_EQ;
6941 ocode = EQ_EXPR;
6942 break;
6943 case CPP_NOT_EQ:
6944 oprec = PREC_EQ;
6945 ocode = NE_EXPR;
6946 break;
6947 case CPP_AND:
6948 oprec = PREC_BITAND;
6949 ocode = BIT_AND_EXPR;
6950 break;
6951 case CPP_XOR:
6952 oprec = PREC_BITXOR;
6953 ocode = BIT_XOR_EXPR;
6954 break;
6955 case CPP_OR:
6956 oprec = PREC_BITOR;
6957 ocode = BIT_IOR_EXPR;
6958 break;
6959 case CPP_AND_AND:
6960 oprec = PREC_LOGAND;
6961 ocode = TRUTH_ANDIF_EXPR;
6962 break;
6963 case CPP_OR_OR:
6964 oprec = PREC_LOGOR;
6965 ocode = TRUTH_ORIF_EXPR;
6966 break;
6967 default:
6968 /* Not a binary operator, so end of the binary
6969 expression. */
6970 goto out;
6972 binary_loc = c_parser_peek_token (parser)->location;
6973 while (oprec <= stack[sp].prec)
6974 POP;
6975 c_parser_consume_token (parser);
6976 switch (ocode)
6978 case TRUTH_ANDIF_EXPR:
6979 src_range = stack[sp].expr.src_range;
6980 stack[sp].expr
6981 = convert_lvalue_to_rvalue (stack[sp].loc,
6982 stack[sp].expr, true, true);
6983 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6984 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6985 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6986 == truthvalue_false_node);
6987 set_c_expr_source_range (&stack[sp].expr, src_range);
6988 break;
6989 case TRUTH_ORIF_EXPR:
6990 src_range = stack[sp].expr.src_range;
6991 stack[sp].expr
6992 = convert_lvalue_to_rvalue (stack[sp].loc,
6993 stack[sp].expr, true, true);
6994 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6995 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6996 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6997 == truthvalue_true_node);
6998 set_c_expr_source_range (&stack[sp].expr, src_range);
6999 break;
7000 default:
7001 break;
7003 sp++;
7004 stack[sp].loc = binary_loc;
7005 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7006 stack[sp].prec = oprec;
7007 stack[sp].op = ocode;
7008 stack[sp].sizeof_arg = c_last_sizeof_arg;
7010 out:
7011 while (sp > 0)
7012 POP;
7013 return stack[0].expr;
7014 #undef POP
7017 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7018 is not NULL then it is an Objective-C message expression which is the
7019 primary-expression starting the expression as an initializer.
7021 cast-expression:
7022 unary-expression
7023 ( type-name ) unary-expression
7026 static struct c_expr
7027 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7029 location_t cast_loc = c_parser_peek_token (parser)->location;
7030 gcc_assert (!after || c_dialect_objc ());
7031 if (after)
7032 return c_parser_postfix_expression_after_primary (parser,
7033 cast_loc, *after);
7034 /* If the expression begins with a parenthesized type name, it may
7035 be either a cast or a compound literal; we need to see whether
7036 the next character is '{' to tell the difference. If not, it is
7037 an unary expression. Full detection of unknown typenames here
7038 would require a 3-token lookahead. */
7039 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7040 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7042 struct c_type_name *type_name;
7043 struct c_expr ret;
7044 struct c_expr expr;
7045 matching_parens parens;
7046 parens.consume_open (parser);
7047 type_name = c_parser_type_name (parser, true);
7048 parens.skip_until_found_close (parser);
7049 if (type_name == NULL)
7051 ret.value = error_mark_node;
7052 ret.original_code = ERROR_MARK;
7053 ret.original_type = NULL;
7054 return ret;
7057 /* Save casted types in the function's used types hash table. */
7058 used_types_insert (type_name->specs->type);
7060 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7061 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7062 cast_loc);
7063 if (type_name->specs->alignas_p)
7064 error_at (type_name->specs->locations[cdw_alignas],
7065 "alignment specified for type name in cast");
7067 location_t expr_loc = c_parser_peek_token (parser)->location;
7068 expr = c_parser_cast_expression (parser, NULL);
7069 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7071 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7072 if (ret.value && expr.value)
7073 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7074 ret.original_code = ERROR_MARK;
7075 ret.original_type = NULL;
7076 return ret;
7078 else
7079 return c_parser_unary_expression (parser);
7082 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7084 unary-expression:
7085 postfix-expression
7086 ++ unary-expression
7087 -- unary-expression
7088 unary-operator cast-expression
7089 sizeof unary-expression
7090 sizeof ( type-name )
7092 unary-operator: one of
7093 & * + - ~ !
7095 GNU extensions:
7097 unary-expression:
7098 __alignof__ unary-expression
7099 __alignof__ ( type-name )
7100 && identifier
7102 (C11 permits _Alignof with type names only.)
7104 unary-operator: one of
7105 __extension__ __real__ __imag__
7107 Transactional Memory:
7109 unary-expression:
7110 transaction-expression
7112 In addition, the GNU syntax treats ++ and -- as unary operators, so
7113 they may be applied to cast expressions with errors for non-lvalues
7114 given later. */
7116 static struct c_expr
7117 c_parser_unary_expression (c_parser *parser)
7119 int ext;
7120 struct c_expr ret, op;
7121 location_t op_loc = c_parser_peek_token (parser)->location;
7122 location_t exp_loc;
7123 location_t finish;
7124 ret.original_code = ERROR_MARK;
7125 ret.original_type = NULL;
7126 switch (c_parser_peek_token (parser)->type)
7128 case CPP_PLUS_PLUS:
7129 c_parser_consume_token (parser);
7130 exp_loc = c_parser_peek_token (parser)->location;
7131 op = c_parser_cast_expression (parser, NULL);
7133 op = default_function_array_read_conversion (exp_loc, op);
7134 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7135 case CPP_MINUS_MINUS:
7136 c_parser_consume_token (parser);
7137 exp_loc = c_parser_peek_token (parser)->location;
7138 op = c_parser_cast_expression (parser, NULL);
7140 op = default_function_array_read_conversion (exp_loc, op);
7141 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7142 case CPP_AND:
7143 c_parser_consume_token (parser);
7144 op = c_parser_cast_expression (parser, NULL);
7145 mark_exp_read (op.value);
7146 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7147 case CPP_MULT:
7149 c_parser_consume_token (parser);
7150 exp_loc = c_parser_peek_token (parser)->location;
7151 op = c_parser_cast_expression (parser, NULL);
7152 finish = op.get_finish ();
7153 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7154 location_t combined_loc = make_location (op_loc, op_loc, finish);
7155 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7156 ret.src_range.m_start = op_loc;
7157 ret.src_range.m_finish = finish;
7158 return ret;
7160 case CPP_PLUS:
7161 if (!c_dialect_objc () && !in_system_header_at (input_location))
7162 warning_at (op_loc,
7163 OPT_Wtraditional,
7164 "traditional C rejects the unary plus operator");
7165 c_parser_consume_token (parser);
7166 exp_loc = c_parser_peek_token (parser)->location;
7167 op = c_parser_cast_expression (parser, NULL);
7168 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7169 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7170 case CPP_MINUS:
7171 c_parser_consume_token (parser);
7172 exp_loc = c_parser_peek_token (parser)->location;
7173 op = c_parser_cast_expression (parser, NULL);
7174 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7175 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7176 case CPP_COMPL:
7177 c_parser_consume_token (parser);
7178 exp_loc = c_parser_peek_token (parser)->location;
7179 op = c_parser_cast_expression (parser, NULL);
7180 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7181 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7182 case CPP_NOT:
7183 c_parser_consume_token (parser);
7184 exp_loc = c_parser_peek_token (parser)->location;
7185 op = c_parser_cast_expression (parser, NULL);
7186 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7187 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7188 case CPP_AND_AND:
7189 /* Refer to the address of a label as a pointer. */
7190 c_parser_consume_token (parser);
7191 if (c_parser_next_token_is (parser, CPP_NAME))
7193 ret.value = finish_label_address_expr
7194 (c_parser_peek_token (parser)->value, op_loc);
7195 set_c_expr_source_range (&ret, op_loc,
7196 c_parser_peek_token (parser)->get_finish ());
7197 c_parser_consume_token (parser);
7199 else
7201 c_parser_error (parser, "expected identifier");
7202 ret.set_error ();
7204 return ret;
7205 case CPP_KEYWORD:
7206 switch (c_parser_peek_token (parser)->keyword)
7208 case RID_SIZEOF:
7209 return c_parser_sizeof_expression (parser);
7210 case RID_ALIGNOF:
7211 return c_parser_alignof_expression (parser);
7212 case RID_EXTENSION:
7213 c_parser_consume_token (parser);
7214 ext = disable_extension_diagnostics ();
7215 ret = c_parser_cast_expression (parser, NULL);
7216 restore_extension_diagnostics (ext);
7217 return ret;
7218 case RID_REALPART:
7219 c_parser_consume_token (parser);
7220 exp_loc = c_parser_peek_token (parser)->location;
7221 op = c_parser_cast_expression (parser, NULL);
7222 op = default_function_array_conversion (exp_loc, op);
7223 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7224 case RID_IMAGPART:
7225 c_parser_consume_token (parser);
7226 exp_loc = c_parser_peek_token (parser)->location;
7227 op = c_parser_cast_expression (parser, NULL);
7228 op = default_function_array_conversion (exp_loc, op);
7229 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7230 case RID_TRANSACTION_ATOMIC:
7231 case RID_TRANSACTION_RELAXED:
7232 return c_parser_transaction_expression (parser,
7233 c_parser_peek_token (parser)->keyword);
7234 default:
7235 return c_parser_postfix_expression (parser);
7237 default:
7238 return c_parser_postfix_expression (parser);
7242 /* Parse a sizeof expression. */
7244 static struct c_expr
7245 c_parser_sizeof_expression (c_parser *parser)
7247 struct c_expr expr;
7248 struct c_expr result;
7249 location_t expr_loc;
7250 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7252 location_t start;
7253 location_t finish = UNKNOWN_LOCATION;
7255 start = c_parser_peek_token (parser)->location;
7257 c_parser_consume_token (parser);
7258 c_inhibit_evaluation_warnings++;
7259 in_sizeof++;
7260 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7261 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7263 /* Either sizeof ( type-name ) or sizeof unary-expression
7264 starting with a compound literal. */
7265 struct c_type_name *type_name;
7266 matching_parens parens;
7267 parens.consume_open (parser);
7268 expr_loc = c_parser_peek_token (parser)->location;
7269 type_name = c_parser_type_name (parser, true);
7270 parens.skip_until_found_close (parser);
7271 finish = parser->tokens_buf[0].location;
7272 if (type_name == NULL)
7274 struct c_expr ret;
7275 c_inhibit_evaluation_warnings--;
7276 in_sizeof--;
7277 ret.value = error_mark_node;
7278 ret.original_code = ERROR_MARK;
7279 ret.original_type = NULL;
7280 return ret;
7282 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7284 expr = c_parser_postfix_expression_after_paren_type (parser,
7285 type_name,
7286 expr_loc);
7287 finish = expr.get_finish ();
7288 goto sizeof_expr;
7290 /* sizeof ( type-name ). */
7291 if (type_name->specs->alignas_p)
7292 error_at (type_name->specs->locations[cdw_alignas],
7293 "alignment specified for type name in %<sizeof%>");
7294 c_inhibit_evaluation_warnings--;
7295 in_sizeof--;
7296 result = c_expr_sizeof_type (expr_loc, type_name);
7298 else
7300 expr_loc = c_parser_peek_token (parser)->location;
7301 expr = c_parser_unary_expression (parser);
7302 finish = expr.get_finish ();
7303 sizeof_expr:
7304 c_inhibit_evaluation_warnings--;
7305 in_sizeof--;
7306 mark_exp_read (expr.value);
7307 if (TREE_CODE (expr.value) == COMPONENT_REF
7308 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7309 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7310 result = c_expr_sizeof_expr (expr_loc, expr);
7312 if (finish != UNKNOWN_LOCATION)
7313 set_c_expr_source_range (&result, start, finish);
7314 return result;
7317 /* Parse an alignof expression. */
7319 static struct c_expr
7320 c_parser_alignof_expression (c_parser *parser)
7322 struct c_expr expr;
7323 location_t start_loc = c_parser_peek_token (parser)->location;
7324 location_t end_loc;
7325 tree alignof_spelling = c_parser_peek_token (parser)->value;
7326 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7327 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7328 "_Alignof") == 0;
7329 /* A diagnostic is not required for the use of this identifier in
7330 the implementation namespace; only diagnose it for the C11
7331 spelling because of existing code using the other spellings. */
7332 if (is_c11_alignof)
7334 if (flag_isoc99)
7335 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7336 alignof_spelling);
7337 else
7338 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7339 alignof_spelling);
7341 c_parser_consume_token (parser);
7342 c_inhibit_evaluation_warnings++;
7343 in_alignof++;
7344 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7345 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7347 /* Either __alignof__ ( type-name ) or __alignof__
7348 unary-expression starting with a compound literal. */
7349 location_t loc;
7350 struct c_type_name *type_name;
7351 struct c_expr ret;
7352 matching_parens parens;
7353 parens.consume_open (parser);
7354 loc = c_parser_peek_token (parser)->location;
7355 type_name = c_parser_type_name (parser, true);
7356 end_loc = c_parser_peek_token (parser)->location;
7357 parens.skip_until_found_close (parser);
7358 if (type_name == NULL)
7360 struct c_expr ret;
7361 c_inhibit_evaluation_warnings--;
7362 in_alignof--;
7363 ret.value = error_mark_node;
7364 ret.original_code = ERROR_MARK;
7365 ret.original_type = NULL;
7366 return ret;
7368 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7370 expr = c_parser_postfix_expression_after_paren_type (parser,
7371 type_name,
7372 loc);
7373 goto alignof_expr;
7375 /* alignof ( type-name ). */
7376 if (type_name->specs->alignas_p)
7377 error_at (type_name->specs->locations[cdw_alignas],
7378 "alignment specified for type name in %qE",
7379 alignof_spelling);
7380 c_inhibit_evaluation_warnings--;
7381 in_alignof--;
7382 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7383 NULL, NULL),
7384 false, is_c11_alignof, 1);
7385 ret.original_code = ERROR_MARK;
7386 ret.original_type = NULL;
7387 set_c_expr_source_range (&ret, start_loc, end_loc);
7388 return ret;
7390 else
7392 struct c_expr ret;
7393 expr = c_parser_unary_expression (parser);
7394 end_loc = expr.src_range.m_finish;
7395 alignof_expr:
7396 mark_exp_read (expr.value);
7397 c_inhibit_evaluation_warnings--;
7398 in_alignof--;
7399 if (is_c11_alignof)
7400 pedwarn (start_loc,
7401 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7402 alignof_spelling);
7403 ret.value = c_alignof_expr (start_loc, expr.value);
7404 ret.original_code = ERROR_MARK;
7405 ret.original_type = NULL;
7406 set_c_expr_source_range (&ret, start_loc, end_loc);
7407 return ret;
7411 /* Helper function to read arguments of builtins which are interfaces
7412 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7413 others. The name of the builtin is passed using BNAME parameter.
7414 Function returns true if there were no errors while parsing and
7415 stores the arguments in CEXPR_LIST. If it returns true,
7416 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7417 parenthesis. */
7418 static bool
7419 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7420 vec<c_expr_t, va_gc> **ret_cexpr_list,
7421 bool choose_expr_p,
7422 location_t *out_close_paren_loc)
7424 location_t loc = c_parser_peek_token (parser)->location;
7425 vec<c_expr_t, va_gc> *cexpr_list;
7426 c_expr_t expr;
7427 bool saved_force_folding_builtin_constant_p;
7429 *ret_cexpr_list = NULL;
7430 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7432 error_at (loc, "cannot take address of %qs", bname);
7433 return false;
7436 c_parser_consume_token (parser);
7438 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7440 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7441 c_parser_consume_token (parser);
7442 return true;
7445 saved_force_folding_builtin_constant_p
7446 = force_folding_builtin_constant_p;
7447 force_folding_builtin_constant_p |= choose_expr_p;
7448 expr = c_parser_expr_no_commas (parser, NULL);
7449 force_folding_builtin_constant_p
7450 = saved_force_folding_builtin_constant_p;
7451 vec_alloc (cexpr_list, 1);
7452 vec_safe_push (cexpr_list, expr);
7453 while (c_parser_next_token_is (parser, CPP_COMMA))
7455 c_parser_consume_token (parser);
7456 expr = c_parser_expr_no_commas (parser, NULL);
7457 vec_safe_push (cexpr_list, expr);
7460 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7461 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7462 return false;
7464 *ret_cexpr_list = cexpr_list;
7465 return true;
7468 /* This represents a single generic-association. */
7470 struct c_generic_association
7472 /* The location of the starting token of the type. */
7473 location_t type_location;
7474 /* The association's type, or NULL_TREE for 'default'. */
7475 tree type;
7476 /* The association's expression. */
7477 struct c_expr expression;
7480 /* Parse a generic-selection. (C11 6.5.1.1).
7482 generic-selection:
7483 _Generic ( assignment-expression , generic-assoc-list )
7485 generic-assoc-list:
7486 generic-association
7487 generic-assoc-list , generic-association
7489 generic-association:
7490 type-name : assignment-expression
7491 default : assignment-expression
7494 static struct c_expr
7495 c_parser_generic_selection (c_parser *parser)
7497 struct c_expr selector, error_expr;
7498 tree selector_type;
7499 struct c_generic_association matched_assoc;
7500 bool match_found = false;
7501 location_t generic_loc, selector_loc;
7503 error_expr.original_code = ERROR_MARK;
7504 error_expr.original_type = NULL;
7505 error_expr.set_error ();
7506 matched_assoc.type_location = UNKNOWN_LOCATION;
7507 matched_assoc.type = NULL_TREE;
7508 matched_assoc.expression = error_expr;
7510 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7511 generic_loc = c_parser_peek_token (parser)->location;
7512 c_parser_consume_token (parser);
7513 if (flag_isoc99)
7514 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7515 "ISO C99 does not support %<_Generic%>");
7516 else
7517 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7518 "ISO C90 does not support %<_Generic%>");
7520 matching_parens parens;
7521 if (!parens.require_open (parser))
7522 return error_expr;
7524 c_inhibit_evaluation_warnings++;
7525 selector_loc = c_parser_peek_token (parser)->location;
7526 selector = c_parser_expr_no_commas (parser, NULL);
7527 selector = default_function_array_conversion (selector_loc, selector);
7528 c_inhibit_evaluation_warnings--;
7530 if (selector.value == error_mark_node)
7532 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7533 return selector;
7535 selector_type = TREE_TYPE (selector.value);
7536 /* In ISO C terms, rvalues (including the controlling expression of
7537 _Generic) do not have qualified types. */
7538 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7539 selector_type = TYPE_MAIN_VARIANT (selector_type);
7540 /* In ISO C terms, _Noreturn is not part of the type of expressions
7541 such as &abort, but in GCC it is represented internally as a type
7542 qualifier. */
7543 if (FUNCTION_POINTER_TYPE_P (selector_type)
7544 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7545 selector_type
7546 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7548 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7550 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7551 return error_expr;
7554 auto_vec<c_generic_association> associations;
7555 while (1)
7557 struct c_generic_association assoc, *iter;
7558 unsigned int ix;
7559 c_token *token = c_parser_peek_token (parser);
7561 assoc.type_location = token->location;
7562 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7564 c_parser_consume_token (parser);
7565 assoc.type = NULL_TREE;
7567 else
7569 struct c_type_name *type_name;
7571 type_name = c_parser_type_name (parser);
7572 if (type_name == NULL)
7574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7575 return error_expr;
7577 assoc.type = groktypename (type_name, NULL, NULL);
7578 if (assoc.type == error_mark_node)
7580 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7581 return error_expr;
7584 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7585 error_at (assoc.type_location,
7586 "%<_Generic%> association has function type");
7587 else if (!COMPLETE_TYPE_P (assoc.type))
7588 error_at (assoc.type_location,
7589 "%<_Generic%> association has incomplete type");
7591 if (variably_modified_type_p (assoc.type, NULL_TREE))
7592 error_at (assoc.type_location,
7593 "%<_Generic%> association has "
7594 "variable length type");
7597 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7600 return error_expr;
7603 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7604 if (assoc.expression.value == error_mark_node)
7606 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7607 return error_expr;
7610 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7612 if (assoc.type == NULL_TREE)
7614 if (iter->type == NULL_TREE)
7616 error_at (assoc.type_location,
7617 "duplicate %<default%> case in %<_Generic%>");
7618 inform (iter->type_location, "original %<default%> is here");
7621 else if (iter->type != NULL_TREE)
7623 if (comptypes (assoc.type, iter->type))
7625 error_at (assoc.type_location,
7626 "%<_Generic%> specifies two compatible types");
7627 inform (iter->type_location, "compatible type is here");
7632 if (assoc.type == NULL_TREE)
7634 if (!match_found)
7636 matched_assoc = assoc;
7637 match_found = true;
7640 else if (comptypes (assoc.type, selector_type))
7642 if (!match_found || matched_assoc.type == NULL_TREE)
7644 matched_assoc = assoc;
7645 match_found = true;
7647 else
7649 error_at (assoc.type_location,
7650 "%<_Generic%> selector matches multiple associations");
7651 inform (matched_assoc.type_location,
7652 "other match is here");
7656 associations.safe_push (assoc);
7658 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7659 break;
7660 c_parser_consume_token (parser);
7663 if (!parens.require_close (parser))
7665 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7666 return error_expr;
7669 if (!match_found)
7671 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7672 "compatible with any association",
7673 selector_type);
7674 return error_expr;
7677 return matched_assoc.expression;
7680 /* Check the validity of a function pointer argument *EXPR (argument
7681 position POS) to __builtin_tgmath. Return the number of function
7682 arguments if possibly valid; return 0 having reported an error if
7683 not valid. */
7685 static unsigned int
7686 check_tgmath_function (c_expr *expr, unsigned int pos)
7688 tree type = TREE_TYPE (expr->value);
7689 if (!FUNCTION_POINTER_TYPE_P (type))
7691 error_at (expr->get_location (),
7692 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7693 pos);
7694 return 0;
7696 type = TREE_TYPE (type);
7697 if (!prototype_p (type))
7699 error_at (expr->get_location (),
7700 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7701 return 0;
7703 if (stdarg_p (type))
7705 error_at (expr->get_location (),
7706 "argument %u of %<__builtin_tgmath%> has variable arguments",
7707 pos);
7708 return 0;
7710 unsigned int nargs = 0;
7711 function_args_iterator iter;
7712 tree t;
7713 FOREACH_FUNCTION_ARGS (type, t, iter)
7715 if (t == void_type_node)
7716 break;
7717 nargs++;
7719 if (nargs == 0)
7721 error_at (expr->get_location (),
7722 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7723 return 0;
7725 return nargs;
7728 /* Ways in which a parameter or return value of a type-generic macro
7729 may vary between the different functions the macro may call. */
7730 enum tgmath_parm_kind
7732 tgmath_fixed, tgmath_real, tgmath_complex
7735 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7736 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7737 call c_parser_postfix_expression_after_paren_type on encountering them.
7739 postfix-expression:
7740 primary-expression
7741 postfix-expression [ expression ]
7742 postfix-expression ( argument-expression-list[opt] )
7743 postfix-expression . identifier
7744 postfix-expression -> identifier
7745 postfix-expression ++
7746 postfix-expression --
7747 ( type-name ) { initializer-list }
7748 ( type-name ) { initializer-list , }
7750 argument-expression-list:
7751 argument-expression
7752 argument-expression-list , argument-expression
7754 primary-expression:
7755 identifier
7756 constant
7757 string-literal
7758 ( expression )
7759 generic-selection
7761 GNU extensions:
7763 primary-expression:
7764 __func__
7765 (treated as a keyword in GNU C)
7766 __FUNCTION__
7767 __PRETTY_FUNCTION__
7768 ( compound-statement )
7769 __builtin_va_arg ( assignment-expression , type-name )
7770 __builtin_offsetof ( type-name , offsetof-member-designator )
7771 __builtin_choose_expr ( assignment-expression ,
7772 assignment-expression ,
7773 assignment-expression )
7774 __builtin_types_compatible_p ( type-name , type-name )
7775 __builtin_tgmath ( expr-list )
7776 __builtin_complex ( assignment-expression , assignment-expression )
7777 __builtin_shuffle ( assignment-expression , assignment-expression )
7778 __builtin_shuffle ( assignment-expression ,
7779 assignment-expression ,
7780 assignment-expression, )
7782 offsetof-member-designator:
7783 identifier
7784 offsetof-member-designator . identifier
7785 offsetof-member-designator [ expression ]
7787 Objective-C:
7789 primary-expression:
7790 [ objc-receiver objc-message-args ]
7791 @selector ( objc-selector-arg )
7792 @protocol ( identifier )
7793 @encode ( type-name )
7794 objc-string-literal
7795 Classname . identifier
7798 static struct c_expr
7799 c_parser_postfix_expression (c_parser *parser)
7801 struct c_expr expr, e1;
7802 struct c_type_name *t1, *t2;
7803 location_t loc = c_parser_peek_token (parser)->location;
7804 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7805 expr.original_code = ERROR_MARK;
7806 expr.original_type = NULL;
7807 switch (c_parser_peek_token (parser)->type)
7809 case CPP_NUMBER:
7810 expr.value = c_parser_peek_token (parser)->value;
7811 set_c_expr_source_range (&expr, tok_range);
7812 loc = c_parser_peek_token (parser)->location;
7813 c_parser_consume_token (parser);
7814 if (TREE_CODE (expr.value) == FIXED_CST
7815 && !targetm.fixed_point_supported_p ())
7817 error_at (loc, "fixed-point types not supported for this target");
7818 expr.value = error_mark_node;
7820 break;
7821 case CPP_CHAR:
7822 case CPP_CHAR16:
7823 case CPP_CHAR32:
7824 case CPP_WCHAR:
7825 expr.value = c_parser_peek_token (parser)->value;
7826 /* For the purpose of warning when a pointer is compared with
7827 a zero character constant. */
7828 expr.original_type = char_type_node;
7829 set_c_expr_source_range (&expr, tok_range);
7830 c_parser_consume_token (parser);
7831 break;
7832 case CPP_STRING:
7833 case CPP_STRING16:
7834 case CPP_STRING32:
7835 case CPP_WSTRING:
7836 case CPP_UTF8STRING:
7837 expr.value = c_parser_peek_token (parser)->value;
7838 set_c_expr_source_range (&expr, tok_range);
7839 expr.original_code = STRING_CST;
7840 c_parser_consume_token (parser);
7841 break;
7842 case CPP_OBJC_STRING:
7843 gcc_assert (c_dialect_objc ());
7844 expr.value
7845 = objc_build_string_object (c_parser_peek_token (parser)->value);
7846 set_c_expr_source_range (&expr, tok_range);
7847 c_parser_consume_token (parser);
7848 break;
7849 case CPP_NAME:
7850 switch (c_parser_peek_token (parser)->id_kind)
7852 case C_ID_ID:
7854 tree id = c_parser_peek_token (parser)->value;
7855 c_parser_consume_token (parser);
7856 expr.value = build_external_ref (loc, id,
7857 (c_parser_peek_token (parser)->type
7858 == CPP_OPEN_PAREN),
7859 &expr.original_type);
7860 set_c_expr_source_range (&expr, tok_range);
7861 break;
7863 case C_ID_CLASSNAME:
7865 /* Here we parse the Objective-C 2.0 Class.name dot
7866 syntax. */
7867 tree class_name = c_parser_peek_token (parser)->value;
7868 tree component;
7869 c_parser_consume_token (parser);
7870 gcc_assert (c_dialect_objc ());
7871 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7873 expr.set_error ();
7874 break;
7876 if (c_parser_next_token_is_not (parser, CPP_NAME))
7878 c_parser_error (parser, "expected identifier");
7879 expr.set_error ();
7880 break;
7882 c_token *component_tok = c_parser_peek_token (parser);
7883 component = component_tok->value;
7884 location_t end_loc = component_tok->get_finish ();
7885 c_parser_consume_token (parser);
7886 expr.value = objc_build_class_component_ref (class_name,
7887 component);
7888 set_c_expr_source_range (&expr, loc, end_loc);
7889 break;
7891 default:
7892 c_parser_error (parser, "expected expression");
7893 expr.set_error ();
7894 break;
7896 break;
7897 case CPP_OPEN_PAREN:
7898 /* A parenthesized expression, statement expression or compound
7899 literal. */
7900 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7902 /* A statement expression. */
7903 tree stmt;
7904 location_t brace_loc;
7905 c_parser_consume_token (parser);
7906 brace_loc = c_parser_peek_token (parser)->location;
7907 c_parser_consume_token (parser);
7908 if (!building_stmt_list_p ())
7910 error_at (loc, "braced-group within expression allowed "
7911 "only inside a function");
7912 parser->error = true;
7913 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7914 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7915 expr.set_error ();
7916 break;
7918 stmt = c_begin_stmt_expr ();
7919 c_parser_compound_statement_nostart (parser);
7920 location_t close_loc = c_parser_peek_token (parser)->location;
7921 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7922 "expected %<)%>");
7923 pedwarn (loc, OPT_Wpedantic,
7924 "ISO C forbids braced-groups within expressions");
7925 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7926 set_c_expr_source_range (&expr, loc, close_loc);
7927 mark_exp_read (expr.value);
7929 else
7931 /* A parenthesized expression. */
7932 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7933 c_parser_consume_token (parser);
7934 expr = c_parser_expression (parser);
7935 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7936 TREE_NO_WARNING (expr.value) = 1;
7937 if (expr.original_code != C_MAYBE_CONST_EXPR
7938 && expr.original_code != SIZEOF_EXPR)
7939 expr.original_code = ERROR_MARK;
7940 /* Don't change EXPR.ORIGINAL_TYPE. */
7941 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7942 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7943 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7944 "expected %<)%>", loc_open_paren);
7946 break;
7947 case CPP_KEYWORD:
7948 switch (c_parser_peek_token (parser)->keyword)
7950 case RID_FUNCTION_NAME:
7951 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7952 "%<__FUNCTION__%> predefined identifier");
7953 expr.value = fname_decl (loc,
7954 c_parser_peek_token (parser)->keyword,
7955 c_parser_peek_token (parser)->value);
7956 set_c_expr_source_range (&expr, loc, loc);
7957 c_parser_consume_token (parser);
7958 break;
7959 case RID_PRETTY_FUNCTION_NAME:
7960 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7961 "%<__PRETTY_FUNCTION__%> predefined identifier");
7962 expr.value = fname_decl (loc,
7963 c_parser_peek_token (parser)->keyword,
7964 c_parser_peek_token (parser)->value);
7965 set_c_expr_source_range (&expr, loc, loc);
7966 c_parser_consume_token (parser);
7967 break;
7968 case RID_C99_FUNCTION_NAME:
7969 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7970 "%<__func__%> predefined identifier");
7971 expr.value = fname_decl (loc,
7972 c_parser_peek_token (parser)->keyword,
7973 c_parser_peek_token (parser)->value);
7974 set_c_expr_source_range (&expr, loc, loc);
7975 c_parser_consume_token (parser);
7976 break;
7977 case RID_VA_ARG:
7979 location_t start_loc = loc;
7980 c_parser_consume_token (parser);
7981 matching_parens parens;
7982 if (!parens.require_open (parser))
7984 expr.set_error ();
7985 break;
7987 e1 = c_parser_expr_no_commas (parser, NULL);
7988 mark_exp_read (e1.value);
7989 e1.value = c_fully_fold (e1.value, false, NULL);
7990 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7992 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7993 expr.set_error ();
7994 break;
7996 loc = c_parser_peek_token (parser)->location;
7997 t1 = c_parser_type_name (parser);
7998 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7999 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8000 "expected %<)%>");
8001 if (t1 == NULL)
8003 expr.set_error ();
8005 else
8007 tree type_expr = NULL_TREE;
8008 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8009 groktypename (t1, &type_expr, NULL));
8010 if (type_expr)
8012 expr.value = build2 (C_MAYBE_CONST_EXPR,
8013 TREE_TYPE (expr.value), type_expr,
8014 expr.value);
8015 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8017 set_c_expr_source_range (&expr, start_loc, end_loc);
8020 break;
8021 case RID_OFFSETOF:
8023 c_parser_consume_token (parser);
8024 matching_parens parens;
8025 if (!parens.require_open (parser))
8027 expr.set_error ();
8028 break;
8030 t1 = c_parser_type_name (parser);
8031 if (t1 == NULL)
8032 parser->error = true;
8033 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8034 gcc_assert (parser->error);
8035 if (parser->error)
8037 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8038 expr.set_error ();
8039 break;
8041 tree type = groktypename (t1, NULL, NULL);
8042 tree offsetof_ref;
8043 if (type == error_mark_node)
8044 offsetof_ref = error_mark_node;
8045 else
8047 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8048 SET_EXPR_LOCATION (offsetof_ref, loc);
8050 /* Parse the second argument to __builtin_offsetof. We
8051 must have one identifier, and beyond that we want to
8052 accept sub structure and sub array references. */
8053 if (c_parser_next_token_is (parser, CPP_NAME))
8055 c_token *comp_tok = c_parser_peek_token (parser);
8056 offsetof_ref = build_component_ref
8057 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8058 c_parser_consume_token (parser);
8059 while (c_parser_next_token_is (parser, CPP_DOT)
8060 || c_parser_next_token_is (parser,
8061 CPP_OPEN_SQUARE)
8062 || c_parser_next_token_is (parser,
8063 CPP_DEREF))
8065 if (c_parser_next_token_is (parser, CPP_DEREF))
8067 loc = c_parser_peek_token (parser)->location;
8068 offsetof_ref = build_array_ref (loc,
8069 offsetof_ref,
8070 integer_zero_node);
8071 goto do_dot;
8073 else if (c_parser_next_token_is (parser, CPP_DOT))
8075 do_dot:
8076 c_parser_consume_token (parser);
8077 if (c_parser_next_token_is_not (parser,
8078 CPP_NAME))
8080 c_parser_error (parser, "expected identifier");
8081 break;
8083 c_token *comp_tok = c_parser_peek_token (parser);
8084 offsetof_ref = build_component_ref
8085 (loc, offsetof_ref, comp_tok->value,
8086 comp_tok->location);
8087 c_parser_consume_token (parser);
8089 else
8091 struct c_expr ce;
8092 tree idx;
8093 loc = c_parser_peek_token (parser)->location;
8094 c_parser_consume_token (parser);
8095 ce = c_parser_expression (parser);
8096 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8097 idx = ce.value;
8098 idx = c_fully_fold (idx, false, NULL);
8099 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8100 "expected %<]%>");
8101 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8105 else
8106 c_parser_error (parser, "expected identifier");
8107 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8109 "expected %<)%>");
8110 expr.value = fold_offsetof (offsetof_ref);
8111 set_c_expr_source_range (&expr, loc, end_loc);
8113 break;
8114 case RID_CHOOSE_EXPR:
8116 vec<c_expr_t, va_gc> *cexpr_list;
8117 c_expr_t *e1_p, *e2_p, *e3_p;
8118 tree c;
8119 location_t close_paren_loc;
8121 c_parser_consume_token (parser);
8122 if (!c_parser_get_builtin_args (parser,
8123 "__builtin_choose_expr",
8124 &cexpr_list, true,
8125 &close_paren_loc))
8127 expr.set_error ();
8128 break;
8131 if (vec_safe_length (cexpr_list) != 3)
8133 error_at (loc, "wrong number of arguments to "
8134 "%<__builtin_choose_expr%>");
8135 expr.set_error ();
8136 break;
8139 e1_p = &(*cexpr_list)[0];
8140 e2_p = &(*cexpr_list)[1];
8141 e3_p = &(*cexpr_list)[2];
8143 c = e1_p->value;
8144 mark_exp_read (e2_p->value);
8145 mark_exp_read (e3_p->value);
8146 if (TREE_CODE (c) != INTEGER_CST
8147 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8148 error_at (loc,
8149 "first argument to %<__builtin_choose_expr%> not"
8150 " a constant");
8151 constant_expression_warning (c);
8152 expr = integer_zerop (c) ? *e3_p : *e2_p;
8153 set_c_expr_source_range (&expr, loc, close_paren_loc);
8154 break;
8156 case RID_TYPES_COMPATIBLE_P:
8158 c_parser_consume_token (parser);
8159 matching_parens parens;
8160 if (!parens.require_open (parser))
8162 expr.set_error ();
8163 break;
8165 t1 = c_parser_type_name (parser);
8166 if (t1 == NULL)
8168 expr.set_error ();
8169 break;
8171 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8173 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8174 expr.set_error ();
8175 break;
8177 t2 = c_parser_type_name (parser);
8178 if (t2 == NULL)
8180 expr.set_error ();
8181 break;
8183 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8184 parens.skip_until_found_close (parser);
8185 tree e1, e2;
8186 e1 = groktypename (t1, NULL, NULL);
8187 e2 = groktypename (t2, NULL, NULL);
8188 if (e1 == error_mark_node || e2 == error_mark_node)
8190 expr.set_error ();
8191 break;
8194 e1 = TYPE_MAIN_VARIANT (e1);
8195 e2 = TYPE_MAIN_VARIANT (e2);
8197 expr.value
8198 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8199 set_c_expr_source_range (&expr, loc, close_paren_loc);
8201 break;
8202 case RID_BUILTIN_TGMATH:
8204 vec<c_expr_t, va_gc> *cexpr_list;
8205 location_t close_paren_loc;
8207 c_parser_consume_token (parser);
8208 if (!c_parser_get_builtin_args (parser,
8209 "__builtin_tgmath",
8210 &cexpr_list, false,
8211 &close_paren_loc))
8213 expr.set_error ();
8214 break;
8217 if (vec_safe_length (cexpr_list) < 3)
8219 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8220 expr.set_error ();
8221 break;
8224 unsigned int i;
8225 c_expr_t *p;
8226 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8227 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8228 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8229 if (nargs == 0)
8231 expr.set_error ();
8232 break;
8234 if (vec_safe_length (cexpr_list) < nargs)
8236 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8237 expr.set_error ();
8238 break;
8240 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8241 if (num_functions < 2)
8243 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8244 expr.set_error ();
8245 break;
8248 /* The first NUM_FUNCTIONS expressions are the function
8249 pointers. The remaining NARGS expressions are the
8250 arguments that are to be passed to one of those
8251 functions, chosen following <tgmath.h> rules. */
8252 for (unsigned int j = 1; j < num_functions; j++)
8254 unsigned int this_nargs
8255 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8256 if (this_nargs == 0)
8258 expr.set_error ();
8259 goto out;
8261 if (this_nargs != nargs)
8263 error_at ((*cexpr_list)[j].get_location (),
8264 "argument %u of %<__builtin_tgmath%> has "
8265 "wrong number of arguments", j + 1);
8266 expr.set_error ();
8267 goto out;
8271 /* The functions all have the same number of arguments.
8272 Determine whether arguments and return types vary in
8273 ways permitted for <tgmath.h> functions. */
8274 /* The first entry in each of these vectors is for the
8275 return type, subsequent entries for parameter
8276 types. */
8277 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8278 auto_vec<tree> parm_first (nargs + 1);
8279 auto_vec<bool> parm_complex (nargs + 1);
8280 auto_vec<bool> parm_varies (nargs + 1);
8281 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8282 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8283 parm_first.quick_push (first_ret);
8284 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8285 parm_varies.quick_push (false);
8286 function_args_iterator iter;
8287 tree t;
8288 unsigned int argpos;
8289 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8291 if (t == void_type_node)
8292 break;
8293 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8294 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8295 parm_varies.quick_push (false);
8297 for (unsigned int j = 1; j < num_functions; j++)
8299 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8300 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8301 if (ret != parm_first[0])
8303 parm_varies[0] = true;
8304 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8305 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8307 error_at ((*cexpr_list)[0].get_location (),
8308 "invalid type-generic return type for "
8309 "argument %u of %<__builtin_tgmath%>",
8311 expr.set_error ();
8312 goto out;
8314 if (!SCALAR_FLOAT_TYPE_P (ret)
8315 && !COMPLEX_FLOAT_TYPE_P (ret))
8317 error_at ((*cexpr_list)[j].get_location (),
8318 "invalid type-generic return type for "
8319 "argument %u of %<__builtin_tgmath%>",
8320 j + 1);
8321 expr.set_error ();
8322 goto out;
8325 if (TREE_CODE (ret) == COMPLEX_TYPE)
8326 parm_complex[0] = true;
8327 argpos = 1;
8328 FOREACH_FUNCTION_ARGS (type, t, iter)
8330 if (t == void_type_node)
8331 break;
8332 t = TYPE_MAIN_VARIANT (t);
8333 if (t != parm_first[argpos])
8335 parm_varies[argpos] = true;
8336 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8337 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8339 error_at ((*cexpr_list)[0].get_location (),
8340 "invalid type-generic type for "
8341 "argument %u of argument %u of "
8342 "%<__builtin_tgmath%>", argpos, 1);
8343 expr.set_error ();
8344 goto out;
8346 if (!SCALAR_FLOAT_TYPE_P (t)
8347 && !COMPLEX_FLOAT_TYPE_P (t))
8349 error_at ((*cexpr_list)[j].get_location (),
8350 "invalid type-generic type for "
8351 "argument %u of argument %u of "
8352 "%<__builtin_tgmath%>", argpos, j + 1);
8353 expr.set_error ();
8354 goto out;
8357 if (TREE_CODE (t) == COMPLEX_TYPE)
8358 parm_complex[argpos] = true;
8359 argpos++;
8362 enum tgmath_parm_kind max_variation = tgmath_fixed;
8363 for (unsigned int j = 0; j <= nargs; j++)
8365 enum tgmath_parm_kind this_kind;
8366 if (parm_varies[j])
8368 if (parm_complex[j])
8369 max_variation = this_kind = tgmath_complex;
8370 else
8372 this_kind = tgmath_real;
8373 if (max_variation != tgmath_complex)
8374 max_variation = tgmath_real;
8377 else
8378 this_kind = tgmath_fixed;
8379 parm_kind.quick_push (this_kind);
8381 if (max_variation == tgmath_fixed)
8383 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8384 "all have the same type");
8385 expr.set_error ();
8386 break;
8389 /* Identify a parameter (not the return type) that varies,
8390 including with complex types if any variation includes
8391 complex types; there must be at least one such
8392 parameter. */
8393 unsigned int tgarg = 0;
8394 for (unsigned int j = 1; j <= nargs; j++)
8395 if (parm_kind[j] == max_variation)
8397 tgarg = j;
8398 break;
8400 if (tgarg == 0)
8402 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8403 "lack type-generic parameter");
8404 expr.set_error ();
8405 break;
8408 /* Determine the type of the relevant parameter for each
8409 function. */
8410 auto_vec<tree> tg_type (num_functions);
8411 for (unsigned int j = 0; j < num_functions; j++)
8413 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8414 argpos = 1;
8415 FOREACH_FUNCTION_ARGS (type, t, iter)
8417 if (argpos == tgarg)
8419 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8420 break;
8422 argpos++;
8426 /* Verify that the corresponding types are different for
8427 all the listed functions. Also determine whether all
8428 the types are complex, whether all the types are
8429 standard or binary, and whether all the types are
8430 decimal. */
8431 bool all_complex = true;
8432 bool all_binary = true;
8433 bool all_decimal = true;
8434 hash_set<tree> tg_types;
8435 FOR_EACH_VEC_ELT (tg_type, i, t)
8437 if (TREE_CODE (t) == COMPLEX_TYPE)
8438 all_decimal = false;
8439 else
8441 all_complex = false;
8442 if (DECIMAL_FLOAT_TYPE_P (t))
8443 all_binary = false;
8444 else
8445 all_decimal = false;
8447 if (tg_types.add (t))
8449 error_at ((*cexpr_list)[i].get_location (),
8450 "duplicate type-generic parameter type for "
8451 "function argument %u of %<__builtin_tgmath%>",
8452 i + 1);
8453 expr.set_error ();
8454 goto out;
8458 /* Verify that other parameters and the return type whose
8459 types vary have their types varying in the correct
8460 way. */
8461 for (unsigned int j = 0; j < num_functions; j++)
8463 tree exp_type = tg_type[j];
8464 tree exp_real_type = exp_type;
8465 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8466 exp_real_type = TREE_TYPE (exp_type);
8467 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8468 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8469 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8470 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8472 error_at ((*cexpr_list)[j].get_location (),
8473 "bad return type for function argument %u "
8474 "of %<__builtin_tgmath%>", j + 1);
8475 expr.set_error ();
8476 goto out;
8478 argpos = 1;
8479 FOREACH_FUNCTION_ARGS (type, t, iter)
8481 if (t == void_type_node)
8482 break;
8483 t = TYPE_MAIN_VARIANT (t);
8484 if ((parm_kind[argpos] == tgmath_complex
8485 && t != exp_type)
8486 || (parm_kind[argpos] == tgmath_real
8487 && t != exp_real_type))
8489 error_at ((*cexpr_list)[j].get_location (),
8490 "bad type for argument %u of "
8491 "function argument %u of "
8492 "%<__builtin_tgmath%>", argpos, j + 1);
8493 expr.set_error ();
8494 goto out;
8496 argpos++;
8500 /* The functions listed are a valid set of functions for a
8501 <tgmath.h> macro to select between. Identify the
8502 matching function, if any. First, the argument types
8503 must be combined following <tgmath.h> rules. Integer
8504 types are treated as _Decimal64 if any type-generic
8505 argument is decimal, or if the only alternatives for
8506 type-generic arguments are of decimal types, and are
8507 otherwise treated as double (or _Complex double for
8508 complex integer types). After that adjustment, types
8509 are combined following the usual arithmetic
8510 conversions. If the function only accepts complex
8511 arguments, a complex type is produced. */
8512 bool arg_complex = all_complex;
8513 bool arg_binary = all_binary;
8514 bool arg_int_decimal = all_decimal;
8515 for (unsigned int j = 1; j <= nargs; j++)
8517 if (parm_kind[j] == tgmath_fixed)
8518 continue;
8519 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8520 tree type = TREE_TYPE (ce->value);
8521 if (!INTEGRAL_TYPE_P (type)
8522 && !SCALAR_FLOAT_TYPE_P (type)
8523 && TREE_CODE (type) != COMPLEX_TYPE)
8525 error_at (ce->get_location (),
8526 "invalid type of argument %u of type-generic "
8527 "function", j);
8528 expr.set_error ();
8529 goto out;
8531 if (DECIMAL_FLOAT_TYPE_P (type))
8533 arg_int_decimal = true;
8534 if (all_complex)
8536 error_at (ce->get_location (),
8537 "decimal floating-point argument %u to "
8538 "complex-only type-generic function", j);
8539 expr.set_error ();
8540 goto out;
8542 else if (all_binary)
8544 error_at (ce->get_location (),
8545 "decimal floating-point argument %u to "
8546 "binary-only type-generic function", j);
8547 expr.set_error ();
8548 goto out;
8550 else if (arg_complex)
8552 error_at (ce->get_location (),
8553 "both complex and decimal floating-point "
8554 "arguments to type-generic function");
8555 expr.set_error ();
8556 goto out;
8558 else if (arg_binary)
8560 error_at (ce->get_location (),
8561 "both binary and decimal floating-point "
8562 "arguments to type-generic function");
8563 expr.set_error ();
8564 goto out;
8567 else if (TREE_CODE (type) == COMPLEX_TYPE)
8569 arg_complex = true;
8570 if (COMPLEX_FLOAT_TYPE_P (type))
8571 arg_binary = true;
8572 if (all_decimal)
8574 error_at (ce->get_location (),
8575 "complex argument %u to "
8576 "decimal-only type-generic function", j);
8577 expr.set_error ();
8578 goto out;
8580 else if (arg_int_decimal)
8582 error_at (ce->get_location (),
8583 "both complex and decimal floating-point "
8584 "arguments to type-generic function");
8585 expr.set_error ();
8586 goto out;
8589 else if (SCALAR_FLOAT_TYPE_P (type))
8591 arg_binary = true;
8592 if (all_decimal)
8594 error_at (ce->get_location (),
8595 "binary argument %u to "
8596 "decimal-only type-generic function", j);
8597 expr.set_error ();
8598 goto out;
8600 else if (arg_int_decimal)
8602 error_at (ce->get_location (),
8603 "both binary and decimal floating-point "
8604 "arguments to type-generic function");
8605 expr.set_error ();
8606 goto out;
8610 tree arg_real = NULL_TREE;
8611 for (unsigned int j = 1; j <= nargs; j++)
8613 if (parm_kind[j] == tgmath_fixed)
8614 continue;
8615 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8616 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8617 if (TREE_CODE (type) == COMPLEX_TYPE)
8618 type = TREE_TYPE (type);
8619 if (INTEGRAL_TYPE_P (type))
8620 type = (arg_int_decimal
8621 ? dfloat64_type_node
8622 : double_type_node);
8623 if (arg_real == NULL_TREE)
8624 arg_real = type;
8625 else
8626 arg_real = common_type (arg_real, type);
8627 if (arg_real == error_mark_node)
8629 expr.set_error ();
8630 goto out;
8633 tree arg_type = (arg_complex
8634 ? build_complex_type (arg_real)
8635 : arg_real);
8637 /* Look for a function to call with type-generic parameter
8638 type ARG_TYPE. */
8639 c_expr_t *fn = NULL;
8640 for (unsigned int j = 0; j < num_functions; j++)
8642 if (tg_type[j] == arg_type)
8644 fn = &(*cexpr_list)[j];
8645 break;
8648 if (fn == NULL
8649 && parm_kind[0] == tgmath_fixed
8650 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8652 /* Presume this is a macro that rounds its result to a
8653 narrower type, and look for the first function with
8654 at least the range and precision of the argument
8655 type. */
8656 for (unsigned int j = 0; j < num_functions; j++)
8658 if (arg_complex
8659 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8660 continue;
8661 tree real_tg_type = (arg_complex
8662 ? TREE_TYPE (tg_type[j])
8663 : tg_type[j]);
8664 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8665 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8666 continue;
8667 scalar_float_mode arg_mode
8668 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8669 scalar_float_mode tg_mode
8670 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8671 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8672 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8673 if (arg_fmt->b == tg_fmt->b
8674 && arg_fmt->p <= tg_fmt->p
8675 && arg_fmt->emax <= tg_fmt->emax
8676 && (arg_fmt->emin - arg_fmt->p
8677 >= tg_fmt->emin - tg_fmt->p))
8679 fn = &(*cexpr_list)[j];
8680 break;
8684 if (fn == NULL)
8686 error_at (loc, "no matching function for type-generic call");
8687 expr.set_error ();
8688 break;
8691 /* Construct a call to FN. */
8692 vec<tree, va_gc> *args;
8693 vec_alloc (args, nargs);
8694 vec<tree, va_gc> *origtypes;
8695 vec_alloc (origtypes, nargs);
8696 auto_vec<location_t> arg_loc (nargs);
8697 for (unsigned int j = 0; j < nargs; j++)
8699 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8700 args->quick_push (ce->value);
8701 arg_loc.quick_push (ce->get_location ());
8702 origtypes->quick_push (ce->original_type);
8704 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8705 args, origtypes);
8706 set_c_expr_source_range (&expr, loc, close_paren_loc);
8707 break;
8709 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8711 vec<c_expr_t, va_gc> *cexpr_list;
8712 c_expr_t *e2_p;
8713 tree chain_value;
8714 location_t close_paren_loc;
8716 c_parser_consume_token (parser);
8717 if (!c_parser_get_builtin_args (parser,
8718 "__builtin_call_with_static_chain",
8719 &cexpr_list, false,
8720 &close_paren_loc))
8722 expr.set_error ();
8723 break;
8725 if (vec_safe_length (cexpr_list) != 2)
8727 error_at (loc, "wrong number of arguments to "
8728 "%<__builtin_call_with_static_chain%>");
8729 expr.set_error ();
8730 break;
8733 expr = (*cexpr_list)[0];
8734 e2_p = &(*cexpr_list)[1];
8735 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8736 chain_value = e2_p->value;
8737 mark_exp_read (chain_value);
8739 if (TREE_CODE (expr.value) != CALL_EXPR)
8740 error_at (loc, "first argument to "
8741 "%<__builtin_call_with_static_chain%> "
8742 "must be a call expression");
8743 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8744 error_at (loc, "second argument to "
8745 "%<__builtin_call_with_static_chain%> "
8746 "must be a pointer type");
8747 else
8748 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8749 set_c_expr_source_range (&expr, loc, close_paren_loc);
8750 break;
8752 case RID_BUILTIN_COMPLEX:
8754 vec<c_expr_t, va_gc> *cexpr_list;
8755 c_expr_t *e1_p, *e2_p;
8756 location_t close_paren_loc;
8758 c_parser_consume_token (parser);
8759 if (!c_parser_get_builtin_args (parser,
8760 "__builtin_complex",
8761 &cexpr_list, false,
8762 &close_paren_loc))
8764 expr.set_error ();
8765 break;
8768 if (vec_safe_length (cexpr_list) != 2)
8770 error_at (loc, "wrong number of arguments to "
8771 "%<__builtin_complex%>");
8772 expr.set_error ();
8773 break;
8776 e1_p = &(*cexpr_list)[0];
8777 e2_p = &(*cexpr_list)[1];
8779 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8780 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8781 e1_p->value = convert (TREE_TYPE (e1_p->value),
8782 TREE_OPERAND (e1_p->value, 0));
8783 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8784 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8785 e2_p->value = convert (TREE_TYPE (e2_p->value),
8786 TREE_OPERAND (e2_p->value, 0));
8787 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8788 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8789 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8790 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8792 error_at (loc, "%<__builtin_complex%> operand "
8793 "not of real binary floating-point type");
8794 expr.set_error ();
8795 break;
8797 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8798 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8800 error_at (loc,
8801 "%<__builtin_complex%> operands of different types");
8802 expr.set_error ();
8803 break;
8805 pedwarn_c90 (loc, OPT_Wpedantic,
8806 "ISO C90 does not support complex types");
8807 expr.value = build2_loc (loc, COMPLEX_EXPR,
8808 build_complex_type
8809 (TYPE_MAIN_VARIANT
8810 (TREE_TYPE (e1_p->value))),
8811 e1_p->value, e2_p->value);
8812 set_c_expr_source_range (&expr, loc, close_paren_loc);
8813 break;
8815 case RID_BUILTIN_SHUFFLE:
8817 vec<c_expr_t, va_gc> *cexpr_list;
8818 unsigned int i;
8819 c_expr_t *p;
8820 location_t close_paren_loc;
8822 c_parser_consume_token (parser);
8823 if (!c_parser_get_builtin_args (parser,
8824 "__builtin_shuffle",
8825 &cexpr_list, false,
8826 &close_paren_loc))
8828 expr.set_error ();
8829 break;
8832 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8833 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8835 if (vec_safe_length (cexpr_list) == 2)
8836 expr.value =
8837 c_build_vec_perm_expr
8838 (loc, (*cexpr_list)[0].value,
8839 NULL_TREE, (*cexpr_list)[1].value);
8841 else if (vec_safe_length (cexpr_list) == 3)
8842 expr.value =
8843 c_build_vec_perm_expr
8844 (loc, (*cexpr_list)[0].value,
8845 (*cexpr_list)[1].value,
8846 (*cexpr_list)[2].value);
8847 else
8849 error_at (loc, "wrong number of arguments to "
8850 "%<__builtin_shuffle%>");
8851 expr.set_error ();
8853 set_c_expr_source_range (&expr, loc, close_paren_loc);
8854 break;
8856 case RID_AT_SELECTOR:
8858 gcc_assert (c_dialect_objc ());
8859 c_parser_consume_token (parser);
8860 matching_parens parens;
8861 if (!parens.require_open (parser))
8863 expr.set_error ();
8864 break;
8866 tree sel = c_parser_objc_selector_arg (parser);
8867 location_t close_loc = c_parser_peek_token (parser)->location;
8868 parens.skip_until_found_close (parser);
8869 expr.value = objc_build_selector_expr (loc, sel);
8870 set_c_expr_source_range (&expr, loc, close_loc);
8872 break;
8873 case RID_AT_PROTOCOL:
8875 gcc_assert (c_dialect_objc ());
8876 c_parser_consume_token (parser);
8877 matching_parens parens;
8878 if (!parens.require_open (parser))
8880 expr.set_error ();
8881 break;
8883 if (c_parser_next_token_is_not (parser, CPP_NAME))
8885 c_parser_error (parser, "expected identifier");
8886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8887 expr.set_error ();
8888 break;
8890 tree id = c_parser_peek_token (parser)->value;
8891 c_parser_consume_token (parser);
8892 location_t close_loc = c_parser_peek_token (parser)->location;
8893 parens.skip_until_found_close (parser);
8894 expr.value = objc_build_protocol_expr (id);
8895 set_c_expr_source_range (&expr, loc, close_loc);
8897 break;
8898 case RID_AT_ENCODE:
8900 /* Extension to support C-structures in the archiver. */
8901 gcc_assert (c_dialect_objc ());
8902 c_parser_consume_token (parser);
8903 matching_parens parens;
8904 if (!parens.require_open (parser))
8906 expr.set_error ();
8907 break;
8909 t1 = c_parser_type_name (parser);
8910 if (t1 == NULL)
8912 expr.set_error ();
8913 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8914 break;
8916 location_t close_loc = c_parser_peek_token (parser)->location;
8917 parens.skip_until_found_close (parser);
8918 tree type = groktypename (t1, NULL, NULL);
8919 expr.value = objc_build_encode_expr (type);
8920 set_c_expr_source_range (&expr, loc, close_loc);
8922 break;
8923 case RID_GENERIC:
8924 expr = c_parser_generic_selection (parser);
8925 break;
8926 default:
8927 c_parser_error (parser, "expected expression");
8928 expr.set_error ();
8929 break;
8931 break;
8932 case CPP_OPEN_SQUARE:
8933 if (c_dialect_objc ())
8935 tree receiver, args;
8936 c_parser_consume_token (parser);
8937 receiver = c_parser_objc_receiver (parser);
8938 args = c_parser_objc_message_args (parser);
8939 location_t close_loc = c_parser_peek_token (parser)->location;
8940 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8941 "expected %<]%>");
8942 expr.value = objc_build_message_expr (receiver, args);
8943 set_c_expr_source_range (&expr, loc, close_loc);
8944 break;
8946 /* Else fall through to report error. */
8947 /* FALLTHRU */
8948 default:
8949 c_parser_error (parser, "expected expression");
8950 expr.set_error ();
8951 break;
8953 out:
8954 return c_parser_postfix_expression_after_primary
8955 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8958 /* Parse a postfix expression after a parenthesized type name: the
8959 brace-enclosed initializer of a compound literal, possibly followed
8960 by some postfix operators. This is separate because it is not
8961 possible to tell until after the type name whether a cast
8962 expression has a cast or a compound literal, or whether the operand
8963 of sizeof is a parenthesized type name or starts with a compound
8964 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8965 location of the first token after the parentheses around the type
8966 name. */
8968 static struct c_expr
8969 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8970 struct c_type_name *type_name,
8971 location_t type_loc)
8973 tree type;
8974 struct c_expr init;
8975 bool non_const;
8976 struct c_expr expr;
8977 location_t start_loc;
8978 tree type_expr = NULL_TREE;
8979 bool type_expr_const = true;
8980 check_compound_literal_type (type_loc, type_name);
8981 rich_location richloc (line_table, type_loc);
8982 start_init (NULL_TREE, NULL, 0, &richloc);
8983 type = groktypename (type_name, &type_expr, &type_expr_const);
8984 start_loc = c_parser_peek_token (parser)->location;
8985 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8987 error_at (type_loc, "compound literal has variable size");
8988 type = error_mark_node;
8990 init = c_parser_braced_init (parser, type, false, NULL);
8991 finish_init ();
8992 maybe_warn_string_init (type_loc, type, init);
8994 if (type != error_mark_node
8995 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8996 && current_function_decl)
8998 error ("compound literal qualified by address-space qualifier");
8999 type = error_mark_node;
9002 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9003 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9004 ? CONSTRUCTOR_NON_CONST (init.value)
9005 : init.original_code == C_MAYBE_CONST_EXPR);
9006 non_const |= !type_expr_const;
9007 unsigned int alignas_align = 0;
9008 if (type != error_mark_node
9009 && type_name->specs->align_log != -1)
9011 alignas_align = 1U << type_name->specs->align_log;
9012 if (alignas_align < min_align_of_type (type))
9014 error_at (type_name->specs->locations[cdw_alignas],
9015 "%<_Alignas%> specifiers cannot reduce "
9016 "alignment of compound literal");
9017 alignas_align = 0;
9020 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9021 alignas_align);
9022 set_c_expr_source_range (&expr, init.src_range);
9023 expr.original_code = ERROR_MARK;
9024 expr.original_type = NULL;
9025 if (type != error_mark_node
9026 && expr.value != error_mark_node
9027 && type_expr)
9029 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9031 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9032 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9034 else
9036 gcc_assert (!non_const);
9037 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9038 type_expr, expr.value);
9041 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9044 /* Callback function for sizeof_pointer_memaccess_warning to compare
9045 types. */
9047 static bool
9048 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9050 return comptypes (type1, type2) == 1;
9053 /* Parse a postfix expression after the initial primary or compound
9054 literal; that is, parse a series of postfix operators.
9056 EXPR_LOC is the location of the primary expression. */
9058 static struct c_expr
9059 c_parser_postfix_expression_after_primary (c_parser *parser,
9060 location_t expr_loc,
9061 struct c_expr expr)
9063 struct c_expr orig_expr;
9064 tree ident, idx;
9065 location_t sizeof_arg_loc[3], comp_loc;
9066 tree sizeof_arg[3];
9067 unsigned int literal_zero_mask;
9068 unsigned int i;
9069 vec<tree, va_gc> *exprlist;
9070 vec<tree, va_gc> *origtypes = NULL;
9071 vec<location_t> arg_loc = vNULL;
9072 location_t start;
9073 location_t finish;
9075 while (true)
9077 location_t op_loc = c_parser_peek_token (parser)->location;
9078 switch (c_parser_peek_token (parser)->type)
9080 case CPP_OPEN_SQUARE:
9081 /* Array reference. */
9082 c_parser_consume_token (parser);
9083 idx = c_parser_expression (parser).value;
9084 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9085 "expected %<]%>");
9086 start = expr.get_start ();
9087 finish = parser->tokens_buf[0].location;
9088 expr.value = build_array_ref (op_loc, expr.value, idx);
9089 set_c_expr_source_range (&expr, start, finish);
9090 expr.original_code = ERROR_MARK;
9091 expr.original_type = NULL;
9092 break;
9093 case CPP_OPEN_PAREN:
9094 /* Function call. */
9095 c_parser_consume_token (parser);
9096 for (i = 0; i < 3; i++)
9098 sizeof_arg[i] = NULL_TREE;
9099 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9101 literal_zero_mask = 0;
9102 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9103 exprlist = NULL;
9104 else
9105 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9106 sizeof_arg_loc, sizeof_arg,
9107 &arg_loc, &literal_zero_mask);
9108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9109 "expected %<)%>");
9110 orig_expr = expr;
9111 mark_exp_read (expr.value);
9112 if (warn_sizeof_pointer_memaccess)
9113 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9114 expr.value, exprlist,
9115 sizeof_arg,
9116 sizeof_ptr_memacc_comptypes);
9117 if (TREE_CODE (expr.value) == FUNCTION_DECL
9118 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9119 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9120 && vec_safe_length (exprlist) == 3)
9122 tree arg0 = (*exprlist)[0];
9123 tree arg2 = (*exprlist)[2];
9124 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9127 start = expr.get_start ();
9128 finish = parser->tokens_buf[0].get_finish ();
9129 expr.value
9130 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9131 exprlist, origtypes);
9132 set_c_expr_source_range (&expr, start, finish);
9134 expr.original_code = ERROR_MARK;
9135 if (TREE_CODE (expr.value) == INTEGER_CST
9136 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9137 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9138 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9139 expr.original_code = C_MAYBE_CONST_EXPR;
9140 expr.original_type = NULL;
9141 if (exprlist)
9143 release_tree_vector (exprlist);
9144 release_tree_vector (origtypes);
9146 arg_loc.release ();
9147 break;
9148 case CPP_DOT:
9149 /* Structure element reference. */
9150 c_parser_consume_token (parser);
9151 expr = default_function_array_conversion (expr_loc, expr);
9152 if (c_parser_next_token_is (parser, CPP_NAME))
9154 c_token *comp_tok = c_parser_peek_token (parser);
9155 ident = comp_tok->value;
9156 comp_loc = comp_tok->location;
9158 else
9160 c_parser_error (parser, "expected identifier");
9161 expr.set_error ();
9162 expr.original_code = ERROR_MARK;
9163 expr.original_type = NULL;
9164 return expr;
9166 start = expr.get_start ();
9167 finish = c_parser_peek_token (parser)->get_finish ();
9168 c_parser_consume_token (parser);
9169 expr.value = build_component_ref (op_loc, expr.value, ident,
9170 comp_loc);
9171 set_c_expr_source_range (&expr, start, finish);
9172 expr.original_code = ERROR_MARK;
9173 if (TREE_CODE (expr.value) != COMPONENT_REF)
9174 expr.original_type = NULL;
9175 else
9177 /* Remember the original type of a bitfield. */
9178 tree field = TREE_OPERAND (expr.value, 1);
9179 if (TREE_CODE (field) != FIELD_DECL)
9180 expr.original_type = NULL;
9181 else
9182 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9184 break;
9185 case CPP_DEREF:
9186 /* Structure element reference. */
9187 c_parser_consume_token (parser);
9188 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9189 if (c_parser_next_token_is (parser, CPP_NAME))
9191 c_token *comp_tok = c_parser_peek_token (parser);
9192 ident = comp_tok->value;
9193 comp_loc = comp_tok->location;
9195 else
9197 c_parser_error (parser, "expected identifier");
9198 expr.set_error ();
9199 expr.original_code = ERROR_MARK;
9200 expr.original_type = NULL;
9201 return expr;
9203 start = expr.get_start ();
9204 finish = c_parser_peek_token (parser)->get_finish ();
9205 c_parser_consume_token (parser);
9206 expr.value = build_component_ref (op_loc,
9207 build_indirect_ref (op_loc,
9208 expr.value,
9209 RO_ARROW),
9210 ident, comp_loc);
9211 set_c_expr_source_range (&expr, start, finish);
9212 expr.original_code = ERROR_MARK;
9213 if (TREE_CODE (expr.value) != COMPONENT_REF)
9214 expr.original_type = NULL;
9215 else
9217 /* Remember the original type of a bitfield. */
9218 tree field = TREE_OPERAND (expr.value, 1);
9219 if (TREE_CODE (field) != FIELD_DECL)
9220 expr.original_type = NULL;
9221 else
9222 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9224 break;
9225 case CPP_PLUS_PLUS:
9226 /* Postincrement. */
9227 start = expr.get_start ();
9228 finish = c_parser_peek_token (parser)->get_finish ();
9229 c_parser_consume_token (parser);
9230 expr = default_function_array_read_conversion (expr_loc, expr);
9231 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9232 expr.value, false);
9233 set_c_expr_source_range (&expr, start, finish);
9234 expr.original_code = ERROR_MARK;
9235 expr.original_type = NULL;
9236 break;
9237 case CPP_MINUS_MINUS:
9238 /* Postdecrement. */
9239 start = expr.get_start ();
9240 finish = c_parser_peek_token (parser)->get_finish ();
9241 c_parser_consume_token (parser);
9242 expr = default_function_array_read_conversion (expr_loc, expr);
9243 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9244 expr.value, false);
9245 set_c_expr_source_range (&expr, start, finish);
9246 expr.original_code = ERROR_MARK;
9247 expr.original_type = NULL;
9248 break;
9249 default:
9250 return expr;
9255 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9257 expression:
9258 assignment-expression
9259 expression , assignment-expression
9262 static struct c_expr
9263 c_parser_expression (c_parser *parser)
9265 location_t tloc = c_parser_peek_token (parser)->location;
9266 struct c_expr expr;
9267 expr = c_parser_expr_no_commas (parser, NULL);
9268 if (c_parser_next_token_is (parser, CPP_COMMA))
9269 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9270 while (c_parser_next_token_is (parser, CPP_COMMA))
9272 struct c_expr next;
9273 tree lhsval;
9274 location_t loc = c_parser_peek_token (parser)->location;
9275 location_t expr_loc;
9276 c_parser_consume_token (parser);
9277 expr_loc = c_parser_peek_token (parser)->location;
9278 lhsval = expr.value;
9279 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9280 lhsval = TREE_OPERAND (lhsval, 1);
9281 if (DECL_P (lhsval) || handled_component_p (lhsval))
9282 mark_exp_read (lhsval);
9283 next = c_parser_expr_no_commas (parser, NULL);
9284 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9285 expr.value = build_compound_expr (loc, expr.value, next.value);
9286 expr.original_code = COMPOUND_EXPR;
9287 expr.original_type = next.original_type;
9289 return expr;
9292 /* Parse an expression and convert functions or arrays to pointers and
9293 lvalues to rvalues. */
9295 static struct c_expr
9296 c_parser_expression_conv (c_parser *parser)
9298 struct c_expr expr;
9299 location_t loc = c_parser_peek_token (parser)->location;
9300 expr = c_parser_expression (parser);
9301 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9302 return expr;
9305 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9306 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9308 static inline void
9309 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9310 unsigned int idx)
9312 if (idx >= HOST_BITS_PER_INT)
9313 return;
9315 c_token *tok = c_parser_peek_token (parser);
9316 switch (tok->type)
9318 case CPP_NUMBER:
9319 case CPP_CHAR:
9320 case CPP_WCHAR:
9321 case CPP_CHAR16:
9322 case CPP_CHAR32:
9323 /* If a parameter is literal zero alone, remember it
9324 for -Wmemset-transposed-args warning. */
9325 if (integer_zerop (tok->value)
9326 && !TREE_OVERFLOW (tok->value)
9327 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9328 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9329 *literal_zero_mask |= 1U << idx;
9330 default:
9331 break;
9335 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9336 functions and arrays to pointers and lvalues to rvalues. If
9337 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9338 locations of function arguments into this vector.
9340 nonempty-expr-list:
9341 assignment-expression
9342 nonempty-expr-list , assignment-expression
9345 static vec<tree, va_gc> *
9346 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9347 vec<tree, va_gc> **p_orig_types,
9348 location_t *sizeof_arg_loc, tree *sizeof_arg,
9349 vec<location_t> *locations,
9350 unsigned int *literal_zero_mask)
9352 vec<tree, va_gc> *ret;
9353 vec<tree, va_gc> *orig_types;
9354 struct c_expr expr;
9355 unsigned int idx = 0;
9357 ret = make_tree_vector ();
9358 if (p_orig_types == NULL)
9359 orig_types = NULL;
9360 else
9361 orig_types = make_tree_vector ();
9363 if (literal_zero_mask)
9364 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9365 expr = c_parser_expr_no_commas (parser, NULL);
9366 if (convert_p)
9367 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9368 if (fold_p)
9369 expr.value = c_fully_fold (expr.value, false, NULL);
9370 ret->quick_push (expr.value);
9371 if (orig_types)
9372 orig_types->quick_push (expr.original_type);
9373 if (locations)
9374 locations->safe_push (expr.get_location ());
9375 if (sizeof_arg != NULL
9376 && expr.original_code == SIZEOF_EXPR)
9378 sizeof_arg[0] = c_last_sizeof_arg;
9379 sizeof_arg_loc[0] = c_last_sizeof_loc;
9381 while (c_parser_next_token_is (parser, CPP_COMMA))
9383 c_parser_consume_token (parser);
9384 if (literal_zero_mask)
9385 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9386 expr = c_parser_expr_no_commas (parser, NULL);
9387 if (convert_p)
9388 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9389 true);
9390 if (fold_p)
9391 expr.value = c_fully_fold (expr.value, false, NULL);
9392 vec_safe_push (ret, expr.value);
9393 if (orig_types)
9394 vec_safe_push (orig_types, expr.original_type);
9395 if (locations)
9396 locations->safe_push (expr.get_location ());
9397 if (++idx < 3
9398 && sizeof_arg != NULL
9399 && expr.original_code == SIZEOF_EXPR)
9401 sizeof_arg[idx] = c_last_sizeof_arg;
9402 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9405 if (orig_types)
9406 *p_orig_types = orig_types;
9407 return ret;
9410 /* Parse Objective-C-specific constructs. */
9412 /* Parse an objc-class-definition.
9414 objc-class-definition:
9415 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9416 objc-class-instance-variables[opt] objc-methodprotolist @end
9417 @implementation identifier objc-superclass[opt]
9418 objc-class-instance-variables[opt]
9419 @interface identifier ( identifier ) objc-protocol-refs[opt]
9420 objc-methodprotolist @end
9421 @interface identifier ( ) objc-protocol-refs[opt]
9422 objc-methodprotolist @end
9423 @implementation identifier ( identifier )
9425 objc-superclass:
9426 : identifier
9428 "@interface identifier (" must start "@interface identifier (
9429 identifier ) ...": objc-methodprotolist in the first production may
9430 not start with a parenthesized identifier as a declarator of a data
9431 definition with no declaration specifiers if the objc-superclass,
9432 objc-protocol-refs and objc-class-instance-variables are omitted. */
9434 static void
9435 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9437 bool iface_p;
9438 tree id1;
9439 tree superclass;
9440 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9441 iface_p = true;
9442 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9443 iface_p = false;
9444 else
9445 gcc_unreachable ();
9447 c_parser_consume_token (parser);
9448 if (c_parser_next_token_is_not (parser, CPP_NAME))
9450 c_parser_error (parser, "expected identifier");
9451 return;
9453 id1 = c_parser_peek_token (parser)->value;
9454 c_parser_consume_token (parser);
9455 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9457 /* We have a category or class extension. */
9458 tree id2;
9459 tree proto = NULL_TREE;
9460 matching_parens parens;
9461 parens.consume_open (parser);
9462 if (c_parser_next_token_is_not (parser, CPP_NAME))
9464 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9466 /* We have a class extension. */
9467 id2 = NULL_TREE;
9469 else
9471 c_parser_error (parser, "expected identifier or %<)%>");
9472 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9473 return;
9476 else
9478 id2 = c_parser_peek_token (parser)->value;
9479 c_parser_consume_token (parser);
9481 parens.skip_until_found_close (parser);
9482 if (!iface_p)
9484 objc_start_category_implementation (id1, id2);
9485 return;
9487 if (c_parser_next_token_is (parser, CPP_LESS))
9488 proto = c_parser_objc_protocol_refs (parser);
9489 objc_start_category_interface (id1, id2, proto, attributes);
9490 c_parser_objc_methodprotolist (parser);
9491 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9492 objc_finish_interface ();
9493 return;
9495 if (c_parser_next_token_is (parser, CPP_COLON))
9497 c_parser_consume_token (parser);
9498 if (c_parser_next_token_is_not (parser, CPP_NAME))
9500 c_parser_error (parser, "expected identifier");
9501 return;
9503 superclass = c_parser_peek_token (parser)->value;
9504 c_parser_consume_token (parser);
9506 else
9507 superclass = NULL_TREE;
9508 if (iface_p)
9510 tree proto = NULL_TREE;
9511 if (c_parser_next_token_is (parser, CPP_LESS))
9512 proto = c_parser_objc_protocol_refs (parser);
9513 objc_start_class_interface (id1, superclass, proto, attributes);
9515 else
9516 objc_start_class_implementation (id1, superclass);
9517 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9518 c_parser_objc_class_instance_variables (parser);
9519 if (iface_p)
9521 objc_continue_interface ();
9522 c_parser_objc_methodprotolist (parser);
9523 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9524 objc_finish_interface ();
9526 else
9528 objc_continue_implementation ();
9529 return;
9533 /* Parse objc-class-instance-variables.
9535 objc-class-instance-variables:
9536 { objc-instance-variable-decl-list[opt] }
9538 objc-instance-variable-decl-list:
9539 objc-visibility-spec
9540 objc-instance-variable-decl ;
9542 objc-instance-variable-decl-list objc-visibility-spec
9543 objc-instance-variable-decl-list objc-instance-variable-decl ;
9544 objc-instance-variable-decl-list ;
9546 objc-visibility-spec:
9547 @private
9548 @protected
9549 @public
9551 objc-instance-variable-decl:
9552 struct-declaration
9555 static void
9556 c_parser_objc_class_instance_variables (c_parser *parser)
9558 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9559 c_parser_consume_token (parser);
9560 while (c_parser_next_token_is_not (parser, CPP_EOF))
9562 tree decls;
9563 /* Parse any stray semicolon. */
9564 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9566 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9567 "extra semicolon");
9568 c_parser_consume_token (parser);
9569 continue;
9571 /* Stop if at the end of the instance variables. */
9572 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9574 c_parser_consume_token (parser);
9575 break;
9577 /* Parse any objc-visibility-spec. */
9578 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9580 c_parser_consume_token (parser);
9581 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9582 continue;
9584 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9586 c_parser_consume_token (parser);
9587 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9588 continue;
9590 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9592 c_parser_consume_token (parser);
9593 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9594 continue;
9596 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9598 c_parser_consume_token (parser);
9599 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9600 continue;
9602 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9604 c_parser_pragma (parser, pragma_external, NULL);
9605 continue;
9608 /* Parse some comma-separated declarations. */
9609 decls = c_parser_struct_declaration (parser);
9610 if (decls == NULL)
9612 /* There is a syntax error. We want to skip the offending
9613 tokens up to the next ';' (included) or '}'
9614 (excluded). */
9616 /* First, skip manually a ')' or ']'. This is because they
9617 reduce the nesting level, so c_parser_skip_until_found()
9618 wouldn't be able to skip past them. */
9619 c_token *token = c_parser_peek_token (parser);
9620 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9621 c_parser_consume_token (parser);
9623 /* Then, do the standard skipping. */
9624 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9626 /* We hopefully recovered. Start normal parsing again. */
9627 parser->error = false;
9628 continue;
9630 else
9632 /* Comma-separated instance variables are chained together
9633 in reverse order; add them one by one. */
9634 tree ivar = nreverse (decls);
9635 for (; ivar; ivar = DECL_CHAIN (ivar))
9636 objc_add_instance_variable (copy_node (ivar));
9638 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9642 /* Parse an objc-class-declaration.
9644 objc-class-declaration:
9645 @class identifier-list ;
9648 static void
9649 c_parser_objc_class_declaration (c_parser *parser)
9651 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9652 c_parser_consume_token (parser);
9653 /* Any identifiers, including those declared as type names, are OK
9654 here. */
9655 while (true)
9657 tree id;
9658 if (c_parser_next_token_is_not (parser, CPP_NAME))
9660 c_parser_error (parser, "expected identifier");
9661 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9662 parser->error = false;
9663 return;
9665 id = c_parser_peek_token (parser)->value;
9666 objc_declare_class (id);
9667 c_parser_consume_token (parser);
9668 if (c_parser_next_token_is (parser, CPP_COMMA))
9669 c_parser_consume_token (parser);
9670 else
9671 break;
9673 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9676 /* Parse an objc-alias-declaration.
9678 objc-alias-declaration:
9679 @compatibility_alias identifier identifier ;
9682 static void
9683 c_parser_objc_alias_declaration (c_parser *parser)
9685 tree id1, id2;
9686 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9687 c_parser_consume_token (parser);
9688 if (c_parser_next_token_is_not (parser, CPP_NAME))
9690 c_parser_error (parser, "expected identifier");
9691 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9692 return;
9694 id1 = c_parser_peek_token (parser)->value;
9695 c_parser_consume_token (parser);
9696 if (c_parser_next_token_is_not (parser, CPP_NAME))
9698 c_parser_error (parser, "expected identifier");
9699 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9700 return;
9702 id2 = c_parser_peek_token (parser)->value;
9703 c_parser_consume_token (parser);
9704 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9705 objc_declare_alias (id1, id2);
9708 /* Parse an objc-protocol-definition.
9710 objc-protocol-definition:
9711 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9712 @protocol identifier-list ;
9714 "@protocol identifier ;" should be resolved as "@protocol
9715 identifier-list ;": objc-methodprotolist may not start with a
9716 semicolon in the first alternative if objc-protocol-refs are
9717 omitted. */
9719 static void
9720 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9722 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9724 c_parser_consume_token (parser);
9725 if (c_parser_next_token_is_not (parser, CPP_NAME))
9727 c_parser_error (parser, "expected identifier");
9728 return;
9730 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9731 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9733 /* Any identifiers, including those declared as type names, are
9734 OK here. */
9735 while (true)
9737 tree id;
9738 if (c_parser_next_token_is_not (parser, CPP_NAME))
9740 c_parser_error (parser, "expected identifier");
9741 break;
9743 id = c_parser_peek_token (parser)->value;
9744 objc_declare_protocol (id, attributes);
9745 c_parser_consume_token (parser);
9746 if (c_parser_next_token_is (parser, CPP_COMMA))
9747 c_parser_consume_token (parser);
9748 else
9749 break;
9751 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9753 else
9755 tree id = c_parser_peek_token (parser)->value;
9756 tree proto = NULL_TREE;
9757 c_parser_consume_token (parser);
9758 if (c_parser_next_token_is (parser, CPP_LESS))
9759 proto = c_parser_objc_protocol_refs (parser);
9760 parser->objc_pq_context = true;
9761 objc_start_protocol (id, proto, attributes);
9762 c_parser_objc_methodprotolist (parser);
9763 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9764 parser->objc_pq_context = false;
9765 objc_finish_interface ();
9769 /* Parse an objc-method-type.
9771 objc-method-type:
9775 Return true if it is a class method (+) and false if it is
9776 an instance method (-).
9778 static inline bool
9779 c_parser_objc_method_type (c_parser *parser)
9781 switch (c_parser_peek_token (parser)->type)
9783 case CPP_PLUS:
9784 c_parser_consume_token (parser);
9785 return true;
9786 case CPP_MINUS:
9787 c_parser_consume_token (parser);
9788 return false;
9789 default:
9790 gcc_unreachable ();
9794 /* Parse an objc-method-definition.
9796 objc-method-definition:
9797 objc-method-type objc-method-decl ;[opt] compound-statement
9800 static void
9801 c_parser_objc_method_definition (c_parser *parser)
9803 bool is_class_method = c_parser_objc_method_type (parser);
9804 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9805 parser->objc_pq_context = true;
9806 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9807 &expr);
9808 if (decl == error_mark_node)
9809 return; /* Bail here. */
9811 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9813 c_parser_consume_token (parser);
9814 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9815 "extra semicolon in method definition specified");
9818 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9820 c_parser_error (parser, "expected %<{%>");
9821 return;
9824 parser->objc_pq_context = false;
9825 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9827 add_stmt (c_parser_compound_statement (parser));
9828 objc_finish_method_definition (current_function_decl);
9830 else
9832 /* This code is executed when we find a method definition
9833 outside of an @implementation context (or invalid for other
9834 reasons). Parse the method (to keep going) but do not emit
9835 any code.
9837 c_parser_compound_statement (parser);
9841 /* Parse an objc-methodprotolist.
9843 objc-methodprotolist:
9844 empty
9845 objc-methodprotolist objc-methodproto
9846 objc-methodprotolist declaration
9847 objc-methodprotolist ;
9848 @optional
9849 @required
9851 The declaration is a data definition, which may be missing
9852 declaration specifiers under the same rules and diagnostics as
9853 other data definitions outside functions, and the stray semicolon
9854 is diagnosed the same way as a stray semicolon outside a
9855 function. */
9857 static void
9858 c_parser_objc_methodprotolist (c_parser *parser)
9860 while (true)
9862 /* The list is terminated by @end. */
9863 switch (c_parser_peek_token (parser)->type)
9865 case CPP_SEMICOLON:
9866 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9867 "ISO C does not allow extra %<;%> outside of a function");
9868 c_parser_consume_token (parser);
9869 break;
9870 case CPP_PLUS:
9871 case CPP_MINUS:
9872 c_parser_objc_methodproto (parser);
9873 break;
9874 case CPP_PRAGMA:
9875 c_parser_pragma (parser, pragma_external, NULL);
9876 break;
9877 case CPP_EOF:
9878 return;
9879 default:
9880 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9881 return;
9882 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9883 c_parser_objc_at_property_declaration (parser);
9884 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9886 objc_set_method_opt (true);
9887 c_parser_consume_token (parser);
9889 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9891 objc_set_method_opt (false);
9892 c_parser_consume_token (parser);
9894 else
9895 c_parser_declaration_or_fndef (parser, false, false, true,
9896 false, true, NULL, vNULL);
9897 break;
9902 /* Parse an objc-methodproto.
9904 objc-methodproto:
9905 objc-method-type objc-method-decl ;
9908 static void
9909 c_parser_objc_methodproto (c_parser *parser)
9911 bool is_class_method = c_parser_objc_method_type (parser);
9912 tree decl, attributes = NULL_TREE;
9914 /* Remember protocol qualifiers in prototypes. */
9915 parser->objc_pq_context = true;
9916 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9917 NULL);
9918 /* Forget protocol qualifiers now. */
9919 parser->objc_pq_context = false;
9921 /* Do not allow the presence of attributes to hide an erroneous
9922 method implementation in the interface section. */
9923 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9925 c_parser_error (parser, "expected %<;%>");
9926 return;
9929 if (decl != error_mark_node)
9930 objc_add_method_declaration (is_class_method, decl, attributes);
9932 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9935 /* If we are at a position that method attributes may be present, check that
9936 there are not any parsed already (a syntax error) and then collect any
9937 specified at the current location. Finally, if new attributes were present,
9938 check that the next token is legal ( ';' for decls and '{' for defs). */
9940 static bool
9941 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9943 bool bad = false;
9944 if (*attributes)
9946 c_parser_error (parser,
9947 "method attributes must be specified at the end only");
9948 *attributes = NULL_TREE;
9949 bad = true;
9952 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9953 *attributes = c_parser_attributes (parser);
9955 /* If there were no attributes here, just report any earlier error. */
9956 if (*attributes == NULL_TREE || bad)
9957 return bad;
9959 /* If the attributes are followed by a ; or {, then just report any earlier
9960 error. */
9961 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9962 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9963 return bad;
9965 /* We've got attributes, but not at the end. */
9966 c_parser_error (parser,
9967 "expected %<;%> or %<{%> after method attribute definition");
9968 return true;
9971 /* Parse an objc-method-decl.
9973 objc-method-decl:
9974 ( objc-type-name ) objc-selector
9975 objc-selector
9976 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9977 objc-keyword-selector objc-optparmlist
9978 attributes
9980 objc-keyword-selector:
9981 objc-keyword-decl
9982 objc-keyword-selector objc-keyword-decl
9984 objc-keyword-decl:
9985 objc-selector : ( objc-type-name ) identifier
9986 objc-selector : identifier
9987 : ( objc-type-name ) identifier
9988 : identifier
9990 objc-optparmlist:
9991 objc-optparms objc-optellipsis
9993 objc-optparms:
9994 empty
9995 objc-opt-parms , parameter-declaration
9997 objc-optellipsis:
9998 empty
9999 , ...
10002 static tree
10003 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10004 tree *attributes, tree *expr)
10006 tree type = NULL_TREE;
10007 tree sel;
10008 tree parms = NULL_TREE;
10009 bool ellipsis = false;
10010 bool attr_err = false;
10012 *attributes = NULL_TREE;
10013 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10015 matching_parens parens;
10016 parens.consume_open (parser);
10017 type = c_parser_objc_type_name (parser);
10018 parens.skip_until_found_close (parser);
10020 sel = c_parser_objc_selector (parser);
10021 /* If there is no selector, or a colon follows, we have an
10022 objc-keyword-selector. If there is a selector, and a colon does
10023 not follow, that selector ends the objc-method-decl. */
10024 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10026 tree tsel = sel;
10027 tree list = NULL_TREE;
10028 while (true)
10030 tree atype = NULL_TREE, id, keyworddecl;
10031 tree param_attr = NULL_TREE;
10032 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10033 break;
10034 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10036 c_parser_consume_token (parser);
10037 atype = c_parser_objc_type_name (parser);
10038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10039 "expected %<)%>");
10041 /* New ObjC allows attributes on method parameters. */
10042 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10043 param_attr = c_parser_attributes (parser);
10044 if (c_parser_next_token_is_not (parser, CPP_NAME))
10046 c_parser_error (parser, "expected identifier");
10047 return error_mark_node;
10049 id = c_parser_peek_token (parser)->value;
10050 c_parser_consume_token (parser);
10051 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10052 list = chainon (list, keyworddecl);
10053 tsel = c_parser_objc_selector (parser);
10054 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10055 break;
10058 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10060 /* Parse the optional parameter list. Optional Objective-C
10061 method parameters follow the C syntax, and may include '...'
10062 to denote a variable number of arguments. */
10063 parms = make_node (TREE_LIST);
10064 while (c_parser_next_token_is (parser, CPP_COMMA))
10066 struct c_parm *parm;
10067 c_parser_consume_token (parser);
10068 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10070 ellipsis = true;
10071 c_parser_consume_token (parser);
10072 attr_err |= c_parser_objc_maybe_method_attributes
10073 (parser, attributes) ;
10074 break;
10076 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10077 if (parm == NULL)
10078 break;
10079 parms = chainon (parms,
10080 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10082 sel = list;
10084 else
10085 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10087 if (sel == NULL)
10089 c_parser_error (parser, "objective-c method declaration is expected");
10090 return error_mark_node;
10093 if (attr_err)
10094 return error_mark_node;
10096 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10099 /* Parse an objc-type-name.
10101 objc-type-name:
10102 objc-type-qualifiers[opt] type-name
10103 objc-type-qualifiers[opt]
10105 objc-type-qualifiers:
10106 objc-type-qualifier
10107 objc-type-qualifiers objc-type-qualifier
10109 objc-type-qualifier: one of
10110 in out inout bycopy byref oneway
10113 static tree
10114 c_parser_objc_type_name (c_parser *parser)
10116 tree quals = NULL_TREE;
10117 struct c_type_name *type_name = NULL;
10118 tree type = NULL_TREE;
10119 while (true)
10121 c_token *token = c_parser_peek_token (parser);
10122 if (token->type == CPP_KEYWORD
10123 && (token->keyword == RID_IN
10124 || token->keyword == RID_OUT
10125 || token->keyword == RID_INOUT
10126 || token->keyword == RID_BYCOPY
10127 || token->keyword == RID_BYREF
10128 || token->keyword == RID_ONEWAY))
10130 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10131 c_parser_consume_token (parser);
10133 else
10134 break;
10136 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10137 type_name = c_parser_type_name (parser);
10138 if (type_name)
10139 type = groktypename (type_name, NULL, NULL);
10141 /* If the type is unknown, and error has already been produced and
10142 we need to recover from the error. In that case, use NULL_TREE
10143 for the type, as if no type had been specified; this will use the
10144 default type ('id') which is good for error recovery. */
10145 if (type == error_mark_node)
10146 type = NULL_TREE;
10148 return build_tree_list (quals, type);
10151 /* Parse objc-protocol-refs.
10153 objc-protocol-refs:
10154 < identifier-list >
10157 static tree
10158 c_parser_objc_protocol_refs (c_parser *parser)
10160 tree list = NULL_TREE;
10161 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10162 c_parser_consume_token (parser);
10163 /* Any identifiers, including those declared as type names, are OK
10164 here. */
10165 while (true)
10167 tree id;
10168 if (c_parser_next_token_is_not (parser, CPP_NAME))
10170 c_parser_error (parser, "expected identifier");
10171 break;
10173 id = c_parser_peek_token (parser)->value;
10174 list = chainon (list, build_tree_list (NULL_TREE, id));
10175 c_parser_consume_token (parser);
10176 if (c_parser_next_token_is (parser, CPP_COMMA))
10177 c_parser_consume_token (parser);
10178 else
10179 break;
10181 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10182 return list;
10185 /* Parse an objc-try-catch-finally-statement.
10187 objc-try-catch-finally-statement:
10188 @try compound-statement objc-catch-list[opt]
10189 @try compound-statement objc-catch-list[opt] @finally compound-statement
10191 objc-catch-list:
10192 @catch ( objc-catch-parameter-declaration ) compound-statement
10193 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10195 objc-catch-parameter-declaration:
10196 parameter-declaration
10197 '...'
10199 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10201 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10202 for C++. Keep them in sync. */
10204 static void
10205 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10207 location_t location;
10208 tree stmt;
10210 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10211 c_parser_consume_token (parser);
10212 location = c_parser_peek_token (parser)->location;
10213 objc_maybe_warn_exceptions (location);
10214 stmt = c_parser_compound_statement (parser);
10215 objc_begin_try_stmt (location, stmt);
10217 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10219 struct c_parm *parm;
10220 tree parameter_declaration = error_mark_node;
10221 bool seen_open_paren = false;
10223 c_parser_consume_token (parser);
10224 matching_parens parens;
10225 if (!parens.require_open (parser))
10226 seen_open_paren = true;
10227 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10229 /* We have "@catch (...)" (where the '...' are literally
10230 what is in the code). Skip the '...'.
10231 parameter_declaration is set to NULL_TREE, and
10232 objc_being_catch_clauses() knows that that means
10233 '...'. */
10234 c_parser_consume_token (parser);
10235 parameter_declaration = NULL_TREE;
10237 else
10239 /* We have "@catch (NSException *exception)" or something
10240 like that. Parse the parameter declaration. */
10241 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10242 if (parm == NULL)
10243 parameter_declaration = error_mark_node;
10244 else
10245 parameter_declaration = grokparm (parm, NULL);
10247 if (seen_open_paren)
10248 parens.require_close (parser);
10249 else
10251 /* If there was no open parenthesis, we are recovering from
10252 an error, and we are trying to figure out what mistake
10253 the user has made. */
10255 /* If there is an immediate closing parenthesis, the user
10256 probably forgot the opening one (ie, they typed "@catch
10257 NSException *e)". Parse the closing parenthesis and keep
10258 going. */
10259 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10260 c_parser_consume_token (parser);
10262 /* If these is no immediate closing parenthesis, the user
10263 probably doesn't know that parenthesis are required at
10264 all (ie, they typed "@catch NSException *e"). So, just
10265 forget about the closing parenthesis and keep going. */
10267 objc_begin_catch_clause (parameter_declaration);
10268 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10269 c_parser_compound_statement_nostart (parser);
10270 objc_finish_catch_clause ();
10272 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10274 c_parser_consume_token (parser);
10275 location = c_parser_peek_token (parser)->location;
10276 stmt = c_parser_compound_statement (parser);
10277 objc_build_finally_clause (location, stmt);
10279 objc_finish_try_stmt ();
10282 /* Parse an objc-synchronized-statement.
10284 objc-synchronized-statement:
10285 @synchronized ( expression ) compound-statement
10288 static void
10289 c_parser_objc_synchronized_statement (c_parser *parser)
10291 location_t loc;
10292 tree expr, stmt;
10293 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10294 c_parser_consume_token (parser);
10295 loc = c_parser_peek_token (parser)->location;
10296 objc_maybe_warn_exceptions (loc);
10297 matching_parens parens;
10298 if (parens.require_open (parser))
10300 struct c_expr ce = c_parser_expression (parser);
10301 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10302 expr = ce.value;
10303 expr = c_fully_fold (expr, false, NULL);
10304 parens.skip_until_found_close (parser);
10306 else
10307 expr = error_mark_node;
10308 stmt = c_parser_compound_statement (parser);
10309 objc_build_synchronized (loc, expr, stmt);
10312 /* Parse an objc-selector; return NULL_TREE without an error if the
10313 next token is not an objc-selector.
10315 objc-selector:
10316 identifier
10317 one of
10318 enum struct union if else while do for switch case default
10319 break continue return goto asm sizeof typeof __alignof
10320 unsigned long const short volatile signed restrict _Complex
10321 in out inout bycopy byref oneway int char float double void _Bool
10322 _Atomic
10324 ??? Why this selection of keywords but not, for example, storage
10325 class specifiers? */
10327 static tree
10328 c_parser_objc_selector (c_parser *parser)
10330 c_token *token = c_parser_peek_token (parser);
10331 tree value = token->value;
10332 if (token->type == CPP_NAME)
10334 c_parser_consume_token (parser);
10335 return value;
10337 if (token->type != CPP_KEYWORD)
10338 return NULL_TREE;
10339 switch (token->keyword)
10341 case RID_ENUM:
10342 case RID_STRUCT:
10343 case RID_UNION:
10344 case RID_IF:
10345 case RID_ELSE:
10346 case RID_WHILE:
10347 case RID_DO:
10348 case RID_FOR:
10349 case RID_SWITCH:
10350 case RID_CASE:
10351 case RID_DEFAULT:
10352 case RID_BREAK:
10353 case RID_CONTINUE:
10354 case RID_RETURN:
10355 case RID_GOTO:
10356 case RID_ASM:
10357 case RID_SIZEOF:
10358 case RID_TYPEOF:
10359 case RID_ALIGNOF:
10360 case RID_UNSIGNED:
10361 case RID_LONG:
10362 case RID_CONST:
10363 case RID_SHORT:
10364 case RID_VOLATILE:
10365 case RID_SIGNED:
10366 case RID_RESTRICT:
10367 case RID_COMPLEX:
10368 case RID_IN:
10369 case RID_OUT:
10370 case RID_INOUT:
10371 case RID_BYCOPY:
10372 case RID_BYREF:
10373 case RID_ONEWAY:
10374 case RID_INT:
10375 case RID_CHAR:
10376 case RID_FLOAT:
10377 case RID_DOUBLE:
10378 CASE_RID_FLOATN_NX:
10379 case RID_VOID:
10380 case RID_BOOL:
10381 case RID_ATOMIC:
10382 case RID_AUTO_TYPE:
10383 case RID_INT_N_0:
10384 case RID_INT_N_1:
10385 case RID_INT_N_2:
10386 case RID_INT_N_3:
10387 c_parser_consume_token (parser);
10388 return value;
10389 default:
10390 return NULL_TREE;
10394 /* Parse an objc-selector-arg.
10396 objc-selector-arg:
10397 objc-selector
10398 objc-keywordname-list
10400 objc-keywordname-list:
10401 objc-keywordname
10402 objc-keywordname-list objc-keywordname
10404 objc-keywordname:
10405 objc-selector :
10409 static tree
10410 c_parser_objc_selector_arg (c_parser *parser)
10412 tree sel = c_parser_objc_selector (parser);
10413 tree list = NULL_TREE;
10414 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10415 return sel;
10416 while (true)
10418 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10419 return list;
10420 list = chainon (list, build_tree_list (sel, NULL_TREE));
10421 sel = c_parser_objc_selector (parser);
10422 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10423 break;
10425 return list;
10428 /* Parse an objc-receiver.
10430 objc-receiver:
10431 expression
10432 class-name
10433 type-name
10436 static tree
10437 c_parser_objc_receiver (c_parser *parser)
10439 location_t loc = c_parser_peek_token (parser)->location;
10441 if (c_parser_peek_token (parser)->type == CPP_NAME
10442 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10443 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10445 tree id = c_parser_peek_token (parser)->value;
10446 c_parser_consume_token (parser);
10447 return objc_get_class_reference (id);
10449 struct c_expr ce = c_parser_expression (parser);
10450 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10451 return c_fully_fold (ce.value, false, NULL);
10454 /* Parse objc-message-args.
10456 objc-message-args:
10457 objc-selector
10458 objc-keywordarg-list
10460 objc-keywordarg-list:
10461 objc-keywordarg
10462 objc-keywordarg-list objc-keywordarg
10464 objc-keywordarg:
10465 objc-selector : objc-keywordexpr
10466 : objc-keywordexpr
10469 static tree
10470 c_parser_objc_message_args (c_parser *parser)
10472 tree sel = c_parser_objc_selector (parser);
10473 tree list = NULL_TREE;
10474 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10475 return sel;
10476 while (true)
10478 tree keywordexpr;
10479 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10480 return error_mark_node;
10481 keywordexpr = c_parser_objc_keywordexpr (parser);
10482 list = chainon (list, build_tree_list (sel, keywordexpr));
10483 sel = c_parser_objc_selector (parser);
10484 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10485 break;
10487 return list;
10490 /* Parse an objc-keywordexpr.
10492 objc-keywordexpr:
10493 nonempty-expr-list
10496 static tree
10497 c_parser_objc_keywordexpr (c_parser *parser)
10499 tree ret;
10500 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10501 NULL, NULL, NULL, NULL);
10502 if (vec_safe_length (expr_list) == 1)
10504 /* Just return the expression, remove a level of
10505 indirection. */
10506 ret = (*expr_list)[0];
10508 else
10510 /* We have a comma expression, we will collapse later. */
10511 ret = build_tree_list_vec (expr_list);
10513 release_tree_vector (expr_list);
10514 return ret;
10517 /* A check, needed in several places, that ObjC interface, implementation or
10518 method definitions are not prefixed by incorrect items. */
10519 static bool
10520 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10521 struct c_declspecs *specs)
10523 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10524 || specs->typespec_kind != ctsk_none)
10526 c_parser_error (parser,
10527 "no type or storage class may be specified here,");
10528 c_parser_skip_to_end_of_block_or_statement (parser);
10529 return true;
10531 return false;
10534 /* Parse an Objective-C @property declaration. The syntax is:
10536 objc-property-declaration:
10537 '@property' objc-property-attributes[opt] struct-declaration ;
10539 objc-property-attributes:
10540 '(' objc-property-attribute-list ')'
10542 objc-property-attribute-list:
10543 objc-property-attribute
10544 objc-property-attribute-list, objc-property-attribute
10546 objc-property-attribute
10547 'getter' = identifier
10548 'setter' = identifier
10549 'readonly'
10550 'readwrite'
10551 'assign'
10552 'retain'
10553 'copy'
10554 'nonatomic'
10556 For example:
10557 @property NSString *name;
10558 @property (readonly) id object;
10559 @property (retain, nonatomic, getter=getTheName) id name;
10560 @property int a, b, c;
10562 PS: This function is identical to cp_parser_objc_at_propery_declaration
10563 for C++. Keep them in sync. */
10564 static void
10565 c_parser_objc_at_property_declaration (c_parser *parser)
10567 /* The following variables hold the attributes of the properties as
10568 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10569 seen. When we see an attribute, we set them to 'true' (if they
10570 are boolean properties) or to the identifier (if they have an
10571 argument, ie, for getter and setter). Note that here we only
10572 parse the list of attributes, check the syntax and accumulate the
10573 attributes that we find. objc_add_property_declaration() will
10574 then process the information. */
10575 bool property_assign = false;
10576 bool property_copy = false;
10577 tree property_getter_ident = NULL_TREE;
10578 bool property_nonatomic = false;
10579 bool property_readonly = false;
10580 bool property_readwrite = false;
10581 bool property_retain = false;
10582 tree property_setter_ident = NULL_TREE;
10584 /* 'properties' is the list of properties that we read. Usually a
10585 single one, but maybe more (eg, in "@property int a, b, c;" there
10586 are three). */
10587 tree properties;
10588 location_t loc;
10590 loc = c_parser_peek_token (parser)->location;
10591 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10593 c_parser_consume_token (parser); /* Eat '@property'. */
10595 /* Parse the optional attribute list... */
10596 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10598 matching_parens parens;
10600 /* Eat the '(' */
10601 parens.consume_open (parser);
10603 /* Property attribute keywords are valid now. */
10604 parser->objc_property_attr_context = true;
10606 while (true)
10608 bool syntax_error = false;
10609 c_token *token = c_parser_peek_token (parser);
10610 enum rid keyword;
10612 if (token->type != CPP_KEYWORD)
10614 if (token->type == CPP_CLOSE_PAREN)
10615 c_parser_error (parser, "expected identifier");
10616 else
10618 c_parser_consume_token (parser);
10619 c_parser_error (parser, "unknown property attribute");
10621 break;
10623 keyword = token->keyword;
10624 c_parser_consume_token (parser);
10625 switch (keyword)
10627 case RID_ASSIGN: property_assign = true; break;
10628 case RID_COPY: property_copy = true; break;
10629 case RID_NONATOMIC: property_nonatomic = true; break;
10630 case RID_READONLY: property_readonly = true; break;
10631 case RID_READWRITE: property_readwrite = true; break;
10632 case RID_RETAIN: property_retain = true; break;
10634 case RID_GETTER:
10635 case RID_SETTER:
10636 if (c_parser_next_token_is_not (parser, CPP_EQ))
10638 if (keyword == RID_GETTER)
10639 c_parser_error (parser,
10640 "missing %<=%> (after %<getter%> attribute)");
10641 else
10642 c_parser_error (parser,
10643 "missing %<=%> (after %<setter%> attribute)");
10644 syntax_error = true;
10645 break;
10647 c_parser_consume_token (parser); /* eat the = */
10648 if (c_parser_next_token_is_not (parser, CPP_NAME))
10650 c_parser_error (parser, "expected identifier");
10651 syntax_error = true;
10652 break;
10654 if (keyword == RID_SETTER)
10656 if (property_setter_ident != NULL_TREE)
10657 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10658 else
10659 property_setter_ident = c_parser_peek_token (parser)->value;
10660 c_parser_consume_token (parser);
10661 if (c_parser_next_token_is_not (parser, CPP_COLON))
10662 c_parser_error (parser, "setter name must terminate with %<:%>");
10663 else
10664 c_parser_consume_token (parser);
10666 else
10668 if (property_getter_ident != NULL_TREE)
10669 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10670 else
10671 property_getter_ident = c_parser_peek_token (parser)->value;
10672 c_parser_consume_token (parser);
10674 break;
10675 default:
10676 c_parser_error (parser, "unknown property attribute");
10677 syntax_error = true;
10678 break;
10681 if (syntax_error)
10682 break;
10684 if (c_parser_next_token_is (parser, CPP_COMMA))
10685 c_parser_consume_token (parser);
10686 else
10687 break;
10689 parser->objc_property_attr_context = false;
10690 parens.skip_until_found_close (parser);
10692 /* ... and the property declaration(s). */
10693 properties = c_parser_struct_declaration (parser);
10695 if (properties == error_mark_node)
10697 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10698 parser->error = false;
10699 return;
10702 if (properties == NULL_TREE)
10703 c_parser_error (parser, "expected identifier");
10704 else
10706 /* Comma-separated properties are chained together in
10707 reverse order; add them one by one. */
10708 properties = nreverse (properties);
10710 for (; properties; properties = TREE_CHAIN (properties))
10711 objc_add_property_declaration (loc, copy_node (properties),
10712 property_readonly, property_readwrite,
10713 property_assign, property_retain,
10714 property_copy, property_nonatomic,
10715 property_getter_ident, property_setter_ident);
10718 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10719 parser->error = false;
10722 /* Parse an Objective-C @synthesize declaration. The syntax is:
10724 objc-synthesize-declaration:
10725 @synthesize objc-synthesize-identifier-list ;
10727 objc-synthesize-identifier-list:
10728 objc-synthesize-identifier
10729 objc-synthesize-identifier-list, objc-synthesize-identifier
10731 objc-synthesize-identifier
10732 identifier
10733 identifier = identifier
10735 For example:
10736 @synthesize MyProperty;
10737 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10739 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10740 for C++. Keep them in sync.
10742 static void
10743 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10745 tree list = NULL_TREE;
10746 location_t loc;
10747 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10748 loc = c_parser_peek_token (parser)->location;
10750 c_parser_consume_token (parser);
10751 while (true)
10753 tree property, ivar;
10754 if (c_parser_next_token_is_not (parser, CPP_NAME))
10756 c_parser_error (parser, "expected identifier");
10757 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10758 /* Once we find the semicolon, we can resume normal parsing.
10759 We have to reset parser->error manually because
10760 c_parser_skip_until_found() won't reset it for us if the
10761 next token is precisely a semicolon. */
10762 parser->error = false;
10763 return;
10765 property = c_parser_peek_token (parser)->value;
10766 c_parser_consume_token (parser);
10767 if (c_parser_next_token_is (parser, CPP_EQ))
10769 c_parser_consume_token (parser);
10770 if (c_parser_next_token_is_not (parser, CPP_NAME))
10772 c_parser_error (parser, "expected identifier");
10773 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10774 parser->error = false;
10775 return;
10777 ivar = c_parser_peek_token (parser)->value;
10778 c_parser_consume_token (parser);
10780 else
10781 ivar = NULL_TREE;
10782 list = chainon (list, build_tree_list (ivar, property));
10783 if (c_parser_next_token_is (parser, CPP_COMMA))
10784 c_parser_consume_token (parser);
10785 else
10786 break;
10788 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10789 objc_add_synthesize_declaration (loc, list);
10792 /* Parse an Objective-C @dynamic declaration. The syntax is:
10794 objc-dynamic-declaration:
10795 @dynamic identifier-list ;
10797 For example:
10798 @dynamic MyProperty;
10799 @dynamic MyProperty, AnotherProperty;
10801 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10802 for C++. Keep them in sync.
10804 static void
10805 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10807 tree list = NULL_TREE;
10808 location_t loc;
10809 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10810 loc = c_parser_peek_token (parser)->location;
10812 c_parser_consume_token (parser);
10813 while (true)
10815 tree property;
10816 if (c_parser_next_token_is_not (parser, CPP_NAME))
10818 c_parser_error (parser, "expected identifier");
10819 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10820 parser->error = false;
10821 return;
10823 property = c_parser_peek_token (parser)->value;
10824 list = chainon (list, build_tree_list (NULL_TREE, property));
10825 c_parser_consume_token (parser);
10826 if (c_parser_next_token_is (parser, CPP_COMMA))
10827 c_parser_consume_token (parser);
10828 else
10829 break;
10831 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10832 objc_add_dynamic_declaration (loc, list);
10836 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10837 should be considered, statements. ALLOW_STMT is true if we're within
10838 the context of a function and such pragmas are to be allowed. Returns
10839 true if we actually parsed such a pragma. */
10841 static bool
10842 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10844 unsigned int id;
10845 const char *construct = NULL;
10847 id = c_parser_peek_token (parser)->pragma_kind;
10848 gcc_assert (id != PRAGMA_NONE);
10850 switch (id)
10852 case PRAGMA_OACC_DECLARE:
10853 c_parser_oacc_declare (parser);
10854 return false;
10856 case PRAGMA_OACC_ENTER_DATA:
10857 if (context != pragma_compound)
10859 construct = "acc enter data";
10860 in_compound:
10861 if (context == pragma_stmt)
10863 error_at (c_parser_peek_token (parser)->location,
10864 "%<#pragma %s%> may only be used in compound "
10865 "statements", construct);
10866 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10867 return false;
10869 goto bad_stmt;
10871 c_parser_oacc_enter_exit_data (parser, true);
10872 return false;
10874 case PRAGMA_OACC_EXIT_DATA:
10875 if (context != pragma_compound)
10877 construct = "acc exit data";
10878 goto in_compound;
10880 c_parser_oacc_enter_exit_data (parser, false);
10881 return false;
10883 case PRAGMA_OACC_ROUTINE:
10884 if (context != pragma_external)
10886 error_at (c_parser_peek_token (parser)->location,
10887 "%<#pragma acc routine%> must be at file scope");
10888 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10889 return false;
10891 c_parser_oacc_routine (parser, context);
10892 return false;
10894 case PRAGMA_OACC_UPDATE:
10895 if (context != pragma_compound)
10897 construct = "acc update";
10898 goto in_compound;
10900 c_parser_oacc_update (parser);
10901 return false;
10903 case PRAGMA_OMP_BARRIER:
10904 if (context != pragma_compound)
10906 construct = "omp barrier";
10907 goto in_compound;
10909 c_parser_omp_barrier (parser);
10910 return false;
10912 case PRAGMA_OMP_FLUSH:
10913 if (context != pragma_compound)
10915 construct = "omp flush";
10916 goto in_compound;
10918 c_parser_omp_flush (parser);
10919 return false;
10921 case PRAGMA_OMP_TASKWAIT:
10922 if (context != pragma_compound)
10924 construct = "omp taskwait";
10925 goto in_compound;
10927 c_parser_omp_taskwait (parser);
10928 return false;
10930 case PRAGMA_OMP_TASKYIELD:
10931 if (context != pragma_compound)
10933 construct = "omp taskyield";
10934 goto in_compound;
10936 c_parser_omp_taskyield (parser);
10937 return false;
10939 case PRAGMA_OMP_CANCEL:
10940 if (context != pragma_compound)
10942 construct = "omp cancel";
10943 goto in_compound;
10945 c_parser_omp_cancel (parser);
10946 return false;
10948 case PRAGMA_OMP_CANCELLATION_POINT:
10949 c_parser_omp_cancellation_point (parser, context);
10950 return false;
10952 case PRAGMA_OMP_THREADPRIVATE:
10953 c_parser_omp_threadprivate (parser);
10954 return false;
10956 case PRAGMA_OMP_TARGET:
10957 return c_parser_omp_target (parser, context, if_p);
10959 case PRAGMA_OMP_END_DECLARE_TARGET:
10960 c_parser_omp_end_declare_target (parser);
10961 return false;
10963 case PRAGMA_OMP_SECTION:
10964 error_at (c_parser_peek_token (parser)->location,
10965 "%<#pragma omp section%> may only be used in "
10966 "%<#pragma omp sections%> construct");
10967 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10968 return false;
10970 case PRAGMA_OMP_DECLARE:
10971 c_parser_omp_declare (parser, context);
10972 return false;
10974 case PRAGMA_OMP_ORDERED:
10975 return c_parser_omp_ordered (parser, context, if_p);
10977 case PRAGMA_IVDEP:
10978 c_parser_consume_pragma (parser);
10979 c_parser_skip_to_pragma_eol (parser);
10980 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10981 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10982 && !c_parser_next_token_is_keyword (parser, RID_DO))
10984 c_parser_error (parser, "for, while or do statement expected");
10985 return false;
10987 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10988 c_parser_for_statement (parser, true, if_p);
10989 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10990 c_parser_while_statement (parser, true, if_p);
10991 else
10992 c_parser_do_statement (parser, true);
10993 return false;
10995 case PRAGMA_GCC_PCH_PREPROCESS:
10996 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10997 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10998 return false;
11000 case PRAGMA_OACC_WAIT:
11001 if (context != pragma_compound)
11003 construct = "acc wait";
11004 goto in_compound;
11006 /* FALL THROUGH. */
11008 default:
11009 if (id < PRAGMA_FIRST_EXTERNAL)
11011 if (context != pragma_stmt && context != pragma_compound)
11013 bad_stmt:
11014 c_parser_error (parser, "expected declaration specifiers");
11015 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11016 return false;
11018 c_parser_omp_construct (parser, if_p);
11019 return true;
11021 break;
11024 c_parser_consume_pragma (parser);
11025 c_invoke_pragma_handler (id);
11027 /* Skip to EOL, but suppress any error message. Those will have been
11028 generated by the handler routine through calling error, as opposed
11029 to calling c_parser_error. */
11030 parser->error = true;
11031 c_parser_skip_to_pragma_eol (parser);
11033 return false;
11036 /* The interface the pragma parsers have to the lexer. */
11038 enum cpp_ttype
11039 pragma_lex (tree *value, location_t *loc)
11041 c_token *tok = c_parser_peek_token (the_parser);
11042 enum cpp_ttype ret = tok->type;
11044 *value = tok->value;
11045 if (loc)
11046 *loc = tok->location;
11048 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11049 ret = CPP_EOF;
11050 else
11052 if (ret == CPP_KEYWORD)
11053 ret = CPP_NAME;
11054 c_parser_consume_token (the_parser);
11057 return ret;
11060 static void
11061 c_parser_pragma_pch_preprocess (c_parser *parser)
11063 tree name = NULL;
11065 c_parser_consume_pragma (parser);
11066 if (c_parser_next_token_is (parser, CPP_STRING))
11068 name = c_parser_peek_token (parser)->value;
11069 c_parser_consume_token (parser);
11071 else
11072 c_parser_error (parser, "expected string literal");
11073 c_parser_skip_to_pragma_eol (parser);
11075 if (name)
11076 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11079 /* OpenACC and OpenMP parsing routines. */
11081 /* Returns name of the next clause.
11082 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11083 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11084 returned and the token is consumed. */
11086 static pragma_omp_clause
11087 c_parser_omp_clause_name (c_parser *parser)
11089 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11091 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11092 result = PRAGMA_OACC_CLAUSE_AUTO;
11093 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11094 result = PRAGMA_OMP_CLAUSE_IF;
11095 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11096 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11097 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11098 result = PRAGMA_OMP_CLAUSE_FOR;
11099 else if (c_parser_next_token_is (parser, CPP_NAME))
11101 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11103 switch (p[0])
11105 case 'a':
11106 if (!strcmp ("aligned", p))
11107 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11108 else if (!strcmp ("async", p))
11109 result = PRAGMA_OACC_CLAUSE_ASYNC;
11110 break;
11111 case 'c':
11112 if (!strcmp ("collapse", p))
11113 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11114 else if (!strcmp ("copy", p))
11115 result = PRAGMA_OACC_CLAUSE_COPY;
11116 else if (!strcmp ("copyin", p))
11117 result = PRAGMA_OMP_CLAUSE_COPYIN;
11118 else if (!strcmp ("copyout", p))
11119 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11120 else if (!strcmp ("copyprivate", p))
11121 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11122 else if (!strcmp ("create", p))
11123 result = PRAGMA_OACC_CLAUSE_CREATE;
11124 break;
11125 case 'd':
11126 if (!strcmp ("defaultmap", p))
11127 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11128 else if (!strcmp ("delete", p))
11129 result = PRAGMA_OACC_CLAUSE_DELETE;
11130 else if (!strcmp ("depend", p))
11131 result = PRAGMA_OMP_CLAUSE_DEPEND;
11132 else if (!strcmp ("device", p))
11133 result = PRAGMA_OMP_CLAUSE_DEVICE;
11134 else if (!strcmp ("deviceptr", p))
11135 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11136 else if (!strcmp ("device_resident", p))
11137 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11138 else if (!strcmp ("dist_schedule", p))
11139 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11140 break;
11141 case 'f':
11142 if (!strcmp ("final", p))
11143 result = PRAGMA_OMP_CLAUSE_FINAL;
11144 else if (!strcmp ("firstprivate", p))
11145 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11146 else if (!strcmp ("from", p))
11147 result = PRAGMA_OMP_CLAUSE_FROM;
11148 break;
11149 case 'g':
11150 if (!strcmp ("gang", p))
11151 result = PRAGMA_OACC_CLAUSE_GANG;
11152 else if (!strcmp ("grainsize", p))
11153 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11154 break;
11155 case 'h':
11156 if (!strcmp ("hint", p))
11157 result = PRAGMA_OMP_CLAUSE_HINT;
11158 else if (!strcmp ("host", p))
11159 result = PRAGMA_OACC_CLAUSE_HOST;
11160 break;
11161 case 'i':
11162 if (!strcmp ("inbranch", p))
11163 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11164 else if (!strcmp ("independent", p))
11165 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11166 else if (!strcmp ("is_device_ptr", p))
11167 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11168 break;
11169 case 'l':
11170 if (!strcmp ("lastprivate", p))
11171 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11172 else if (!strcmp ("linear", p))
11173 result = PRAGMA_OMP_CLAUSE_LINEAR;
11174 else if (!strcmp ("link", p))
11175 result = PRAGMA_OMP_CLAUSE_LINK;
11176 break;
11177 case 'm':
11178 if (!strcmp ("map", p))
11179 result = PRAGMA_OMP_CLAUSE_MAP;
11180 else if (!strcmp ("mergeable", p))
11181 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11182 break;
11183 case 'n':
11184 if (!strcmp ("nogroup", p))
11185 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11186 else if (!strcmp ("notinbranch", p))
11187 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11188 else if (!strcmp ("nowait", p))
11189 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11190 else if (!strcmp ("num_gangs", p))
11191 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11192 else if (!strcmp ("num_tasks", p))
11193 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11194 else if (!strcmp ("num_teams", p))
11195 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11196 else if (!strcmp ("num_threads", p))
11197 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11198 else if (!strcmp ("num_workers", p))
11199 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11200 break;
11201 case 'o':
11202 if (!strcmp ("ordered", p))
11203 result = PRAGMA_OMP_CLAUSE_ORDERED;
11204 break;
11205 case 'p':
11206 if (!strcmp ("parallel", p))
11207 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11208 else if (!strcmp ("present", p))
11209 result = PRAGMA_OACC_CLAUSE_PRESENT;
11210 else if (!strcmp ("present_or_copy", p)
11211 || !strcmp ("pcopy", p))
11212 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
11213 else if (!strcmp ("present_or_copyin", p)
11214 || !strcmp ("pcopyin", p))
11215 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
11216 else if (!strcmp ("present_or_copyout", p)
11217 || !strcmp ("pcopyout", p))
11218 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
11219 else if (!strcmp ("present_or_create", p)
11220 || !strcmp ("pcreate", p))
11221 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
11222 else if (!strcmp ("priority", p))
11223 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11224 else if (!strcmp ("private", p))
11225 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11226 else if (!strcmp ("proc_bind", p))
11227 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11228 break;
11229 case 'r':
11230 if (!strcmp ("reduction", p))
11231 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11232 break;
11233 case 's':
11234 if (!strcmp ("safelen", p))
11235 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11236 else if (!strcmp ("schedule", p))
11237 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11238 else if (!strcmp ("sections", p))
11239 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11240 else if (!strcmp ("seq", p))
11241 result = PRAGMA_OACC_CLAUSE_SEQ;
11242 else if (!strcmp ("shared", p))
11243 result = PRAGMA_OMP_CLAUSE_SHARED;
11244 else if (!strcmp ("simd", p))
11245 result = PRAGMA_OMP_CLAUSE_SIMD;
11246 else if (!strcmp ("simdlen", p))
11247 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11248 else if (!strcmp ("self", p))
11249 result = PRAGMA_OACC_CLAUSE_SELF;
11250 break;
11251 case 't':
11252 if (!strcmp ("taskgroup", p))
11253 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11254 else if (!strcmp ("thread_limit", p))
11255 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11256 else if (!strcmp ("threads", p))
11257 result = PRAGMA_OMP_CLAUSE_THREADS;
11258 else if (!strcmp ("tile", p))
11259 result = PRAGMA_OACC_CLAUSE_TILE;
11260 else if (!strcmp ("to", p))
11261 result = PRAGMA_OMP_CLAUSE_TO;
11262 break;
11263 case 'u':
11264 if (!strcmp ("uniform", p))
11265 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11266 else if (!strcmp ("untied", p))
11267 result = PRAGMA_OMP_CLAUSE_UNTIED;
11268 else if (!strcmp ("use_device", p))
11269 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11270 else if (!strcmp ("use_device_ptr", p))
11271 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11272 break;
11273 case 'v':
11274 if (!strcmp ("vector", p))
11275 result = PRAGMA_OACC_CLAUSE_VECTOR;
11276 else if (!strcmp ("vector_length", p))
11277 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11278 break;
11279 case 'w':
11280 if (!strcmp ("wait", p))
11281 result = PRAGMA_OACC_CLAUSE_WAIT;
11282 else if (!strcmp ("worker", p))
11283 result = PRAGMA_OACC_CLAUSE_WORKER;
11284 break;
11288 if (result != PRAGMA_OMP_CLAUSE_NONE)
11289 c_parser_consume_token (parser);
11291 return result;
11294 /* Validate that a clause of the given type does not already exist. */
11296 static void
11297 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11298 const char *name)
11300 tree c;
11302 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11303 if (OMP_CLAUSE_CODE (c) == code)
11305 location_t loc = OMP_CLAUSE_LOCATION (c);
11306 error_at (loc, "too many %qs clauses", name);
11307 break;
11311 /* OpenACC 2.0
11312 Parse wait clause or wait directive parameters. */
11314 static tree
11315 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11317 vec<tree, va_gc> *args;
11318 tree t, args_tree;
11320 matching_parens parens;
11321 if (!parens.require_open (parser))
11322 return list;
11324 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11326 if (args->length () == 0)
11328 c_parser_error (parser, "expected integer expression before ')'");
11329 release_tree_vector (args);
11330 return list;
11333 args_tree = build_tree_list_vec (args);
11335 for (t = args_tree; t; t = TREE_CHAIN (t))
11337 tree targ = TREE_VALUE (t);
11339 if (targ != error_mark_node)
11341 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11343 c_parser_error (parser, "expression must be integral");
11344 targ = error_mark_node;
11346 else
11348 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11350 OMP_CLAUSE_DECL (c) = targ;
11351 OMP_CLAUSE_CHAIN (c) = list;
11352 list = c;
11357 release_tree_vector (args);
11358 parens.require_close (parser);
11359 return list;
11362 /* OpenACC 2.0, OpenMP 2.5:
11363 variable-list:
11364 identifier
11365 variable-list , identifier
11367 If KIND is nonzero, create the appropriate node and install the
11368 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11369 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11371 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11372 return the list created. */
11374 static tree
11375 c_parser_omp_variable_list (c_parser *parser,
11376 location_t clause_loc,
11377 enum omp_clause_code kind, tree list)
11379 if (c_parser_next_token_is_not (parser, CPP_NAME)
11380 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11381 c_parser_error (parser, "expected identifier");
11383 while (c_parser_next_token_is (parser, CPP_NAME)
11384 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11386 tree t = lookup_name (c_parser_peek_token (parser)->value);
11388 if (t == NULL_TREE)
11390 undeclared_variable (c_parser_peek_token (parser)->location,
11391 c_parser_peek_token (parser)->value);
11392 t = error_mark_node;
11395 c_parser_consume_token (parser);
11397 if (t == error_mark_node)
11399 else if (kind != 0)
11401 switch (kind)
11403 case OMP_CLAUSE__CACHE_:
11404 /* The OpenACC cache directive explicitly only allows "array
11405 elements or subarrays". */
11406 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11408 c_parser_error (parser, "expected %<[%>");
11409 t = error_mark_node;
11410 break;
11412 /* FALLTHROUGH */
11413 case OMP_CLAUSE_MAP:
11414 case OMP_CLAUSE_FROM:
11415 case OMP_CLAUSE_TO:
11416 while (c_parser_next_token_is (parser, CPP_DOT))
11418 location_t op_loc = c_parser_peek_token (parser)->location;
11419 c_parser_consume_token (parser);
11420 if (!c_parser_next_token_is (parser, CPP_NAME))
11422 c_parser_error (parser, "expected identifier");
11423 t = error_mark_node;
11424 break;
11427 c_token *comp_tok = c_parser_peek_token (parser);
11428 tree ident = comp_tok->value;
11429 location_t comp_loc = comp_tok->location;
11430 c_parser_consume_token (parser);
11431 t = build_component_ref (op_loc, t, ident, comp_loc);
11433 /* FALLTHROUGH */
11434 case OMP_CLAUSE_DEPEND:
11435 case OMP_CLAUSE_REDUCTION:
11436 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11438 tree low_bound = NULL_TREE, length = NULL_TREE;
11440 c_parser_consume_token (parser);
11441 if (!c_parser_next_token_is (parser, CPP_COLON))
11443 location_t expr_loc
11444 = c_parser_peek_token (parser)->location;
11445 c_expr expr = c_parser_expression (parser);
11446 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11447 false, true);
11448 low_bound = expr.value;
11450 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11451 length = integer_one_node;
11452 else
11454 /* Look for `:'. */
11455 if (!c_parser_require (parser, CPP_COLON,
11456 "expected %<:%>"))
11458 t = error_mark_node;
11459 break;
11461 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11463 location_t expr_loc
11464 = c_parser_peek_token (parser)->location;
11465 c_expr expr = c_parser_expression (parser);
11466 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11467 false, true);
11468 length = expr.value;
11471 /* Look for the closing `]'. */
11472 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11473 "expected %<]%>"))
11475 t = error_mark_node;
11476 break;
11479 t = tree_cons (low_bound, length, t);
11481 break;
11482 default:
11483 break;
11486 if (t != error_mark_node)
11488 tree u = build_omp_clause (clause_loc, kind);
11489 OMP_CLAUSE_DECL (u) = t;
11490 OMP_CLAUSE_CHAIN (u) = list;
11491 list = u;
11494 else
11495 list = tree_cons (t, NULL_TREE, list);
11497 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11498 break;
11500 c_parser_consume_token (parser);
11503 return list;
11506 /* Similarly, but expect leading and trailing parenthesis. This is a very
11507 common case for OpenACC and OpenMP clauses. */
11509 static tree
11510 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11511 tree list)
11513 /* The clauses location. */
11514 location_t loc = c_parser_peek_token (parser)->location;
11516 matching_parens parens;
11517 if (parens.require_open (parser))
11519 list = c_parser_omp_variable_list (parser, loc, kind, list);
11520 parens.skip_until_found_close (parser);
11522 return list;
11525 /* OpenACC 2.0:
11526 copy ( variable-list )
11527 copyin ( variable-list )
11528 copyout ( variable-list )
11529 create ( variable-list )
11530 delete ( variable-list )
11531 present ( variable-list )
11532 present_or_copy ( variable-list )
11533 pcopy ( variable-list )
11534 present_or_copyin ( variable-list )
11535 pcopyin ( variable-list )
11536 present_or_copyout ( variable-list )
11537 pcopyout ( variable-list )
11538 present_or_create ( variable-list )
11539 pcreate ( variable-list ) */
11541 static tree
11542 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11543 tree list)
11545 enum gomp_map_kind kind;
11546 switch (c_kind)
11548 case PRAGMA_OACC_CLAUSE_COPY:
11549 kind = GOMP_MAP_FORCE_TOFROM;
11550 break;
11551 case PRAGMA_OACC_CLAUSE_COPYIN:
11552 kind = GOMP_MAP_FORCE_TO;
11553 break;
11554 case PRAGMA_OACC_CLAUSE_COPYOUT:
11555 kind = GOMP_MAP_FORCE_FROM;
11556 break;
11557 case PRAGMA_OACC_CLAUSE_CREATE:
11558 kind = GOMP_MAP_FORCE_ALLOC;
11559 break;
11560 case PRAGMA_OACC_CLAUSE_DELETE:
11561 kind = GOMP_MAP_DELETE;
11562 break;
11563 case PRAGMA_OACC_CLAUSE_DEVICE:
11564 kind = GOMP_MAP_FORCE_TO;
11565 break;
11566 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11567 kind = GOMP_MAP_DEVICE_RESIDENT;
11568 break;
11569 case PRAGMA_OACC_CLAUSE_HOST:
11570 case PRAGMA_OACC_CLAUSE_SELF:
11571 kind = GOMP_MAP_FORCE_FROM;
11572 break;
11573 case PRAGMA_OACC_CLAUSE_LINK:
11574 kind = GOMP_MAP_LINK;
11575 break;
11576 case PRAGMA_OACC_CLAUSE_PRESENT:
11577 kind = GOMP_MAP_FORCE_PRESENT;
11578 break;
11579 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11580 kind = GOMP_MAP_TOFROM;
11581 break;
11582 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11583 kind = GOMP_MAP_TO;
11584 break;
11585 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11586 kind = GOMP_MAP_FROM;
11587 break;
11588 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11589 kind = GOMP_MAP_ALLOC;
11590 break;
11591 default:
11592 gcc_unreachable ();
11594 tree nl, c;
11595 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11597 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11598 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11600 return nl;
11603 /* OpenACC 2.0:
11604 deviceptr ( variable-list ) */
11606 static tree
11607 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11609 location_t loc = c_parser_peek_token (parser)->location;
11610 tree vars, t;
11612 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11613 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11614 variable-list must only allow for pointer variables. */
11615 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11616 for (t = vars; t && t; t = TREE_CHAIN (t))
11618 tree v = TREE_PURPOSE (t);
11620 /* FIXME diagnostics: Ideally we should keep individual
11621 locations for all the variables in the var list to make the
11622 following errors more precise. Perhaps
11623 c_parser_omp_var_list_parens() should construct a list of
11624 locations to go along with the var list. */
11626 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11627 error_at (loc, "%qD is not a variable", v);
11628 else if (TREE_TYPE (v) == error_mark_node)
11630 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11631 error_at (loc, "%qD is not a pointer variable", v);
11633 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11634 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11635 OMP_CLAUSE_DECL (u) = v;
11636 OMP_CLAUSE_CHAIN (u) = list;
11637 list = u;
11640 return list;
11643 /* OpenACC 2.0, OpenMP 3.0:
11644 collapse ( constant-expression ) */
11646 static tree
11647 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11649 tree c, num = error_mark_node;
11650 HOST_WIDE_INT n;
11651 location_t loc;
11653 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11654 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11656 loc = c_parser_peek_token (parser)->location;
11657 matching_parens parens;
11658 if (parens.require_open (parser))
11660 num = c_parser_expr_no_commas (parser, NULL).value;
11661 parens.skip_until_found_close (parser);
11663 if (num == error_mark_node)
11664 return list;
11665 mark_exp_read (num);
11666 num = c_fully_fold (num, false, NULL);
11667 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11668 || !tree_fits_shwi_p (num)
11669 || (n = tree_to_shwi (num)) <= 0
11670 || (int) n != n)
11672 error_at (loc,
11673 "collapse argument needs positive constant integer expression");
11674 return list;
11676 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11677 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11678 OMP_CLAUSE_CHAIN (c) = list;
11679 return c;
11682 /* OpenMP 2.5:
11683 copyin ( variable-list ) */
11685 static tree
11686 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11688 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11691 /* OpenMP 2.5:
11692 copyprivate ( variable-list ) */
11694 static tree
11695 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11697 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11700 /* OpenMP 2.5:
11701 default ( none | shared )
11703 OpenACC:
11704 default ( none | present ) */
11706 static tree
11707 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11709 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11710 location_t loc = c_parser_peek_token (parser)->location;
11711 tree c;
11713 matching_parens parens;
11714 if (!parens.require_open (parser))
11715 return list;
11716 if (c_parser_next_token_is (parser, CPP_NAME))
11718 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11720 switch (p[0])
11722 case 'n':
11723 if (strcmp ("none", p) != 0)
11724 goto invalid_kind;
11725 kind = OMP_CLAUSE_DEFAULT_NONE;
11726 break;
11728 case 'p':
11729 if (strcmp ("present", p) != 0 || !is_oacc)
11730 goto invalid_kind;
11731 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11732 break;
11734 case 's':
11735 if (strcmp ("shared", p) != 0 || is_oacc)
11736 goto invalid_kind;
11737 kind = OMP_CLAUSE_DEFAULT_SHARED;
11738 break;
11740 default:
11741 goto invalid_kind;
11744 c_parser_consume_token (parser);
11746 else
11748 invalid_kind:
11749 if (is_oacc)
11750 c_parser_error (parser, "expected %<none%> or %<present%>");
11751 else
11752 c_parser_error (parser, "expected %<none%> or %<shared%>");
11754 parens.skip_until_found_close (parser);
11756 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11757 return list;
11759 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11760 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11761 OMP_CLAUSE_CHAIN (c) = list;
11762 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11764 return c;
11767 /* OpenMP 2.5:
11768 firstprivate ( variable-list ) */
11770 static tree
11771 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11773 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11776 /* OpenMP 3.1:
11777 final ( expression ) */
11779 static tree
11780 c_parser_omp_clause_final (c_parser *parser, tree list)
11782 location_t loc = c_parser_peek_token (parser)->location;
11783 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11785 tree t = c_parser_paren_condition (parser);
11786 tree c;
11788 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11790 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11791 OMP_CLAUSE_FINAL_EXPR (c) = t;
11792 OMP_CLAUSE_CHAIN (c) = list;
11793 list = c;
11795 else
11796 c_parser_error (parser, "expected %<(%>");
11798 return list;
11801 /* OpenACC, OpenMP 2.5:
11802 if ( expression )
11804 OpenMP 4.5:
11805 if ( directive-name-modifier : expression )
11807 directive-name-modifier:
11808 parallel | task | taskloop | target data | target | target update
11809 | target enter data | target exit data */
11811 static tree
11812 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11814 location_t location = c_parser_peek_token (parser)->location;
11815 enum tree_code if_modifier = ERROR_MARK;
11817 matching_parens parens;
11818 if (!parens.require_open (parser))
11819 return list;
11821 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11823 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11824 int n = 2;
11825 if (strcmp (p, "parallel") == 0)
11826 if_modifier = OMP_PARALLEL;
11827 else if (strcmp (p, "task") == 0)
11828 if_modifier = OMP_TASK;
11829 else if (strcmp (p, "taskloop") == 0)
11830 if_modifier = OMP_TASKLOOP;
11831 else if (strcmp (p, "target") == 0)
11833 if_modifier = OMP_TARGET;
11834 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11836 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11837 if (strcmp ("data", p) == 0)
11838 if_modifier = OMP_TARGET_DATA;
11839 else if (strcmp ("update", p) == 0)
11840 if_modifier = OMP_TARGET_UPDATE;
11841 else if (strcmp ("enter", p) == 0)
11842 if_modifier = OMP_TARGET_ENTER_DATA;
11843 else if (strcmp ("exit", p) == 0)
11844 if_modifier = OMP_TARGET_EXIT_DATA;
11845 if (if_modifier != OMP_TARGET)
11847 n = 3;
11848 c_parser_consume_token (parser);
11850 else
11852 location_t loc = c_parser_peek_2nd_token (parser)->location;
11853 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11854 "or %<exit%>");
11855 if_modifier = ERROR_MARK;
11857 if (if_modifier == OMP_TARGET_ENTER_DATA
11858 || if_modifier == OMP_TARGET_EXIT_DATA)
11860 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11862 p = IDENTIFIER_POINTER
11863 (c_parser_peek_2nd_token (parser)->value);
11864 if (strcmp ("data", p) == 0)
11865 n = 4;
11867 if (n == 4)
11868 c_parser_consume_token (parser);
11869 else
11871 location_t loc
11872 = c_parser_peek_2nd_token (parser)->location;
11873 error_at (loc, "expected %<data%>");
11874 if_modifier = ERROR_MARK;
11879 if (if_modifier != ERROR_MARK)
11881 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11883 c_parser_consume_token (parser);
11884 c_parser_consume_token (parser);
11886 else
11888 if (n > 2)
11890 location_t loc = c_parser_peek_2nd_token (parser)->location;
11891 error_at (loc, "expected %<:%>");
11893 if_modifier = ERROR_MARK;
11898 tree t = c_parser_condition (parser), c;
11899 parens.skip_until_found_close (parser);
11901 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11902 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11904 if (if_modifier != ERROR_MARK
11905 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11907 const char *p = NULL;
11908 switch (if_modifier)
11910 case OMP_PARALLEL: p = "parallel"; break;
11911 case OMP_TASK: p = "task"; break;
11912 case OMP_TASKLOOP: p = "taskloop"; break;
11913 case OMP_TARGET_DATA: p = "target data"; break;
11914 case OMP_TARGET: p = "target"; break;
11915 case OMP_TARGET_UPDATE: p = "target update"; break;
11916 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11917 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11918 default: gcc_unreachable ();
11920 error_at (location, "too many %<if%> clauses with %qs modifier",
11922 return list;
11924 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11926 if (!is_omp)
11927 error_at (location, "too many %<if%> clauses");
11928 else
11929 error_at (location, "too many %<if%> clauses without modifier");
11930 return list;
11932 else if (if_modifier == ERROR_MARK
11933 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11935 error_at (location, "if any %<if%> clause has modifier, then all "
11936 "%<if%> clauses have to use modifier");
11937 return list;
11941 c = build_omp_clause (location, OMP_CLAUSE_IF);
11942 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11943 OMP_CLAUSE_IF_EXPR (c) = t;
11944 OMP_CLAUSE_CHAIN (c) = list;
11945 return c;
11948 /* OpenMP 2.5:
11949 lastprivate ( variable-list ) */
11951 static tree
11952 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11954 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11957 /* OpenMP 3.1:
11958 mergeable */
11960 static tree
11961 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11963 tree c;
11965 /* FIXME: Should we allow duplicates? */
11966 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11968 c = build_omp_clause (c_parser_peek_token (parser)->location,
11969 OMP_CLAUSE_MERGEABLE);
11970 OMP_CLAUSE_CHAIN (c) = list;
11972 return c;
11975 /* OpenMP 2.5:
11976 nowait */
11978 static tree
11979 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11981 tree c;
11982 location_t loc = c_parser_peek_token (parser)->location;
11984 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11986 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11987 OMP_CLAUSE_CHAIN (c) = list;
11988 return c;
11991 /* OpenMP 2.5:
11992 num_threads ( expression ) */
11994 static tree
11995 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11997 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11998 matching_parens parens;
11999 if (parens.require_open (parser))
12001 location_t expr_loc = c_parser_peek_token (parser)->location;
12002 c_expr expr = c_parser_expression (parser);
12003 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12004 tree c, t = expr.value;
12005 t = c_fully_fold (t, false, NULL);
12007 parens.skip_until_found_close (parser);
12009 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12011 c_parser_error (parser, "expected integer expression");
12012 return list;
12015 /* Attempt to statically determine when the number isn't positive. */
12016 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12017 build_int_cst (TREE_TYPE (t), 0));
12018 protected_set_expr_location (c, expr_loc);
12019 if (c == boolean_true_node)
12021 warning_at (expr_loc, 0,
12022 "%<num_threads%> value must be positive");
12023 t = integer_one_node;
12026 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12028 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12029 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12030 OMP_CLAUSE_CHAIN (c) = list;
12031 list = c;
12034 return list;
12037 /* OpenMP 4.5:
12038 num_tasks ( expression ) */
12040 static tree
12041 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12043 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12044 matching_parens parens;
12045 if (parens.require_open (parser))
12047 location_t expr_loc = c_parser_peek_token (parser)->location;
12048 c_expr expr = c_parser_expression (parser);
12049 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12050 tree c, t = expr.value;
12051 t = c_fully_fold (t, false, NULL);
12053 parens.skip_until_found_close (parser);
12055 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12057 c_parser_error (parser, "expected integer expression");
12058 return list;
12061 /* Attempt to statically determine when the number isn't positive. */
12062 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12063 build_int_cst (TREE_TYPE (t), 0));
12064 if (CAN_HAVE_LOCATION_P (c))
12065 SET_EXPR_LOCATION (c, expr_loc);
12066 if (c == boolean_true_node)
12068 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12069 t = integer_one_node;
12072 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12074 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12075 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12076 OMP_CLAUSE_CHAIN (c) = list;
12077 list = c;
12080 return list;
12083 /* OpenMP 4.5:
12084 grainsize ( expression ) */
12086 static tree
12087 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12089 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12090 matching_parens parens;
12091 if (parens.require_open (parser))
12093 location_t expr_loc = c_parser_peek_token (parser)->location;
12094 c_expr expr = c_parser_expression (parser);
12095 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12096 tree c, t = expr.value;
12097 t = c_fully_fold (t, false, NULL);
12099 parens.skip_until_found_close (parser);
12101 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12103 c_parser_error (parser, "expected integer expression");
12104 return list;
12107 /* Attempt to statically determine when the number isn't positive. */
12108 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12109 build_int_cst (TREE_TYPE (t), 0));
12110 if (CAN_HAVE_LOCATION_P (c))
12111 SET_EXPR_LOCATION (c, expr_loc);
12112 if (c == boolean_true_node)
12114 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12115 t = integer_one_node;
12118 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12120 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12121 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12122 OMP_CLAUSE_CHAIN (c) = list;
12123 list = c;
12126 return list;
12129 /* OpenMP 4.5:
12130 priority ( expression ) */
12132 static tree
12133 c_parser_omp_clause_priority (c_parser *parser, tree list)
12135 location_t priority_loc = c_parser_peek_token (parser)->location;
12136 matching_parens parens;
12137 if (parens.require_open (parser))
12139 location_t expr_loc = c_parser_peek_token (parser)->location;
12140 c_expr expr = c_parser_expression (parser);
12141 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12142 tree c, t = expr.value;
12143 t = c_fully_fold (t, false, NULL);
12145 parens.skip_until_found_close (parser);
12147 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12149 c_parser_error (parser, "expected integer expression");
12150 return list;
12153 /* Attempt to statically determine when the number isn't
12154 non-negative. */
12155 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12156 build_int_cst (TREE_TYPE (t), 0));
12157 if (CAN_HAVE_LOCATION_P (c))
12158 SET_EXPR_LOCATION (c, expr_loc);
12159 if (c == boolean_true_node)
12161 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12162 t = integer_one_node;
12165 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12167 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12168 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12169 OMP_CLAUSE_CHAIN (c) = list;
12170 list = c;
12173 return list;
12176 /* OpenMP 4.5:
12177 hint ( expression ) */
12179 static tree
12180 c_parser_omp_clause_hint (c_parser *parser, tree list)
12182 location_t hint_loc = c_parser_peek_token (parser)->location;
12183 matching_parens parens;
12184 if (parens.require_open (parser))
12186 location_t expr_loc = c_parser_peek_token (parser)->location;
12187 c_expr expr = c_parser_expression (parser);
12188 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12189 tree c, t = expr.value;
12190 t = c_fully_fold (t, false, NULL);
12192 parens.skip_until_found_close (parser);
12194 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12196 c_parser_error (parser, "expected integer expression");
12197 return list;
12200 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12202 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12203 OMP_CLAUSE_HINT_EXPR (c) = t;
12204 OMP_CLAUSE_CHAIN (c) = list;
12205 list = c;
12208 return list;
12211 /* OpenMP 4.5:
12212 defaultmap ( tofrom : scalar ) */
12214 static tree
12215 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12217 location_t loc = c_parser_peek_token (parser)->location;
12218 tree c;
12219 const char *p;
12221 matching_parens parens;
12222 if (!parens.require_open (parser))
12223 return list;
12224 if (!c_parser_next_token_is (parser, CPP_NAME))
12226 c_parser_error (parser, "expected %<tofrom%>");
12227 goto out_err;
12229 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12230 if (strcmp (p, "tofrom") != 0)
12232 c_parser_error (parser, "expected %<tofrom%>");
12233 goto out_err;
12235 c_parser_consume_token (parser);
12236 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12237 goto out_err;
12238 if (!c_parser_next_token_is (parser, CPP_NAME))
12240 c_parser_error (parser, "expected %<scalar%>");
12241 goto out_err;
12243 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12244 if (strcmp (p, "scalar") != 0)
12246 c_parser_error (parser, "expected %<scalar%>");
12247 goto out_err;
12249 c_parser_consume_token (parser);
12250 parens.skip_until_found_close (parser);
12251 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12252 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12253 OMP_CLAUSE_CHAIN (c) = list;
12254 return c;
12256 out_err:
12257 parens.skip_until_found_close (parser);
12258 return list;
12261 /* OpenACC 2.0:
12262 use_device ( variable-list )
12264 OpenMP 4.5:
12265 use_device_ptr ( variable-list ) */
12267 static tree
12268 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12270 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12271 list);
12274 /* OpenMP 4.5:
12275 is_device_ptr ( variable-list ) */
12277 static tree
12278 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12280 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12283 /* OpenACC:
12284 num_gangs ( expression )
12285 num_workers ( expression )
12286 vector_length ( expression ) */
12288 static tree
12289 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12290 tree list)
12292 location_t loc = c_parser_peek_token (parser)->location;
12294 matching_parens parens;
12295 if (!parens.require_open (parser))
12296 return list;
12298 location_t expr_loc = c_parser_peek_token (parser)->location;
12299 c_expr expr = c_parser_expression (parser);
12300 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12301 tree c, t = expr.value;
12302 t = c_fully_fold (t, false, NULL);
12304 parens.skip_until_found_close (parser);
12306 if (t == error_mark_node)
12307 return list;
12308 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12310 error_at (expr_loc, "%qs expression must be integral",
12311 omp_clause_code_name[code]);
12312 return list;
12315 /* Attempt to statically determine when the number isn't positive. */
12316 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12317 build_int_cst (TREE_TYPE (t), 0));
12318 protected_set_expr_location (c, expr_loc);
12319 if (c == boolean_true_node)
12321 warning_at (expr_loc, 0,
12322 "%qs value must be positive",
12323 omp_clause_code_name[code]);
12324 t = integer_one_node;
12327 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12329 c = build_omp_clause (loc, code);
12330 OMP_CLAUSE_OPERAND (c, 0) = t;
12331 OMP_CLAUSE_CHAIN (c) = list;
12332 return c;
12335 /* OpenACC:
12337 gang [( gang-arg-list )]
12338 worker [( [num:] int-expr )]
12339 vector [( [length:] int-expr )]
12341 where gang-arg is one of:
12343 [num:] int-expr
12344 static: size-expr
12346 and size-expr may be:
12349 int-expr
12352 static tree
12353 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12354 const char *str, tree list)
12356 const char *id = "num";
12357 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12358 location_t loc = c_parser_peek_token (parser)->location;
12360 if (kind == OMP_CLAUSE_VECTOR)
12361 id = "length";
12363 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12365 c_parser_consume_token (parser);
12369 c_token *next = c_parser_peek_token (parser);
12370 int idx = 0;
12372 /* Gang static argument. */
12373 if (kind == OMP_CLAUSE_GANG
12374 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12376 c_parser_consume_token (parser);
12378 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12379 goto cleanup_error;
12381 idx = 1;
12382 if (ops[idx] != NULL_TREE)
12384 c_parser_error (parser, "too many %<static%> arguments");
12385 goto cleanup_error;
12388 /* Check for the '*' argument. */
12389 if (c_parser_next_token_is (parser, CPP_MULT)
12390 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12391 || c_parser_peek_2nd_token (parser)->type
12392 == CPP_CLOSE_PAREN))
12394 c_parser_consume_token (parser);
12395 ops[idx] = integer_minus_one_node;
12397 if (c_parser_next_token_is (parser, CPP_COMMA))
12399 c_parser_consume_token (parser);
12400 continue;
12402 else
12403 break;
12406 /* Worker num: argument and vector length: arguments. */
12407 else if (c_parser_next_token_is (parser, CPP_NAME)
12408 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12409 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12411 c_parser_consume_token (parser); /* id */
12412 c_parser_consume_token (parser); /* ':' */
12415 /* Now collect the actual argument. */
12416 if (ops[idx] != NULL_TREE)
12418 c_parser_error (parser, "unexpected argument");
12419 goto cleanup_error;
12422 location_t expr_loc = c_parser_peek_token (parser)->location;
12423 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12424 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12425 tree expr = cexpr.value;
12426 if (expr == error_mark_node)
12427 goto cleanup_error;
12429 expr = c_fully_fold (expr, false, NULL);
12431 /* Attempt to statically determine when the number isn't a
12432 positive integer. */
12434 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12436 c_parser_error (parser, "expected integer expression");
12437 return list;
12440 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12441 build_int_cst (TREE_TYPE (expr), 0));
12442 if (c == boolean_true_node)
12444 warning_at (loc, 0,
12445 "%qs value must be positive", str);
12446 expr = integer_one_node;
12449 ops[idx] = expr;
12451 if (kind == OMP_CLAUSE_GANG
12452 && c_parser_next_token_is (parser, CPP_COMMA))
12454 c_parser_consume_token (parser);
12455 continue;
12457 break;
12459 while (1);
12461 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12462 goto cleanup_error;
12465 check_no_duplicate_clause (list, kind, str);
12467 c = build_omp_clause (loc, kind);
12469 if (ops[1])
12470 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12472 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12473 OMP_CLAUSE_CHAIN (c) = list;
12475 return c;
12477 cleanup_error:
12478 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12479 return list;
12482 /* OpenACC:
12483 auto
12484 independent
12485 nohost
12486 seq */
12488 static tree
12489 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12490 tree list)
12492 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12494 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12495 OMP_CLAUSE_CHAIN (c) = list;
12497 return c;
12500 /* OpenACC:
12501 async [( int-expr )] */
12503 static tree
12504 c_parser_oacc_clause_async (c_parser *parser, tree list)
12506 tree c, t;
12507 location_t loc = c_parser_peek_token (parser)->location;
12509 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12511 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12513 c_parser_consume_token (parser);
12515 t = c_parser_expression (parser).value;
12516 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12517 c_parser_error (parser, "expected integer expression");
12518 else if (t == error_mark_node
12519 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12520 return list;
12522 else
12523 t = c_fully_fold (t, false, NULL);
12525 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12527 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12528 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12529 OMP_CLAUSE_CHAIN (c) = list;
12530 list = c;
12532 return list;
12535 /* OpenACC 2.0:
12536 tile ( size-expr-list ) */
12538 static tree
12539 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12541 tree c, expr = error_mark_node;
12542 location_t loc;
12543 tree tile = NULL_TREE;
12545 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12546 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12548 loc = c_parser_peek_token (parser)->location;
12549 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12550 return list;
12554 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12555 return list;
12557 if (c_parser_next_token_is (parser, CPP_MULT)
12558 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12559 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12561 c_parser_consume_token (parser);
12562 expr = integer_zero_node;
12564 else
12566 location_t expr_loc = c_parser_peek_token (parser)->location;
12567 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12568 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12569 expr = cexpr.value;
12571 if (expr == error_mark_node)
12573 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12574 "expected %<)%>");
12575 return list;
12578 expr = c_fully_fold (expr, false, NULL);
12580 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12581 || !tree_fits_shwi_p (expr)
12582 || tree_to_shwi (expr) <= 0)
12584 error_at (expr_loc, "%<tile%> argument needs positive"
12585 " integral constant");
12586 expr = integer_zero_node;
12590 tile = tree_cons (NULL_TREE, expr, tile);
12592 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12594 /* Consume the trailing ')'. */
12595 c_parser_consume_token (parser);
12597 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12598 tile = nreverse (tile);
12599 OMP_CLAUSE_TILE_LIST (c) = tile;
12600 OMP_CLAUSE_CHAIN (c) = list;
12601 return c;
12604 /* OpenACC:
12605 wait ( int-expr-list ) */
12607 static tree
12608 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12610 location_t clause_loc = c_parser_peek_token (parser)->location;
12612 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12613 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12615 return list;
12618 /* OpenMP 2.5:
12619 ordered
12621 OpenMP 4.5:
12622 ordered ( constant-expression ) */
12624 static tree
12625 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12627 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12629 tree c, num = NULL_TREE;
12630 HOST_WIDE_INT n;
12631 location_t loc = c_parser_peek_token (parser)->location;
12632 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12634 matching_parens parens;
12635 parens.consume_open (parser);
12636 num = c_parser_expr_no_commas (parser, NULL).value;
12637 parens.skip_until_found_close (parser);
12639 if (num == error_mark_node)
12640 return list;
12641 if (num)
12643 mark_exp_read (num);
12644 num = c_fully_fold (num, false, NULL);
12645 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12646 || !tree_fits_shwi_p (num)
12647 || (n = tree_to_shwi (num)) <= 0
12648 || (int) n != n)
12650 error_at (loc, "ordered argument needs positive "
12651 "constant integer expression");
12652 return list;
12655 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12656 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12657 OMP_CLAUSE_CHAIN (c) = list;
12658 return c;
12661 /* OpenMP 2.5:
12662 private ( variable-list ) */
12664 static tree
12665 c_parser_omp_clause_private (c_parser *parser, tree list)
12667 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12670 /* OpenMP 2.5:
12671 reduction ( reduction-operator : variable-list )
12673 reduction-operator:
12674 One of: + * - & ^ | && ||
12676 OpenMP 3.1:
12678 reduction-operator:
12679 One of: + * - & ^ | && || max min
12681 OpenMP 4.0:
12683 reduction-operator:
12684 One of: + * - & ^ | && ||
12685 identifier */
12687 static tree
12688 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12690 location_t clause_loc = c_parser_peek_token (parser)->location;
12691 matching_parens parens;
12692 if (parens.require_open (parser))
12694 enum tree_code code = ERROR_MARK;
12695 tree reduc_id = NULL_TREE;
12697 switch (c_parser_peek_token (parser)->type)
12699 case CPP_PLUS:
12700 code = PLUS_EXPR;
12701 break;
12702 case CPP_MULT:
12703 code = MULT_EXPR;
12704 break;
12705 case CPP_MINUS:
12706 code = MINUS_EXPR;
12707 break;
12708 case CPP_AND:
12709 code = BIT_AND_EXPR;
12710 break;
12711 case CPP_XOR:
12712 code = BIT_XOR_EXPR;
12713 break;
12714 case CPP_OR:
12715 code = BIT_IOR_EXPR;
12716 break;
12717 case CPP_AND_AND:
12718 code = TRUTH_ANDIF_EXPR;
12719 break;
12720 case CPP_OR_OR:
12721 code = TRUTH_ORIF_EXPR;
12722 break;
12723 case CPP_NAME:
12725 const char *p
12726 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12727 if (strcmp (p, "min") == 0)
12729 code = MIN_EXPR;
12730 break;
12732 if (strcmp (p, "max") == 0)
12734 code = MAX_EXPR;
12735 break;
12737 reduc_id = c_parser_peek_token (parser)->value;
12738 break;
12740 default:
12741 c_parser_error (parser,
12742 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12743 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12744 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12745 return list;
12747 c_parser_consume_token (parser);
12748 reduc_id = c_omp_reduction_id (code, reduc_id);
12749 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12751 tree nl, c;
12753 nl = c_parser_omp_variable_list (parser, clause_loc,
12754 OMP_CLAUSE_REDUCTION, list);
12755 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12757 tree d = OMP_CLAUSE_DECL (c), type;
12758 if (TREE_CODE (d) != TREE_LIST)
12759 type = TREE_TYPE (d);
12760 else
12762 int cnt = 0;
12763 tree t;
12764 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12765 cnt++;
12766 type = TREE_TYPE (t);
12767 while (cnt > 0)
12769 if (TREE_CODE (type) != POINTER_TYPE
12770 && TREE_CODE (type) != ARRAY_TYPE)
12771 break;
12772 type = TREE_TYPE (type);
12773 cnt--;
12776 while (TREE_CODE (type) == ARRAY_TYPE)
12777 type = TREE_TYPE (type);
12778 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12779 if (code == ERROR_MARK
12780 || !(INTEGRAL_TYPE_P (type)
12781 || TREE_CODE (type) == REAL_TYPE
12782 || TREE_CODE (type) == COMPLEX_TYPE))
12783 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12784 = c_omp_reduction_lookup (reduc_id,
12785 TYPE_MAIN_VARIANT (type));
12788 list = nl;
12790 parens.skip_until_found_close (parser);
12792 return list;
12795 /* OpenMP 2.5:
12796 schedule ( schedule-kind )
12797 schedule ( schedule-kind , expression )
12799 schedule-kind:
12800 static | dynamic | guided | runtime | auto
12802 OpenMP 4.5:
12803 schedule ( schedule-modifier : schedule-kind )
12804 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12806 schedule-modifier:
12807 simd
12808 monotonic
12809 nonmonotonic */
12811 static tree
12812 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12814 tree c, t;
12815 location_t loc = c_parser_peek_token (parser)->location;
12816 int modifiers = 0, nmodifiers = 0;
12818 matching_parens parens;
12819 if (!parens.require_open (parser))
12820 return list;
12822 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12824 while (c_parser_next_token_is (parser, CPP_NAME))
12826 tree kind = c_parser_peek_token (parser)->value;
12827 const char *p = IDENTIFIER_POINTER (kind);
12828 if (strcmp ("simd", p) == 0)
12829 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12830 else if (strcmp ("monotonic", p) == 0)
12831 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12832 else if (strcmp ("nonmonotonic", p) == 0)
12833 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12834 else
12835 break;
12836 c_parser_consume_token (parser);
12837 if (nmodifiers++ == 0
12838 && c_parser_next_token_is (parser, CPP_COMMA))
12839 c_parser_consume_token (parser);
12840 else
12842 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12843 break;
12847 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12848 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12849 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12850 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12852 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12853 "specified");
12854 modifiers = 0;
12857 if (c_parser_next_token_is (parser, CPP_NAME))
12859 tree kind = c_parser_peek_token (parser)->value;
12860 const char *p = IDENTIFIER_POINTER (kind);
12862 switch (p[0])
12864 case 'd':
12865 if (strcmp ("dynamic", p) != 0)
12866 goto invalid_kind;
12867 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12868 break;
12870 case 'g':
12871 if (strcmp ("guided", p) != 0)
12872 goto invalid_kind;
12873 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12874 break;
12876 case 'r':
12877 if (strcmp ("runtime", p) != 0)
12878 goto invalid_kind;
12879 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12880 break;
12882 default:
12883 goto invalid_kind;
12886 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12887 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12888 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12889 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12890 else
12891 goto invalid_kind;
12893 c_parser_consume_token (parser);
12894 if (c_parser_next_token_is (parser, CPP_COMMA))
12896 location_t here;
12897 c_parser_consume_token (parser);
12899 here = c_parser_peek_token (parser)->location;
12900 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12901 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12902 t = expr.value;
12903 t = c_fully_fold (t, false, NULL);
12905 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12906 error_at (here, "schedule %<runtime%> does not take "
12907 "a %<chunk_size%> parameter");
12908 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12909 error_at (here,
12910 "schedule %<auto%> does not take "
12911 "a %<chunk_size%> parameter");
12912 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12914 /* Attempt to statically determine when the number isn't
12915 positive. */
12916 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12917 build_int_cst (TREE_TYPE (t), 0));
12918 protected_set_expr_location (s, loc);
12919 if (s == boolean_true_node)
12921 warning_at (loc, 0,
12922 "chunk size value must be positive");
12923 t = integer_one_node;
12925 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12927 else
12928 c_parser_error (parser, "expected integer expression");
12930 parens.skip_until_found_close (parser);
12932 else
12933 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12934 "expected %<,%> or %<)%>");
12936 OMP_CLAUSE_SCHEDULE_KIND (c)
12937 = (enum omp_clause_schedule_kind)
12938 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12940 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12941 OMP_CLAUSE_CHAIN (c) = list;
12942 return c;
12944 invalid_kind:
12945 c_parser_error (parser, "invalid schedule kind");
12946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12947 return list;
12950 /* OpenMP 2.5:
12951 shared ( variable-list ) */
12953 static tree
12954 c_parser_omp_clause_shared (c_parser *parser, tree list)
12956 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12959 /* OpenMP 3.0:
12960 untied */
12962 static tree
12963 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12965 tree c;
12967 /* FIXME: Should we allow duplicates? */
12968 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12970 c = build_omp_clause (c_parser_peek_token (parser)->location,
12971 OMP_CLAUSE_UNTIED);
12972 OMP_CLAUSE_CHAIN (c) = list;
12974 return c;
12977 /* OpenMP 4.0:
12978 inbranch
12979 notinbranch */
12981 static tree
12982 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12983 enum omp_clause_code code, tree list)
12985 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12987 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12988 OMP_CLAUSE_CHAIN (c) = list;
12990 return c;
12993 /* OpenMP 4.0:
12994 parallel
12996 sections
12997 taskgroup */
12999 static tree
13000 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13001 enum omp_clause_code code, tree list)
13003 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13004 OMP_CLAUSE_CHAIN (c) = list;
13006 return c;
13009 /* OpenMP 4.5:
13010 nogroup */
13012 static tree
13013 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13015 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13016 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13017 OMP_CLAUSE_NOGROUP);
13018 OMP_CLAUSE_CHAIN (c) = list;
13019 return c;
13022 /* OpenMP 4.5:
13023 simd
13024 threads */
13026 static tree
13027 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13028 enum omp_clause_code code, tree list)
13030 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13031 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13032 OMP_CLAUSE_CHAIN (c) = list;
13033 return c;
13036 /* OpenMP 4.0:
13037 num_teams ( expression ) */
13039 static tree
13040 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13042 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13043 matching_parens parens;
13044 if (parens.require_open (parser))
13046 location_t expr_loc = c_parser_peek_token (parser)->location;
13047 c_expr expr = c_parser_expression (parser);
13048 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13049 tree c, t = expr.value;
13050 t = c_fully_fold (t, false, NULL);
13052 parens.skip_until_found_close (parser);
13054 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13056 c_parser_error (parser, "expected integer expression");
13057 return list;
13060 /* Attempt to statically determine when the number isn't positive. */
13061 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13062 build_int_cst (TREE_TYPE (t), 0));
13063 protected_set_expr_location (c, expr_loc);
13064 if (c == boolean_true_node)
13066 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13067 t = integer_one_node;
13070 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13072 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13073 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13074 OMP_CLAUSE_CHAIN (c) = list;
13075 list = c;
13078 return list;
13081 /* OpenMP 4.0:
13082 thread_limit ( expression ) */
13084 static tree
13085 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13087 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13088 matching_parens parens;
13089 if (parens.require_open (parser))
13091 location_t expr_loc = c_parser_peek_token (parser)->location;
13092 c_expr expr = c_parser_expression (parser);
13093 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13094 tree c, t = expr.value;
13095 t = c_fully_fold (t, false, NULL);
13097 parens.skip_until_found_close (parser);
13099 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13101 c_parser_error (parser, "expected integer expression");
13102 return list;
13105 /* Attempt to statically determine when the number isn't positive. */
13106 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13107 build_int_cst (TREE_TYPE (t), 0));
13108 protected_set_expr_location (c, expr_loc);
13109 if (c == boolean_true_node)
13111 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13112 t = integer_one_node;
13115 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13116 "thread_limit");
13118 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13119 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13120 OMP_CLAUSE_CHAIN (c) = list;
13121 list = c;
13124 return list;
13127 /* OpenMP 4.0:
13128 aligned ( variable-list )
13129 aligned ( variable-list : constant-expression ) */
13131 static tree
13132 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13134 location_t clause_loc = c_parser_peek_token (parser)->location;
13135 tree nl, c;
13137 matching_parens parens;
13138 if (!parens.require_open (parser))
13139 return list;
13141 nl = c_parser_omp_variable_list (parser, clause_loc,
13142 OMP_CLAUSE_ALIGNED, list);
13144 if (c_parser_next_token_is (parser, CPP_COLON))
13146 c_parser_consume_token (parser);
13147 location_t expr_loc = c_parser_peek_token (parser)->location;
13148 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13149 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13150 tree alignment = expr.value;
13151 alignment = c_fully_fold (alignment, false, NULL);
13152 if (TREE_CODE (alignment) != INTEGER_CST
13153 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13154 || tree_int_cst_sgn (alignment) != 1)
13156 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13157 "be positive constant integer expression");
13158 alignment = NULL_TREE;
13161 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13162 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13165 parens.skip_until_found_close (parser);
13166 return nl;
13169 /* OpenMP 4.0:
13170 linear ( variable-list )
13171 linear ( variable-list : expression )
13173 OpenMP 4.5:
13174 linear ( modifier ( variable-list ) )
13175 linear ( modifier ( variable-list ) : expression ) */
13177 static tree
13178 c_parser_omp_clause_linear (c_parser *parser, tree list)
13180 location_t clause_loc = c_parser_peek_token (parser)->location;
13181 tree nl, c, step;
13182 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13184 matching_parens parens;
13185 if (!parens.require_open (parser))
13186 return list;
13188 if (c_parser_next_token_is (parser, CPP_NAME))
13190 c_token *tok = c_parser_peek_token (parser);
13191 const char *p = IDENTIFIER_POINTER (tok->value);
13192 if (strcmp ("val", p) == 0)
13193 kind = OMP_CLAUSE_LINEAR_VAL;
13194 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13195 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13196 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13198 c_parser_consume_token (parser);
13199 c_parser_consume_token (parser);
13203 nl = c_parser_omp_variable_list (parser, clause_loc,
13204 OMP_CLAUSE_LINEAR, list);
13206 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13207 parens.skip_until_found_close (parser);
13209 if (c_parser_next_token_is (parser, CPP_COLON))
13211 c_parser_consume_token (parser);
13212 location_t expr_loc = c_parser_peek_token (parser)->location;
13213 c_expr expr = c_parser_expression (parser);
13214 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13215 step = expr.value;
13216 step = c_fully_fold (step, false, NULL);
13217 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13219 error_at (clause_loc, "%<linear%> clause step expression must "
13220 "be integral");
13221 step = integer_one_node;
13225 else
13226 step = integer_one_node;
13228 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13230 OMP_CLAUSE_LINEAR_STEP (c) = step;
13231 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13234 parens.skip_until_found_close (parser);
13235 return nl;
13238 /* OpenMP 4.0:
13239 safelen ( constant-expression ) */
13241 static tree
13242 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13244 location_t clause_loc = c_parser_peek_token (parser)->location;
13245 tree c, t;
13247 matching_parens parens;
13248 if (!parens.require_open (parser))
13249 return list;
13251 location_t expr_loc = c_parser_peek_token (parser)->location;
13252 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13253 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13254 t = expr.value;
13255 t = c_fully_fold (t, false, NULL);
13256 if (TREE_CODE (t) != INTEGER_CST
13257 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13258 || tree_int_cst_sgn (t) != 1)
13260 error_at (clause_loc, "%<safelen%> clause expression must "
13261 "be positive constant integer expression");
13262 t = NULL_TREE;
13265 parens.skip_until_found_close (parser);
13266 if (t == NULL_TREE || t == error_mark_node)
13267 return list;
13269 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13271 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13272 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13273 OMP_CLAUSE_CHAIN (c) = list;
13274 return c;
13277 /* OpenMP 4.0:
13278 simdlen ( constant-expression ) */
13280 static tree
13281 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13283 location_t clause_loc = c_parser_peek_token (parser)->location;
13284 tree c, t;
13286 matching_parens parens;
13287 if (!parens.require_open (parser))
13288 return list;
13290 location_t expr_loc = c_parser_peek_token (parser)->location;
13291 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13292 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13293 t = expr.value;
13294 t = c_fully_fold (t, false, NULL);
13295 if (TREE_CODE (t) != INTEGER_CST
13296 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13297 || tree_int_cst_sgn (t) != 1)
13299 error_at (clause_loc, "%<simdlen%> clause expression must "
13300 "be positive constant integer expression");
13301 t = NULL_TREE;
13304 parens.skip_until_found_close (parser);
13305 if (t == NULL_TREE || t == error_mark_node)
13306 return list;
13308 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13310 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13311 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13312 OMP_CLAUSE_CHAIN (c) = list;
13313 return c;
13316 /* OpenMP 4.5:
13317 vec:
13318 identifier [+/- integer]
13319 vec , identifier [+/- integer]
13322 static tree
13323 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13324 tree list)
13326 tree vec = NULL;
13327 if (c_parser_next_token_is_not (parser, CPP_NAME)
13328 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13330 c_parser_error (parser, "expected identifier");
13331 return list;
13334 while (c_parser_next_token_is (parser, CPP_NAME)
13335 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13337 tree t = lookup_name (c_parser_peek_token (parser)->value);
13338 tree addend = NULL;
13340 if (t == NULL_TREE)
13342 undeclared_variable (c_parser_peek_token (parser)->location,
13343 c_parser_peek_token (parser)->value);
13344 t = error_mark_node;
13347 c_parser_consume_token (parser);
13349 bool neg = false;
13350 if (c_parser_next_token_is (parser, CPP_MINUS))
13351 neg = true;
13352 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13354 addend = integer_zero_node;
13355 neg = false;
13356 goto add_to_vector;
13358 c_parser_consume_token (parser);
13360 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13362 c_parser_error (parser, "expected integer");
13363 return list;
13366 addend = c_parser_peek_token (parser)->value;
13367 if (TREE_CODE (addend) != INTEGER_CST)
13369 c_parser_error (parser, "expected integer");
13370 return list;
13372 c_parser_consume_token (parser);
13374 add_to_vector:
13375 if (t != error_mark_node)
13377 vec = tree_cons (addend, t, vec);
13378 if (neg)
13379 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13382 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13383 break;
13385 c_parser_consume_token (parser);
13388 if (vec == NULL_TREE)
13389 return list;
13391 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13392 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13393 OMP_CLAUSE_DECL (u) = nreverse (vec);
13394 OMP_CLAUSE_CHAIN (u) = list;
13395 return u;
13398 /* OpenMP 4.0:
13399 depend ( depend-kind: variable-list )
13401 depend-kind:
13402 in | out | inout
13404 OpenMP 4.5:
13405 depend ( source )
13407 depend ( sink : vec ) */
13409 static tree
13410 c_parser_omp_clause_depend (c_parser *parser, tree list)
13412 location_t clause_loc = c_parser_peek_token (parser)->location;
13413 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13414 tree nl, c;
13416 matching_parens parens;
13417 if (!parens.require_open (parser))
13418 return list;
13420 if (c_parser_next_token_is (parser, CPP_NAME))
13422 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13423 if (strcmp ("in", p) == 0)
13424 kind = OMP_CLAUSE_DEPEND_IN;
13425 else if (strcmp ("inout", p) == 0)
13426 kind = OMP_CLAUSE_DEPEND_INOUT;
13427 else if (strcmp ("out", p) == 0)
13428 kind = OMP_CLAUSE_DEPEND_OUT;
13429 else if (strcmp ("source", p) == 0)
13430 kind = OMP_CLAUSE_DEPEND_SOURCE;
13431 else if (strcmp ("sink", p) == 0)
13432 kind = OMP_CLAUSE_DEPEND_SINK;
13433 else
13434 goto invalid_kind;
13436 else
13437 goto invalid_kind;
13439 c_parser_consume_token (parser);
13441 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13443 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13444 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13445 OMP_CLAUSE_DECL (c) = NULL_TREE;
13446 OMP_CLAUSE_CHAIN (c) = list;
13447 parens.skip_until_found_close (parser);
13448 return c;
13451 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13452 goto resync_fail;
13454 if (kind == OMP_CLAUSE_DEPEND_SINK)
13455 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13456 else
13458 nl = c_parser_omp_variable_list (parser, clause_loc,
13459 OMP_CLAUSE_DEPEND, list);
13461 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13462 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13465 parens.skip_until_found_close (parser);
13466 return nl;
13468 invalid_kind:
13469 c_parser_error (parser, "invalid depend kind");
13470 resync_fail:
13471 parens.skip_until_found_close (parser);
13472 return list;
13475 /* OpenMP 4.0:
13476 map ( map-kind: variable-list )
13477 map ( variable-list )
13479 map-kind:
13480 alloc | to | from | tofrom
13482 OpenMP 4.5:
13483 map-kind:
13484 alloc | to | from | tofrom | release | delete
13486 map ( always [,] map-kind: variable-list ) */
13488 static tree
13489 c_parser_omp_clause_map (c_parser *parser, tree list)
13491 location_t clause_loc = c_parser_peek_token (parser)->location;
13492 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13493 int always = 0;
13494 enum c_id_kind always_id_kind = C_ID_NONE;
13495 location_t always_loc = UNKNOWN_LOCATION;
13496 tree always_id = NULL_TREE;
13497 tree nl, c;
13499 matching_parens parens;
13500 if (!parens.require_open (parser))
13501 return list;
13503 if (c_parser_next_token_is (parser, CPP_NAME))
13505 c_token *tok = c_parser_peek_token (parser);
13506 const char *p = IDENTIFIER_POINTER (tok->value);
13507 always_id_kind = tok->id_kind;
13508 always_loc = tok->location;
13509 always_id = tok->value;
13510 if (strcmp ("always", p) == 0)
13512 c_token *sectok = c_parser_peek_2nd_token (parser);
13513 if (sectok->type == CPP_COMMA)
13515 c_parser_consume_token (parser);
13516 c_parser_consume_token (parser);
13517 always = 2;
13519 else if (sectok->type == CPP_NAME)
13521 p = IDENTIFIER_POINTER (sectok->value);
13522 if (strcmp ("alloc", p) == 0
13523 || strcmp ("to", p) == 0
13524 || strcmp ("from", p) == 0
13525 || strcmp ("tofrom", p) == 0
13526 || strcmp ("release", p) == 0
13527 || strcmp ("delete", p) == 0)
13529 c_parser_consume_token (parser);
13530 always = 1;
13536 if (c_parser_next_token_is (parser, CPP_NAME)
13537 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13539 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13540 if (strcmp ("alloc", p) == 0)
13541 kind = GOMP_MAP_ALLOC;
13542 else if (strcmp ("to", p) == 0)
13543 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13544 else if (strcmp ("from", p) == 0)
13545 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13546 else if (strcmp ("tofrom", p) == 0)
13547 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13548 else if (strcmp ("release", p) == 0)
13549 kind = GOMP_MAP_RELEASE;
13550 else if (strcmp ("delete", p) == 0)
13551 kind = GOMP_MAP_DELETE;
13552 else
13554 c_parser_error (parser, "invalid map kind");
13555 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13556 "expected %<)%>");
13557 return list;
13559 c_parser_consume_token (parser);
13560 c_parser_consume_token (parser);
13562 else if (always)
13564 if (always_id_kind != C_ID_ID)
13566 c_parser_error (parser, "expected identifier");
13567 parens.skip_until_found_close (parser);
13568 return list;
13571 tree t = lookup_name (always_id);
13572 if (t == NULL_TREE)
13574 undeclared_variable (always_loc, always_id);
13575 t = error_mark_node;
13577 if (t != error_mark_node)
13579 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13580 OMP_CLAUSE_DECL (u) = t;
13581 OMP_CLAUSE_CHAIN (u) = list;
13582 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13583 list = u;
13585 if (always == 1)
13587 parens.skip_until_found_close (parser);
13588 return list;
13592 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13594 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13595 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13597 parens.skip_until_found_close (parser);
13598 return nl;
13601 /* OpenMP 4.0:
13602 device ( expression ) */
13604 static tree
13605 c_parser_omp_clause_device (c_parser *parser, tree list)
13607 location_t clause_loc = c_parser_peek_token (parser)->location;
13608 matching_parens parens;
13609 if (parens.require_open (parser))
13611 location_t expr_loc = c_parser_peek_token (parser)->location;
13612 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13613 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13614 tree c, t = expr.value;
13615 t = c_fully_fold (t, false, NULL);
13617 parens.skip_until_found_close (parser);
13619 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13621 c_parser_error (parser, "expected integer expression");
13622 return list;
13625 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13627 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13628 OMP_CLAUSE_DEVICE_ID (c) = t;
13629 OMP_CLAUSE_CHAIN (c) = list;
13630 list = c;
13633 return list;
13636 /* OpenMP 4.0:
13637 dist_schedule ( static )
13638 dist_schedule ( static , expression ) */
13640 static tree
13641 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13643 tree c, t = NULL_TREE;
13644 location_t loc = c_parser_peek_token (parser)->location;
13646 matching_parens parens;
13647 if (!parens.require_open (parser))
13648 return list;
13650 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13652 c_parser_error (parser, "invalid dist_schedule kind");
13653 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13654 "expected %<)%>");
13655 return list;
13658 c_parser_consume_token (parser);
13659 if (c_parser_next_token_is (parser, CPP_COMMA))
13661 c_parser_consume_token (parser);
13663 location_t expr_loc = c_parser_peek_token (parser)->location;
13664 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13665 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13666 t = expr.value;
13667 t = c_fully_fold (t, false, NULL);
13668 parens.skip_until_found_close (parser);
13670 else
13671 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13672 "expected %<,%> or %<)%>");
13674 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13675 if (t == error_mark_node)
13676 return list;
13678 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13679 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13680 OMP_CLAUSE_CHAIN (c) = list;
13681 return c;
13684 /* OpenMP 4.0:
13685 proc_bind ( proc-bind-kind )
13687 proc-bind-kind:
13688 master | close | spread */
13690 static tree
13691 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13693 location_t clause_loc = c_parser_peek_token (parser)->location;
13694 enum omp_clause_proc_bind_kind kind;
13695 tree c;
13697 matching_parens parens;
13698 if (!parens.require_open (parser))
13699 return list;
13701 if (c_parser_next_token_is (parser, CPP_NAME))
13703 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13704 if (strcmp ("master", p) == 0)
13705 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13706 else if (strcmp ("close", p) == 0)
13707 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13708 else if (strcmp ("spread", p) == 0)
13709 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13710 else
13711 goto invalid_kind;
13713 else
13714 goto invalid_kind;
13716 c_parser_consume_token (parser);
13717 parens.skip_until_found_close (parser);
13718 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13719 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13720 OMP_CLAUSE_CHAIN (c) = list;
13721 return c;
13723 invalid_kind:
13724 c_parser_error (parser, "invalid proc_bind kind");
13725 parens.skip_until_found_close (parser);
13726 return list;
13729 /* OpenMP 4.0:
13730 to ( variable-list ) */
13732 static tree
13733 c_parser_omp_clause_to (c_parser *parser, tree list)
13735 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13738 /* OpenMP 4.0:
13739 from ( variable-list ) */
13741 static tree
13742 c_parser_omp_clause_from (c_parser *parser, tree list)
13744 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13747 /* OpenMP 4.0:
13748 uniform ( variable-list ) */
13750 static tree
13751 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13753 /* The clauses location. */
13754 location_t loc = c_parser_peek_token (parser)->location;
13756 matching_parens parens;
13757 if (parens.require_open (parser))
13759 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13760 list);
13761 parens.skip_until_found_close (parser);
13763 return list;
13766 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13767 is a bitmask in MASK. Return the list of clauses found. */
13769 static tree
13770 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13771 const char *where, bool finish_p = true)
13773 tree clauses = NULL;
13774 bool first = true;
13776 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13778 location_t here;
13779 pragma_omp_clause c_kind;
13780 const char *c_name;
13781 tree prev = clauses;
13783 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13784 c_parser_consume_token (parser);
13786 here = c_parser_peek_token (parser)->location;
13787 c_kind = c_parser_omp_clause_name (parser);
13789 switch (c_kind)
13791 case PRAGMA_OACC_CLAUSE_ASYNC:
13792 clauses = c_parser_oacc_clause_async (parser, clauses);
13793 c_name = "async";
13794 break;
13795 case PRAGMA_OACC_CLAUSE_AUTO:
13796 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13797 clauses);
13798 c_name = "auto";
13799 break;
13800 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13801 clauses = c_parser_omp_clause_collapse (parser, clauses);
13802 c_name = "collapse";
13803 break;
13804 case PRAGMA_OACC_CLAUSE_COPY:
13805 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13806 c_name = "copy";
13807 break;
13808 case PRAGMA_OACC_CLAUSE_COPYIN:
13809 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13810 c_name = "copyin";
13811 break;
13812 case PRAGMA_OACC_CLAUSE_COPYOUT:
13813 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13814 c_name = "copyout";
13815 break;
13816 case PRAGMA_OACC_CLAUSE_CREATE:
13817 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13818 c_name = "create";
13819 break;
13820 case PRAGMA_OACC_CLAUSE_DELETE:
13821 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13822 c_name = "delete";
13823 break;
13824 case PRAGMA_OMP_CLAUSE_DEFAULT:
13825 clauses = c_parser_omp_clause_default (parser, clauses, true);
13826 c_name = "default";
13827 break;
13828 case PRAGMA_OACC_CLAUSE_DEVICE:
13829 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13830 c_name = "device";
13831 break;
13832 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13833 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13834 c_name = "deviceptr";
13835 break;
13836 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13837 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13838 c_name = "device_resident";
13839 break;
13840 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13841 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13842 c_name = "firstprivate";
13843 break;
13844 case PRAGMA_OACC_CLAUSE_GANG:
13845 c_name = "gang";
13846 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13847 c_name, clauses);
13848 break;
13849 case PRAGMA_OACC_CLAUSE_HOST:
13850 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13851 c_name = "host";
13852 break;
13853 case PRAGMA_OACC_CLAUSE_IF:
13854 clauses = c_parser_omp_clause_if (parser, clauses, false);
13855 c_name = "if";
13856 break;
13857 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13858 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13859 clauses);
13860 c_name = "independent";
13861 break;
13862 case PRAGMA_OACC_CLAUSE_LINK:
13863 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13864 c_name = "link";
13865 break;
13866 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13867 clauses = c_parser_oacc_single_int_clause (parser,
13868 OMP_CLAUSE_NUM_GANGS,
13869 clauses);
13870 c_name = "num_gangs";
13871 break;
13872 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13873 clauses = c_parser_oacc_single_int_clause (parser,
13874 OMP_CLAUSE_NUM_WORKERS,
13875 clauses);
13876 c_name = "num_workers";
13877 break;
13878 case PRAGMA_OACC_CLAUSE_PRESENT:
13879 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13880 c_name = "present";
13881 break;
13882 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13883 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13884 c_name = "present_or_copy";
13885 break;
13886 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13887 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13888 c_name = "present_or_copyin";
13889 break;
13890 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13891 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13892 c_name = "present_or_copyout";
13893 break;
13894 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13895 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13896 c_name = "present_or_create";
13897 break;
13898 case PRAGMA_OACC_CLAUSE_PRIVATE:
13899 clauses = c_parser_omp_clause_private (parser, clauses);
13900 c_name = "private";
13901 break;
13902 case PRAGMA_OACC_CLAUSE_REDUCTION:
13903 clauses = c_parser_omp_clause_reduction (parser, clauses);
13904 c_name = "reduction";
13905 break;
13906 case PRAGMA_OACC_CLAUSE_SELF:
13907 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13908 c_name = "self";
13909 break;
13910 case PRAGMA_OACC_CLAUSE_SEQ:
13911 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13912 clauses);
13913 c_name = "seq";
13914 break;
13915 case PRAGMA_OACC_CLAUSE_TILE:
13916 clauses = c_parser_oacc_clause_tile (parser, clauses);
13917 c_name = "tile";
13918 break;
13919 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13920 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13921 c_name = "use_device";
13922 break;
13923 case PRAGMA_OACC_CLAUSE_VECTOR:
13924 c_name = "vector";
13925 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13926 c_name, clauses);
13927 break;
13928 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13929 clauses = c_parser_oacc_single_int_clause (parser,
13930 OMP_CLAUSE_VECTOR_LENGTH,
13931 clauses);
13932 c_name = "vector_length";
13933 break;
13934 case PRAGMA_OACC_CLAUSE_WAIT:
13935 clauses = c_parser_oacc_clause_wait (parser, clauses);
13936 c_name = "wait";
13937 break;
13938 case PRAGMA_OACC_CLAUSE_WORKER:
13939 c_name = "worker";
13940 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13941 c_name, clauses);
13942 break;
13943 default:
13944 c_parser_error (parser, "expected %<#pragma acc%> clause");
13945 goto saw_error;
13948 first = false;
13950 if (((mask >> c_kind) & 1) == 0)
13952 /* Remove the invalid clause(s) from the list to avoid
13953 confusing the rest of the compiler. */
13954 clauses = prev;
13955 error_at (here, "%qs is not valid for %qs", c_name, where);
13959 saw_error:
13960 c_parser_skip_to_pragma_eol (parser);
13962 if (finish_p)
13963 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13965 return clauses;
13968 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13969 is a bitmask in MASK. Return the list of clauses found. */
13971 static tree
13972 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13973 const char *where, bool finish_p = true)
13975 tree clauses = NULL;
13976 bool first = true;
13978 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13980 location_t here;
13981 pragma_omp_clause c_kind;
13982 const char *c_name;
13983 tree prev = clauses;
13985 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13986 c_parser_consume_token (parser);
13988 here = c_parser_peek_token (parser)->location;
13989 c_kind = c_parser_omp_clause_name (parser);
13991 switch (c_kind)
13993 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13994 clauses = c_parser_omp_clause_collapse (parser, clauses);
13995 c_name = "collapse";
13996 break;
13997 case PRAGMA_OMP_CLAUSE_COPYIN:
13998 clauses = c_parser_omp_clause_copyin (parser, clauses);
13999 c_name = "copyin";
14000 break;
14001 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14002 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14003 c_name = "copyprivate";
14004 break;
14005 case PRAGMA_OMP_CLAUSE_DEFAULT:
14006 clauses = c_parser_omp_clause_default (parser, clauses, false);
14007 c_name = "default";
14008 break;
14009 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14010 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14011 c_name = "firstprivate";
14012 break;
14013 case PRAGMA_OMP_CLAUSE_FINAL:
14014 clauses = c_parser_omp_clause_final (parser, clauses);
14015 c_name = "final";
14016 break;
14017 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14018 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14019 c_name = "grainsize";
14020 break;
14021 case PRAGMA_OMP_CLAUSE_HINT:
14022 clauses = c_parser_omp_clause_hint (parser, clauses);
14023 c_name = "hint";
14024 break;
14025 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14026 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14027 c_name = "defaultmap";
14028 break;
14029 case PRAGMA_OMP_CLAUSE_IF:
14030 clauses = c_parser_omp_clause_if (parser, clauses, true);
14031 c_name = "if";
14032 break;
14033 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14034 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14035 c_name = "lastprivate";
14036 break;
14037 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14038 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14039 c_name = "mergeable";
14040 break;
14041 case PRAGMA_OMP_CLAUSE_NOWAIT:
14042 clauses = c_parser_omp_clause_nowait (parser, clauses);
14043 c_name = "nowait";
14044 break;
14045 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14046 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14047 c_name = "num_tasks";
14048 break;
14049 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14050 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14051 c_name = "num_threads";
14052 break;
14053 case PRAGMA_OMP_CLAUSE_ORDERED:
14054 clauses = c_parser_omp_clause_ordered (parser, clauses);
14055 c_name = "ordered";
14056 break;
14057 case PRAGMA_OMP_CLAUSE_PRIORITY:
14058 clauses = c_parser_omp_clause_priority (parser, clauses);
14059 c_name = "priority";
14060 break;
14061 case PRAGMA_OMP_CLAUSE_PRIVATE:
14062 clauses = c_parser_omp_clause_private (parser, clauses);
14063 c_name = "private";
14064 break;
14065 case PRAGMA_OMP_CLAUSE_REDUCTION:
14066 clauses = c_parser_omp_clause_reduction (parser, clauses);
14067 c_name = "reduction";
14068 break;
14069 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14070 clauses = c_parser_omp_clause_schedule (parser, clauses);
14071 c_name = "schedule";
14072 break;
14073 case PRAGMA_OMP_CLAUSE_SHARED:
14074 clauses = c_parser_omp_clause_shared (parser, clauses);
14075 c_name = "shared";
14076 break;
14077 case PRAGMA_OMP_CLAUSE_UNTIED:
14078 clauses = c_parser_omp_clause_untied (parser, clauses);
14079 c_name = "untied";
14080 break;
14081 case PRAGMA_OMP_CLAUSE_INBRANCH:
14082 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14083 clauses);
14084 c_name = "inbranch";
14085 break;
14086 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14087 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14088 clauses);
14089 c_name = "notinbranch";
14090 break;
14091 case PRAGMA_OMP_CLAUSE_PARALLEL:
14092 clauses
14093 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14094 clauses);
14095 c_name = "parallel";
14096 if (!first)
14098 clause_not_first:
14099 error_at (here, "%qs must be the first clause of %qs",
14100 c_name, where);
14101 clauses = prev;
14103 break;
14104 case PRAGMA_OMP_CLAUSE_FOR:
14105 clauses
14106 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14107 clauses);
14108 c_name = "for";
14109 if (!first)
14110 goto clause_not_first;
14111 break;
14112 case PRAGMA_OMP_CLAUSE_SECTIONS:
14113 clauses
14114 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14115 clauses);
14116 c_name = "sections";
14117 if (!first)
14118 goto clause_not_first;
14119 break;
14120 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14121 clauses
14122 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14123 clauses);
14124 c_name = "taskgroup";
14125 if (!first)
14126 goto clause_not_first;
14127 break;
14128 case PRAGMA_OMP_CLAUSE_LINK:
14129 clauses
14130 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14131 c_name = "link";
14132 break;
14133 case PRAGMA_OMP_CLAUSE_TO:
14134 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14135 clauses
14136 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14137 clauses);
14138 else
14139 clauses = c_parser_omp_clause_to (parser, clauses);
14140 c_name = "to";
14141 break;
14142 case PRAGMA_OMP_CLAUSE_FROM:
14143 clauses = c_parser_omp_clause_from (parser, clauses);
14144 c_name = "from";
14145 break;
14146 case PRAGMA_OMP_CLAUSE_UNIFORM:
14147 clauses = c_parser_omp_clause_uniform (parser, clauses);
14148 c_name = "uniform";
14149 break;
14150 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14151 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14152 c_name = "num_teams";
14153 break;
14154 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14155 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14156 c_name = "thread_limit";
14157 break;
14158 case PRAGMA_OMP_CLAUSE_ALIGNED:
14159 clauses = c_parser_omp_clause_aligned (parser, clauses);
14160 c_name = "aligned";
14161 break;
14162 case PRAGMA_OMP_CLAUSE_LINEAR:
14163 clauses = c_parser_omp_clause_linear (parser, clauses);
14164 c_name = "linear";
14165 break;
14166 case PRAGMA_OMP_CLAUSE_DEPEND:
14167 clauses = c_parser_omp_clause_depend (parser, clauses);
14168 c_name = "depend";
14169 break;
14170 case PRAGMA_OMP_CLAUSE_MAP:
14171 clauses = c_parser_omp_clause_map (parser, clauses);
14172 c_name = "map";
14173 break;
14174 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14175 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14176 c_name = "use_device_ptr";
14177 break;
14178 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14179 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14180 c_name = "is_device_ptr";
14181 break;
14182 case PRAGMA_OMP_CLAUSE_DEVICE:
14183 clauses = c_parser_omp_clause_device (parser, clauses);
14184 c_name = "device";
14185 break;
14186 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14187 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14188 c_name = "dist_schedule";
14189 break;
14190 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14191 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14192 c_name = "proc_bind";
14193 break;
14194 case PRAGMA_OMP_CLAUSE_SAFELEN:
14195 clauses = c_parser_omp_clause_safelen (parser, clauses);
14196 c_name = "safelen";
14197 break;
14198 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14199 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14200 c_name = "simdlen";
14201 break;
14202 case PRAGMA_OMP_CLAUSE_NOGROUP:
14203 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14204 c_name = "nogroup";
14205 break;
14206 case PRAGMA_OMP_CLAUSE_THREADS:
14207 clauses
14208 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14209 clauses);
14210 c_name = "threads";
14211 break;
14212 case PRAGMA_OMP_CLAUSE_SIMD:
14213 clauses
14214 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14215 clauses);
14216 c_name = "simd";
14217 break;
14218 default:
14219 c_parser_error (parser, "expected %<#pragma omp%> clause");
14220 goto saw_error;
14223 first = false;
14225 if (((mask >> c_kind) & 1) == 0)
14227 /* Remove the invalid clause(s) from the list to avoid
14228 confusing the rest of the compiler. */
14229 clauses = prev;
14230 error_at (here, "%qs is not valid for %qs", c_name, where);
14234 saw_error:
14235 c_parser_skip_to_pragma_eol (parser);
14237 if (finish_p)
14239 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14240 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14241 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14244 return clauses;
14247 /* OpenACC 2.0, OpenMP 2.5:
14248 structured-block:
14249 statement
14251 In practice, we're also interested in adding the statement to an
14252 outer node. So it is convenient if we work around the fact that
14253 c_parser_statement calls add_stmt. */
14255 static tree
14256 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14258 tree stmt = push_stmt_list ();
14259 c_parser_statement (parser, if_p);
14260 return pop_stmt_list (stmt);
14263 /* OpenACC 2.0:
14264 # pragma acc cache (variable-list) new-line
14266 LOC is the location of the #pragma token.
14269 static tree
14270 c_parser_oacc_cache (location_t loc, c_parser *parser)
14272 tree stmt, clauses;
14274 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14275 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14277 c_parser_skip_to_pragma_eol (parser);
14279 stmt = make_node (OACC_CACHE);
14280 TREE_TYPE (stmt) = void_type_node;
14281 OACC_CACHE_CLAUSES (stmt) = clauses;
14282 SET_EXPR_LOCATION (stmt, loc);
14283 add_stmt (stmt);
14285 return stmt;
14288 /* OpenACC 2.0:
14289 # pragma acc data oacc-data-clause[optseq] new-line
14290 structured-block
14292 LOC is the location of the #pragma token.
14295 #define OACC_DATA_CLAUSE_MASK \
14296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14308 static tree
14309 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14311 tree stmt, clauses, block;
14313 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14314 "#pragma acc data");
14316 block = c_begin_omp_parallel ();
14317 add_stmt (c_parser_omp_structured_block (parser, if_p));
14319 stmt = c_finish_oacc_data (loc, clauses, block);
14321 return stmt;
14324 /* OpenACC 2.0:
14325 # pragma acc declare oacc-data-clause[optseq] new-line
14328 #define OACC_DECLARE_CLAUSE_MASK \
14329 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14342 static void
14343 c_parser_oacc_declare (c_parser *parser)
14345 location_t pragma_loc = c_parser_peek_token (parser)->location;
14346 tree clauses, stmt, t, decl;
14348 bool error = false;
14350 c_parser_consume_pragma (parser);
14352 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14353 "#pragma acc declare");
14354 if (!clauses)
14356 error_at (pragma_loc,
14357 "no valid clauses specified in %<#pragma acc declare%>");
14358 return;
14361 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14363 location_t loc = OMP_CLAUSE_LOCATION (t);
14364 decl = OMP_CLAUSE_DECL (t);
14365 if (!DECL_P (decl))
14367 error_at (loc, "array section in %<#pragma acc declare%>");
14368 error = true;
14369 continue;
14372 switch (OMP_CLAUSE_MAP_KIND (t))
14374 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14375 case GOMP_MAP_FORCE_ALLOC:
14376 case GOMP_MAP_FORCE_TO:
14377 case GOMP_MAP_FORCE_DEVICEPTR:
14378 case GOMP_MAP_DEVICE_RESIDENT:
14379 break;
14381 case GOMP_MAP_LINK:
14382 if (!global_bindings_p ()
14383 && (TREE_STATIC (decl)
14384 || !DECL_EXTERNAL (decl)))
14386 error_at (loc,
14387 "%qD must be a global variable in "
14388 "%<#pragma acc declare link%>",
14389 decl);
14390 error = true;
14391 continue;
14393 break;
14395 default:
14396 if (global_bindings_p ())
14398 error_at (loc, "invalid OpenACC clause at file scope");
14399 error = true;
14400 continue;
14402 if (DECL_EXTERNAL (decl))
14404 error_at (loc,
14405 "invalid use of %<extern%> variable %qD "
14406 "in %<#pragma acc declare%>", decl);
14407 error = true;
14408 continue;
14410 else if (TREE_PUBLIC (decl))
14412 error_at (loc,
14413 "invalid use of %<global%> variable %qD "
14414 "in %<#pragma acc declare%>", decl);
14415 error = true;
14416 continue;
14418 break;
14421 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14422 || lookup_attribute ("omp declare target link",
14423 DECL_ATTRIBUTES (decl)))
14425 error_at (loc, "variable %qD used more than once with "
14426 "%<#pragma acc declare%>", decl);
14427 error = true;
14428 continue;
14431 if (!error)
14433 tree id;
14435 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14436 id = get_identifier ("omp declare target link");
14437 else
14438 id = get_identifier ("omp declare target");
14440 DECL_ATTRIBUTES (decl)
14441 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14443 if (global_bindings_p ())
14445 symtab_node *node = symtab_node::get (decl);
14446 if (node != NULL)
14448 node->offloadable = 1;
14449 if (ENABLE_OFFLOADING)
14451 g->have_offload = true;
14452 if (is_a <varpool_node *> (node))
14453 vec_safe_push (offload_vars, decl);
14460 if (error || global_bindings_p ())
14461 return;
14463 stmt = make_node (OACC_DECLARE);
14464 TREE_TYPE (stmt) = void_type_node;
14465 OACC_DECLARE_CLAUSES (stmt) = clauses;
14466 SET_EXPR_LOCATION (stmt, pragma_loc);
14468 add_stmt (stmt);
14470 return;
14473 /* OpenACC 2.0:
14474 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14478 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14481 LOC is the location of the #pragma token.
14484 #define OACC_ENTER_DATA_CLAUSE_MASK \
14485 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14493 #define OACC_EXIT_DATA_CLAUSE_MASK \
14494 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14500 static void
14501 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14503 location_t loc = c_parser_peek_token (parser)->location;
14504 tree clauses, stmt;
14505 const char *p = "";
14507 c_parser_consume_pragma (parser);
14509 if (c_parser_next_token_is (parser, CPP_NAME))
14511 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14512 c_parser_consume_token (parser);
14515 if (strcmp (p, "data") != 0)
14517 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14518 enter ? "enter" : "exit");
14519 parser->error = true;
14520 c_parser_skip_to_pragma_eol (parser);
14521 return;
14524 if (enter)
14525 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14526 "#pragma acc enter data");
14527 else
14528 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14529 "#pragma acc exit data");
14531 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14533 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14534 enter ? "enter" : "exit");
14535 return;
14538 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14539 TREE_TYPE (stmt) = void_type_node;
14540 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14541 SET_EXPR_LOCATION (stmt, loc);
14542 add_stmt (stmt);
14546 /* OpenACC 2.0:
14547 # pragma acc host_data oacc-data-clause[optseq] new-line
14548 structured-block
14551 #define OACC_HOST_DATA_CLAUSE_MASK \
14552 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14554 static tree
14555 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14557 tree stmt, clauses, block;
14559 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14560 "#pragma acc host_data");
14562 block = c_begin_omp_parallel ();
14563 add_stmt (c_parser_omp_structured_block (parser, if_p));
14564 stmt = c_finish_oacc_host_data (loc, clauses, block);
14565 return stmt;
14569 /* OpenACC 2.0:
14571 # pragma acc loop oacc-loop-clause[optseq] new-line
14572 structured-block
14574 LOC is the location of the #pragma token.
14577 #define OACC_LOOP_CLAUSE_MASK \
14578 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14588 static tree
14589 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14590 omp_clause_mask mask, tree *cclauses, bool *if_p)
14592 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14594 strcat (p_name, " loop");
14595 mask |= OACC_LOOP_CLAUSE_MASK;
14597 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14598 cclauses == NULL);
14599 if (cclauses)
14601 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14602 if (*cclauses)
14603 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14604 if (clauses)
14605 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14608 tree block = c_begin_compound_stmt (true);
14609 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14610 if_p);
14611 block = c_end_compound_stmt (loc, block, true);
14612 add_stmt (block);
14614 return stmt;
14617 /* OpenACC 2.0:
14618 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14619 structured-block
14623 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14624 structured-block
14626 LOC is the location of the #pragma token.
14629 #define OACC_KERNELS_CLAUSE_MASK \
14630 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14648 #define OACC_PARALLEL_CLAUSE_MASK \
14649 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14670 static tree
14671 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14672 enum pragma_kind p_kind, char *p_name,
14673 bool *if_p)
14675 omp_clause_mask mask;
14676 enum tree_code code;
14677 switch (p_kind)
14679 case PRAGMA_OACC_KERNELS:
14680 strcat (p_name, " kernels");
14681 mask = OACC_KERNELS_CLAUSE_MASK;
14682 code = OACC_KERNELS;
14683 break;
14684 case PRAGMA_OACC_PARALLEL:
14685 strcat (p_name, " parallel");
14686 mask = OACC_PARALLEL_CLAUSE_MASK;
14687 code = OACC_PARALLEL;
14688 break;
14689 default:
14690 gcc_unreachable ();
14693 if (c_parser_next_token_is (parser, CPP_NAME))
14695 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14696 if (strcmp (p, "loop") == 0)
14698 c_parser_consume_token (parser);
14699 tree block = c_begin_omp_parallel ();
14700 tree clauses;
14701 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14702 return c_finish_omp_construct (loc, code, block, clauses);
14706 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14708 tree block = c_begin_omp_parallel ();
14709 add_stmt (c_parser_omp_structured_block (parser, if_p));
14711 return c_finish_omp_construct (loc, code, block, clauses);
14714 /* OpenACC 2.0:
14715 # pragma acc routine oacc-routine-clause[optseq] new-line
14716 function-definition
14718 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14721 #define OACC_ROUTINE_CLAUSE_MASK \
14722 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14727 /* Parse an OpenACC routine directive. For named directives, we apply
14728 immediately to the named function. For unnamed ones we then parse
14729 a declaration or definition, which must be for a function. */
14731 static void
14732 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14734 gcc_checking_assert (context == pragma_external);
14736 oacc_routine_data data;
14737 data.error_seen = false;
14738 data.fndecl_seen = false;
14739 data.clauses = NULL_TREE;
14740 data.loc = c_parser_peek_token (parser)->location;
14742 c_parser_consume_pragma (parser);
14744 /* Look for optional '( name )'. */
14745 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14747 c_parser_consume_token (parser); /* '(' */
14749 tree decl = NULL_TREE;
14750 c_token *name_token = c_parser_peek_token (parser);
14751 location_t name_loc = name_token->location;
14752 if (name_token->type == CPP_NAME
14753 && (name_token->id_kind == C_ID_ID
14754 || name_token->id_kind == C_ID_TYPENAME))
14756 decl = lookup_name (name_token->value);
14757 if (!decl)
14758 error_at (name_loc,
14759 "%qE has not been declared", name_token->value);
14760 c_parser_consume_token (parser);
14762 else
14763 c_parser_error (parser, "expected function name");
14765 if (!decl
14766 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14768 c_parser_skip_to_pragma_eol (parser, false);
14769 return;
14772 data.clauses
14773 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14774 "#pragma acc routine");
14776 if (TREE_CODE (decl) != FUNCTION_DECL)
14778 error_at (name_loc, "%qD does not refer to a function", decl);
14779 return;
14782 c_finish_oacc_routine (&data, decl, false);
14784 else /* No optional '( name )'. */
14786 data.clauses
14787 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14788 "#pragma acc routine");
14790 /* Emit a helpful diagnostic if there's another pragma following this
14791 one. Also don't allow a static assertion declaration, as in the
14792 following we'll just parse a *single* "declaration or function
14793 definition", and the static assertion counts an one. */
14794 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14795 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14797 error_at (data.loc,
14798 "%<#pragma acc routine%> not immediately followed by"
14799 " function declaration or definition");
14800 /* ..., and then just keep going. */
14801 return;
14804 /* We only have to consider the pragma_external case here. */
14805 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14806 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14808 int ext = disable_extension_diagnostics ();
14810 c_parser_consume_token (parser);
14811 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14812 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14813 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14814 NULL, vNULL, &data);
14815 restore_extension_diagnostics (ext);
14817 else
14818 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14819 NULL, vNULL, &data);
14823 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14824 IS_DEFN is true if we're applying it to the definition. */
14826 static void
14827 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14828 bool is_defn)
14830 /* Keep going if we're in error reporting mode. */
14831 if (data->error_seen
14832 || fndecl == error_mark_node)
14833 return;
14835 if (data->fndecl_seen)
14837 error_at (data->loc,
14838 "%<#pragma acc routine%> not immediately followed by"
14839 " a single function declaration or definition");
14840 data->error_seen = true;
14841 return;
14843 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14845 error_at (data->loc,
14846 "%<#pragma acc routine%> not immediately followed by"
14847 " function declaration or definition");
14848 data->error_seen = true;
14849 return;
14852 if (oacc_get_fn_attrib (fndecl))
14854 error_at (data->loc,
14855 "%<#pragma acc routine%> already applied to %qD", fndecl);
14856 data->error_seen = true;
14857 return;
14860 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14862 error_at (data->loc,
14863 TREE_USED (fndecl)
14864 ? G_("%<#pragma acc routine%> must be applied before use")
14865 : G_("%<#pragma acc routine%> must be applied before "
14866 "definition"));
14867 data->error_seen = true;
14868 return;
14871 /* Process the routine's dimension clauses. */
14872 tree dims = oacc_build_routine_dims (data->clauses);
14873 oacc_replace_fn_attrib (fndecl, dims);
14875 /* Add an "omp declare target" attribute. */
14876 DECL_ATTRIBUTES (fndecl)
14877 = tree_cons (get_identifier ("omp declare target"),
14878 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14880 /* Remember that we've used this "#pragma acc routine". */
14881 data->fndecl_seen = true;
14884 /* OpenACC 2.0:
14885 # pragma acc update oacc-update-clause[optseq] new-line
14888 #define OACC_UPDATE_CLAUSE_MASK \
14889 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14896 static void
14897 c_parser_oacc_update (c_parser *parser)
14899 location_t loc = c_parser_peek_token (parser)->location;
14901 c_parser_consume_pragma (parser);
14903 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14904 "#pragma acc update");
14905 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14907 error_at (loc,
14908 "%<#pragma acc update%> must contain at least one "
14909 "%<device%> or %<host%> or %<self%> clause");
14910 return;
14913 if (parser->error)
14914 return;
14916 tree stmt = make_node (OACC_UPDATE);
14917 TREE_TYPE (stmt) = void_type_node;
14918 OACC_UPDATE_CLAUSES (stmt) = clauses;
14919 SET_EXPR_LOCATION (stmt, loc);
14920 add_stmt (stmt);
14923 /* OpenACC 2.0:
14924 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14926 LOC is the location of the #pragma token.
14929 #define OACC_WAIT_CLAUSE_MASK \
14930 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14932 static tree
14933 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14935 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14937 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14938 list = c_parser_oacc_wait_list (parser, loc, list);
14940 strcpy (p_name, " wait");
14941 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14942 stmt = c_finish_oacc_wait (loc, list, clauses);
14943 add_stmt (stmt);
14945 return stmt;
14948 /* OpenMP 2.5:
14949 # pragma omp atomic new-line
14950 expression-stmt
14952 expression-stmt:
14953 x binop= expr | x++ | ++x | x-- | --x
14954 binop:
14955 +, *, -, /, &, ^, |, <<, >>
14957 where x is an lvalue expression with scalar type.
14959 OpenMP 3.1:
14960 # pragma omp atomic new-line
14961 update-stmt
14963 # pragma omp atomic read new-line
14964 read-stmt
14966 # pragma omp atomic write new-line
14967 write-stmt
14969 # pragma omp atomic update new-line
14970 update-stmt
14972 # pragma omp atomic capture new-line
14973 capture-stmt
14975 # pragma omp atomic capture new-line
14976 capture-block
14978 read-stmt:
14979 v = x
14980 write-stmt:
14981 x = expr
14982 update-stmt:
14983 expression-stmt | x = x binop expr
14984 capture-stmt:
14985 v = expression-stmt
14986 capture-block:
14987 { v = x; update-stmt; } | { update-stmt; v = x; }
14989 OpenMP 4.0:
14990 update-stmt:
14991 expression-stmt | x = x binop expr | x = expr binop x
14992 capture-stmt:
14993 v = update-stmt
14994 capture-block:
14995 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14997 where x and v are lvalue expressions with scalar type.
14999 LOC is the location of the #pragma token. */
15001 static void
15002 c_parser_omp_atomic (location_t loc, c_parser *parser)
15004 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15005 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15006 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15007 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15008 struct c_expr expr;
15009 location_t eloc;
15010 bool structured_block = false;
15011 bool swapped = false;
15012 bool seq_cst = false;
15013 bool non_lvalue_p;
15015 if (c_parser_next_token_is (parser, CPP_NAME))
15017 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15018 if (!strcmp (p, "seq_cst"))
15020 seq_cst = true;
15021 c_parser_consume_token (parser);
15022 if (c_parser_next_token_is (parser, CPP_COMMA)
15023 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15024 c_parser_consume_token (parser);
15027 if (c_parser_next_token_is (parser, CPP_NAME))
15029 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15031 if (!strcmp (p, "read"))
15032 code = OMP_ATOMIC_READ;
15033 else if (!strcmp (p, "write"))
15034 code = NOP_EXPR;
15035 else if (!strcmp (p, "update"))
15036 code = OMP_ATOMIC;
15037 else if (!strcmp (p, "capture"))
15038 code = OMP_ATOMIC_CAPTURE_NEW;
15039 else
15040 p = NULL;
15041 if (p)
15042 c_parser_consume_token (parser);
15044 if (!seq_cst)
15046 if (c_parser_next_token_is (parser, CPP_COMMA)
15047 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15048 c_parser_consume_token (parser);
15050 if (c_parser_next_token_is (parser, CPP_NAME))
15052 const char *p
15053 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15054 if (!strcmp (p, "seq_cst"))
15056 seq_cst = true;
15057 c_parser_consume_token (parser);
15061 c_parser_skip_to_pragma_eol (parser);
15063 switch (code)
15065 case OMP_ATOMIC_READ:
15066 case NOP_EXPR: /* atomic write */
15067 v = c_parser_cast_expression (parser, NULL).value;
15068 non_lvalue_p = !lvalue_p (v);
15069 v = c_fully_fold (v, false, NULL, true);
15070 if (v == error_mark_node)
15071 goto saw_error;
15072 if (non_lvalue_p)
15073 v = non_lvalue (v);
15074 loc = c_parser_peek_token (parser)->location;
15075 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15076 goto saw_error;
15077 if (code == NOP_EXPR)
15079 lhs = c_parser_expression (parser).value;
15080 lhs = c_fully_fold (lhs, false, NULL);
15081 if (lhs == error_mark_node)
15082 goto saw_error;
15084 else
15086 lhs = c_parser_cast_expression (parser, NULL).value;
15087 non_lvalue_p = !lvalue_p (lhs);
15088 lhs = c_fully_fold (lhs, false, NULL, true);
15089 if (lhs == error_mark_node)
15090 goto saw_error;
15091 if (non_lvalue_p)
15092 lhs = non_lvalue (lhs);
15094 if (code == NOP_EXPR)
15096 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15097 opcode. */
15098 code = OMP_ATOMIC;
15099 rhs = lhs;
15100 lhs = v;
15101 v = NULL_TREE;
15103 goto done;
15104 case OMP_ATOMIC_CAPTURE_NEW:
15105 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15107 c_parser_consume_token (parser);
15108 structured_block = true;
15110 else
15112 v = c_parser_cast_expression (parser, NULL).value;
15113 non_lvalue_p = !lvalue_p (v);
15114 v = c_fully_fold (v, false, NULL, true);
15115 if (v == error_mark_node)
15116 goto saw_error;
15117 if (non_lvalue_p)
15118 v = non_lvalue (v);
15119 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15120 goto saw_error;
15122 break;
15123 default:
15124 break;
15127 /* For structured_block case we don't know yet whether
15128 old or new x should be captured. */
15129 restart:
15130 eloc = c_parser_peek_token (parser)->location;
15131 expr = c_parser_cast_expression (parser, NULL);
15132 lhs = expr.value;
15133 expr = default_function_array_conversion (eloc, expr);
15134 unfolded_lhs = expr.value;
15135 lhs = c_fully_fold (lhs, false, NULL, true);
15136 orig_lhs = lhs;
15137 switch (TREE_CODE (lhs))
15139 case ERROR_MARK:
15140 saw_error:
15141 c_parser_skip_to_end_of_block_or_statement (parser);
15142 if (structured_block)
15144 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15145 c_parser_consume_token (parser);
15146 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15148 c_parser_skip_to_end_of_block_or_statement (parser);
15149 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15150 c_parser_consume_token (parser);
15153 return;
15155 case POSTINCREMENT_EXPR:
15156 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15157 code = OMP_ATOMIC_CAPTURE_OLD;
15158 /* FALLTHROUGH */
15159 case PREINCREMENT_EXPR:
15160 lhs = TREE_OPERAND (lhs, 0);
15161 unfolded_lhs = NULL_TREE;
15162 opcode = PLUS_EXPR;
15163 rhs = integer_one_node;
15164 break;
15166 case POSTDECREMENT_EXPR:
15167 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15168 code = OMP_ATOMIC_CAPTURE_OLD;
15169 /* FALLTHROUGH */
15170 case PREDECREMENT_EXPR:
15171 lhs = TREE_OPERAND (lhs, 0);
15172 unfolded_lhs = NULL_TREE;
15173 opcode = MINUS_EXPR;
15174 rhs = integer_one_node;
15175 break;
15177 case COMPOUND_EXPR:
15178 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15179 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15180 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15181 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15182 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15183 (TREE_OPERAND (lhs, 1), 0), 0)))
15184 == BOOLEAN_TYPE)
15185 /* Undo effects of boolean_increment for post {in,de}crement. */
15186 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15187 /* FALLTHRU */
15188 case MODIFY_EXPR:
15189 if (TREE_CODE (lhs) == MODIFY_EXPR
15190 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15192 /* Undo effects of boolean_increment. */
15193 if (integer_onep (TREE_OPERAND (lhs, 1)))
15195 /* This is pre or post increment. */
15196 rhs = TREE_OPERAND (lhs, 1);
15197 lhs = TREE_OPERAND (lhs, 0);
15198 unfolded_lhs = NULL_TREE;
15199 opcode = NOP_EXPR;
15200 if (code == OMP_ATOMIC_CAPTURE_NEW
15201 && !structured_block
15202 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15203 code = OMP_ATOMIC_CAPTURE_OLD;
15204 break;
15206 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15207 && TREE_OPERAND (lhs, 0)
15208 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15210 /* This is pre or post decrement. */
15211 rhs = TREE_OPERAND (lhs, 1);
15212 lhs = TREE_OPERAND (lhs, 0);
15213 unfolded_lhs = NULL_TREE;
15214 opcode = NOP_EXPR;
15215 if (code == OMP_ATOMIC_CAPTURE_NEW
15216 && !structured_block
15217 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15218 code = OMP_ATOMIC_CAPTURE_OLD;
15219 break;
15222 /* FALLTHRU */
15223 default:
15224 if (!lvalue_p (unfolded_lhs))
15225 lhs = non_lvalue (lhs);
15226 switch (c_parser_peek_token (parser)->type)
15228 case CPP_MULT_EQ:
15229 opcode = MULT_EXPR;
15230 break;
15231 case CPP_DIV_EQ:
15232 opcode = TRUNC_DIV_EXPR;
15233 break;
15234 case CPP_PLUS_EQ:
15235 opcode = PLUS_EXPR;
15236 break;
15237 case CPP_MINUS_EQ:
15238 opcode = MINUS_EXPR;
15239 break;
15240 case CPP_LSHIFT_EQ:
15241 opcode = LSHIFT_EXPR;
15242 break;
15243 case CPP_RSHIFT_EQ:
15244 opcode = RSHIFT_EXPR;
15245 break;
15246 case CPP_AND_EQ:
15247 opcode = BIT_AND_EXPR;
15248 break;
15249 case CPP_OR_EQ:
15250 opcode = BIT_IOR_EXPR;
15251 break;
15252 case CPP_XOR_EQ:
15253 opcode = BIT_XOR_EXPR;
15254 break;
15255 case CPP_EQ:
15256 c_parser_consume_token (parser);
15257 eloc = c_parser_peek_token (parser)->location;
15258 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15259 rhs1 = expr.value;
15260 switch (TREE_CODE (rhs1))
15262 case MULT_EXPR:
15263 case TRUNC_DIV_EXPR:
15264 case RDIV_EXPR:
15265 case PLUS_EXPR:
15266 case MINUS_EXPR:
15267 case LSHIFT_EXPR:
15268 case RSHIFT_EXPR:
15269 case BIT_AND_EXPR:
15270 case BIT_IOR_EXPR:
15271 case BIT_XOR_EXPR:
15272 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15274 opcode = TREE_CODE (rhs1);
15275 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15276 true);
15277 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15278 true);
15279 goto stmt_done;
15281 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15283 opcode = TREE_CODE (rhs1);
15284 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15285 true);
15286 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15287 true);
15288 swapped = !commutative_tree_code (opcode);
15289 goto stmt_done;
15291 break;
15292 case ERROR_MARK:
15293 goto saw_error;
15294 default:
15295 break;
15297 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15299 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15301 code = OMP_ATOMIC_CAPTURE_OLD;
15302 v = lhs;
15303 lhs = NULL_TREE;
15304 expr = default_function_array_read_conversion (eloc, expr);
15305 unfolded_lhs1 = expr.value;
15306 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15307 rhs1 = NULL_TREE;
15308 c_parser_consume_token (parser);
15309 goto restart;
15311 if (structured_block)
15313 opcode = NOP_EXPR;
15314 expr = default_function_array_read_conversion (eloc, expr);
15315 rhs = c_fully_fold (expr.value, false, NULL, true);
15316 rhs1 = NULL_TREE;
15317 goto stmt_done;
15320 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15321 goto saw_error;
15322 default:
15323 c_parser_error (parser,
15324 "invalid operator for %<#pragma omp atomic%>");
15325 goto saw_error;
15328 /* Arrange to pass the location of the assignment operator to
15329 c_finish_omp_atomic. */
15330 loc = c_parser_peek_token (parser)->location;
15331 c_parser_consume_token (parser);
15332 eloc = c_parser_peek_token (parser)->location;
15333 expr = c_parser_expression (parser);
15334 expr = default_function_array_read_conversion (eloc, expr);
15335 rhs = expr.value;
15336 rhs = c_fully_fold (rhs, false, NULL, true);
15337 break;
15339 stmt_done:
15340 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15342 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15343 goto saw_error;
15344 v = c_parser_cast_expression (parser, NULL).value;
15345 non_lvalue_p = !lvalue_p (v);
15346 v = c_fully_fold (v, false, NULL, true);
15347 if (v == error_mark_node)
15348 goto saw_error;
15349 if (non_lvalue_p)
15350 v = non_lvalue (v);
15351 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15352 goto saw_error;
15353 eloc = c_parser_peek_token (parser)->location;
15354 expr = c_parser_cast_expression (parser, NULL);
15355 lhs1 = expr.value;
15356 expr = default_function_array_read_conversion (eloc, expr);
15357 unfolded_lhs1 = expr.value;
15358 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15359 if (lhs1 == error_mark_node)
15360 goto saw_error;
15361 if (!lvalue_p (unfolded_lhs1))
15362 lhs1 = non_lvalue (lhs1);
15364 if (structured_block)
15366 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15367 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15369 done:
15370 if (unfolded_lhs && unfolded_lhs1
15371 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15373 error ("%<#pragma omp atomic capture%> uses two different "
15374 "expressions for memory");
15375 stmt = error_mark_node;
15377 else
15378 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15379 swapped, seq_cst);
15380 if (stmt != error_mark_node)
15381 add_stmt (stmt);
15383 if (!structured_block)
15384 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15388 /* OpenMP 2.5:
15389 # pragma omp barrier new-line
15392 static void
15393 c_parser_omp_barrier (c_parser *parser)
15395 location_t loc = c_parser_peek_token (parser)->location;
15396 c_parser_consume_pragma (parser);
15397 c_parser_skip_to_pragma_eol (parser);
15399 c_finish_omp_barrier (loc);
15402 /* OpenMP 2.5:
15403 # pragma omp critical [(name)] new-line
15404 structured-block
15406 OpenMP 4.5:
15407 # pragma omp critical [(name) [hint(expression)]] new-line
15409 LOC is the location of the #pragma itself. */
15411 #define OMP_CRITICAL_CLAUSE_MASK \
15412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15414 static tree
15415 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15417 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15419 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15421 c_parser_consume_token (parser);
15422 if (c_parser_next_token_is (parser, CPP_NAME))
15424 name = c_parser_peek_token (parser)->value;
15425 c_parser_consume_token (parser);
15426 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15428 else
15429 c_parser_error (parser, "expected identifier");
15431 clauses = c_parser_omp_all_clauses (parser,
15432 OMP_CRITICAL_CLAUSE_MASK,
15433 "#pragma omp critical");
15435 else
15437 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15438 c_parser_error (parser, "expected %<(%> or end of line");
15439 c_parser_skip_to_pragma_eol (parser);
15442 stmt = c_parser_omp_structured_block (parser, if_p);
15443 return c_finish_omp_critical (loc, stmt, name, clauses);
15446 /* OpenMP 2.5:
15447 # pragma omp flush flush-vars[opt] new-line
15449 flush-vars:
15450 ( variable-list ) */
15452 static void
15453 c_parser_omp_flush (c_parser *parser)
15455 location_t loc = c_parser_peek_token (parser)->location;
15456 c_parser_consume_pragma (parser);
15457 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15458 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15459 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15460 c_parser_error (parser, "expected %<(%> or end of line");
15461 c_parser_skip_to_pragma_eol (parser);
15463 c_finish_omp_flush (loc);
15466 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15467 The real trick here is to determine the loop control variable early
15468 so that we can push a new decl if necessary to make it private.
15469 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15470 respectively. */
15472 static tree
15473 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15474 tree clauses, tree *cclauses, bool *if_p)
15476 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15477 tree declv, condv, incrv, initv, ret = NULL_TREE;
15478 tree pre_body = NULL_TREE, this_pre_body;
15479 tree ordered_cl = NULL_TREE;
15480 bool fail = false, open_brace_parsed = false;
15481 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15482 location_t for_loc;
15483 bool tiling = false;
15484 vec<tree, va_gc> *for_block = make_tree_vector ();
15486 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15487 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15488 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15489 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15491 tiling = true;
15492 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15494 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15495 && OMP_CLAUSE_ORDERED_EXPR (cl))
15497 ordered_cl = cl;
15498 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15501 if (ordered && ordered < collapse)
15503 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15504 "%<ordered%> clause parameter is less than %<collapse%>");
15505 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15506 = build_int_cst (NULL_TREE, collapse);
15507 ordered = collapse;
15509 if (ordered)
15511 for (tree *pc = &clauses; *pc; )
15512 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15514 error_at (OMP_CLAUSE_LOCATION (*pc),
15515 "%<linear%> clause may not be specified together "
15516 "with %<ordered%> clause with a parameter");
15517 *pc = OMP_CLAUSE_CHAIN (*pc);
15519 else
15520 pc = &OMP_CLAUSE_CHAIN (*pc);
15523 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15524 count = ordered ? ordered : collapse;
15526 declv = make_tree_vec (count);
15527 initv = make_tree_vec (count);
15528 condv = make_tree_vec (count);
15529 incrv = make_tree_vec (count);
15531 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15533 c_parser_error (parser, "for statement expected");
15534 return NULL;
15536 for_loc = c_parser_peek_token (parser)->location;
15537 c_parser_consume_token (parser);
15539 for (i = 0; i < count; i++)
15541 int bracecount = 0;
15543 matching_parens parens;
15544 if (!parens.require_open (parser))
15545 goto pop_scopes;
15547 /* Parse the initialization declaration or expression. */
15548 if (c_parser_next_tokens_start_declaration (parser))
15550 if (i > 0)
15551 vec_safe_push (for_block, c_begin_compound_stmt (true));
15552 this_pre_body = push_stmt_list ();
15553 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15554 NULL, vNULL);
15555 if (this_pre_body)
15557 this_pre_body = pop_stmt_list (this_pre_body);
15558 if (pre_body)
15560 tree t = pre_body;
15561 pre_body = push_stmt_list ();
15562 add_stmt (t);
15563 add_stmt (this_pre_body);
15564 pre_body = pop_stmt_list (pre_body);
15566 else
15567 pre_body = this_pre_body;
15569 decl = check_for_loop_decls (for_loc, flag_isoc99);
15570 if (decl == NULL)
15571 goto error_init;
15572 if (DECL_INITIAL (decl) == error_mark_node)
15573 decl = error_mark_node;
15574 init = decl;
15576 else if (c_parser_next_token_is (parser, CPP_NAME)
15577 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15579 struct c_expr decl_exp;
15580 struct c_expr init_exp;
15581 location_t init_loc;
15583 decl_exp = c_parser_postfix_expression (parser);
15584 decl = decl_exp.value;
15586 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15588 init_loc = c_parser_peek_token (parser)->location;
15589 init_exp = c_parser_expr_no_commas (parser, NULL);
15590 init_exp = default_function_array_read_conversion (init_loc,
15591 init_exp);
15592 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15593 NOP_EXPR, init_loc, init_exp.value,
15594 init_exp.original_type);
15595 init = c_process_expr_stmt (init_loc, init);
15597 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15599 else
15601 error_init:
15602 c_parser_error (parser,
15603 "expected iteration declaration or initialization");
15604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15605 "expected %<)%>");
15606 fail = true;
15607 goto parse_next;
15610 /* Parse the loop condition. */
15611 cond = NULL_TREE;
15612 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15614 location_t cond_loc = c_parser_peek_token (parser)->location;
15615 struct c_expr cond_expr
15616 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15618 cond = cond_expr.value;
15619 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15620 if (COMPARISON_CLASS_P (cond))
15622 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15623 op0 = c_fully_fold (op0, false, NULL);
15624 op1 = c_fully_fold (op1, false, NULL);
15625 TREE_OPERAND (cond, 0) = op0;
15626 TREE_OPERAND (cond, 1) = op1;
15628 switch (cond_expr.original_code)
15630 case GT_EXPR:
15631 case GE_EXPR:
15632 case LT_EXPR:
15633 case LE_EXPR:
15634 break;
15635 default:
15636 /* Can't be cond = error_mark_node, because we want to preserve
15637 the location until c_finish_omp_for. */
15638 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15639 break;
15641 protected_set_expr_location (cond, cond_loc);
15643 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15645 /* Parse the increment expression. */
15646 incr = NULL_TREE;
15647 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15649 location_t incr_loc = c_parser_peek_token (parser)->location;
15651 incr = c_process_expr_stmt (incr_loc,
15652 c_parser_expression (parser).value);
15654 parens.skip_until_found_close (parser);
15656 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15657 fail = true;
15658 else
15660 TREE_VEC_ELT (declv, i) = decl;
15661 TREE_VEC_ELT (initv, i) = init;
15662 TREE_VEC_ELT (condv, i) = cond;
15663 TREE_VEC_ELT (incrv, i) = incr;
15666 parse_next:
15667 if (i == count - 1)
15668 break;
15670 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15671 in between the collapsed for loops to be still considered perfectly
15672 nested. Hopefully the final version clarifies this.
15673 For now handle (multiple) {'s and empty statements. */
15676 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15678 c_parser_consume_token (parser);
15679 break;
15681 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15683 c_parser_consume_token (parser);
15684 bracecount++;
15686 else if (bracecount
15687 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15688 c_parser_consume_token (parser);
15689 else
15691 c_parser_error (parser, "not enough perfectly nested loops");
15692 if (bracecount)
15694 open_brace_parsed = true;
15695 bracecount--;
15697 fail = true;
15698 count = 0;
15699 break;
15702 while (1);
15704 nbraces += bracecount;
15707 if (nbraces)
15708 if_p = NULL;
15710 save_break = c_break_label;
15711 c_break_label = size_one_node;
15712 save_cont = c_cont_label;
15713 c_cont_label = NULL_TREE;
15714 body = push_stmt_list ();
15716 if (open_brace_parsed)
15718 location_t here = c_parser_peek_token (parser)->location;
15719 stmt = c_begin_compound_stmt (true);
15720 c_parser_compound_statement_nostart (parser);
15721 add_stmt (c_end_compound_stmt (here, stmt, true));
15723 else
15724 add_stmt (c_parser_c99_block_statement (parser, if_p));
15725 if (c_cont_label)
15727 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15728 SET_EXPR_LOCATION (t, loc);
15729 add_stmt (t);
15732 body = pop_stmt_list (body);
15733 c_break_label = save_break;
15734 c_cont_label = save_cont;
15736 while (nbraces)
15738 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15740 c_parser_consume_token (parser);
15741 nbraces--;
15743 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15744 c_parser_consume_token (parser);
15745 else
15747 c_parser_error (parser, "collapsed loops not perfectly nested");
15748 while (nbraces)
15750 location_t here = c_parser_peek_token (parser)->location;
15751 stmt = c_begin_compound_stmt (true);
15752 add_stmt (body);
15753 c_parser_compound_statement_nostart (parser);
15754 body = c_end_compound_stmt (here, stmt, true);
15755 nbraces--;
15757 goto pop_scopes;
15761 /* Only bother calling c_finish_omp_for if we haven't already generated
15762 an error from the initialization parsing. */
15763 if (!fail)
15765 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15766 incrv, body, pre_body);
15768 /* Check for iterators appearing in lb, b or incr expressions. */
15769 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15770 stmt = NULL_TREE;
15772 if (stmt)
15774 add_stmt (stmt);
15776 if (cclauses != NULL
15777 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15779 tree *c;
15780 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15781 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15782 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15783 c = &OMP_CLAUSE_CHAIN (*c);
15784 else
15786 for (i = 0; i < count; i++)
15787 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15788 break;
15789 if (i == count)
15790 c = &OMP_CLAUSE_CHAIN (*c);
15791 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15793 error_at (loc,
15794 "iteration variable %qD should not be firstprivate",
15795 OMP_CLAUSE_DECL (*c));
15796 *c = OMP_CLAUSE_CHAIN (*c);
15798 else
15800 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15801 tree l = *c;
15802 *c = OMP_CLAUSE_CHAIN (*c);
15803 if (code == OMP_SIMD)
15805 OMP_CLAUSE_CHAIN (l)
15806 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15807 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15809 else
15811 OMP_CLAUSE_CHAIN (l) = clauses;
15812 clauses = l;
15817 OMP_FOR_CLAUSES (stmt) = clauses;
15819 ret = stmt;
15821 pop_scopes:
15822 while (!for_block->is_empty ())
15824 /* FIXME diagnostics: LOC below should be the actual location of
15825 this particular for block. We need to build a list of
15826 locations to go along with FOR_BLOCK. */
15827 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15828 add_stmt (stmt);
15830 release_tree_vector (for_block);
15831 return ret;
15834 /* Helper function for OpenMP parsing, split clauses and call
15835 finish_omp_clauses on each of the set of clauses afterwards. */
15837 static void
15838 omp_split_clauses (location_t loc, enum tree_code code,
15839 omp_clause_mask mask, tree clauses, tree *cclauses)
15841 int i;
15842 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15843 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15844 if (cclauses[i])
15845 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15848 /* OpenMP 4.0:
15849 #pragma omp simd simd-clause[optseq] new-line
15850 for-loop
15852 LOC is the location of the #pragma token.
15855 #define OMP_SIMD_CLAUSE_MASK \
15856 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15865 static tree
15866 c_parser_omp_simd (location_t loc, c_parser *parser,
15867 char *p_name, omp_clause_mask mask, tree *cclauses,
15868 bool *if_p)
15870 tree block, clauses, ret;
15872 strcat (p_name, " simd");
15873 mask |= OMP_SIMD_CLAUSE_MASK;
15875 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15876 if (cclauses)
15878 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15879 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15880 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15881 OMP_CLAUSE_ORDERED);
15882 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15884 error_at (OMP_CLAUSE_LOCATION (c),
15885 "%<ordered%> clause with parameter may not be specified "
15886 "on %qs construct", p_name);
15887 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15891 block = c_begin_compound_stmt (true);
15892 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15893 block = c_end_compound_stmt (loc, block, true);
15894 add_stmt (block);
15896 return ret;
15899 /* OpenMP 2.5:
15900 #pragma omp for for-clause[optseq] new-line
15901 for-loop
15903 OpenMP 4.0:
15904 #pragma omp for simd for-simd-clause[optseq] new-line
15905 for-loop
15907 LOC is the location of the #pragma token.
15910 #define OMP_FOR_CLAUSE_MASK \
15911 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15921 static tree
15922 c_parser_omp_for (location_t loc, c_parser *parser,
15923 char *p_name, omp_clause_mask mask, tree *cclauses,
15924 bool *if_p)
15926 tree block, clauses, ret;
15928 strcat (p_name, " for");
15929 mask |= OMP_FOR_CLAUSE_MASK;
15930 /* parallel for{, simd} disallows nowait clause, but for
15931 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15932 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15933 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15934 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15935 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15936 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15938 if (c_parser_next_token_is (parser, CPP_NAME))
15940 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15942 if (strcmp (p, "simd") == 0)
15944 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15945 if (cclauses == NULL)
15946 cclauses = cclauses_buf;
15948 c_parser_consume_token (parser);
15949 if (!flag_openmp) /* flag_openmp_simd */
15950 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15951 if_p);
15952 block = c_begin_compound_stmt (true);
15953 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15954 block = c_end_compound_stmt (loc, block, true);
15955 if (ret == NULL_TREE)
15956 return ret;
15957 ret = make_node (OMP_FOR);
15958 TREE_TYPE (ret) = void_type_node;
15959 OMP_FOR_BODY (ret) = block;
15960 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15961 SET_EXPR_LOCATION (ret, loc);
15962 add_stmt (ret);
15963 return ret;
15966 if (!flag_openmp) /* flag_openmp_simd */
15968 c_parser_skip_to_pragma_eol (parser, false);
15969 return NULL_TREE;
15972 /* Composite distribute parallel for disallows linear clause. */
15973 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15974 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15976 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15977 if (cclauses)
15979 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15980 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15983 block = c_begin_compound_stmt (true);
15984 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15985 block = c_end_compound_stmt (loc, block, true);
15986 add_stmt (block);
15988 return ret;
15991 /* OpenMP 2.5:
15992 # pragma omp master new-line
15993 structured-block
15995 LOC is the location of the #pragma token.
15998 static tree
15999 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16001 c_parser_skip_to_pragma_eol (parser);
16002 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16003 if_p));
16006 /* OpenMP 2.5:
16007 # pragma omp ordered new-line
16008 structured-block
16010 OpenMP 4.5:
16011 # pragma omp ordered ordered-clauses new-line
16012 structured-block
16014 # pragma omp ordered depend-clauses new-line */
16016 #define OMP_ORDERED_CLAUSE_MASK \
16017 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16020 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16021 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16023 static bool
16024 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16025 bool *if_p)
16027 location_t loc = c_parser_peek_token (parser)->location;
16028 c_parser_consume_pragma (parser);
16030 if (context != pragma_stmt && context != pragma_compound)
16032 c_parser_error (parser, "expected declaration specifiers");
16033 c_parser_skip_to_pragma_eol (parser, false);
16034 return false;
16037 if (c_parser_next_token_is (parser, CPP_NAME))
16039 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16041 if (!strcmp ("depend", p))
16043 if (!flag_openmp) /* flag_openmp_simd */
16045 c_parser_skip_to_pragma_eol (parser, false);
16046 return false;
16048 if (context == pragma_stmt)
16050 error_at (loc,
16051 "%<#pragma omp ordered%> with %<depend%> clause may "
16052 "only be used in compound statements");
16053 c_parser_skip_to_pragma_eol (parser, false);
16054 return false;
16057 tree clauses
16058 = c_parser_omp_all_clauses (parser,
16059 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16060 "#pragma omp ordered");
16061 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16062 return false;
16066 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16067 "#pragma omp ordered");
16069 if (!flag_openmp /* flag_openmp_simd */
16070 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16071 return false;
16073 c_finish_omp_ordered (loc, clauses,
16074 c_parser_omp_structured_block (parser, if_p));
16075 return true;
16078 /* OpenMP 2.5:
16080 section-scope:
16081 { section-sequence }
16083 section-sequence:
16084 section-directive[opt] structured-block
16085 section-sequence section-directive structured-block
16087 SECTIONS_LOC is the location of the #pragma omp sections. */
16089 static tree
16090 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16092 tree stmt, substmt;
16093 bool error_suppress = false;
16094 location_t loc;
16096 loc = c_parser_peek_token (parser)->location;
16097 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16099 /* Avoid skipping until the end of the block. */
16100 parser->error = false;
16101 return NULL_TREE;
16104 stmt = push_stmt_list ();
16106 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16108 substmt = c_parser_omp_structured_block (parser, NULL);
16109 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16110 SET_EXPR_LOCATION (substmt, loc);
16111 add_stmt (substmt);
16114 while (1)
16116 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16117 break;
16118 if (c_parser_next_token_is (parser, CPP_EOF))
16119 break;
16121 loc = c_parser_peek_token (parser)->location;
16122 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16124 c_parser_consume_pragma (parser);
16125 c_parser_skip_to_pragma_eol (parser);
16126 error_suppress = false;
16128 else if (!error_suppress)
16130 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16131 error_suppress = true;
16134 substmt = c_parser_omp_structured_block (parser, NULL);
16135 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16136 SET_EXPR_LOCATION (substmt, loc);
16137 add_stmt (substmt);
16139 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16140 "expected %<#pragma omp section%> or %<}%>");
16142 substmt = pop_stmt_list (stmt);
16144 stmt = make_node (OMP_SECTIONS);
16145 SET_EXPR_LOCATION (stmt, sections_loc);
16146 TREE_TYPE (stmt) = void_type_node;
16147 OMP_SECTIONS_BODY (stmt) = substmt;
16149 return add_stmt (stmt);
16152 /* OpenMP 2.5:
16153 # pragma omp sections sections-clause[optseq] newline
16154 sections-scope
16156 LOC is the location of the #pragma token.
16159 #define OMP_SECTIONS_CLAUSE_MASK \
16160 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16166 static tree
16167 c_parser_omp_sections (location_t loc, c_parser *parser,
16168 char *p_name, omp_clause_mask mask, tree *cclauses)
16170 tree block, clauses, ret;
16172 strcat (p_name, " sections");
16173 mask |= OMP_SECTIONS_CLAUSE_MASK;
16174 if (cclauses)
16175 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16177 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16178 if (cclauses)
16180 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16181 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16184 block = c_begin_compound_stmt (true);
16185 ret = c_parser_omp_sections_scope (loc, parser);
16186 if (ret)
16187 OMP_SECTIONS_CLAUSES (ret) = clauses;
16188 block = c_end_compound_stmt (loc, block, true);
16189 add_stmt (block);
16191 return ret;
16194 /* OpenMP 2.5:
16195 # pragma omp parallel parallel-clause[optseq] new-line
16196 structured-block
16197 # pragma omp parallel for parallel-for-clause[optseq] new-line
16198 structured-block
16199 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16200 structured-block
16202 OpenMP 4.0:
16203 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16204 structured-block
16206 LOC is the location of the #pragma token.
16209 #define OMP_PARALLEL_CLAUSE_MASK \
16210 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16220 static tree
16221 c_parser_omp_parallel (location_t loc, c_parser *parser,
16222 char *p_name, omp_clause_mask mask, tree *cclauses,
16223 bool *if_p)
16225 tree stmt, clauses, block;
16227 strcat (p_name, " parallel");
16228 mask |= OMP_PARALLEL_CLAUSE_MASK;
16229 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16230 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16231 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16232 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16234 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16236 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16237 if (cclauses == NULL)
16238 cclauses = cclauses_buf;
16240 c_parser_consume_token (parser);
16241 if (!flag_openmp) /* flag_openmp_simd */
16242 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16243 block = c_begin_omp_parallel ();
16244 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16245 stmt
16246 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16247 block);
16248 if (ret == NULL_TREE)
16249 return ret;
16250 OMP_PARALLEL_COMBINED (stmt) = 1;
16251 return stmt;
16253 /* When combined with distribute, parallel has to be followed by for.
16254 #pragma omp target parallel is allowed though. */
16255 else if (cclauses
16256 && (mask & (OMP_CLAUSE_MASK_1
16257 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16259 error_at (loc, "expected %<for%> after %qs", p_name);
16260 c_parser_skip_to_pragma_eol (parser);
16261 return NULL_TREE;
16263 else if (!flag_openmp) /* flag_openmp_simd */
16265 c_parser_skip_to_pragma_eol (parser, false);
16266 return NULL_TREE;
16268 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16270 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16271 if (strcmp (p, "sections") == 0)
16273 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16274 if (cclauses == NULL)
16275 cclauses = cclauses_buf;
16277 c_parser_consume_token (parser);
16278 block = c_begin_omp_parallel ();
16279 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16280 stmt = c_finish_omp_parallel (loc,
16281 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16282 block);
16283 OMP_PARALLEL_COMBINED (stmt) = 1;
16284 return stmt;
16288 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16289 if (cclauses)
16291 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16292 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16295 block = c_begin_omp_parallel ();
16296 c_parser_statement (parser, if_p);
16297 stmt = c_finish_omp_parallel (loc, clauses, block);
16299 return stmt;
16302 /* OpenMP 2.5:
16303 # pragma omp single single-clause[optseq] new-line
16304 structured-block
16306 LOC is the location of the #pragma.
16309 #define OMP_SINGLE_CLAUSE_MASK \
16310 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16315 static tree
16316 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16318 tree stmt = make_node (OMP_SINGLE);
16319 SET_EXPR_LOCATION (stmt, loc);
16320 TREE_TYPE (stmt) = void_type_node;
16322 OMP_SINGLE_CLAUSES (stmt)
16323 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16324 "#pragma omp single");
16325 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16327 return add_stmt (stmt);
16330 /* OpenMP 3.0:
16331 # pragma omp task task-clause[optseq] new-line
16333 LOC is the location of the #pragma.
16336 #define OMP_TASK_CLAUSE_MASK \
16337 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16348 static tree
16349 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16351 tree clauses, block;
16353 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16354 "#pragma omp task");
16356 block = c_begin_omp_task ();
16357 c_parser_statement (parser, if_p);
16358 return c_finish_omp_task (loc, clauses, block);
16361 /* OpenMP 3.0:
16362 # pragma omp taskwait new-line
16365 static void
16366 c_parser_omp_taskwait (c_parser *parser)
16368 location_t loc = c_parser_peek_token (parser)->location;
16369 c_parser_consume_pragma (parser);
16370 c_parser_skip_to_pragma_eol (parser);
16372 c_finish_omp_taskwait (loc);
16375 /* OpenMP 3.1:
16376 # pragma omp taskyield new-line
16379 static void
16380 c_parser_omp_taskyield (c_parser *parser)
16382 location_t loc = c_parser_peek_token (parser)->location;
16383 c_parser_consume_pragma (parser);
16384 c_parser_skip_to_pragma_eol (parser);
16386 c_finish_omp_taskyield (loc);
16389 /* OpenMP 4.0:
16390 # pragma omp taskgroup new-line
16393 static tree
16394 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16396 location_t loc = c_parser_peek_token (parser)->location;
16397 c_parser_skip_to_pragma_eol (parser);
16398 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16399 if_p));
16402 /* OpenMP 4.0:
16403 # pragma omp cancel cancel-clause[optseq] new-line
16405 LOC is the location of the #pragma.
16408 #define OMP_CANCEL_CLAUSE_MASK \
16409 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16415 static void
16416 c_parser_omp_cancel (c_parser *parser)
16418 location_t loc = c_parser_peek_token (parser)->location;
16420 c_parser_consume_pragma (parser);
16421 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16422 "#pragma omp cancel");
16424 c_finish_omp_cancel (loc, clauses);
16427 /* OpenMP 4.0:
16428 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16430 LOC is the location of the #pragma.
16433 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16434 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16439 static void
16440 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16442 location_t loc = c_parser_peek_token (parser)->location;
16443 tree clauses;
16444 bool point_seen = false;
16446 c_parser_consume_pragma (parser);
16447 if (c_parser_next_token_is (parser, CPP_NAME))
16449 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16450 if (strcmp (p, "point") == 0)
16452 c_parser_consume_token (parser);
16453 point_seen = true;
16456 if (!point_seen)
16458 c_parser_error (parser, "expected %<point%>");
16459 c_parser_skip_to_pragma_eol (parser);
16460 return;
16463 if (context != pragma_compound)
16465 if (context == pragma_stmt)
16466 error_at (loc,
16467 "%<#pragma %s%> may only be used in compound statements",
16468 "omp cancellation point");
16469 else
16470 c_parser_error (parser, "expected declaration specifiers");
16471 c_parser_skip_to_pragma_eol (parser, false);
16472 return;
16475 clauses
16476 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16477 "#pragma omp cancellation point");
16479 c_finish_omp_cancellation_point (loc, clauses);
16482 /* OpenMP 4.0:
16483 #pragma omp distribute distribute-clause[optseq] new-line
16484 for-loop */
16486 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16487 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16493 static tree
16494 c_parser_omp_distribute (location_t loc, c_parser *parser,
16495 char *p_name, omp_clause_mask mask, tree *cclauses,
16496 bool *if_p)
16498 tree clauses, block, ret;
16500 strcat (p_name, " distribute");
16501 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16503 if (c_parser_next_token_is (parser, CPP_NAME))
16505 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16506 bool simd = false;
16507 bool parallel = false;
16509 if (strcmp (p, "simd") == 0)
16510 simd = true;
16511 else
16512 parallel = strcmp (p, "parallel") == 0;
16513 if (parallel || simd)
16515 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16516 if (cclauses == NULL)
16517 cclauses = cclauses_buf;
16518 c_parser_consume_token (parser);
16519 if (!flag_openmp) /* flag_openmp_simd */
16521 if (simd)
16522 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16523 if_p);
16524 else
16525 return c_parser_omp_parallel (loc, parser, p_name, mask,
16526 cclauses, if_p);
16528 block = c_begin_compound_stmt (true);
16529 if (simd)
16530 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16531 if_p);
16532 else
16533 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16534 if_p);
16535 block = c_end_compound_stmt (loc, block, true);
16536 if (ret == NULL)
16537 return ret;
16538 ret = make_node (OMP_DISTRIBUTE);
16539 TREE_TYPE (ret) = void_type_node;
16540 OMP_FOR_BODY (ret) = block;
16541 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16542 SET_EXPR_LOCATION (ret, loc);
16543 add_stmt (ret);
16544 return ret;
16547 if (!flag_openmp) /* flag_openmp_simd */
16549 c_parser_skip_to_pragma_eol (parser, false);
16550 return NULL_TREE;
16553 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16554 if (cclauses)
16556 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16557 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16560 block = c_begin_compound_stmt (true);
16561 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16562 if_p);
16563 block = c_end_compound_stmt (loc, block, true);
16564 add_stmt (block);
16566 return ret;
16569 /* OpenMP 4.0:
16570 # pragma omp teams teams-clause[optseq] new-line
16571 structured-block */
16573 #define OMP_TEAMS_CLAUSE_MASK \
16574 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16582 static tree
16583 c_parser_omp_teams (location_t loc, c_parser *parser,
16584 char *p_name, omp_clause_mask mask, tree *cclauses,
16585 bool *if_p)
16587 tree clauses, block, ret;
16589 strcat (p_name, " teams");
16590 mask |= OMP_TEAMS_CLAUSE_MASK;
16592 if (c_parser_next_token_is (parser, CPP_NAME))
16594 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16595 if (strcmp (p, "distribute") == 0)
16597 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16598 if (cclauses == NULL)
16599 cclauses = cclauses_buf;
16601 c_parser_consume_token (parser);
16602 if (!flag_openmp) /* flag_openmp_simd */
16603 return c_parser_omp_distribute (loc, parser, p_name, mask,
16604 cclauses, if_p);
16605 block = c_begin_compound_stmt (true);
16606 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16607 if_p);
16608 block = c_end_compound_stmt (loc, block, true);
16609 if (ret == NULL)
16610 return ret;
16611 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16612 ret = make_node (OMP_TEAMS);
16613 TREE_TYPE (ret) = void_type_node;
16614 OMP_TEAMS_CLAUSES (ret) = clauses;
16615 OMP_TEAMS_BODY (ret) = block;
16616 OMP_TEAMS_COMBINED (ret) = 1;
16617 return add_stmt (ret);
16620 if (!flag_openmp) /* flag_openmp_simd */
16622 c_parser_skip_to_pragma_eol (parser, false);
16623 return NULL_TREE;
16626 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16627 if (cclauses)
16629 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16630 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16633 tree stmt = make_node (OMP_TEAMS);
16634 TREE_TYPE (stmt) = void_type_node;
16635 OMP_TEAMS_CLAUSES (stmt) = clauses;
16636 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16638 return add_stmt (stmt);
16641 /* OpenMP 4.0:
16642 # pragma omp target data target-data-clause[optseq] new-line
16643 structured-block */
16645 #define OMP_TARGET_DATA_CLAUSE_MASK \
16646 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16651 static tree
16652 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16654 tree clauses
16655 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16656 "#pragma omp target data");
16657 int map_seen = 0;
16658 for (tree *pc = &clauses; *pc;)
16660 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16661 switch (OMP_CLAUSE_MAP_KIND (*pc))
16663 case GOMP_MAP_TO:
16664 case GOMP_MAP_ALWAYS_TO:
16665 case GOMP_MAP_FROM:
16666 case GOMP_MAP_ALWAYS_FROM:
16667 case GOMP_MAP_TOFROM:
16668 case GOMP_MAP_ALWAYS_TOFROM:
16669 case GOMP_MAP_ALLOC:
16670 map_seen = 3;
16671 break;
16672 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16673 case GOMP_MAP_ALWAYS_POINTER:
16674 break;
16675 default:
16676 map_seen |= 1;
16677 error_at (OMP_CLAUSE_LOCATION (*pc),
16678 "%<#pragma omp target data%> with map-type other "
16679 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16680 "on %<map%> clause");
16681 *pc = OMP_CLAUSE_CHAIN (*pc);
16682 continue;
16684 pc = &OMP_CLAUSE_CHAIN (*pc);
16687 if (map_seen != 3)
16689 if (map_seen == 0)
16690 error_at (loc,
16691 "%<#pragma omp target data%> must contain at least "
16692 "one %<map%> clause");
16693 return NULL_TREE;
16696 tree stmt = make_node (OMP_TARGET_DATA);
16697 TREE_TYPE (stmt) = void_type_node;
16698 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16699 keep_next_level ();
16700 tree block = c_begin_compound_stmt (true);
16701 add_stmt (c_parser_omp_structured_block (parser, if_p));
16702 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16704 SET_EXPR_LOCATION (stmt, loc);
16705 return add_stmt (stmt);
16708 /* OpenMP 4.0:
16709 # pragma omp target update target-update-clause[optseq] new-line */
16711 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16712 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16719 static bool
16720 c_parser_omp_target_update (location_t loc, c_parser *parser,
16721 enum pragma_context context)
16723 if (context == pragma_stmt)
16725 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16726 "omp target update");
16727 c_parser_skip_to_pragma_eol (parser, false);
16728 return false;
16731 tree clauses
16732 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16733 "#pragma omp target update");
16734 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16735 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16737 error_at (loc,
16738 "%<#pragma omp target update%> must contain at least one "
16739 "%<from%> or %<to%> clauses");
16740 return false;
16743 tree stmt = make_node (OMP_TARGET_UPDATE);
16744 TREE_TYPE (stmt) = void_type_node;
16745 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16746 SET_EXPR_LOCATION (stmt, loc);
16747 add_stmt (stmt);
16748 return false;
16751 /* OpenMP 4.5:
16752 # pragma omp target enter data target-data-clause[optseq] new-line */
16754 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16755 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16761 static tree
16762 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16763 enum pragma_context context)
16765 bool data_seen = false;
16766 if (c_parser_next_token_is (parser, CPP_NAME))
16768 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16769 if (strcmp (p, "data") == 0)
16771 c_parser_consume_token (parser);
16772 data_seen = true;
16775 if (!data_seen)
16777 c_parser_error (parser, "expected %<data%>");
16778 c_parser_skip_to_pragma_eol (parser);
16779 return NULL_TREE;
16782 if (context == pragma_stmt)
16784 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16785 "omp target enter data");
16786 c_parser_skip_to_pragma_eol (parser, false);
16787 return NULL_TREE;
16790 tree clauses
16791 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16792 "#pragma omp target enter data");
16793 int map_seen = 0;
16794 for (tree *pc = &clauses; *pc;)
16796 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16797 switch (OMP_CLAUSE_MAP_KIND (*pc))
16799 case GOMP_MAP_TO:
16800 case GOMP_MAP_ALWAYS_TO:
16801 case GOMP_MAP_ALLOC:
16802 map_seen = 3;
16803 break;
16804 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16805 case GOMP_MAP_ALWAYS_POINTER:
16806 break;
16807 default:
16808 map_seen |= 1;
16809 error_at (OMP_CLAUSE_LOCATION (*pc),
16810 "%<#pragma omp target enter data%> with map-type other "
16811 "than %<to%> or %<alloc%> on %<map%> clause");
16812 *pc = OMP_CLAUSE_CHAIN (*pc);
16813 continue;
16815 pc = &OMP_CLAUSE_CHAIN (*pc);
16818 if (map_seen != 3)
16820 if (map_seen == 0)
16821 error_at (loc,
16822 "%<#pragma omp target enter data%> must contain at least "
16823 "one %<map%> clause");
16824 return NULL_TREE;
16827 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16828 TREE_TYPE (stmt) = void_type_node;
16829 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16830 SET_EXPR_LOCATION (stmt, loc);
16831 add_stmt (stmt);
16832 return stmt;
16835 /* OpenMP 4.5:
16836 # pragma omp target exit data target-data-clause[optseq] new-line */
16838 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16839 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16845 static tree
16846 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16847 enum pragma_context context)
16849 bool data_seen = false;
16850 if (c_parser_next_token_is (parser, CPP_NAME))
16852 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16853 if (strcmp (p, "data") == 0)
16855 c_parser_consume_token (parser);
16856 data_seen = true;
16859 if (!data_seen)
16861 c_parser_error (parser, "expected %<data%>");
16862 c_parser_skip_to_pragma_eol (parser);
16863 return NULL_TREE;
16866 if (context == pragma_stmt)
16868 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16869 "omp target exit data");
16870 c_parser_skip_to_pragma_eol (parser, false);
16871 return NULL_TREE;
16874 tree clauses
16875 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16876 "#pragma omp target exit data");
16878 int map_seen = 0;
16879 for (tree *pc = &clauses; *pc;)
16881 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16882 switch (OMP_CLAUSE_MAP_KIND (*pc))
16884 case GOMP_MAP_FROM:
16885 case GOMP_MAP_ALWAYS_FROM:
16886 case GOMP_MAP_RELEASE:
16887 case GOMP_MAP_DELETE:
16888 map_seen = 3;
16889 break;
16890 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16891 case GOMP_MAP_ALWAYS_POINTER:
16892 break;
16893 default:
16894 map_seen |= 1;
16895 error_at (OMP_CLAUSE_LOCATION (*pc),
16896 "%<#pragma omp target exit data%> with map-type other "
16897 "than %<from%>, %<release%> or %<delete%> on %<map%>"
16898 " clause");
16899 *pc = OMP_CLAUSE_CHAIN (*pc);
16900 continue;
16902 pc = &OMP_CLAUSE_CHAIN (*pc);
16905 if (map_seen != 3)
16907 if (map_seen == 0)
16908 error_at (loc,
16909 "%<#pragma omp target exit data%> must contain at least one "
16910 "%<map%> clause");
16911 return NULL_TREE;
16914 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16915 TREE_TYPE (stmt) = void_type_node;
16916 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16917 SET_EXPR_LOCATION (stmt, loc);
16918 add_stmt (stmt);
16919 return stmt;
16922 /* OpenMP 4.0:
16923 # pragma omp target target-clause[optseq] new-line
16924 structured-block */
16926 #define OMP_TARGET_CLAUSE_MASK \
16927 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16937 static bool
16938 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16940 location_t loc = c_parser_peek_token (parser)->location;
16941 c_parser_consume_pragma (parser);
16942 tree *pc = NULL, stmt, block;
16944 if (context != pragma_stmt && context != pragma_compound)
16946 c_parser_error (parser, "expected declaration specifiers");
16947 c_parser_skip_to_pragma_eol (parser);
16948 return false;
16951 if (c_parser_next_token_is (parser, CPP_NAME))
16953 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16954 enum tree_code ccode = ERROR_MARK;
16956 if (strcmp (p, "teams") == 0)
16957 ccode = OMP_TEAMS;
16958 else if (strcmp (p, "parallel") == 0)
16959 ccode = OMP_PARALLEL;
16960 else if (strcmp (p, "simd") == 0)
16961 ccode = OMP_SIMD;
16962 if (ccode != ERROR_MARK)
16964 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16965 char p_name[sizeof ("#pragma omp target teams distribute "
16966 "parallel for simd")];
16968 c_parser_consume_token (parser);
16969 strcpy (p_name, "#pragma omp target");
16970 if (!flag_openmp) /* flag_openmp_simd */
16972 tree stmt;
16973 switch (ccode)
16975 case OMP_TEAMS:
16976 stmt = c_parser_omp_teams (loc, parser, p_name,
16977 OMP_TARGET_CLAUSE_MASK,
16978 cclauses, if_p);
16979 break;
16980 case OMP_PARALLEL:
16981 stmt = c_parser_omp_parallel (loc, parser, p_name,
16982 OMP_TARGET_CLAUSE_MASK,
16983 cclauses, if_p);
16984 break;
16985 case OMP_SIMD:
16986 stmt = c_parser_omp_simd (loc, parser, p_name,
16987 OMP_TARGET_CLAUSE_MASK,
16988 cclauses, if_p);
16989 break;
16990 default:
16991 gcc_unreachable ();
16993 return stmt != NULL_TREE;
16995 keep_next_level ();
16996 tree block = c_begin_compound_stmt (true), ret;
16997 switch (ccode)
16999 case OMP_TEAMS:
17000 ret = c_parser_omp_teams (loc, parser, p_name,
17001 OMP_TARGET_CLAUSE_MASK, cclauses,
17002 if_p);
17003 break;
17004 case OMP_PARALLEL:
17005 ret = c_parser_omp_parallel (loc, parser, p_name,
17006 OMP_TARGET_CLAUSE_MASK, cclauses,
17007 if_p);
17008 break;
17009 case OMP_SIMD:
17010 ret = c_parser_omp_simd (loc, parser, p_name,
17011 OMP_TARGET_CLAUSE_MASK, cclauses,
17012 if_p);
17013 break;
17014 default:
17015 gcc_unreachable ();
17017 block = c_end_compound_stmt (loc, block, true);
17018 if (ret == NULL_TREE)
17019 return false;
17020 if (ccode == OMP_TEAMS)
17022 /* For combined target teams, ensure the num_teams and
17023 thread_limit clause expressions are evaluated on the host,
17024 before entering the target construct. */
17025 tree c;
17026 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17027 c; c = OMP_CLAUSE_CHAIN (c))
17028 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17029 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17030 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17032 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17033 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17034 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17035 expr, NULL_TREE, NULL_TREE);
17036 add_stmt (expr);
17037 OMP_CLAUSE_OPERAND (c, 0) = expr;
17038 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17039 OMP_CLAUSE_FIRSTPRIVATE);
17040 OMP_CLAUSE_DECL (tc) = tmp;
17041 OMP_CLAUSE_CHAIN (tc)
17042 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17043 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17046 tree stmt = make_node (OMP_TARGET);
17047 TREE_TYPE (stmt) = void_type_node;
17048 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17049 OMP_TARGET_BODY (stmt) = block;
17050 OMP_TARGET_COMBINED (stmt) = 1;
17051 add_stmt (stmt);
17052 pc = &OMP_TARGET_CLAUSES (stmt);
17053 goto check_clauses;
17055 else if (!flag_openmp) /* flag_openmp_simd */
17057 c_parser_skip_to_pragma_eol (parser, false);
17058 return false;
17060 else if (strcmp (p, "data") == 0)
17062 c_parser_consume_token (parser);
17063 c_parser_omp_target_data (loc, parser, if_p);
17064 return true;
17066 else if (strcmp (p, "enter") == 0)
17068 c_parser_consume_token (parser);
17069 c_parser_omp_target_enter_data (loc, parser, context);
17070 return false;
17072 else if (strcmp (p, "exit") == 0)
17074 c_parser_consume_token (parser);
17075 c_parser_omp_target_exit_data (loc, parser, context);
17076 return false;
17078 else if (strcmp (p, "update") == 0)
17080 c_parser_consume_token (parser);
17081 return c_parser_omp_target_update (loc, parser, context);
17084 if (!flag_openmp) /* flag_openmp_simd */
17086 c_parser_skip_to_pragma_eol (parser, false);
17087 return false;
17090 stmt = make_node (OMP_TARGET);
17091 TREE_TYPE (stmt) = void_type_node;
17093 OMP_TARGET_CLAUSES (stmt)
17094 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17095 "#pragma omp target");
17096 pc = &OMP_TARGET_CLAUSES (stmt);
17097 keep_next_level ();
17098 block = c_begin_compound_stmt (true);
17099 add_stmt (c_parser_omp_structured_block (parser, if_p));
17100 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17102 SET_EXPR_LOCATION (stmt, loc);
17103 add_stmt (stmt);
17105 check_clauses:
17106 while (*pc)
17108 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17109 switch (OMP_CLAUSE_MAP_KIND (*pc))
17111 case GOMP_MAP_TO:
17112 case GOMP_MAP_ALWAYS_TO:
17113 case GOMP_MAP_FROM:
17114 case GOMP_MAP_ALWAYS_FROM:
17115 case GOMP_MAP_TOFROM:
17116 case GOMP_MAP_ALWAYS_TOFROM:
17117 case GOMP_MAP_ALLOC:
17118 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17119 case GOMP_MAP_ALWAYS_POINTER:
17120 break;
17121 default:
17122 error_at (OMP_CLAUSE_LOCATION (*pc),
17123 "%<#pragma omp target%> with map-type other "
17124 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17125 "on %<map%> clause");
17126 *pc = OMP_CLAUSE_CHAIN (*pc);
17127 continue;
17129 pc = &OMP_CLAUSE_CHAIN (*pc);
17131 return true;
17134 /* OpenMP 4.0:
17135 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17137 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17138 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17145 static void
17146 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17148 auto_vec<c_token> clauses;
17149 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17151 c_token *token = c_parser_peek_token (parser);
17152 if (token->type == CPP_EOF)
17154 c_parser_skip_to_pragma_eol (parser);
17155 return;
17157 clauses.safe_push (*token);
17158 c_parser_consume_token (parser);
17160 clauses.safe_push (*c_parser_peek_token (parser));
17161 c_parser_skip_to_pragma_eol (parser);
17163 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17165 if (c_parser_peek_token (parser)->pragma_kind
17166 != PRAGMA_OMP_DECLARE
17167 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17168 || strcmp (IDENTIFIER_POINTER
17169 (c_parser_peek_2nd_token (parser)->value),
17170 "simd") != 0)
17172 c_parser_error (parser,
17173 "%<#pragma omp declare simd%> must be followed by "
17174 "function declaration or definition or another "
17175 "%<#pragma omp declare simd%>");
17176 return;
17178 c_parser_consume_pragma (parser);
17179 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17181 c_token *token = c_parser_peek_token (parser);
17182 if (token->type == CPP_EOF)
17184 c_parser_skip_to_pragma_eol (parser);
17185 return;
17187 clauses.safe_push (*token);
17188 c_parser_consume_token (parser);
17190 clauses.safe_push (*c_parser_peek_token (parser));
17191 c_parser_skip_to_pragma_eol (parser);
17194 /* Make sure nothing tries to read past the end of the tokens. */
17195 c_token eof_token;
17196 memset (&eof_token, 0, sizeof (eof_token));
17197 eof_token.type = CPP_EOF;
17198 clauses.safe_push (eof_token);
17199 clauses.safe_push (eof_token);
17201 switch (context)
17203 case pragma_external:
17204 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17205 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17207 int ext = disable_extension_diagnostics ();
17209 c_parser_consume_token (parser);
17210 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17211 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17212 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17213 NULL, clauses);
17214 restore_extension_diagnostics (ext);
17216 else
17217 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17218 NULL, clauses);
17219 break;
17220 case pragma_struct:
17221 case pragma_param:
17222 case pragma_stmt:
17223 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17224 "function declaration or definition");
17225 break;
17226 case pragma_compound:
17227 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17228 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17230 int ext = disable_extension_diagnostics ();
17232 c_parser_consume_token (parser);
17233 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17234 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17235 if (c_parser_next_tokens_start_declaration (parser))
17237 c_parser_declaration_or_fndef (parser, true, true, true, true,
17238 true, NULL, clauses);
17239 restore_extension_diagnostics (ext);
17240 break;
17242 restore_extension_diagnostics (ext);
17244 else if (c_parser_next_tokens_start_declaration (parser))
17246 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17247 NULL, clauses);
17248 break;
17250 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17251 "function declaration or definition");
17252 break;
17253 default:
17254 gcc_unreachable ();
17258 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17259 and put that into "omp declare simd" attribute. */
17261 static void
17262 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17263 vec<c_token> clauses)
17265 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17266 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17267 has already processed the tokens. */
17268 if (clauses.exists () && clauses[0].type == CPP_EOF)
17269 return;
17270 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17272 error ("%<#pragma omp declare simd%> not immediately followed by "
17273 "a function declaration or definition");
17274 clauses[0].type = CPP_EOF;
17275 return;
17277 if (clauses.exists () && clauses[0].type != CPP_NAME)
17279 error_at (DECL_SOURCE_LOCATION (fndecl),
17280 "%<#pragma omp declare simd%> not immediately followed by "
17281 "a single function declaration or definition");
17282 clauses[0].type = CPP_EOF;
17283 return;
17286 if (parms == NULL_TREE)
17287 parms = DECL_ARGUMENTS (fndecl);
17289 unsigned int tokens_avail = parser->tokens_avail;
17290 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17293 parser->tokens = clauses.address ();
17294 parser->tokens_avail = clauses.length ();
17296 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17297 while (parser->tokens_avail > 3)
17299 c_token *token = c_parser_peek_token (parser);
17300 gcc_assert (token->type == CPP_NAME
17301 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17302 c_parser_consume_token (parser);
17303 parser->in_pragma = true;
17305 tree c = NULL_TREE;
17306 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17307 "#pragma omp declare simd");
17308 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17309 if (c != NULL_TREE)
17310 c = tree_cons (NULL_TREE, c, NULL_TREE);
17311 c = build_tree_list (get_identifier ("omp declare simd"), c);
17312 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17313 DECL_ATTRIBUTES (fndecl) = c;
17316 parser->tokens = &parser->tokens_buf[0];
17317 parser->tokens_avail = tokens_avail;
17318 if (clauses.exists ())
17319 clauses[0].type = CPP_PRAGMA;
17323 /* OpenMP 4.0:
17324 # pragma omp declare target new-line
17325 declarations and definitions
17326 # pragma omp end declare target new-line
17328 OpenMP 4.5:
17329 # pragma omp declare target ( extended-list ) new-line
17331 # pragma omp declare target declare-target-clauses[seq] new-line */
17333 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17334 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17337 static void
17338 c_parser_omp_declare_target (c_parser *parser)
17340 location_t loc = c_parser_peek_token (parser)->location;
17341 tree clauses = NULL_TREE;
17342 if (c_parser_next_token_is (parser, CPP_NAME))
17343 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17344 "#pragma omp declare target");
17345 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17347 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17348 clauses);
17349 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17350 c_parser_skip_to_pragma_eol (parser);
17352 else
17354 c_parser_skip_to_pragma_eol (parser);
17355 current_omp_declare_target_attribute++;
17356 return;
17358 if (current_omp_declare_target_attribute)
17359 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17360 "%<#pragma omp declare target%> without clauses and "
17361 "%<#pragma omp end declare target%>");
17362 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17364 tree t = OMP_CLAUSE_DECL (c), id;
17365 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17366 tree at2 = lookup_attribute ("omp declare target link",
17367 DECL_ATTRIBUTES (t));
17368 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17370 id = get_identifier ("omp declare target link");
17371 std::swap (at1, at2);
17373 else
17374 id = get_identifier ("omp declare target");
17375 if (at2)
17377 error_at (OMP_CLAUSE_LOCATION (c),
17378 "%qD specified both in declare target %<link%> and %<to%>"
17379 " clauses", t);
17380 continue;
17382 if (!at1)
17384 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17385 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17386 continue;
17388 symtab_node *node = symtab_node::get (t);
17389 if (node != NULL)
17391 node->offloadable = 1;
17392 if (ENABLE_OFFLOADING)
17394 g->have_offload = true;
17395 if (is_a <varpool_node *> (node))
17396 vec_safe_push (offload_vars, t);
17403 static void
17404 c_parser_omp_end_declare_target (c_parser *parser)
17406 location_t loc = c_parser_peek_token (parser)->location;
17407 c_parser_consume_pragma (parser);
17408 if (c_parser_next_token_is (parser, CPP_NAME)
17409 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17410 "declare") == 0)
17412 c_parser_consume_token (parser);
17413 if (c_parser_next_token_is (parser, CPP_NAME)
17414 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17415 "target") == 0)
17416 c_parser_consume_token (parser);
17417 else
17419 c_parser_error (parser, "expected %<target%>");
17420 c_parser_skip_to_pragma_eol (parser);
17421 return;
17424 else
17426 c_parser_error (parser, "expected %<declare%>");
17427 c_parser_skip_to_pragma_eol (parser);
17428 return;
17430 c_parser_skip_to_pragma_eol (parser);
17431 if (!current_omp_declare_target_attribute)
17432 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17433 "%<#pragma omp declare target%>");
17434 else
17435 current_omp_declare_target_attribute--;
17439 /* OpenMP 4.0
17440 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17441 initializer-clause[opt] new-line
17443 initializer-clause:
17444 initializer (omp_priv = initializer)
17445 initializer (function-name (argument-list)) */
17447 static void
17448 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17450 unsigned int tokens_avail = 0, i;
17451 vec<tree> types = vNULL;
17452 vec<c_token> clauses = vNULL;
17453 enum tree_code reduc_code = ERROR_MARK;
17454 tree reduc_id = NULL_TREE;
17455 tree type;
17456 location_t rloc = c_parser_peek_token (parser)->location;
17458 if (context == pragma_struct || context == pragma_param)
17460 error ("%<#pragma omp declare reduction%> not at file or block scope");
17461 goto fail;
17464 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17465 goto fail;
17467 switch (c_parser_peek_token (parser)->type)
17469 case CPP_PLUS:
17470 reduc_code = PLUS_EXPR;
17471 break;
17472 case CPP_MULT:
17473 reduc_code = MULT_EXPR;
17474 break;
17475 case CPP_MINUS:
17476 reduc_code = MINUS_EXPR;
17477 break;
17478 case CPP_AND:
17479 reduc_code = BIT_AND_EXPR;
17480 break;
17481 case CPP_XOR:
17482 reduc_code = BIT_XOR_EXPR;
17483 break;
17484 case CPP_OR:
17485 reduc_code = BIT_IOR_EXPR;
17486 break;
17487 case CPP_AND_AND:
17488 reduc_code = TRUTH_ANDIF_EXPR;
17489 break;
17490 case CPP_OR_OR:
17491 reduc_code = TRUTH_ORIF_EXPR;
17492 break;
17493 case CPP_NAME:
17494 const char *p;
17495 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17496 if (strcmp (p, "min") == 0)
17498 reduc_code = MIN_EXPR;
17499 break;
17501 if (strcmp (p, "max") == 0)
17503 reduc_code = MAX_EXPR;
17504 break;
17506 reduc_id = c_parser_peek_token (parser)->value;
17507 break;
17508 default:
17509 c_parser_error (parser,
17510 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17511 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17512 goto fail;
17515 tree orig_reduc_id, reduc_decl;
17516 orig_reduc_id = reduc_id;
17517 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17518 reduc_decl = c_omp_reduction_decl (reduc_id);
17519 c_parser_consume_token (parser);
17521 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17522 goto fail;
17524 while (true)
17526 location_t loc = c_parser_peek_token (parser)->location;
17527 struct c_type_name *ctype = c_parser_type_name (parser);
17528 if (ctype != NULL)
17530 type = groktypename (ctype, NULL, NULL);
17531 if (type == error_mark_node)
17533 else if ((INTEGRAL_TYPE_P (type)
17534 || TREE_CODE (type) == REAL_TYPE
17535 || TREE_CODE (type) == COMPLEX_TYPE)
17536 && orig_reduc_id == NULL_TREE)
17537 error_at (loc, "predeclared arithmetic type in "
17538 "%<#pragma omp declare reduction%>");
17539 else if (TREE_CODE (type) == FUNCTION_TYPE
17540 || TREE_CODE (type) == ARRAY_TYPE)
17541 error_at (loc, "function or array type in "
17542 "%<#pragma omp declare reduction%>");
17543 else if (TYPE_ATOMIC (type))
17544 error_at (loc, "%<_Atomic%> qualified type in "
17545 "%<#pragma omp declare reduction%>");
17546 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17547 error_at (loc, "const, volatile or restrict qualified type in "
17548 "%<#pragma omp declare reduction%>");
17549 else
17551 tree t;
17552 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17553 if (comptypes (TREE_PURPOSE (t), type))
17555 error_at (loc, "redeclaration of %qs "
17556 "%<#pragma omp declare reduction%> for "
17557 "type %qT",
17558 IDENTIFIER_POINTER (reduc_id)
17559 + sizeof ("omp declare reduction ") - 1,
17560 type);
17561 location_t ploc
17562 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17563 0));
17564 error_at (ploc, "previous %<#pragma omp declare "
17565 "reduction%>");
17566 break;
17568 if (t == NULL_TREE)
17569 types.safe_push (type);
17571 if (c_parser_next_token_is (parser, CPP_COMMA))
17572 c_parser_consume_token (parser);
17573 else
17574 break;
17576 else
17577 break;
17580 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17581 || types.is_empty ())
17583 fail:
17584 clauses.release ();
17585 types.release ();
17586 while (true)
17588 c_token *token = c_parser_peek_token (parser);
17589 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17590 break;
17591 c_parser_consume_token (parser);
17593 c_parser_skip_to_pragma_eol (parser);
17594 return;
17597 if (types.length () > 1)
17599 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17601 c_token *token = c_parser_peek_token (parser);
17602 if (token->type == CPP_EOF)
17603 goto fail;
17604 clauses.safe_push (*token);
17605 c_parser_consume_token (parser);
17607 clauses.safe_push (*c_parser_peek_token (parser));
17608 c_parser_skip_to_pragma_eol (parser);
17610 /* Make sure nothing tries to read past the end of the tokens. */
17611 c_token eof_token;
17612 memset (&eof_token, 0, sizeof (eof_token));
17613 eof_token.type = CPP_EOF;
17614 clauses.safe_push (eof_token);
17615 clauses.safe_push (eof_token);
17618 int errs = errorcount;
17619 FOR_EACH_VEC_ELT (types, i, type)
17621 tokens_avail = parser->tokens_avail;
17622 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17623 if (!clauses.is_empty ())
17625 parser->tokens = clauses.address ();
17626 parser->tokens_avail = clauses.length ();
17627 parser->in_pragma = true;
17630 bool nested = current_function_decl != NULL_TREE;
17631 if (nested)
17632 c_push_function_context ();
17633 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17634 reduc_id, default_function_type);
17635 current_function_decl = fndecl;
17636 allocate_struct_function (fndecl, true);
17637 push_scope ();
17638 tree stmt = push_stmt_list ();
17639 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17640 warn about these. */
17641 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17642 get_identifier ("omp_out"), type);
17643 DECL_ARTIFICIAL (omp_out) = 1;
17644 DECL_CONTEXT (omp_out) = fndecl;
17645 pushdecl (omp_out);
17646 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17647 get_identifier ("omp_in"), type);
17648 DECL_ARTIFICIAL (omp_in) = 1;
17649 DECL_CONTEXT (omp_in) = fndecl;
17650 pushdecl (omp_in);
17651 struct c_expr combiner = c_parser_expression (parser);
17652 struct c_expr initializer;
17653 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17654 bool bad = false;
17655 initializer.value = error_mark_node;
17656 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17657 bad = true;
17658 else if (c_parser_next_token_is (parser, CPP_NAME)
17659 && strcmp (IDENTIFIER_POINTER
17660 (c_parser_peek_token (parser)->value),
17661 "initializer") == 0)
17663 c_parser_consume_token (parser);
17664 pop_scope ();
17665 push_scope ();
17666 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17667 get_identifier ("omp_priv"), type);
17668 DECL_ARTIFICIAL (omp_priv) = 1;
17669 DECL_INITIAL (omp_priv) = error_mark_node;
17670 DECL_CONTEXT (omp_priv) = fndecl;
17671 pushdecl (omp_priv);
17672 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17673 get_identifier ("omp_orig"), type);
17674 DECL_ARTIFICIAL (omp_orig) = 1;
17675 DECL_CONTEXT (omp_orig) = fndecl;
17676 pushdecl (omp_orig);
17677 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17678 bad = true;
17679 else if (!c_parser_next_token_is (parser, CPP_NAME))
17681 c_parser_error (parser, "expected %<omp_priv%> or "
17682 "function-name");
17683 bad = true;
17685 else if (strcmp (IDENTIFIER_POINTER
17686 (c_parser_peek_token (parser)->value),
17687 "omp_priv") != 0)
17689 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17690 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17692 c_parser_error (parser, "expected function-name %<(%>");
17693 bad = true;
17695 else
17696 initializer = c_parser_postfix_expression (parser);
17697 if (initializer.value
17698 && TREE_CODE (initializer.value) == CALL_EXPR)
17700 int j;
17701 tree c = initializer.value;
17702 for (j = 0; j < call_expr_nargs (c); j++)
17704 tree a = CALL_EXPR_ARG (c, j);
17705 STRIP_NOPS (a);
17706 if (TREE_CODE (a) == ADDR_EXPR
17707 && TREE_OPERAND (a, 0) == omp_priv)
17708 break;
17710 if (j == call_expr_nargs (c))
17711 error ("one of the initializer call arguments should be "
17712 "%<&omp_priv%>");
17715 else
17717 c_parser_consume_token (parser);
17718 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17719 bad = true;
17720 else
17722 tree st = push_stmt_list ();
17723 location_t loc = c_parser_peek_token (parser)->location;
17724 rich_location richloc (line_table, loc);
17725 start_init (omp_priv, NULL_TREE, 0, &richloc);
17726 struct c_expr init = c_parser_initializer (parser);
17727 finish_init ();
17728 finish_decl (omp_priv, loc, init.value,
17729 init.original_type, NULL_TREE);
17730 pop_stmt_list (st);
17733 if (!bad
17734 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17735 bad = true;
17738 if (!bad)
17740 c_parser_skip_to_pragma_eol (parser);
17742 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17743 DECL_INITIAL (reduc_decl));
17744 DECL_INITIAL (reduc_decl) = t;
17745 DECL_SOURCE_LOCATION (omp_out) = rloc;
17746 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17747 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17748 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17749 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17750 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17751 if (omp_priv)
17753 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17754 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17755 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17756 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17757 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17758 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17759 walk_tree (&DECL_INITIAL (omp_priv),
17760 c_check_omp_declare_reduction_r,
17761 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17765 pop_stmt_list (stmt);
17766 pop_scope ();
17767 if (cfun->language != NULL)
17769 ggc_free (cfun->language);
17770 cfun->language = NULL;
17772 set_cfun (NULL);
17773 current_function_decl = NULL_TREE;
17774 if (nested)
17775 c_pop_function_context ();
17777 if (!clauses.is_empty ())
17779 parser->tokens = &parser->tokens_buf[0];
17780 parser->tokens_avail = tokens_avail;
17782 if (bad)
17783 goto fail;
17784 if (errs != errorcount)
17785 break;
17788 clauses.release ();
17789 types.release ();
17793 /* OpenMP 4.0
17794 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17795 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17796 initializer-clause[opt] new-line
17797 #pragma omp declare target new-line */
17799 static void
17800 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17802 c_parser_consume_pragma (parser);
17803 if (c_parser_next_token_is (parser, CPP_NAME))
17805 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17806 if (strcmp (p, "simd") == 0)
17808 /* c_parser_consume_token (parser); done in
17809 c_parser_omp_declare_simd. */
17810 c_parser_omp_declare_simd (parser, context);
17811 return;
17813 if (strcmp (p, "reduction") == 0)
17815 c_parser_consume_token (parser);
17816 c_parser_omp_declare_reduction (parser, context);
17817 return;
17819 if (!flag_openmp) /* flag_openmp_simd */
17821 c_parser_skip_to_pragma_eol (parser, false);
17822 return;
17824 if (strcmp (p, "target") == 0)
17826 c_parser_consume_token (parser);
17827 c_parser_omp_declare_target (parser);
17828 return;
17832 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17833 "or %<target%>");
17834 c_parser_skip_to_pragma_eol (parser);
17837 /* OpenMP 4.5:
17838 #pragma omp taskloop taskloop-clause[optseq] new-line
17839 for-loop
17841 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17842 for-loop */
17844 #define OMP_TASKLOOP_CLAUSE_MASK \
17845 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17860 static tree
17861 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17862 char *p_name, omp_clause_mask mask, tree *cclauses,
17863 bool *if_p)
17865 tree clauses, block, ret;
17867 strcat (p_name, " taskloop");
17868 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17870 if (c_parser_next_token_is (parser, CPP_NAME))
17872 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17874 if (strcmp (p, "simd") == 0)
17876 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17877 if (cclauses == NULL)
17878 cclauses = cclauses_buf;
17879 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17880 c_parser_consume_token (parser);
17881 if (!flag_openmp) /* flag_openmp_simd */
17882 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17883 if_p);
17884 block = c_begin_compound_stmt (true);
17885 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17886 block = c_end_compound_stmt (loc, block, true);
17887 if (ret == NULL)
17888 return ret;
17889 ret = make_node (OMP_TASKLOOP);
17890 TREE_TYPE (ret) = void_type_node;
17891 OMP_FOR_BODY (ret) = block;
17892 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17893 SET_EXPR_LOCATION (ret, loc);
17894 add_stmt (ret);
17895 return ret;
17898 if (!flag_openmp) /* flag_openmp_simd */
17900 c_parser_skip_to_pragma_eol (parser, false);
17901 return NULL_TREE;
17904 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17905 if (cclauses)
17907 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17908 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17911 block = c_begin_compound_stmt (true);
17912 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17913 block = c_end_compound_stmt (loc, block, true);
17914 add_stmt (block);
17916 return ret;
17919 /* Main entry point to parsing most OpenMP pragmas. */
17921 static void
17922 c_parser_omp_construct (c_parser *parser, bool *if_p)
17924 enum pragma_kind p_kind;
17925 location_t loc;
17926 tree stmt;
17927 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17928 omp_clause_mask mask (0);
17930 loc = c_parser_peek_token (parser)->location;
17931 p_kind = c_parser_peek_token (parser)->pragma_kind;
17932 c_parser_consume_pragma (parser);
17934 switch (p_kind)
17936 case PRAGMA_OACC_ATOMIC:
17937 c_parser_omp_atomic (loc, parser);
17938 return;
17939 case PRAGMA_OACC_CACHE:
17940 strcpy (p_name, "#pragma acc");
17941 stmt = c_parser_oacc_cache (loc, parser);
17942 break;
17943 case PRAGMA_OACC_DATA:
17944 stmt = c_parser_oacc_data (loc, parser, if_p);
17945 break;
17946 case PRAGMA_OACC_HOST_DATA:
17947 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17948 break;
17949 case PRAGMA_OACC_KERNELS:
17950 case PRAGMA_OACC_PARALLEL:
17951 strcpy (p_name, "#pragma acc");
17952 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17953 if_p);
17954 break;
17955 case PRAGMA_OACC_LOOP:
17956 strcpy (p_name, "#pragma acc");
17957 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17958 break;
17959 case PRAGMA_OACC_WAIT:
17960 strcpy (p_name, "#pragma wait");
17961 stmt = c_parser_oacc_wait (loc, parser, p_name);
17962 break;
17963 case PRAGMA_OMP_ATOMIC:
17964 c_parser_omp_atomic (loc, parser);
17965 return;
17966 case PRAGMA_OMP_CRITICAL:
17967 stmt = c_parser_omp_critical (loc, parser, if_p);
17968 break;
17969 case PRAGMA_OMP_DISTRIBUTE:
17970 strcpy (p_name, "#pragma omp");
17971 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17972 break;
17973 case PRAGMA_OMP_FOR:
17974 strcpy (p_name, "#pragma omp");
17975 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17976 break;
17977 case PRAGMA_OMP_MASTER:
17978 stmt = c_parser_omp_master (loc, parser, if_p);
17979 break;
17980 case PRAGMA_OMP_PARALLEL:
17981 strcpy (p_name, "#pragma omp");
17982 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17983 break;
17984 case PRAGMA_OMP_SECTIONS:
17985 strcpy (p_name, "#pragma omp");
17986 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17987 break;
17988 case PRAGMA_OMP_SIMD:
17989 strcpy (p_name, "#pragma omp");
17990 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17991 break;
17992 case PRAGMA_OMP_SINGLE:
17993 stmt = c_parser_omp_single (loc, parser, if_p);
17994 break;
17995 case PRAGMA_OMP_TASK:
17996 stmt = c_parser_omp_task (loc, parser, if_p);
17997 break;
17998 case PRAGMA_OMP_TASKGROUP:
17999 stmt = c_parser_omp_taskgroup (parser, if_p);
18000 break;
18001 case PRAGMA_OMP_TASKLOOP:
18002 strcpy (p_name, "#pragma omp");
18003 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18004 break;
18005 case PRAGMA_OMP_TEAMS:
18006 strcpy (p_name, "#pragma omp");
18007 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18008 break;
18009 default:
18010 gcc_unreachable ();
18013 if (stmt)
18014 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18018 /* OpenMP 2.5:
18019 # pragma omp threadprivate (variable-list) */
18021 static void
18022 c_parser_omp_threadprivate (c_parser *parser)
18024 tree vars, t;
18025 location_t loc;
18027 c_parser_consume_pragma (parser);
18028 loc = c_parser_peek_token (parser)->location;
18029 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18031 /* Mark every variable in VARS to be assigned thread local storage. */
18032 for (t = vars; t; t = TREE_CHAIN (t))
18034 tree v = TREE_PURPOSE (t);
18036 /* FIXME diagnostics: Ideally we should keep individual
18037 locations for all the variables in the var list to make the
18038 following errors more precise. Perhaps
18039 c_parser_omp_var_list_parens() should construct a list of
18040 locations to go along with the var list. */
18042 /* If V had already been marked threadprivate, it doesn't matter
18043 whether it had been used prior to this point. */
18044 if (!VAR_P (v))
18045 error_at (loc, "%qD is not a variable", v);
18046 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18047 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18048 else if (! is_global_var (v))
18049 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18050 else if (TREE_TYPE (v) == error_mark_node)
18052 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18053 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18054 else
18056 if (! DECL_THREAD_LOCAL_P (v))
18058 set_decl_tls_model (v, decl_default_tls_model (v));
18059 /* If rtl has been already set for this var, call
18060 make_decl_rtl once again, so that encode_section_info
18061 has a chance to look at the new decl flags. */
18062 if (DECL_RTL_SET_P (v))
18063 make_decl_rtl (v);
18065 C_DECL_THREADPRIVATE_P (v) = 1;
18069 c_parser_skip_to_pragma_eol (parser);
18072 /* Parse a transaction attribute (GCC Extension).
18074 transaction-attribute:
18075 attributes
18076 [ [ any-word ] ]
18078 The transactional memory language description is written for C++,
18079 and uses the C++0x attribute syntax. For compatibility, allow the
18080 bracket style for transactions in C as well. */
18082 static tree
18083 c_parser_transaction_attributes (c_parser *parser)
18085 tree attr_name, attr = NULL;
18087 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18088 return c_parser_attributes (parser);
18090 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18091 return NULL_TREE;
18092 c_parser_consume_token (parser);
18093 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18094 goto error1;
18096 attr_name = c_parser_attribute_any_word (parser);
18097 if (attr_name)
18099 c_parser_consume_token (parser);
18100 attr = build_tree_list (attr_name, NULL_TREE);
18102 else
18103 c_parser_error (parser, "expected identifier");
18105 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18106 error1:
18107 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18108 return attr;
18111 /* Parse a __transaction_atomic or __transaction_relaxed statement
18112 (GCC Extension).
18114 transaction-statement:
18115 __transaction_atomic transaction-attribute[opt] compound-statement
18116 __transaction_relaxed compound-statement
18118 Note that the only valid attribute is: "outer".
18121 static tree
18122 c_parser_transaction (c_parser *parser, enum rid keyword)
18124 unsigned int old_in = parser->in_transaction;
18125 unsigned int this_in = 1, new_in;
18126 location_t loc = c_parser_peek_token (parser)->location;
18127 tree stmt, attrs;
18129 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18130 || keyword == RID_TRANSACTION_RELAXED)
18131 && c_parser_next_token_is_keyword (parser, keyword));
18132 c_parser_consume_token (parser);
18134 if (keyword == RID_TRANSACTION_RELAXED)
18135 this_in |= TM_STMT_ATTR_RELAXED;
18136 else
18138 attrs = c_parser_transaction_attributes (parser);
18139 if (attrs)
18140 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18143 /* Keep track if we're in the lexical scope of an outer transaction. */
18144 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18146 parser->in_transaction = new_in;
18147 stmt = c_parser_compound_statement (parser);
18148 parser->in_transaction = old_in;
18150 if (flag_tm)
18151 stmt = c_finish_transaction (loc, stmt, this_in);
18152 else
18153 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18154 "%<__transaction_atomic%> without transactional memory support enabled"
18155 : "%<__transaction_relaxed %> "
18156 "without transactional memory support enabled"));
18158 return stmt;
18161 /* Parse a __transaction_atomic or __transaction_relaxed expression
18162 (GCC Extension).
18164 transaction-expression:
18165 __transaction_atomic ( expression )
18166 __transaction_relaxed ( expression )
18169 static struct c_expr
18170 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18172 struct c_expr ret;
18173 unsigned int old_in = parser->in_transaction;
18174 unsigned int this_in = 1;
18175 location_t loc = c_parser_peek_token (parser)->location;
18176 tree attrs;
18178 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18179 || keyword == RID_TRANSACTION_RELAXED)
18180 && c_parser_next_token_is_keyword (parser, keyword));
18181 c_parser_consume_token (parser);
18183 if (keyword == RID_TRANSACTION_RELAXED)
18184 this_in |= TM_STMT_ATTR_RELAXED;
18185 else
18187 attrs = c_parser_transaction_attributes (parser);
18188 if (attrs)
18189 this_in |= parse_tm_stmt_attr (attrs, 0);
18192 parser->in_transaction = this_in;
18193 matching_parens parens;
18194 if (parens.require_open (parser))
18196 tree expr = c_parser_expression (parser).value;
18197 ret.original_type = TREE_TYPE (expr);
18198 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18199 if (this_in & TM_STMT_ATTR_RELAXED)
18200 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18201 SET_EXPR_LOCATION (ret.value, loc);
18202 ret.original_code = TRANSACTION_EXPR;
18203 if (!parens.require_close (parser))
18205 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18206 goto error;
18209 else
18211 error:
18212 ret.value = error_mark_node;
18213 ret.original_code = ERROR_MARK;
18214 ret.original_type = NULL;
18216 parser->in_transaction = old_in;
18218 if (!flag_tm)
18219 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18220 "%<__transaction_atomic%> without transactional memory support enabled"
18221 : "%<__transaction_relaxed %> "
18222 "without transactional memory support enabled"));
18224 set_c_expr_source_range (&ret, loc, loc);
18226 return ret;
18229 /* Parse a __transaction_cancel statement (GCC Extension).
18231 transaction-cancel-statement:
18232 __transaction_cancel transaction-attribute[opt] ;
18234 Note that the only valid attribute is "outer".
18237 static tree
18238 c_parser_transaction_cancel (c_parser *parser)
18240 location_t loc = c_parser_peek_token (parser)->location;
18241 tree attrs;
18242 bool is_outer = false;
18244 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18245 c_parser_consume_token (parser);
18247 attrs = c_parser_transaction_attributes (parser);
18248 if (attrs)
18249 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18251 if (!flag_tm)
18253 error_at (loc, "%<__transaction_cancel%> without "
18254 "transactional memory support enabled");
18255 goto ret_error;
18257 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18259 error_at (loc, "%<__transaction_cancel%> within a "
18260 "%<__transaction_relaxed%>");
18261 goto ret_error;
18263 else if (is_outer)
18265 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18266 && !is_tm_may_cancel_outer (current_function_decl))
18268 error_at (loc, "outer %<__transaction_cancel%> not "
18269 "within outer %<__transaction_atomic%>");
18270 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18271 goto ret_error;
18274 else if (parser->in_transaction == 0)
18276 error_at (loc, "%<__transaction_cancel%> not within "
18277 "%<__transaction_atomic%>");
18278 goto ret_error;
18281 return add_stmt (build_tm_abort_call (loc, is_outer));
18283 ret_error:
18284 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18287 /* Parse a single source file. */
18289 void
18290 c_parse_file (void)
18292 /* Use local storage to begin. If the first token is a pragma, parse it.
18293 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18294 which will cause garbage collection. */
18295 c_parser tparser;
18297 memset (&tparser, 0, sizeof tparser);
18298 tparser.tokens = &tparser.tokens_buf[0];
18299 the_parser = &tparser;
18301 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18302 c_parser_pragma_pch_preprocess (&tparser);
18304 the_parser = ggc_alloc<c_parser> ();
18305 *the_parser = tparser;
18306 if (tparser.tokens == &tparser.tokens_buf[0])
18307 the_parser->tokens = &the_parser->tokens_buf[0];
18309 /* Initialize EH, if we've been told to do so. */
18310 if (flag_exceptions)
18311 using_eh_for_cleanups ();
18313 c_parser_translation_unit (the_parser);
18314 the_parser = NULL;
18317 /* Parse the body of a function declaration marked with "__RTL".
18319 The RTL parser works on the level of characters read from a
18320 FILE *, whereas c_parser works at the level of tokens.
18321 Square this circle by consuming all of the tokens up to and
18322 including the closing brace, recording the start/end of the RTL
18323 fragment, and reopening the file and re-reading the relevant
18324 lines within the RTL parser.
18326 This requires the opening and closing braces of the C function
18327 to be on separate lines from the RTL they wrap.
18329 Take ownership of START_WITH_PASS, if non-NULL. */
18331 void
18332 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18334 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18336 free (start_with_pass);
18337 return;
18340 location_t start_loc = c_parser_peek_token (parser)->location;
18342 /* Consume all tokens, up to the closing brace, handling
18343 matching pairs of braces in the rtl dump. */
18344 int num_open_braces = 1;
18345 while (1)
18347 switch (c_parser_peek_token (parser)->type)
18349 case CPP_OPEN_BRACE:
18350 num_open_braces++;
18351 break;
18352 case CPP_CLOSE_BRACE:
18353 if (--num_open_braces == 0)
18354 goto found_closing_brace;
18355 break;
18356 case CPP_EOF:
18357 error_at (start_loc, "no closing brace");
18358 free (start_with_pass);
18359 return;
18360 default:
18361 break;
18363 c_parser_consume_token (parser);
18366 found_closing_brace:
18367 /* At the closing brace; record its location. */
18368 location_t end_loc = c_parser_peek_token (parser)->location;
18370 /* Consume the closing brace. */
18371 c_parser_consume_token (parser);
18373 /* Invoke the RTL parser. */
18374 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18376 free (start_with_pass);
18377 return;
18380 /* If a pass name was provided for START_WITH_PASS, run the backend
18381 accordingly now, on the cfun created above, transferring
18382 ownership of START_WITH_PASS. */
18383 if (start_with_pass)
18384 run_rtl_passes (start_with_pass);
18387 #include "gt-c-c-parser.h"