PR c/84721
[official-gcc.git] / gcc / c / c-parser.c
blob945838cde59bba9e48fdcad53047b4d4e0800992
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
72 /* We need to walk over decls with incomplete struct/union/enum types
73 after parsing the whole translation unit.
74 In finish_decl(), if the decl is static, has incomplete
75 struct/union/enum type, it is appeneded to incomplete_record_decls.
76 In c_parser_translation_unit(), we iterate over incomplete_record_decls
77 and report error if any of the decls are still incomplete. */
79 vec<tree> incomplete_record_decls;
81 void
82 set_c_expr_source_range (c_expr *expr,
83 location_t start, location_t finish)
85 expr->src_range.m_start = start;
86 expr->src_range.m_finish = finish;
87 if (expr->value)
88 set_source_range (expr->value, start, finish);
91 void
92 set_c_expr_source_range (c_expr *expr,
93 source_range src_range)
95 expr->src_range = src_range;
96 if (expr->value)
97 set_source_range (expr->value, src_range);
101 /* Initialization routine for this file. */
103 void
104 c_parse_init (void)
106 /* The only initialization required is of the reserved word
107 identifiers. */
108 unsigned int i;
109 tree id;
110 int mask = 0;
112 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
113 the c_token structure. */
114 gcc_assert (RID_MAX <= 255);
116 mask |= D_CXXONLY;
117 if (!flag_isoc99)
118 mask |= D_C99;
119 if (flag_no_asm)
121 mask |= D_ASM | D_EXT;
122 if (!flag_isoc99)
123 mask |= D_EXT89;
125 if (!c_dialect_objc ())
126 mask |= D_OBJC | D_CXX_OBJC;
128 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
129 for (i = 0; i < num_c_common_reswords; i++)
131 /* If a keyword is disabled, do not enter it into the table
132 and so create a canonical spelling that isn't a keyword. */
133 if (c_common_reswords[i].disable & mask)
135 if (warn_cxx_compat
136 && (c_common_reswords[i].disable & D_CXXWARN))
138 id = get_identifier (c_common_reswords[i].word);
139 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
140 C_IS_RESERVED_WORD (id) = 1;
142 continue;
145 id = get_identifier (c_common_reswords[i].word);
146 C_SET_RID_CODE (id, c_common_reswords[i].rid);
147 C_IS_RESERVED_WORD (id) = 1;
148 ridpointers [(int) c_common_reswords[i].rid] = id;
151 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 /* We always create the symbols but they aren't always supported. */
154 char name[50];
155 sprintf (name, "__int%d", int_n_data[i].bitsize);
156 id = get_identifier (name);
157 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
158 C_IS_RESERVED_WORD (id) = 1;
162 /* A parser structure recording information about the state and
163 context of parsing. Includes lexer information with up to two
164 tokens of look-ahead; more are not needed for C. */
165 struct GTY(()) c_parser {
166 /* The look-ahead tokens. */
167 c_token * GTY((skip)) tokens;
168 /* Buffer for look-ahead tokens. */
169 c_token tokens_buf[4];
170 /* How many look-ahead tokens are available (0 - 4, or
171 more if parsing from pre-lexed tokens). */
172 unsigned int tokens_avail;
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
185 /* Objective-C specific parser/lexer information. */
187 /* True if we are in a context where the Objective-C "PQ" keywords
188 are considered keywords. */
189 BOOL_BITFIELD objc_pq_context : 1;
190 /* True if we are parsing a (potential) Objective-C foreach
191 statement. This is set to true after we parsed 'for (' and while
192 we wait for 'in' or ';' to decide if it's a standard C for loop or an
193 Objective-C foreach loop. */
194 BOOL_BITFIELD objc_could_be_foreach_context : 1;
195 /* The following flag is needed to contextualize Objective-C lexical
196 analysis. In some cases (e.g., 'int NSObject;'), it is
197 undesirable to bind an identifier to an Objective-C class, even
198 if a class with that name exists. */
199 BOOL_BITFIELD objc_need_raw_identifier : 1;
200 /* Nonzero if we're processing a __transaction statement. The value
201 is 1 | TM_STMT_ATTR_*. */
202 unsigned int in_transaction : 4;
203 /* True if we are in a context where the Objective-C "Property attribute"
204 keywords are valid. */
205 BOOL_BITFIELD objc_property_attr_context : 1;
207 /* Location of the last consumed token. */
208 location_t last_token_location;
211 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
213 c_token *
214 c_parser_tokens_buf (c_parser *parser, unsigned n)
216 return &parser->tokens_buf[n];
219 /* Return the error state of PARSER. */
221 bool
222 c_parser_error (c_parser *parser)
224 return parser->error;
227 /* Set the error state of PARSER to ERR. */
229 void
230 c_parser_set_error (c_parser *parser, bool err)
232 parser->error = err;
236 /* The actual parser and external interface. ??? Does this need to be
237 garbage-collected? */
239 static GTY (()) c_parser *the_parser;
241 /* Read in and lex a single token, storing it in *TOKEN. */
243 static void
244 c_lex_one_token (c_parser *parser, c_token *token)
246 timevar_push (TV_LEX);
248 token->type = c_lex_with_flags (&token->value, &token->location,
249 &token->flags,
250 (parser->lex_untranslated_string
251 ? C_LEX_STRING_NO_TRANSLATE : 0));
252 token->id_kind = C_ID_NONE;
253 token->keyword = RID_MAX;
254 token->pragma_kind = PRAGMA_NONE;
256 switch (token->type)
258 case CPP_NAME:
260 tree decl;
262 bool objc_force_identifier = parser->objc_need_raw_identifier;
263 if (c_dialect_objc ())
264 parser->objc_need_raw_identifier = false;
266 if (C_IS_RESERVED_WORD (token->value))
268 enum rid rid_code = C_RID_CODE (token->value);
270 if (rid_code == RID_CXX_COMPAT_WARN)
272 warning_at (token->location,
273 OPT_Wc___compat,
274 "identifier %qE conflicts with C++ keyword",
275 token->value);
277 else if (rid_code >= RID_FIRST_ADDR_SPACE
278 && rid_code <= RID_LAST_ADDR_SPACE)
280 addr_space_t as;
281 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
282 targetm.addr_space.diagnose_usage (as, token->location);
283 token->id_kind = C_ID_ADDRSPACE;
284 token->keyword = rid_code;
285 break;
287 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
289 /* We found an Objective-C "pq" keyword (in, out,
290 inout, bycopy, byref, oneway). They need special
291 care because the interpretation depends on the
292 context. */
293 if (parser->objc_pq_context)
295 token->type = CPP_KEYWORD;
296 token->keyword = rid_code;
297 break;
299 else if (parser->objc_could_be_foreach_context
300 && rid_code == RID_IN)
302 /* We are in Objective-C, inside a (potential)
303 foreach context (which means after having
304 parsed 'for (', but before having parsed ';'),
305 and we found 'in'. We consider it the keyword
306 which terminates the declaration at the
307 beginning of a foreach-statement. Note that
308 this means you can't use 'in' for anything else
309 in that context; in particular, in Objective-C
310 you can't use 'in' as the name of the running
311 variable in a C for loop. We could potentially
312 try to add code here to disambiguate, but it
313 seems a reasonable limitation. */
314 token->type = CPP_KEYWORD;
315 token->keyword = rid_code;
316 break;
318 /* Else, "pq" keywords outside of the "pq" context are
319 not keywords, and we fall through to the code for
320 normal tokens. */
322 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
324 /* We found an Objective-C "property attribute"
325 keyword (getter, setter, readonly, etc). These are
326 only valid in the property context. */
327 if (parser->objc_property_attr_context)
329 token->type = CPP_KEYWORD;
330 token->keyword = rid_code;
331 break;
333 /* Else they are not special keywords.
336 else if (c_dialect_objc ()
337 && (OBJC_IS_AT_KEYWORD (rid_code)
338 || OBJC_IS_CXX_KEYWORD (rid_code)))
340 /* We found one of the Objective-C "@" keywords (defs,
341 selector, synchronized, etc) or one of the
342 Objective-C "cxx" keywords (class, private,
343 protected, public, try, catch, throw) without a
344 preceding '@' sign. Do nothing and fall through to
345 the code for normal tokens (in C++ we would still
346 consider the CXX ones keywords, but not in C). */
349 else
351 token->type = CPP_KEYWORD;
352 token->keyword = rid_code;
353 break;
357 decl = lookup_name (token->value);
358 if (decl)
360 if (TREE_CODE (decl) == TYPE_DECL)
362 token->id_kind = C_ID_TYPENAME;
363 break;
366 else if (c_dialect_objc ())
368 tree objc_interface_decl = objc_is_class_name (token->value);
369 /* Objective-C class names are in the same namespace as
370 variables and typedefs, and hence are shadowed by local
371 declarations. */
372 if (objc_interface_decl
373 && (!objc_force_identifier || global_bindings_p ()))
375 token->value = objc_interface_decl;
376 token->id_kind = C_ID_CLASSNAME;
377 break;
380 token->id_kind = C_ID_ID;
382 break;
383 case CPP_AT_NAME:
384 /* This only happens in Objective-C; it must be a keyword. */
385 token->type = CPP_KEYWORD;
386 switch (C_RID_CODE (token->value))
388 /* Replace 'class' with '@class', 'private' with '@private',
389 etc. This prevents confusion with the C++ keyword
390 'class', and makes the tokens consistent with other
391 Objective-C 'AT' keywords. For example '@class' is
392 reported as RID_AT_CLASS which is consistent with
393 '@synchronized', which is reported as
394 RID_AT_SYNCHRONIZED.
396 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
397 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
398 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
399 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
400 case RID_THROW: token->keyword = RID_AT_THROW; break;
401 case RID_TRY: token->keyword = RID_AT_TRY; break;
402 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
403 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
404 default: token->keyword = C_RID_CODE (token->value);
406 break;
407 case CPP_COLON:
408 case CPP_COMMA:
409 case CPP_CLOSE_PAREN:
410 case CPP_SEMICOLON:
411 /* These tokens may affect the interpretation of any identifiers
412 following, if doing Objective-C. */
413 if (c_dialect_objc ())
414 parser->objc_need_raw_identifier = false;
415 break;
416 case CPP_PRAGMA:
417 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
418 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
419 token->value = NULL;
420 break;
421 default:
422 break;
424 timevar_pop (TV_LEX);
427 /* Return a pointer to the next token from PARSER, reading it in if
428 necessary. */
430 c_token *
431 c_parser_peek_token (c_parser *parser)
433 if (parser->tokens_avail == 0)
435 c_lex_one_token (parser, &parser->tokens[0]);
436 parser->tokens_avail = 1;
438 return &parser->tokens[0];
441 /* Return a pointer to the next-but-one token from PARSER, reading it
442 in if necessary. The next token is already read in. */
444 c_token *
445 c_parser_peek_2nd_token (c_parser *parser)
447 if (parser->tokens_avail >= 2)
448 return &parser->tokens[1];
449 gcc_assert (parser->tokens_avail == 1);
450 gcc_assert (parser->tokens[0].type != CPP_EOF);
451 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
452 c_lex_one_token (parser, &parser->tokens[1]);
453 parser->tokens_avail = 2;
454 return &parser->tokens[1];
457 /* Return a pointer to the Nth token from PARSER, reading it
458 in if necessary. The N-1th token is already read in. */
460 c_token *
461 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
463 /* N is 1-based, not zero-based. */
464 gcc_assert (n > 0);
466 if (parser->tokens_avail >= n)
467 return &parser->tokens[n - 1];
468 gcc_assert (parser->tokens_avail == n - 1);
469 c_lex_one_token (parser, &parser->tokens[n - 1]);
470 parser->tokens_avail = n;
471 return &parser->tokens[n - 1];
474 bool
475 c_keyword_starts_typename (enum rid keyword)
477 switch (keyword)
479 case RID_UNSIGNED:
480 case RID_LONG:
481 case RID_SHORT:
482 case RID_SIGNED:
483 case RID_COMPLEX:
484 case RID_INT:
485 case RID_CHAR:
486 case RID_FLOAT:
487 case RID_DOUBLE:
488 case RID_VOID:
489 case RID_DFLOAT32:
490 case RID_DFLOAT64:
491 case RID_DFLOAT128:
492 CASE_RID_FLOATN_NX:
493 case RID_BOOL:
494 case RID_ENUM:
495 case RID_STRUCT:
496 case RID_UNION:
497 case RID_TYPEOF:
498 case RID_CONST:
499 case RID_ATOMIC:
500 case RID_VOLATILE:
501 case RID_RESTRICT:
502 case RID_ATTRIBUTE:
503 case RID_FRACT:
504 case RID_ACCUM:
505 case RID_SAT:
506 case RID_AUTO_TYPE:
507 case RID_ALIGNAS:
508 return true;
509 default:
510 if (keyword >= RID_FIRST_INT_N
511 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
512 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
513 return true;
514 return false;
518 /* Return true if TOKEN can start a type name,
519 false otherwise. */
520 bool
521 c_token_starts_typename (c_token *token)
523 switch (token->type)
525 case CPP_NAME:
526 switch (token->id_kind)
528 case C_ID_ID:
529 return false;
530 case C_ID_ADDRSPACE:
531 return true;
532 case C_ID_TYPENAME:
533 return true;
534 case C_ID_CLASSNAME:
535 gcc_assert (c_dialect_objc ());
536 return true;
537 default:
538 gcc_unreachable ();
540 case CPP_KEYWORD:
541 return c_keyword_starts_typename (token->keyword);
542 case CPP_LESS:
543 if (c_dialect_objc ())
544 return true;
545 return false;
546 default:
547 return false;
551 /* Return true if the next token from PARSER can start a type name,
552 false otherwise. LA specifies how to do lookahead in order to
553 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
555 static inline bool
556 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
558 c_token *token = c_parser_peek_token (parser);
559 if (c_token_starts_typename (token))
560 return true;
562 /* Try a bit harder to detect an unknown typename. */
563 if (la != cla_prefer_id
564 && token->type == CPP_NAME
565 && token->id_kind == C_ID_ID
567 /* Do not try too hard when we could have "object in array". */
568 && !parser->objc_could_be_foreach_context
570 && (la == cla_prefer_type
571 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
572 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
574 /* Only unknown identifiers. */
575 && !lookup_name (token->value))
576 return true;
578 return false;
581 /* Return true if TOKEN is a type qualifier, false otherwise. */
582 static bool
583 c_token_is_qualifier (c_token *token)
585 switch (token->type)
587 case CPP_NAME:
588 switch (token->id_kind)
590 case C_ID_ADDRSPACE:
591 return true;
592 default:
593 return false;
595 case CPP_KEYWORD:
596 switch (token->keyword)
598 case RID_CONST:
599 case RID_VOLATILE:
600 case RID_RESTRICT:
601 case RID_ATTRIBUTE:
602 case RID_ATOMIC:
603 return true;
604 default:
605 return false;
607 case CPP_LESS:
608 return false;
609 default:
610 gcc_unreachable ();
614 /* Return true if the next token from PARSER is a type qualifier,
615 false otherwise. */
616 static inline bool
617 c_parser_next_token_is_qualifier (c_parser *parser)
619 c_token *token = c_parser_peek_token (parser);
620 return c_token_is_qualifier (token);
623 /* Return true if TOKEN can start declaration specifiers, false
624 otherwise. */
625 static bool
626 c_token_starts_declspecs (c_token *token)
628 switch (token->type)
630 case CPP_NAME:
631 switch (token->id_kind)
633 case C_ID_ID:
634 return false;
635 case C_ID_ADDRSPACE:
636 return true;
637 case C_ID_TYPENAME:
638 return true;
639 case C_ID_CLASSNAME:
640 gcc_assert (c_dialect_objc ());
641 return true;
642 default:
643 gcc_unreachable ();
645 case CPP_KEYWORD:
646 switch (token->keyword)
648 case RID_STATIC:
649 case RID_EXTERN:
650 case RID_REGISTER:
651 case RID_TYPEDEF:
652 case RID_INLINE:
653 case RID_NORETURN:
654 case RID_AUTO:
655 case RID_THREAD:
656 case RID_UNSIGNED:
657 case RID_LONG:
658 case RID_SHORT:
659 case RID_SIGNED:
660 case RID_COMPLEX:
661 case RID_INT:
662 case RID_CHAR:
663 case RID_FLOAT:
664 case RID_DOUBLE:
665 case RID_VOID:
666 case RID_DFLOAT32:
667 case RID_DFLOAT64:
668 case RID_DFLOAT128:
669 CASE_RID_FLOATN_NX:
670 case RID_BOOL:
671 case RID_ENUM:
672 case RID_STRUCT:
673 case RID_UNION:
674 case RID_TYPEOF:
675 case RID_CONST:
676 case RID_VOLATILE:
677 case RID_RESTRICT:
678 case RID_ATTRIBUTE:
679 case RID_FRACT:
680 case RID_ACCUM:
681 case RID_SAT:
682 case RID_ALIGNAS:
683 case RID_ATOMIC:
684 case RID_AUTO_TYPE:
685 return true;
686 default:
687 if (token->keyword >= RID_FIRST_INT_N
688 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
689 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
690 return true;
691 return false;
693 case CPP_LESS:
694 if (c_dialect_objc ())
695 return true;
696 return false;
697 default:
698 return false;
703 /* Return true if TOKEN can start declaration specifiers or a static
704 assertion, false otherwise. */
705 static bool
706 c_token_starts_declaration (c_token *token)
708 if (c_token_starts_declspecs (token)
709 || token->keyword == RID_STATIC_ASSERT)
710 return true;
711 else
712 return false;
715 /* Return true if the next token from PARSER can start declaration
716 specifiers, false otherwise. */
717 bool
718 c_parser_next_token_starts_declspecs (c_parser *parser)
720 c_token *token = c_parser_peek_token (parser);
722 /* In Objective-C, a classname normally starts a declspecs unless it
723 is immediately followed by a dot. In that case, it is the
724 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
725 setter/getter on the class. c_token_starts_declspecs() can't
726 differentiate between the two cases because it only checks the
727 current token, so we have a special check here. */
728 if (c_dialect_objc ()
729 && token->type == CPP_NAME
730 && token->id_kind == C_ID_CLASSNAME
731 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
732 return false;
734 return c_token_starts_declspecs (token);
737 /* Return true if the next tokens from PARSER can start declaration
738 specifiers or a static assertion, false otherwise. */
739 bool
740 c_parser_next_tokens_start_declaration (c_parser *parser)
742 c_token *token = c_parser_peek_token (parser);
744 /* Same as above. */
745 if (c_dialect_objc ()
746 && token->type == CPP_NAME
747 && token->id_kind == C_ID_CLASSNAME
748 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
749 return false;
751 /* Labels do not start declarations. */
752 if (token->type == CPP_NAME
753 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
754 return false;
756 if (c_token_starts_declaration (token))
757 return true;
759 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
760 return true;
762 return false;
765 /* Consume the next token from PARSER. */
767 void
768 c_parser_consume_token (c_parser *parser)
770 gcc_assert (parser->tokens_avail >= 1);
771 gcc_assert (parser->tokens[0].type != CPP_EOF);
772 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
773 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
774 parser->last_token_location = parser->tokens[0].location;
775 if (parser->tokens != &parser->tokens_buf[0])
776 parser->tokens++;
777 else if (parser->tokens_avail == 2)
778 parser->tokens[0] = parser->tokens[1];
779 parser->tokens_avail--;
782 /* Expect the current token to be a #pragma. Consume it and remember
783 that we've begun parsing a pragma. */
785 static void
786 c_parser_consume_pragma (c_parser *parser)
788 gcc_assert (!parser->in_pragma);
789 gcc_assert (parser->tokens_avail >= 1);
790 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
791 if (parser->tokens != &parser->tokens_buf[0])
792 parser->tokens++;
793 else if (parser->tokens_avail == 2)
794 parser->tokens[0] = parser->tokens[1];
795 parser->tokens_avail--;
796 parser->in_pragma = true;
799 /* Update the global input_location from TOKEN. */
800 static inline void
801 c_parser_set_source_position_from_token (c_token *token)
803 if (token->type != CPP_EOF)
805 input_location = token->location;
809 /* Helper function for c_parser_error.
810 Having peeked a token of kind TOK1_KIND that might signify
811 a conflict marker, peek successor tokens to determine
812 if we actually do have a conflict marker.
813 Specifically, we consider a run of 7 '<', '=' or '>' characters
814 at the start of a line as a conflict marker.
815 These come through the lexer as three pairs and a single,
816 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
817 If it returns true, *OUT_LOC is written to with the location/range
818 of the marker. */
820 static bool
821 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
822 location_t *out_loc)
824 c_token *token2 = c_parser_peek_2nd_token (parser);
825 if (token2->type != tok1_kind)
826 return false;
827 c_token *token3 = c_parser_peek_nth_token (parser, 3);
828 if (token3->type != tok1_kind)
829 return false;
830 c_token *token4 = c_parser_peek_nth_token (parser, 4);
831 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
832 return false;
834 /* It must be at the start of the line. */
835 location_t start_loc = c_parser_peek_token (parser)->location;
836 if (LOCATION_COLUMN (start_loc) != 1)
837 return false;
839 /* We have a conflict marker. Construct a location of the form:
840 <<<<<<<
841 ^~~~~~~
842 with start == caret, finishing at the end of the marker. */
843 location_t finish_loc = get_finish (token4->location);
844 *out_loc = make_location (start_loc, start_loc, finish_loc);
846 return true;
849 /* Issue a diagnostic of the form
850 FILE:LINE: MESSAGE before TOKEN
851 where TOKEN is the next token in the input stream of PARSER.
852 MESSAGE (specified by the caller) is usually of the form "expected
853 OTHER-TOKEN".
855 Use RICHLOC as the location of the diagnostic.
857 Do not issue a diagnostic if still recovering from an error.
859 Return true iff an error was actually emitted.
861 ??? This is taken from the C++ parser, but building up messages in
862 this way is not i18n-friendly and some other approach should be
863 used. */
865 static bool
866 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
867 rich_location *richloc)
869 c_token *token = c_parser_peek_token (parser);
870 if (parser->error)
871 return false;
872 parser->error = true;
873 if (!gmsgid)
874 return false;
876 /* If this is actually a conflict marker, report it as such. */
877 if (token->type == CPP_LSHIFT
878 || token->type == CPP_RSHIFT
879 || token->type == CPP_EQ_EQ)
881 location_t loc;
882 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
884 error_at (loc, "version control conflict marker in file");
885 return true;
889 c_parse_error (gmsgid,
890 /* Because c_parse_error does not understand
891 CPP_KEYWORD, keywords are treated like
892 identifiers. */
893 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
894 /* ??? The C parser does not save the cpp flags of a
895 token, we need to pass 0 here and we will not get
896 the source spelling of some tokens but rather the
897 canonical spelling. */
898 token->value, /*flags=*/0, richloc);
899 return true;
902 /* As c_parser_error_richloc, but issue the message at the
903 location of PARSER's next token, or at input_location
904 if the next token is EOF. */
906 bool
907 c_parser_error (c_parser *parser, const char *gmsgid)
909 c_token *token = c_parser_peek_token (parser);
910 c_parser_set_source_position_from_token (token);
911 rich_location richloc (line_table, input_location);
912 return c_parser_error_richloc (parser, gmsgid, &richloc);
915 /* Some tokens naturally come in pairs e.g.'(' and ')'.
916 This class is for tracking such a matching pair of symbols.
917 In particular, it tracks the location of the first token,
918 so that if the second token is missing, we can highlight the
919 location of the first token when notifying the user about the
920 problem. */
922 template <typename traits_t>
923 class token_pair
925 public:
926 /* token_pair's ctor. */
927 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
929 /* If the next token is the opening symbol for this pair, consume it and
930 return true.
931 Otherwise, issue an error and return false.
932 In either case, record the location of the opening token. */
934 bool require_open (c_parser *parser)
936 c_token *token = c_parser_peek_token (parser);
937 if (token)
938 m_open_loc = token->location;
940 return c_parser_require (parser, traits_t::open_token_type,
941 traits_t::open_gmsgid);
944 /* Consume the next token from PARSER, recording its location as
945 that of the opening token within the pair. */
947 void consume_open (c_parser *parser)
949 c_token *token = c_parser_peek_token (parser);
950 gcc_assert (token->type == traits_t::open_token_type);
951 m_open_loc = token->location;
952 c_parser_consume_token (parser);
955 /* If the next token is the closing symbol for this pair, consume it
956 and return true.
957 Otherwise, issue an error, highlighting the location of the
958 corresponding opening token, and return false. */
960 bool require_close (c_parser *parser) const
962 return c_parser_require (parser, traits_t::close_token_type,
963 traits_t::close_gmsgid, m_open_loc);
966 /* Like token_pair::require_close, except that tokens will be skipped
967 until the desired token is found. An error message is still produced
968 if the next token is not as expected. */
970 void skip_until_found_close (c_parser *parser) const
972 c_parser_skip_until_found (parser, traits_t::close_token_type,
973 traits_t::close_gmsgid, m_open_loc);
976 private:
977 location_t m_open_loc;
980 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
982 struct matching_paren_traits
984 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
985 static const char * const open_gmsgid;
986 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
987 static const char * const close_gmsgid;
990 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
991 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
993 /* "matching_parens" is a token_pair<T> class for tracking matching
994 pairs of parentheses. */
996 typedef token_pair<matching_paren_traits> matching_parens;
998 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1000 struct matching_brace_traits
1002 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1003 static const char * const open_gmsgid;
1004 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1005 static const char * const close_gmsgid;
1008 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1009 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1011 /* "matching_braces" is a token_pair<T> class for tracking matching
1012 pairs of braces. */
1014 typedef token_pair<matching_brace_traits> matching_braces;
1016 /* Get a description of the matching symbol to TYPE e.g. "(" for
1017 CPP_CLOSE_PAREN. */
1019 static const char *
1020 get_matching_symbol (enum cpp_ttype type)
1022 switch (type)
1024 default:
1025 gcc_unreachable ();
1026 return "";
1027 case CPP_CLOSE_PAREN:
1028 return "(";
1029 case CPP_CLOSE_BRACE:
1030 return "{";
1034 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1035 issue the error MSGID. If MSGID is NULL then a message has already
1036 been produced and no message will be produced this time. Returns
1037 true if found, false otherwise.
1039 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1040 within any error as the location of an "opening" token matching
1041 the close token TYPE (e.g. the location of the '(' when TYPE is
1042 CPP_CLOSE_PAREN).
1044 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1045 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1046 attempt to generate a fix-it hint for the problem.
1047 Otherwise msgid describes multiple token types (e.g.
1048 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1049 generate a fix-it hint. */
1051 bool
1052 c_parser_require (c_parser *parser,
1053 enum cpp_ttype type,
1054 const char *msgid,
1055 location_t matching_location,
1056 bool type_is_unique)
1058 if (c_parser_next_token_is (parser, type))
1060 c_parser_consume_token (parser);
1061 return true;
1063 else
1065 location_t next_token_loc = c_parser_peek_token (parser)->location;
1066 gcc_rich_location richloc (next_token_loc);
1068 /* Potentially supply a fix-it hint, suggesting to add the
1069 missing token immediately after the *previous* token.
1070 This may move the primary location within richloc. */
1071 if (!parser->error && type_is_unique)
1072 maybe_suggest_missing_token_insertion (&richloc, type,
1073 parser->last_token_location);
1075 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1076 Attempt to consolidate diagnostics by printing it as a
1077 secondary range within the main diagnostic. */
1078 bool added_matching_location = false;
1079 if (matching_location != UNKNOWN_LOCATION)
1080 added_matching_location
1081 = richloc.add_location_if_nearby (matching_location);
1083 if (c_parser_error_richloc (parser, msgid, &richloc))
1084 /* If we weren't able to consolidate matching_location, then
1085 print it as a secondary diagnostic. */
1086 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1087 inform (matching_location, "to match this %qs",
1088 get_matching_symbol (type));
1090 return false;
1094 /* If the next token is the indicated keyword, consume it. Otherwise,
1095 issue the error MSGID. Returns true if found, false otherwise. */
1097 static bool
1098 c_parser_require_keyword (c_parser *parser,
1099 enum rid keyword,
1100 const char *msgid)
1102 if (c_parser_next_token_is_keyword (parser, keyword))
1104 c_parser_consume_token (parser);
1105 return true;
1107 else
1109 c_parser_error (parser, msgid);
1110 return false;
1114 /* Like c_parser_require, except that tokens will be skipped until the
1115 desired token is found. An error message is still produced if the
1116 next token is not as expected. If MSGID is NULL then a message has
1117 already been produced and no message will be produced this
1118 time.
1120 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1121 within any error as the location of an "opening" token matching
1122 the close token TYPE (e.g. the location of the '(' when TYPE is
1123 CPP_CLOSE_PAREN). */
1125 void
1126 c_parser_skip_until_found (c_parser *parser,
1127 enum cpp_ttype type,
1128 const char *msgid,
1129 location_t matching_location)
1131 unsigned nesting_depth = 0;
1133 if (c_parser_require (parser, type, msgid, matching_location))
1134 return;
1136 /* Skip tokens until the desired token is found. */
1137 while (true)
1139 /* Peek at the next token. */
1140 c_token *token = c_parser_peek_token (parser);
1141 /* If we've reached the token we want, consume it and stop. */
1142 if (token->type == type && !nesting_depth)
1144 c_parser_consume_token (parser);
1145 break;
1148 /* If we've run out of tokens, stop. */
1149 if (token->type == CPP_EOF)
1150 return;
1151 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1152 return;
1153 if (token->type == CPP_OPEN_BRACE
1154 || token->type == CPP_OPEN_PAREN
1155 || token->type == CPP_OPEN_SQUARE)
1156 ++nesting_depth;
1157 else if (token->type == CPP_CLOSE_BRACE
1158 || token->type == CPP_CLOSE_PAREN
1159 || token->type == CPP_CLOSE_SQUARE)
1161 if (nesting_depth-- == 0)
1162 break;
1164 /* Consume this token. */
1165 c_parser_consume_token (parser);
1167 parser->error = false;
1170 /* Skip tokens until the end of a parameter is found, but do not
1171 consume the comma, semicolon or closing delimiter. */
1173 static void
1174 c_parser_skip_to_end_of_parameter (c_parser *parser)
1176 unsigned nesting_depth = 0;
1178 while (true)
1180 c_token *token = c_parser_peek_token (parser);
1181 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1182 && !nesting_depth)
1183 break;
1184 /* If we've run out of tokens, stop. */
1185 if (token->type == CPP_EOF)
1186 return;
1187 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1188 return;
1189 if (token->type == CPP_OPEN_BRACE
1190 || token->type == CPP_OPEN_PAREN
1191 || token->type == CPP_OPEN_SQUARE)
1192 ++nesting_depth;
1193 else if (token->type == CPP_CLOSE_BRACE
1194 || token->type == CPP_CLOSE_PAREN
1195 || token->type == CPP_CLOSE_SQUARE)
1197 if (nesting_depth-- == 0)
1198 break;
1200 /* Consume this token. */
1201 c_parser_consume_token (parser);
1203 parser->error = false;
1206 /* Expect to be at the end of the pragma directive and consume an
1207 end of line marker. */
1209 static void
1210 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1212 gcc_assert (parser->in_pragma);
1213 parser->in_pragma = false;
1215 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1216 c_parser_error (parser, "expected end of line");
1218 cpp_ttype token_type;
1221 c_token *token = c_parser_peek_token (parser);
1222 token_type = token->type;
1223 if (token_type == CPP_EOF)
1224 break;
1225 c_parser_consume_token (parser);
1227 while (token_type != CPP_PRAGMA_EOL);
1229 parser->error = false;
1232 /* Skip tokens until we have consumed an entire block, or until we
1233 have consumed a non-nested ';'. */
1235 static void
1236 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1238 unsigned nesting_depth = 0;
1239 bool save_error = parser->error;
1241 while (true)
1243 c_token *token;
1245 /* Peek at the next token. */
1246 token = c_parser_peek_token (parser);
1248 switch (token->type)
1250 case CPP_EOF:
1251 return;
1253 case CPP_PRAGMA_EOL:
1254 if (parser->in_pragma)
1255 return;
1256 break;
1258 case CPP_SEMICOLON:
1259 /* If the next token is a ';', we have reached the
1260 end of the statement. */
1261 if (!nesting_depth)
1263 /* Consume the ';'. */
1264 c_parser_consume_token (parser);
1265 goto finished;
1267 break;
1269 case CPP_CLOSE_BRACE:
1270 /* If the next token is a non-nested '}', then we have
1271 reached the end of the current block. */
1272 if (nesting_depth == 0 || --nesting_depth == 0)
1274 c_parser_consume_token (parser);
1275 goto finished;
1277 break;
1279 case CPP_OPEN_BRACE:
1280 /* If it the next token is a '{', then we are entering a new
1281 block. Consume the entire block. */
1282 ++nesting_depth;
1283 break;
1285 case CPP_PRAGMA:
1286 /* If we see a pragma, consume the whole thing at once. We
1287 have some safeguards against consuming pragmas willy-nilly.
1288 Normally, we'd expect to be here with parser->error set,
1289 which disables these safeguards. But it's possible to get
1290 here for secondary error recovery, after parser->error has
1291 been cleared. */
1292 c_parser_consume_pragma (parser);
1293 c_parser_skip_to_pragma_eol (parser);
1294 parser->error = save_error;
1295 continue;
1297 default:
1298 break;
1301 c_parser_consume_token (parser);
1304 finished:
1305 parser->error = false;
1308 /* CPP's options (initialized by c-opts.c). */
1309 extern cpp_options *cpp_opts;
1311 /* Save the warning flags which are controlled by __extension__. */
1313 static inline int
1314 disable_extension_diagnostics (void)
1316 int ret = (pedantic
1317 | (warn_pointer_arith << 1)
1318 | (warn_traditional << 2)
1319 | (flag_iso << 3)
1320 | (warn_long_long << 4)
1321 | (warn_cxx_compat << 5)
1322 | (warn_overlength_strings << 6)
1323 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1324 play tricks to properly restore it. */
1325 | ((warn_c90_c99_compat == 1) << 7)
1326 | ((warn_c90_c99_compat == -1) << 8)
1327 /* Similarly for warn_c99_c11_compat. */
1328 | ((warn_c99_c11_compat == 1) << 9)
1329 | ((warn_c99_c11_compat == -1) << 10)
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, unsigned short, bool *);
1410 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1411 static void c_parser_for_statement (c_parser *, bool, unsigned short, 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 /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721. */
1658 if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1659 return;
1661 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1662 SET_EXPR_LOCATION (stmt, loc);
1663 add_stmt (stmt);
1666 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1667 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1668 is accepted; otherwise (old-style parameter declarations) only other
1669 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1670 assertion is accepted; otherwise (old-style parameter declarations)
1671 it is not. If NESTED is true, we are inside a function or parsing
1672 old-style parameter declarations; any functions encountered are
1673 nested functions and declaration specifiers are required; otherwise
1674 we are at top level and functions are normal functions and
1675 declaration specifiers may be optional. If EMPTY_OK is true, empty
1676 declarations are OK (subject to all other constraints); otherwise
1677 (old-style parameter declarations) they are diagnosed. If
1678 START_ATTR_OK is true, the declaration specifiers may start with
1679 attributes; otherwise they may not.
1680 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1681 declaration when parsing an Objective-C foreach statement.
1682 FALLTHRU_ATTR_P is used to signal whether this function parsed
1683 "__attribute__((fallthrough));".
1685 declaration:
1686 declaration-specifiers init-declarator-list[opt] ;
1687 static_assert-declaration
1689 function-definition:
1690 declaration-specifiers[opt] declarator declaration-list[opt]
1691 compound-statement
1693 declaration-list:
1694 declaration
1695 declaration-list declaration
1697 init-declarator-list:
1698 init-declarator
1699 init-declarator-list , init-declarator
1701 init-declarator:
1702 declarator simple-asm-expr[opt] attributes[opt]
1703 declarator simple-asm-expr[opt] attributes[opt] = initializer
1705 GNU extensions:
1707 nested-function-definition:
1708 declaration-specifiers declarator declaration-list[opt]
1709 compound-statement
1711 attribute ;
1713 Objective-C:
1714 attributes objc-class-definition
1715 attributes objc-category-definition
1716 attributes objc-protocol-definition
1718 The simple-asm-expr and attributes are GNU extensions.
1720 This function does not handle __extension__; that is handled in its
1721 callers. ??? Following the old parser, __extension__ may start
1722 external declarations, declarations in functions and declarations
1723 at the start of "for" loops, but not old-style parameter
1724 declarations.
1726 C99 requires declaration specifiers in a function definition; the
1727 absence is diagnosed through the diagnosis of implicit int. In GNU
1728 C we also allow but diagnose declarations without declaration
1729 specifiers, but only at top level (elsewhere they conflict with
1730 other syntax).
1732 In Objective-C, declarations of the looping variable in a foreach
1733 statement are exceptionally terminated by 'in' (for example, 'for
1734 (NSObject *object in array) { ... }').
1736 OpenMP:
1738 declaration:
1739 threadprivate-directive
1741 GIMPLE:
1743 gimple-function-definition:
1744 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1745 declaration-list[opt] compound-statement
1747 rtl-function-definition:
1748 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1749 declaration-list[opt] compound-statement */
1751 static void
1752 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1753 bool static_assert_ok, bool empty_ok,
1754 bool nested, bool start_attr_ok,
1755 tree *objc_foreach_object_declaration,
1756 vec<c_token> omp_declare_simd_clauses,
1757 struct oacc_routine_data *oacc_routine_data,
1758 bool *fallthru_attr_p)
1760 struct c_declspecs *specs;
1761 tree prefix_attrs;
1762 tree all_prefix_attrs;
1763 bool diagnosed_no_specs = false;
1764 location_t here = c_parser_peek_token (parser)->location;
1766 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1768 if (static_assert_ok
1769 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1771 c_parser_static_assert_declaration (parser);
1772 return;
1774 specs = build_null_declspecs ();
1776 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1777 if (c_parser_peek_token (parser)->type == CPP_NAME
1778 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1779 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1780 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1781 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1783 tree name = c_parser_peek_token (parser)->value;
1785 /* Issue a warning about NAME being an unknown type name, perhaps
1786 with some kind of hint.
1787 If the user forgot a "struct" etc, suggest inserting
1788 it. Otherwise, attempt to look for misspellings. */
1789 gcc_rich_location richloc (here);
1790 if (tag_exists_p (RECORD_TYPE, name))
1792 /* This is not C++ with its implicit typedef. */
1793 richloc.add_fixit_insert_before ("struct ");
1794 error_at (&richloc,
1795 "unknown type name %qE;"
1796 " use %<struct%> keyword to refer to the type",
1797 name);
1799 else if (tag_exists_p (UNION_TYPE, name))
1801 richloc.add_fixit_insert_before ("union ");
1802 error_at (&richloc,
1803 "unknown type name %qE;"
1804 " use %<union%> keyword to refer to the type",
1805 name);
1807 else if (tag_exists_p (ENUMERAL_TYPE, name))
1809 richloc.add_fixit_insert_before ("enum ");
1810 error_at (&richloc,
1811 "unknown type name %qE;"
1812 " use %<enum%> keyword to refer to the type",
1813 name);
1815 else
1817 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1818 here);
1819 if (hint)
1821 richloc.add_fixit_replace (hint.suggestion ());
1822 error_at (&richloc,
1823 "unknown type name %qE; did you mean %qs?",
1824 name, hint.suggestion ());
1826 else
1827 error_at (here, "unknown type name %qE", name);
1830 /* Parse declspecs normally to get a correct pointer type, but avoid
1831 a further "fails to be a type name" error. Refuse nested functions
1832 since it is not how the user likely wants us to recover. */
1833 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1834 c_parser_peek_token (parser)->keyword = RID_VOID;
1835 c_parser_peek_token (parser)->value = error_mark_node;
1836 fndef_ok = !nested;
1839 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1840 true, true, cla_nonabstract_decl);
1841 if (parser->error)
1843 c_parser_skip_to_end_of_block_or_statement (parser);
1844 return;
1846 if (nested && !specs->declspecs_seen_p)
1848 c_parser_error (parser, "expected declaration specifiers");
1849 c_parser_skip_to_end_of_block_or_statement (parser);
1850 return;
1853 finish_declspecs (specs);
1854 bool auto_type_p = specs->typespec_word == cts_auto_type;
1855 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1857 if (auto_type_p)
1858 error_at (here, "%<__auto_type%> in empty declaration");
1859 else if (specs->typespec_kind == ctsk_none
1860 && attribute_fallthrough_p (specs->attrs))
1862 if (fallthru_attr_p != NULL)
1863 *fallthru_attr_p = true;
1864 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1865 void_type_node, 0);
1866 add_stmt (fn);
1868 else if (empty_ok)
1869 shadow_tag (specs);
1870 else
1872 shadow_tag_warned (specs, 1);
1873 pedwarn (here, 0, "empty declaration");
1875 c_parser_consume_token (parser);
1876 if (oacc_routine_data)
1877 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1878 return;
1881 /* Provide better error recovery. Note that a type name here is usually
1882 better diagnosed as a redeclaration. */
1883 if (empty_ok
1884 && specs->typespec_kind == ctsk_tagdef
1885 && c_parser_next_token_starts_declspecs (parser)
1886 && !c_parser_next_token_is (parser, CPP_NAME))
1888 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1889 parser->error = false;
1890 shadow_tag_warned (specs, 1);
1891 return;
1893 else if (c_dialect_objc () && !auto_type_p)
1895 /* Prefix attributes are an error on method decls. */
1896 switch (c_parser_peek_token (parser)->type)
1898 case CPP_PLUS:
1899 case CPP_MINUS:
1900 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1901 return;
1902 if (specs->attrs)
1904 warning_at (c_parser_peek_token (parser)->location,
1905 OPT_Wattributes,
1906 "prefix attributes are ignored for methods");
1907 specs->attrs = NULL_TREE;
1909 if (fndef_ok)
1910 c_parser_objc_method_definition (parser);
1911 else
1912 c_parser_objc_methodproto (parser);
1913 return;
1914 break;
1915 default:
1916 break;
1918 /* This is where we parse 'attributes @interface ...',
1919 'attributes @implementation ...', 'attributes @protocol ...'
1920 (where attributes could be, for example, __attribute__
1921 ((deprecated)).
1923 switch (c_parser_peek_token (parser)->keyword)
1925 case RID_AT_INTERFACE:
1927 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1928 return;
1929 c_parser_objc_class_definition (parser, specs->attrs);
1930 return;
1932 break;
1933 case RID_AT_IMPLEMENTATION:
1935 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1936 return;
1937 if (specs->attrs)
1939 warning_at (c_parser_peek_token (parser)->location,
1940 OPT_Wattributes,
1941 "prefix attributes are ignored for implementations");
1942 specs->attrs = NULL_TREE;
1944 c_parser_objc_class_definition (parser, NULL_TREE);
1945 return;
1947 break;
1948 case RID_AT_PROTOCOL:
1950 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1951 return;
1952 c_parser_objc_protocol_definition (parser, specs->attrs);
1953 return;
1955 break;
1956 case RID_AT_ALIAS:
1957 case RID_AT_CLASS:
1958 case RID_AT_END:
1959 case RID_AT_PROPERTY:
1960 if (specs->attrs)
1962 c_parser_error (parser, "unexpected attribute");
1963 specs->attrs = NULL;
1965 break;
1966 default:
1967 break;
1970 else if (attribute_fallthrough_p (specs->attrs))
1971 warning_at (here, OPT_Wattributes,
1972 "%<fallthrough%> attribute not followed by %<;%>");
1974 pending_xref_error ();
1975 prefix_attrs = specs->attrs;
1976 all_prefix_attrs = prefix_attrs;
1977 specs->attrs = NULL_TREE;
1978 while (true)
1980 struct c_declarator *declarator;
1981 bool dummy = false;
1982 timevar_id_t tv;
1983 tree fnbody = NULL_TREE;
1984 /* Declaring either one or more declarators (in which case we
1985 should diagnose if there were no declaration specifiers) or a
1986 function definition (in which case the diagnostic for
1987 implicit int suffices). */
1988 declarator = c_parser_declarator (parser,
1989 specs->typespec_kind != ctsk_none,
1990 C_DTR_NORMAL, &dummy);
1991 if (declarator == NULL)
1993 if (omp_declare_simd_clauses.exists ())
1994 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1995 omp_declare_simd_clauses);
1996 if (oacc_routine_data)
1997 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1998 c_parser_skip_to_end_of_block_or_statement (parser);
1999 return;
2001 if (auto_type_p && declarator->kind != cdk_id)
2003 error_at (here,
2004 "%<__auto_type%> requires a plain identifier"
2005 " as declarator");
2006 c_parser_skip_to_end_of_block_or_statement (parser);
2007 return;
2009 if (c_parser_next_token_is (parser, CPP_EQ)
2010 || c_parser_next_token_is (parser, CPP_COMMA)
2011 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2012 || c_parser_next_token_is_keyword (parser, RID_ASM)
2013 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2014 || c_parser_next_token_is_keyword (parser, RID_IN))
2016 tree asm_name = NULL_TREE;
2017 tree postfix_attrs = NULL_TREE;
2018 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2020 diagnosed_no_specs = true;
2021 pedwarn (here, 0, "data definition has no type or storage class");
2023 /* Having seen a data definition, there cannot now be a
2024 function definition. */
2025 fndef_ok = false;
2026 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2027 asm_name = c_parser_simple_asm_expr (parser);
2028 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2030 postfix_attrs = c_parser_attributes (parser);
2031 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2033 /* This means there is an attribute specifier after
2034 the declarator in a function definition. Provide
2035 some more information for the user. */
2036 error_at (here, "attributes should be specified before the "
2037 "declarator in a function definition");
2038 c_parser_skip_to_end_of_block_or_statement (parser);
2039 return;
2042 if (c_parser_next_token_is (parser, CPP_EQ))
2044 tree d;
2045 struct c_expr init;
2046 location_t init_loc;
2047 c_parser_consume_token (parser);
2048 if (auto_type_p)
2050 init_loc = c_parser_peek_token (parser)->location;
2051 rich_location richloc (line_table, init_loc);
2052 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2053 /* A parameter is initialized, which is invalid. Don't
2054 attempt to instrument the initializer. */
2055 int flag_sanitize_save = flag_sanitize;
2056 if (nested && !empty_ok)
2057 flag_sanitize = 0;
2058 init = c_parser_expr_no_commas (parser, NULL);
2059 flag_sanitize = flag_sanitize_save;
2060 if (TREE_CODE (init.value) == COMPONENT_REF
2061 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2062 error_at (here,
2063 "%<__auto_type%> used with a bit-field"
2064 " initializer");
2065 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2066 tree init_type = TREE_TYPE (init.value);
2067 /* As with typeof, remove all qualifiers from atomic types. */
2068 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2069 init_type
2070 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2071 bool vm_type = variably_modified_type_p (init_type,
2072 NULL_TREE);
2073 if (vm_type)
2074 init.value = save_expr (init.value);
2075 finish_init ();
2076 specs->typespec_kind = ctsk_typeof;
2077 specs->locations[cdw_typedef] = init_loc;
2078 specs->typedef_p = true;
2079 specs->type = init_type;
2080 if (vm_type)
2082 bool maybe_const = true;
2083 tree type_expr = c_fully_fold (init.value, false,
2084 &maybe_const);
2085 specs->expr_const_operands &= maybe_const;
2086 if (specs->expr)
2087 specs->expr = build2 (COMPOUND_EXPR,
2088 TREE_TYPE (type_expr),
2089 specs->expr, type_expr);
2090 else
2091 specs->expr = type_expr;
2093 d = start_decl (declarator, specs, true,
2094 chainon (postfix_attrs, all_prefix_attrs));
2095 if (!d)
2096 d = error_mark_node;
2097 if (omp_declare_simd_clauses.exists ())
2098 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2099 omp_declare_simd_clauses);
2101 else
2103 /* The declaration of the variable is in effect while
2104 its initializer is parsed. */
2105 d = start_decl (declarator, specs, true,
2106 chainon (postfix_attrs, all_prefix_attrs));
2107 if (!d)
2108 d = error_mark_node;
2109 if (omp_declare_simd_clauses.exists ())
2110 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2111 omp_declare_simd_clauses);
2112 init_loc = c_parser_peek_token (parser)->location;
2113 rich_location richloc (line_table, init_loc);
2114 start_init (d, asm_name, global_bindings_p (), &richloc);
2115 /* A parameter is initialized, which is invalid. Don't
2116 attempt to instrument the initializer. */
2117 int flag_sanitize_save = flag_sanitize;
2118 if (TREE_CODE (d) == PARM_DECL)
2119 flag_sanitize = 0;
2120 init = c_parser_initializer (parser);
2121 flag_sanitize = flag_sanitize_save;
2122 finish_init ();
2124 if (oacc_routine_data)
2125 c_finish_oacc_routine (oacc_routine_data, d, false);
2126 if (d != error_mark_node)
2128 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2129 finish_decl (d, init_loc, init.value,
2130 init.original_type, asm_name);
2133 else
2135 if (auto_type_p)
2137 error_at (here,
2138 "%<__auto_type%> requires an initialized "
2139 "data declaration");
2140 c_parser_skip_to_end_of_block_or_statement (parser);
2141 return;
2143 tree d = start_decl (declarator, specs, false,
2144 chainon (postfix_attrs,
2145 all_prefix_attrs));
2146 if (d && TREE_CODE (d) == FUNCTION_DECL)
2147 if (declarator->kind == cdk_function)
2148 if (DECL_ARGUMENTS (d) == NULL_TREE)
2149 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2150 if (omp_declare_simd_clauses.exists ())
2152 tree parms = NULL_TREE;
2153 if (d && TREE_CODE (d) == FUNCTION_DECL)
2155 struct c_declarator *ce = declarator;
2156 while (ce != NULL)
2157 if (ce->kind == cdk_function)
2159 parms = ce->u.arg_info->parms;
2160 break;
2162 else
2163 ce = ce->declarator;
2165 if (parms)
2166 temp_store_parm_decls (d, parms);
2167 c_finish_omp_declare_simd (parser, d, parms,
2168 omp_declare_simd_clauses);
2169 if (parms)
2170 temp_pop_parm_decls ();
2172 if (oacc_routine_data)
2173 c_finish_oacc_routine (oacc_routine_data, d, false);
2174 if (d)
2175 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2176 NULL_TREE, asm_name);
2178 if (c_parser_next_token_is_keyword (parser, RID_IN))
2180 if (d)
2181 *objc_foreach_object_declaration = d;
2182 else
2183 *objc_foreach_object_declaration = error_mark_node;
2186 if (c_parser_next_token_is (parser, CPP_COMMA))
2188 if (auto_type_p)
2190 error_at (here,
2191 "%<__auto_type%> may only be used with"
2192 " a single declarator");
2193 c_parser_skip_to_end_of_block_or_statement (parser);
2194 return;
2196 c_parser_consume_token (parser);
2197 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2198 all_prefix_attrs = chainon (c_parser_attributes (parser),
2199 prefix_attrs);
2200 else
2201 all_prefix_attrs = prefix_attrs;
2202 continue;
2204 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2206 c_parser_consume_token (parser);
2207 return;
2209 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2211 /* This can only happen in Objective-C: we found the
2212 'in' that terminates the declaration inside an
2213 Objective-C foreach statement. Do not consume the
2214 token, so that the caller can use it to determine
2215 that this indeed is a foreach context. */
2216 return;
2218 else
2220 c_parser_error (parser, "expected %<,%> or %<;%>");
2221 c_parser_skip_to_end_of_block_or_statement (parser);
2222 return;
2225 else if (auto_type_p)
2227 error_at (here,
2228 "%<__auto_type%> requires an initialized data declaration");
2229 c_parser_skip_to_end_of_block_or_statement (parser);
2230 return;
2232 else if (!fndef_ok)
2234 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2235 "%<asm%> or %<__attribute__%>");
2236 c_parser_skip_to_end_of_block_or_statement (parser);
2237 return;
2239 /* Function definition (nested or otherwise). */
2240 if (nested)
2242 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2243 c_push_function_context ();
2245 if (!start_function (specs, declarator, all_prefix_attrs))
2247 /* At this point we've consumed:
2248 declaration-specifiers declarator
2249 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2250 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2251 but the
2252 declaration-specifiers declarator
2253 aren't grokkable as a function definition, so we have
2254 an error. */
2255 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2256 if (c_parser_next_token_starts_declspecs (parser))
2258 /* If we have
2259 declaration-specifiers declarator decl-specs
2260 then assume we have a missing semicolon, which would
2261 give us:
2262 declaration-specifiers declarator decl-specs
2265 <~~~~~~~~~ declaration ~~~~~~~~~~>
2266 Use c_parser_require to get an error with a fix-it hint. */
2267 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2268 parser->error = false;
2270 else
2272 /* This can appear in many cases looking nothing like a
2273 function definition, so we don't give a more specific
2274 error suggesting there was one. */
2275 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2276 "or %<__attribute__%>");
2278 if (nested)
2279 c_pop_function_context ();
2280 break;
2283 if (DECL_DECLARED_INLINE_P (current_function_decl))
2284 tv = TV_PARSE_INLINE;
2285 else
2286 tv = TV_PARSE_FUNC;
2287 auto_timevar at (g_timer, tv);
2289 /* Parse old-style parameter declarations. ??? Attributes are
2290 not allowed to start declaration specifiers here because of a
2291 syntax conflict between a function declaration with attribute
2292 suffix and a function definition with an attribute prefix on
2293 first old-style parameter declaration. Following the old
2294 parser, they are not accepted on subsequent old-style
2295 parameter declarations either. However, there is no
2296 ambiguity after the first declaration, nor indeed on the
2297 first as long as we don't allow postfix attributes after a
2298 declarator with a nonempty identifier list in a definition;
2299 and postfix attributes have never been accepted here in
2300 function definitions either. */
2301 while (c_parser_next_token_is_not (parser, CPP_EOF)
2302 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2303 c_parser_declaration_or_fndef (parser, false, false, false,
2304 true, false, NULL, vNULL);
2305 store_parm_decls ();
2306 if (omp_declare_simd_clauses.exists ())
2307 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2308 omp_declare_simd_clauses);
2309 if (oacc_routine_data)
2310 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2311 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2312 = c_parser_peek_token (parser)->location;
2314 /* If the definition was marked with __GIMPLE then parse the
2315 function body as GIMPLE. */
2316 if (specs->gimple_p)
2318 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2319 bool saved = in_late_binary_op;
2320 in_late_binary_op = true;
2321 c_parser_parse_gimple_body (parser);
2322 in_late_binary_op = saved;
2324 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2325 consuming the function body. */
2326 else if (specs->rtl_p)
2328 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2330 /* Normally, store_parm_decls sets next_is_function_body,
2331 anticipating a function body. We need a push_scope/pop_scope
2332 pair to flush out this state, or subsequent function parsing
2333 will go wrong. */
2334 push_scope ();
2335 pop_scope ();
2337 finish_function ();
2338 return;
2340 else
2341 fnbody = c_parser_compound_statement (parser);
2342 tree fndecl = current_function_decl;
2343 if (nested)
2345 tree decl = current_function_decl;
2346 /* Mark nested functions as needing static-chain initially.
2347 lower_nested_functions will recompute it but the
2348 DECL_STATIC_CHAIN flag is also used before that happens,
2349 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2350 DECL_STATIC_CHAIN (decl) = 1;
2351 add_stmt (fnbody);
2352 finish_function ();
2353 c_pop_function_context ();
2354 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2356 else
2358 if (fnbody)
2359 add_stmt (fnbody);
2360 finish_function ();
2362 /* Get rid of the empty stmt list for GIMPLE. */
2363 if (specs->gimple_p)
2364 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2366 break;
2370 /* Parse an asm-definition (asm() outside a function body). This is a
2371 GNU extension.
2373 asm-definition:
2374 simple-asm-expr ;
2377 static void
2378 c_parser_asm_definition (c_parser *parser)
2380 tree asm_str = c_parser_simple_asm_expr (parser);
2381 if (asm_str)
2382 symtab->finalize_toplevel_asm (asm_str);
2383 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2386 /* Parse a static assertion (C11 6.7.10).
2388 static_assert-declaration:
2389 static_assert-declaration-no-semi ;
2392 static void
2393 c_parser_static_assert_declaration (c_parser *parser)
2395 c_parser_static_assert_declaration_no_semi (parser);
2396 if (parser->error
2397 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2398 c_parser_skip_to_end_of_block_or_statement (parser);
2401 /* Parse a static assertion (C11 6.7.10), without the trailing
2402 semicolon.
2404 static_assert-declaration-no-semi:
2405 _Static_assert ( constant-expression , string-literal )
2408 static void
2409 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2411 location_t assert_loc, value_loc;
2412 tree value;
2413 tree string;
2415 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2416 assert_loc = c_parser_peek_token (parser)->location;
2417 if (flag_isoc99)
2418 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2419 "ISO C99 does not support %<_Static_assert%>");
2420 else
2421 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2422 "ISO C90 does not support %<_Static_assert%>");
2423 c_parser_consume_token (parser);
2424 matching_parens parens;
2425 if (!parens.require_open (parser))
2426 return;
2427 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2428 value = c_parser_expr_no_commas (parser, NULL).value;
2429 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2430 parser->lex_untranslated_string = true;
2431 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2433 parser->lex_untranslated_string = false;
2434 return;
2436 switch (c_parser_peek_token (parser)->type)
2438 case CPP_STRING:
2439 case CPP_STRING16:
2440 case CPP_STRING32:
2441 case CPP_WSTRING:
2442 case CPP_UTF8STRING:
2443 string = c_parser_peek_token (parser)->value;
2444 c_parser_consume_token (parser);
2445 parser->lex_untranslated_string = false;
2446 break;
2447 default:
2448 c_parser_error (parser, "expected string literal");
2449 parser->lex_untranslated_string = false;
2450 return;
2452 parens.require_close (parser);
2454 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2456 error_at (value_loc, "expression in static assertion is not an integer");
2457 return;
2459 if (TREE_CODE (value) != INTEGER_CST)
2461 value = c_fully_fold (value, false, NULL);
2462 /* Strip no-op conversions. */
2463 STRIP_TYPE_NOPS (value);
2464 if (TREE_CODE (value) == INTEGER_CST)
2465 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2466 "is not an integer constant expression");
2468 if (TREE_CODE (value) != INTEGER_CST)
2470 error_at (value_loc, "expression in static assertion is not constant");
2471 return;
2473 constant_expression_warning (value);
2474 if (integer_zerop (value))
2475 error_at (assert_loc, "static assertion failed: %E", string);
2478 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2479 6.7, C11 6.7), adding them to SPECS (which may already include some).
2480 Storage class specifiers are accepted iff SCSPEC_OK; type
2481 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2482 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2483 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2485 declaration-specifiers:
2486 storage-class-specifier declaration-specifiers[opt]
2487 type-specifier declaration-specifiers[opt]
2488 type-qualifier declaration-specifiers[opt]
2489 function-specifier declaration-specifiers[opt]
2490 alignment-specifier declaration-specifiers[opt]
2492 Function specifiers (inline) are from C99, and are currently
2493 handled as storage class specifiers, as is __thread. Alignment
2494 specifiers are from C11.
2496 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2497 storage-class-specifier:
2498 typedef
2499 extern
2500 static
2501 auto
2502 register
2503 _Thread_local
2505 (_Thread_local is new in C11.)
2507 C99 6.7.4, C11 6.7.4:
2508 function-specifier:
2509 inline
2510 _Noreturn
2512 (_Noreturn is new in C11.)
2514 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2515 type-specifier:
2516 void
2517 char
2518 short
2520 long
2521 float
2522 double
2523 signed
2524 unsigned
2525 _Bool
2526 _Complex
2527 [_Imaginary removed in C99 TC2]
2528 struct-or-union-specifier
2529 enum-specifier
2530 typedef-name
2531 atomic-type-specifier
2533 (_Bool and _Complex are new in C99.)
2534 (atomic-type-specifier is new in C11.)
2536 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2538 type-qualifier:
2539 const
2540 restrict
2541 volatile
2542 address-space-qualifier
2543 _Atomic
2545 (restrict is new in C99.)
2546 (_Atomic is new in C11.)
2548 GNU extensions:
2550 declaration-specifiers:
2551 attributes declaration-specifiers[opt]
2553 type-qualifier:
2554 address-space
2556 address-space:
2557 identifier recognized by the target
2559 storage-class-specifier:
2560 __thread
2562 type-specifier:
2563 typeof-specifier
2564 __auto_type
2565 __intN
2566 _Decimal32
2567 _Decimal64
2568 _Decimal128
2569 _Fract
2570 _Accum
2571 _Sat
2573 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2574 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2576 atomic-type-specifier
2577 _Atomic ( type-name )
2579 Objective-C:
2581 type-specifier:
2582 class-name objc-protocol-refs[opt]
2583 typedef-name objc-protocol-refs
2584 objc-protocol-refs
2587 void
2588 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2589 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2590 bool alignspec_ok, bool auto_type_ok,
2591 enum c_lookahead_kind la)
2593 bool attrs_ok = start_attr_ok;
2594 bool seen_type = specs->typespec_kind != ctsk_none;
2596 if (!typespec_ok)
2597 gcc_assert (la == cla_prefer_id);
2599 while (c_parser_next_token_is (parser, CPP_NAME)
2600 || c_parser_next_token_is (parser, CPP_KEYWORD)
2601 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2603 struct c_typespec t;
2604 tree attrs;
2605 tree align;
2606 location_t loc = c_parser_peek_token (parser)->location;
2608 /* If we cannot accept a type, exit if the next token must start
2609 one. Also, if we already have seen a tagged definition,
2610 a typename would be an error anyway and likely the user
2611 has simply forgotten a semicolon, so we exit. */
2612 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2613 && c_parser_next_tokens_start_typename (parser, la)
2614 && !c_parser_next_token_is_qualifier (parser)
2615 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2616 break;
2618 if (c_parser_next_token_is (parser, CPP_NAME))
2620 c_token *name_token = c_parser_peek_token (parser);
2621 tree value = name_token->value;
2622 c_id_kind kind = name_token->id_kind;
2624 if (kind == C_ID_ADDRSPACE)
2626 addr_space_t as
2627 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2628 declspecs_add_addrspace (name_token->location, specs, as);
2629 c_parser_consume_token (parser);
2630 attrs_ok = true;
2631 continue;
2634 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2636 /* If we cannot accept a type, and the next token must start one,
2637 exit. Do the same if we already have seen a tagged definition,
2638 since it would be an error anyway and likely the user has simply
2639 forgotten a semicolon. */
2640 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2641 break;
2643 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2644 a C_ID_CLASSNAME. */
2645 c_parser_consume_token (parser);
2646 seen_type = true;
2647 attrs_ok = true;
2648 if (kind == C_ID_ID)
2650 error_at (loc, "unknown type name %qE", value);
2651 t.kind = ctsk_typedef;
2652 t.spec = error_mark_node;
2654 else if (kind == C_ID_TYPENAME
2655 && (!c_dialect_objc ()
2656 || c_parser_next_token_is_not (parser, CPP_LESS)))
2658 t.kind = ctsk_typedef;
2659 /* For a typedef name, record the meaning, not the name.
2660 In case of 'foo foo, bar;'. */
2661 t.spec = lookup_name (value);
2663 else
2665 tree proto = NULL_TREE;
2666 gcc_assert (c_dialect_objc ());
2667 t.kind = ctsk_objc;
2668 if (c_parser_next_token_is (parser, CPP_LESS))
2669 proto = c_parser_objc_protocol_refs (parser);
2670 t.spec = objc_get_protocol_qualified_type (value, proto);
2672 t.expr = NULL_TREE;
2673 t.expr_const_operands = true;
2674 declspecs_add_type (name_token->location, specs, t);
2675 continue;
2677 if (c_parser_next_token_is (parser, CPP_LESS))
2679 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2680 nisse@lysator.liu.se. */
2681 tree proto;
2682 gcc_assert (c_dialect_objc ());
2683 if (!typespec_ok || seen_type)
2684 break;
2685 proto = c_parser_objc_protocol_refs (parser);
2686 t.kind = ctsk_objc;
2687 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2688 t.expr = NULL_TREE;
2689 t.expr_const_operands = true;
2690 declspecs_add_type (loc, specs, t);
2691 continue;
2693 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2694 switch (c_parser_peek_token (parser)->keyword)
2696 case RID_STATIC:
2697 case RID_EXTERN:
2698 case RID_REGISTER:
2699 case RID_TYPEDEF:
2700 case RID_INLINE:
2701 case RID_NORETURN:
2702 case RID_AUTO:
2703 case RID_THREAD:
2704 if (!scspec_ok)
2705 goto out;
2706 attrs_ok = true;
2707 /* TODO: Distinguish between function specifiers (inline, noreturn)
2708 and storage class specifiers, either here or in
2709 declspecs_add_scspec. */
2710 declspecs_add_scspec (loc, specs,
2711 c_parser_peek_token (parser)->value);
2712 c_parser_consume_token (parser);
2713 break;
2714 case RID_AUTO_TYPE:
2715 if (!auto_type_ok)
2716 goto out;
2717 /* Fall through. */
2718 case RID_UNSIGNED:
2719 case RID_LONG:
2720 case RID_SHORT:
2721 case RID_SIGNED:
2722 case RID_COMPLEX:
2723 case RID_INT:
2724 case RID_CHAR:
2725 case RID_FLOAT:
2726 case RID_DOUBLE:
2727 case RID_VOID:
2728 case RID_DFLOAT32:
2729 case RID_DFLOAT64:
2730 case RID_DFLOAT128:
2731 CASE_RID_FLOATN_NX:
2732 case RID_BOOL:
2733 case RID_FRACT:
2734 case RID_ACCUM:
2735 case RID_SAT:
2736 case RID_INT_N_0:
2737 case RID_INT_N_1:
2738 case RID_INT_N_2:
2739 case RID_INT_N_3:
2740 if (!typespec_ok)
2741 goto out;
2742 attrs_ok = true;
2743 seen_type = true;
2744 if (c_dialect_objc ())
2745 parser->objc_need_raw_identifier = true;
2746 t.kind = ctsk_resword;
2747 t.spec = c_parser_peek_token (parser)->value;
2748 t.expr = NULL_TREE;
2749 t.expr_const_operands = true;
2750 declspecs_add_type (loc, specs, t);
2751 c_parser_consume_token (parser);
2752 break;
2753 case RID_ENUM:
2754 if (!typespec_ok)
2755 goto out;
2756 attrs_ok = true;
2757 seen_type = true;
2758 t = c_parser_enum_specifier (parser);
2759 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2760 declspecs_add_type (loc, specs, t);
2761 break;
2762 case RID_STRUCT:
2763 case RID_UNION:
2764 if (!typespec_ok)
2765 goto out;
2766 attrs_ok = true;
2767 seen_type = true;
2768 t = c_parser_struct_or_union_specifier (parser);
2769 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2770 declspecs_add_type (loc, specs, t);
2771 break;
2772 case RID_TYPEOF:
2773 /* ??? The old parser rejected typeof after other type
2774 specifiers, but is a syntax error the best way of
2775 handling this? */
2776 if (!typespec_ok || seen_type)
2777 goto out;
2778 attrs_ok = true;
2779 seen_type = true;
2780 t = c_parser_typeof_specifier (parser);
2781 declspecs_add_type (loc, specs, t);
2782 break;
2783 case RID_ATOMIC:
2784 /* C parser handling of Objective-C constructs needs
2785 checking for correct lvalue-to-rvalue conversions, and
2786 the code in build_modify_expr handling various
2787 Objective-C cases, and that in build_unary_op handling
2788 Objective-C cases for increment / decrement, also needs
2789 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2790 and objc_types_are_equivalent may also need updates. */
2791 if (c_dialect_objc ())
2792 sorry ("%<_Atomic%> in Objective-C");
2793 if (flag_isoc99)
2794 pedwarn_c99 (loc, OPT_Wpedantic,
2795 "ISO C99 does not support the %<_Atomic%> qualifier");
2796 else
2797 pedwarn_c99 (loc, OPT_Wpedantic,
2798 "ISO C90 does not support the %<_Atomic%> qualifier");
2799 attrs_ok = true;
2800 tree value;
2801 value = c_parser_peek_token (parser)->value;
2802 c_parser_consume_token (parser);
2803 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2805 /* _Atomic ( type-name ). */
2806 seen_type = true;
2807 c_parser_consume_token (parser);
2808 struct c_type_name *type = c_parser_type_name (parser);
2809 t.kind = ctsk_typeof;
2810 t.spec = error_mark_node;
2811 t.expr = NULL_TREE;
2812 t.expr_const_operands = true;
2813 if (type != NULL)
2814 t.spec = groktypename (type, &t.expr,
2815 &t.expr_const_operands);
2816 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2817 "expected %<)%>");
2818 if (t.spec != error_mark_node)
2820 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2821 error_at (loc, "%<_Atomic%>-qualified array type");
2822 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2823 error_at (loc, "%<_Atomic%>-qualified function type");
2824 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2825 error_at (loc, "%<_Atomic%> applied to a qualified type");
2826 else
2827 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2829 declspecs_add_type (loc, specs, t);
2831 else
2832 declspecs_add_qual (loc, specs, value);
2833 break;
2834 case RID_CONST:
2835 case RID_VOLATILE:
2836 case RID_RESTRICT:
2837 attrs_ok = true;
2838 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2839 c_parser_consume_token (parser);
2840 break;
2841 case RID_ATTRIBUTE:
2842 if (!attrs_ok)
2843 goto out;
2844 attrs = c_parser_attributes (parser);
2845 declspecs_add_attrs (loc, specs, attrs);
2846 break;
2847 case RID_ALIGNAS:
2848 if (!alignspec_ok)
2849 goto out;
2850 align = c_parser_alignas_specifier (parser);
2851 declspecs_add_alignas (loc, specs, align);
2852 break;
2853 case RID_GIMPLE:
2854 if (! flag_gimple)
2855 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2856 c_parser_consume_token (parser);
2857 specs->gimple_p = true;
2858 specs->locations[cdw_gimple] = loc;
2859 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2860 break;
2861 case RID_RTL:
2862 c_parser_consume_token (parser);
2863 specs->rtl_p = true;
2864 specs->locations[cdw_rtl] = loc;
2865 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2866 break;
2867 default:
2868 goto out;
2871 out: ;
2874 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2876 enum-specifier:
2877 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2878 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2879 enum attributes[opt] identifier
2881 The form with trailing comma is new in C99. The forms with
2882 attributes are GNU extensions. In GNU C, we accept any expression
2883 without commas in the syntax (assignment expressions, not just
2884 conditional expressions); assignment expressions will be diagnosed
2885 as non-constant.
2887 enumerator-list:
2888 enumerator
2889 enumerator-list , enumerator
2891 enumerator:
2892 enumeration-constant
2893 enumeration-constant = constant-expression
2895 GNU Extensions:
2897 enumerator:
2898 enumeration-constant attributes[opt]
2899 enumeration-constant attributes[opt] = constant-expression
2903 static struct c_typespec
2904 c_parser_enum_specifier (c_parser *parser)
2906 struct c_typespec ret;
2907 tree attrs;
2908 tree ident = NULL_TREE;
2909 location_t enum_loc;
2910 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2911 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2912 c_parser_consume_token (parser);
2913 attrs = c_parser_attributes (parser);
2914 enum_loc = c_parser_peek_token (parser)->location;
2915 /* Set the location in case we create a decl now. */
2916 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2917 if (c_parser_next_token_is (parser, CPP_NAME))
2919 ident = c_parser_peek_token (parser)->value;
2920 ident_loc = c_parser_peek_token (parser)->location;
2921 enum_loc = ident_loc;
2922 c_parser_consume_token (parser);
2924 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2926 /* Parse an enum definition. */
2927 struct c_enum_contents the_enum;
2928 tree type;
2929 tree postfix_attrs;
2930 /* We chain the enumerators in reverse order, then put them in
2931 forward order at the end. */
2932 tree values;
2933 timevar_push (TV_PARSE_ENUM);
2934 type = start_enum (enum_loc, &the_enum, ident);
2935 values = NULL_TREE;
2936 c_parser_consume_token (parser);
2937 while (true)
2939 tree enum_id;
2940 tree enum_value;
2941 tree enum_decl;
2942 bool seen_comma;
2943 c_token *token;
2944 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2945 location_t decl_loc, value_loc;
2946 if (c_parser_next_token_is_not (parser, CPP_NAME))
2948 /* Give a nicer error for "enum {}". */
2949 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2950 && !parser->error)
2952 error_at (c_parser_peek_token (parser)->location,
2953 "empty enum is invalid");
2954 parser->error = true;
2956 else
2957 c_parser_error (parser, "expected identifier");
2958 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2959 values = error_mark_node;
2960 break;
2962 token = c_parser_peek_token (parser);
2963 enum_id = token->value;
2964 /* Set the location in case we create a decl now. */
2965 c_parser_set_source_position_from_token (token);
2966 decl_loc = value_loc = token->location;
2967 c_parser_consume_token (parser);
2968 /* Parse any specified attributes. */
2969 tree enum_attrs = c_parser_attributes (parser);
2970 if (c_parser_next_token_is (parser, CPP_EQ))
2972 c_parser_consume_token (parser);
2973 value_loc = c_parser_peek_token (parser)->location;
2974 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2976 else
2977 enum_value = NULL_TREE;
2978 enum_decl = build_enumerator (decl_loc, value_loc,
2979 &the_enum, enum_id, enum_value);
2980 if (enum_attrs)
2981 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2982 TREE_CHAIN (enum_decl) = values;
2983 values = enum_decl;
2984 seen_comma = false;
2985 if (c_parser_next_token_is (parser, CPP_COMMA))
2987 comma_loc = c_parser_peek_token (parser)->location;
2988 seen_comma = true;
2989 c_parser_consume_token (parser);
2991 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2993 if (seen_comma)
2994 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2995 "comma at end of enumerator list");
2996 c_parser_consume_token (parser);
2997 break;
2999 if (!seen_comma)
3001 c_parser_error (parser, "expected %<,%> or %<}%>");
3002 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3003 values = error_mark_node;
3004 break;
3007 postfix_attrs = c_parser_attributes (parser);
3008 ret.spec = finish_enum (type, nreverse (values),
3009 chainon (attrs, postfix_attrs));
3010 ret.kind = ctsk_tagdef;
3011 ret.expr = NULL_TREE;
3012 ret.expr_const_operands = true;
3013 timevar_pop (TV_PARSE_ENUM);
3014 return ret;
3016 else if (!ident)
3018 c_parser_error (parser, "expected %<{%>");
3019 ret.spec = error_mark_node;
3020 ret.kind = ctsk_tagref;
3021 ret.expr = NULL_TREE;
3022 ret.expr_const_operands = true;
3023 return ret;
3025 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3026 /* In ISO C, enumerated types can be referred to only if already
3027 defined. */
3028 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3030 gcc_assert (ident);
3031 pedwarn (enum_loc, OPT_Wpedantic,
3032 "ISO C forbids forward references to %<enum%> types");
3034 return ret;
3037 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3039 struct-or-union-specifier:
3040 struct-or-union attributes[opt] identifier[opt]
3041 { struct-contents } attributes[opt]
3042 struct-or-union attributes[opt] identifier
3044 struct-contents:
3045 struct-declaration-list
3047 struct-declaration-list:
3048 struct-declaration ;
3049 struct-declaration-list struct-declaration ;
3051 GNU extensions:
3053 struct-contents:
3054 empty
3055 struct-declaration
3056 struct-declaration-list struct-declaration
3058 struct-declaration-list:
3059 struct-declaration-list ;
3062 (Note that in the syntax here, unlike that in ISO C, the semicolons
3063 are included here rather than in struct-declaration, in order to
3064 describe the syntax with extra semicolons and missing semicolon at
3065 end.)
3067 Objective-C:
3069 struct-declaration-list:
3070 @defs ( class-name )
3072 (Note this does not include a trailing semicolon, but can be
3073 followed by further declarations, and gets a pedwarn-if-pedantic
3074 when followed by a semicolon.) */
3076 static struct c_typespec
3077 c_parser_struct_or_union_specifier (c_parser *parser)
3079 struct c_typespec ret;
3080 tree attrs;
3081 tree ident = NULL_TREE;
3082 location_t struct_loc;
3083 location_t ident_loc = UNKNOWN_LOCATION;
3084 enum tree_code code;
3085 switch (c_parser_peek_token (parser)->keyword)
3087 case RID_STRUCT:
3088 code = RECORD_TYPE;
3089 break;
3090 case RID_UNION:
3091 code = UNION_TYPE;
3092 break;
3093 default:
3094 gcc_unreachable ();
3096 struct_loc = c_parser_peek_token (parser)->location;
3097 c_parser_consume_token (parser);
3098 attrs = c_parser_attributes (parser);
3100 /* Set the location in case we create a decl now. */
3101 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3103 if (c_parser_next_token_is (parser, CPP_NAME))
3105 ident = c_parser_peek_token (parser)->value;
3106 ident_loc = c_parser_peek_token (parser)->location;
3107 struct_loc = ident_loc;
3108 c_parser_consume_token (parser);
3110 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3112 /* Parse a struct or union definition. Start the scope of the
3113 tag before parsing components. */
3114 struct c_struct_parse_info *struct_info;
3115 tree type = start_struct (struct_loc, code, ident, &struct_info);
3116 tree postfix_attrs;
3117 /* We chain the components in reverse order, then put them in
3118 forward order at the end. Each struct-declaration may
3119 declare multiple components (comma-separated), so we must use
3120 chainon to join them, although when parsing each
3121 struct-declaration we can use TREE_CHAIN directly.
3123 The theory behind all this is that there will be more
3124 semicolon separated fields than comma separated fields, and
3125 so we'll be minimizing the number of node traversals required
3126 by chainon. */
3127 tree contents;
3128 timevar_push (TV_PARSE_STRUCT);
3129 contents = NULL_TREE;
3130 c_parser_consume_token (parser);
3131 /* Handle the Objective-C @defs construct,
3132 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3133 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3135 tree name;
3136 gcc_assert (c_dialect_objc ());
3137 c_parser_consume_token (parser);
3138 matching_parens parens;
3139 if (!parens.require_open (parser))
3140 goto end_at_defs;
3141 if (c_parser_next_token_is (parser, CPP_NAME)
3142 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3144 name = c_parser_peek_token (parser)->value;
3145 c_parser_consume_token (parser);
3147 else
3149 c_parser_error (parser, "expected class name");
3150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3151 goto end_at_defs;
3153 parens.skip_until_found_close (parser);
3154 contents = nreverse (objc_get_class_ivars (name));
3156 end_at_defs:
3157 /* Parse the struct-declarations and semicolons. Problems with
3158 semicolons are diagnosed here; empty structures are diagnosed
3159 elsewhere. */
3160 while (true)
3162 tree decls;
3163 /* Parse any stray semicolon. */
3164 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3166 location_t semicolon_loc
3167 = c_parser_peek_token (parser)->location;
3168 gcc_rich_location richloc (semicolon_loc);
3169 richloc.add_fixit_remove ();
3170 pedwarn (&richloc, OPT_Wpedantic,
3171 "extra semicolon in struct or union specified");
3172 c_parser_consume_token (parser);
3173 continue;
3175 /* Stop if at the end of the struct or union contents. */
3176 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3178 c_parser_consume_token (parser);
3179 break;
3181 /* Accept #pragmas at struct scope. */
3182 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3184 c_parser_pragma (parser, pragma_struct, NULL);
3185 continue;
3187 /* Parse some comma-separated declarations, but not the
3188 trailing semicolon if any. */
3189 decls = c_parser_struct_declaration (parser);
3190 contents = chainon (decls, contents);
3191 /* If no semicolon follows, either we have a parse error or
3192 are at the end of the struct or union and should
3193 pedwarn. */
3194 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3195 c_parser_consume_token (parser);
3196 else
3198 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3199 pedwarn (c_parser_peek_token (parser)->location, 0,
3200 "no semicolon at end of struct or union");
3201 else if (parser->error
3202 || !c_parser_next_token_starts_declspecs (parser))
3204 c_parser_error (parser, "expected %<;%>");
3205 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3206 break;
3209 /* If we come here, we have already emitted an error
3210 for an expected `;', identifier or `(', and we also
3211 recovered already. Go on with the next field. */
3214 postfix_attrs = c_parser_attributes (parser);
3215 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3216 chainon (attrs, postfix_attrs), struct_info);
3217 ret.kind = ctsk_tagdef;
3218 ret.expr = NULL_TREE;
3219 ret.expr_const_operands = true;
3220 timevar_pop (TV_PARSE_STRUCT);
3221 return ret;
3223 else if (!ident)
3225 c_parser_error (parser, "expected %<{%>");
3226 ret.spec = error_mark_node;
3227 ret.kind = ctsk_tagref;
3228 ret.expr = NULL_TREE;
3229 ret.expr_const_operands = true;
3230 return ret;
3232 ret = parser_xref_tag (ident_loc, code, ident);
3233 return ret;
3236 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3237 *without* the trailing semicolon.
3239 struct-declaration:
3240 specifier-qualifier-list struct-declarator-list
3241 static_assert-declaration-no-semi
3243 specifier-qualifier-list:
3244 type-specifier specifier-qualifier-list[opt]
3245 type-qualifier specifier-qualifier-list[opt]
3246 alignment-specifier specifier-qualifier-list[opt]
3247 attributes specifier-qualifier-list[opt]
3249 struct-declarator-list:
3250 struct-declarator
3251 struct-declarator-list , attributes[opt] struct-declarator
3253 struct-declarator:
3254 declarator attributes[opt]
3255 declarator[opt] : constant-expression attributes[opt]
3257 GNU extensions:
3259 struct-declaration:
3260 __extension__ struct-declaration
3261 specifier-qualifier-list
3263 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3264 of attributes where shown is a GNU extension. In GNU C, we accept
3265 any expression without commas in the syntax (assignment
3266 expressions, not just conditional expressions); assignment
3267 expressions will be diagnosed as non-constant. */
3269 static tree
3270 c_parser_struct_declaration (c_parser *parser)
3272 struct c_declspecs *specs;
3273 tree prefix_attrs;
3274 tree all_prefix_attrs;
3275 tree decls;
3276 location_t decl_loc;
3277 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3279 int ext;
3280 tree decl;
3281 ext = disable_extension_diagnostics ();
3282 c_parser_consume_token (parser);
3283 decl = c_parser_struct_declaration (parser);
3284 restore_extension_diagnostics (ext);
3285 return decl;
3287 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3289 c_parser_static_assert_declaration_no_semi (parser);
3290 return NULL_TREE;
3292 specs = build_null_declspecs ();
3293 decl_loc = c_parser_peek_token (parser)->location;
3294 /* Strictly by the standard, we shouldn't allow _Alignas here,
3295 but it appears to have been intended to allow it there, so
3296 we're keeping it as it is until WG14 reaches a conclusion
3297 of N1731.
3298 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3299 c_parser_declspecs (parser, specs, false, true, true,
3300 true, false, cla_nonabstract_decl);
3301 if (parser->error)
3302 return NULL_TREE;
3303 if (!specs->declspecs_seen_p)
3305 c_parser_error (parser, "expected specifier-qualifier-list");
3306 return NULL_TREE;
3308 finish_declspecs (specs);
3309 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3310 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3312 tree ret;
3313 if (specs->typespec_kind == ctsk_none)
3315 pedwarn (decl_loc, OPT_Wpedantic,
3316 "ISO C forbids member declarations with no members");
3317 shadow_tag_warned (specs, pedantic);
3318 ret = NULL_TREE;
3320 else
3322 /* Support for unnamed structs or unions as members of
3323 structs or unions (which is [a] useful and [b] supports
3324 MS P-SDK). */
3325 tree attrs = NULL;
3327 ret = grokfield (c_parser_peek_token (parser)->location,
3328 build_id_declarator (NULL_TREE), specs,
3329 NULL_TREE, &attrs);
3330 if (ret)
3331 decl_attributes (&ret, attrs, 0);
3333 return ret;
3336 /* Provide better error recovery. Note that a type name here is valid,
3337 and will be treated as a field name. */
3338 if (specs->typespec_kind == ctsk_tagdef
3339 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3340 && c_parser_next_token_starts_declspecs (parser)
3341 && !c_parser_next_token_is (parser, CPP_NAME))
3343 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3344 parser->error = false;
3345 return NULL_TREE;
3348 pending_xref_error ();
3349 prefix_attrs = specs->attrs;
3350 all_prefix_attrs = prefix_attrs;
3351 specs->attrs = NULL_TREE;
3352 decls = NULL_TREE;
3353 while (true)
3355 /* Declaring one or more declarators or un-named bit-fields. */
3356 struct c_declarator *declarator;
3357 bool dummy = false;
3358 if (c_parser_next_token_is (parser, CPP_COLON))
3359 declarator = build_id_declarator (NULL_TREE);
3360 else
3361 declarator = c_parser_declarator (parser,
3362 specs->typespec_kind != ctsk_none,
3363 C_DTR_NORMAL, &dummy);
3364 if (declarator == NULL)
3366 c_parser_skip_to_end_of_block_or_statement (parser);
3367 break;
3369 if (c_parser_next_token_is (parser, CPP_COLON)
3370 || c_parser_next_token_is (parser, CPP_COMMA)
3371 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3372 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3373 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3375 tree postfix_attrs = NULL_TREE;
3376 tree width = NULL_TREE;
3377 tree d;
3378 if (c_parser_next_token_is (parser, CPP_COLON))
3380 c_parser_consume_token (parser);
3381 width = c_parser_expr_no_commas (parser, NULL).value;
3383 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3384 postfix_attrs = c_parser_attributes (parser);
3385 d = grokfield (c_parser_peek_token (parser)->location,
3386 declarator, specs, width, &all_prefix_attrs);
3387 decl_attributes (&d, chainon (postfix_attrs,
3388 all_prefix_attrs), 0);
3389 DECL_CHAIN (d) = decls;
3390 decls = d;
3391 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3392 all_prefix_attrs = chainon (c_parser_attributes (parser),
3393 prefix_attrs);
3394 else
3395 all_prefix_attrs = prefix_attrs;
3396 if (c_parser_next_token_is (parser, CPP_COMMA))
3397 c_parser_consume_token (parser);
3398 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3399 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3401 /* Semicolon consumed in caller. */
3402 break;
3404 else
3406 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3407 break;
3410 else
3412 c_parser_error (parser,
3413 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3414 "%<__attribute__%>");
3415 break;
3418 return decls;
3421 /* Parse a typeof specifier (a GNU extension).
3423 typeof-specifier:
3424 typeof ( expression )
3425 typeof ( type-name )
3428 static struct c_typespec
3429 c_parser_typeof_specifier (c_parser *parser)
3431 struct c_typespec ret;
3432 ret.kind = ctsk_typeof;
3433 ret.spec = error_mark_node;
3434 ret.expr = NULL_TREE;
3435 ret.expr_const_operands = true;
3436 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3437 c_parser_consume_token (parser);
3438 c_inhibit_evaluation_warnings++;
3439 in_typeof++;
3440 matching_parens parens;
3441 if (!parens.require_open (parser))
3443 c_inhibit_evaluation_warnings--;
3444 in_typeof--;
3445 return ret;
3447 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3449 struct c_type_name *type = c_parser_type_name (parser);
3450 c_inhibit_evaluation_warnings--;
3451 in_typeof--;
3452 if (type != NULL)
3454 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3455 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3458 else
3460 bool was_vm;
3461 location_t here = c_parser_peek_token (parser)->location;
3462 struct c_expr expr = c_parser_expression (parser);
3463 c_inhibit_evaluation_warnings--;
3464 in_typeof--;
3465 if (TREE_CODE (expr.value) == COMPONENT_REF
3466 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3467 error_at (here, "%<typeof%> applied to a bit-field");
3468 mark_exp_read (expr.value);
3469 ret.spec = TREE_TYPE (expr.value);
3470 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3471 /* This is returned with the type so that when the type is
3472 evaluated, this can be evaluated. */
3473 if (was_vm)
3474 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3475 pop_maybe_used (was_vm);
3476 /* For use in macros such as those in <stdatomic.h>, remove all
3477 qualifiers from atomic types. (const can be an issue for more macros
3478 using typeof than just the <stdatomic.h> ones.) */
3479 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3480 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3482 parens.skip_until_found_close (parser);
3483 return ret;
3486 /* Parse an alignment-specifier.
3488 C11 6.7.5:
3490 alignment-specifier:
3491 _Alignas ( type-name )
3492 _Alignas ( constant-expression )
3495 static tree
3496 c_parser_alignas_specifier (c_parser * parser)
3498 tree ret = error_mark_node;
3499 location_t loc = c_parser_peek_token (parser)->location;
3500 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3501 c_parser_consume_token (parser);
3502 if (flag_isoc99)
3503 pedwarn_c99 (loc, OPT_Wpedantic,
3504 "ISO C99 does not support %<_Alignas%>");
3505 else
3506 pedwarn_c99 (loc, OPT_Wpedantic,
3507 "ISO C90 does not support %<_Alignas%>");
3508 matching_parens parens;
3509 if (!parens.require_open (parser))
3510 return ret;
3511 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3513 struct c_type_name *type = c_parser_type_name (parser);
3514 if (type != NULL)
3515 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3516 false, true, 1);
3518 else
3519 ret = c_parser_expr_no_commas (parser, NULL).value;
3520 parens.skip_until_found_close (parser);
3521 return ret;
3524 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3525 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3526 a typedef name may be redeclared; otherwise it may not. KIND
3527 indicates which kind of declarator is wanted. Returns a valid
3528 declarator except in the case of a syntax error in which case NULL is
3529 returned. *SEEN_ID is set to true if an identifier being declared is
3530 seen; this is used to diagnose bad forms of abstract array declarators
3531 and to determine whether an identifier list is syntactically permitted.
3533 declarator:
3534 pointer[opt] direct-declarator
3536 direct-declarator:
3537 identifier
3538 ( attributes[opt] declarator )
3539 direct-declarator array-declarator
3540 direct-declarator ( parameter-type-list )
3541 direct-declarator ( identifier-list[opt] )
3543 pointer:
3544 * type-qualifier-list[opt]
3545 * type-qualifier-list[opt] pointer
3547 type-qualifier-list:
3548 type-qualifier
3549 attributes
3550 type-qualifier-list type-qualifier
3551 type-qualifier-list attributes
3553 array-declarator:
3554 [ type-qualifier-list[opt] assignment-expression[opt] ]
3555 [ static type-qualifier-list[opt] assignment-expression ]
3556 [ type-qualifier-list static assignment-expression ]
3557 [ type-qualifier-list[opt] * ]
3559 parameter-type-list:
3560 parameter-list
3561 parameter-list , ...
3563 parameter-list:
3564 parameter-declaration
3565 parameter-list , parameter-declaration
3567 parameter-declaration:
3568 declaration-specifiers declarator attributes[opt]
3569 declaration-specifiers abstract-declarator[opt] attributes[opt]
3571 identifier-list:
3572 identifier
3573 identifier-list , identifier
3575 abstract-declarator:
3576 pointer
3577 pointer[opt] direct-abstract-declarator
3579 direct-abstract-declarator:
3580 ( attributes[opt] abstract-declarator )
3581 direct-abstract-declarator[opt] array-declarator
3582 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3584 GNU extensions:
3586 direct-declarator:
3587 direct-declarator ( parameter-forward-declarations
3588 parameter-type-list[opt] )
3590 direct-abstract-declarator:
3591 direct-abstract-declarator[opt] ( parameter-forward-declarations
3592 parameter-type-list[opt] )
3594 parameter-forward-declarations:
3595 parameter-list ;
3596 parameter-forward-declarations parameter-list ;
3598 The uses of attributes shown above are GNU extensions.
3600 Some forms of array declarator are not included in C99 in the
3601 syntax for abstract declarators; these are disallowed elsewhere.
3602 This may be a defect (DR#289).
3604 This function also accepts an omitted abstract declarator as being
3605 an abstract declarator, although not part of the formal syntax. */
3607 struct c_declarator *
3608 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3609 bool *seen_id)
3611 /* Parse any initial pointer part. */
3612 if (c_parser_next_token_is (parser, CPP_MULT))
3614 struct c_declspecs *quals_attrs = build_null_declspecs ();
3615 struct c_declarator *inner;
3616 c_parser_consume_token (parser);
3617 c_parser_declspecs (parser, quals_attrs, false, false, true,
3618 false, false, cla_prefer_id);
3619 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3620 if (inner == NULL)
3621 return NULL;
3622 else
3623 return make_pointer_declarator (quals_attrs, inner);
3625 /* Now we have a direct declarator, direct abstract declarator or
3626 nothing (which counts as a direct abstract declarator here). */
3627 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3630 /* Parse a direct declarator or direct abstract declarator; arguments
3631 as c_parser_declarator. */
3633 static struct c_declarator *
3634 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3635 bool *seen_id)
3637 /* The direct declarator must start with an identifier (possibly
3638 omitted) or a parenthesized declarator (possibly abstract). In
3639 an ordinary declarator, initial parentheses must start a
3640 parenthesized declarator. In an abstract declarator or parameter
3641 declarator, they could start a parenthesized declarator or a
3642 parameter list. To tell which, the open parenthesis and any
3643 following attributes must be read. If a declaration specifier
3644 follows, then it is a parameter list; if the specifier is a
3645 typedef name, there might be an ambiguity about redeclaring it,
3646 which is resolved in the direction of treating it as a typedef
3647 name. If a close parenthesis follows, it is also an empty
3648 parameter list, as the syntax does not permit empty abstract
3649 declarators. Otherwise, it is a parenthesized declarator (in
3650 which case the analysis may be repeated inside it, recursively).
3652 ??? There is an ambiguity in a parameter declaration "int
3653 (__attribute__((foo)) x)", where x is not a typedef name: it
3654 could be an abstract declarator for a function, or declare x with
3655 parentheses. The proper resolution of this ambiguity needs
3656 documenting. At present we follow an accident of the old
3657 parser's implementation, whereby the first parameter must have
3658 some declaration specifiers other than just attributes. Thus as
3659 a parameter declaration it is treated as a parenthesized
3660 parameter named x, and as an abstract declarator it is
3661 rejected.
3663 ??? Also following the old parser, attributes inside an empty
3664 parameter list are ignored, making it a list not yielding a
3665 prototype, rather than giving an error or making it have one
3666 parameter with implicit type int.
3668 ??? Also following the old parser, typedef names may be
3669 redeclared in declarators, but not Objective-C class names. */
3671 if (kind != C_DTR_ABSTRACT
3672 && c_parser_next_token_is (parser, CPP_NAME)
3673 && ((type_seen_p
3674 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3675 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3676 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3678 struct c_declarator *inner
3679 = build_id_declarator (c_parser_peek_token (parser)->value);
3680 *seen_id = true;
3681 inner->id_loc = c_parser_peek_token (parser)->location;
3682 c_parser_consume_token (parser);
3683 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3686 if (kind != C_DTR_NORMAL
3687 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3689 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3690 inner->id_loc = c_parser_peek_token (parser)->location;
3691 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3694 /* Either we are at the end of an abstract declarator, or we have
3695 parentheses. */
3697 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3699 tree attrs;
3700 struct c_declarator *inner;
3701 c_parser_consume_token (parser);
3702 attrs = c_parser_attributes (parser);
3703 if (kind != C_DTR_NORMAL
3704 && (c_parser_next_token_starts_declspecs (parser)
3705 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3707 struct c_arg_info *args
3708 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3709 attrs);
3710 if (args == NULL)
3711 return NULL;
3712 else
3714 inner
3715 = build_function_declarator (args,
3716 build_id_declarator (NULL_TREE));
3717 return c_parser_direct_declarator_inner (parser, *seen_id,
3718 inner);
3721 /* A parenthesized declarator. */
3722 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3723 if (inner != NULL && attrs != NULL)
3724 inner = build_attrs_declarator (attrs, inner);
3725 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3727 c_parser_consume_token (parser);
3728 if (inner == NULL)
3729 return NULL;
3730 else
3731 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3733 else
3735 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3736 "expected %<)%>");
3737 return NULL;
3740 else
3742 if (kind == C_DTR_NORMAL)
3744 c_parser_error (parser, "expected identifier or %<(%>");
3745 return NULL;
3747 else
3748 return build_id_declarator (NULL_TREE);
3752 /* Parse part of a direct declarator or direct abstract declarator,
3753 given that some (in INNER) has already been parsed; ID_PRESENT is
3754 true if an identifier is present, false for an abstract
3755 declarator. */
3757 static struct c_declarator *
3758 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3759 struct c_declarator *inner)
3761 /* Parse a sequence of array declarators and parameter lists. */
3762 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3764 location_t brace_loc = c_parser_peek_token (parser)->location;
3765 struct c_declarator *declarator;
3766 struct c_declspecs *quals_attrs = build_null_declspecs ();
3767 bool static_seen;
3768 bool star_seen;
3769 struct c_expr dimen;
3770 dimen.value = NULL_TREE;
3771 dimen.original_code = ERROR_MARK;
3772 dimen.original_type = NULL_TREE;
3773 c_parser_consume_token (parser);
3774 c_parser_declspecs (parser, quals_attrs, false, false, true,
3775 false, false, cla_prefer_id);
3776 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3777 if (static_seen)
3778 c_parser_consume_token (parser);
3779 if (static_seen && !quals_attrs->declspecs_seen_p)
3780 c_parser_declspecs (parser, quals_attrs, false, false, true,
3781 false, false, cla_prefer_id);
3782 if (!quals_attrs->declspecs_seen_p)
3783 quals_attrs = NULL;
3784 /* If "static" is present, there must be an array dimension.
3785 Otherwise, there may be a dimension, "*", or no
3786 dimension. */
3787 if (static_seen)
3789 star_seen = false;
3790 dimen = c_parser_expr_no_commas (parser, NULL);
3792 else
3794 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3796 dimen.value = NULL_TREE;
3797 star_seen = false;
3799 else if (c_parser_next_token_is (parser, CPP_MULT))
3801 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3803 dimen.value = NULL_TREE;
3804 star_seen = true;
3805 c_parser_consume_token (parser);
3807 else
3809 star_seen = false;
3810 dimen = c_parser_expr_no_commas (parser, NULL);
3813 else
3815 star_seen = false;
3816 dimen = c_parser_expr_no_commas (parser, NULL);
3819 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3820 c_parser_consume_token (parser);
3821 else
3823 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3824 "expected %<]%>");
3825 return NULL;
3827 if (dimen.value)
3828 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3829 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3830 static_seen, star_seen);
3831 if (declarator == NULL)
3832 return NULL;
3833 inner = set_array_declarator_inner (declarator, inner);
3834 return c_parser_direct_declarator_inner (parser, id_present, inner);
3836 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3838 tree attrs;
3839 struct c_arg_info *args;
3840 c_parser_consume_token (parser);
3841 attrs = c_parser_attributes (parser);
3842 args = c_parser_parms_declarator (parser, id_present, attrs);
3843 if (args == NULL)
3844 return NULL;
3845 else
3847 inner = build_function_declarator (args, inner);
3848 return c_parser_direct_declarator_inner (parser, id_present, inner);
3851 return inner;
3854 /* Parse a parameter list or identifier list, including the closing
3855 parenthesis but not the opening one. ATTRS are the attributes at
3856 the start of the list. ID_LIST_OK is true if an identifier list is
3857 acceptable; such a list must not have attributes at the start. */
3859 static struct c_arg_info *
3860 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3862 push_scope ();
3863 declare_parm_level ();
3864 /* If the list starts with an identifier, it is an identifier list.
3865 Otherwise, it is either a prototype list or an empty list. */
3866 if (id_list_ok
3867 && !attrs
3868 && c_parser_next_token_is (parser, CPP_NAME)
3869 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3871 /* Look ahead to detect typos in type names. */
3872 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3873 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3874 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3875 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3876 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3878 tree list = NULL_TREE, *nextp = &list;
3879 while (c_parser_next_token_is (parser, CPP_NAME)
3880 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3882 *nextp = build_tree_list (NULL_TREE,
3883 c_parser_peek_token (parser)->value);
3884 nextp = & TREE_CHAIN (*nextp);
3885 c_parser_consume_token (parser);
3886 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3887 break;
3888 c_parser_consume_token (parser);
3889 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3891 c_parser_error (parser, "expected identifier");
3892 break;
3895 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3897 struct c_arg_info *ret = build_arg_info ();
3898 ret->types = list;
3899 c_parser_consume_token (parser);
3900 pop_scope ();
3901 return ret;
3903 else
3905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3906 "expected %<)%>");
3907 pop_scope ();
3908 return NULL;
3911 else
3913 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3914 NULL);
3915 pop_scope ();
3916 return ret;
3920 /* Parse a parameter list (possibly empty), including the closing
3921 parenthesis but not the opening one. ATTRS are the attributes at
3922 the start of the list. EXPR is NULL or an expression that needs to
3923 be evaluated for the side effects of array size expressions in the
3924 parameters. */
3926 static struct c_arg_info *
3927 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3929 bool bad_parm = false;
3931 /* ??? Following the old parser, forward parameter declarations may
3932 use abstract declarators, and if no real parameter declarations
3933 follow the forward declarations then this is not diagnosed. Also
3934 note as above that attributes are ignored as the only contents of
3935 the parentheses, or as the only contents after forward
3936 declarations. */
3937 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3939 struct c_arg_info *ret = build_arg_info ();
3940 c_parser_consume_token (parser);
3941 return ret;
3943 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3945 struct c_arg_info *ret = build_arg_info ();
3947 if (flag_allow_parameterless_variadic_functions)
3949 /* F (...) is allowed. */
3950 ret->types = NULL_TREE;
3952 else
3954 /* Suppress -Wold-style-definition for this case. */
3955 ret->types = error_mark_node;
3956 error_at (c_parser_peek_token (parser)->location,
3957 "ISO C requires a named argument before %<...%>");
3959 c_parser_consume_token (parser);
3960 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3962 c_parser_consume_token (parser);
3963 return ret;
3965 else
3967 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3968 "expected %<)%>");
3969 return NULL;
3972 /* Nonempty list of parameters, either terminated with semicolon
3973 (forward declarations; recurse) or with close parenthesis (normal
3974 function) or with ", ... )" (variadic function). */
3975 while (true)
3977 /* Parse a parameter. */
3978 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3979 attrs = NULL_TREE;
3980 if (parm == NULL)
3981 bad_parm = true;
3982 else
3983 push_parm_decl (parm, &expr);
3984 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3986 tree new_attrs;
3987 c_parser_consume_token (parser);
3988 mark_forward_parm_decls ();
3989 new_attrs = c_parser_attributes (parser);
3990 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3992 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3994 c_parser_consume_token (parser);
3995 if (bad_parm)
3996 return NULL;
3997 else
3998 return get_parm_info (false, expr);
4000 if (!c_parser_require (parser, CPP_COMMA,
4001 "expected %<;%>, %<,%> or %<)%>",
4002 UNKNOWN_LOCATION, false))
4004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4005 return NULL;
4007 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4009 c_parser_consume_token (parser);
4010 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4012 c_parser_consume_token (parser);
4013 if (bad_parm)
4014 return NULL;
4015 else
4016 return get_parm_info (true, expr);
4018 else
4020 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4021 "expected %<)%>");
4022 return NULL;
4028 /* Parse a parameter declaration. ATTRS are the attributes at the
4029 start of the declaration if it is the first parameter. */
4031 static struct c_parm *
4032 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4034 struct c_declspecs *specs;
4035 struct c_declarator *declarator;
4036 tree prefix_attrs;
4037 tree postfix_attrs = NULL_TREE;
4038 bool dummy = false;
4040 /* Accept #pragmas between parameter declarations. */
4041 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4042 c_parser_pragma (parser, pragma_param, NULL);
4044 if (!c_parser_next_token_starts_declspecs (parser))
4046 c_token *token = c_parser_peek_token (parser);
4047 if (parser->error)
4048 return NULL;
4049 c_parser_set_source_position_from_token (token);
4050 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4052 name_hint hint = lookup_name_fuzzy (token->value,
4053 FUZZY_LOOKUP_TYPENAME,
4054 token->location);
4055 if (hint)
4057 gcc_rich_location richloc (token->location);
4058 richloc.add_fixit_replace (hint.suggestion ());
4059 error_at (&richloc,
4060 "unknown type name %qE; did you mean %qs?",
4061 token->value, hint.suggestion ());
4063 else
4064 error_at (token->location, "unknown type name %qE", token->value);
4065 parser->error = true;
4067 /* ??? In some Objective-C cases '...' isn't applicable so there
4068 should be a different message. */
4069 else
4070 c_parser_error (parser,
4071 "expected declaration specifiers or %<...%>");
4072 c_parser_skip_to_end_of_parameter (parser);
4073 return NULL;
4076 location_t start_loc = c_parser_peek_token (parser)->location;
4078 specs = build_null_declspecs ();
4079 if (attrs)
4081 declspecs_add_attrs (input_location, specs, attrs);
4082 attrs = NULL_TREE;
4084 c_parser_declspecs (parser, specs, true, true, true, true, false,
4085 cla_nonabstract_decl);
4086 finish_declspecs (specs);
4087 pending_xref_error ();
4088 prefix_attrs = specs->attrs;
4089 specs->attrs = NULL_TREE;
4090 declarator = c_parser_declarator (parser,
4091 specs->typespec_kind != ctsk_none,
4092 C_DTR_PARM, &dummy);
4093 if (declarator == NULL)
4095 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4096 return NULL;
4098 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4099 postfix_attrs = c_parser_attributes (parser);
4101 /* Generate a location for the parameter, ranging from the start of the
4102 initial token to the end of the final token.
4104 If we have a identifier, then use it for the caret location, e.g.
4106 extern int callee (int one, int (*two)(int, int), float three);
4107 ~~~~~~^~~~~~~~~~~~~~
4109 otherwise, reuse the start location for the caret location e.g.:
4111 extern int callee (int one, int (*)(int, int), float three);
4112 ^~~~~~~~~~~~~~~~~
4114 location_t end_loc = parser->last_token_location;
4116 /* Find any cdk_id declarator; determine if we have an identifier. */
4117 c_declarator *id_declarator = declarator;
4118 while (id_declarator && id_declarator->kind != cdk_id)
4119 id_declarator = id_declarator->declarator;
4120 location_t caret_loc = (id_declarator->u.id
4121 ? id_declarator->id_loc
4122 : start_loc);
4123 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4125 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4126 declarator, param_loc);
4129 /* Parse a string literal in an asm expression. It should not be
4130 translated, and wide string literals are an error although
4131 permitted by the syntax. This is a GNU extension.
4133 asm-string-literal:
4134 string-literal
4136 ??? At present, following the old parser, the caller needs to have
4137 set lex_untranslated_string to 1. It would be better to follow the
4138 C++ parser rather than using this kludge. */
4140 static tree
4141 c_parser_asm_string_literal (c_parser *parser)
4143 tree str;
4144 int save_flag = warn_overlength_strings;
4145 warn_overlength_strings = 0;
4146 if (c_parser_next_token_is (parser, CPP_STRING))
4148 str = c_parser_peek_token (parser)->value;
4149 c_parser_consume_token (parser);
4151 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4153 error_at (c_parser_peek_token (parser)->location,
4154 "wide string literal in %<asm%>");
4155 str = build_string (1, "");
4156 c_parser_consume_token (parser);
4158 else
4160 c_parser_error (parser, "expected string literal");
4161 str = NULL_TREE;
4163 warn_overlength_strings = save_flag;
4164 return str;
4167 /* Parse a simple asm expression. This is used in restricted
4168 contexts, where a full expression with inputs and outputs does not
4169 make sense. This is a GNU extension.
4171 simple-asm-expr:
4172 asm ( asm-string-literal )
4175 static tree
4176 c_parser_simple_asm_expr (c_parser *parser)
4178 tree str;
4179 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4180 /* ??? Follow the C++ parser rather than using the
4181 lex_untranslated_string kludge. */
4182 parser->lex_untranslated_string = true;
4183 c_parser_consume_token (parser);
4184 matching_parens parens;
4185 if (!parens.require_open (parser))
4187 parser->lex_untranslated_string = false;
4188 return NULL_TREE;
4190 str = c_parser_asm_string_literal (parser);
4191 parser->lex_untranslated_string = false;
4192 if (!parens.require_close (parser))
4194 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4195 return NULL_TREE;
4197 return str;
4200 static tree
4201 c_parser_attribute_any_word (c_parser *parser)
4203 tree attr_name = NULL_TREE;
4205 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4207 /* ??? See comment above about what keywords are accepted here. */
4208 bool ok;
4209 switch (c_parser_peek_token (parser)->keyword)
4211 case RID_STATIC:
4212 case RID_UNSIGNED:
4213 case RID_LONG:
4214 case RID_CONST:
4215 case RID_EXTERN:
4216 case RID_REGISTER:
4217 case RID_TYPEDEF:
4218 case RID_SHORT:
4219 case RID_INLINE:
4220 case RID_NORETURN:
4221 case RID_VOLATILE:
4222 case RID_SIGNED:
4223 case RID_AUTO:
4224 case RID_RESTRICT:
4225 case RID_COMPLEX:
4226 case RID_THREAD:
4227 case RID_INT:
4228 case RID_CHAR:
4229 case RID_FLOAT:
4230 case RID_DOUBLE:
4231 case RID_VOID:
4232 case RID_DFLOAT32:
4233 case RID_DFLOAT64:
4234 case RID_DFLOAT128:
4235 CASE_RID_FLOATN_NX:
4236 case RID_BOOL:
4237 case RID_FRACT:
4238 case RID_ACCUM:
4239 case RID_SAT:
4240 case RID_TRANSACTION_ATOMIC:
4241 case RID_TRANSACTION_CANCEL:
4242 case RID_ATOMIC:
4243 case RID_AUTO_TYPE:
4244 case RID_INT_N_0:
4245 case RID_INT_N_1:
4246 case RID_INT_N_2:
4247 case RID_INT_N_3:
4248 ok = true;
4249 break;
4250 default:
4251 ok = false;
4252 break;
4254 if (!ok)
4255 return NULL_TREE;
4257 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4258 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4260 else if (c_parser_next_token_is (parser, CPP_NAME))
4261 attr_name = c_parser_peek_token (parser)->value;
4263 return attr_name;
4266 /* Parse (possibly empty) attributes. This is a GNU extension.
4268 attributes:
4269 empty
4270 attributes attribute
4272 attribute:
4273 __attribute__ ( ( attribute-list ) )
4275 attribute-list:
4276 attrib
4277 attribute_list , attrib
4279 attrib:
4280 empty
4281 any-word
4282 any-word ( identifier )
4283 any-word ( identifier , nonempty-expr-list )
4284 any-word ( expr-list )
4286 where the "identifier" must not be declared as a type, and
4287 "any-word" may be any identifier (including one declared as a
4288 type), a reserved word storage class specifier, type specifier or
4289 type qualifier. ??? This still leaves out most reserved keywords
4290 (following the old parser), shouldn't we include them, and why not
4291 allow identifiers declared as types to start the arguments? */
4293 static tree
4294 c_parser_attributes (c_parser *parser)
4296 tree attrs = NULL_TREE;
4297 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4299 /* ??? Follow the C++ parser rather than using the
4300 lex_untranslated_string kludge. */
4301 parser->lex_untranslated_string = true;
4302 /* Consume the `__attribute__' keyword. */
4303 c_parser_consume_token (parser);
4304 /* Look for the two `(' tokens. */
4305 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4307 parser->lex_untranslated_string = false;
4308 return attrs;
4310 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4312 parser->lex_untranslated_string = false;
4313 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4314 return attrs;
4316 /* Parse the attribute list. */
4317 while (c_parser_next_token_is (parser, CPP_COMMA)
4318 || c_parser_next_token_is (parser, CPP_NAME)
4319 || c_parser_next_token_is (parser, CPP_KEYWORD))
4321 tree attr, attr_name, attr_args;
4322 vec<tree, va_gc> *expr_list;
4323 if (c_parser_next_token_is (parser, CPP_COMMA))
4325 c_parser_consume_token (parser);
4326 continue;
4329 attr_name = c_parser_attribute_any_word (parser);
4330 if (attr_name == NULL)
4331 break;
4332 attr_name = canonicalize_attr_name (attr_name);
4333 c_parser_consume_token (parser);
4334 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4336 attr = build_tree_list (attr_name, NULL_TREE);
4337 /* Add this attribute to the list. */
4338 attrs = chainon (attrs, attr);
4339 /* If the next token isn't a comma, we're done. */
4340 if (!c_parser_next_token_is (parser, CPP_COMMA))
4341 break;
4342 continue;
4344 c_parser_consume_token (parser);
4345 /* Parse the attribute contents. If they start with an
4346 identifier which is followed by a comma or close
4347 parenthesis, then the arguments start with that
4348 identifier; otherwise they are an expression list.
4349 In objective-c the identifier may be a classname. */
4350 if (c_parser_next_token_is (parser, CPP_NAME)
4351 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4352 || (c_dialect_objc ()
4353 && c_parser_peek_token (parser)->id_kind
4354 == C_ID_CLASSNAME))
4355 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4356 || (c_parser_peek_2nd_token (parser)->type
4357 == CPP_CLOSE_PAREN))
4358 && (attribute_takes_identifier_p (attr_name)
4359 || (c_dialect_objc ()
4360 && c_parser_peek_token (parser)->id_kind
4361 == C_ID_CLASSNAME)))
4363 tree arg1 = c_parser_peek_token (parser)->value;
4364 c_parser_consume_token (parser);
4365 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4366 attr_args = build_tree_list (NULL_TREE, arg1);
4367 else
4369 tree tree_list;
4370 c_parser_consume_token (parser);
4371 expr_list = c_parser_expr_list (parser, false, true,
4372 NULL, NULL, NULL, NULL);
4373 tree_list = build_tree_list_vec (expr_list);
4374 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4375 release_tree_vector (expr_list);
4378 else
4380 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4381 attr_args = NULL_TREE;
4382 else
4384 expr_list = c_parser_expr_list (parser, false, true,
4385 NULL, NULL, NULL, NULL);
4386 attr_args = build_tree_list_vec (expr_list);
4387 release_tree_vector (expr_list);
4391 attr = build_tree_list (attr_name, attr_args);
4392 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4393 c_parser_consume_token (parser);
4394 else
4396 parser->lex_untranslated_string = false;
4397 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4398 "expected %<)%>");
4399 return attrs;
4401 /* Add this attribute to the list. */
4402 attrs = chainon (attrs, attr);
4403 /* If the next token isn't a comma, we're done. */
4404 if (!c_parser_next_token_is (parser, CPP_COMMA))
4405 break;
4407 /* Look for the two `)' tokens. */
4408 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4409 c_parser_consume_token (parser);
4410 else
4412 parser->lex_untranslated_string = false;
4413 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4414 "expected %<)%>");
4415 return attrs;
4417 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4418 c_parser_consume_token (parser);
4419 else
4421 parser->lex_untranslated_string = false;
4422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4423 "expected %<)%>");
4424 return attrs;
4426 parser->lex_untranslated_string = false;
4429 return attrs;
4432 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4433 says whether alignment specifiers are OK (only in cases that might
4434 be the type name of a compound literal).
4436 type-name:
4437 specifier-qualifier-list abstract-declarator[opt]
4440 struct c_type_name *
4441 c_parser_type_name (c_parser *parser, bool alignas_ok)
4443 struct c_declspecs *specs = build_null_declspecs ();
4444 struct c_declarator *declarator;
4445 struct c_type_name *ret;
4446 bool dummy = false;
4447 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4448 cla_prefer_type);
4449 if (!specs->declspecs_seen_p)
4451 c_parser_error (parser, "expected specifier-qualifier-list");
4452 return NULL;
4454 if (specs->type != error_mark_node)
4456 pending_xref_error ();
4457 finish_declspecs (specs);
4459 declarator = c_parser_declarator (parser,
4460 specs->typespec_kind != ctsk_none,
4461 C_DTR_ABSTRACT, &dummy);
4462 if (declarator == NULL)
4463 return NULL;
4464 ret = XOBNEW (&parser_obstack, struct c_type_name);
4465 ret->specs = specs;
4466 ret->declarator = declarator;
4467 return ret;
4470 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4472 initializer:
4473 assignment-expression
4474 { initializer-list }
4475 { initializer-list , }
4477 initializer-list:
4478 designation[opt] initializer
4479 initializer-list , designation[opt] initializer
4481 designation:
4482 designator-list =
4484 designator-list:
4485 designator
4486 designator-list designator
4488 designator:
4489 array-designator
4490 . identifier
4492 array-designator:
4493 [ constant-expression ]
4495 GNU extensions:
4497 initializer:
4500 designation:
4501 array-designator
4502 identifier :
4504 array-designator:
4505 [ constant-expression ... constant-expression ]
4507 Any expression without commas is accepted in the syntax for the
4508 constant-expressions, with non-constant expressions rejected later.
4510 This function is only used for top-level initializers; for nested
4511 ones, see c_parser_initval. */
4513 static struct c_expr
4514 c_parser_initializer (c_parser *parser)
4516 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4517 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4518 else
4520 struct c_expr ret;
4521 location_t loc = c_parser_peek_token (parser)->location;
4522 ret = c_parser_expr_no_commas (parser, NULL);
4523 if (TREE_CODE (ret.value) != STRING_CST
4524 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4525 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4526 return ret;
4530 /* The location of the last comma within the current initializer list,
4531 or UNKNOWN_LOCATION if not within one. */
4533 location_t last_init_list_comma;
4535 /* Parse a braced initializer list. TYPE is the type specified for a
4536 compound literal, and NULL_TREE for other initializers and for
4537 nested braced lists. NESTED_P is true for nested braced lists,
4538 false for the list of a compound literal or the list that is the
4539 top-level initializer in a declaration. */
4541 static struct c_expr
4542 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4543 struct obstack *outer_obstack)
4545 struct c_expr ret;
4546 struct obstack braced_init_obstack;
4547 location_t brace_loc = c_parser_peek_token (parser)->location;
4548 gcc_obstack_init (&braced_init_obstack);
4549 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4550 matching_braces braces;
4551 braces.consume_open (parser);
4552 if (nested_p)
4554 finish_implicit_inits (brace_loc, outer_obstack);
4555 push_init_level (brace_loc, 0, &braced_init_obstack);
4557 else
4558 really_start_incremental_init (type);
4559 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4561 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4563 else
4565 /* Parse a non-empty initializer list, possibly with a trailing
4566 comma. */
4567 while (true)
4569 c_parser_initelt (parser, &braced_init_obstack);
4570 if (parser->error)
4571 break;
4572 if (c_parser_next_token_is (parser, CPP_COMMA))
4574 last_init_list_comma = c_parser_peek_token (parser)->location;
4575 c_parser_consume_token (parser);
4577 else
4578 break;
4579 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4580 break;
4583 c_token *next_tok = c_parser_peek_token (parser);
4584 if (next_tok->type != CPP_CLOSE_BRACE)
4586 ret.set_error ();
4587 ret.original_code = ERROR_MARK;
4588 ret.original_type = NULL;
4589 braces.skip_until_found_close (parser);
4590 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4591 obstack_free (&braced_init_obstack, NULL);
4592 return ret;
4594 location_t close_loc = next_tok->location;
4595 c_parser_consume_token (parser);
4596 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4597 obstack_free (&braced_init_obstack, NULL);
4598 set_c_expr_source_range (&ret, brace_loc, close_loc);
4599 return ret;
4602 /* Parse a nested initializer, including designators. */
4604 static void
4605 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4607 /* Parse any designator or designator list. A single array
4608 designator may have the subsequent "=" omitted in GNU C, but a
4609 longer list or a structure member designator may not. */
4610 if (c_parser_next_token_is (parser, CPP_NAME)
4611 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4613 /* Old-style structure member designator. */
4614 set_init_label (c_parser_peek_token (parser)->location,
4615 c_parser_peek_token (parser)->value,
4616 c_parser_peek_token (parser)->location,
4617 braced_init_obstack);
4618 /* Use the colon as the error location. */
4619 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4620 "obsolete use of designated initializer with %<:%>");
4621 c_parser_consume_token (parser);
4622 c_parser_consume_token (parser);
4624 else
4626 /* des_seen is 0 if there have been no designators, 1 if there
4627 has been a single array designator and 2 otherwise. */
4628 int des_seen = 0;
4629 /* Location of a designator. */
4630 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4631 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4632 || c_parser_next_token_is (parser, CPP_DOT))
4634 int des_prev = des_seen;
4635 if (!des_seen)
4636 des_loc = c_parser_peek_token (parser)->location;
4637 if (des_seen < 2)
4638 des_seen++;
4639 if (c_parser_next_token_is (parser, CPP_DOT))
4641 des_seen = 2;
4642 c_parser_consume_token (parser);
4643 if (c_parser_next_token_is (parser, CPP_NAME))
4645 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4646 c_parser_peek_token (parser)->location,
4647 braced_init_obstack);
4648 c_parser_consume_token (parser);
4650 else
4652 struct c_expr init;
4653 init.set_error ();
4654 init.original_code = ERROR_MARK;
4655 init.original_type = NULL;
4656 c_parser_error (parser, "expected identifier");
4657 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4658 process_init_element (input_location, init, false,
4659 braced_init_obstack);
4660 return;
4663 else
4665 tree first, second;
4666 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4667 location_t array_index_loc = UNKNOWN_LOCATION;
4668 /* ??? Following the old parser, [ objc-receiver
4669 objc-message-args ] is accepted as an initializer,
4670 being distinguished from a designator by what follows
4671 the first assignment expression inside the square
4672 brackets, but after a first array designator a
4673 subsequent square bracket is for Objective-C taken to
4674 start an expression, using the obsolete form of
4675 designated initializer without '=', rather than
4676 possibly being a second level of designation: in LALR
4677 terms, the '[' is shifted rather than reducing
4678 designator to designator-list. */
4679 if (des_prev == 1 && c_dialect_objc ())
4681 des_seen = des_prev;
4682 break;
4684 if (des_prev == 0 && c_dialect_objc ())
4686 /* This might be an array designator or an
4687 Objective-C message expression. If the former,
4688 continue parsing here; if the latter, parse the
4689 remainder of the initializer given the starting
4690 primary-expression. ??? It might make sense to
4691 distinguish when des_prev == 1 as well; see
4692 previous comment. */
4693 tree rec, args;
4694 struct c_expr mexpr;
4695 c_parser_consume_token (parser);
4696 if (c_parser_peek_token (parser)->type == CPP_NAME
4697 && ((c_parser_peek_token (parser)->id_kind
4698 == C_ID_TYPENAME)
4699 || (c_parser_peek_token (parser)->id_kind
4700 == C_ID_CLASSNAME)))
4702 /* Type name receiver. */
4703 tree id = c_parser_peek_token (parser)->value;
4704 c_parser_consume_token (parser);
4705 rec = objc_get_class_reference (id);
4706 goto parse_message_args;
4708 first = c_parser_expr_no_commas (parser, NULL).value;
4709 mark_exp_read (first);
4710 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4711 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4712 goto array_desig_after_first;
4713 /* Expression receiver. So far only one part
4714 without commas has been parsed; there might be
4715 more of the expression. */
4716 rec = first;
4717 while (c_parser_next_token_is (parser, CPP_COMMA))
4719 struct c_expr next;
4720 location_t comma_loc, exp_loc;
4721 comma_loc = c_parser_peek_token (parser)->location;
4722 c_parser_consume_token (parser);
4723 exp_loc = c_parser_peek_token (parser)->location;
4724 next = c_parser_expr_no_commas (parser, NULL);
4725 next = convert_lvalue_to_rvalue (exp_loc, next,
4726 true, true);
4727 rec = build_compound_expr (comma_loc, rec, next.value);
4729 parse_message_args:
4730 /* Now parse the objc-message-args. */
4731 args = c_parser_objc_message_args (parser);
4732 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4733 "expected %<]%>");
4734 mexpr.value
4735 = objc_build_message_expr (rec, args);
4736 mexpr.original_code = ERROR_MARK;
4737 mexpr.original_type = NULL;
4738 /* Now parse and process the remainder of the
4739 initializer, starting with this message
4740 expression as a primary-expression. */
4741 c_parser_initval (parser, &mexpr, braced_init_obstack);
4742 return;
4744 c_parser_consume_token (parser);
4745 array_index_loc = c_parser_peek_token (parser)->location;
4746 first = c_parser_expr_no_commas (parser, NULL).value;
4747 mark_exp_read (first);
4748 array_desig_after_first:
4749 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4751 ellipsis_loc = c_parser_peek_token (parser)->location;
4752 c_parser_consume_token (parser);
4753 second = c_parser_expr_no_commas (parser, NULL).value;
4754 mark_exp_read (second);
4756 else
4757 second = NULL_TREE;
4758 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4760 c_parser_consume_token (parser);
4761 set_init_index (array_index_loc, first, second,
4762 braced_init_obstack);
4763 if (second)
4764 pedwarn (ellipsis_loc, OPT_Wpedantic,
4765 "ISO C forbids specifying range of elements to initialize");
4767 else
4768 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4769 "expected %<]%>");
4772 if (des_seen >= 1)
4774 if (c_parser_next_token_is (parser, CPP_EQ))
4776 pedwarn_c90 (des_loc, OPT_Wpedantic,
4777 "ISO C90 forbids specifying subobject "
4778 "to initialize");
4779 c_parser_consume_token (parser);
4781 else
4783 if (des_seen == 1)
4784 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4785 "obsolete use of designated initializer without %<=%>");
4786 else
4788 struct c_expr init;
4789 init.set_error ();
4790 init.original_code = ERROR_MARK;
4791 init.original_type = NULL;
4792 c_parser_error (parser, "expected %<=%>");
4793 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4794 process_init_element (input_location, init, false,
4795 braced_init_obstack);
4796 return;
4801 c_parser_initval (parser, NULL, braced_init_obstack);
4804 /* Parse a nested initializer; as c_parser_initializer but parses
4805 initializers within braced lists, after any designators have been
4806 applied. If AFTER is not NULL then it is an Objective-C message
4807 expression which is the primary-expression starting the
4808 initializer. */
4810 static void
4811 c_parser_initval (c_parser *parser, struct c_expr *after,
4812 struct obstack * braced_init_obstack)
4814 struct c_expr init;
4815 gcc_assert (!after || c_dialect_objc ());
4816 location_t loc = c_parser_peek_token (parser)->location;
4818 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4819 init = c_parser_braced_init (parser, NULL_TREE, true,
4820 braced_init_obstack);
4821 else
4823 init = c_parser_expr_no_commas (parser, after);
4824 if (init.value != NULL_TREE
4825 && TREE_CODE (init.value) != STRING_CST
4826 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4827 init = convert_lvalue_to_rvalue (loc, init, true, true);
4829 process_init_element (loc, init, false, braced_init_obstack);
4832 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4833 C99 6.8.2, C11 6.8.2).
4835 compound-statement:
4836 { block-item-list[opt] }
4837 { label-declarations block-item-list }
4839 block-item-list:
4840 block-item
4841 block-item-list block-item
4843 block-item:
4844 nested-declaration
4845 statement
4847 nested-declaration:
4848 declaration
4850 GNU extensions:
4852 compound-statement:
4853 { label-declarations block-item-list }
4855 nested-declaration:
4856 __extension__ nested-declaration
4857 nested-function-definition
4859 label-declarations:
4860 label-declaration
4861 label-declarations label-declaration
4863 label-declaration:
4864 __label__ identifier-list ;
4866 Allowing the mixing of declarations and code is new in C99. The
4867 GNU syntax also permits (not shown above) labels at the end of
4868 compound statements, which yield an error. We don't allow labels
4869 on declarations; this might seem like a natural extension, but
4870 there would be a conflict between attributes on the label and
4871 prefix attributes on the declaration. ??? The syntax follows the
4872 old parser in requiring something after label declarations.
4873 Although they are erroneous if the labels declared aren't defined,
4874 is it useful for the syntax to be this way?
4876 OpenACC:
4878 block-item:
4879 openacc-directive
4881 openacc-directive:
4882 update-directive
4884 OpenMP:
4886 block-item:
4887 openmp-directive
4889 openmp-directive:
4890 barrier-directive
4891 flush-directive
4892 taskwait-directive
4893 taskyield-directive
4894 cancel-directive
4895 cancellation-point-directive */
4897 static tree
4898 c_parser_compound_statement (c_parser *parser)
4900 tree stmt;
4901 location_t brace_loc;
4902 brace_loc = c_parser_peek_token (parser)->location;
4903 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4905 /* Ensure a scope is entered and left anyway to avoid confusion
4906 if we have just prepared to enter a function body. */
4907 stmt = c_begin_compound_stmt (true);
4908 c_end_compound_stmt (brace_loc, stmt, true);
4909 return error_mark_node;
4911 stmt = c_begin_compound_stmt (true);
4912 c_parser_compound_statement_nostart (parser);
4914 return c_end_compound_stmt (brace_loc, stmt, true);
4917 /* Parse a compound statement except for the opening brace. This is
4918 used for parsing both compound statements and statement expressions
4919 (which follow different paths to handling the opening). */
4921 static void
4922 c_parser_compound_statement_nostart (c_parser *parser)
4924 bool last_stmt = false;
4925 bool last_label = false;
4926 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4927 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4928 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4930 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4931 c_parser_consume_token (parser);
4932 return;
4934 mark_valid_location_for_stdc_pragma (true);
4935 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4937 /* Read zero or more forward-declarations for labels that nested
4938 functions can jump to. */
4939 mark_valid_location_for_stdc_pragma (false);
4940 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4942 label_loc = c_parser_peek_token (parser)->location;
4943 c_parser_consume_token (parser);
4944 /* Any identifiers, including those declared as type names,
4945 are OK here. */
4946 while (true)
4948 tree label;
4949 if (c_parser_next_token_is_not (parser, CPP_NAME))
4951 c_parser_error (parser, "expected identifier");
4952 break;
4954 label
4955 = declare_label (c_parser_peek_token (parser)->value);
4956 C_DECLARED_LABEL_FLAG (label) = 1;
4957 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4958 c_parser_consume_token (parser);
4959 if (c_parser_next_token_is (parser, CPP_COMMA))
4960 c_parser_consume_token (parser);
4961 else
4962 break;
4964 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4966 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4968 /* We must now have at least one statement, label or declaration. */
4969 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4971 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4972 c_parser_error (parser, "expected declaration or statement");
4973 c_parser_consume_token (parser);
4974 return;
4976 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4978 location_t loc = c_parser_peek_token (parser)->location;
4979 loc = expansion_point_location_if_in_system_header (loc);
4980 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4981 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4982 || (c_parser_next_token_is (parser, CPP_NAME)
4983 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4985 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4986 label_loc = c_parser_peek_2nd_token (parser)->location;
4987 else
4988 label_loc = c_parser_peek_token (parser)->location;
4989 last_label = true;
4990 last_stmt = false;
4991 mark_valid_location_for_stdc_pragma (false);
4992 c_parser_label (parser);
4994 else if (!last_label
4995 && c_parser_next_tokens_start_declaration (parser))
4997 last_label = false;
4998 mark_valid_location_for_stdc_pragma (false);
4999 bool fallthru_attr_p = false;
5000 c_parser_declaration_or_fndef (parser, true, true, true, true,
5001 true, NULL, vNULL, NULL,
5002 &fallthru_attr_p);
5003 if (last_stmt && !fallthru_attr_p)
5004 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5005 "ISO C90 forbids mixed declarations and code");
5006 last_stmt = fallthru_attr_p;
5008 else if (!last_label
5009 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5011 /* __extension__ can start a declaration, but is also an
5012 unary operator that can start an expression. Consume all
5013 but the last of a possible series of __extension__ to
5014 determine which. */
5015 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5016 && (c_parser_peek_2nd_token (parser)->keyword
5017 == RID_EXTENSION))
5018 c_parser_consume_token (parser);
5019 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5021 int ext;
5022 ext = disable_extension_diagnostics ();
5023 c_parser_consume_token (parser);
5024 last_label = false;
5025 mark_valid_location_for_stdc_pragma (false);
5026 c_parser_declaration_or_fndef (parser, true, true, true, true,
5027 true, NULL, vNULL);
5028 /* Following the old parser, __extension__ does not
5029 disable this diagnostic. */
5030 restore_extension_diagnostics (ext);
5031 if (last_stmt)
5032 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5033 "ISO C90 forbids mixed declarations and code");
5034 last_stmt = false;
5036 else
5037 goto statement;
5039 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5041 /* External pragmas, and some omp pragmas, are not associated
5042 with regular c code, and so are not to be considered statements
5043 syntactically. This ensures that the user doesn't put them
5044 places that would turn into syntax errors if the directive
5045 were ignored. */
5046 if (c_parser_pragma (parser,
5047 last_label ? pragma_stmt : pragma_compound,
5048 NULL))
5049 last_label = false, last_stmt = true;
5051 else if (c_parser_next_token_is (parser, CPP_EOF))
5053 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5054 c_parser_error (parser, "expected declaration or statement");
5055 return;
5057 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5059 if (parser->in_if_block)
5061 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5062 error_at (loc, "expected %<}%> before %<else%>");
5063 return;
5065 else
5067 error_at (loc, "%<else%> without a previous %<if%>");
5068 c_parser_consume_token (parser);
5069 continue;
5072 else
5074 statement:
5075 last_label = false;
5076 last_stmt = true;
5077 mark_valid_location_for_stdc_pragma (false);
5078 c_parser_statement_after_labels (parser, NULL);
5081 parser->error = false;
5083 if (last_label)
5084 error_at (label_loc, "label at end of compound statement");
5085 c_parser_consume_token (parser);
5086 /* Restore the value we started with. */
5087 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5090 /* Parse all consecutive labels. */
5092 static void
5093 c_parser_all_labels (c_parser *parser)
5095 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5096 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5097 || (c_parser_next_token_is (parser, CPP_NAME)
5098 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5099 c_parser_label (parser);
5102 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5104 label:
5105 identifier : attributes[opt]
5106 case constant-expression :
5107 default :
5109 GNU extensions:
5111 label:
5112 case constant-expression ... constant-expression :
5114 The use of attributes on labels is a GNU extension. The syntax in
5115 GNU C accepts any expressions without commas, non-constant
5116 expressions being rejected later. */
5118 static void
5119 c_parser_label (c_parser *parser)
5121 location_t loc1 = c_parser_peek_token (parser)->location;
5122 tree label = NULL_TREE;
5124 /* Remember whether this case or a user-defined label is allowed to fall
5125 through to. */
5126 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5128 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5130 tree exp1, exp2;
5131 c_parser_consume_token (parser);
5132 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5133 if (c_parser_next_token_is (parser, CPP_COLON))
5135 c_parser_consume_token (parser);
5136 label = do_case (loc1, exp1, NULL_TREE);
5138 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5140 c_parser_consume_token (parser);
5141 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5142 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5143 label = do_case (loc1, exp1, exp2);
5145 else
5146 c_parser_error (parser, "expected %<:%> or %<...%>");
5148 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5150 c_parser_consume_token (parser);
5151 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5152 label = do_case (loc1, NULL_TREE, NULL_TREE);
5154 else
5156 tree name = c_parser_peek_token (parser)->value;
5157 tree tlab;
5158 tree attrs;
5159 location_t loc2 = c_parser_peek_token (parser)->location;
5160 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5161 c_parser_consume_token (parser);
5162 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5163 c_parser_consume_token (parser);
5164 attrs = c_parser_attributes (parser);
5165 tlab = define_label (loc2, name);
5166 if (tlab)
5168 decl_attributes (&tlab, attrs, 0);
5169 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5172 if (label)
5174 if (TREE_CODE (label) == LABEL_EXPR)
5175 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5176 else
5177 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5179 /* Allow '__attribute__((fallthrough));'. */
5180 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5182 location_t loc = c_parser_peek_token (parser)->location;
5183 tree attrs = c_parser_attributes (parser);
5184 if (attribute_fallthrough_p (attrs))
5186 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5188 tree fn = build_call_expr_internal_loc (loc,
5189 IFN_FALLTHROUGH,
5190 void_type_node, 0);
5191 add_stmt (fn);
5193 else
5194 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5195 "not followed by %<;%>");
5197 else if (attrs != NULL_TREE)
5198 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5199 " can be applied to a null statement");
5201 if (c_parser_next_tokens_start_declaration (parser))
5203 error_at (c_parser_peek_token (parser)->location,
5204 "a label can only be part of a statement and "
5205 "a declaration is not a statement");
5206 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5207 /*static_assert_ok*/ true,
5208 /*empty_ok*/ true, /*nested*/ true,
5209 /*start_attr_ok*/ true, NULL,
5210 vNULL);
5215 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5217 statement:
5218 labeled-statement
5219 compound-statement
5220 expression-statement
5221 selection-statement
5222 iteration-statement
5223 jump-statement
5225 labeled-statement:
5226 label statement
5228 expression-statement:
5229 expression[opt] ;
5231 selection-statement:
5232 if-statement
5233 switch-statement
5235 iteration-statement:
5236 while-statement
5237 do-statement
5238 for-statement
5240 jump-statement:
5241 goto identifier ;
5242 continue ;
5243 break ;
5244 return expression[opt] ;
5246 GNU extensions:
5248 statement:
5249 asm-statement
5251 jump-statement:
5252 goto * expression ;
5254 expression-statement:
5255 attributes ;
5257 Objective-C:
5259 statement:
5260 objc-throw-statement
5261 objc-try-catch-statement
5262 objc-synchronized-statement
5264 objc-throw-statement:
5265 @throw expression ;
5266 @throw ;
5268 OpenACC:
5270 statement:
5271 openacc-construct
5273 openacc-construct:
5274 parallel-construct
5275 kernels-construct
5276 data-construct
5277 loop-construct
5279 parallel-construct:
5280 parallel-directive structured-block
5282 kernels-construct:
5283 kernels-directive structured-block
5285 data-construct:
5286 data-directive structured-block
5288 loop-construct:
5289 loop-directive structured-block
5291 OpenMP:
5293 statement:
5294 openmp-construct
5296 openmp-construct:
5297 parallel-construct
5298 for-construct
5299 simd-construct
5300 for-simd-construct
5301 sections-construct
5302 single-construct
5303 parallel-for-construct
5304 parallel-for-simd-construct
5305 parallel-sections-construct
5306 master-construct
5307 critical-construct
5308 atomic-construct
5309 ordered-construct
5311 parallel-construct:
5312 parallel-directive structured-block
5314 for-construct:
5315 for-directive iteration-statement
5317 simd-construct:
5318 simd-directive iteration-statements
5320 for-simd-construct:
5321 for-simd-directive iteration-statements
5323 sections-construct:
5324 sections-directive section-scope
5326 single-construct:
5327 single-directive structured-block
5329 parallel-for-construct:
5330 parallel-for-directive iteration-statement
5332 parallel-for-simd-construct:
5333 parallel-for-simd-directive iteration-statement
5335 parallel-sections-construct:
5336 parallel-sections-directive section-scope
5338 master-construct:
5339 master-directive structured-block
5341 critical-construct:
5342 critical-directive structured-block
5344 atomic-construct:
5345 atomic-directive expression-statement
5347 ordered-construct:
5348 ordered-directive structured-block
5350 Transactional Memory:
5352 statement:
5353 transaction-statement
5354 transaction-cancel-statement
5356 IF_P is used to track whether there's a (possibly labeled) if statement
5357 which is not enclosed in braces and has an else clause. This is used to
5358 implement -Wparentheses. */
5360 static void
5361 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5363 c_parser_all_labels (parser);
5364 if (loc_after_labels)
5365 *loc_after_labels = c_parser_peek_token (parser)->location;
5366 c_parser_statement_after_labels (parser, if_p, NULL);
5369 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5370 of if-else-if conditions.
5372 IF_P is used to track whether there's a (possibly labeled) if statement
5373 which is not enclosed in braces and has an else clause. This is used to
5374 implement -Wparentheses. */
5376 static void
5377 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5378 vec<tree> *chain)
5380 location_t loc = c_parser_peek_token (parser)->location;
5381 tree stmt = NULL_TREE;
5382 bool in_if_block = parser->in_if_block;
5383 parser->in_if_block = false;
5384 if (if_p != NULL)
5385 *if_p = false;
5387 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5388 add_debug_begin_stmt (loc);
5390 switch (c_parser_peek_token (parser)->type)
5392 case CPP_OPEN_BRACE:
5393 add_stmt (c_parser_compound_statement (parser));
5394 break;
5395 case CPP_KEYWORD:
5396 switch (c_parser_peek_token (parser)->keyword)
5398 case RID_IF:
5399 c_parser_if_statement (parser, if_p, chain);
5400 break;
5401 case RID_SWITCH:
5402 c_parser_switch_statement (parser, if_p);
5403 break;
5404 case RID_WHILE:
5405 c_parser_while_statement (parser, false, 0, if_p);
5406 break;
5407 case RID_DO:
5408 c_parser_do_statement (parser, 0, false);
5409 break;
5410 case RID_FOR:
5411 c_parser_for_statement (parser, false, 0, if_p);
5412 break;
5413 case RID_GOTO:
5414 c_parser_consume_token (parser);
5415 if (c_parser_next_token_is (parser, CPP_NAME))
5417 stmt = c_finish_goto_label (loc,
5418 c_parser_peek_token (parser)->value);
5419 c_parser_consume_token (parser);
5421 else if (c_parser_next_token_is (parser, CPP_MULT))
5423 struct c_expr val;
5425 c_parser_consume_token (parser);
5426 val = c_parser_expression (parser);
5427 val = convert_lvalue_to_rvalue (loc, val, false, true);
5428 stmt = c_finish_goto_ptr (loc, val.value);
5430 else
5431 c_parser_error (parser, "expected identifier or %<*%>");
5432 goto expect_semicolon;
5433 case RID_CONTINUE:
5434 c_parser_consume_token (parser);
5435 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5436 goto expect_semicolon;
5437 case RID_BREAK:
5438 c_parser_consume_token (parser);
5439 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5440 goto expect_semicolon;
5441 case RID_RETURN:
5442 c_parser_consume_token (parser);
5443 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5445 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5446 c_parser_consume_token (parser);
5448 else
5450 location_t xloc = c_parser_peek_token (parser)->location;
5451 struct c_expr expr = c_parser_expression_conv (parser);
5452 mark_exp_read (expr.value);
5453 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5454 expr.value, expr.original_type);
5455 goto expect_semicolon;
5457 break;
5458 case RID_ASM:
5459 stmt = c_parser_asm_statement (parser);
5460 break;
5461 case RID_TRANSACTION_ATOMIC:
5462 case RID_TRANSACTION_RELAXED:
5463 stmt = c_parser_transaction (parser,
5464 c_parser_peek_token (parser)->keyword);
5465 break;
5466 case RID_TRANSACTION_CANCEL:
5467 stmt = c_parser_transaction_cancel (parser);
5468 goto expect_semicolon;
5469 case RID_AT_THROW:
5470 gcc_assert (c_dialect_objc ());
5471 c_parser_consume_token (parser);
5472 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5474 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5475 c_parser_consume_token (parser);
5477 else
5479 struct c_expr expr = c_parser_expression (parser);
5480 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5481 expr.value = c_fully_fold (expr.value, false, NULL);
5482 stmt = objc_build_throw_stmt (loc, expr.value);
5483 goto expect_semicolon;
5485 break;
5486 case RID_AT_TRY:
5487 gcc_assert (c_dialect_objc ());
5488 c_parser_objc_try_catch_finally_statement (parser);
5489 break;
5490 case RID_AT_SYNCHRONIZED:
5491 gcc_assert (c_dialect_objc ());
5492 c_parser_objc_synchronized_statement (parser);
5493 break;
5494 case RID_ATTRIBUTE:
5496 /* Allow '__attribute__((fallthrough));'. */
5497 tree attrs = c_parser_attributes (parser);
5498 if (attribute_fallthrough_p (attrs))
5500 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5502 tree fn = build_call_expr_internal_loc (loc,
5503 IFN_FALLTHROUGH,
5504 void_type_node, 0);
5505 add_stmt (fn);
5506 /* Eat the ';'. */
5507 c_parser_consume_token (parser);
5509 else
5510 warning_at (loc, OPT_Wattributes,
5511 "%<fallthrough%> attribute not followed "
5512 "by %<;%>");
5514 else if (attrs != NULL_TREE)
5515 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5516 " can be applied to a null statement");
5517 break;
5519 default:
5520 goto expr_stmt;
5522 break;
5523 case CPP_SEMICOLON:
5524 c_parser_consume_token (parser);
5525 break;
5526 case CPP_CLOSE_PAREN:
5527 case CPP_CLOSE_SQUARE:
5528 /* Avoid infinite loop in error recovery:
5529 c_parser_skip_until_found stops at a closing nesting
5530 delimiter without consuming it, but here we need to consume
5531 it to proceed further. */
5532 c_parser_error (parser, "expected statement");
5533 c_parser_consume_token (parser);
5534 break;
5535 case CPP_PRAGMA:
5536 c_parser_pragma (parser, pragma_stmt, if_p);
5537 break;
5538 default:
5539 expr_stmt:
5540 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5541 expect_semicolon:
5542 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5543 break;
5545 /* Two cases cannot and do not have line numbers associated: If stmt
5546 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5547 cannot hold line numbers. But that's OK because the statement
5548 will either be changed to a MODIFY_EXPR during gimplification of
5549 the statement expr, or discarded. If stmt was compound, but
5550 without new variables, we will have skipped the creation of a
5551 BIND and will have a bare STATEMENT_LIST. But that's OK because
5552 (recursively) all of the component statements should already have
5553 line numbers assigned. ??? Can we discard no-op statements
5554 earlier? */
5555 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5556 protected_set_expr_location (stmt, loc);
5558 parser->in_if_block = in_if_block;
5561 /* Parse the condition from an if, do, while or for statements. */
5563 static tree
5564 c_parser_condition (c_parser *parser)
5566 location_t loc = c_parser_peek_token (parser)->location;
5567 tree cond;
5568 cond = c_parser_expression_conv (parser).value;
5569 cond = c_objc_common_truthvalue_conversion (loc, cond);
5570 cond = c_fully_fold (cond, false, NULL);
5571 if (warn_sequence_point)
5572 verify_sequence_points (cond);
5573 return cond;
5576 /* Parse a parenthesized condition from an if, do or while statement.
5578 condition:
5579 ( expression )
5581 static tree
5582 c_parser_paren_condition (c_parser *parser)
5584 tree cond;
5585 matching_parens parens;
5586 if (!parens.require_open (parser))
5587 return error_mark_node;
5588 cond = c_parser_condition (parser);
5589 parens.skip_until_found_close (parser);
5590 return cond;
5593 /* Parse a statement which is a block in C99.
5595 IF_P is used to track whether there's a (possibly labeled) if statement
5596 which is not enclosed in braces and has an else clause. This is used to
5597 implement -Wparentheses. */
5599 static tree
5600 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5601 location_t *loc_after_labels)
5603 tree block = c_begin_compound_stmt (flag_isoc99);
5604 location_t loc = c_parser_peek_token (parser)->location;
5605 c_parser_statement (parser, if_p, loc_after_labels);
5606 return c_end_compound_stmt (loc, block, flag_isoc99);
5609 /* Parse the body of an if statement. This is just parsing a
5610 statement but (a) it is a block in C99, (b) we track whether the
5611 body is an if statement for the sake of -Wparentheses warnings, (c)
5612 we handle an empty body specially for the sake of -Wempty-body
5613 warnings, and (d) we call parser_compound_statement directly
5614 because c_parser_statement_after_labels resets
5615 parser->in_if_block.
5617 IF_P is used to track whether there's a (possibly labeled) if statement
5618 which is not enclosed in braces and has an else clause. This is used to
5619 implement -Wparentheses. */
5621 static tree
5622 c_parser_if_body (c_parser *parser, bool *if_p,
5623 const token_indent_info &if_tinfo)
5625 tree block = c_begin_compound_stmt (flag_isoc99);
5626 location_t body_loc = c_parser_peek_token (parser)->location;
5627 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5628 token_indent_info body_tinfo
5629 = get_token_indent_info (c_parser_peek_token (parser));
5631 c_parser_all_labels (parser);
5632 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5634 location_t loc = c_parser_peek_token (parser)->location;
5635 add_stmt (build_empty_stmt (loc));
5636 c_parser_consume_token (parser);
5637 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5638 warning_at (loc, OPT_Wempty_body,
5639 "suggest braces around empty body in an %<if%> statement");
5641 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5642 add_stmt (c_parser_compound_statement (parser));
5643 else
5645 body_loc_after_labels = c_parser_peek_token (parser)->location;
5646 c_parser_statement_after_labels (parser, if_p);
5649 token_indent_info next_tinfo
5650 = get_token_indent_info (c_parser_peek_token (parser));
5651 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5652 if (body_loc_after_labels != UNKNOWN_LOCATION
5653 && next_tinfo.type != CPP_SEMICOLON)
5654 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5655 if_tinfo.location, RID_IF);
5657 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5660 /* Parse the else body of an if statement. This is just parsing a
5661 statement but (a) it is a block in C99, (b) we handle an empty body
5662 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5663 of if-else-if conditions. */
5665 static tree
5666 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5667 vec<tree> *chain)
5669 location_t body_loc = c_parser_peek_token (parser)->location;
5670 tree block = c_begin_compound_stmt (flag_isoc99);
5671 token_indent_info body_tinfo
5672 = get_token_indent_info (c_parser_peek_token (parser));
5673 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5675 c_parser_all_labels (parser);
5676 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5678 location_t loc = c_parser_peek_token (parser)->location;
5679 warning_at (loc,
5680 OPT_Wempty_body,
5681 "suggest braces around empty body in an %<else%> statement");
5682 add_stmt (build_empty_stmt (loc));
5683 c_parser_consume_token (parser);
5685 else
5687 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5688 body_loc_after_labels = c_parser_peek_token (parser)->location;
5689 c_parser_statement_after_labels (parser, NULL, chain);
5692 token_indent_info next_tinfo
5693 = get_token_indent_info (c_parser_peek_token (parser));
5694 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5695 if (body_loc_after_labels != UNKNOWN_LOCATION
5696 && next_tinfo.type != CPP_SEMICOLON)
5697 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5698 else_tinfo.location, RID_ELSE);
5700 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5703 /* We might need to reclassify any previously-lexed identifier, e.g.
5704 when we've left a for loop with an if-statement without else in the
5705 body - we might have used a wrong scope for the token. See PR67784. */
5707 static void
5708 c_parser_maybe_reclassify_token (c_parser *parser)
5710 if (c_parser_next_token_is (parser, CPP_NAME))
5712 c_token *token = c_parser_peek_token (parser);
5714 if (token->id_kind != C_ID_CLASSNAME)
5716 tree decl = lookup_name (token->value);
5718 token->id_kind = C_ID_ID;
5719 if (decl)
5721 if (TREE_CODE (decl) == TYPE_DECL)
5722 token->id_kind = C_ID_TYPENAME;
5724 else if (c_dialect_objc ())
5726 tree objc_interface_decl = objc_is_class_name (token->value);
5727 /* Objective-C class names are in the same namespace as
5728 variables and typedefs, and hence are shadowed by local
5729 declarations. */
5730 if (objc_interface_decl)
5732 token->value = objc_interface_decl;
5733 token->id_kind = C_ID_CLASSNAME;
5740 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5742 if-statement:
5743 if ( expression ) statement
5744 if ( expression ) statement else statement
5746 CHAIN is a vector of if-else-if conditions.
5747 IF_P is used to track whether there's a (possibly labeled) if statement
5748 which is not enclosed in braces and has an else clause. This is used to
5749 implement -Wparentheses. */
5751 static void
5752 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5754 tree block;
5755 location_t loc;
5756 tree cond;
5757 bool nested_if = false;
5758 tree first_body, second_body;
5759 bool in_if_block;
5761 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5762 token_indent_info if_tinfo
5763 = get_token_indent_info (c_parser_peek_token (parser));
5764 c_parser_consume_token (parser);
5765 block = c_begin_compound_stmt (flag_isoc99);
5766 loc = c_parser_peek_token (parser)->location;
5767 cond = c_parser_paren_condition (parser);
5768 in_if_block = parser->in_if_block;
5769 parser->in_if_block = true;
5770 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5771 parser->in_if_block = in_if_block;
5773 if (warn_duplicated_cond)
5774 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5776 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5778 token_indent_info else_tinfo
5779 = get_token_indent_info (c_parser_peek_token (parser));
5780 c_parser_consume_token (parser);
5781 if (warn_duplicated_cond)
5783 if (c_parser_next_token_is_keyword (parser, RID_IF)
5784 && chain == NULL)
5786 /* We've got "if (COND) else if (COND2)". Start the
5787 condition chain and add COND as the first element. */
5788 chain = new vec<tree> ();
5789 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5790 chain->safe_push (cond);
5792 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5794 /* This is if-else without subsequent if. Zap the condition
5795 chain; we would have already warned at this point. */
5796 delete chain;
5797 chain = NULL;
5800 second_body = c_parser_else_body (parser, else_tinfo, chain);
5801 /* Set IF_P to true to indicate that this if statement has an
5802 else clause. This may trigger the Wparentheses warning
5803 below when we get back up to the parent if statement. */
5804 if (if_p != NULL)
5805 *if_p = true;
5807 else
5809 second_body = NULL_TREE;
5811 /* Diagnose an ambiguous else if if-then-else is nested inside
5812 if-then. */
5813 if (nested_if)
5814 warning_at (loc, OPT_Wdangling_else,
5815 "suggest explicit braces to avoid ambiguous %<else%>");
5817 if (warn_duplicated_cond)
5819 /* This if statement does not have an else clause. We don't
5820 need the condition chain anymore. */
5821 delete chain;
5822 chain = NULL;
5825 c_finish_if_stmt (loc, cond, first_body, second_body);
5826 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5828 c_parser_maybe_reclassify_token (parser);
5831 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5833 switch-statement:
5834 switch (expression) statement
5837 static void
5838 c_parser_switch_statement (c_parser *parser, bool *if_p)
5840 struct c_expr ce;
5841 tree block, expr, body, save_break;
5842 location_t switch_loc = c_parser_peek_token (parser)->location;
5843 location_t switch_cond_loc;
5844 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5845 c_parser_consume_token (parser);
5846 block = c_begin_compound_stmt (flag_isoc99);
5847 bool explicit_cast_p = false;
5848 matching_parens parens;
5849 if (parens.require_open (parser))
5851 switch_cond_loc = c_parser_peek_token (parser)->location;
5852 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5853 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5854 explicit_cast_p = true;
5855 ce = c_parser_expression (parser);
5856 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5857 expr = ce.value;
5858 /* ??? expr has no valid location? */
5859 parens.skip_until_found_close (parser);
5861 else
5863 switch_cond_loc = UNKNOWN_LOCATION;
5864 expr = error_mark_node;
5865 ce.original_type = error_mark_node;
5867 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5868 save_break = c_break_label;
5869 c_break_label = NULL_TREE;
5870 location_t loc_after_labels;
5871 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5872 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5873 location_t next_loc = c_parser_peek_token (parser)->location;
5874 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5875 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5876 RID_SWITCH);
5877 if (c_break_label)
5879 location_t here = c_parser_peek_token (parser)->location;
5880 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5881 SET_EXPR_LOCATION (t, here);
5882 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5883 append_to_statement_list_force (t, &body);
5885 c_finish_case (body, ce.original_type);
5886 c_break_label = save_break;
5887 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5888 c_parser_maybe_reclassify_token (parser);
5891 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5893 while-statement:
5894 while (expression) statement
5896 IF_P is used to track whether there's a (possibly labeled) if statement
5897 which is not enclosed in braces and has an else clause. This is used to
5898 implement -Wparentheses. */
5900 static void
5901 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5902 bool *if_p)
5904 tree block, cond, body, save_break, save_cont;
5905 location_t loc;
5906 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5907 token_indent_info while_tinfo
5908 = get_token_indent_info (c_parser_peek_token (parser));
5909 c_parser_consume_token (parser);
5910 block = c_begin_compound_stmt (flag_isoc99);
5911 loc = c_parser_peek_token (parser)->location;
5912 cond = c_parser_paren_condition (parser);
5913 if (ivdep && cond != error_mark_node)
5914 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5915 build_int_cst (integer_type_node,
5916 annot_expr_ivdep_kind),
5917 integer_zero_node);
5918 if (unroll && cond != error_mark_node)
5919 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5920 build_int_cst (integer_type_node,
5921 annot_expr_unroll_kind),
5922 build_int_cst (integer_type_node, unroll));
5923 save_break = c_break_label;
5924 c_break_label = NULL_TREE;
5925 save_cont = c_cont_label;
5926 c_cont_label = NULL_TREE;
5928 token_indent_info body_tinfo
5929 = get_token_indent_info (c_parser_peek_token (parser));
5931 location_t loc_after_labels;
5932 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5933 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5934 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5935 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5936 c_parser_maybe_reclassify_token (parser);
5938 token_indent_info next_tinfo
5939 = get_token_indent_info (c_parser_peek_token (parser));
5940 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5942 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5943 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5944 while_tinfo.location, RID_WHILE);
5946 c_break_label = save_break;
5947 c_cont_label = save_cont;
5950 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5952 do-statement:
5953 do statement while ( expression ) ;
5956 static void
5957 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
5959 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5960 location_t loc;
5961 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5962 c_parser_consume_token (parser);
5963 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5964 warning_at (c_parser_peek_token (parser)->location,
5965 OPT_Wempty_body,
5966 "suggest braces around empty body in %<do%> statement");
5967 block = c_begin_compound_stmt (flag_isoc99);
5968 loc = c_parser_peek_token (parser)->location;
5969 save_break = c_break_label;
5970 c_break_label = NULL_TREE;
5971 save_cont = c_cont_label;
5972 c_cont_label = NULL_TREE;
5973 body = c_parser_c99_block_statement (parser, NULL);
5974 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5975 new_break = c_break_label;
5976 c_break_label = save_break;
5977 new_cont = c_cont_label;
5978 c_cont_label = save_cont;
5979 cond = c_parser_paren_condition (parser);
5980 if (ivdep && cond != error_mark_node)
5981 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5982 build_int_cst (integer_type_node,
5983 annot_expr_ivdep_kind),
5984 integer_zero_node);
5985 if (unroll && cond != error_mark_node)
5986 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5987 build_int_cst (integer_type_node,
5988 annot_expr_unroll_kind),
5989 build_int_cst (integer_type_node, unroll));
5990 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5991 c_parser_skip_to_end_of_block_or_statement (parser);
5992 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5993 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5996 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5998 for-statement:
5999 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6000 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6002 The form with a declaration is new in C99.
6004 ??? In accordance with the old parser, the declaration may be a
6005 nested function, which is then rejected in check_for_loop_decls,
6006 but does it make any sense for this to be included in the grammar?
6007 Note in particular that the nested function does not include a
6008 trailing ';', whereas the "declaration" production includes one.
6009 Also, can we reject bad declarations earlier and cheaper than
6010 check_for_loop_decls?
6012 In Objective-C, there are two additional variants:
6014 foreach-statement:
6015 for ( expression in expresssion ) statement
6016 for ( declaration in expression ) statement
6018 This is inconsistent with C, because the second variant is allowed
6019 even if c99 is not enabled.
6021 The rest of the comment documents these Objective-C foreach-statement.
6023 Here is the canonical example of the first variant:
6024 for (object in array) { do something with object }
6025 we call the first expression ("object") the "object_expression" and
6026 the second expression ("array") the "collection_expression".
6027 object_expression must be an lvalue of type "id" (a generic Objective-C
6028 object) because the loop works by assigning to object_expression the
6029 various objects from the collection_expression. collection_expression
6030 must evaluate to something of type "id" which responds to the method
6031 countByEnumeratingWithState:objects:count:.
6033 The canonical example of the second variant is:
6034 for (id object in array) { do something with object }
6035 which is completely equivalent to
6037 id object;
6038 for (object in array) { do something with object }
6040 Note that initizializing 'object' in some way (eg, "for ((object =
6041 xxx) in array) { do something with object }") is possibly
6042 technically valid, but completely pointless as 'object' will be
6043 assigned to something else as soon as the loop starts. We should
6044 most likely reject it (TODO).
6046 The beginning of the Objective-C foreach-statement looks exactly
6047 like the beginning of the for-statement, and we can tell it is a
6048 foreach-statement only because the initial declaration or
6049 expression is terminated by 'in' instead of ';'.
6051 IF_P is used to track whether there's a (possibly labeled) if statement
6052 which is not enclosed in braces and has an else clause. This is used to
6053 implement -Wparentheses. */
6055 static void
6056 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6057 bool *if_p)
6059 tree block, cond, incr, save_break, save_cont, body;
6060 /* The following are only used when parsing an ObjC foreach statement. */
6061 tree object_expression;
6062 /* Silence the bogus uninitialized warning. */
6063 tree collection_expression = NULL;
6064 location_t loc = c_parser_peek_token (parser)->location;
6065 location_t for_loc = c_parser_peek_token (parser)->location;
6066 bool is_foreach_statement = false;
6067 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6068 token_indent_info for_tinfo
6069 = get_token_indent_info (c_parser_peek_token (parser));
6070 c_parser_consume_token (parser);
6071 /* Open a compound statement in Objective-C as well, just in case this is
6072 as foreach expression. */
6073 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6074 cond = error_mark_node;
6075 incr = error_mark_node;
6076 matching_parens parens;
6077 if (parens.require_open (parser))
6079 /* Parse the initialization declaration or expression. */
6080 object_expression = error_mark_node;
6081 parser->objc_could_be_foreach_context = c_dialect_objc ();
6082 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6084 parser->objc_could_be_foreach_context = false;
6085 c_parser_consume_token (parser);
6086 c_finish_expr_stmt (loc, NULL_TREE);
6088 else if (c_parser_next_tokens_start_declaration (parser))
6090 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6091 &object_expression, vNULL);
6092 parser->objc_could_be_foreach_context = false;
6094 if (c_parser_next_token_is_keyword (parser, RID_IN))
6096 c_parser_consume_token (parser);
6097 is_foreach_statement = true;
6098 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6099 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6101 else
6102 check_for_loop_decls (for_loc, flag_isoc99);
6104 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6106 /* __extension__ can start a declaration, but is also an
6107 unary operator that can start an expression. Consume all
6108 but the last of a possible series of __extension__ to
6109 determine which. */
6110 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6111 && (c_parser_peek_2nd_token (parser)->keyword
6112 == RID_EXTENSION))
6113 c_parser_consume_token (parser);
6114 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6116 int ext;
6117 ext = disable_extension_diagnostics ();
6118 c_parser_consume_token (parser);
6119 c_parser_declaration_or_fndef (parser, true, true, true, true,
6120 true, &object_expression, vNULL);
6121 parser->objc_could_be_foreach_context = false;
6123 restore_extension_diagnostics (ext);
6124 if (c_parser_next_token_is_keyword (parser, RID_IN))
6126 c_parser_consume_token (parser);
6127 is_foreach_statement = true;
6128 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6129 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6131 else
6132 check_for_loop_decls (for_loc, flag_isoc99);
6134 else
6135 goto init_expr;
6137 else
6139 init_expr:
6141 struct c_expr ce;
6142 tree init_expression;
6143 ce = c_parser_expression (parser);
6144 init_expression = ce.value;
6145 parser->objc_could_be_foreach_context = false;
6146 if (c_parser_next_token_is_keyword (parser, RID_IN))
6148 c_parser_consume_token (parser);
6149 is_foreach_statement = true;
6150 if (! lvalue_p (init_expression))
6151 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6152 object_expression = c_fully_fold (init_expression, false, NULL);
6154 else
6156 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6157 init_expression = ce.value;
6158 c_finish_expr_stmt (loc, init_expression);
6159 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6163 /* Parse the loop condition. In the case of a foreach
6164 statement, there is no loop condition. */
6165 gcc_assert (!parser->objc_could_be_foreach_context);
6166 if (!is_foreach_statement)
6168 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6170 if (ivdep)
6172 c_parser_error (parser, "missing loop condition in loop with "
6173 "%<GCC ivdep%> pragma");
6174 cond = error_mark_node;
6176 else if (unroll)
6178 c_parser_error (parser, "missing loop condition in loop with "
6179 "%<GCC unroll%> pragma");
6180 cond = error_mark_node;
6182 else
6184 c_parser_consume_token (parser);
6185 cond = NULL_TREE;
6188 else
6190 cond = c_parser_condition (parser);
6191 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6192 "expected %<;%>");
6194 if (ivdep && cond != error_mark_node)
6195 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6196 build_int_cst (integer_type_node,
6197 annot_expr_ivdep_kind),
6198 integer_zero_node);
6199 if (unroll && cond != error_mark_node)
6200 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6201 build_int_cst (integer_type_node,
6202 annot_expr_unroll_kind),
6203 build_int_cst (integer_type_node, unroll));
6205 /* Parse the increment expression (the third expression in a
6206 for-statement). In the case of a foreach-statement, this is
6207 the expression that follows the 'in'. */
6208 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6210 if (is_foreach_statement)
6212 c_parser_error (parser, "missing collection in fast enumeration");
6213 collection_expression = error_mark_node;
6215 else
6216 incr = c_process_expr_stmt (loc, NULL_TREE);
6218 else
6220 if (is_foreach_statement)
6221 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6222 false, NULL);
6223 else
6225 struct c_expr ce = c_parser_expression (parser);
6226 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6227 incr = c_process_expr_stmt (loc, ce.value);
6230 parens.skip_until_found_close (parser);
6232 save_break = c_break_label;
6233 c_break_label = NULL_TREE;
6234 save_cont = c_cont_label;
6235 c_cont_label = NULL_TREE;
6237 token_indent_info body_tinfo
6238 = get_token_indent_info (c_parser_peek_token (parser));
6240 location_t loc_after_labels;
6241 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6242 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6244 if (is_foreach_statement)
6245 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6246 else
6247 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6248 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6249 c_parser_maybe_reclassify_token (parser);
6251 token_indent_info next_tinfo
6252 = get_token_indent_info (c_parser_peek_token (parser));
6253 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6255 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6256 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6257 for_tinfo.location, RID_FOR);
6259 c_break_label = save_break;
6260 c_cont_label = save_cont;
6263 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6264 statement with inputs, outputs, clobbers, and volatile tag
6265 allowed.
6267 asm-statement:
6268 asm type-qualifier[opt] ( asm-argument ) ;
6269 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6271 asm-argument:
6272 asm-string-literal
6273 asm-string-literal : asm-operands[opt]
6274 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6275 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6277 asm-goto-argument:
6278 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6279 : asm-goto-operands
6281 Qualifiers other than volatile are accepted in the syntax but
6282 warned for. */
6284 static tree
6285 c_parser_asm_statement (c_parser *parser)
6287 tree quals, str, outputs, inputs, clobbers, labels, ret;
6288 bool simple, is_goto;
6289 location_t asm_loc = c_parser_peek_token (parser)->location;
6290 int section, nsections;
6292 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6293 c_parser_consume_token (parser);
6294 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6296 quals = c_parser_peek_token (parser)->value;
6297 c_parser_consume_token (parser);
6299 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6300 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6302 warning_at (c_parser_peek_token (parser)->location,
6304 "%E qualifier ignored on asm",
6305 c_parser_peek_token (parser)->value);
6306 quals = NULL_TREE;
6307 c_parser_consume_token (parser);
6309 else
6310 quals = NULL_TREE;
6312 is_goto = false;
6313 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6315 c_parser_consume_token (parser);
6316 is_goto = true;
6319 /* ??? Follow the C++ parser rather than using the
6320 lex_untranslated_string kludge. */
6321 parser->lex_untranslated_string = true;
6322 ret = NULL;
6324 matching_parens parens;
6325 if (!parens.require_open (parser))
6326 goto error;
6328 str = c_parser_asm_string_literal (parser);
6329 if (str == NULL_TREE)
6330 goto error_close_paren;
6332 simple = true;
6333 outputs = NULL_TREE;
6334 inputs = NULL_TREE;
6335 clobbers = NULL_TREE;
6336 labels = NULL_TREE;
6338 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6339 goto done_asm;
6341 /* Parse each colon-delimited section of operands. */
6342 nsections = 3 + is_goto;
6343 for (section = 0; section < nsections; ++section)
6345 if (!c_parser_require (parser, CPP_COLON,
6346 is_goto
6347 ? G_("expected %<:%>")
6348 : G_("expected %<:%> or %<)%>"),
6349 UNKNOWN_LOCATION, is_goto))
6350 goto error_close_paren;
6352 /* Once past any colon, we're no longer a simple asm. */
6353 simple = false;
6355 if ((!c_parser_next_token_is (parser, CPP_COLON)
6356 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6357 || section == 3)
6358 switch (section)
6360 case 0:
6361 /* For asm goto, we don't allow output operands, but reserve
6362 the slot for a future extension that does allow them. */
6363 if (!is_goto)
6364 outputs = c_parser_asm_operands (parser);
6365 break;
6366 case 1:
6367 inputs = c_parser_asm_operands (parser);
6368 break;
6369 case 2:
6370 clobbers = c_parser_asm_clobbers (parser);
6371 break;
6372 case 3:
6373 labels = c_parser_asm_goto_operands (parser);
6374 break;
6375 default:
6376 gcc_unreachable ();
6379 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6380 goto done_asm;
6383 done_asm:
6384 if (!parens.require_close (parser))
6386 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6387 goto error;
6390 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6391 c_parser_skip_to_end_of_block_or_statement (parser);
6393 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6394 clobbers, labels, simple));
6396 error:
6397 parser->lex_untranslated_string = false;
6398 return ret;
6400 error_close_paren:
6401 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6402 goto error;
6405 /* Parse asm operands, a GNU extension.
6407 asm-operands:
6408 asm-operand
6409 asm-operands , asm-operand
6411 asm-operand:
6412 asm-string-literal ( expression )
6413 [ identifier ] asm-string-literal ( expression )
6416 static tree
6417 c_parser_asm_operands (c_parser *parser)
6419 tree list = NULL_TREE;
6420 while (true)
6422 tree name, str;
6423 struct c_expr expr;
6424 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6426 c_parser_consume_token (parser);
6427 if (c_parser_next_token_is (parser, CPP_NAME))
6429 tree id = c_parser_peek_token (parser)->value;
6430 c_parser_consume_token (parser);
6431 name = build_string (IDENTIFIER_LENGTH (id),
6432 IDENTIFIER_POINTER (id));
6434 else
6436 c_parser_error (parser, "expected identifier");
6437 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6438 return NULL_TREE;
6440 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6441 "expected %<]%>");
6443 else
6444 name = NULL_TREE;
6445 str = c_parser_asm_string_literal (parser);
6446 if (str == NULL_TREE)
6447 return NULL_TREE;
6448 parser->lex_untranslated_string = false;
6449 matching_parens parens;
6450 if (!parens.require_open (parser))
6452 parser->lex_untranslated_string = true;
6453 return NULL_TREE;
6455 expr = c_parser_expression (parser);
6456 mark_exp_read (expr.value);
6457 parser->lex_untranslated_string = true;
6458 if (!parens.require_close (parser))
6460 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6461 return NULL_TREE;
6463 list = chainon (list, build_tree_list (build_tree_list (name, str),
6464 expr.value));
6465 if (c_parser_next_token_is (parser, CPP_COMMA))
6466 c_parser_consume_token (parser);
6467 else
6468 break;
6470 return list;
6473 /* Parse asm clobbers, a GNU extension.
6475 asm-clobbers:
6476 asm-string-literal
6477 asm-clobbers , asm-string-literal
6480 static tree
6481 c_parser_asm_clobbers (c_parser *parser)
6483 tree list = NULL_TREE;
6484 while (true)
6486 tree str = c_parser_asm_string_literal (parser);
6487 if (str)
6488 list = tree_cons (NULL_TREE, str, list);
6489 else
6490 return NULL_TREE;
6491 if (c_parser_next_token_is (parser, CPP_COMMA))
6492 c_parser_consume_token (parser);
6493 else
6494 break;
6496 return list;
6499 /* Parse asm goto labels, a GNU extension.
6501 asm-goto-operands:
6502 identifier
6503 asm-goto-operands , identifier
6506 static tree
6507 c_parser_asm_goto_operands (c_parser *parser)
6509 tree list = NULL_TREE;
6510 while (true)
6512 tree name, label;
6514 if (c_parser_next_token_is (parser, CPP_NAME))
6516 c_token *tok = c_parser_peek_token (parser);
6517 name = tok->value;
6518 label = lookup_label_for_goto (tok->location, name);
6519 c_parser_consume_token (parser);
6520 TREE_USED (label) = 1;
6522 else
6524 c_parser_error (parser, "expected identifier");
6525 return NULL_TREE;
6528 name = build_string (IDENTIFIER_LENGTH (name),
6529 IDENTIFIER_POINTER (name));
6530 list = tree_cons (name, label, list);
6531 if (c_parser_next_token_is (parser, CPP_COMMA))
6532 c_parser_consume_token (parser);
6533 else
6534 return nreverse (list);
6538 /* Parse an expression other than a compound expression; that is, an
6539 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6540 AFTER is not NULL then it is an Objective-C message expression which
6541 is the primary-expression starting the expression as an initializer.
6543 assignment-expression:
6544 conditional-expression
6545 unary-expression assignment-operator assignment-expression
6547 assignment-operator: one of
6548 = *= /= %= += -= <<= >>= &= ^= |=
6550 In GNU C we accept any conditional expression on the LHS and
6551 diagnose the invalid lvalue rather than producing a syntax
6552 error. */
6554 static struct c_expr
6555 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6556 tree omp_atomic_lhs)
6558 struct c_expr lhs, rhs, ret;
6559 enum tree_code code;
6560 location_t op_location, exp_location;
6561 gcc_assert (!after || c_dialect_objc ());
6562 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6563 op_location = c_parser_peek_token (parser)->location;
6564 switch (c_parser_peek_token (parser)->type)
6566 case CPP_EQ:
6567 code = NOP_EXPR;
6568 break;
6569 case CPP_MULT_EQ:
6570 code = MULT_EXPR;
6571 break;
6572 case CPP_DIV_EQ:
6573 code = TRUNC_DIV_EXPR;
6574 break;
6575 case CPP_MOD_EQ:
6576 code = TRUNC_MOD_EXPR;
6577 break;
6578 case CPP_PLUS_EQ:
6579 code = PLUS_EXPR;
6580 break;
6581 case CPP_MINUS_EQ:
6582 code = MINUS_EXPR;
6583 break;
6584 case CPP_LSHIFT_EQ:
6585 code = LSHIFT_EXPR;
6586 break;
6587 case CPP_RSHIFT_EQ:
6588 code = RSHIFT_EXPR;
6589 break;
6590 case CPP_AND_EQ:
6591 code = BIT_AND_EXPR;
6592 break;
6593 case CPP_XOR_EQ:
6594 code = BIT_XOR_EXPR;
6595 break;
6596 case CPP_OR_EQ:
6597 code = BIT_IOR_EXPR;
6598 break;
6599 default:
6600 return lhs;
6602 c_parser_consume_token (parser);
6603 exp_location = c_parser_peek_token (parser)->location;
6604 rhs = c_parser_expr_no_commas (parser, NULL);
6605 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6607 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6608 code, exp_location, rhs.value,
6609 rhs.original_type);
6610 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6611 if (code == NOP_EXPR)
6612 ret.original_code = MODIFY_EXPR;
6613 else
6615 TREE_NO_WARNING (ret.value) = 1;
6616 ret.original_code = ERROR_MARK;
6618 ret.original_type = NULL;
6619 return ret;
6622 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6623 AFTER is not NULL then it is an Objective-C message expression which is
6624 the primary-expression starting the expression as an initializer.
6626 conditional-expression:
6627 logical-OR-expression
6628 logical-OR-expression ? expression : conditional-expression
6630 GNU extensions:
6632 conditional-expression:
6633 logical-OR-expression ? : conditional-expression
6636 static struct c_expr
6637 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6638 tree omp_atomic_lhs)
6640 struct c_expr cond, exp1, exp2, ret;
6641 location_t start, cond_loc, colon_loc;
6643 gcc_assert (!after || c_dialect_objc ());
6645 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6647 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6648 return cond;
6649 if (cond.value != error_mark_node)
6650 start = cond.get_start ();
6651 else
6652 start = UNKNOWN_LOCATION;
6653 cond_loc = c_parser_peek_token (parser)->location;
6654 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6655 c_parser_consume_token (parser);
6656 if (c_parser_next_token_is (parser, CPP_COLON))
6658 tree eptype = NULL_TREE;
6660 location_t middle_loc = c_parser_peek_token (parser)->location;
6661 pedwarn (middle_loc, OPT_Wpedantic,
6662 "ISO C forbids omitting the middle term of a ?: expression");
6663 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6665 eptype = TREE_TYPE (cond.value);
6666 cond.value = TREE_OPERAND (cond.value, 0);
6668 tree e = cond.value;
6669 while (TREE_CODE (e) == COMPOUND_EXPR)
6670 e = TREE_OPERAND (e, 1);
6671 warn_for_omitted_condop (middle_loc, e);
6672 /* Make sure first operand is calculated only once. */
6673 exp1.value = save_expr (default_conversion (cond.value));
6674 if (eptype)
6675 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6676 exp1.original_type = NULL;
6677 exp1.src_range = cond.src_range;
6678 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6679 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6681 else
6683 cond.value
6684 = c_objc_common_truthvalue_conversion
6685 (cond_loc, default_conversion (cond.value));
6686 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6687 exp1 = c_parser_expression_conv (parser);
6688 mark_exp_read (exp1.value);
6689 c_inhibit_evaluation_warnings +=
6690 ((cond.value == truthvalue_true_node)
6691 - (cond.value == truthvalue_false_node));
6694 colon_loc = c_parser_peek_token (parser)->location;
6695 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6697 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6698 ret.set_error ();
6699 ret.original_code = ERROR_MARK;
6700 ret.original_type = NULL;
6701 return ret;
6704 location_t exp2_loc = c_parser_peek_token (parser)->location;
6705 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6706 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6708 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6709 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6710 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6711 ret.value = build_conditional_expr (colon_loc, cond.value,
6712 cond.original_code == C_MAYBE_CONST_EXPR,
6713 exp1.value, exp1.original_type, loc1,
6714 exp2.value, exp2.original_type, loc2);
6715 ret.original_code = ERROR_MARK;
6716 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6717 ret.original_type = NULL;
6718 else
6720 tree t1, t2;
6722 /* If both sides are enum type, the default conversion will have
6723 made the type of the result be an integer type. We want to
6724 remember the enum types we started with. */
6725 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6726 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6727 ret.original_type = ((t1 != error_mark_node
6728 && t2 != error_mark_node
6729 && (TYPE_MAIN_VARIANT (t1)
6730 == TYPE_MAIN_VARIANT (t2)))
6731 ? t1
6732 : NULL);
6734 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6735 return ret;
6738 /* Parse a binary expression; that is, a logical-OR-expression (C90
6739 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6740 NULL then it is an Objective-C message expression which is the
6741 primary-expression starting the expression as an initializer.
6743 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6744 when it should be the unfolded lhs. In a valid OpenMP source,
6745 one of the operands of the toplevel binary expression must be equal
6746 to it. In that case, just return a build2 created binary operation
6747 rather than result of parser_build_binary_op.
6749 multiplicative-expression:
6750 cast-expression
6751 multiplicative-expression * cast-expression
6752 multiplicative-expression / cast-expression
6753 multiplicative-expression % cast-expression
6755 additive-expression:
6756 multiplicative-expression
6757 additive-expression + multiplicative-expression
6758 additive-expression - multiplicative-expression
6760 shift-expression:
6761 additive-expression
6762 shift-expression << additive-expression
6763 shift-expression >> additive-expression
6765 relational-expression:
6766 shift-expression
6767 relational-expression < shift-expression
6768 relational-expression > shift-expression
6769 relational-expression <= shift-expression
6770 relational-expression >= shift-expression
6772 equality-expression:
6773 relational-expression
6774 equality-expression == relational-expression
6775 equality-expression != relational-expression
6777 AND-expression:
6778 equality-expression
6779 AND-expression & equality-expression
6781 exclusive-OR-expression:
6782 AND-expression
6783 exclusive-OR-expression ^ AND-expression
6785 inclusive-OR-expression:
6786 exclusive-OR-expression
6787 inclusive-OR-expression | exclusive-OR-expression
6789 logical-AND-expression:
6790 inclusive-OR-expression
6791 logical-AND-expression && inclusive-OR-expression
6793 logical-OR-expression:
6794 logical-AND-expression
6795 logical-OR-expression || logical-AND-expression
6798 static struct c_expr
6799 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6800 tree omp_atomic_lhs)
6802 /* A binary expression is parsed using operator-precedence parsing,
6803 with the operands being cast expressions. All the binary
6804 operators are left-associative. Thus a binary expression is of
6805 form:
6807 E0 op1 E1 op2 E2 ...
6809 which we represent on a stack. On the stack, the precedence
6810 levels are strictly increasing. When a new operator is
6811 encountered of higher precedence than that at the top of the
6812 stack, it is pushed; its LHS is the top expression, and its RHS
6813 is everything parsed until it is popped. When a new operator is
6814 encountered with precedence less than or equal to that at the top
6815 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6816 by the result of the operation until the operator at the top of
6817 the stack has lower precedence than the new operator or there is
6818 only one element on the stack; then the top expression is the LHS
6819 of the new operator. In the case of logical AND and OR
6820 expressions, we also need to adjust c_inhibit_evaluation_warnings
6821 as appropriate when the operators are pushed and popped. */
6823 struct {
6824 /* The expression at this stack level. */
6825 struct c_expr expr;
6826 /* The precedence of the operator on its left, PREC_NONE at the
6827 bottom of the stack. */
6828 enum c_parser_prec prec;
6829 /* The operation on its left. */
6830 enum tree_code op;
6831 /* The source location of this operation. */
6832 location_t loc;
6833 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6834 tree sizeof_arg;
6835 } stack[NUM_PRECS];
6836 int sp;
6837 /* Location of the binary operator. */
6838 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6839 #define POP \
6840 do { \
6841 switch (stack[sp].op) \
6843 case TRUTH_ANDIF_EXPR: \
6844 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6845 == truthvalue_false_node); \
6846 break; \
6847 case TRUTH_ORIF_EXPR: \
6848 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6849 == truthvalue_true_node); \
6850 break; \
6851 case TRUNC_DIV_EXPR: \
6852 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6853 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6855 tree type0 = stack[sp - 1].sizeof_arg; \
6856 tree type1 = stack[sp].sizeof_arg; \
6857 tree first_arg = type0; \
6858 if (!TYPE_P (type0)) \
6859 type0 = TREE_TYPE (type0); \
6860 if (!TYPE_P (type1)) \
6861 type1 = TREE_TYPE (type1); \
6862 if (POINTER_TYPE_P (type0) \
6863 && comptypes (TREE_TYPE (type0), type1) \
6864 && !(TREE_CODE (first_arg) == PARM_DECL \
6865 && C_ARRAY_PARAMETER (first_arg) \
6866 && warn_sizeof_array_argument)) \
6867 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6868 "division %<sizeof (%T) / sizeof (%T)%> does " \
6869 "not compute the number of array elements", \
6870 type0, type1)) \
6871 if (DECL_P (first_arg)) \
6872 inform (DECL_SOURCE_LOCATION (first_arg), \
6873 "first %<sizeof%> operand was declared here"); \
6875 break; \
6876 default: \
6877 break; \
6879 stack[sp - 1].expr \
6880 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6881 stack[sp - 1].expr, true, true); \
6882 stack[sp].expr \
6883 = convert_lvalue_to_rvalue (stack[sp].loc, \
6884 stack[sp].expr, true, true); \
6885 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6886 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6887 && ((1 << stack[sp].prec) \
6888 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6889 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6890 && stack[sp].op != TRUNC_MOD_EXPR \
6891 && stack[0].expr.value != error_mark_node \
6892 && stack[1].expr.value != error_mark_node \
6893 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6894 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6895 stack[0].expr.value \
6896 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6897 stack[0].expr.value, stack[1].expr.value); \
6898 else \
6899 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6900 stack[sp].op, \
6901 stack[sp - 1].expr, \
6902 stack[sp].expr); \
6903 sp--; \
6904 } while (0)
6905 gcc_assert (!after || c_dialect_objc ());
6906 stack[0].loc = c_parser_peek_token (parser)->location;
6907 stack[0].expr = c_parser_cast_expression (parser, after);
6908 stack[0].prec = PREC_NONE;
6909 stack[0].sizeof_arg = c_last_sizeof_arg;
6910 sp = 0;
6911 while (true)
6913 enum c_parser_prec oprec;
6914 enum tree_code ocode;
6915 source_range src_range;
6916 if (parser->error)
6917 goto out;
6918 switch (c_parser_peek_token (parser)->type)
6920 case CPP_MULT:
6921 oprec = PREC_MULT;
6922 ocode = MULT_EXPR;
6923 break;
6924 case CPP_DIV:
6925 oprec = PREC_MULT;
6926 ocode = TRUNC_DIV_EXPR;
6927 break;
6928 case CPP_MOD:
6929 oprec = PREC_MULT;
6930 ocode = TRUNC_MOD_EXPR;
6931 break;
6932 case CPP_PLUS:
6933 oprec = PREC_ADD;
6934 ocode = PLUS_EXPR;
6935 break;
6936 case CPP_MINUS:
6937 oprec = PREC_ADD;
6938 ocode = MINUS_EXPR;
6939 break;
6940 case CPP_LSHIFT:
6941 oprec = PREC_SHIFT;
6942 ocode = LSHIFT_EXPR;
6943 break;
6944 case CPP_RSHIFT:
6945 oprec = PREC_SHIFT;
6946 ocode = RSHIFT_EXPR;
6947 break;
6948 case CPP_LESS:
6949 oprec = PREC_REL;
6950 ocode = LT_EXPR;
6951 break;
6952 case CPP_GREATER:
6953 oprec = PREC_REL;
6954 ocode = GT_EXPR;
6955 break;
6956 case CPP_LESS_EQ:
6957 oprec = PREC_REL;
6958 ocode = LE_EXPR;
6959 break;
6960 case CPP_GREATER_EQ:
6961 oprec = PREC_REL;
6962 ocode = GE_EXPR;
6963 break;
6964 case CPP_EQ_EQ:
6965 oprec = PREC_EQ;
6966 ocode = EQ_EXPR;
6967 break;
6968 case CPP_NOT_EQ:
6969 oprec = PREC_EQ;
6970 ocode = NE_EXPR;
6971 break;
6972 case CPP_AND:
6973 oprec = PREC_BITAND;
6974 ocode = BIT_AND_EXPR;
6975 break;
6976 case CPP_XOR:
6977 oprec = PREC_BITXOR;
6978 ocode = BIT_XOR_EXPR;
6979 break;
6980 case CPP_OR:
6981 oprec = PREC_BITOR;
6982 ocode = BIT_IOR_EXPR;
6983 break;
6984 case CPP_AND_AND:
6985 oprec = PREC_LOGAND;
6986 ocode = TRUTH_ANDIF_EXPR;
6987 break;
6988 case CPP_OR_OR:
6989 oprec = PREC_LOGOR;
6990 ocode = TRUTH_ORIF_EXPR;
6991 break;
6992 default:
6993 /* Not a binary operator, so end of the binary
6994 expression. */
6995 goto out;
6997 binary_loc = c_parser_peek_token (parser)->location;
6998 while (oprec <= stack[sp].prec)
6999 POP;
7000 c_parser_consume_token (parser);
7001 switch (ocode)
7003 case TRUTH_ANDIF_EXPR:
7004 src_range = stack[sp].expr.src_range;
7005 stack[sp].expr
7006 = convert_lvalue_to_rvalue (stack[sp].loc,
7007 stack[sp].expr, true, true);
7008 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7009 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7010 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7011 == truthvalue_false_node);
7012 set_c_expr_source_range (&stack[sp].expr, src_range);
7013 break;
7014 case TRUTH_ORIF_EXPR:
7015 src_range = stack[sp].expr.src_range;
7016 stack[sp].expr
7017 = convert_lvalue_to_rvalue (stack[sp].loc,
7018 stack[sp].expr, true, true);
7019 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7020 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7021 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7022 == truthvalue_true_node);
7023 set_c_expr_source_range (&stack[sp].expr, src_range);
7024 break;
7025 default:
7026 break;
7028 sp++;
7029 stack[sp].loc = binary_loc;
7030 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7031 stack[sp].prec = oprec;
7032 stack[sp].op = ocode;
7033 stack[sp].sizeof_arg = c_last_sizeof_arg;
7035 out:
7036 while (sp > 0)
7037 POP;
7038 return stack[0].expr;
7039 #undef POP
7042 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7043 is not NULL then it is an Objective-C message expression which is the
7044 primary-expression starting the expression as an initializer.
7046 cast-expression:
7047 unary-expression
7048 ( type-name ) unary-expression
7051 static struct c_expr
7052 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7054 location_t cast_loc = c_parser_peek_token (parser)->location;
7055 gcc_assert (!after || c_dialect_objc ());
7056 if (after)
7057 return c_parser_postfix_expression_after_primary (parser,
7058 cast_loc, *after);
7059 /* If the expression begins with a parenthesized type name, it may
7060 be either a cast or a compound literal; we need to see whether
7061 the next character is '{' to tell the difference. If not, it is
7062 an unary expression. Full detection of unknown typenames here
7063 would require a 3-token lookahead. */
7064 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7065 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7067 struct c_type_name *type_name;
7068 struct c_expr ret;
7069 struct c_expr expr;
7070 matching_parens parens;
7071 parens.consume_open (parser);
7072 type_name = c_parser_type_name (parser, true);
7073 parens.skip_until_found_close (parser);
7074 if (type_name == NULL)
7076 ret.set_error ();
7077 ret.original_code = ERROR_MARK;
7078 ret.original_type = NULL;
7079 return ret;
7082 /* Save casted types in the function's used types hash table. */
7083 used_types_insert (type_name->specs->type);
7085 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7086 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7087 cast_loc);
7088 if (type_name->specs->alignas_p)
7089 error_at (type_name->specs->locations[cdw_alignas],
7090 "alignment specified for type name in cast");
7092 location_t expr_loc = c_parser_peek_token (parser)->location;
7093 expr = c_parser_cast_expression (parser, NULL);
7094 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7096 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7097 if (ret.value && expr.value)
7098 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7099 ret.original_code = ERROR_MARK;
7100 ret.original_type = NULL;
7101 return ret;
7103 else
7104 return c_parser_unary_expression (parser);
7107 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7109 unary-expression:
7110 postfix-expression
7111 ++ unary-expression
7112 -- unary-expression
7113 unary-operator cast-expression
7114 sizeof unary-expression
7115 sizeof ( type-name )
7117 unary-operator: one of
7118 & * + - ~ !
7120 GNU extensions:
7122 unary-expression:
7123 __alignof__ unary-expression
7124 __alignof__ ( type-name )
7125 && identifier
7127 (C11 permits _Alignof with type names only.)
7129 unary-operator: one of
7130 __extension__ __real__ __imag__
7132 Transactional Memory:
7134 unary-expression:
7135 transaction-expression
7137 In addition, the GNU syntax treats ++ and -- as unary operators, so
7138 they may be applied to cast expressions with errors for non-lvalues
7139 given later. */
7141 static struct c_expr
7142 c_parser_unary_expression (c_parser *parser)
7144 int ext;
7145 struct c_expr ret, op;
7146 location_t op_loc = c_parser_peek_token (parser)->location;
7147 location_t exp_loc;
7148 location_t finish;
7149 ret.original_code = ERROR_MARK;
7150 ret.original_type = NULL;
7151 switch (c_parser_peek_token (parser)->type)
7153 case CPP_PLUS_PLUS:
7154 c_parser_consume_token (parser);
7155 exp_loc = c_parser_peek_token (parser)->location;
7156 op = c_parser_cast_expression (parser, NULL);
7158 op = default_function_array_read_conversion (exp_loc, op);
7159 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7160 case CPP_MINUS_MINUS:
7161 c_parser_consume_token (parser);
7162 exp_loc = c_parser_peek_token (parser)->location;
7163 op = c_parser_cast_expression (parser, NULL);
7165 op = default_function_array_read_conversion (exp_loc, op);
7166 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7167 case CPP_AND:
7168 c_parser_consume_token (parser);
7169 op = c_parser_cast_expression (parser, NULL);
7170 mark_exp_read (op.value);
7171 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7172 case CPP_MULT:
7174 c_parser_consume_token (parser);
7175 exp_loc = c_parser_peek_token (parser)->location;
7176 op = c_parser_cast_expression (parser, NULL);
7177 finish = op.get_finish ();
7178 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7179 location_t combined_loc = make_location (op_loc, op_loc, finish);
7180 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7181 ret.src_range.m_start = op_loc;
7182 ret.src_range.m_finish = finish;
7183 return ret;
7185 case CPP_PLUS:
7186 if (!c_dialect_objc () && !in_system_header_at (input_location))
7187 warning_at (op_loc,
7188 OPT_Wtraditional,
7189 "traditional C rejects the unary plus operator");
7190 c_parser_consume_token (parser);
7191 exp_loc = c_parser_peek_token (parser)->location;
7192 op = c_parser_cast_expression (parser, NULL);
7193 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7194 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7195 case CPP_MINUS:
7196 c_parser_consume_token (parser);
7197 exp_loc = c_parser_peek_token (parser)->location;
7198 op = c_parser_cast_expression (parser, NULL);
7199 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7200 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7201 case CPP_COMPL:
7202 c_parser_consume_token (parser);
7203 exp_loc = c_parser_peek_token (parser)->location;
7204 op = c_parser_cast_expression (parser, NULL);
7205 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7206 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7207 case CPP_NOT:
7208 c_parser_consume_token (parser);
7209 exp_loc = c_parser_peek_token (parser)->location;
7210 op = c_parser_cast_expression (parser, NULL);
7211 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7212 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7213 case CPP_AND_AND:
7214 /* Refer to the address of a label as a pointer. */
7215 c_parser_consume_token (parser);
7216 if (c_parser_next_token_is (parser, CPP_NAME))
7218 ret.value = finish_label_address_expr
7219 (c_parser_peek_token (parser)->value, op_loc);
7220 set_c_expr_source_range (&ret, op_loc,
7221 c_parser_peek_token (parser)->get_finish ());
7222 c_parser_consume_token (parser);
7224 else
7226 c_parser_error (parser, "expected identifier");
7227 ret.set_error ();
7229 return ret;
7230 case CPP_KEYWORD:
7231 switch (c_parser_peek_token (parser)->keyword)
7233 case RID_SIZEOF:
7234 return c_parser_sizeof_expression (parser);
7235 case RID_ALIGNOF:
7236 return c_parser_alignof_expression (parser);
7237 case RID_EXTENSION:
7238 c_parser_consume_token (parser);
7239 ext = disable_extension_diagnostics ();
7240 ret = c_parser_cast_expression (parser, NULL);
7241 restore_extension_diagnostics (ext);
7242 return ret;
7243 case RID_REALPART:
7244 c_parser_consume_token (parser);
7245 exp_loc = c_parser_peek_token (parser)->location;
7246 op = c_parser_cast_expression (parser, NULL);
7247 op = default_function_array_conversion (exp_loc, op);
7248 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7249 case RID_IMAGPART:
7250 c_parser_consume_token (parser);
7251 exp_loc = c_parser_peek_token (parser)->location;
7252 op = c_parser_cast_expression (parser, NULL);
7253 op = default_function_array_conversion (exp_loc, op);
7254 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7255 case RID_TRANSACTION_ATOMIC:
7256 case RID_TRANSACTION_RELAXED:
7257 return c_parser_transaction_expression (parser,
7258 c_parser_peek_token (parser)->keyword);
7259 default:
7260 return c_parser_postfix_expression (parser);
7262 default:
7263 return c_parser_postfix_expression (parser);
7267 /* Parse a sizeof expression. */
7269 static struct c_expr
7270 c_parser_sizeof_expression (c_parser *parser)
7272 struct c_expr expr;
7273 struct c_expr result;
7274 location_t expr_loc;
7275 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7277 location_t start;
7278 location_t finish = UNKNOWN_LOCATION;
7280 start = c_parser_peek_token (parser)->location;
7282 c_parser_consume_token (parser);
7283 c_inhibit_evaluation_warnings++;
7284 in_sizeof++;
7285 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7286 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7288 /* Either sizeof ( type-name ) or sizeof unary-expression
7289 starting with a compound literal. */
7290 struct c_type_name *type_name;
7291 matching_parens parens;
7292 parens.consume_open (parser);
7293 expr_loc = c_parser_peek_token (parser)->location;
7294 type_name = c_parser_type_name (parser, true);
7295 parens.skip_until_found_close (parser);
7296 finish = parser->tokens_buf[0].location;
7297 if (type_name == NULL)
7299 struct c_expr ret;
7300 c_inhibit_evaluation_warnings--;
7301 in_sizeof--;
7302 ret.set_error ();
7303 ret.original_code = ERROR_MARK;
7304 ret.original_type = NULL;
7305 return ret;
7307 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7309 expr = c_parser_postfix_expression_after_paren_type (parser,
7310 type_name,
7311 expr_loc);
7312 finish = expr.get_finish ();
7313 goto sizeof_expr;
7315 /* sizeof ( type-name ). */
7316 if (type_name->specs->alignas_p)
7317 error_at (type_name->specs->locations[cdw_alignas],
7318 "alignment specified for type name in %<sizeof%>");
7319 c_inhibit_evaluation_warnings--;
7320 in_sizeof--;
7321 result = c_expr_sizeof_type (expr_loc, type_name);
7323 else
7325 expr_loc = c_parser_peek_token (parser)->location;
7326 expr = c_parser_unary_expression (parser);
7327 finish = expr.get_finish ();
7328 sizeof_expr:
7329 c_inhibit_evaluation_warnings--;
7330 in_sizeof--;
7331 mark_exp_read (expr.value);
7332 if (TREE_CODE (expr.value) == COMPONENT_REF
7333 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7334 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7335 result = c_expr_sizeof_expr (expr_loc, expr);
7337 if (finish != UNKNOWN_LOCATION)
7338 set_c_expr_source_range (&result, start, finish);
7339 return result;
7342 /* Parse an alignof expression. */
7344 static struct c_expr
7345 c_parser_alignof_expression (c_parser *parser)
7347 struct c_expr expr;
7348 location_t start_loc = c_parser_peek_token (parser)->location;
7349 location_t end_loc;
7350 tree alignof_spelling = c_parser_peek_token (parser)->value;
7351 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7352 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7353 "_Alignof") == 0;
7354 /* A diagnostic is not required for the use of this identifier in
7355 the implementation namespace; only diagnose it for the C11
7356 spelling because of existing code using the other spellings. */
7357 if (is_c11_alignof)
7359 if (flag_isoc99)
7360 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7361 alignof_spelling);
7362 else
7363 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7364 alignof_spelling);
7366 c_parser_consume_token (parser);
7367 c_inhibit_evaluation_warnings++;
7368 in_alignof++;
7369 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7370 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7372 /* Either __alignof__ ( type-name ) or __alignof__
7373 unary-expression starting with a compound literal. */
7374 location_t loc;
7375 struct c_type_name *type_name;
7376 struct c_expr ret;
7377 matching_parens parens;
7378 parens.consume_open (parser);
7379 loc = c_parser_peek_token (parser)->location;
7380 type_name = c_parser_type_name (parser, true);
7381 end_loc = c_parser_peek_token (parser)->location;
7382 parens.skip_until_found_close (parser);
7383 if (type_name == NULL)
7385 struct c_expr ret;
7386 c_inhibit_evaluation_warnings--;
7387 in_alignof--;
7388 ret.set_error ();
7389 ret.original_code = ERROR_MARK;
7390 ret.original_type = NULL;
7391 return ret;
7393 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7395 expr = c_parser_postfix_expression_after_paren_type (parser,
7396 type_name,
7397 loc);
7398 goto alignof_expr;
7400 /* alignof ( type-name ). */
7401 if (type_name->specs->alignas_p)
7402 error_at (type_name->specs->locations[cdw_alignas],
7403 "alignment specified for type name in %qE",
7404 alignof_spelling);
7405 c_inhibit_evaluation_warnings--;
7406 in_alignof--;
7407 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7408 NULL, NULL),
7409 false, is_c11_alignof, 1);
7410 ret.original_code = ERROR_MARK;
7411 ret.original_type = NULL;
7412 set_c_expr_source_range (&ret, start_loc, end_loc);
7413 return ret;
7415 else
7417 struct c_expr ret;
7418 expr = c_parser_unary_expression (parser);
7419 end_loc = expr.src_range.m_finish;
7420 alignof_expr:
7421 mark_exp_read (expr.value);
7422 c_inhibit_evaluation_warnings--;
7423 in_alignof--;
7424 if (is_c11_alignof)
7425 pedwarn (start_loc,
7426 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7427 alignof_spelling);
7428 ret.value = c_alignof_expr (start_loc, expr.value);
7429 ret.original_code = ERROR_MARK;
7430 ret.original_type = NULL;
7431 set_c_expr_source_range (&ret, start_loc, end_loc);
7432 return ret;
7436 /* Helper function to read arguments of builtins which are interfaces
7437 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7438 others. The name of the builtin is passed using BNAME parameter.
7439 Function returns true if there were no errors while parsing and
7440 stores the arguments in CEXPR_LIST. If it returns true,
7441 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7442 parenthesis. */
7443 static bool
7444 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7445 vec<c_expr_t, va_gc> **ret_cexpr_list,
7446 bool choose_expr_p,
7447 location_t *out_close_paren_loc)
7449 location_t loc = c_parser_peek_token (parser)->location;
7450 vec<c_expr_t, va_gc> *cexpr_list;
7451 c_expr_t expr;
7452 bool saved_force_folding_builtin_constant_p;
7454 *ret_cexpr_list = NULL;
7455 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7457 error_at (loc, "cannot take address of %qs", bname);
7458 return false;
7461 c_parser_consume_token (parser);
7463 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7465 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7466 c_parser_consume_token (parser);
7467 return true;
7470 saved_force_folding_builtin_constant_p
7471 = force_folding_builtin_constant_p;
7472 force_folding_builtin_constant_p |= choose_expr_p;
7473 expr = c_parser_expr_no_commas (parser, NULL);
7474 force_folding_builtin_constant_p
7475 = saved_force_folding_builtin_constant_p;
7476 vec_alloc (cexpr_list, 1);
7477 vec_safe_push (cexpr_list, expr);
7478 while (c_parser_next_token_is (parser, CPP_COMMA))
7480 c_parser_consume_token (parser);
7481 expr = c_parser_expr_no_commas (parser, NULL);
7482 vec_safe_push (cexpr_list, expr);
7485 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7486 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7487 return false;
7489 *ret_cexpr_list = cexpr_list;
7490 return true;
7493 /* This represents a single generic-association. */
7495 struct c_generic_association
7497 /* The location of the starting token of the type. */
7498 location_t type_location;
7499 /* The association's type, or NULL_TREE for 'default'. */
7500 tree type;
7501 /* The association's expression. */
7502 struct c_expr expression;
7505 /* Parse a generic-selection. (C11 6.5.1.1).
7507 generic-selection:
7508 _Generic ( assignment-expression , generic-assoc-list )
7510 generic-assoc-list:
7511 generic-association
7512 generic-assoc-list , generic-association
7514 generic-association:
7515 type-name : assignment-expression
7516 default : assignment-expression
7519 static struct c_expr
7520 c_parser_generic_selection (c_parser *parser)
7522 struct c_expr selector, error_expr;
7523 tree selector_type;
7524 struct c_generic_association matched_assoc;
7525 bool match_found = false;
7526 location_t generic_loc, selector_loc;
7528 error_expr.original_code = ERROR_MARK;
7529 error_expr.original_type = NULL;
7530 error_expr.set_error ();
7531 matched_assoc.type_location = UNKNOWN_LOCATION;
7532 matched_assoc.type = NULL_TREE;
7533 matched_assoc.expression = error_expr;
7535 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7536 generic_loc = c_parser_peek_token (parser)->location;
7537 c_parser_consume_token (parser);
7538 if (flag_isoc99)
7539 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7540 "ISO C99 does not support %<_Generic%>");
7541 else
7542 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7543 "ISO C90 does not support %<_Generic%>");
7545 matching_parens parens;
7546 if (!parens.require_open (parser))
7547 return error_expr;
7549 c_inhibit_evaluation_warnings++;
7550 selector_loc = c_parser_peek_token (parser)->location;
7551 selector = c_parser_expr_no_commas (parser, NULL);
7552 selector = default_function_array_conversion (selector_loc, selector);
7553 c_inhibit_evaluation_warnings--;
7555 if (selector.value == error_mark_node)
7557 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7558 return selector;
7560 selector_type = TREE_TYPE (selector.value);
7561 /* In ISO C terms, rvalues (including the controlling expression of
7562 _Generic) do not have qualified types. */
7563 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7564 selector_type = TYPE_MAIN_VARIANT (selector_type);
7565 /* In ISO C terms, _Noreturn is not part of the type of expressions
7566 such as &abort, but in GCC it is represented internally as a type
7567 qualifier. */
7568 if (FUNCTION_POINTER_TYPE_P (selector_type)
7569 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7570 selector_type
7571 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7573 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7576 return error_expr;
7579 auto_vec<c_generic_association> associations;
7580 while (1)
7582 struct c_generic_association assoc, *iter;
7583 unsigned int ix;
7584 c_token *token = c_parser_peek_token (parser);
7586 assoc.type_location = token->location;
7587 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7589 c_parser_consume_token (parser);
7590 assoc.type = NULL_TREE;
7592 else
7594 struct c_type_name *type_name;
7596 type_name = c_parser_type_name (parser);
7597 if (type_name == NULL)
7599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7600 return error_expr;
7602 assoc.type = groktypename (type_name, NULL, NULL);
7603 if (assoc.type == error_mark_node)
7605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7606 return error_expr;
7609 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7610 error_at (assoc.type_location,
7611 "%<_Generic%> association has function type");
7612 else if (!COMPLETE_TYPE_P (assoc.type))
7613 error_at (assoc.type_location,
7614 "%<_Generic%> association has incomplete type");
7616 if (variably_modified_type_p (assoc.type, NULL_TREE))
7617 error_at (assoc.type_location,
7618 "%<_Generic%> association has "
7619 "variable length type");
7622 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7624 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7625 return error_expr;
7628 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7629 if (assoc.expression.value == error_mark_node)
7631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7632 return error_expr;
7635 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7637 if (assoc.type == NULL_TREE)
7639 if (iter->type == NULL_TREE)
7641 error_at (assoc.type_location,
7642 "duplicate %<default%> case in %<_Generic%>");
7643 inform (iter->type_location, "original %<default%> is here");
7646 else if (iter->type != NULL_TREE)
7648 if (comptypes (assoc.type, iter->type))
7650 error_at (assoc.type_location,
7651 "%<_Generic%> specifies two compatible types");
7652 inform (iter->type_location, "compatible type is here");
7657 if (assoc.type == NULL_TREE)
7659 if (!match_found)
7661 matched_assoc = assoc;
7662 match_found = true;
7665 else if (comptypes (assoc.type, selector_type))
7667 if (!match_found || matched_assoc.type == NULL_TREE)
7669 matched_assoc = assoc;
7670 match_found = true;
7672 else
7674 error_at (assoc.type_location,
7675 "%<_Generic%> selector matches multiple associations");
7676 inform (matched_assoc.type_location,
7677 "other match is here");
7681 associations.safe_push (assoc);
7683 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7684 break;
7685 c_parser_consume_token (parser);
7688 if (!parens.require_close (parser))
7690 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7691 return error_expr;
7694 if (!match_found)
7696 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7697 "compatible with any association",
7698 selector_type);
7699 return error_expr;
7702 return matched_assoc.expression;
7705 /* Check the validity of a function pointer argument *EXPR (argument
7706 position POS) to __builtin_tgmath. Return the number of function
7707 arguments if possibly valid; return 0 having reported an error if
7708 not valid. */
7710 static unsigned int
7711 check_tgmath_function (c_expr *expr, unsigned int pos)
7713 tree type = TREE_TYPE (expr->value);
7714 if (!FUNCTION_POINTER_TYPE_P (type))
7716 error_at (expr->get_location (),
7717 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7718 pos);
7719 return 0;
7721 type = TREE_TYPE (type);
7722 if (!prototype_p (type))
7724 error_at (expr->get_location (),
7725 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7726 return 0;
7728 if (stdarg_p (type))
7730 error_at (expr->get_location (),
7731 "argument %u of %<__builtin_tgmath%> has variable arguments",
7732 pos);
7733 return 0;
7735 unsigned int nargs = 0;
7736 function_args_iterator iter;
7737 tree t;
7738 FOREACH_FUNCTION_ARGS (type, t, iter)
7740 if (t == void_type_node)
7741 break;
7742 nargs++;
7744 if (nargs == 0)
7746 error_at (expr->get_location (),
7747 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7748 return 0;
7750 return nargs;
7753 /* Ways in which a parameter or return value of a type-generic macro
7754 may vary between the different functions the macro may call. */
7755 enum tgmath_parm_kind
7757 tgmath_fixed, tgmath_real, tgmath_complex
7760 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7761 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7762 call c_parser_postfix_expression_after_paren_type on encountering them.
7764 postfix-expression:
7765 primary-expression
7766 postfix-expression [ expression ]
7767 postfix-expression ( argument-expression-list[opt] )
7768 postfix-expression . identifier
7769 postfix-expression -> identifier
7770 postfix-expression ++
7771 postfix-expression --
7772 ( type-name ) { initializer-list }
7773 ( type-name ) { initializer-list , }
7775 argument-expression-list:
7776 argument-expression
7777 argument-expression-list , argument-expression
7779 primary-expression:
7780 identifier
7781 constant
7782 string-literal
7783 ( expression )
7784 generic-selection
7786 GNU extensions:
7788 primary-expression:
7789 __func__
7790 (treated as a keyword in GNU C)
7791 __FUNCTION__
7792 __PRETTY_FUNCTION__
7793 ( compound-statement )
7794 __builtin_va_arg ( assignment-expression , type-name )
7795 __builtin_offsetof ( type-name , offsetof-member-designator )
7796 __builtin_choose_expr ( assignment-expression ,
7797 assignment-expression ,
7798 assignment-expression )
7799 __builtin_types_compatible_p ( type-name , type-name )
7800 __builtin_tgmath ( expr-list )
7801 __builtin_complex ( assignment-expression , assignment-expression )
7802 __builtin_shuffle ( assignment-expression , assignment-expression )
7803 __builtin_shuffle ( assignment-expression ,
7804 assignment-expression ,
7805 assignment-expression, )
7807 offsetof-member-designator:
7808 identifier
7809 offsetof-member-designator . identifier
7810 offsetof-member-designator [ expression ]
7812 Objective-C:
7814 primary-expression:
7815 [ objc-receiver objc-message-args ]
7816 @selector ( objc-selector-arg )
7817 @protocol ( identifier )
7818 @encode ( type-name )
7819 objc-string-literal
7820 Classname . identifier
7823 static struct c_expr
7824 c_parser_postfix_expression (c_parser *parser)
7826 struct c_expr expr, e1;
7827 struct c_type_name *t1, *t2;
7828 location_t loc = c_parser_peek_token (parser)->location;
7829 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7830 expr.original_code = ERROR_MARK;
7831 expr.original_type = NULL;
7832 switch (c_parser_peek_token (parser)->type)
7834 case CPP_NUMBER:
7835 expr.value = c_parser_peek_token (parser)->value;
7836 set_c_expr_source_range (&expr, tok_range);
7837 loc = c_parser_peek_token (parser)->location;
7838 c_parser_consume_token (parser);
7839 if (TREE_CODE (expr.value) == FIXED_CST
7840 && !targetm.fixed_point_supported_p ())
7842 error_at (loc, "fixed-point types not supported for this target");
7843 expr.set_error ();
7845 break;
7846 case CPP_CHAR:
7847 case CPP_CHAR16:
7848 case CPP_CHAR32:
7849 case CPP_WCHAR:
7850 expr.value = c_parser_peek_token (parser)->value;
7851 /* For the purpose of warning when a pointer is compared with
7852 a zero character constant. */
7853 expr.original_type = char_type_node;
7854 set_c_expr_source_range (&expr, tok_range);
7855 c_parser_consume_token (parser);
7856 break;
7857 case CPP_STRING:
7858 case CPP_STRING16:
7859 case CPP_STRING32:
7860 case CPP_WSTRING:
7861 case CPP_UTF8STRING:
7862 expr.value = c_parser_peek_token (parser)->value;
7863 set_c_expr_source_range (&expr, tok_range);
7864 expr.original_code = STRING_CST;
7865 c_parser_consume_token (parser);
7866 break;
7867 case CPP_OBJC_STRING:
7868 gcc_assert (c_dialect_objc ());
7869 expr.value
7870 = objc_build_string_object (c_parser_peek_token (parser)->value);
7871 set_c_expr_source_range (&expr, tok_range);
7872 c_parser_consume_token (parser);
7873 break;
7874 case CPP_NAME:
7875 switch (c_parser_peek_token (parser)->id_kind)
7877 case C_ID_ID:
7879 tree id = c_parser_peek_token (parser)->value;
7880 c_parser_consume_token (parser);
7881 expr.value = build_external_ref (loc, id,
7882 (c_parser_peek_token (parser)->type
7883 == CPP_OPEN_PAREN),
7884 &expr.original_type);
7885 set_c_expr_source_range (&expr, tok_range);
7886 break;
7888 case C_ID_CLASSNAME:
7890 /* Here we parse the Objective-C 2.0 Class.name dot
7891 syntax. */
7892 tree class_name = c_parser_peek_token (parser)->value;
7893 tree component;
7894 c_parser_consume_token (parser);
7895 gcc_assert (c_dialect_objc ());
7896 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7898 expr.set_error ();
7899 break;
7901 if (c_parser_next_token_is_not (parser, CPP_NAME))
7903 c_parser_error (parser, "expected identifier");
7904 expr.set_error ();
7905 break;
7907 c_token *component_tok = c_parser_peek_token (parser);
7908 component = component_tok->value;
7909 location_t end_loc = component_tok->get_finish ();
7910 c_parser_consume_token (parser);
7911 expr.value = objc_build_class_component_ref (class_name,
7912 component);
7913 set_c_expr_source_range (&expr, loc, end_loc);
7914 break;
7916 default:
7917 c_parser_error (parser, "expected expression");
7918 expr.set_error ();
7919 break;
7921 break;
7922 case CPP_OPEN_PAREN:
7923 /* A parenthesized expression, statement expression or compound
7924 literal. */
7925 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7927 /* A statement expression. */
7928 tree stmt;
7929 location_t brace_loc;
7930 c_parser_consume_token (parser);
7931 brace_loc = c_parser_peek_token (parser)->location;
7932 c_parser_consume_token (parser);
7933 if (!building_stmt_list_p ())
7935 error_at (loc, "braced-group within expression allowed "
7936 "only inside a function");
7937 parser->error = true;
7938 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7939 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7940 expr.set_error ();
7941 break;
7943 stmt = c_begin_stmt_expr ();
7944 c_parser_compound_statement_nostart (parser);
7945 location_t close_loc = c_parser_peek_token (parser)->location;
7946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7947 "expected %<)%>");
7948 pedwarn (loc, OPT_Wpedantic,
7949 "ISO C forbids braced-groups within expressions");
7950 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7951 set_c_expr_source_range (&expr, loc, close_loc);
7952 mark_exp_read (expr.value);
7954 else
7956 /* A parenthesized expression. */
7957 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7958 c_parser_consume_token (parser);
7959 expr = c_parser_expression (parser);
7960 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7961 TREE_NO_WARNING (expr.value) = 1;
7962 if (expr.original_code != C_MAYBE_CONST_EXPR
7963 && expr.original_code != SIZEOF_EXPR)
7964 expr.original_code = ERROR_MARK;
7965 /* Don't change EXPR.ORIGINAL_TYPE. */
7966 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7967 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7968 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7969 "expected %<)%>", loc_open_paren);
7971 break;
7972 case CPP_KEYWORD:
7973 switch (c_parser_peek_token (parser)->keyword)
7975 case RID_FUNCTION_NAME:
7976 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7977 "%<__FUNCTION__%> predefined identifier");
7978 expr.value = fname_decl (loc,
7979 c_parser_peek_token (parser)->keyword,
7980 c_parser_peek_token (parser)->value);
7981 set_c_expr_source_range (&expr, loc, loc);
7982 c_parser_consume_token (parser);
7983 break;
7984 case RID_PRETTY_FUNCTION_NAME:
7985 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7986 "%<__PRETTY_FUNCTION__%> predefined identifier");
7987 expr.value = fname_decl (loc,
7988 c_parser_peek_token (parser)->keyword,
7989 c_parser_peek_token (parser)->value);
7990 set_c_expr_source_range (&expr, loc, loc);
7991 c_parser_consume_token (parser);
7992 break;
7993 case RID_C99_FUNCTION_NAME:
7994 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7995 "%<__func__%> predefined identifier");
7996 expr.value = fname_decl (loc,
7997 c_parser_peek_token (parser)->keyword,
7998 c_parser_peek_token (parser)->value);
7999 set_c_expr_source_range (&expr, loc, loc);
8000 c_parser_consume_token (parser);
8001 break;
8002 case RID_VA_ARG:
8004 location_t start_loc = loc;
8005 c_parser_consume_token (parser);
8006 matching_parens parens;
8007 if (!parens.require_open (parser))
8009 expr.set_error ();
8010 break;
8012 e1 = c_parser_expr_no_commas (parser, NULL);
8013 mark_exp_read (e1.value);
8014 e1.value = c_fully_fold (e1.value, false, NULL);
8015 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8017 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8018 expr.set_error ();
8019 break;
8021 loc = c_parser_peek_token (parser)->location;
8022 t1 = c_parser_type_name (parser);
8023 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8024 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8025 "expected %<)%>");
8026 if (t1 == NULL)
8028 expr.set_error ();
8030 else
8032 tree type_expr = NULL_TREE;
8033 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8034 groktypename (t1, &type_expr, NULL));
8035 if (type_expr)
8037 expr.value = build2 (C_MAYBE_CONST_EXPR,
8038 TREE_TYPE (expr.value), type_expr,
8039 expr.value);
8040 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8042 set_c_expr_source_range (&expr, start_loc, end_loc);
8045 break;
8046 case RID_OFFSETOF:
8048 c_parser_consume_token (parser);
8049 matching_parens parens;
8050 if (!parens.require_open (parser))
8052 expr.set_error ();
8053 break;
8055 t1 = c_parser_type_name (parser);
8056 if (t1 == NULL)
8057 parser->error = true;
8058 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8059 gcc_assert (parser->error);
8060 if (parser->error)
8062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8063 expr.set_error ();
8064 break;
8066 tree type = groktypename (t1, NULL, NULL);
8067 tree offsetof_ref;
8068 if (type == error_mark_node)
8069 offsetof_ref = error_mark_node;
8070 else
8072 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8073 SET_EXPR_LOCATION (offsetof_ref, loc);
8075 /* Parse the second argument to __builtin_offsetof. We
8076 must have one identifier, and beyond that we want to
8077 accept sub structure and sub array references. */
8078 if (c_parser_next_token_is (parser, CPP_NAME))
8080 c_token *comp_tok = c_parser_peek_token (parser);
8081 offsetof_ref = build_component_ref
8082 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8083 c_parser_consume_token (parser);
8084 while (c_parser_next_token_is (parser, CPP_DOT)
8085 || c_parser_next_token_is (parser,
8086 CPP_OPEN_SQUARE)
8087 || c_parser_next_token_is (parser,
8088 CPP_DEREF))
8090 if (c_parser_next_token_is (parser, CPP_DEREF))
8092 loc = c_parser_peek_token (parser)->location;
8093 offsetof_ref = build_array_ref (loc,
8094 offsetof_ref,
8095 integer_zero_node);
8096 goto do_dot;
8098 else if (c_parser_next_token_is (parser, CPP_DOT))
8100 do_dot:
8101 c_parser_consume_token (parser);
8102 if (c_parser_next_token_is_not (parser,
8103 CPP_NAME))
8105 c_parser_error (parser, "expected identifier");
8106 break;
8108 c_token *comp_tok = c_parser_peek_token (parser);
8109 offsetof_ref = build_component_ref
8110 (loc, offsetof_ref, comp_tok->value,
8111 comp_tok->location);
8112 c_parser_consume_token (parser);
8114 else
8116 struct c_expr ce;
8117 tree idx;
8118 loc = c_parser_peek_token (parser)->location;
8119 c_parser_consume_token (parser);
8120 ce = c_parser_expression (parser);
8121 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8122 idx = ce.value;
8123 idx = c_fully_fold (idx, false, NULL);
8124 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8125 "expected %<]%>");
8126 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8130 else
8131 c_parser_error (parser, "expected identifier");
8132 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8134 "expected %<)%>");
8135 expr.value = fold_offsetof (offsetof_ref);
8136 set_c_expr_source_range (&expr, loc, end_loc);
8138 break;
8139 case RID_CHOOSE_EXPR:
8141 vec<c_expr_t, va_gc> *cexpr_list;
8142 c_expr_t *e1_p, *e2_p, *e3_p;
8143 tree c;
8144 location_t close_paren_loc;
8146 c_parser_consume_token (parser);
8147 if (!c_parser_get_builtin_args (parser,
8148 "__builtin_choose_expr",
8149 &cexpr_list, true,
8150 &close_paren_loc))
8152 expr.set_error ();
8153 break;
8156 if (vec_safe_length (cexpr_list) != 3)
8158 error_at (loc, "wrong number of arguments to "
8159 "%<__builtin_choose_expr%>");
8160 expr.set_error ();
8161 break;
8164 e1_p = &(*cexpr_list)[0];
8165 e2_p = &(*cexpr_list)[1];
8166 e3_p = &(*cexpr_list)[2];
8168 c = e1_p->value;
8169 mark_exp_read (e2_p->value);
8170 mark_exp_read (e3_p->value);
8171 if (TREE_CODE (c) != INTEGER_CST
8172 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8173 error_at (loc,
8174 "first argument to %<__builtin_choose_expr%> not"
8175 " a constant");
8176 constant_expression_warning (c);
8177 expr = integer_zerop (c) ? *e3_p : *e2_p;
8178 set_c_expr_source_range (&expr, loc, close_paren_loc);
8179 break;
8181 case RID_TYPES_COMPATIBLE_P:
8183 c_parser_consume_token (parser);
8184 matching_parens parens;
8185 if (!parens.require_open (parser))
8187 expr.set_error ();
8188 break;
8190 t1 = c_parser_type_name (parser);
8191 if (t1 == NULL)
8193 expr.set_error ();
8194 break;
8196 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8199 expr.set_error ();
8200 break;
8202 t2 = c_parser_type_name (parser);
8203 if (t2 == NULL)
8205 expr.set_error ();
8206 break;
8208 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8209 parens.skip_until_found_close (parser);
8210 tree e1, e2;
8211 e1 = groktypename (t1, NULL, NULL);
8212 e2 = groktypename (t2, NULL, NULL);
8213 if (e1 == error_mark_node || e2 == error_mark_node)
8215 expr.set_error ();
8216 break;
8219 e1 = TYPE_MAIN_VARIANT (e1);
8220 e2 = TYPE_MAIN_VARIANT (e2);
8222 expr.value
8223 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8224 set_c_expr_source_range (&expr, loc, close_paren_loc);
8226 break;
8227 case RID_BUILTIN_TGMATH:
8229 vec<c_expr_t, va_gc> *cexpr_list;
8230 location_t close_paren_loc;
8232 c_parser_consume_token (parser);
8233 if (!c_parser_get_builtin_args (parser,
8234 "__builtin_tgmath",
8235 &cexpr_list, false,
8236 &close_paren_loc))
8238 expr.set_error ();
8239 break;
8242 if (vec_safe_length (cexpr_list) < 3)
8244 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8245 expr.set_error ();
8246 break;
8249 unsigned int i;
8250 c_expr_t *p;
8251 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8252 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8253 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8254 if (nargs == 0)
8256 expr.set_error ();
8257 break;
8259 if (vec_safe_length (cexpr_list) < nargs)
8261 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8262 expr.set_error ();
8263 break;
8265 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8266 if (num_functions < 2)
8268 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8269 expr.set_error ();
8270 break;
8273 /* The first NUM_FUNCTIONS expressions are the function
8274 pointers. The remaining NARGS expressions are the
8275 arguments that are to be passed to one of those
8276 functions, chosen following <tgmath.h> rules. */
8277 for (unsigned int j = 1; j < num_functions; j++)
8279 unsigned int this_nargs
8280 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8281 if (this_nargs == 0)
8283 expr.set_error ();
8284 goto out;
8286 if (this_nargs != nargs)
8288 error_at ((*cexpr_list)[j].get_location (),
8289 "argument %u of %<__builtin_tgmath%> has "
8290 "wrong number of arguments", j + 1);
8291 expr.set_error ();
8292 goto out;
8296 /* The functions all have the same number of arguments.
8297 Determine whether arguments and return types vary in
8298 ways permitted for <tgmath.h> functions. */
8299 /* The first entry in each of these vectors is for the
8300 return type, subsequent entries for parameter
8301 types. */
8302 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8303 auto_vec<tree> parm_first (nargs + 1);
8304 auto_vec<bool> parm_complex (nargs + 1);
8305 auto_vec<bool> parm_varies (nargs + 1);
8306 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8307 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8308 parm_first.quick_push (first_ret);
8309 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8310 parm_varies.quick_push (false);
8311 function_args_iterator iter;
8312 tree t;
8313 unsigned int argpos;
8314 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8316 if (t == void_type_node)
8317 break;
8318 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8319 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8320 parm_varies.quick_push (false);
8322 for (unsigned int j = 1; j < num_functions; j++)
8324 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8325 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8326 if (ret != parm_first[0])
8328 parm_varies[0] = true;
8329 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8330 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8332 error_at ((*cexpr_list)[0].get_location (),
8333 "invalid type-generic return type for "
8334 "argument %u of %<__builtin_tgmath%>",
8336 expr.set_error ();
8337 goto out;
8339 if (!SCALAR_FLOAT_TYPE_P (ret)
8340 && !COMPLEX_FLOAT_TYPE_P (ret))
8342 error_at ((*cexpr_list)[j].get_location (),
8343 "invalid type-generic return type for "
8344 "argument %u of %<__builtin_tgmath%>",
8345 j + 1);
8346 expr.set_error ();
8347 goto out;
8350 if (TREE_CODE (ret) == COMPLEX_TYPE)
8351 parm_complex[0] = true;
8352 argpos = 1;
8353 FOREACH_FUNCTION_ARGS (type, t, iter)
8355 if (t == void_type_node)
8356 break;
8357 t = TYPE_MAIN_VARIANT (t);
8358 if (t != parm_first[argpos])
8360 parm_varies[argpos] = true;
8361 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8362 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8364 error_at ((*cexpr_list)[0].get_location (),
8365 "invalid type-generic type for "
8366 "argument %u of argument %u of "
8367 "%<__builtin_tgmath%>", argpos, 1);
8368 expr.set_error ();
8369 goto out;
8371 if (!SCALAR_FLOAT_TYPE_P (t)
8372 && !COMPLEX_FLOAT_TYPE_P (t))
8374 error_at ((*cexpr_list)[j].get_location (),
8375 "invalid type-generic type for "
8376 "argument %u of argument %u of "
8377 "%<__builtin_tgmath%>", argpos, j + 1);
8378 expr.set_error ();
8379 goto out;
8382 if (TREE_CODE (t) == COMPLEX_TYPE)
8383 parm_complex[argpos] = true;
8384 argpos++;
8387 enum tgmath_parm_kind max_variation = tgmath_fixed;
8388 for (unsigned int j = 0; j <= nargs; j++)
8390 enum tgmath_parm_kind this_kind;
8391 if (parm_varies[j])
8393 if (parm_complex[j])
8394 max_variation = this_kind = tgmath_complex;
8395 else
8397 this_kind = tgmath_real;
8398 if (max_variation != tgmath_complex)
8399 max_variation = tgmath_real;
8402 else
8403 this_kind = tgmath_fixed;
8404 parm_kind.quick_push (this_kind);
8406 if (max_variation == tgmath_fixed)
8408 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8409 "all have the same type");
8410 expr.set_error ();
8411 break;
8414 /* Identify a parameter (not the return type) that varies,
8415 including with complex types if any variation includes
8416 complex types; there must be at least one such
8417 parameter. */
8418 unsigned int tgarg = 0;
8419 for (unsigned int j = 1; j <= nargs; j++)
8420 if (parm_kind[j] == max_variation)
8422 tgarg = j;
8423 break;
8425 if (tgarg == 0)
8427 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8428 "lack type-generic parameter");
8429 expr.set_error ();
8430 break;
8433 /* Determine the type of the relevant parameter for each
8434 function. */
8435 auto_vec<tree> tg_type (num_functions);
8436 for (unsigned int j = 0; j < num_functions; j++)
8438 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8439 argpos = 1;
8440 FOREACH_FUNCTION_ARGS (type, t, iter)
8442 if (argpos == tgarg)
8444 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8445 break;
8447 argpos++;
8451 /* Verify that the corresponding types are different for
8452 all the listed functions. Also determine whether all
8453 the types are complex, whether all the types are
8454 standard or binary, and whether all the types are
8455 decimal. */
8456 bool all_complex = true;
8457 bool all_binary = true;
8458 bool all_decimal = true;
8459 hash_set<tree> tg_types;
8460 FOR_EACH_VEC_ELT (tg_type, i, t)
8462 if (TREE_CODE (t) == COMPLEX_TYPE)
8463 all_decimal = false;
8464 else
8466 all_complex = false;
8467 if (DECIMAL_FLOAT_TYPE_P (t))
8468 all_binary = false;
8469 else
8470 all_decimal = false;
8472 if (tg_types.add (t))
8474 error_at ((*cexpr_list)[i].get_location (),
8475 "duplicate type-generic parameter type for "
8476 "function argument %u of %<__builtin_tgmath%>",
8477 i + 1);
8478 expr.set_error ();
8479 goto out;
8483 /* Verify that other parameters and the return type whose
8484 types vary have their types varying in the correct
8485 way. */
8486 for (unsigned int j = 0; j < num_functions; j++)
8488 tree exp_type = tg_type[j];
8489 tree exp_real_type = exp_type;
8490 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8491 exp_real_type = TREE_TYPE (exp_type);
8492 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8493 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8494 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8495 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8497 error_at ((*cexpr_list)[j].get_location (),
8498 "bad return type for function argument %u "
8499 "of %<__builtin_tgmath%>", j + 1);
8500 expr.set_error ();
8501 goto out;
8503 argpos = 1;
8504 FOREACH_FUNCTION_ARGS (type, t, iter)
8506 if (t == void_type_node)
8507 break;
8508 t = TYPE_MAIN_VARIANT (t);
8509 if ((parm_kind[argpos] == tgmath_complex
8510 && t != exp_type)
8511 || (parm_kind[argpos] == tgmath_real
8512 && t != exp_real_type))
8514 error_at ((*cexpr_list)[j].get_location (),
8515 "bad type for argument %u of "
8516 "function argument %u of "
8517 "%<__builtin_tgmath%>", argpos, j + 1);
8518 expr.set_error ();
8519 goto out;
8521 argpos++;
8525 /* The functions listed are a valid set of functions for a
8526 <tgmath.h> macro to select between. Identify the
8527 matching function, if any. First, the argument types
8528 must be combined following <tgmath.h> rules. Integer
8529 types are treated as _Decimal64 if any type-generic
8530 argument is decimal, or if the only alternatives for
8531 type-generic arguments are of decimal types, and are
8532 otherwise treated as double (or _Complex double for
8533 complex integer types). After that adjustment, types
8534 are combined following the usual arithmetic
8535 conversions. If the function only accepts complex
8536 arguments, a complex type is produced. */
8537 bool arg_complex = all_complex;
8538 bool arg_binary = all_binary;
8539 bool arg_int_decimal = all_decimal;
8540 for (unsigned int j = 1; j <= nargs; j++)
8542 if (parm_kind[j] == tgmath_fixed)
8543 continue;
8544 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8545 tree type = TREE_TYPE (ce->value);
8546 if (!INTEGRAL_TYPE_P (type)
8547 && !SCALAR_FLOAT_TYPE_P (type)
8548 && TREE_CODE (type) != COMPLEX_TYPE)
8550 error_at (ce->get_location (),
8551 "invalid type of argument %u of type-generic "
8552 "function", j);
8553 expr.set_error ();
8554 goto out;
8556 if (DECIMAL_FLOAT_TYPE_P (type))
8558 arg_int_decimal = true;
8559 if (all_complex)
8561 error_at (ce->get_location (),
8562 "decimal floating-point argument %u to "
8563 "complex-only type-generic function", j);
8564 expr.set_error ();
8565 goto out;
8567 else if (all_binary)
8569 error_at (ce->get_location (),
8570 "decimal floating-point argument %u to "
8571 "binary-only type-generic function", j);
8572 expr.set_error ();
8573 goto out;
8575 else if (arg_complex)
8577 error_at (ce->get_location (),
8578 "both complex and decimal floating-point "
8579 "arguments to type-generic function");
8580 expr.set_error ();
8581 goto out;
8583 else if (arg_binary)
8585 error_at (ce->get_location (),
8586 "both binary and decimal floating-point "
8587 "arguments to type-generic function");
8588 expr.set_error ();
8589 goto out;
8592 else if (TREE_CODE (type) == COMPLEX_TYPE)
8594 arg_complex = true;
8595 if (COMPLEX_FLOAT_TYPE_P (type))
8596 arg_binary = true;
8597 if (all_decimal)
8599 error_at (ce->get_location (),
8600 "complex argument %u to "
8601 "decimal-only type-generic function", j);
8602 expr.set_error ();
8603 goto out;
8605 else if (arg_int_decimal)
8607 error_at (ce->get_location (),
8608 "both complex and decimal floating-point "
8609 "arguments to type-generic function");
8610 expr.set_error ();
8611 goto out;
8614 else if (SCALAR_FLOAT_TYPE_P (type))
8616 arg_binary = true;
8617 if (all_decimal)
8619 error_at (ce->get_location (),
8620 "binary argument %u to "
8621 "decimal-only type-generic function", j);
8622 expr.set_error ();
8623 goto out;
8625 else if (arg_int_decimal)
8627 error_at (ce->get_location (),
8628 "both binary and decimal floating-point "
8629 "arguments to type-generic function");
8630 expr.set_error ();
8631 goto out;
8635 tree arg_real = NULL_TREE;
8636 for (unsigned int j = 1; j <= nargs; j++)
8638 if (parm_kind[j] == tgmath_fixed)
8639 continue;
8640 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8641 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8642 if (TREE_CODE (type) == COMPLEX_TYPE)
8643 type = TREE_TYPE (type);
8644 if (INTEGRAL_TYPE_P (type))
8645 type = (arg_int_decimal
8646 ? dfloat64_type_node
8647 : double_type_node);
8648 if (arg_real == NULL_TREE)
8649 arg_real = type;
8650 else
8651 arg_real = common_type (arg_real, type);
8652 if (arg_real == error_mark_node)
8654 expr.set_error ();
8655 goto out;
8658 tree arg_type = (arg_complex
8659 ? build_complex_type (arg_real)
8660 : arg_real);
8662 /* Look for a function to call with type-generic parameter
8663 type ARG_TYPE. */
8664 c_expr_t *fn = NULL;
8665 for (unsigned int j = 0; j < num_functions; j++)
8667 if (tg_type[j] == arg_type)
8669 fn = &(*cexpr_list)[j];
8670 break;
8673 if (fn == NULL
8674 && parm_kind[0] == tgmath_fixed
8675 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8677 /* Presume this is a macro that rounds its result to a
8678 narrower type, and look for the first function with
8679 at least the range and precision of the argument
8680 type. */
8681 for (unsigned int j = 0; j < num_functions; j++)
8683 if (arg_complex
8684 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8685 continue;
8686 tree real_tg_type = (arg_complex
8687 ? TREE_TYPE (tg_type[j])
8688 : tg_type[j]);
8689 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8690 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8691 continue;
8692 scalar_float_mode arg_mode
8693 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8694 scalar_float_mode tg_mode
8695 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8696 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8697 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8698 if (arg_fmt->b == tg_fmt->b
8699 && arg_fmt->p <= tg_fmt->p
8700 && arg_fmt->emax <= tg_fmt->emax
8701 && (arg_fmt->emin - arg_fmt->p
8702 >= tg_fmt->emin - tg_fmt->p))
8704 fn = &(*cexpr_list)[j];
8705 break;
8709 if (fn == NULL)
8711 error_at (loc, "no matching function for type-generic call");
8712 expr.set_error ();
8713 break;
8716 /* Construct a call to FN. */
8717 vec<tree, va_gc> *args;
8718 vec_alloc (args, nargs);
8719 vec<tree, va_gc> *origtypes;
8720 vec_alloc (origtypes, nargs);
8721 auto_vec<location_t> arg_loc (nargs);
8722 for (unsigned int j = 0; j < nargs; j++)
8724 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8725 args->quick_push (ce->value);
8726 arg_loc.quick_push (ce->get_location ());
8727 origtypes->quick_push (ce->original_type);
8729 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8730 args, origtypes);
8731 set_c_expr_source_range (&expr, loc, close_paren_loc);
8732 break;
8734 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8736 vec<c_expr_t, va_gc> *cexpr_list;
8737 c_expr_t *e2_p;
8738 tree chain_value;
8739 location_t close_paren_loc;
8741 c_parser_consume_token (parser);
8742 if (!c_parser_get_builtin_args (parser,
8743 "__builtin_call_with_static_chain",
8744 &cexpr_list, false,
8745 &close_paren_loc))
8747 expr.set_error ();
8748 break;
8750 if (vec_safe_length (cexpr_list) != 2)
8752 error_at (loc, "wrong number of arguments to "
8753 "%<__builtin_call_with_static_chain%>");
8754 expr.set_error ();
8755 break;
8758 expr = (*cexpr_list)[0];
8759 e2_p = &(*cexpr_list)[1];
8760 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8761 chain_value = e2_p->value;
8762 mark_exp_read (chain_value);
8764 if (TREE_CODE (expr.value) != CALL_EXPR)
8765 error_at (loc, "first argument to "
8766 "%<__builtin_call_with_static_chain%> "
8767 "must be a call expression");
8768 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8769 error_at (loc, "second argument to "
8770 "%<__builtin_call_with_static_chain%> "
8771 "must be a pointer type");
8772 else
8773 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8774 set_c_expr_source_range (&expr, loc, close_paren_loc);
8775 break;
8777 case RID_BUILTIN_COMPLEX:
8779 vec<c_expr_t, va_gc> *cexpr_list;
8780 c_expr_t *e1_p, *e2_p;
8781 location_t close_paren_loc;
8783 c_parser_consume_token (parser);
8784 if (!c_parser_get_builtin_args (parser,
8785 "__builtin_complex",
8786 &cexpr_list, false,
8787 &close_paren_loc))
8789 expr.set_error ();
8790 break;
8793 if (vec_safe_length (cexpr_list) != 2)
8795 error_at (loc, "wrong number of arguments to "
8796 "%<__builtin_complex%>");
8797 expr.set_error ();
8798 break;
8801 e1_p = &(*cexpr_list)[0];
8802 e2_p = &(*cexpr_list)[1];
8804 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8805 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8806 e1_p->value = convert (TREE_TYPE (e1_p->value),
8807 TREE_OPERAND (e1_p->value, 0));
8808 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8809 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8810 e2_p->value = convert (TREE_TYPE (e2_p->value),
8811 TREE_OPERAND (e2_p->value, 0));
8812 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8813 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8814 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8815 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8817 error_at (loc, "%<__builtin_complex%> operand "
8818 "not of real binary floating-point type");
8819 expr.set_error ();
8820 break;
8822 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8823 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8825 error_at (loc,
8826 "%<__builtin_complex%> operands of different types");
8827 expr.set_error ();
8828 break;
8830 pedwarn_c90 (loc, OPT_Wpedantic,
8831 "ISO C90 does not support complex types");
8832 expr.value = build2_loc (loc, COMPLEX_EXPR,
8833 build_complex_type
8834 (TYPE_MAIN_VARIANT
8835 (TREE_TYPE (e1_p->value))),
8836 e1_p->value, e2_p->value);
8837 set_c_expr_source_range (&expr, loc, close_paren_loc);
8838 break;
8840 case RID_BUILTIN_SHUFFLE:
8842 vec<c_expr_t, va_gc> *cexpr_list;
8843 unsigned int i;
8844 c_expr_t *p;
8845 location_t close_paren_loc;
8847 c_parser_consume_token (parser);
8848 if (!c_parser_get_builtin_args (parser,
8849 "__builtin_shuffle",
8850 &cexpr_list, false,
8851 &close_paren_loc))
8853 expr.set_error ();
8854 break;
8857 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8858 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8860 if (vec_safe_length (cexpr_list) == 2)
8861 expr.value =
8862 c_build_vec_perm_expr
8863 (loc, (*cexpr_list)[0].value,
8864 NULL_TREE, (*cexpr_list)[1].value);
8866 else if (vec_safe_length (cexpr_list) == 3)
8867 expr.value =
8868 c_build_vec_perm_expr
8869 (loc, (*cexpr_list)[0].value,
8870 (*cexpr_list)[1].value,
8871 (*cexpr_list)[2].value);
8872 else
8874 error_at (loc, "wrong number of arguments to "
8875 "%<__builtin_shuffle%>");
8876 expr.set_error ();
8878 set_c_expr_source_range (&expr, loc, close_paren_loc);
8879 break;
8881 case RID_AT_SELECTOR:
8883 gcc_assert (c_dialect_objc ());
8884 c_parser_consume_token (parser);
8885 matching_parens parens;
8886 if (!parens.require_open (parser))
8888 expr.set_error ();
8889 break;
8891 tree sel = c_parser_objc_selector_arg (parser);
8892 location_t close_loc = c_parser_peek_token (parser)->location;
8893 parens.skip_until_found_close (parser);
8894 expr.value = objc_build_selector_expr (loc, sel);
8895 set_c_expr_source_range (&expr, loc, close_loc);
8897 break;
8898 case RID_AT_PROTOCOL:
8900 gcc_assert (c_dialect_objc ());
8901 c_parser_consume_token (parser);
8902 matching_parens parens;
8903 if (!parens.require_open (parser))
8905 expr.set_error ();
8906 break;
8908 if (c_parser_next_token_is_not (parser, CPP_NAME))
8910 c_parser_error (parser, "expected identifier");
8911 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8912 expr.set_error ();
8913 break;
8915 tree id = c_parser_peek_token (parser)->value;
8916 c_parser_consume_token (parser);
8917 location_t close_loc = c_parser_peek_token (parser)->location;
8918 parens.skip_until_found_close (parser);
8919 expr.value = objc_build_protocol_expr (id);
8920 set_c_expr_source_range (&expr, loc, close_loc);
8922 break;
8923 case RID_AT_ENCODE:
8925 /* Extension to support C-structures in the archiver. */
8926 gcc_assert (c_dialect_objc ());
8927 c_parser_consume_token (parser);
8928 matching_parens parens;
8929 if (!parens.require_open (parser))
8931 expr.set_error ();
8932 break;
8934 t1 = c_parser_type_name (parser);
8935 if (t1 == NULL)
8937 expr.set_error ();
8938 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8939 break;
8941 location_t close_loc = c_parser_peek_token (parser)->location;
8942 parens.skip_until_found_close (parser);
8943 tree type = groktypename (t1, NULL, NULL);
8944 expr.value = objc_build_encode_expr (type);
8945 set_c_expr_source_range (&expr, loc, close_loc);
8947 break;
8948 case RID_GENERIC:
8949 expr = c_parser_generic_selection (parser);
8950 break;
8951 default:
8952 c_parser_error (parser, "expected expression");
8953 expr.set_error ();
8954 break;
8956 break;
8957 case CPP_OPEN_SQUARE:
8958 if (c_dialect_objc ())
8960 tree receiver, args;
8961 c_parser_consume_token (parser);
8962 receiver = c_parser_objc_receiver (parser);
8963 args = c_parser_objc_message_args (parser);
8964 location_t close_loc = c_parser_peek_token (parser)->location;
8965 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8966 "expected %<]%>");
8967 expr.value = objc_build_message_expr (receiver, args);
8968 set_c_expr_source_range (&expr, loc, close_loc);
8969 break;
8971 /* Else fall through to report error. */
8972 /* FALLTHRU */
8973 default:
8974 c_parser_error (parser, "expected expression");
8975 expr.set_error ();
8976 break;
8978 out:
8979 return c_parser_postfix_expression_after_primary
8980 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8983 /* Parse a postfix expression after a parenthesized type name: the
8984 brace-enclosed initializer of a compound literal, possibly followed
8985 by some postfix operators. This is separate because it is not
8986 possible to tell until after the type name whether a cast
8987 expression has a cast or a compound literal, or whether the operand
8988 of sizeof is a parenthesized type name or starts with a compound
8989 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8990 location of the first token after the parentheses around the type
8991 name. */
8993 static struct c_expr
8994 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8995 struct c_type_name *type_name,
8996 location_t type_loc)
8998 tree type;
8999 struct c_expr init;
9000 bool non_const;
9001 struct c_expr expr;
9002 location_t start_loc;
9003 tree type_expr = NULL_TREE;
9004 bool type_expr_const = true;
9005 check_compound_literal_type (type_loc, type_name);
9006 rich_location richloc (line_table, type_loc);
9007 start_init (NULL_TREE, NULL, 0, &richloc);
9008 type = groktypename (type_name, &type_expr, &type_expr_const);
9009 start_loc = c_parser_peek_token (parser)->location;
9010 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9012 error_at (type_loc, "compound literal has variable size");
9013 type = error_mark_node;
9015 init = c_parser_braced_init (parser, type, false, NULL);
9016 finish_init ();
9017 maybe_warn_string_init (type_loc, type, init);
9019 if (type != error_mark_node
9020 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9021 && current_function_decl)
9023 error ("compound literal qualified by address-space qualifier");
9024 type = error_mark_node;
9027 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9028 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9029 ? CONSTRUCTOR_NON_CONST (init.value)
9030 : init.original_code == C_MAYBE_CONST_EXPR);
9031 non_const |= !type_expr_const;
9032 unsigned int alignas_align = 0;
9033 if (type != error_mark_node
9034 && type_name->specs->align_log != -1)
9036 alignas_align = 1U << type_name->specs->align_log;
9037 if (alignas_align < min_align_of_type (type))
9039 error_at (type_name->specs->locations[cdw_alignas],
9040 "%<_Alignas%> specifiers cannot reduce "
9041 "alignment of compound literal");
9042 alignas_align = 0;
9045 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9046 alignas_align);
9047 set_c_expr_source_range (&expr, init.src_range);
9048 expr.original_code = ERROR_MARK;
9049 expr.original_type = NULL;
9050 if (type != error_mark_node
9051 && expr.value != error_mark_node
9052 && type_expr)
9054 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9056 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9057 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9059 else
9061 gcc_assert (!non_const);
9062 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9063 type_expr, expr.value);
9066 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9069 /* Callback function for sizeof_pointer_memaccess_warning to compare
9070 types. */
9072 static bool
9073 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9075 return comptypes (type1, type2) == 1;
9078 /* Parse a postfix expression after the initial primary or compound
9079 literal; that is, parse a series of postfix operators.
9081 EXPR_LOC is the location of the primary expression. */
9083 static struct c_expr
9084 c_parser_postfix_expression_after_primary (c_parser *parser,
9085 location_t expr_loc,
9086 struct c_expr expr)
9088 struct c_expr orig_expr;
9089 tree ident, idx;
9090 location_t sizeof_arg_loc[3], comp_loc;
9091 tree sizeof_arg[3];
9092 unsigned int literal_zero_mask;
9093 unsigned int i;
9094 vec<tree, va_gc> *exprlist;
9095 vec<tree, va_gc> *origtypes = NULL;
9096 vec<location_t> arg_loc = vNULL;
9097 location_t start;
9098 location_t finish;
9100 while (true)
9102 location_t op_loc = c_parser_peek_token (parser)->location;
9103 switch (c_parser_peek_token (parser)->type)
9105 case CPP_OPEN_SQUARE:
9106 /* Array reference. */
9107 c_parser_consume_token (parser);
9108 idx = c_parser_expression (parser).value;
9109 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9110 "expected %<]%>");
9111 start = expr.get_start ();
9112 finish = parser->tokens_buf[0].location;
9113 expr.value = build_array_ref (op_loc, expr.value, idx);
9114 set_c_expr_source_range (&expr, start, finish);
9115 expr.original_code = ERROR_MARK;
9116 expr.original_type = NULL;
9117 break;
9118 case CPP_OPEN_PAREN:
9119 /* Function call. */
9120 c_parser_consume_token (parser);
9121 for (i = 0; i < 3; i++)
9123 sizeof_arg[i] = NULL_TREE;
9124 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9126 literal_zero_mask = 0;
9127 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9128 exprlist = NULL;
9129 else
9130 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9131 sizeof_arg_loc, sizeof_arg,
9132 &arg_loc, &literal_zero_mask);
9133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9134 "expected %<)%>");
9135 orig_expr = expr;
9136 mark_exp_read (expr.value);
9137 if (warn_sizeof_pointer_memaccess)
9138 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9139 expr.value, exprlist,
9140 sizeof_arg,
9141 sizeof_ptr_memacc_comptypes);
9142 if (TREE_CODE (expr.value) == FUNCTION_DECL
9143 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9144 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9145 && vec_safe_length (exprlist) == 3)
9147 tree arg0 = (*exprlist)[0];
9148 tree arg2 = (*exprlist)[2];
9149 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9152 start = expr.get_start ();
9153 finish = parser->tokens_buf[0].get_finish ();
9154 expr.value
9155 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9156 exprlist, origtypes);
9157 set_c_expr_source_range (&expr, start, finish);
9159 expr.original_code = ERROR_MARK;
9160 if (TREE_CODE (expr.value) == INTEGER_CST
9161 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9162 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9163 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9164 expr.original_code = C_MAYBE_CONST_EXPR;
9165 expr.original_type = NULL;
9166 if (exprlist)
9168 release_tree_vector (exprlist);
9169 release_tree_vector (origtypes);
9171 arg_loc.release ();
9172 break;
9173 case CPP_DOT:
9174 /* Structure element reference. */
9175 c_parser_consume_token (parser);
9176 expr = default_function_array_conversion (expr_loc, expr);
9177 if (c_parser_next_token_is (parser, CPP_NAME))
9179 c_token *comp_tok = c_parser_peek_token (parser);
9180 ident = comp_tok->value;
9181 comp_loc = comp_tok->location;
9183 else
9185 c_parser_error (parser, "expected identifier");
9186 expr.set_error ();
9187 expr.original_code = ERROR_MARK;
9188 expr.original_type = NULL;
9189 return expr;
9191 start = expr.get_start ();
9192 finish = c_parser_peek_token (parser)->get_finish ();
9193 c_parser_consume_token (parser);
9194 expr.value = build_component_ref (op_loc, expr.value, ident,
9195 comp_loc);
9196 set_c_expr_source_range (&expr, start, finish);
9197 expr.original_code = ERROR_MARK;
9198 if (TREE_CODE (expr.value) != COMPONENT_REF)
9199 expr.original_type = NULL;
9200 else
9202 /* Remember the original type of a bitfield. */
9203 tree field = TREE_OPERAND (expr.value, 1);
9204 if (TREE_CODE (field) != FIELD_DECL)
9205 expr.original_type = NULL;
9206 else
9207 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9209 break;
9210 case CPP_DEREF:
9211 /* Structure element reference. */
9212 c_parser_consume_token (parser);
9213 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9214 if (c_parser_next_token_is (parser, CPP_NAME))
9216 c_token *comp_tok = c_parser_peek_token (parser);
9217 ident = comp_tok->value;
9218 comp_loc = comp_tok->location;
9220 else
9222 c_parser_error (parser, "expected identifier");
9223 expr.set_error ();
9224 expr.original_code = ERROR_MARK;
9225 expr.original_type = NULL;
9226 return expr;
9228 start = expr.get_start ();
9229 finish = c_parser_peek_token (parser)->get_finish ();
9230 c_parser_consume_token (parser);
9231 expr.value = build_component_ref (op_loc,
9232 build_indirect_ref (op_loc,
9233 expr.value,
9234 RO_ARROW),
9235 ident, comp_loc);
9236 set_c_expr_source_range (&expr, start, finish);
9237 expr.original_code = ERROR_MARK;
9238 if (TREE_CODE (expr.value) != COMPONENT_REF)
9239 expr.original_type = NULL;
9240 else
9242 /* Remember the original type of a bitfield. */
9243 tree field = TREE_OPERAND (expr.value, 1);
9244 if (TREE_CODE (field) != FIELD_DECL)
9245 expr.original_type = NULL;
9246 else
9247 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9249 break;
9250 case CPP_PLUS_PLUS:
9251 /* Postincrement. */
9252 start = expr.get_start ();
9253 finish = c_parser_peek_token (parser)->get_finish ();
9254 c_parser_consume_token (parser);
9255 expr = default_function_array_read_conversion (expr_loc, expr);
9256 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9257 expr.value, false);
9258 set_c_expr_source_range (&expr, start, finish);
9259 expr.original_code = ERROR_MARK;
9260 expr.original_type = NULL;
9261 break;
9262 case CPP_MINUS_MINUS:
9263 /* Postdecrement. */
9264 start = expr.get_start ();
9265 finish = c_parser_peek_token (parser)->get_finish ();
9266 c_parser_consume_token (parser);
9267 expr = default_function_array_read_conversion (expr_loc, expr);
9268 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9269 expr.value, false);
9270 set_c_expr_source_range (&expr, start, finish);
9271 expr.original_code = ERROR_MARK;
9272 expr.original_type = NULL;
9273 break;
9274 default:
9275 return expr;
9280 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9282 expression:
9283 assignment-expression
9284 expression , assignment-expression
9287 static struct c_expr
9288 c_parser_expression (c_parser *parser)
9290 location_t tloc = c_parser_peek_token (parser)->location;
9291 struct c_expr expr;
9292 expr = c_parser_expr_no_commas (parser, NULL);
9293 if (c_parser_next_token_is (parser, CPP_COMMA))
9294 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9295 while (c_parser_next_token_is (parser, CPP_COMMA))
9297 struct c_expr next;
9298 tree lhsval;
9299 location_t loc = c_parser_peek_token (parser)->location;
9300 location_t expr_loc;
9301 c_parser_consume_token (parser);
9302 expr_loc = c_parser_peek_token (parser)->location;
9303 lhsval = expr.value;
9304 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9305 lhsval = TREE_OPERAND (lhsval, 1);
9306 if (DECL_P (lhsval) || handled_component_p (lhsval))
9307 mark_exp_read (lhsval);
9308 next = c_parser_expr_no_commas (parser, NULL);
9309 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9310 expr.value = build_compound_expr (loc, expr.value, next.value);
9311 expr.original_code = COMPOUND_EXPR;
9312 expr.original_type = next.original_type;
9314 return expr;
9317 /* Parse an expression and convert functions or arrays to pointers and
9318 lvalues to rvalues. */
9320 static struct c_expr
9321 c_parser_expression_conv (c_parser *parser)
9323 struct c_expr expr;
9324 location_t loc = c_parser_peek_token (parser)->location;
9325 expr = c_parser_expression (parser);
9326 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9327 return expr;
9330 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9331 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9333 static inline void
9334 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9335 unsigned int idx)
9337 if (idx >= HOST_BITS_PER_INT)
9338 return;
9340 c_token *tok = c_parser_peek_token (parser);
9341 switch (tok->type)
9343 case CPP_NUMBER:
9344 case CPP_CHAR:
9345 case CPP_WCHAR:
9346 case CPP_CHAR16:
9347 case CPP_CHAR32:
9348 /* If a parameter is literal zero alone, remember it
9349 for -Wmemset-transposed-args warning. */
9350 if (integer_zerop (tok->value)
9351 && !TREE_OVERFLOW (tok->value)
9352 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9353 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9354 *literal_zero_mask |= 1U << idx;
9355 default:
9356 break;
9360 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9361 functions and arrays to pointers and lvalues to rvalues. If
9362 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9363 locations of function arguments into this vector.
9365 nonempty-expr-list:
9366 assignment-expression
9367 nonempty-expr-list , assignment-expression
9370 static vec<tree, va_gc> *
9371 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9372 vec<tree, va_gc> **p_orig_types,
9373 location_t *sizeof_arg_loc, tree *sizeof_arg,
9374 vec<location_t> *locations,
9375 unsigned int *literal_zero_mask)
9377 vec<tree, va_gc> *ret;
9378 vec<tree, va_gc> *orig_types;
9379 struct c_expr expr;
9380 unsigned int idx = 0;
9382 ret = make_tree_vector ();
9383 if (p_orig_types == NULL)
9384 orig_types = NULL;
9385 else
9386 orig_types = make_tree_vector ();
9388 if (literal_zero_mask)
9389 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9390 expr = c_parser_expr_no_commas (parser, NULL);
9391 if (convert_p)
9392 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9393 if (fold_p)
9394 expr.value = c_fully_fold (expr.value, false, NULL);
9395 ret->quick_push (expr.value);
9396 if (orig_types)
9397 orig_types->quick_push (expr.original_type);
9398 if (locations)
9399 locations->safe_push (expr.get_location ());
9400 if (sizeof_arg != NULL
9401 && expr.original_code == SIZEOF_EXPR)
9403 sizeof_arg[0] = c_last_sizeof_arg;
9404 sizeof_arg_loc[0] = c_last_sizeof_loc;
9406 while (c_parser_next_token_is (parser, CPP_COMMA))
9408 c_parser_consume_token (parser);
9409 if (literal_zero_mask)
9410 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9411 expr = c_parser_expr_no_commas (parser, NULL);
9412 if (convert_p)
9413 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9414 true);
9415 if (fold_p)
9416 expr.value = c_fully_fold (expr.value, false, NULL);
9417 vec_safe_push (ret, expr.value);
9418 if (orig_types)
9419 vec_safe_push (orig_types, expr.original_type);
9420 if (locations)
9421 locations->safe_push (expr.get_location ());
9422 if (++idx < 3
9423 && sizeof_arg != NULL
9424 && expr.original_code == SIZEOF_EXPR)
9426 sizeof_arg[idx] = c_last_sizeof_arg;
9427 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9430 if (orig_types)
9431 *p_orig_types = orig_types;
9432 return ret;
9435 /* Parse Objective-C-specific constructs. */
9437 /* Parse an objc-class-definition.
9439 objc-class-definition:
9440 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9441 objc-class-instance-variables[opt] objc-methodprotolist @end
9442 @implementation identifier objc-superclass[opt]
9443 objc-class-instance-variables[opt]
9444 @interface identifier ( identifier ) objc-protocol-refs[opt]
9445 objc-methodprotolist @end
9446 @interface identifier ( ) objc-protocol-refs[opt]
9447 objc-methodprotolist @end
9448 @implementation identifier ( identifier )
9450 objc-superclass:
9451 : identifier
9453 "@interface identifier (" must start "@interface identifier (
9454 identifier ) ...": objc-methodprotolist in the first production may
9455 not start with a parenthesized identifier as a declarator of a data
9456 definition with no declaration specifiers if the objc-superclass,
9457 objc-protocol-refs and objc-class-instance-variables are omitted. */
9459 static void
9460 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9462 bool iface_p;
9463 tree id1;
9464 tree superclass;
9465 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9466 iface_p = true;
9467 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9468 iface_p = false;
9469 else
9470 gcc_unreachable ();
9472 c_parser_consume_token (parser);
9473 if (c_parser_next_token_is_not (parser, CPP_NAME))
9475 c_parser_error (parser, "expected identifier");
9476 return;
9478 id1 = c_parser_peek_token (parser)->value;
9479 c_parser_consume_token (parser);
9480 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9482 /* We have a category or class extension. */
9483 tree id2;
9484 tree proto = NULL_TREE;
9485 matching_parens parens;
9486 parens.consume_open (parser);
9487 if (c_parser_next_token_is_not (parser, CPP_NAME))
9489 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9491 /* We have a class extension. */
9492 id2 = NULL_TREE;
9494 else
9496 c_parser_error (parser, "expected identifier or %<)%>");
9497 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9498 return;
9501 else
9503 id2 = c_parser_peek_token (parser)->value;
9504 c_parser_consume_token (parser);
9506 parens.skip_until_found_close (parser);
9507 if (!iface_p)
9509 objc_start_category_implementation (id1, id2);
9510 return;
9512 if (c_parser_next_token_is (parser, CPP_LESS))
9513 proto = c_parser_objc_protocol_refs (parser);
9514 objc_start_category_interface (id1, id2, proto, attributes);
9515 c_parser_objc_methodprotolist (parser);
9516 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9517 objc_finish_interface ();
9518 return;
9520 if (c_parser_next_token_is (parser, CPP_COLON))
9522 c_parser_consume_token (parser);
9523 if (c_parser_next_token_is_not (parser, CPP_NAME))
9525 c_parser_error (parser, "expected identifier");
9526 return;
9528 superclass = c_parser_peek_token (parser)->value;
9529 c_parser_consume_token (parser);
9531 else
9532 superclass = NULL_TREE;
9533 if (iface_p)
9535 tree proto = NULL_TREE;
9536 if (c_parser_next_token_is (parser, CPP_LESS))
9537 proto = c_parser_objc_protocol_refs (parser);
9538 objc_start_class_interface (id1, superclass, proto, attributes);
9540 else
9541 objc_start_class_implementation (id1, superclass);
9542 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9543 c_parser_objc_class_instance_variables (parser);
9544 if (iface_p)
9546 objc_continue_interface ();
9547 c_parser_objc_methodprotolist (parser);
9548 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9549 objc_finish_interface ();
9551 else
9553 objc_continue_implementation ();
9554 return;
9558 /* Parse objc-class-instance-variables.
9560 objc-class-instance-variables:
9561 { objc-instance-variable-decl-list[opt] }
9563 objc-instance-variable-decl-list:
9564 objc-visibility-spec
9565 objc-instance-variable-decl ;
9567 objc-instance-variable-decl-list objc-visibility-spec
9568 objc-instance-variable-decl-list objc-instance-variable-decl ;
9569 objc-instance-variable-decl-list ;
9571 objc-visibility-spec:
9572 @private
9573 @protected
9574 @public
9576 objc-instance-variable-decl:
9577 struct-declaration
9580 static void
9581 c_parser_objc_class_instance_variables (c_parser *parser)
9583 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9584 c_parser_consume_token (parser);
9585 while (c_parser_next_token_is_not (parser, CPP_EOF))
9587 tree decls;
9588 /* Parse any stray semicolon. */
9589 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9591 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9592 "extra semicolon");
9593 c_parser_consume_token (parser);
9594 continue;
9596 /* Stop if at the end of the instance variables. */
9597 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9599 c_parser_consume_token (parser);
9600 break;
9602 /* Parse any objc-visibility-spec. */
9603 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9605 c_parser_consume_token (parser);
9606 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9607 continue;
9609 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9611 c_parser_consume_token (parser);
9612 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9613 continue;
9615 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9617 c_parser_consume_token (parser);
9618 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9619 continue;
9621 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9623 c_parser_consume_token (parser);
9624 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9625 continue;
9627 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9629 c_parser_pragma (parser, pragma_external, NULL);
9630 continue;
9633 /* Parse some comma-separated declarations. */
9634 decls = c_parser_struct_declaration (parser);
9635 if (decls == NULL)
9637 /* There is a syntax error. We want to skip the offending
9638 tokens up to the next ';' (included) or '}'
9639 (excluded). */
9641 /* First, skip manually a ')' or ']'. This is because they
9642 reduce the nesting level, so c_parser_skip_until_found()
9643 wouldn't be able to skip past them. */
9644 c_token *token = c_parser_peek_token (parser);
9645 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9646 c_parser_consume_token (parser);
9648 /* Then, do the standard skipping. */
9649 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9651 /* We hopefully recovered. Start normal parsing again. */
9652 parser->error = false;
9653 continue;
9655 else
9657 /* Comma-separated instance variables are chained together
9658 in reverse order; add them one by one. */
9659 tree ivar = nreverse (decls);
9660 for (; ivar; ivar = DECL_CHAIN (ivar))
9661 objc_add_instance_variable (copy_node (ivar));
9663 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9667 /* Parse an objc-class-declaration.
9669 objc-class-declaration:
9670 @class identifier-list ;
9673 static void
9674 c_parser_objc_class_declaration (c_parser *parser)
9676 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9677 c_parser_consume_token (parser);
9678 /* Any identifiers, including those declared as type names, are OK
9679 here. */
9680 while (true)
9682 tree id;
9683 if (c_parser_next_token_is_not (parser, CPP_NAME))
9685 c_parser_error (parser, "expected identifier");
9686 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9687 parser->error = false;
9688 return;
9690 id = c_parser_peek_token (parser)->value;
9691 objc_declare_class (id);
9692 c_parser_consume_token (parser);
9693 if (c_parser_next_token_is (parser, CPP_COMMA))
9694 c_parser_consume_token (parser);
9695 else
9696 break;
9698 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9701 /* Parse an objc-alias-declaration.
9703 objc-alias-declaration:
9704 @compatibility_alias identifier identifier ;
9707 static void
9708 c_parser_objc_alias_declaration (c_parser *parser)
9710 tree id1, id2;
9711 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9712 c_parser_consume_token (parser);
9713 if (c_parser_next_token_is_not (parser, CPP_NAME))
9715 c_parser_error (parser, "expected identifier");
9716 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9717 return;
9719 id1 = c_parser_peek_token (parser)->value;
9720 c_parser_consume_token (parser);
9721 if (c_parser_next_token_is_not (parser, CPP_NAME))
9723 c_parser_error (parser, "expected identifier");
9724 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9725 return;
9727 id2 = c_parser_peek_token (parser)->value;
9728 c_parser_consume_token (parser);
9729 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9730 objc_declare_alias (id1, id2);
9733 /* Parse an objc-protocol-definition.
9735 objc-protocol-definition:
9736 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9737 @protocol identifier-list ;
9739 "@protocol identifier ;" should be resolved as "@protocol
9740 identifier-list ;": objc-methodprotolist may not start with a
9741 semicolon in the first alternative if objc-protocol-refs are
9742 omitted. */
9744 static void
9745 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9747 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9749 c_parser_consume_token (parser);
9750 if (c_parser_next_token_is_not (parser, CPP_NAME))
9752 c_parser_error (parser, "expected identifier");
9753 return;
9755 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9756 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9758 /* Any identifiers, including those declared as type names, are
9759 OK here. */
9760 while (true)
9762 tree id;
9763 if (c_parser_next_token_is_not (parser, CPP_NAME))
9765 c_parser_error (parser, "expected identifier");
9766 break;
9768 id = c_parser_peek_token (parser)->value;
9769 objc_declare_protocol (id, attributes);
9770 c_parser_consume_token (parser);
9771 if (c_parser_next_token_is (parser, CPP_COMMA))
9772 c_parser_consume_token (parser);
9773 else
9774 break;
9776 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9778 else
9780 tree id = c_parser_peek_token (parser)->value;
9781 tree proto = NULL_TREE;
9782 c_parser_consume_token (parser);
9783 if (c_parser_next_token_is (parser, CPP_LESS))
9784 proto = c_parser_objc_protocol_refs (parser);
9785 parser->objc_pq_context = true;
9786 objc_start_protocol (id, proto, attributes);
9787 c_parser_objc_methodprotolist (parser);
9788 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9789 parser->objc_pq_context = false;
9790 objc_finish_interface ();
9794 /* Parse an objc-method-type.
9796 objc-method-type:
9800 Return true if it is a class method (+) and false if it is
9801 an instance method (-).
9803 static inline bool
9804 c_parser_objc_method_type (c_parser *parser)
9806 switch (c_parser_peek_token (parser)->type)
9808 case CPP_PLUS:
9809 c_parser_consume_token (parser);
9810 return true;
9811 case CPP_MINUS:
9812 c_parser_consume_token (parser);
9813 return false;
9814 default:
9815 gcc_unreachable ();
9819 /* Parse an objc-method-definition.
9821 objc-method-definition:
9822 objc-method-type objc-method-decl ;[opt] compound-statement
9825 static void
9826 c_parser_objc_method_definition (c_parser *parser)
9828 bool is_class_method = c_parser_objc_method_type (parser);
9829 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9830 parser->objc_pq_context = true;
9831 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9832 &expr);
9833 if (decl == error_mark_node)
9834 return; /* Bail here. */
9836 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9838 c_parser_consume_token (parser);
9839 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9840 "extra semicolon in method definition specified");
9843 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9845 c_parser_error (parser, "expected %<{%>");
9846 return;
9849 parser->objc_pq_context = false;
9850 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9852 add_stmt (c_parser_compound_statement (parser));
9853 objc_finish_method_definition (current_function_decl);
9855 else
9857 /* This code is executed when we find a method definition
9858 outside of an @implementation context (or invalid for other
9859 reasons). Parse the method (to keep going) but do not emit
9860 any code.
9862 c_parser_compound_statement (parser);
9866 /* Parse an objc-methodprotolist.
9868 objc-methodprotolist:
9869 empty
9870 objc-methodprotolist objc-methodproto
9871 objc-methodprotolist declaration
9872 objc-methodprotolist ;
9873 @optional
9874 @required
9876 The declaration is a data definition, which may be missing
9877 declaration specifiers under the same rules and diagnostics as
9878 other data definitions outside functions, and the stray semicolon
9879 is diagnosed the same way as a stray semicolon outside a
9880 function. */
9882 static void
9883 c_parser_objc_methodprotolist (c_parser *parser)
9885 while (true)
9887 /* The list is terminated by @end. */
9888 switch (c_parser_peek_token (parser)->type)
9890 case CPP_SEMICOLON:
9891 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9892 "ISO C does not allow extra %<;%> outside of a function");
9893 c_parser_consume_token (parser);
9894 break;
9895 case CPP_PLUS:
9896 case CPP_MINUS:
9897 c_parser_objc_methodproto (parser);
9898 break;
9899 case CPP_PRAGMA:
9900 c_parser_pragma (parser, pragma_external, NULL);
9901 break;
9902 case CPP_EOF:
9903 return;
9904 default:
9905 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9906 return;
9907 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9908 c_parser_objc_at_property_declaration (parser);
9909 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9911 objc_set_method_opt (true);
9912 c_parser_consume_token (parser);
9914 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9916 objc_set_method_opt (false);
9917 c_parser_consume_token (parser);
9919 else
9920 c_parser_declaration_or_fndef (parser, false, false, true,
9921 false, true, NULL, vNULL);
9922 break;
9927 /* Parse an objc-methodproto.
9929 objc-methodproto:
9930 objc-method-type objc-method-decl ;
9933 static void
9934 c_parser_objc_methodproto (c_parser *parser)
9936 bool is_class_method = c_parser_objc_method_type (parser);
9937 tree decl, attributes = NULL_TREE;
9939 /* Remember protocol qualifiers in prototypes. */
9940 parser->objc_pq_context = true;
9941 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9942 NULL);
9943 /* Forget protocol qualifiers now. */
9944 parser->objc_pq_context = false;
9946 /* Do not allow the presence of attributes to hide an erroneous
9947 method implementation in the interface section. */
9948 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9950 c_parser_error (parser, "expected %<;%>");
9951 return;
9954 if (decl != error_mark_node)
9955 objc_add_method_declaration (is_class_method, decl, attributes);
9957 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9960 /* If we are at a position that method attributes may be present, check that
9961 there are not any parsed already (a syntax error) and then collect any
9962 specified at the current location. Finally, if new attributes were present,
9963 check that the next token is legal ( ';' for decls and '{' for defs). */
9965 static bool
9966 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9968 bool bad = false;
9969 if (*attributes)
9971 c_parser_error (parser,
9972 "method attributes must be specified at the end only");
9973 *attributes = NULL_TREE;
9974 bad = true;
9977 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9978 *attributes = c_parser_attributes (parser);
9980 /* If there were no attributes here, just report any earlier error. */
9981 if (*attributes == NULL_TREE || bad)
9982 return bad;
9984 /* If the attributes are followed by a ; or {, then just report any earlier
9985 error. */
9986 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9987 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9988 return bad;
9990 /* We've got attributes, but not at the end. */
9991 c_parser_error (parser,
9992 "expected %<;%> or %<{%> after method attribute definition");
9993 return true;
9996 /* Parse an objc-method-decl.
9998 objc-method-decl:
9999 ( objc-type-name ) objc-selector
10000 objc-selector
10001 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10002 objc-keyword-selector objc-optparmlist
10003 attributes
10005 objc-keyword-selector:
10006 objc-keyword-decl
10007 objc-keyword-selector objc-keyword-decl
10009 objc-keyword-decl:
10010 objc-selector : ( objc-type-name ) identifier
10011 objc-selector : identifier
10012 : ( objc-type-name ) identifier
10013 : identifier
10015 objc-optparmlist:
10016 objc-optparms objc-optellipsis
10018 objc-optparms:
10019 empty
10020 objc-opt-parms , parameter-declaration
10022 objc-optellipsis:
10023 empty
10024 , ...
10027 static tree
10028 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10029 tree *attributes, tree *expr)
10031 tree type = NULL_TREE;
10032 tree sel;
10033 tree parms = NULL_TREE;
10034 bool ellipsis = false;
10035 bool attr_err = false;
10037 *attributes = NULL_TREE;
10038 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10040 matching_parens parens;
10041 parens.consume_open (parser);
10042 type = c_parser_objc_type_name (parser);
10043 parens.skip_until_found_close (parser);
10045 sel = c_parser_objc_selector (parser);
10046 /* If there is no selector, or a colon follows, we have an
10047 objc-keyword-selector. If there is a selector, and a colon does
10048 not follow, that selector ends the objc-method-decl. */
10049 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10051 tree tsel = sel;
10052 tree list = NULL_TREE;
10053 while (true)
10055 tree atype = NULL_TREE, id, keyworddecl;
10056 tree param_attr = NULL_TREE;
10057 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10058 break;
10059 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10061 c_parser_consume_token (parser);
10062 atype = c_parser_objc_type_name (parser);
10063 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10064 "expected %<)%>");
10066 /* New ObjC allows attributes on method parameters. */
10067 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10068 param_attr = c_parser_attributes (parser);
10069 if (c_parser_next_token_is_not (parser, CPP_NAME))
10071 c_parser_error (parser, "expected identifier");
10072 return error_mark_node;
10074 id = c_parser_peek_token (parser)->value;
10075 c_parser_consume_token (parser);
10076 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10077 list = chainon (list, keyworddecl);
10078 tsel = c_parser_objc_selector (parser);
10079 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10080 break;
10083 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10085 /* Parse the optional parameter list. Optional Objective-C
10086 method parameters follow the C syntax, and may include '...'
10087 to denote a variable number of arguments. */
10088 parms = make_node (TREE_LIST);
10089 while (c_parser_next_token_is (parser, CPP_COMMA))
10091 struct c_parm *parm;
10092 c_parser_consume_token (parser);
10093 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10095 ellipsis = true;
10096 c_parser_consume_token (parser);
10097 attr_err |= c_parser_objc_maybe_method_attributes
10098 (parser, attributes) ;
10099 break;
10101 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10102 if (parm == NULL)
10103 break;
10104 parms = chainon (parms,
10105 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10107 sel = list;
10109 else
10110 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10112 if (sel == NULL)
10114 c_parser_error (parser, "objective-c method declaration is expected");
10115 return error_mark_node;
10118 if (attr_err)
10119 return error_mark_node;
10121 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10124 /* Parse an objc-type-name.
10126 objc-type-name:
10127 objc-type-qualifiers[opt] type-name
10128 objc-type-qualifiers[opt]
10130 objc-type-qualifiers:
10131 objc-type-qualifier
10132 objc-type-qualifiers objc-type-qualifier
10134 objc-type-qualifier: one of
10135 in out inout bycopy byref oneway
10138 static tree
10139 c_parser_objc_type_name (c_parser *parser)
10141 tree quals = NULL_TREE;
10142 struct c_type_name *type_name = NULL;
10143 tree type = NULL_TREE;
10144 while (true)
10146 c_token *token = c_parser_peek_token (parser);
10147 if (token->type == CPP_KEYWORD
10148 && (token->keyword == RID_IN
10149 || token->keyword == RID_OUT
10150 || token->keyword == RID_INOUT
10151 || token->keyword == RID_BYCOPY
10152 || token->keyword == RID_BYREF
10153 || token->keyword == RID_ONEWAY))
10155 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10156 c_parser_consume_token (parser);
10158 else
10159 break;
10161 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10162 type_name = c_parser_type_name (parser);
10163 if (type_name)
10164 type = groktypename (type_name, NULL, NULL);
10166 /* If the type is unknown, and error has already been produced and
10167 we need to recover from the error. In that case, use NULL_TREE
10168 for the type, as if no type had been specified; this will use the
10169 default type ('id') which is good for error recovery. */
10170 if (type == error_mark_node)
10171 type = NULL_TREE;
10173 return build_tree_list (quals, type);
10176 /* Parse objc-protocol-refs.
10178 objc-protocol-refs:
10179 < identifier-list >
10182 static tree
10183 c_parser_objc_protocol_refs (c_parser *parser)
10185 tree list = NULL_TREE;
10186 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10187 c_parser_consume_token (parser);
10188 /* Any identifiers, including those declared as type names, are OK
10189 here. */
10190 while (true)
10192 tree id;
10193 if (c_parser_next_token_is_not (parser, CPP_NAME))
10195 c_parser_error (parser, "expected identifier");
10196 break;
10198 id = c_parser_peek_token (parser)->value;
10199 list = chainon (list, build_tree_list (NULL_TREE, id));
10200 c_parser_consume_token (parser);
10201 if (c_parser_next_token_is (parser, CPP_COMMA))
10202 c_parser_consume_token (parser);
10203 else
10204 break;
10206 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10207 return list;
10210 /* Parse an objc-try-catch-finally-statement.
10212 objc-try-catch-finally-statement:
10213 @try compound-statement objc-catch-list[opt]
10214 @try compound-statement objc-catch-list[opt] @finally compound-statement
10216 objc-catch-list:
10217 @catch ( objc-catch-parameter-declaration ) compound-statement
10218 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10220 objc-catch-parameter-declaration:
10221 parameter-declaration
10222 '...'
10224 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10226 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10227 for C++. Keep them in sync. */
10229 static void
10230 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10232 location_t location;
10233 tree stmt;
10235 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10236 c_parser_consume_token (parser);
10237 location = c_parser_peek_token (parser)->location;
10238 objc_maybe_warn_exceptions (location);
10239 stmt = c_parser_compound_statement (parser);
10240 objc_begin_try_stmt (location, stmt);
10242 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10244 struct c_parm *parm;
10245 tree parameter_declaration = error_mark_node;
10246 bool seen_open_paren = false;
10248 c_parser_consume_token (parser);
10249 matching_parens parens;
10250 if (!parens.require_open (parser))
10251 seen_open_paren = true;
10252 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10254 /* We have "@catch (...)" (where the '...' are literally
10255 what is in the code). Skip the '...'.
10256 parameter_declaration is set to NULL_TREE, and
10257 objc_being_catch_clauses() knows that that means
10258 '...'. */
10259 c_parser_consume_token (parser);
10260 parameter_declaration = NULL_TREE;
10262 else
10264 /* We have "@catch (NSException *exception)" or something
10265 like that. Parse the parameter declaration. */
10266 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10267 if (parm == NULL)
10268 parameter_declaration = error_mark_node;
10269 else
10270 parameter_declaration = grokparm (parm, NULL);
10272 if (seen_open_paren)
10273 parens.require_close (parser);
10274 else
10276 /* If there was no open parenthesis, we are recovering from
10277 an error, and we are trying to figure out what mistake
10278 the user has made. */
10280 /* If there is an immediate closing parenthesis, the user
10281 probably forgot the opening one (ie, they typed "@catch
10282 NSException *e)". Parse the closing parenthesis and keep
10283 going. */
10284 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10285 c_parser_consume_token (parser);
10287 /* If these is no immediate closing parenthesis, the user
10288 probably doesn't know that parenthesis are required at
10289 all (ie, they typed "@catch NSException *e"). So, just
10290 forget about the closing parenthesis and keep going. */
10292 objc_begin_catch_clause (parameter_declaration);
10293 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10294 c_parser_compound_statement_nostart (parser);
10295 objc_finish_catch_clause ();
10297 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10299 c_parser_consume_token (parser);
10300 location = c_parser_peek_token (parser)->location;
10301 stmt = c_parser_compound_statement (parser);
10302 objc_build_finally_clause (location, stmt);
10304 objc_finish_try_stmt ();
10307 /* Parse an objc-synchronized-statement.
10309 objc-synchronized-statement:
10310 @synchronized ( expression ) compound-statement
10313 static void
10314 c_parser_objc_synchronized_statement (c_parser *parser)
10316 location_t loc;
10317 tree expr, stmt;
10318 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10319 c_parser_consume_token (parser);
10320 loc = c_parser_peek_token (parser)->location;
10321 objc_maybe_warn_exceptions (loc);
10322 matching_parens parens;
10323 if (parens.require_open (parser))
10325 struct c_expr ce = c_parser_expression (parser);
10326 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10327 expr = ce.value;
10328 expr = c_fully_fold (expr, false, NULL);
10329 parens.skip_until_found_close (parser);
10331 else
10332 expr = error_mark_node;
10333 stmt = c_parser_compound_statement (parser);
10334 objc_build_synchronized (loc, expr, stmt);
10337 /* Parse an objc-selector; return NULL_TREE without an error if the
10338 next token is not an objc-selector.
10340 objc-selector:
10341 identifier
10342 one of
10343 enum struct union if else while do for switch case default
10344 break continue return goto asm sizeof typeof __alignof
10345 unsigned long const short volatile signed restrict _Complex
10346 in out inout bycopy byref oneway int char float double void _Bool
10347 _Atomic
10349 ??? Why this selection of keywords but not, for example, storage
10350 class specifiers? */
10352 static tree
10353 c_parser_objc_selector (c_parser *parser)
10355 c_token *token = c_parser_peek_token (parser);
10356 tree value = token->value;
10357 if (token->type == CPP_NAME)
10359 c_parser_consume_token (parser);
10360 return value;
10362 if (token->type != CPP_KEYWORD)
10363 return NULL_TREE;
10364 switch (token->keyword)
10366 case RID_ENUM:
10367 case RID_STRUCT:
10368 case RID_UNION:
10369 case RID_IF:
10370 case RID_ELSE:
10371 case RID_WHILE:
10372 case RID_DO:
10373 case RID_FOR:
10374 case RID_SWITCH:
10375 case RID_CASE:
10376 case RID_DEFAULT:
10377 case RID_BREAK:
10378 case RID_CONTINUE:
10379 case RID_RETURN:
10380 case RID_GOTO:
10381 case RID_ASM:
10382 case RID_SIZEOF:
10383 case RID_TYPEOF:
10384 case RID_ALIGNOF:
10385 case RID_UNSIGNED:
10386 case RID_LONG:
10387 case RID_CONST:
10388 case RID_SHORT:
10389 case RID_VOLATILE:
10390 case RID_SIGNED:
10391 case RID_RESTRICT:
10392 case RID_COMPLEX:
10393 case RID_IN:
10394 case RID_OUT:
10395 case RID_INOUT:
10396 case RID_BYCOPY:
10397 case RID_BYREF:
10398 case RID_ONEWAY:
10399 case RID_INT:
10400 case RID_CHAR:
10401 case RID_FLOAT:
10402 case RID_DOUBLE:
10403 CASE_RID_FLOATN_NX:
10404 case RID_VOID:
10405 case RID_BOOL:
10406 case RID_ATOMIC:
10407 case RID_AUTO_TYPE:
10408 case RID_INT_N_0:
10409 case RID_INT_N_1:
10410 case RID_INT_N_2:
10411 case RID_INT_N_3:
10412 c_parser_consume_token (parser);
10413 return value;
10414 default:
10415 return NULL_TREE;
10419 /* Parse an objc-selector-arg.
10421 objc-selector-arg:
10422 objc-selector
10423 objc-keywordname-list
10425 objc-keywordname-list:
10426 objc-keywordname
10427 objc-keywordname-list objc-keywordname
10429 objc-keywordname:
10430 objc-selector :
10434 static tree
10435 c_parser_objc_selector_arg (c_parser *parser)
10437 tree sel = c_parser_objc_selector (parser);
10438 tree list = NULL_TREE;
10439 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10440 return sel;
10441 while (true)
10443 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10444 return list;
10445 list = chainon (list, build_tree_list (sel, NULL_TREE));
10446 sel = c_parser_objc_selector (parser);
10447 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10448 break;
10450 return list;
10453 /* Parse an objc-receiver.
10455 objc-receiver:
10456 expression
10457 class-name
10458 type-name
10461 static tree
10462 c_parser_objc_receiver (c_parser *parser)
10464 location_t loc = c_parser_peek_token (parser)->location;
10466 if (c_parser_peek_token (parser)->type == CPP_NAME
10467 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10468 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10470 tree id = c_parser_peek_token (parser)->value;
10471 c_parser_consume_token (parser);
10472 return objc_get_class_reference (id);
10474 struct c_expr ce = c_parser_expression (parser);
10475 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10476 return c_fully_fold (ce.value, false, NULL);
10479 /* Parse objc-message-args.
10481 objc-message-args:
10482 objc-selector
10483 objc-keywordarg-list
10485 objc-keywordarg-list:
10486 objc-keywordarg
10487 objc-keywordarg-list objc-keywordarg
10489 objc-keywordarg:
10490 objc-selector : objc-keywordexpr
10491 : objc-keywordexpr
10494 static tree
10495 c_parser_objc_message_args (c_parser *parser)
10497 tree sel = c_parser_objc_selector (parser);
10498 tree list = NULL_TREE;
10499 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10500 return sel;
10501 while (true)
10503 tree keywordexpr;
10504 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10505 return error_mark_node;
10506 keywordexpr = c_parser_objc_keywordexpr (parser);
10507 list = chainon (list, build_tree_list (sel, keywordexpr));
10508 sel = c_parser_objc_selector (parser);
10509 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10510 break;
10512 return list;
10515 /* Parse an objc-keywordexpr.
10517 objc-keywordexpr:
10518 nonempty-expr-list
10521 static tree
10522 c_parser_objc_keywordexpr (c_parser *parser)
10524 tree ret;
10525 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10526 NULL, NULL, NULL, NULL);
10527 if (vec_safe_length (expr_list) == 1)
10529 /* Just return the expression, remove a level of
10530 indirection. */
10531 ret = (*expr_list)[0];
10533 else
10535 /* We have a comma expression, we will collapse later. */
10536 ret = build_tree_list_vec (expr_list);
10538 release_tree_vector (expr_list);
10539 return ret;
10542 /* A check, needed in several places, that ObjC interface, implementation or
10543 method definitions are not prefixed by incorrect items. */
10544 static bool
10545 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10546 struct c_declspecs *specs)
10548 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10549 || specs->typespec_kind != ctsk_none)
10551 c_parser_error (parser,
10552 "no type or storage class may be specified here,");
10553 c_parser_skip_to_end_of_block_or_statement (parser);
10554 return true;
10556 return false;
10559 /* Parse an Objective-C @property declaration. The syntax is:
10561 objc-property-declaration:
10562 '@property' objc-property-attributes[opt] struct-declaration ;
10564 objc-property-attributes:
10565 '(' objc-property-attribute-list ')'
10567 objc-property-attribute-list:
10568 objc-property-attribute
10569 objc-property-attribute-list, objc-property-attribute
10571 objc-property-attribute
10572 'getter' = identifier
10573 'setter' = identifier
10574 'readonly'
10575 'readwrite'
10576 'assign'
10577 'retain'
10578 'copy'
10579 'nonatomic'
10581 For example:
10582 @property NSString *name;
10583 @property (readonly) id object;
10584 @property (retain, nonatomic, getter=getTheName) id name;
10585 @property int a, b, c;
10587 PS: This function is identical to cp_parser_objc_at_propery_declaration
10588 for C++. Keep them in sync. */
10589 static void
10590 c_parser_objc_at_property_declaration (c_parser *parser)
10592 /* The following variables hold the attributes of the properties as
10593 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10594 seen. When we see an attribute, we set them to 'true' (if they
10595 are boolean properties) or to the identifier (if they have an
10596 argument, ie, for getter and setter). Note that here we only
10597 parse the list of attributes, check the syntax and accumulate the
10598 attributes that we find. objc_add_property_declaration() will
10599 then process the information. */
10600 bool property_assign = false;
10601 bool property_copy = false;
10602 tree property_getter_ident = NULL_TREE;
10603 bool property_nonatomic = false;
10604 bool property_readonly = false;
10605 bool property_readwrite = false;
10606 bool property_retain = false;
10607 tree property_setter_ident = NULL_TREE;
10609 /* 'properties' is the list of properties that we read. Usually a
10610 single one, but maybe more (eg, in "@property int a, b, c;" there
10611 are three). */
10612 tree properties;
10613 location_t loc;
10615 loc = c_parser_peek_token (parser)->location;
10616 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10618 c_parser_consume_token (parser); /* Eat '@property'. */
10620 /* Parse the optional attribute list... */
10621 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10623 matching_parens parens;
10625 /* Eat the '(' */
10626 parens.consume_open (parser);
10628 /* Property attribute keywords are valid now. */
10629 parser->objc_property_attr_context = true;
10631 while (true)
10633 bool syntax_error = false;
10634 c_token *token = c_parser_peek_token (parser);
10635 enum rid keyword;
10637 if (token->type != CPP_KEYWORD)
10639 if (token->type == CPP_CLOSE_PAREN)
10640 c_parser_error (parser, "expected identifier");
10641 else
10643 c_parser_consume_token (parser);
10644 c_parser_error (parser, "unknown property attribute");
10646 break;
10648 keyword = token->keyword;
10649 c_parser_consume_token (parser);
10650 switch (keyword)
10652 case RID_ASSIGN: property_assign = true; break;
10653 case RID_COPY: property_copy = true; break;
10654 case RID_NONATOMIC: property_nonatomic = true; break;
10655 case RID_READONLY: property_readonly = true; break;
10656 case RID_READWRITE: property_readwrite = true; break;
10657 case RID_RETAIN: property_retain = true; break;
10659 case RID_GETTER:
10660 case RID_SETTER:
10661 if (c_parser_next_token_is_not (parser, CPP_EQ))
10663 if (keyword == RID_GETTER)
10664 c_parser_error (parser,
10665 "missing %<=%> (after %<getter%> attribute)");
10666 else
10667 c_parser_error (parser,
10668 "missing %<=%> (after %<setter%> attribute)");
10669 syntax_error = true;
10670 break;
10672 c_parser_consume_token (parser); /* eat the = */
10673 if (c_parser_next_token_is_not (parser, CPP_NAME))
10675 c_parser_error (parser, "expected identifier");
10676 syntax_error = true;
10677 break;
10679 if (keyword == RID_SETTER)
10681 if (property_setter_ident != NULL_TREE)
10682 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10683 else
10684 property_setter_ident = c_parser_peek_token (parser)->value;
10685 c_parser_consume_token (parser);
10686 if (c_parser_next_token_is_not (parser, CPP_COLON))
10687 c_parser_error (parser, "setter name must terminate with %<:%>");
10688 else
10689 c_parser_consume_token (parser);
10691 else
10693 if (property_getter_ident != NULL_TREE)
10694 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10695 else
10696 property_getter_ident = c_parser_peek_token (parser)->value;
10697 c_parser_consume_token (parser);
10699 break;
10700 default:
10701 c_parser_error (parser, "unknown property attribute");
10702 syntax_error = true;
10703 break;
10706 if (syntax_error)
10707 break;
10709 if (c_parser_next_token_is (parser, CPP_COMMA))
10710 c_parser_consume_token (parser);
10711 else
10712 break;
10714 parser->objc_property_attr_context = false;
10715 parens.skip_until_found_close (parser);
10717 /* ... and the property declaration(s). */
10718 properties = c_parser_struct_declaration (parser);
10720 if (properties == error_mark_node)
10722 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10723 parser->error = false;
10724 return;
10727 if (properties == NULL_TREE)
10728 c_parser_error (parser, "expected identifier");
10729 else
10731 /* Comma-separated properties are chained together in
10732 reverse order; add them one by one. */
10733 properties = nreverse (properties);
10735 for (; properties; properties = TREE_CHAIN (properties))
10736 objc_add_property_declaration (loc, copy_node (properties),
10737 property_readonly, property_readwrite,
10738 property_assign, property_retain,
10739 property_copy, property_nonatomic,
10740 property_getter_ident, property_setter_ident);
10743 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10744 parser->error = false;
10747 /* Parse an Objective-C @synthesize declaration. The syntax is:
10749 objc-synthesize-declaration:
10750 @synthesize objc-synthesize-identifier-list ;
10752 objc-synthesize-identifier-list:
10753 objc-synthesize-identifier
10754 objc-synthesize-identifier-list, objc-synthesize-identifier
10756 objc-synthesize-identifier
10757 identifier
10758 identifier = identifier
10760 For example:
10761 @synthesize MyProperty;
10762 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10764 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10765 for C++. Keep them in sync.
10767 static void
10768 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10770 tree list = NULL_TREE;
10771 location_t loc;
10772 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10773 loc = c_parser_peek_token (parser)->location;
10775 c_parser_consume_token (parser);
10776 while (true)
10778 tree property, ivar;
10779 if (c_parser_next_token_is_not (parser, CPP_NAME))
10781 c_parser_error (parser, "expected identifier");
10782 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10783 /* Once we find the semicolon, we can resume normal parsing.
10784 We have to reset parser->error manually because
10785 c_parser_skip_until_found() won't reset it for us if the
10786 next token is precisely a semicolon. */
10787 parser->error = false;
10788 return;
10790 property = c_parser_peek_token (parser)->value;
10791 c_parser_consume_token (parser);
10792 if (c_parser_next_token_is (parser, CPP_EQ))
10794 c_parser_consume_token (parser);
10795 if (c_parser_next_token_is_not (parser, CPP_NAME))
10797 c_parser_error (parser, "expected identifier");
10798 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10799 parser->error = false;
10800 return;
10802 ivar = c_parser_peek_token (parser)->value;
10803 c_parser_consume_token (parser);
10805 else
10806 ivar = NULL_TREE;
10807 list = chainon (list, build_tree_list (ivar, property));
10808 if (c_parser_next_token_is (parser, CPP_COMMA))
10809 c_parser_consume_token (parser);
10810 else
10811 break;
10813 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10814 objc_add_synthesize_declaration (loc, list);
10817 /* Parse an Objective-C @dynamic declaration. The syntax is:
10819 objc-dynamic-declaration:
10820 @dynamic identifier-list ;
10822 For example:
10823 @dynamic MyProperty;
10824 @dynamic MyProperty, AnotherProperty;
10826 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10827 for C++. Keep them in sync.
10829 static void
10830 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10832 tree list = NULL_TREE;
10833 location_t loc;
10834 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10835 loc = c_parser_peek_token (parser)->location;
10837 c_parser_consume_token (parser);
10838 while (true)
10840 tree property;
10841 if (c_parser_next_token_is_not (parser, CPP_NAME))
10843 c_parser_error (parser, "expected identifier");
10844 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10845 parser->error = false;
10846 return;
10848 property = c_parser_peek_token (parser)->value;
10849 list = chainon (list, build_tree_list (NULL_TREE, property));
10850 c_parser_consume_token (parser);
10851 if (c_parser_next_token_is (parser, CPP_COMMA))
10852 c_parser_consume_token (parser);
10853 else
10854 break;
10856 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10857 objc_add_dynamic_declaration (loc, list);
10861 /* Parse a pragma GCC ivdep. */
10863 static bool
10864 c_parse_pragma_ivdep (c_parser *parser)
10866 c_parser_consume_pragma (parser);
10867 c_parser_skip_to_pragma_eol (parser);
10868 return true;
10871 /* Parse a pragma GCC unroll. */
10873 static unsigned short
10874 c_parser_pragma_unroll (c_parser *parser)
10876 unsigned short unroll;
10877 c_parser_consume_pragma (parser);
10878 location_t location = c_parser_peek_token (parser)->location;
10879 tree expr = c_parser_expr_no_commas (parser, NULL).value;
10880 mark_exp_read (expr);
10881 expr = c_fully_fold (expr, false, NULL);
10882 HOST_WIDE_INT lunroll = 0;
10883 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
10884 || TREE_CODE (expr) != INTEGER_CST
10885 || (lunroll = tree_to_shwi (expr)) < 0
10886 || lunroll >= USHRT_MAX)
10888 error_at (location, "%<#pragma GCC unroll%> requires an"
10889 " assignment-expression that evaluates to a non-negative"
10890 " integral constant less than %u", USHRT_MAX);
10891 unroll = 0;
10893 else
10895 unroll = (unsigned short)lunroll;
10896 if (unroll == 0)
10897 unroll = 1;
10900 c_parser_skip_to_pragma_eol (parser);
10901 return unroll;
10904 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10905 should be considered, statements. ALLOW_STMT is true if we're within
10906 the context of a function and such pragmas are to be allowed. Returns
10907 true if we actually parsed such a pragma. */
10909 static bool
10910 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10912 unsigned int id;
10913 const char *construct = NULL;
10915 id = c_parser_peek_token (parser)->pragma_kind;
10916 gcc_assert (id != PRAGMA_NONE);
10918 switch (id)
10920 case PRAGMA_OACC_DECLARE:
10921 c_parser_oacc_declare (parser);
10922 return false;
10924 case PRAGMA_OACC_ENTER_DATA:
10925 if (context != pragma_compound)
10927 construct = "acc enter data";
10928 in_compound:
10929 if (context == pragma_stmt)
10931 error_at (c_parser_peek_token (parser)->location,
10932 "%<#pragma %s%> may only be used in compound "
10933 "statements", construct);
10934 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10935 return false;
10937 goto bad_stmt;
10939 c_parser_oacc_enter_exit_data (parser, true);
10940 return false;
10942 case PRAGMA_OACC_EXIT_DATA:
10943 if (context != pragma_compound)
10945 construct = "acc exit data";
10946 goto in_compound;
10948 c_parser_oacc_enter_exit_data (parser, false);
10949 return false;
10951 case PRAGMA_OACC_ROUTINE:
10952 if (context != pragma_external)
10954 error_at (c_parser_peek_token (parser)->location,
10955 "%<#pragma acc routine%> must be at file scope");
10956 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10957 return false;
10959 c_parser_oacc_routine (parser, context);
10960 return false;
10962 case PRAGMA_OACC_UPDATE:
10963 if (context != pragma_compound)
10965 construct = "acc update";
10966 goto in_compound;
10968 c_parser_oacc_update (parser);
10969 return false;
10971 case PRAGMA_OMP_BARRIER:
10972 if (context != pragma_compound)
10974 construct = "omp barrier";
10975 goto in_compound;
10977 c_parser_omp_barrier (parser);
10978 return false;
10980 case PRAGMA_OMP_FLUSH:
10981 if (context != pragma_compound)
10983 construct = "omp flush";
10984 goto in_compound;
10986 c_parser_omp_flush (parser);
10987 return false;
10989 case PRAGMA_OMP_TASKWAIT:
10990 if (context != pragma_compound)
10992 construct = "omp taskwait";
10993 goto in_compound;
10995 c_parser_omp_taskwait (parser);
10996 return false;
10998 case PRAGMA_OMP_TASKYIELD:
10999 if (context != pragma_compound)
11001 construct = "omp taskyield";
11002 goto in_compound;
11004 c_parser_omp_taskyield (parser);
11005 return false;
11007 case PRAGMA_OMP_CANCEL:
11008 if (context != pragma_compound)
11010 construct = "omp cancel";
11011 goto in_compound;
11013 c_parser_omp_cancel (parser);
11014 return false;
11016 case PRAGMA_OMP_CANCELLATION_POINT:
11017 c_parser_omp_cancellation_point (parser, context);
11018 return false;
11020 case PRAGMA_OMP_THREADPRIVATE:
11021 c_parser_omp_threadprivate (parser);
11022 return false;
11024 case PRAGMA_OMP_TARGET:
11025 return c_parser_omp_target (parser, context, if_p);
11027 case PRAGMA_OMP_END_DECLARE_TARGET:
11028 c_parser_omp_end_declare_target (parser);
11029 return false;
11031 case PRAGMA_OMP_SECTION:
11032 error_at (c_parser_peek_token (parser)->location,
11033 "%<#pragma omp section%> may only be used in "
11034 "%<#pragma omp sections%> construct");
11035 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11036 return false;
11038 case PRAGMA_OMP_DECLARE:
11039 c_parser_omp_declare (parser, context);
11040 return false;
11042 case PRAGMA_OMP_ORDERED:
11043 return c_parser_omp_ordered (parser, context, if_p);
11045 case PRAGMA_IVDEP:
11047 const bool ivdep = c_parse_pragma_ivdep (parser);
11048 unsigned short unroll;
11049 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11050 unroll = c_parser_pragma_unroll (parser);
11051 else
11052 unroll = 0;
11053 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11054 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11055 && !c_parser_next_token_is_keyword (parser, RID_DO))
11057 c_parser_error (parser, "for, while or do statement expected");
11058 return false;
11060 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11061 c_parser_for_statement (parser, ivdep, unroll, if_p);
11062 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11063 c_parser_while_statement (parser, ivdep, unroll, if_p);
11064 else
11065 c_parser_do_statement (parser, ivdep, unroll);
11067 return false;
11069 case PRAGMA_UNROLL:
11071 unsigned short unroll = c_parser_pragma_unroll (parser);
11072 bool ivdep;
11073 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11074 ivdep = c_parse_pragma_ivdep (parser);
11075 else
11076 ivdep = false;
11077 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11078 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11079 && !c_parser_next_token_is_keyword (parser, RID_DO))
11081 c_parser_error (parser, "for, while or do statement expected");
11082 return false;
11084 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11085 c_parser_for_statement (parser, ivdep, unroll, if_p);
11086 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11087 c_parser_while_statement (parser, ivdep, unroll, if_p);
11088 else
11089 c_parser_do_statement (parser, ivdep, unroll);
11091 return false;
11093 case PRAGMA_GCC_PCH_PREPROCESS:
11094 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11095 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11096 return false;
11098 case PRAGMA_OACC_WAIT:
11099 if (context != pragma_compound)
11101 construct = "acc wait";
11102 goto in_compound;
11104 /* FALL THROUGH. */
11106 default:
11107 if (id < PRAGMA_FIRST_EXTERNAL)
11109 if (context != pragma_stmt && context != pragma_compound)
11111 bad_stmt:
11112 c_parser_error (parser, "expected declaration specifiers");
11113 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11114 return false;
11116 c_parser_omp_construct (parser, if_p);
11117 return true;
11119 break;
11122 c_parser_consume_pragma (parser);
11123 c_invoke_pragma_handler (id);
11125 /* Skip to EOL, but suppress any error message. Those will have been
11126 generated by the handler routine through calling error, as opposed
11127 to calling c_parser_error. */
11128 parser->error = true;
11129 c_parser_skip_to_pragma_eol (parser);
11131 return false;
11134 /* The interface the pragma parsers have to the lexer. */
11136 enum cpp_ttype
11137 pragma_lex (tree *value, location_t *loc)
11139 c_token *tok = c_parser_peek_token (the_parser);
11140 enum cpp_ttype ret = tok->type;
11142 *value = tok->value;
11143 if (loc)
11144 *loc = tok->location;
11146 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11147 ret = CPP_EOF;
11148 else
11150 if (ret == CPP_KEYWORD)
11151 ret = CPP_NAME;
11152 c_parser_consume_token (the_parser);
11155 return ret;
11158 static void
11159 c_parser_pragma_pch_preprocess (c_parser *parser)
11161 tree name = NULL;
11163 c_parser_consume_pragma (parser);
11164 if (c_parser_next_token_is (parser, CPP_STRING))
11166 name = c_parser_peek_token (parser)->value;
11167 c_parser_consume_token (parser);
11169 else
11170 c_parser_error (parser, "expected string literal");
11171 c_parser_skip_to_pragma_eol (parser);
11173 if (name)
11174 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11177 /* OpenACC and OpenMP parsing routines. */
11179 /* Returns name of the next clause.
11180 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11181 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11182 returned and the token is consumed. */
11184 static pragma_omp_clause
11185 c_parser_omp_clause_name (c_parser *parser)
11187 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11189 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11190 result = PRAGMA_OACC_CLAUSE_AUTO;
11191 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11192 result = PRAGMA_OMP_CLAUSE_IF;
11193 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11194 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11195 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11196 result = PRAGMA_OMP_CLAUSE_FOR;
11197 else if (c_parser_next_token_is (parser, CPP_NAME))
11199 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11201 switch (p[0])
11203 case 'a':
11204 if (!strcmp ("aligned", p))
11205 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11206 else if (!strcmp ("async", p))
11207 result = PRAGMA_OACC_CLAUSE_ASYNC;
11208 break;
11209 case 'c':
11210 if (!strcmp ("collapse", p))
11211 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11212 else if (!strcmp ("copy", p))
11213 result = PRAGMA_OACC_CLAUSE_COPY;
11214 else if (!strcmp ("copyin", p))
11215 result = PRAGMA_OMP_CLAUSE_COPYIN;
11216 else if (!strcmp ("copyout", p))
11217 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11218 else if (!strcmp ("copyprivate", p))
11219 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11220 else if (!strcmp ("create", p))
11221 result = PRAGMA_OACC_CLAUSE_CREATE;
11222 break;
11223 case 'd':
11224 if (!strcmp ("defaultmap", p))
11225 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11226 else if (!strcmp ("delete", p))
11227 result = PRAGMA_OACC_CLAUSE_DELETE;
11228 else if (!strcmp ("depend", p))
11229 result = PRAGMA_OMP_CLAUSE_DEPEND;
11230 else if (!strcmp ("device", p))
11231 result = PRAGMA_OMP_CLAUSE_DEVICE;
11232 else if (!strcmp ("deviceptr", p))
11233 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11234 else if (!strcmp ("device_resident", p))
11235 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11236 else if (!strcmp ("dist_schedule", p))
11237 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11238 break;
11239 case 'f':
11240 if (!strcmp ("final", p))
11241 result = PRAGMA_OMP_CLAUSE_FINAL;
11242 else if (!strcmp ("firstprivate", p))
11243 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11244 else if (!strcmp ("from", p))
11245 result = PRAGMA_OMP_CLAUSE_FROM;
11246 break;
11247 case 'g':
11248 if (!strcmp ("gang", p))
11249 result = PRAGMA_OACC_CLAUSE_GANG;
11250 else if (!strcmp ("grainsize", p))
11251 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11252 break;
11253 case 'h':
11254 if (!strcmp ("hint", p))
11255 result = PRAGMA_OMP_CLAUSE_HINT;
11256 else if (!strcmp ("host", p))
11257 result = PRAGMA_OACC_CLAUSE_HOST;
11258 break;
11259 case 'i':
11260 if (!strcmp ("inbranch", p))
11261 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11262 else if (!strcmp ("independent", p))
11263 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11264 else if (!strcmp ("is_device_ptr", p))
11265 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11266 break;
11267 case 'l':
11268 if (!strcmp ("lastprivate", p))
11269 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11270 else if (!strcmp ("linear", p))
11271 result = PRAGMA_OMP_CLAUSE_LINEAR;
11272 else if (!strcmp ("link", p))
11273 result = PRAGMA_OMP_CLAUSE_LINK;
11274 break;
11275 case 'm':
11276 if (!strcmp ("map", p))
11277 result = PRAGMA_OMP_CLAUSE_MAP;
11278 else if (!strcmp ("mergeable", p))
11279 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11280 break;
11281 case 'n':
11282 if (!strcmp ("nogroup", p))
11283 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11284 else if (!strcmp ("notinbranch", p))
11285 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11286 else if (!strcmp ("nowait", p))
11287 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11288 else if (!strcmp ("num_gangs", p))
11289 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11290 else if (!strcmp ("num_tasks", p))
11291 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11292 else if (!strcmp ("num_teams", p))
11293 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11294 else if (!strcmp ("num_threads", p))
11295 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11296 else if (!strcmp ("num_workers", p))
11297 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11298 break;
11299 case 'o':
11300 if (!strcmp ("ordered", p))
11301 result = PRAGMA_OMP_CLAUSE_ORDERED;
11302 break;
11303 case 'p':
11304 if (!strcmp ("parallel", p))
11305 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11306 else if (!strcmp ("present", p))
11307 result = PRAGMA_OACC_CLAUSE_PRESENT;
11308 else if (!strcmp ("present_or_copy", p)
11309 || !strcmp ("pcopy", p))
11310 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
11311 else if (!strcmp ("present_or_copyin", p)
11312 || !strcmp ("pcopyin", p))
11313 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
11314 else if (!strcmp ("present_or_copyout", p)
11315 || !strcmp ("pcopyout", p))
11316 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
11317 else if (!strcmp ("present_or_create", p)
11318 || !strcmp ("pcreate", p))
11319 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
11320 else if (!strcmp ("priority", p))
11321 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11322 else if (!strcmp ("private", p))
11323 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11324 else if (!strcmp ("proc_bind", p))
11325 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11326 break;
11327 case 'r':
11328 if (!strcmp ("reduction", p))
11329 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11330 break;
11331 case 's':
11332 if (!strcmp ("safelen", p))
11333 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11334 else if (!strcmp ("schedule", p))
11335 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11336 else if (!strcmp ("sections", p))
11337 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11338 else if (!strcmp ("seq", p))
11339 result = PRAGMA_OACC_CLAUSE_SEQ;
11340 else if (!strcmp ("shared", p))
11341 result = PRAGMA_OMP_CLAUSE_SHARED;
11342 else if (!strcmp ("simd", p))
11343 result = PRAGMA_OMP_CLAUSE_SIMD;
11344 else if (!strcmp ("simdlen", p))
11345 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11346 else if (!strcmp ("self", p))
11347 result = PRAGMA_OACC_CLAUSE_SELF;
11348 break;
11349 case 't':
11350 if (!strcmp ("taskgroup", p))
11351 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11352 else if (!strcmp ("thread_limit", p))
11353 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11354 else if (!strcmp ("threads", p))
11355 result = PRAGMA_OMP_CLAUSE_THREADS;
11356 else if (!strcmp ("tile", p))
11357 result = PRAGMA_OACC_CLAUSE_TILE;
11358 else if (!strcmp ("to", p))
11359 result = PRAGMA_OMP_CLAUSE_TO;
11360 break;
11361 case 'u':
11362 if (!strcmp ("uniform", p))
11363 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11364 else if (!strcmp ("untied", p))
11365 result = PRAGMA_OMP_CLAUSE_UNTIED;
11366 else if (!strcmp ("use_device", p))
11367 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11368 else if (!strcmp ("use_device_ptr", p))
11369 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11370 break;
11371 case 'v':
11372 if (!strcmp ("vector", p))
11373 result = PRAGMA_OACC_CLAUSE_VECTOR;
11374 else if (!strcmp ("vector_length", p))
11375 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11376 break;
11377 case 'w':
11378 if (!strcmp ("wait", p))
11379 result = PRAGMA_OACC_CLAUSE_WAIT;
11380 else if (!strcmp ("worker", p))
11381 result = PRAGMA_OACC_CLAUSE_WORKER;
11382 break;
11386 if (result != PRAGMA_OMP_CLAUSE_NONE)
11387 c_parser_consume_token (parser);
11389 return result;
11392 /* Validate that a clause of the given type does not already exist. */
11394 static void
11395 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11396 const char *name)
11398 tree c;
11400 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11401 if (OMP_CLAUSE_CODE (c) == code)
11403 location_t loc = OMP_CLAUSE_LOCATION (c);
11404 error_at (loc, "too many %qs clauses", name);
11405 break;
11409 /* OpenACC 2.0
11410 Parse wait clause or wait directive parameters. */
11412 static tree
11413 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11415 vec<tree, va_gc> *args;
11416 tree t, args_tree;
11418 matching_parens parens;
11419 if (!parens.require_open (parser))
11420 return list;
11422 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11424 if (args->length () == 0)
11426 c_parser_error (parser, "expected integer expression before ')'");
11427 release_tree_vector (args);
11428 return list;
11431 args_tree = build_tree_list_vec (args);
11433 for (t = args_tree; t; t = TREE_CHAIN (t))
11435 tree targ = TREE_VALUE (t);
11437 if (targ != error_mark_node)
11439 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11441 c_parser_error (parser, "expression must be integral");
11442 targ = error_mark_node;
11444 else
11446 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11448 OMP_CLAUSE_DECL (c) = targ;
11449 OMP_CLAUSE_CHAIN (c) = list;
11450 list = c;
11455 release_tree_vector (args);
11456 parens.require_close (parser);
11457 return list;
11460 /* OpenACC 2.0, OpenMP 2.5:
11461 variable-list:
11462 identifier
11463 variable-list , identifier
11465 If KIND is nonzero, create the appropriate node and install the
11466 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11467 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11469 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11470 return the list created. */
11472 static tree
11473 c_parser_omp_variable_list (c_parser *parser,
11474 location_t clause_loc,
11475 enum omp_clause_code kind, tree list)
11477 if (c_parser_next_token_is_not (parser, CPP_NAME)
11478 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11479 c_parser_error (parser, "expected identifier");
11481 while (c_parser_next_token_is (parser, CPP_NAME)
11482 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11484 tree t = lookup_name (c_parser_peek_token (parser)->value);
11486 if (t == NULL_TREE)
11488 undeclared_variable (c_parser_peek_token (parser)->location,
11489 c_parser_peek_token (parser)->value);
11490 t = error_mark_node;
11493 c_parser_consume_token (parser);
11495 if (t == error_mark_node)
11497 else if (kind != 0)
11499 switch (kind)
11501 case OMP_CLAUSE__CACHE_:
11502 /* The OpenACC cache directive explicitly only allows "array
11503 elements or subarrays". */
11504 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11506 c_parser_error (parser, "expected %<[%>");
11507 t = error_mark_node;
11508 break;
11510 /* FALLTHROUGH */
11511 case OMP_CLAUSE_MAP:
11512 case OMP_CLAUSE_FROM:
11513 case OMP_CLAUSE_TO:
11514 while (c_parser_next_token_is (parser, CPP_DOT))
11516 location_t op_loc = c_parser_peek_token (parser)->location;
11517 c_parser_consume_token (parser);
11518 if (!c_parser_next_token_is (parser, CPP_NAME))
11520 c_parser_error (parser, "expected identifier");
11521 t = error_mark_node;
11522 break;
11525 c_token *comp_tok = c_parser_peek_token (parser);
11526 tree ident = comp_tok->value;
11527 location_t comp_loc = comp_tok->location;
11528 c_parser_consume_token (parser);
11529 t = build_component_ref (op_loc, t, ident, comp_loc);
11531 /* FALLTHROUGH */
11532 case OMP_CLAUSE_DEPEND:
11533 case OMP_CLAUSE_REDUCTION:
11534 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11536 tree low_bound = NULL_TREE, length = NULL_TREE;
11538 c_parser_consume_token (parser);
11539 if (!c_parser_next_token_is (parser, CPP_COLON))
11541 location_t expr_loc
11542 = c_parser_peek_token (parser)->location;
11543 c_expr expr = c_parser_expression (parser);
11544 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11545 false, true);
11546 low_bound = expr.value;
11548 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11549 length = integer_one_node;
11550 else
11552 /* Look for `:'. */
11553 if (!c_parser_require (parser, CPP_COLON,
11554 "expected %<:%>"))
11556 t = error_mark_node;
11557 break;
11559 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11561 location_t expr_loc
11562 = c_parser_peek_token (parser)->location;
11563 c_expr expr = c_parser_expression (parser);
11564 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11565 false, true);
11566 length = expr.value;
11569 /* Look for the closing `]'. */
11570 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11571 "expected %<]%>"))
11573 t = error_mark_node;
11574 break;
11577 t = tree_cons (low_bound, length, t);
11579 break;
11580 default:
11581 break;
11584 if (t != error_mark_node)
11586 tree u = build_omp_clause (clause_loc, kind);
11587 OMP_CLAUSE_DECL (u) = t;
11588 OMP_CLAUSE_CHAIN (u) = list;
11589 list = u;
11592 else
11593 list = tree_cons (t, NULL_TREE, list);
11595 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11596 break;
11598 c_parser_consume_token (parser);
11601 return list;
11604 /* Similarly, but expect leading and trailing parenthesis. This is a very
11605 common case for OpenACC and OpenMP clauses. */
11607 static tree
11608 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11609 tree list)
11611 /* The clauses location. */
11612 location_t loc = c_parser_peek_token (parser)->location;
11614 matching_parens parens;
11615 if (parens.require_open (parser))
11617 list = c_parser_omp_variable_list (parser, loc, kind, list);
11618 parens.skip_until_found_close (parser);
11620 return list;
11623 /* OpenACC 2.0:
11624 copy ( variable-list )
11625 copyin ( variable-list )
11626 copyout ( variable-list )
11627 create ( variable-list )
11628 delete ( variable-list )
11629 present ( variable-list )
11630 present_or_copy ( variable-list )
11631 pcopy ( variable-list )
11632 present_or_copyin ( variable-list )
11633 pcopyin ( variable-list )
11634 present_or_copyout ( variable-list )
11635 pcopyout ( variable-list )
11636 present_or_create ( variable-list )
11637 pcreate ( variable-list ) */
11639 static tree
11640 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11641 tree list)
11643 enum gomp_map_kind kind;
11644 switch (c_kind)
11646 case PRAGMA_OACC_CLAUSE_COPY:
11647 kind = GOMP_MAP_FORCE_TOFROM;
11648 break;
11649 case PRAGMA_OACC_CLAUSE_COPYIN:
11650 kind = GOMP_MAP_FORCE_TO;
11651 break;
11652 case PRAGMA_OACC_CLAUSE_COPYOUT:
11653 kind = GOMP_MAP_FORCE_FROM;
11654 break;
11655 case PRAGMA_OACC_CLAUSE_CREATE:
11656 kind = GOMP_MAP_FORCE_ALLOC;
11657 break;
11658 case PRAGMA_OACC_CLAUSE_DELETE:
11659 kind = GOMP_MAP_DELETE;
11660 break;
11661 case PRAGMA_OACC_CLAUSE_DEVICE:
11662 kind = GOMP_MAP_FORCE_TO;
11663 break;
11664 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11665 kind = GOMP_MAP_DEVICE_RESIDENT;
11666 break;
11667 case PRAGMA_OACC_CLAUSE_HOST:
11668 case PRAGMA_OACC_CLAUSE_SELF:
11669 kind = GOMP_MAP_FORCE_FROM;
11670 break;
11671 case PRAGMA_OACC_CLAUSE_LINK:
11672 kind = GOMP_MAP_LINK;
11673 break;
11674 case PRAGMA_OACC_CLAUSE_PRESENT:
11675 kind = GOMP_MAP_FORCE_PRESENT;
11676 break;
11677 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11678 kind = GOMP_MAP_TOFROM;
11679 break;
11680 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11681 kind = GOMP_MAP_TO;
11682 break;
11683 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11684 kind = GOMP_MAP_FROM;
11685 break;
11686 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11687 kind = GOMP_MAP_ALLOC;
11688 break;
11689 default:
11690 gcc_unreachable ();
11692 tree nl, c;
11693 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11695 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11696 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11698 return nl;
11701 /* OpenACC 2.0:
11702 deviceptr ( variable-list ) */
11704 static tree
11705 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11707 location_t loc = c_parser_peek_token (parser)->location;
11708 tree vars, t;
11710 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11711 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11712 variable-list must only allow for pointer variables. */
11713 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11714 for (t = vars; t && t; t = TREE_CHAIN (t))
11716 tree v = TREE_PURPOSE (t);
11718 /* FIXME diagnostics: Ideally we should keep individual
11719 locations for all the variables in the var list to make the
11720 following errors more precise. Perhaps
11721 c_parser_omp_var_list_parens() should construct a list of
11722 locations to go along with the var list. */
11724 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11725 error_at (loc, "%qD is not a variable", v);
11726 else if (TREE_TYPE (v) == error_mark_node)
11728 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11729 error_at (loc, "%qD is not a pointer variable", v);
11731 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11732 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11733 OMP_CLAUSE_DECL (u) = v;
11734 OMP_CLAUSE_CHAIN (u) = list;
11735 list = u;
11738 return list;
11741 /* OpenACC 2.0, OpenMP 3.0:
11742 collapse ( constant-expression ) */
11744 static tree
11745 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11747 tree c, num = error_mark_node;
11748 HOST_WIDE_INT n;
11749 location_t loc;
11751 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11752 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11754 loc = c_parser_peek_token (parser)->location;
11755 matching_parens parens;
11756 if (parens.require_open (parser))
11758 num = c_parser_expr_no_commas (parser, NULL).value;
11759 parens.skip_until_found_close (parser);
11761 if (num == error_mark_node)
11762 return list;
11763 mark_exp_read (num);
11764 num = c_fully_fold (num, false, NULL);
11765 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11766 || !tree_fits_shwi_p (num)
11767 || (n = tree_to_shwi (num)) <= 0
11768 || (int) n != n)
11770 error_at (loc,
11771 "collapse argument needs positive constant integer expression");
11772 return list;
11774 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11775 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11776 OMP_CLAUSE_CHAIN (c) = list;
11777 return c;
11780 /* OpenMP 2.5:
11781 copyin ( variable-list ) */
11783 static tree
11784 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11786 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11789 /* OpenMP 2.5:
11790 copyprivate ( variable-list ) */
11792 static tree
11793 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11795 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11798 /* OpenMP 2.5:
11799 default ( none | shared )
11801 OpenACC:
11802 default ( none | present ) */
11804 static tree
11805 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11807 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11808 location_t loc = c_parser_peek_token (parser)->location;
11809 tree c;
11811 matching_parens parens;
11812 if (!parens.require_open (parser))
11813 return list;
11814 if (c_parser_next_token_is (parser, CPP_NAME))
11816 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11818 switch (p[0])
11820 case 'n':
11821 if (strcmp ("none", p) != 0)
11822 goto invalid_kind;
11823 kind = OMP_CLAUSE_DEFAULT_NONE;
11824 break;
11826 case 'p':
11827 if (strcmp ("present", p) != 0 || !is_oacc)
11828 goto invalid_kind;
11829 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11830 break;
11832 case 's':
11833 if (strcmp ("shared", p) != 0 || is_oacc)
11834 goto invalid_kind;
11835 kind = OMP_CLAUSE_DEFAULT_SHARED;
11836 break;
11838 default:
11839 goto invalid_kind;
11842 c_parser_consume_token (parser);
11844 else
11846 invalid_kind:
11847 if (is_oacc)
11848 c_parser_error (parser, "expected %<none%> or %<present%>");
11849 else
11850 c_parser_error (parser, "expected %<none%> or %<shared%>");
11852 parens.skip_until_found_close (parser);
11854 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11855 return list;
11857 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11858 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11859 OMP_CLAUSE_CHAIN (c) = list;
11860 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11862 return c;
11865 /* OpenMP 2.5:
11866 firstprivate ( variable-list ) */
11868 static tree
11869 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11871 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11874 /* OpenMP 3.1:
11875 final ( expression ) */
11877 static tree
11878 c_parser_omp_clause_final (c_parser *parser, tree list)
11880 location_t loc = c_parser_peek_token (parser)->location;
11881 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11883 tree t = c_parser_paren_condition (parser);
11884 tree c;
11886 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11888 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11889 OMP_CLAUSE_FINAL_EXPR (c) = t;
11890 OMP_CLAUSE_CHAIN (c) = list;
11891 list = c;
11893 else
11894 c_parser_error (parser, "expected %<(%>");
11896 return list;
11899 /* OpenACC, OpenMP 2.5:
11900 if ( expression )
11902 OpenMP 4.5:
11903 if ( directive-name-modifier : expression )
11905 directive-name-modifier:
11906 parallel | task | taskloop | target data | target | target update
11907 | target enter data | target exit data */
11909 static tree
11910 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11912 location_t location = c_parser_peek_token (parser)->location;
11913 enum tree_code if_modifier = ERROR_MARK;
11915 matching_parens parens;
11916 if (!parens.require_open (parser))
11917 return list;
11919 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11921 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11922 int n = 2;
11923 if (strcmp (p, "parallel") == 0)
11924 if_modifier = OMP_PARALLEL;
11925 else if (strcmp (p, "task") == 0)
11926 if_modifier = OMP_TASK;
11927 else if (strcmp (p, "taskloop") == 0)
11928 if_modifier = OMP_TASKLOOP;
11929 else if (strcmp (p, "target") == 0)
11931 if_modifier = OMP_TARGET;
11932 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11934 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11935 if (strcmp ("data", p) == 0)
11936 if_modifier = OMP_TARGET_DATA;
11937 else if (strcmp ("update", p) == 0)
11938 if_modifier = OMP_TARGET_UPDATE;
11939 else if (strcmp ("enter", p) == 0)
11940 if_modifier = OMP_TARGET_ENTER_DATA;
11941 else if (strcmp ("exit", p) == 0)
11942 if_modifier = OMP_TARGET_EXIT_DATA;
11943 if (if_modifier != OMP_TARGET)
11945 n = 3;
11946 c_parser_consume_token (parser);
11948 else
11950 location_t loc = c_parser_peek_2nd_token (parser)->location;
11951 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11952 "or %<exit%>");
11953 if_modifier = ERROR_MARK;
11955 if (if_modifier == OMP_TARGET_ENTER_DATA
11956 || if_modifier == OMP_TARGET_EXIT_DATA)
11958 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11960 p = IDENTIFIER_POINTER
11961 (c_parser_peek_2nd_token (parser)->value);
11962 if (strcmp ("data", p) == 0)
11963 n = 4;
11965 if (n == 4)
11966 c_parser_consume_token (parser);
11967 else
11969 location_t loc
11970 = c_parser_peek_2nd_token (parser)->location;
11971 error_at (loc, "expected %<data%>");
11972 if_modifier = ERROR_MARK;
11977 if (if_modifier != ERROR_MARK)
11979 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11981 c_parser_consume_token (parser);
11982 c_parser_consume_token (parser);
11984 else
11986 if (n > 2)
11988 location_t loc = c_parser_peek_2nd_token (parser)->location;
11989 error_at (loc, "expected %<:%>");
11991 if_modifier = ERROR_MARK;
11996 tree t = c_parser_condition (parser), c;
11997 parens.skip_until_found_close (parser);
11999 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12000 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12002 if (if_modifier != ERROR_MARK
12003 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12005 const char *p = NULL;
12006 switch (if_modifier)
12008 case OMP_PARALLEL: p = "parallel"; break;
12009 case OMP_TASK: p = "task"; break;
12010 case OMP_TASKLOOP: p = "taskloop"; break;
12011 case OMP_TARGET_DATA: p = "target data"; break;
12012 case OMP_TARGET: p = "target"; break;
12013 case OMP_TARGET_UPDATE: p = "target update"; break;
12014 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12015 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12016 default: gcc_unreachable ();
12018 error_at (location, "too many %<if%> clauses with %qs modifier",
12020 return list;
12022 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12024 if (!is_omp)
12025 error_at (location, "too many %<if%> clauses");
12026 else
12027 error_at (location, "too many %<if%> clauses without modifier");
12028 return list;
12030 else if (if_modifier == ERROR_MARK
12031 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12033 error_at (location, "if any %<if%> clause has modifier, then all "
12034 "%<if%> clauses have to use modifier");
12035 return list;
12039 c = build_omp_clause (location, OMP_CLAUSE_IF);
12040 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12041 OMP_CLAUSE_IF_EXPR (c) = t;
12042 OMP_CLAUSE_CHAIN (c) = list;
12043 return c;
12046 /* OpenMP 2.5:
12047 lastprivate ( variable-list ) */
12049 static tree
12050 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12052 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12055 /* OpenMP 3.1:
12056 mergeable */
12058 static tree
12059 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12061 tree c;
12063 /* FIXME: Should we allow duplicates? */
12064 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12066 c = build_omp_clause (c_parser_peek_token (parser)->location,
12067 OMP_CLAUSE_MERGEABLE);
12068 OMP_CLAUSE_CHAIN (c) = list;
12070 return c;
12073 /* OpenMP 2.5:
12074 nowait */
12076 static tree
12077 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12079 tree c;
12080 location_t loc = c_parser_peek_token (parser)->location;
12082 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12084 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12085 OMP_CLAUSE_CHAIN (c) = list;
12086 return c;
12089 /* OpenMP 2.5:
12090 num_threads ( expression ) */
12092 static tree
12093 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12095 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12096 matching_parens parens;
12097 if (parens.require_open (parser))
12099 location_t expr_loc = c_parser_peek_token (parser)->location;
12100 c_expr expr = c_parser_expression (parser);
12101 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12102 tree c, t = expr.value;
12103 t = c_fully_fold (t, false, NULL);
12105 parens.skip_until_found_close (parser);
12107 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12109 c_parser_error (parser, "expected integer expression");
12110 return list;
12113 /* Attempt to statically determine when the number isn't positive. */
12114 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12115 build_int_cst (TREE_TYPE (t), 0));
12116 protected_set_expr_location (c, expr_loc);
12117 if (c == boolean_true_node)
12119 warning_at (expr_loc, 0,
12120 "%<num_threads%> value must be positive");
12121 t = integer_one_node;
12124 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12126 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12127 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12128 OMP_CLAUSE_CHAIN (c) = list;
12129 list = c;
12132 return list;
12135 /* OpenMP 4.5:
12136 num_tasks ( expression ) */
12138 static tree
12139 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12141 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12142 matching_parens parens;
12143 if (parens.require_open (parser))
12145 location_t expr_loc = c_parser_peek_token (parser)->location;
12146 c_expr expr = c_parser_expression (parser);
12147 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12148 tree c, t = expr.value;
12149 t = c_fully_fold (t, false, NULL);
12151 parens.skip_until_found_close (parser);
12153 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12155 c_parser_error (parser, "expected integer expression");
12156 return list;
12159 /* Attempt to statically determine when the number isn't positive. */
12160 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12161 build_int_cst (TREE_TYPE (t), 0));
12162 if (CAN_HAVE_LOCATION_P (c))
12163 SET_EXPR_LOCATION (c, expr_loc);
12164 if (c == boolean_true_node)
12166 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12167 t = integer_one_node;
12170 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12172 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12173 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12174 OMP_CLAUSE_CHAIN (c) = list;
12175 list = c;
12178 return list;
12181 /* OpenMP 4.5:
12182 grainsize ( expression ) */
12184 static tree
12185 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12187 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12188 matching_parens parens;
12189 if (parens.require_open (parser))
12191 location_t expr_loc = c_parser_peek_token (parser)->location;
12192 c_expr expr = c_parser_expression (parser);
12193 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12194 tree c, t = expr.value;
12195 t = c_fully_fold (t, false, NULL);
12197 parens.skip_until_found_close (parser);
12199 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12201 c_parser_error (parser, "expected integer expression");
12202 return list;
12205 /* Attempt to statically determine when the number isn't positive. */
12206 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12207 build_int_cst (TREE_TYPE (t), 0));
12208 if (CAN_HAVE_LOCATION_P (c))
12209 SET_EXPR_LOCATION (c, expr_loc);
12210 if (c == boolean_true_node)
12212 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12213 t = integer_one_node;
12216 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12218 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12219 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12220 OMP_CLAUSE_CHAIN (c) = list;
12221 list = c;
12224 return list;
12227 /* OpenMP 4.5:
12228 priority ( expression ) */
12230 static tree
12231 c_parser_omp_clause_priority (c_parser *parser, tree list)
12233 location_t priority_loc = c_parser_peek_token (parser)->location;
12234 matching_parens parens;
12235 if (parens.require_open (parser))
12237 location_t expr_loc = c_parser_peek_token (parser)->location;
12238 c_expr expr = c_parser_expression (parser);
12239 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12240 tree c, t = expr.value;
12241 t = c_fully_fold (t, false, NULL);
12243 parens.skip_until_found_close (parser);
12245 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12247 c_parser_error (parser, "expected integer expression");
12248 return list;
12251 /* Attempt to statically determine when the number isn't
12252 non-negative. */
12253 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12254 build_int_cst (TREE_TYPE (t), 0));
12255 if (CAN_HAVE_LOCATION_P (c))
12256 SET_EXPR_LOCATION (c, expr_loc);
12257 if (c == boolean_true_node)
12259 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12260 t = integer_one_node;
12263 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12265 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12266 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12267 OMP_CLAUSE_CHAIN (c) = list;
12268 list = c;
12271 return list;
12274 /* OpenMP 4.5:
12275 hint ( expression ) */
12277 static tree
12278 c_parser_omp_clause_hint (c_parser *parser, tree list)
12280 location_t hint_loc = c_parser_peek_token (parser)->location;
12281 matching_parens parens;
12282 if (parens.require_open (parser))
12284 location_t expr_loc = c_parser_peek_token (parser)->location;
12285 c_expr expr = c_parser_expression (parser);
12286 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12287 tree c, t = expr.value;
12288 t = c_fully_fold (t, false, NULL);
12290 parens.skip_until_found_close (parser);
12292 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12294 c_parser_error (parser, "expected integer expression");
12295 return list;
12298 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12300 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12301 OMP_CLAUSE_HINT_EXPR (c) = t;
12302 OMP_CLAUSE_CHAIN (c) = list;
12303 list = c;
12306 return list;
12309 /* OpenMP 4.5:
12310 defaultmap ( tofrom : scalar ) */
12312 static tree
12313 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12315 location_t loc = c_parser_peek_token (parser)->location;
12316 tree c;
12317 const char *p;
12319 matching_parens parens;
12320 if (!parens.require_open (parser))
12321 return list;
12322 if (!c_parser_next_token_is (parser, CPP_NAME))
12324 c_parser_error (parser, "expected %<tofrom%>");
12325 goto out_err;
12327 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12328 if (strcmp (p, "tofrom") != 0)
12330 c_parser_error (parser, "expected %<tofrom%>");
12331 goto out_err;
12333 c_parser_consume_token (parser);
12334 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12335 goto out_err;
12336 if (!c_parser_next_token_is (parser, CPP_NAME))
12338 c_parser_error (parser, "expected %<scalar%>");
12339 goto out_err;
12341 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12342 if (strcmp (p, "scalar") != 0)
12344 c_parser_error (parser, "expected %<scalar%>");
12345 goto out_err;
12347 c_parser_consume_token (parser);
12348 parens.skip_until_found_close (parser);
12349 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12350 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12351 OMP_CLAUSE_CHAIN (c) = list;
12352 return c;
12354 out_err:
12355 parens.skip_until_found_close (parser);
12356 return list;
12359 /* OpenACC 2.0:
12360 use_device ( variable-list )
12362 OpenMP 4.5:
12363 use_device_ptr ( variable-list ) */
12365 static tree
12366 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12368 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12369 list);
12372 /* OpenMP 4.5:
12373 is_device_ptr ( variable-list ) */
12375 static tree
12376 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12378 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12381 /* OpenACC:
12382 num_gangs ( expression )
12383 num_workers ( expression )
12384 vector_length ( expression ) */
12386 static tree
12387 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12388 tree list)
12390 location_t loc = c_parser_peek_token (parser)->location;
12392 matching_parens parens;
12393 if (!parens.require_open (parser))
12394 return list;
12396 location_t expr_loc = c_parser_peek_token (parser)->location;
12397 c_expr expr = c_parser_expression (parser);
12398 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12399 tree c, t = expr.value;
12400 t = c_fully_fold (t, false, NULL);
12402 parens.skip_until_found_close (parser);
12404 if (t == error_mark_node)
12405 return list;
12406 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12408 error_at (expr_loc, "%qs expression must be integral",
12409 omp_clause_code_name[code]);
12410 return list;
12413 /* Attempt to statically determine when the number isn't positive. */
12414 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12415 build_int_cst (TREE_TYPE (t), 0));
12416 protected_set_expr_location (c, expr_loc);
12417 if (c == boolean_true_node)
12419 warning_at (expr_loc, 0,
12420 "%qs value must be positive",
12421 omp_clause_code_name[code]);
12422 t = integer_one_node;
12425 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12427 c = build_omp_clause (loc, code);
12428 OMP_CLAUSE_OPERAND (c, 0) = t;
12429 OMP_CLAUSE_CHAIN (c) = list;
12430 return c;
12433 /* OpenACC:
12435 gang [( gang-arg-list )]
12436 worker [( [num:] int-expr )]
12437 vector [( [length:] int-expr )]
12439 where gang-arg is one of:
12441 [num:] int-expr
12442 static: size-expr
12444 and size-expr may be:
12447 int-expr
12450 static tree
12451 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12452 const char *str, tree list)
12454 const char *id = "num";
12455 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12456 location_t loc = c_parser_peek_token (parser)->location;
12458 if (kind == OMP_CLAUSE_VECTOR)
12459 id = "length";
12461 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12463 c_parser_consume_token (parser);
12467 c_token *next = c_parser_peek_token (parser);
12468 int idx = 0;
12470 /* Gang static argument. */
12471 if (kind == OMP_CLAUSE_GANG
12472 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12474 c_parser_consume_token (parser);
12476 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12477 goto cleanup_error;
12479 idx = 1;
12480 if (ops[idx] != NULL_TREE)
12482 c_parser_error (parser, "too many %<static%> arguments");
12483 goto cleanup_error;
12486 /* Check for the '*' argument. */
12487 if (c_parser_next_token_is (parser, CPP_MULT)
12488 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12489 || c_parser_peek_2nd_token (parser)->type
12490 == CPP_CLOSE_PAREN))
12492 c_parser_consume_token (parser);
12493 ops[idx] = integer_minus_one_node;
12495 if (c_parser_next_token_is (parser, CPP_COMMA))
12497 c_parser_consume_token (parser);
12498 continue;
12500 else
12501 break;
12504 /* Worker num: argument and vector length: arguments. */
12505 else if (c_parser_next_token_is (parser, CPP_NAME)
12506 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12507 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12509 c_parser_consume_token (parser); /* id */
12510 c_parser_consume_token (parser); /* ':' */
12513 /* Now collect the actual argument. */
12514 if (ops[idx] != NULL_TREE)
12516 c_parser_error (parser, "unexpected argument");
12517 goto cleanup_error;
12520 location_t expr_loc = c_parser_peek_token (parser)->location;
12521 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12522 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12523 tree expr = cexpr.value;
12524 if (expr == error_mark_node)
12525 goto cleanup_error;
12527 expr = c_fully_fold (expr, false, NULL);
12529 /* Attempt to statically determine when the number isn't a
12530 positive integer. */
12532 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12534 c_parser_error (parser, "expected integer expression");
12535 return list;
12538 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12539 build_int_cst (TREE_TYPE (expr), 0));
12540 if (c == boolean_true_node)
12542 warning_at (loc, 0,
12543 "%qs value must be positive", str);
12544 expr = integer_one_node;
12547 ops[idx] = expr;
12549 if (kind == OMP_CLAUSE_GANG
12550 && c_parser_next_token_is (parser, CPP_COMMA))
12552 c_parser_consume_token (parser);
12553 continue;
12555 break;
12557 while (1);
12559 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12560 goto cleanup_error;
12563 check_no_duplicate_clause (list, kind, str);
12565 c = build_omp_clause (loc, kind);
12567 if (ops[1])
12568 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12570 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12571 OMP_CLAUSE_CHAIN (c) = list;
12573 return c;
12575 cleanup_error:
12576 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12577 return list;
12580 /* OpenACC:
12581 auto
12582 independent
12583 nohost
12584 seq */
12586 static tree
12587 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12588 tree list)
12590 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12592 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12593 OMP_CLAUSE_CHAIN (c) = list;
12595 return c;
12598 /* OpenACC:
12599 async [( int-expr )] */
12601 static tree
12602 c_parser_oacc_clause_async (c_parser *parser, tree list)
12604 tree c, t;
12605 location_t loc = c_parser_peek_token (parser)->location;
12607 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12609 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12611 c_parser_consume_token (parser);
12613 t = c_parser_expression (parser).value;
12614 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12615 c_parser_error (parser, "expected integer expression");
12616 else if (t == error_mark_node
12617 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12618 return list;
12620 else
12621 t = c_fully_fold (t, false, NULL);
12623 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12625 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12626 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12627 OMP_CLAUSE_CHAIN (c) = list;
12628 list = c;
12630 return list;
12633 /* OpenACC 2.0:
12634 tile ( size-expr-list ) */
12636 static tree
12637 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12639 tree c, expr = error_mark_node;
12640 location_t loc;
12641 tree tile = NULL_TREE;
12643 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12644 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12646 loc = c_parser_peek_token (parser)->location;
12647 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12648 return list;
12652 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12653 return list;
12655 if (c_parser_next_token_is (parser, CPP_MULT)
12656 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12657 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12659 c_parser_consume_token (parser);
12660 expr = integer_zero_node;
12662 else
12664 location_t expr_loc = c_parser_peek_token (parser)->location;
12665 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12666 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12667 expr = cexpr.value;
12669 if (expr == error_mark_node)
12671 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12672 "expected %<)%>");
12673 return list;
12676 expr = c_fully_fold (expr, false, NULL);
12678 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12679 || !tree_fits_shwi_p (expr)
12680 || tree_to_shwi (expr) <= 0)
12682 error_at (expr_loc, "%<tile%> argument needs positive"
12683 " integral constant");
12684 expr = integer_zero_node;
12688 tile = tree_cons (NULL_TREE, expr, tile);
12690 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12692 /* Consume the trailing ')'. */
12693 c_parser_consume_token (parser);
12695 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12696 tile = nreverse (tile);
12697 OMP_CLAUSE_TILE_LIST (c) = tile;
12698 OMP_CLAUSE_CHAIN (c) = list;
12699 return c;
12702 /* OpenACC:
12703 wait ( int-expr-list ) */
12705 static tree
12706 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12708 location_t clause_loc = c_parser_peek_token (parser)->location;
12710 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12711 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12713 return list;
12716 /* OpenMP 2.5:
12717 ordered
12719 OpenMP 4.5:
12720 ordered ( constant-expression ) */
12722 static tree
12723 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12725 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12727 tree c, num = NULL_TREE;
12728 HOST_WIDE_INT n;
12729 location_t loc = c_parser_peek_token (parser)->location;
12730 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12732 matching_parens parens;
12733 parens.consume_open (parser);
12734 num = c_parser_expr_no_commas (parser, NULL).value;
12735 parens.skip_until_found_close (parser);
12737 if (num == error_mark_node)
12738 return list;
12739 if (num)
12741 mark_exp_read (num);
12742 num = c_fully_fold (num, false, NULL);
12743 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12744 || !tree_fits_shwi_p (num)
12745 || (n = tree_to_shwi (num)) <= 0
12746 || (int) n != n)
12748 error_at (loc, "ordered argument needs positive "
12749 "constant integer expression");
12750 return list;
12753 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12754 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12755 OMP_CLAUSE_CHAIN (c) = list;
12756 return c;
12759 /* OpenMP 2.5:
12760 private ( variable-list ) */
12762 static tree
12763 c_parser_omp_clause_private (c_parser *parser, tree list)
12765 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12768 /* OpenMP 2.5:
12769 reduction ( reduction-operator : variable-list )
12771 reduction-operator:
12772 One of: + * - & ^ | && ||
12774 OpenMP 3.1:
12776 reduction-operator:
12777 One of: + * - & ^ | && || max min
12779 OpenMP 4.0:
12781 reduction-operator:
12782 One of: + * - & ^ | && ||
12783 identifier */
12785 static tree
12786 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12788 location_t clause_loc = c_parser_peek_token (parser)->location;
12789 matching_parens parens;
12790 if (parens.require_open (parser))
12792 enum tree_code code = ERROR_MARK;
12793 tree reduc_id = NULL_TREE;
12795 switch (c_parser_peek_token (parser)->type)
12797 case CPP_PLUS:
12798 code = PLUS_EXPR;
12799 break;
12800 case CPP_MULT:
12801 code = MULT_EXPR;
12802 break;
12803 case CPP_MINUS:
12804 code = MINUS_EXPR;
12805 break;
12806 case CPP_AND:
12807 code = BIT_AND_EXPR;
12808 break;
12809 case CPP_XOR:
12810 code = BIT_XOR_EXPR;
12811 break;
12812 case CPP_OR:
12813 code = BIT_IOR_EXPR;
12814 break;
12815 case CPP_AND_AND:
12816 code = TRUTH_ANDIF_EXPR;
12817 break;
12818 case CPP_OR_OR:
12819 code = TRUTH_ORIF_EXPR;
12820 break;
12821 case CPP_NAME:
12823 const char *p
12824 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12825 if (strcmp (p, "min") == 0)
12827 code = MIN_EXPR;
12828 break;
12830 if (strcmp (p, "max") == 0)
12832 code = MAX_EXPR;
12833 break;
12835 reduc_id = c_parser_peek_token (parser)->value;
12836 break;
12838 default:
12839 c_parser_error (parser,
12840 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12841 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12842 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12843 return list;
12845 c_parser_consume_token (parser);
12846 reduc_id = c_omp_reduction_id (code, reduc_id);
12847 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12849 tree nl, c;
12851 nl = c_parser_omp_variable_list (parser, clause_loc,
12852 OMP_CLAUSE_REDUCTION, list);
12853 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12855 tree d = OMP_CLAUSE_DECL (c), type;
12856 if (TREE_CODE (d) != TREE_LIST)
12857 type = TREE_TYPE (d);
12858 else
12860 int cnt = 0;
12861 tree t;
12862 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12863 cnt++;
12864 type = TREE_TYPE (t);
12865 while (cnt > 0)
12867 if (TREE_CODE (type) != POINTER_TYPE
12868 && TREE_CODE (type) != ARRAY_TYPE)
12869 break;
12870 type = TREE_TYPE (type);
12871 cnt--;
12874 while (TREE_CODE (type) == ARRAY_TYPE)
12875 type = TREE_TYPE (type);
12876 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12877 if (code == ERROR_MARK
12878 || !(INTEGRAL_TYPE_P (type)
12879 || TREE_CODE (type) == REAL_TYPE
12880 || TREE_CODE (type) == COMPLEX_TYPE))
12881 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12882 = c_omp_reduction_lookup (reduc_id,
12883 TYPE_MAIN_VARIANT (type));
12886 list = nl;
12888 parens.skip_until_found_close (parser);
12890 return list;
12893 /* OpenMP 2.5:
12894 schedule ( schedule-kind )
12895 schedule ( schedule-kind , expression )
12897 schedule-kind:
12898 static | dynamic | guided | runtime | auto
12900 OpenMP 4.5:
12901 schedule ( schedule-modifier : schedule-kind )
12902 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12904 schedule-modifier:
12905 simd
12906 monotonic
12907 nonmonotonic */
12909 static tree
12910 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12912 tree c, t;
12913 location_t loc = c_parser_peek_token (parser)->location;
12914 int modifiers = 0, nmodifiers = 0;
12916 matching_parens parens;
12917 if (!parens.require_open (parser))
12918 return list;
12920 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12922 while (c_parser_next_token_is (parser, CPP_NAME))
12924 tree kind = c_parser_peek_token (parser)->value;
12925 const char *p = IDENTIFIER_POINTER (kind);
12926 if (strcmp ("simd", p) == 0)
12927 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12928 else if (strcmp ("monotonic", p) == 0)
12929 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12930 else if (strcmp ("nonmonotonic", p) == 0)
12931 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12932 else
12933 break;
12934 c_parser_consume_token (parser);
12935 if (nmodifiers++ == 0
12936 && c_parser_next_token_is (parser, CPP_COMMA))
12937 c_parser_consume_token (parser);
12938 else
12940 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12941 break;
12945 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12946 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12947 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12948 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12950 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12951 "specified");
12952 modifiers = 0;
12955 if (c_parser_next_token_is (parser, CPP_NAME))
12957 tree kind = c_parser_peek_token (parser)->value;
12958 const char *p = IDENTIFIER_POINTER (kind);
12960 switch (p[0])
12962 case 'd':
12963 if (strcmp ("dynamic", p) != 0)
12964 goto invalid_kind;
12965 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12966 break;
12968 case 'g':
12969 if (strcmp ("guided", p) != 0)
12970 goto invalid_kind;
12971 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12972 break;
12974 case 'r':
12975 if (strcmp ("runtime", p) != 0)
12976 goto invalid_kind;
12977 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12978 break;
12980 default:
12981 goto invalid_kind;
12984 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12985 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12986 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12987 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12988 else
12989 goto invalid_kind;
12991 c_parser_consume_token (parser);
12992 if (c_parser_next_token_is (parser, CPP_COMMA))
12994 location_t here;
12995 c_parser_consume_token (parser);
12997 here = c_parser_peek_token (parser)->location;
12998 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12999 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13000 t = expr.value;
13001 t = c_fully_fold (t, false, NULL);
13003 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13004 error_at (here, "schedule %<runtime%> does not take "
13005 "a %<chunk_size%> parameter");
13006 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13007 error_at (here,
13008 "schedule %<auto%> does not take "
13009 "a %<chunk_size%> parameter");
13010 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13012 /* Attempt to statically determine when the number isn't
13013 positive. */
13014 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13015 build_int_cst (TREE_TYPE (t), 0));
13016 protected_set_expr_location (s, loc);
13017 if (s == boolean_true_node)
13019 warning_at (loc, 0,
13020 "chunk size value must be positive");
13021 t = integer_one_node;
13023 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13025 else
13026 c_parser_error (parser, "expected integer expression");
13028 parens.skip_until_found_close (parser);
13030 else
13031 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13032 "expected %<,%> or %<)%>");
13034 OMP_CLAUSE_SCHEDULE_KIND (c)
13035 = (enum omp_clause_schedule_kind)
13036 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13038 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13039 OMP_CLAUSE_CHAIN (c) = list;
13040 return c;
13042 invalid_kind:
13043 c_parser_error (parser, "invalid schedule kind");
13044 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13045 return list;
13048 /* OpenMP 2.5:
13049 shared ( variable-list ) */
13051 static tree
13052 c_parser_omp_clause_shared (c_parser *parser, tree list)
13054 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13057 /* OpenMP 3.0:
13058 untied */
13060 static tree
13061 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13063 tree c;
13065 /* FIXME: Should we allow duplicates? */
13066 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13068 c = build_omp_clause (c_parser_peek_token (parser)->location,
13069 OMP_CLAUSE_UNTIED);
13070 OMP_CLAUSE_CHAIN (c) = list;
13072 return c;
13075 /* OpenMP 4.0:
13076 inbranch
13077 notinbranch */
13079 static tree
13080 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13081 enum omp_clause_code code, tree list)
13083 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13085 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13086 OMP_CLAUSE_CHAIN (c) = list;
13088 return c;
13091 /* OpenMP 4.0:
13092 parallel
13094 sections
13095 taskgroup */
13097 static tree
13098 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13099 enum omp_clause_code code, tree list)
13101 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13102 OMP_CLAUSE_CHAIN (c) = list;
13104 return c;
13107 /* OpenMP 4.5:
13108 nogroup */
13110 static tree
13111 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13113 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13114 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13115 OMP_CLAUSE_NOGROUP);
13116 OMP_CLAUSE_CHAIN (c) = list;
13117 return c;
13120 /* OpenMP 4.5:
13121 simd
13122 threads */
13124 static tree
13125 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13126 enum omp_clause_code code, tree list)
13128 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13129 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13130 OMP_CLAUSE_CHAIN (c) = list;
13131 return c;
13134 /* OpenMP 4.0:
13135 num_teams ( expression ) */
13137 static tree
13138 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13140 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13141 matching_parens parens;
13142 if (parens.require_open (parser))
13144 location_t expr_loc = c_parser_peek_token (parser)->location;
13145 c_expr expr = c_parser_expression (parser);
13146 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13147 tree c, t = expr.value;
13148 t = c_fully_fold (t, false, NULL);
13150 parens.skip_until_found_close (parser);
13152 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13154 c_parser_error (parser, "expected integer expression");
13155 return list;
13158 /* Attempt to statically determine when the number isn't positive. */
13159 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13160 build_int_cst (TREE_TYPE (t), 0));
13161 protected_set_expr_location (c, expr_loc);
13162 if (c == boolean_true_node)
13164 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13165 t = integer_one_node;
13168 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13170 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13171 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13172 OMP_CLAUSE_CHAIN (c) = list;
13173 list = c;
13176 return list;
13179 /* OpenMP 4.0:
13180 thread_limit ( expression ) */
13182 static tree
13183 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13185 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13186 matching_parens parens;
13187 if (parens.require_open (parser))
13189 location_t expr_loc = c_parser_peek_token (parser)->location;
13190 c_expr expr = c_parser_expression (parser);
13191 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13192 tree c, t = expr.value;
13193 t = c_fully_fold (t, false, NULL);
13195 parens.skip_until_found_close (parser);
13197 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13199 c_parser_error (parser, "expected integer expression");
13200 return list;
13203 /* Attempt to statically determine when the number isn't positive. */
13204 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13205 build_int_cst (TREE_TYPE (t), 0));
13206 protected_set_expr_location (c, expr_loc);
13207 if (c == boolean_true_node)
13209 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13210 t = integer_one_node;
13213 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13214 "thread_limit");
13216 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13217 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13218 OMP_CLAUSE_CHAIN (c) = list;
13219 list = c;
13222 return list;
13225 /* OpenMP 4.0:
13226 aligned ( variable-list )
13227 aligned ( variable-list : constant-expression ) */
13229 static tree
13230 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13232 location_t clause_loc = c_parser_peek_token (parser)->location;
13233 tree nl, c;
13235 matching_parens parens;
13236 if (!parens.require_open (parser))
13237 return list;
13239 nl = c_parser_omp_variable_list (parser, clause_loc,
13240 OMP_CLAUSE_ALIGNED, list);
13242 if (c_parser_next_token_is (parser, CPP_COLON))
13244 c_parser_consume_token (parser);
13245 location_t expr_loc = c_parser_peek_token (parser)->location;
13246 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13247 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13248 tree alignment = expr.value;
13249 alignment = c_fully_fold (alignment, false, NULL);
13250 if (TREE_CODE (alignment) != INTEGER_CST
13251 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13252 || tree_int_cst_sgn (alignment) != 1)
13254 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13255 "be positive constant integer expression");
13256 alignment = NULL_TREE;
13259 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13260 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13263 parens.skip_until_found_close (parser);
13264 return nl;
13267 /* OpenMP 4.0:
13268 linear ( variable-list )
13269 linear ( variable-list : expression )
13271 OpenMP 4.5:
13272 linear ( modifier ( variable-list ) )
13273 linear ( modifier ( variable-list ) : expression ) */
13275 static tree
13276 c_parser_omp_clause_linear (c_parser *parser, tree list)
13278 location_t clause_loc = c_parser_peek_token (parser)->location;
13279 tree nl, c, step;
13280 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13282 matching_parens parens;
13283 if (!parens.require_open (parser))
13284 return list;
13286 if (c_parser_next_token_is (parser, CPP_NAME))
13288 c_token *tok = c_parser_peek_token (parser);
13289 const char *p = IDENTIFIER_POINTER (tok->value);
13290 if (strcmp ("val", p) == 0)
13291 kind = OMP_CLAUSE_LINEAR_VAL;
13292 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13293 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13294 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13296 c_parser_consume_token (parser);
13297 c_parser_consume_token (parser);
13301 nl = c_parser_omp_variable_list (parser, clause_loc,
13302 OMP_CLAUSE_LINEAR, list);
13304 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13305 parens.skip_until_found_close (parser);
13307 if (c_parser_next_token_is (parser, CPP_COLON))
13309 c_parser_consume_token (parser);
13310 location_t expr_loc = c_parser_peek_token (parser)->location;
13311 c_expr expr = c_parser_expression (parser);
13312 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13313 step = expr.value;
13314 step = c_fully_fold (step, false, NULL);
13315 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13317 error_at (clause_loc, "%<linear%> clause step expression must "
13318 "be integral");
13319 step = integer_one_node;
13323 else
13324 step = integer_one_node;
13326 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13328 OMP_CLAUSE_LINEAR_STEP (c) = step;
13329 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13332 parens.skip_until_found_close (parser);
13333 return nl;
13336 /* OpenMP 4.0:
13337 safelen ( constant-expression ) */
13339 static tree
13340 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13342 location_t clause_loc = c_parser_peek_token (parser)->location;
13343 tree c, t;
13345 matching_parens parens;
13346 if (!parens.require_open (parser))
13347 return list;
13349 location_t expr_loc = c_parser_peek_token (parser)->location;
13350 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13351 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13352 t = expr.value;
13353 t = c_fully_fold (t, false, NULL);
13354 if (TREE_CODE (t) != INTEGER_CST
13355 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13356 || tree_int_cst_sgn (t) != 1)
13358 error_at (clause_loc, "%<safelen%> clause expression must "
13359 "be positive constant integer expression");
13360 t = NULL_TREE;
13363 parens.skip_until_found_close (parser);
13364 if (t == NULL_TREE || t == error_mark_node)
13365 return list;
13367 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13369 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13370 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13371 OMP_CLAUSE_CHAIN (c) = list;
13372 return c;
13375 /* OpenMP 4.0:
13376 simdlen ( constant-expression ) */
13378 static tree
13379 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13381 location_t clause_loc = c_parser_peek_token (parser)->location;
13382 tree c, t;
13384 matching_parens parens;
13385 if (!parens.require_open (parser))
13386 return list;
13388 location_t expr_loc = c_parser_peek_token (parser)->location;
13389 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13390 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13391 t = expr.value;
13392 t = c_fully_fold (t, false, NULL);
13393 if (TREE_CODE (t) != INTEGER_CST
13394 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13395 || tree_int_cst_sgn (t) != 1)
13397 error_at (clause_loc, "%<simdlen%> clause expression must "
13398 "be positive constant integer expression");
13399 t = NULL_TREE;
13402 parens.skip_until_found_close (parser);
13403 if (t == NULL_TREE || t == error_mark_node)
13404 return list;
13406 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13408 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13409 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13410 OMP_CLAUSE_CHAIN (c) = list;
13411 return c;
13414 /* OpenMP 4.5:
13415 vec:
13416 identifier [+/- integer]
13417 vec , identifier [+/- integer]
13420 static tree
13421 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13422 tree list)
13424 tree vec = NULL;
13425 if (c_parser_next_token_is_not (parser, CPP_NAME)
13426 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13428 c_parser_error (parser, "expected identifier");
13429 return list;
13432 while (c_parser_next_token_is (parser, CPP_NAME)
13433 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13435 tree t = lookup_name (c_parser_peek_token (parser)->value);
13436 tree addend = NULL;
13438 if (t == NULL_TREE)
13440 undeclared_variable (c_parser_peek_token (parser)->location,
13441 c_parser_peek_token (parser)->value);
13442 t = error_mark_node;
13445 c_parser_consume_token (parser);
13447 bool neg = false;
13448 if (c_parser_next_token_is (parser, CPP_MINUS))
13449 neg = true;
13450 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13452 addend = integer_zero_node;
13453 neg = false;
13454 goto add_to_vector;
13456 c_parser_consume_token (parser);
13458 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13460 c_parser_error (parser, "expected integer");
13461 return list;
13464 addend = c_parser_peek_token (parser)->value;
13465 if (TREE_CODE (addend) != INTEGER_CST)
13467 c_parser_error (parser, "expected integer");
13468 return list;
13470 c_parser_consume_token (parser);
13472 add_to_vector:
13473 if (t != error_mark_node)
13475 vec = tree_cons (addend, t, vec);
13476 if (neg)
13477 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13480 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13481 break;
13483 c_parser_consume_token (parser);
13486 if (vec == NULL_TREE)
13487 return list;
13489 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13490 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13491 OMP_CLAUSE_DECL (u) = nreverse (vec);
13492 OMP_CLAUSE_CHAIN (u) = list;
13493 return u;
13496 /* OpenMP 4.0:
13497 depend ( depend-kind: variable-list )
13499 depend-kind:
13500 in | out | inout
13502 OpenMP 4.5:
13503 depend ( source )
13505 depend ( sink : vec ) */
13507 static tree
13508 c_parser_omp_clause_depend (c_parser *parser, tree list)
13510 location_t clause_loc = c_parser_peek_token (parser)->location;
13511 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13512 tree nl, c;
13514 matching_parens parens;
13515 if (!parens.require_open (parser))
13516 return list;
13518 if (c_parser_next_token_is (parser, CPP_NAME))
13520 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13521 if (strcmp ("in", p) == 0)
13522 kind = OMP_CLAUSE_DEPEND_IN;
13523 else if (strcmp ("inout", p) == 0)
13524 kind = OMP_CLAUSE_DEPEND_INOUT;
13525 else if (strcmp ("out", p) == 0)
13526 kind = OMP_CLAUSE_DEPEND_OUT;
13527 else if (strcmp ("source", p) == 0)
13528 kind = OMP_CLAUSE_DEPEND_SOURCE;
13529 else if (strcmp ("sink", p) == 0)
13530 kind = OMP_CLAUSE_DEPEND_SINK;
13531 else
13532 goto invalid_kind;
13534 else
13535 goto invalid_kind;
13537 c_parser_consume_token (parser);
13539 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13541 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13542 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13543 OMP_CLAUSE_DECL (c) = NULL_TREE;
13544 OMP_CLAUSE_CHAIN (c) = list;
13545 parens.skip_until_found_close (parser);
13546 return c;
13549 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13550 goto resync_fail;
13552 if (kind == OMP_CLAUSE_DEPEND_SINK)
13553 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13554 else
13556 nl = c_parser_omp_variable_list (parser, clause_loc,
13557 OMP_CLAUSE_DEPEND, list);
13559 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13560 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13563 parens.skip_until_found_close (parser);
13564 return nl;
13566 invalid_kind:
13567 c_parser_error (parser, "invalid depend kind");
13568 resync_fail:
13569 parens.skip_until_found_close (parser);
13570 return list;
13573 /* OpenMP 4.0:
13574 map ( map-kind: variable-list )
13575 map ( variable-list )
13577 map-kind:
13578 alloc | to | from | tofrom
13580 OpenMP 4.5:
13581 map-kind:
13582 alloc | to | from | tofrom | release | delete
13584 map ( always [,] map-kind: variable-list ) */
13586 static tree
13587 c_parser_omp_clause_map (c_parser *parser, tree list)
13589 location_t clause_loc = c_parser_peek_token (parser)->location;
13590 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13591 int always = 0;
13592 enum c_id_kind always_id_kind = C_ID_NONE;
13593 location_t always_loc = UNKNOWN_LOCATION;
13594 tree always_id = NULL_TREE;
13595 tree nl, c;
13597 matching_parens parens;
13598 if (!parens.require_open (parser))
13599 return list;
13601 if (c_parser_next_token_is (parser, CPP_NAME))
13603 c_token *tok = c_parser_peek_token (parser);
13604 const char *p = IDENTIFIER_POINTER (tok->value);
13605 always_id_kind = tok->id_kind;
13606 always_loc = tok->location;
13607 always_id = tok->value;
13608 if (strcmp ("always", p) == 0)
13610 c_token *sectok = c_parser_peek_2nd_token (parser);
13611 if (sectok->type == CPP_COMMA)
13613 c_parser_consume_token (parser);
13614 c_parser_consume_token (parser);
13615 always = 2;
13617 else if (sectok->type == CPP_NAME)
13619 p = IDENTIFIER_POINTER (sectok->value);
13620 if (strcmp ("alloc", p) == 0
13621 || strcmp ("to", p) == 0
13622 || strcmp ("from", p) == 0
13623 || strcmp ("tofrom", p) == 0
13624 || strcmp ("release", p) == 0
13625 || strcmp ("delete", p) == 0)
13627 c_parser_consume_token (parser);
13628 always = 1;
13634 if (c_parser_next_token_is (parser, CPP_NAME)
13635 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13637 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13638 if (strcmp ("alloc", p) == 0)
13639 kind = GOMP_MAP_ALLOC;
13640 else if (strcmp ("to", p) == 0)
13641 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13642 else if (strcmp ("from", p) == 0)
13643 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13644 else if (strcmp ("tofrom", p) == 0)
13645 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13646 else if (strcmp ("release", p) == 0)
13647 kind = GOMP_MAP_RELEASE;
13648 else if (strcmp ("delete", p) == 0)
13649 kind = GOMP_MAP_DELETE;
13650 else
13652 c_parser_error (parser, "invalid map kind");
13653 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13654 "expected %<)%>");
13655 return list;
13657 c_parser_consume_token (parser);
13658 c_parser_consume_token (parser);
13660 else if (always)
13662 if (always_id_kind != C_ID_ID)
13664 c_parser_error (parser, "expected identifier");
13665 parens.skip_until_found_close (parser);
13666 return list;
13669 tree t = lookup_name (always_id);
13670 if (t == NULL_TREE)
13672 undeclared_variable (always_loc, always_id);
13673 t = error_mark_node;
13675 if (t != error_mark_node)
13677 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13678 OMP_CLAUSE_DECL (u) = t;
13679 OMP_CLAUSE_CHAIN (u) = list;
13680 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13681 list = u;
13683 if (always == 1)
13685 parens.skip_until_found_close (parser);
13686 return list;
13690 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13692 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13693 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13695 parens.skip_until_found_close (parser);
13696 return nl;
13699 /* OpenMP 4.0:
13700 device ( expression ) */
13702 static tree
13703 c_parser_omp_clause_device (c_parser *parser, tree list)
13705 location_t clause_loc = c_parser_peek_token (parser)->location;
13706 matching_parens parens;
13707 if (parens.require_open (parser))
13709 location_t expr_loc = c_parser_peek_token (parser)->location;
13710 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13711 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13712 tree c, t = expr.value;
13713 t = c_fully_fold (t, false, NULL);
13715 parens.skip_until_found_close (parser);
13717 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13719 c_parser_error (parser, "expected integer expression");
13720 return list;
13723 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13725 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13726 OMP_CLAUSE_DEVICE_ID (c) = t;
13727 OMP_CLAUSE_CHAIN (c) = list;
13728 list = c;
13731 return list;
13734 /* OpenMP 4.0:
13735 dist_schedule ( static )
13736 dist_schedule ( static , expression ) */
13738 static tree
13739 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13741 tree c, t = NULL_TREE;
13742 location_t loc = c_parser_peek_token (parser)->location;
13744 matching_parens parens;
13745 if (!parens.require_open (parser))
13746 return list;
13748 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13750 c_parser_error (parser, "invalid dist_schedule kind");
13751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13752 "expected %<)%>");
13753 return list;
13756 c_parser_consume_token (parser);
13757 if (c_parser_next_token_is (parser, CPP_COMMA))
13759 c_parser_consume_token (parser);
13761 location_t expr_loc = c_parser_peek_token (parser)->location;
13762 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13763 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13764 t = expr.value;
13765 t = c_fully_fold (t, false, NULL);
13766 parens.skip_until_found_close (parser);
13768 else
13769 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13770 "expected %<,%> or %<)%>");
13772 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13773 if (t == error_mark_node)
13774 return list;
13776 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13777 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13778 OMP_CLAUSE_CHAIN (c) = list;
13779 return c;
13782 /* OpenMP 4.0:
13783 proc_bind ( proc-bind-kind )
13785 proc-bind-kind:
13786 master | close | spread */
13788 static tree
13789 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13791 location_t clause_loc = c_parser_peek_token (parser)->location;
13792 enum omp_clause_proc_bind_kind kind;
13793 tree c;
13795 matching_parens parens;
13796 if (!parens.require_open (parser))
13797 return list;
13799 if (c_parser_next_token_is (parser, CPP_NAME))
13801 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13802 if (strcmp ("master", p) == 0)
13803 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13804 else if (strcmp ("close", p) == 0)
13805 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13806 else if (strcmp ("spread", p) == 0)
13807 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13808 else
13809 goto invalid_kind;
13811 else
13812 goto invalid_kind;
13814 c_parser_consume_token (parser);
13815 parens.skip_until_found_close (parser);
13816 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13817 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13818 OMP_CLAUSE_CHAIN (c) = list;
13819 return c;
13821 invalid_kind:
13822 c_parser_error (parser, "invalid proc_bind kind");
13823 parens.skip_until_found_close (parser);
13824 return list;
13827 /* OpenMP 4.0:
13828 to ( variable-list ) */
13830 static tree
13831 c_parser_omp_clause_to (c_parser *parser, tree list)
13833 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13836 /* OpenMP 4.0:
13837 from ( variable-list ) */
13839 static tree
13840 c_parser_omp_clause_from (c_parser *parser, tree list)
13842 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13845 /* OpenMP 4.0:
13846 uniform ( variable-list ) */
13848 static tree
13849 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13851 /* The clauses location. */
13852 location_t loc = c_parser_peek_token (parser)->location;
13854 matching_parens parens;
13855 if (parens.require_open (parser))
13857 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13858 list);
13859 parens.skip_until_found_close (parser);
13861 return list;
13864 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13865 is a bitmask in MASK. Return the list of clauses found. */
13867 static tree
13868 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13869 const char *where, bool finish_p = true)
13871 tree clauses = NULL;
13872 bool first = true;
13874 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13876 location_t here;
13877 pragma_omp_clause c_kind;
13878 const char *c_name;
13879 tree prev = clauses;
13881 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13882 c_parser_consume_token (parser);
13884 here = c_parser_peek_token (parser)->location;
13885 c_kind = c_parser_omp_clause_name (parser);
13887 switch (c_kind)
13889 case PRAGMA_OACC_CLAUSE_ASYNC:
13890 clauses = c_parser_oacc_clause_async (parser, clauses);
13891 c_name = "async";
13892 break;
13893 case PRAGMA_OACC_CLAUSE_AUTO:
13894 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13895 clauses);
13896 c_name = "auto";
13897 break;
13898 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13899 clauses = c_parser_omp_clause_collapse (parser, clauses);
13900 c_name = "collapse";
13901 break;
13902 case PRAGMA_OACC_CLAUSE_COPY:
13903 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13904 c_name = "copy";
13905 break;
13906 case PRAGMA_OACC_CLAUSE_COPYIN:
13907 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13908 c_name = "copyin";
13909 break;
13910 case PRAGMA_OACC_CLAUSE_COPYOUT:
13911 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13912 c_name = "copyout";
13913 break;
13914 case PRAGMA_OACC_CLAUSE_CREATE:
13915 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13916 c_name = "create";
13917 break;
13918 case PRAGMA_OACC_CLAUSE_DELETE:
13919 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13920 c_name = "delete";
13921 break;
13922 case PRAGMA_OMP_CLAUSE_DEFAULT:
13923 clauses = c_parser_omp_clause_default (parser, clauses, true);
13924 c_name = "default";
13925 break;
13926 case PRAGMA_OACC_CLAUSE_DEVICE:
13927 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13928 c_name = "device";
13929 break;
13930 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13931 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13932 c_name = "deviceptr";
13933 break;
13934 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13935 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13936 c_name = "device_resident";
13937 break;
13938 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13939 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13940 c_name = "firstprivate";
13941 break;
13942 case PRAGMA_OACC_CLAUSE_GANG:
13943 c_name = "gang";
13944 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13945 c_name, clauses);
13946 break;
13947 case PRAGMA_OACC_CLAUSE_HOST:
13948 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13949 c_name = "host";
13950 break;
13951 case PRAGMA_OACC_CLAUSE_IF:
13952 clauses = c_parser_omp_clause_if (parser, clauses, false);
13953 c_name = "if";
13954 break;
13955 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13956 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13957 clauses);
13958 c_name = "independent";
13959 break;
13960 case PRAGMA_OACC_CLAUSE_LINK:
13961 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13962 c_name = "link";
13963 break;
13964 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13965 clauses = c_parser_oacc_single_int_clause (parser,
13966 OMP_CLAUSE_NUM_GANGS,
13967 clauses);
13968 c_name = "num_gangs";
13969 break;
13970 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13971 clauses = c_parser_oacc_single_int_clause (parser,
13972 OMP_CLAUSE_NUM_WORKERS,
13973 clauses);
13974 c_name = "num_workers";
13975 break;
13976 case PRAGMA_OACC_CLAUSE_PRESENT:
13977 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13978 c_name = "present";
13979 break;
13980 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13981 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13982 c_name = "present_or_copy";
13983 break;
13984 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13985 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13986 c_name = "present_or_copyin";
13987 break;
13988 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13989 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13990 c_name = "present_or_copyout";
13991 break;
13992 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13993 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13994 c_name = "present_or_create";
13995 break;
13996 case PRAGMA_OACC_CLAUSE_PRIVATE:
13997 clauses = c_parser_omp_clause_private (parser, clauses);
13998 c_name = "private";
13999 break;
14000 case PRAGMA_OACC_CLAUSE_REDUCTION:
14001 clauses = c_parser_omp_clause_reduction (parser, clauses);
14002 c_name = "reduction";
14003 break;
14004 case PRAGMA_OACC_CLAUSE_SELF:
14005 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14006 c_name = "self";
14007 break;
14008 case PRAGMA_OACC_CLAUSE_SEQ:
14009 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14010 clauses);
14011 c_name = "seq";
14012 break;
14013 case PRAGMA_OACC_CLAUSE_TILE:
14014 clauses = c_parser_oacc_clause_tile (parser, clauses);
14015 c_name = "tile";
14016 break;
14017 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14018 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14019 c_name = "use_device";
14020 break;
14021 case PRAGMA_OACC_CLAUSE_VECTOR:
14022 c_name = "vector";
14023 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14024 c_name, clauses);
14025 break;
14026 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14027 clauses = c_parser_oacc_single_int_clause (parser,
14028 OMP_CLAUSE_VECTOR_LENGTH,
14029 clauses);
14030 c_name = "vector_length";
14031 break;
14032 case PRAGMA_OACC_CLAUSE_WAIT:
14033 clauses = c_parser_oacc_clause_wait (parser, clauses);
14034 c_name = "wait";
14035 break;
14036 case PRAGMA_OACC_CLAUSE_WORKER:
14037 c_name = "worker";
14038 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14039 c_name, clauses);
14040 break;
14041 default:
14042 c_parser_error (parser, "expected %<#pragma acc%> clause");
14043 goto saw_error;
14046 first = false;
14048 if (((mask >> c_kind) & 1) == 0)
14050 /* Remove the invalid clause(s) from the list to avoid
14051 confusing the rest of the compiler. */
14052 clauses = prev;
14053 error_at (here, "%qs is not valid for %qs", c_name, where);
14057 saw_error:
14058 c_parser_skip_to_pragma_eol (parser);
14060 if (finish_p)
14061 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14063 return clauses;
14066 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14067 is a bitmask in MASK. Return the list of clauses found. */
14069 static tree
14070 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14071 const char *where, bool finish_p = true)
14073 tree clauses = NULL;
14074 bool first = true;
14076 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14078 location_t here;
14079 pragma_omp_clause c_kind;
14080 const char *c_name;
14081 tree prev = clauses;
14083 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14084 c_parser_consume_token (parser);
14086 here = c_parser_peek_token (parser)->location;
14087 c_kind = c_parser_omp_clause_name (parser);
14089 switch (c_kind)
14091 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14092 clauses = c_parser_omp_clause_collapse (parser, clauses);
14093 c_name = "collapse";
14094 break;
14095 case PRAGMA_OMP_CLAUSE_COPYIN:
14096 clauses = c_parser_omp_clause_copyin (parser, clauses);
14097 c_name = "copyin";
14098 break;
14099 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14100 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14101 c_name = "copyprivate";
14102 break;
14103 case PRAGMA_OMP_CLAUSE_DEFAULT:
14104 clauses = c_parser_omp_clause_default (parser, clauses, false);
14105 c_name = "default";
14106 break;
14107 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14108 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14109 c_name = "firstprivate";
14110 break;
14111 case PRAGMA_OMP_CLAUSE_FINAL:
14112 clauses = c_parser_omp_clause_final (parser, clauses);
14113 c_name = "final";
14114 break;
14115 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14116 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14117 c_name = "grainsize";
14118 break;
14119 case PRAGMA_OMP_CLAUSE_HINT:
14120 clauses = c_parser_omp_clause_hint (parser, clauses);
14121 c_name = "hint";
14122 break;
14123 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14124 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14125 c_name = "defaultmap";
14126 break;
14127 case PRAGMA_OMP_CLAUSE_IF:
14128 clauses = c_parser_omp_clause_if (parser, clauses, true);
14129 c_name = "if";
14130 break;
14131 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14132 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14133 c_name = "lastprivate";
14134 break;
14135 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14136 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14137 c_name = "mergeable";
14138 break;
14139 case PRAGMA_OMP_CLAUSE_NOWAIT:
14140 clauses = c_parser_omp_clause_nowait (parser, clauses);
14141 c_name = "nowait";
14142 break;
14143 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14144 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14145 c_name = "num_tasks";
14146 break;
14147 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14148 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14149 c_name = "num_threads";
14150 break;
14151 case PRAGMA_OMP_CLAUSE_ORDERED:
14152 clauses = c_parser_omp_clause_ordered (parser, clauses);
14153 c_name = "ordered";
14154 break;
14155 case PRAGMA_OMP_CLAUSE_PRIORITY:
14156 clauses = c_parser_omp_clause_priority (parser, clauses);
14157 c_name = "priority";
14158 break;
14159 case PRAGMA_OMP_CLAUSE_PRIVATE:
14160 clauses = c_parser_omp_clause_private (parser, clauses);
14161 c_name = "private";
14162 break;
14163 case PRAGMA_OMP_CLAUSE_REDUCTION:
14164 clauses = c_parser_omp_clause_reduction (parser, clauses);
14165 c_name = "reduction";
14166 break;
14167 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14168 clauses = c_parser_omp_clause_schedule (parser, clauses);
14169 c_name = "schedule";
14170 break;
14171 case PRAGMA_OMP_CLAUSE_SHARED:
14172 clauses = c_parser_omp_clause_shared (parser, clauses);
14173 c_name = "shared";
14174 break;
14175 case PRAGMA_OMP_CLAUSE_UNTIED:
14176 clauses = c_parser_omp_clause_untied (parser, clauses);
14177 c_name = "untied";
14178 break;
14179 case PRAGMA_OMP_CLAUSE_INBRANCH:
14180 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14181 clauses);
14182 c_name = "inbranch";
14183 break;
14184 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14185 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14186 clauses);
14187 c_name = "notinbranch";
14188 break;
14189 case PRAGMA_OMP_CLAUSE_PARALLEL:
14190 clauses
14191 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14192 clauses);
14193 c_name = "parallel";
14194 if (!first)
14196 clause_not_first:
14197 error_at (here, "%qs must be the first clause of %qs",
14198 c_name, where);
14199 clauses = prev;
14201 break;
14202 case PRAGMA_OMP_CLAUSE_FOR:
14203 clauses
14204 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14205 clauses);
14206 c_name = "for";
14207 if (!first)
14208 goto clause_not_first;
14209 break;
14210 case PRAGMA_OMP_CLAUSE_SECTIONS:
14211 clauses
14212 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14213 clauses);
14214 c_name = "sections";
14215 if (!first)
14216 goto clause_not_first;
14217 break;
14218 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14219 clauses
14220 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14221 clauses);
14222 c_name = "taskgroup";
14223 if (!first)
14224 goto clause_not_first;
14225 break;
14226 case PRAGMA_OMP_CLAUSE_LINK:
14227 clauses
14228 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14229 c_name = "link";
14230 break;
14231 case PRAGMA_OMP_CLAUSE_TO:
14232 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14233 clauses
14234 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14235 clauses);
14236 else
14237 clauses = c_parser_omp_clause_to (parser, clauses);
14238 c_name = "to";
14239 break;
14240 case PRAGMA_OMP_CLAUSE_FROM:
14241 clauses = c_parser_omp_clause_from (parser, clauses);
14242 c_name = "from";
14243 break;
14244 case PRAGMA_OMP_CLAUSE_UNIFORM:
14245 clauses = c_parser_omp_clause_uniform (parser, clauses);
14246 c_name = "uniform";
14247 break;
14248 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14249 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14250 c_name = "num_teams";
14251 break;
14252 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14253 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14254 c_name = "thread_limit";
14255 break;
14256 case PRAGMA_OMP_CLAUSE_ALIGNED:
14257 clauses = c_parser_omp_clause_aligned (parser, clauses);
14258 c_name = "aligned";
14259 break;
14260 case PRAGMA_OMP_CLAUSE_LINEAR:
14261 clauses = c_parser_omp_clause_linear (parser, clauses);
14262 c_name = "linear";
14263 break;
14264 case PRAGMA_OMP_CLAUSE_DEPEND:
14265 clauses = c_parser_omp_clause_depend (parser, clauses);
14266 c_name = "depend";
14267 break;
14268 case PRAGMA_OMP_CLAUSE_MAP:
14269 clauses = c_parser_omp_clause_map (parser, clauses);
14270 c_name = "map";
14271 break;
14272 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14273 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14274 c_name = "use_device_ptr";
14275 break;
14276 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14277 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14278 c_name = "is_device_ptr";
14279 break;
14280 case PRAGMA_OMP_CLAUSE_DEVICE:
14281 clauses = c_parser_omp_clause_device (parser, clauses);
14282 c_name = "device";
14283 break;
14284 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14285 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14286 c_name = "dist_schedule";
14287 break;
14288 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14289 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14290 c_name = "proc_bind";
14291 break;
14292 case PRAGMA_OMP_CLAUSE_SAFELEN:
14293 clauses = c_parser_omp_clause_safelen (parser, clauses);
14294 c_name = "safelen";
14295 break;
14296 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14297 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14298 c_name = "simdlen";
14299 break;
14300 case PRAGMA_OMP_CLAUSE_NOGROUP:
14301 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14302 c_name = "nogroup";
14303 break;
14304 case PRAGMA_OMP_CLAUSE_THREADS:
14305 clauses
14306 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14307 clauses);
14308 c_name = "threads";
14309 break;
14310 case PRAGMA_OMP_CLAUSE_SIMD:
14311 clauses
14312 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14313 clauses);
14314 c_name = "simd";
14315 break;
14316 default:
14317 c_parser_error (parser, "expected %<#pragma omp%> clause");
14318 goto saw_error;
14321 first = false;
14323 if (((mask >> c_kind) & 1) == 0)
14325 /* Remove the invalid clause(s) from the list to avoid
14326 confusing the rest of the compiler. */
14327 clauses = prev;
14328 error_at (here, "%qs is not valid for %qs", c_name, where);
14332 saw_error:
14333 c_parser_skip_to_pragma_eol (parser);
14335 if (finish_p)
14337 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14338 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14339 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14342 return clauses;
14345 /* OpenACC 2.0, OpenMP 2.5:
14346 structured-block:
14347 statement
14349 In practice, we're also interested in adding the statement to an
14350 outer node. So it is convenient if we work around the fact that
14351 c_parser_statement calls add_stmt. */
14353 static tree
14354 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14356 tree stmt = push_stmt_list ();
14357 c_parser_statement (parser, if_p);
14358 return pop_stmt_list (stmt);
14361 /* OpenACC 2.0:
14362 # pragma acc cache (variable-list) new-line
14364 LOC is the location of the #pragma token.
14367 static tree
14368 c_parser_oacc_cache (location_t loc, c_parser *parser)
14370 tree stmt, clauses;
14372 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14373 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14375 c_parser_skip_to_pragma_eol (parser);
14377 stmt = make_node (OACC_CACHE);
14378 TREE_TYPE (stmt) = void_type_node;
14379 OACC_CACHE_CLAUSES (stmt) = clauses;
14380 SET_EXPR_LOCATION (stmt, loc);
14381 add_stmt (stmt);
14383 return stmt;
14386 /* OpenACC 2.0:
14387 # pragma acc data oacc-data-clause[optseq] new-line
14388 structured-block
14390 LOC is the location of the #pragma token.
14393 #define OACC_DATA_CLAUSE_MASK \
14394 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14406 static tree
14407 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14409 tree stmt, clauses, block;
14411 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14412 "#pragma acc data");
14414 block = c_begin_omp_parallel ();
14415 add_stmt (c_parser_omp_structured_block (parser, if_p));
14417 stmt = c_finish_oacc_data (loc, clauses, block);
14419 return stmt;
14422 /* OpenACC 2.0:
14423 # pragma acc declare oacc-data-clause[optseq] new-line
14426 #define OACC_DECLARE_CLAUSE_MASK \
14427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14440 static void
14441 c_parser_oacc_declare (c_parser *parser)
14443 location_t pragma_loc = c_parser_peek_token (parser)->location;
14444 tree clauses, stmt, t, decl;
14446 bool error = false;
14448 c_parser_consume_pragma (parser);
14450 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14451 "#pragma acc declare");
14452 if (!clauses)
14454 error_at (pragma_loc,
14455 "no valid clauses specified in %<#pragma acc declare%>");
14456 return;
14459 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14461 location_t loc = OMP_CLAUSE_LOCATION (t);
14462 decl = OMP_CLAUSE_DECL (t);
14463 if (!DECL_P (decl))
14465 error_at (loc, "array section in %<#pragma acc declare%>");
14466 error = true;
14467 continue;
14470 switch (OMP_CLAUSE_MAP_KIND (t))
14472 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14473 case GOMP_MAP_FORCE_ALLOC:
14474 case GOMP_MAP_FORCE_TO:
14475 case GOMP_MAP_FORCE_DEVICEPTR:
14476 case GOMP_MAP_DEVICE_RESIDENT:
14477 break;
14479 case GOMP_MAP_LINK:
14480 if (!global_bindings_p ()
14481 && (TREE_STATIC (decl)
14482 || !DECL_EXTERNAL (decl)))
14484 error_at (loc,
14485 "%qD must be a global variable in "
14486 "%<#pragma acc declare link%>",
14487 decl);
14488 error = true;
14489 continue;
14491 break;
14493 default:
14494 if (global_bindings_p ())
14496 error_at (loc, "invalid OpenACC clause at file scope");
14497 error = true;
14498 continue;
14500 if (DECL_EXTERNAL (decl))
14502 error_at (loc,
14503 "invalid use of %<extern%> variable %qD "
14504 "in %<#pragma acc declare%>", decl);
14505 error = true;
14506 continue;
14508 else if (TREE_PUBLIC (decl))
14510 error_at (loc,
14511 "invalid use of %<global%> variable %qD "
14512 "in %<#pragma acc declare%>", decl);
14513 error = true;
14514 continue;
14516 break;
14519 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14520 || lookup_attribute ("omp declare target link",
14521 DECL_ATTRIBUTES (decl)))
14523 error_at (loc, "variable %qD used more than once with "
14524 "%<#pragma acc declare%>", decl);
14525 error = true;
14526 continue;
14529 if (!error)
14531 tree id;
14533 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14534 id = get_identifier ("omp declare target link");
14535 else
14536 id = get_identifier ("omp declare target");
14538 DECL_ATTRIBUTES (decl)
14539 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14541 if (global_bindings_p ())
14543 symtab_node *node = symtab_node::get (decl);
14544 if (node != NULL)
14546 node->offloadable = 1;
14547 if (ENABLE_OFFLOADING)
14549 g->have_offload = true;
14550 if (is_a <varpool_node *> (node))
14551 vec_safe_push (offload_vars, decl);
14558 if (error || global_bindings_p ())
14559 return;
14561 stmt = make_node (OACC_DECLARE);
14562 TREE_TYPE (stmt) = void_type_node;
14563 OACC_DECLARE_CLAUSES (stmt) = clauses;
14564 SET_EXPR_LOCATION (stmt, pragma_loc);
14566 add_stmt (stmt);
14568 return;
14571 /* OpenACC 2.0:
14572 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14576 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14579 LOC is the location of the #pragma token.
14582 #define OACC_ENTER_DATA_CLAUSE_MASK \
14583 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14591 #define OACC_EXIT_DATA_CLAUSE_MASK \
14592 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14598 static void
14599 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14601 location_t loc = c_parser_peek_token (parser)->location;
14602 tree clauses, stmt;
14603 const char *p = "";
14605 c_parser_consume_pragma (parser);
14607 if (c_parser_next_token_is (parser, CPP_NAME))
14609 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14610 c_parser_consume_token (parser);
14613 if (strcmp (p, "data") != 0)
14615 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14616 enter ? "enter" : "exit");
14617 parser->error = true;
14618 c_parser_skip_to_pragma_eol (parser);
14619 return;
14622 if (enter)
14623 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14624 "#pragma acc enter data");
14625 else
14626 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14627 "#pragma acc exit data");
14629 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14631 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14632 enter ? "enter" : "exit");
14633 return;
14636 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14637 TREE_TYPE (stmt) = void_type_node;
14638 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14639 SET_EXPR_LOCATION (stmt, loc);
14640 add_stmt (stmt);
14644 /* OpenACC 2.0:
14645 # pragma acc host_data oacc-data-clause[optseq] new-line
14646 structured-block
14649 #define OACC_HOST_DATA_CLAUSE_MASK \
14650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14652 static tree
14653 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14655 tree stmt, clauses, block;
14657 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14658 "#pragma acc host_data");
14660 block = c_begin_omp_parallel ();
14661 add_stmt (c_parser_omp_structured_block (parser, if_p));
14662 stmt = c_finish_oacc_host_data (loc, clauses, block);
14663 return stmt;
14667 /* OpenACC 2.0:
14669 # pragma acc loop oacc-loop-clause[optseq] new-line
14670 structured-block
14672 LOC is the location of the #pragma token.
14675 #define OACC_LOOP_CLAUSE_MASK \
14676 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14686 static tree
14687 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14688 omp_clause_mask mask, tree *cclauses, bool *if_p)
14690 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14692 strcat (p_name, " loop");
14693 mask |= OACC_LOOP_CLAUSE_MASK;
14695 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14696 cclauses == NULL);
14697 if (cclauses)
14699 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14700 if (*cclauses)
14701 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14702 if (clauses)
14703 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14706 tree block = c_begin_compound_stmt (true);
14707 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14708 if_p);
14709 block = c_end_compound_stmt (loc, block, true);
14710 add_stmt (block);
14712 return stmt;
14715 /* OpenACC 2.0:
14716 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14717 structured-block
14721 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14722 structured-block
14724 LOC is the location of the #pragma token.
14727 #define OACC_KERNELS_CLAUSE_MASK \
14728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14746 #define OACC_PARALLEL_CLAUSE_MASK \
14747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14768 static tree
14769 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14770 enum pragma_kind p_kind, char *p_name,
14771 bool *if_p)
14773 omp_clause_mask mask;
14774 enum tree_code code;
14775 switch (p_kind)
14777 case PRAGMA_OACC_KERNELS:
14778 strcat (p_name, " kernels");
14779 mask = OACC_KERNELS_CLAUSE_MASK;
14780 code = OACC_KERNELS;
14781 break;
14782 case PRAGMA_OACC_PARALLEL:
14783 strcat (p_name, " parallel");
14784 mask = OACC_PARALLEL_CLAUSE_MASK;
14785 code = OACC_PARALLEL;
14786 break;
14787 default:
14788 gcc_unreachable ();
14791 if (c_parser_next_token_is (parser, CPP_NAME))
14793 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14794 if (strcmp (p, "loop") == 0)
14796 c_parser_consume_token (parser);
14797 tree block = c_begin_omp_parallel ();
14798 tree clauses;
14799 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14800 return c_finish_omp_construct (loc, code, block, clauses);
14804 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14806 tree block = c_begin_omp_parallel ();
14807 add_stmt (c_parser_omp_structured_block (parser, if_p));
14809 return c_finish_omp_construct (loc, code, block, clauses);
14812 /* OpenACC 2.0:
14813 # pragma acc routine oacc-routine-clause[optseq] new-line
14814 function-definition
14816 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14819 #define OACC_ROUTINE_CLAUSE_MASK \
14820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14825 /* Parse an OpenACC routine directive. For named directives, we apply
14826 immediately to the named function. For unnamed ones we then parse
14827 a declaration or definition, which must be for a function. */
14829 static void
14830 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14832 gcc_checking_assert (context == pragma_external);
14834 oacc_routine_data data;
14835 data.error_seen = false;
14836 data.fndecl_seen = false;
14837 data.clauses = NULL_TREE;
14838 data.loc = c_parser_peek_token (parser)->location;
14840 c_parser_consume_pragma (parser);
14842 /* Look for optional '( name )'. */
14843 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14845 c_parser_consume_token (parser); /* '(' */
14847 tree decl = NULL_TREE;
14848 c_token *name_token = c_parser_peek_token (parser);
14849 location_t name_loc = name_token->location;
14850 if (name_token->type == CPP_NAME
14851 && (name_token->id_kind == C_ID_ID
14852 || name_token->id_kind == C_ID_TYPENAME))
14854 decl = lookup_name (name_token->value);
14855 if (!decl)
14856 error_at (name_loc,
14857 "%qE has not been declared", name_token->value);
14858 c_parser_consume_token (parser);
14860 else
14861 c_parser_error (parser, "expected function name");
14863 if (!decl
14864 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14866 c_parser_skip_to_pragma_eol (parser, false);
14867 return;
14870 data.clauses
14871 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14872 "#pragma acc routine");
14874 if (TREE_CODE (decl) != FUNCTION_DECL)
14876 error_at (name_loc, "%qD does not refer to a function", decl);
14877 return;
14880 c_finish_oacc_routine (&data, decl, false);
14882 else /* No optional '( name )'. */
14884 data.clauses
14885 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14886 "#pragma acc routine");
14888 /* Emit a helpful diagnostic if there's another pragma following this
14889 one. Also don't allow a static assertion declaration, as in the
14890 following we'll just parse a *single* "declaration or function
14891 definition", and the static assertion counts an one. */
14892 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14893 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14895 error_at (data.loc,
14896 "%<#pragma acc routine%> not immediately followed by"
14897 " function declaration or definition");
14898 /* ..., and then just keep going. */
14899 return;
14902 /* We only have to consider the pragma_external case here. */
14903 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14904 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14906 int ext = disable_extension_diagnostics ();
14908 c_parser_consume_token (parser);
14909 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14910 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14911 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14912 NULL, vNULL, &data);
14913 restore_extension_diagnostics (ext);
14915 else
14916 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14917 NULL, vNULL, &data);
14921 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14922 IS_DEFN is true if we're applying it to the definition. */
14924 static void
14925 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14926 bool is_defn)
14928 /* Keep going if we're in error reporting mode. */
14929 if (data->error_seen
14930 || fndecl == error_mark_node)
14931 return;
14933 if (data->fndecl_seen)
14935 error_at (data->loc,
14936 "%<#pragma acc routine%> not immediately followed by"
14937 " a single function declaration or definition");
14938 data->error_seen = true;
14939 return;
14941 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14943 error_at (data->loc,
14944 "%<#pragma acc routine%> not immediately followed by"
14945 " function declaration or definition");
14946 data->error_seen = true;
14947 return;
14950 if (oacc_get_fn_attrib (fndecl))
14952 error_at (data->loc,
14953 "%<#pragma acc routine%> already applied to %qD", fndecl);
14954 data->error_seen = true;
14955 return;
14958 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14960 error_at (data->loc,
14961 TREE_USED (fndecl)
14962 ? G_("%<#pragma acc routine%> must be applied before use")
14963 : G_("%<#pragma acc routine%> must be applied before "
14964 "definition"));
14965 data->error_seen = true;
14966 return;
14969 /* Process the routine's dimension clauses. */
14970 tree dims = oacc_build_routine_dims (data->clauses);
14971 oacc_replace_fn_attrib (fndecl, dims);
14973 /* Add an "omp declare target" attribute. */
14974 DECL_ATTRIBUTES (fndecl)
14975 = tree_cons (get_identifier ("omp declare target"),
14976 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14978 /* Remember that we've used this "#pragma acc routine". */
14979 data->fndecl_seen = true;
14982 /* OpenACC 2.0:
14983 # pragma acc update oacc-update-clause[optseq] new-line
14986 #define OACC_UPDATE_CLAUSE_MASK \
14987 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14994 static void
14995 c_parser_oacc_update (c_parser *parser)
14997 location_t loc = c_parser_peek_token (parser)->location;
14999 c_parser_consume_pragma (parser);
15001 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15002 "#pragma acc update");
15003 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15005 error_at (loc,
15006 "%<#pragma acc update%> must contain at least one "
15007 "%<device%> or %<host%> or %<self%> clause");
15008 return;
15011 if (parser->error)
15012 return;
15014 tree stmt = make_node (OACC_UPDATE);
15015 TREE_TYPE (stmt) = void_type_node;
15016 OACC_UPDATE_CLAUSES (stmt) = clauses;
15017 SET_EXPR_LOCATION (stmt, loc);
15018 add_stmt (stmt);
15021 /* OpenACC 2.0:
15022 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15024 LOC is the location of the #pragma token.
15027 #define OACC_WAIT_CLAUSE_MASK \
15028 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15030 static tree
15031 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15033 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15035 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15036 list = c_parser_oacc_wait_list (parser, loc, list);
15038 strcpy (p_name, " wait");
15039 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15040 stmt = c_finish_oacc_wait (loc, list, clauses);
15041 add_stmt (stmt);
15043 return stmt;
15046 /* OpenMP 2.5:
15047 # pragma omp atomic new-line
15048 expression-stmt
15050 expression-stmt:
15051 x binop= expr | x++ | ++x | x-- | --x
15052 binop:
15053 +, *, -, /, &, ^, |, <<, >>
15055 where x is an lvalue expression with scalar type.
15057 OpenMP 3.1:
15058 # pragma omp atomic new-line
15059 update-stmt
15061 # pragma omp atomic read new-line
15062 read-stmt
15064 # pragma omp atomic write new-line
15065 write-stmt
15067 # pragma omp atomic update new-line
15068 update-stmt
15070 # pragma omp atomic capture new-line
15071 capture-stmt
15073 # pragma omp atomic capture new-line
15074 capture-block
15076 read-stmt:
15077 v = x
15078 write-stmt:
15079 x = expr
15080 update-stmt:
15081 expression-stmt | x = x binop expr
15082 capture-stmt:
15083 v = expression-stmt
15084 capture-block:
15085 { v = x; update-stmt; } | { update-stmt; v = x; }
15087 OpenMP 4.0:
15088 update-stmt:
15089 expression-stmt | x = x binop expr | x = expr binop x
15090 capture-stmt:
15091 v = update-stmt
15092 capture-block:
15093 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15095 where x and v are lvalue expressions with scalar type.
15097 LOC is the location of the #pragma token. */
15099 static void
15100 c_parser_omp_atomic (location_t loc, c_parser *parser)
15102 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15103 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15104 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15105 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15106 struct c_expr expr;
15107 location_t eloc;
15108 bool structured_block = false;
15109 bool swapped = false;
15110 bool seq_cst = false;
15111 bool non_lvalue_p;
15113 if (c_parser_next_token_is (parser, CPP_NAME))
15115 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15116 if (!strcmp (p, "seq_cst"))
15118 seq_cst = true;
15119 c_parser_consume_token (parser);
15120 if (c_parser_next_token_is (parser, CPP_COMMA)
15121 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15122 c_parser_consume_token (parser);
15125 if (c_parser_next_token_is (parser, CPP_NAME))
15127 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15129 if (!strcmp (p, "read"))
15130 code = OMP_ATOMIC_READ;
15131 else if (!strcmp (p, "write"))
15132 code = NOP_EXPR;
15133 else if (!strcmp (p, "update"))
15134 code = OMP_ATOMIC;
15135 else if (!strcmp (p, "capture"))
15136 code = OMP_ATOMIC_CAPTURE_NEW;
15137 else
15138 p = NULL;
15139 if (p)
15140 c_parser_consume_token (parser);
15142 if (!seq_cst)
15144 if (c_parser_next_token_is (parser, CPP_COMMA)
15145 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15146 c_parser_consume_token (parser);
15148 if (c_parser_next_token_is (parser, CPP_NAME))
15150 const char *p
15151 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15152 if (!strcmp (p, "seq_cst"))
15154 seq_cst = true;
15155 c_parser_consume_token (parser);
15159 c_parser_skip_to_pragma_eol (parser);
15161 switch (code)
15163 case OMP_ATOMIC_READ:
15164 case NOP_EXPR: /* atomic write */
15165 v = c_parser_cast_expression (parser, NULL).value;
15166 non_lvalue_p = !lvalue_p (v);
15167 v = c_fully_fold (v, false, NULL, true);
15168 if (v == error_mark_node)
15169 goto saw_error;
15170 if (non_lvalue_p)
15171 v = non_lvalue (v);
15172 loc = c_parser_peek_token (parser)->location;
15173 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15174 goto saw_error;
15175 if (code == NOP_EXPR)
15177 lhs = c_parser_expression (parser).value;
15178 lhs = c_fully_fold (lhs, false, NULL);
15179 if (lhs == error_mark_node)
15180 goto saw_error;
15182 else
15184 lhs = c_parser_cast_expression (parser, NULL).value;
15185 non_lvalue_p = !lvalue_p (lhs);
15186 lhs = c_fully_fold (lhs, false, NULL, true);
15187 if (lhs == error_mark_node)
15188 goto saw_error;
15189 if (non_lvalue_p)
15190 lhs = non_lvalue (lhs);
15192 if (code == NOP_EXPR)
15194 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15195 opcode. */
15196 code = OMP_ATOMIC;
15197 rhs = lhs;
15198 lhs = v;
15199 v = NULL_TREE;
15201 goto done;
15202 case OMP_ATOMIC_CAPTURE_NEW:
15203 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15205 c_parser_consume_token (parser);
15206 structured_block = true;
15208 else
15210 v = c_parser_cast_expression (parser, NULL).value;
15211 non_lvalue_p = !lvalue_p (v);
15212 v = c_fully_fold (v, false, NULL, true);
15213 if (v == error_mark_node)
15214 goto saw_error;
15215 if (non_lvalue_p)
15216 v = non_lvalue (v);
15217 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15218 goto saw_error;
15220 break;
15221 default:
15222 break;
15225 /* For structured_block case we don't know yet whether
15226 old or new x should be captured. */
15227 restart:
15228 eloc = c_parser_peek_token (parser)->location;
15229 expr = c_parser_cast_expression (parser, NULL);
15230 lhs = expr.value;
15231 expr = default_function_array_conversion (eloc, expr);
15232 unfolded_lhs = expr.value;
15233 lhs = c_fully_fold (lhs, false, NULL, true);
15234 orig_lhs = lhs;
15235 switch (TREE_CODE (lhs))
15237 case ERROR_MARK:
15238 saw_error:
15239 c_parser_skip_to_end_of_block_or_statement (parser);
15240 if (structured_block)
15242 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15243 c_parser_consume_token (parser);
15244 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15246 c_parser_skip_to_end_of_block_or_statement (parser);
15247 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15248 c_parser_consume_token (parser);
15251 return;
15253 case POSTINCREMENT_EXPR:
15254 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15255 code = OMP_ATOMIC_CAPTURE_OLD;
15256 /* FALLTHROUGH */
15257 case PREINCREMENT_EXPR:
15258 lhs = TREE_OPERAND (lhs, 0);
15259 unfolded_lhs = NULL_TREE;
15260 opcode = PLUS_EXPR;
15261 rhs = integer_one_node;
15262 break;
15264 case POSTDECREMENT_EXPR:
15265 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15266 code = OMP_ATOMIC_CAPTURE_OLD;
15267 /* FALLTHROUGH */
15268 case PREDECREMENT_EXPR:
15269 lhs = TREE_OPERAND (lhs, 0);
15270 unfolded_lhs = NULL_TREE;
15271 opcode = MINUS_EXPR;
15272 rhs = integer_one_node;
15273 break;
15275 case COMPOUND_EXPR:
15276 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15277 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15278 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15279 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15280 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15281 (TREE_OPERAND (lhs, 1), 0), 0)))
15282 == BOOLEAN_TYPE)
15283 /* Undo effects of boolean_increment for post {in,de}crement. */
15284 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15285 /* FALLTHRU */
15286 case MODIFY_EXPR:
15287 if (TREE_CODE (lhs) == MODIFY_EXPR
15288 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15290 /* Undo effects of boolean_increment. */
15291 if (integer_onep (TREE_OPERAND (lhs, 1)))
15293 /* This is pre or post increment. */
15294 rhs = TREE_OPERAND (lhs, 1);
15295 lhs = TREE_OPERAND (lhs, 0);
15296 unfolded_lhs = NULL_TREE;
15297 opcode = NOP_EXPR;
15298 if (code == OMP_ATOMIC_CAPTURE_NEW
15299 && !structured_block
15300 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15301 code = OMP_ATOMIC_CAPTURE_OLD;
15302 break;
15304 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15305 && TREE_OPERAND (lhs, 0)
15306 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15308 /* This is pre or post decrement. */
15309 rhs = TREE_OPERAND (lhs, 1);
15310 lhs = TREE_OPERAND (lhs, 0);
15311 unfolded_lhs = NULL_TREE;
15312 opcode = NOP_EXPR;
15313 if (code == OMP_ATOMIC_CAPTURE_NEW
15314 && !structured_block
15315 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15316 code = OMP_ATOMIC_CAPTURE_OLD;
15317 break;
15320 /* FALLTHRU */
15321 default:
15322 if (!lvalue_p (unfolded_lhs))
15323 lhs = non_lvalue (lhs);
15324 switch (c_parser_peek_token (parser)->type)
15326 case CPP_MULT_EQ:
15327 opcode = MULT_EXPR;
15328 break;
15329 case CPP_DIV_EQ:
15330 opcode = TRUNC_DIV_EXPR;
15331 break;
15332 case CPP_PLUS_EQ:
15333 opcode = PLUS_EXPR;
15334 break;
15335 case CPP_MINUS_EQ:
15336 opcode = MINUS_EXPR;
15337 break;
15338 case CPP_LSHIFT_EQ:
15339 opcode = LSHIFT_EXPR;
15340 break;
15341 case CPP_RSHIFT_EQ:
15342 opcode = RSHIFT_EXPR;
15343 break;
15344 case CPP_AND_EQ:
15345 opcode = BIT_AND_EXPR;
15346 break;
15347 case CPP_OR_EQ:
15348 opcode = BIT_IOR_EXPR;
15349 break;
15350 case CPP_XOR_EQ:
15351 opcode = BIT_XOR_EXPR;
15352 break;
15353 case CPP_EQ:
15354 c_parser_consume_token (parser);
15355 eloc = c_parser_peek_token (parser)->location;
15356 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15357 rhs1 = expr.value;
15358 switch (TREE_CODE (rhs1))
15360 case MULT_EXPR:
15361 case TRUNC_DIV_EXPR:
15362 case RDIV_EXPR:
15363 case PLUS_EXPR:
15364 case MINUS_EXPR:
15365 case LSHIFT_EXPR:
15366 case RSHIFT_EXPR:
15367 case BIT_AND_EXPR:
15368 case BIT_IOR_EXPR:
15369 case BIT_XOR_EXPR:
15370 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15372 opcode = TREE_CODE (rhs1);
15373 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15374 true);
15375 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15376 true);
15377 goto stmt_done;
15379 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15381 opcode = TREE_CODE (rhs1);
15382 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15383 true);
15384 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15385 true);
15386 swapped = !commutative_tree_code (opcode);
15387 goto stmt_done;
15389 break;
15390 case ERROR_MARK:
15391 goto saw_error;
15392 default:
15393 break;
15395 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15397 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15399 code = OMP_ATOMIC_CAPTURE_OLD;
15400 v = lhs;
15401 lhs = NULL_TREE;
15402 expr = default_function_array_read_conversion (eloc, expr);
15403 unfolded_lhs1 = expr.value;
15404 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15405 rhs1 = NULL_TREE;
15406 c_parser_consume_token (parser);
15407 goto restart;
15409 if (structured_block)
15411 opcode = NOP_EXPR;
15412 expr = default_function_array_read_conversion (eloc, expr);
15413 rhs = c_fully_fold (expr.value, false, NULL, true);
15414 rhs1 = NULL_TREE;
15415 goto stmt_done;
15418 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15419 goto saw_error;
15420 default:
15421 c_parser_error (parser,
15422 "invalid operator for %<#pragma omp atomic%>");
15423 goto saw_error;
15426 /* Arrange to pass the location of the assignment operator to
15427 c_finish_omp_atomic. */
15428 loc = c_parser_peek_token (parser)->location;
15429 c_parser_consume_token (parser);
15430 eloc = c_parser_peek_token (parser)->location;
15431 expr = c_parser_expression (parser);
15432 expr = default_function_array_read_conversion (eloc, expr);
15433 rhs = expr.value;
15434 rhs = c_fully_fold (rhs, false, NULL, true);
15435 break;
15437 stmt_done:
15438 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15440 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15441 goto saw_error;
15442 v = c_parser_cast_expression (parser, NULL).value;
15443 non_lvalue_p = !lvalue_p (v);
15444 v = c_fully_fold (v, false, NULL, true);
15445 if (v == error_mark_node)
15446 goto saw_error;
15447 if (non_lvalue_p)
15448 v = non_lvalue (v);
15449 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15450 goto saw_error;
15451 eloc = c_parser_peek_token (parser)->location;
15452 expr = c_parser_cast_expression (parser, NULL);
15453 lhs1 = expr.value;
15454 expr = default_function_array_read_conversion (eloc, expr);
15455 unfolded_lhs1 = expr.value;
15456 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15457 if (lhs1 == error_mark_node)
15458 goto saw_error;
15459 if (!lvalue_p (unfolded_lhs1))
15460 lhs1 = non_lvalue (lhs1);
15462 if (structured_block)
15464 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15465 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15467 done:
15468 if (unfolded_lhs && unfolded_lhs1
15469 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15471 error ("%<#pragma omp atomic capture%> uses two different "
15472 "expressions for memory");
15473 stmt = error_mark_node;
15475 else
15476 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15477 swapped, seq_cst);
15478 if (stmt != error_mark_node)
15479 add_stmt (stmt);
15481 if (!structured_block)
15482 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15486 /* OpenMP 2.5:
15487 # pragma omp barrier new-line
15490 static void
15491 c_parser_omp_barrier (c_parser *parser)
15493 location_t loc = c_parser_peek_token (parser)->location;
15494 c_parser_consume_pragma (parser);
15495 c_parser_skip_to_pragma_eol (parser);
15497 c_finish_omp_barrier (loc);
15500 /* OpenMP 2.5:
15501 # pragma omp critical [(name)] new-line
15502 structured-block
15504 OpenMP 4.5:
15505 # pragma omp critical [(name) [hint(expression)]] new-line
15507 LOC is the location of the #pragma itself. */
15509 #define OMP_CRITICAL_CLAUSE_MASK \
15510 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15512 static tree
15513 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15515 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15517 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15519 c_parser_consume_token (parser);
15520 if (c_parser_next_token_is (parser, CPP_NAME))
15522 name = c_parser_peek_token (parser)->value;
15523 c_parser_consume_token (parser);
15524 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15526 else
15527 c_parser_error (parser, "expected identifier");
15529 clauses = c_parser_omp_all_clauses (parser,
15530 OMP_CRITICAL_CLAUSE_MASK,
15531 "#pragma omp critical");
15533 else
15535 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15536 c_parser_error (parser, "expected %<(%> or end of line");
15537 c_parser_skip_to_pragma_eol (parser);
15540 stmt = c_parser_omp_structured_block (parser, if_p);
15541 return c_finish_omp_critical (loc, stmt, name, clauses);
15544 /* OpenMP 2.5:
15545 # pragma omp flush flush-vars[opt] new-line
15547 flush-vars:
15548 ( variable-list ) */
15550 static void
15551 c_parser_omp_flush (c_parser *parser)
15553 location_t loc = c_parser_peek_token (parser)->location;
15554 c_parser_consume_pragma (parser);
15555 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15556 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15557 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15558 c_parser_error (parser, "expected %<(%> or end of line");
15559 c_parser_skip_to_pragma_eol (parser);
15561 c_finish_omp_flush (loc);
15564 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15565 The real trick here is to determine the loop control variable early
15566 so that we can push a new decl if necessary to make it private.
15567 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15568 respectively. */
15570 static tree
15571 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15572 tree clauses, tree *cclauses, bool *if_p)
15574 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15575 tree declv, condv, incrv, initv, ret = NULL_TREE;
15576 tree pre_body = NULL_TREE, this_pre_body;
15577 tree ordered_cl = NULL_TREE;
15578 bool fail = false, open_brace_parsed = false;
15579 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15580 location_t for_loc;
15581 bool tiling = false;
15582 vec<tree, va_gc> *for_block = make_tree_vector ();
15584 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15585 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15586 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15587 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15589 tiling = true;
15590 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15592 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15593 && OMP_CLAUSE_ORDERED_EXPR (cl))
15595 ordered_cl = cl;
15596 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15599 if (ordered && ordered < collapse)
15601 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15602 "%<ordered%> clause parameter is less than %<collapse%>");
15603 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15604 = build_int_cst (NULL_TREE, collapse);
15605 ordered = collapse;
15607 if (ordered)
15609 for (tree *pc = &clauses; *pc; )
15610 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15612 error_at (OMP_CLAUSE_LOCATION (*pc),
15613 "%<linear%> clause may not be specified together "
15614 "with %<ordered%> clause with a parameter");
15615 *pc = OMP_CLAUSE_CHAIN (*pc);
15617 else
15618 pc = &OMP_CLAUSE_CHAIN (*pc);
15621 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15622 count = ordered ? ordered : collapse;
15624 declv = make_tree_vec (count);
15625 initv = make_tree_vec (count);
15626 condv = make_tree_vec (count);
15627 incrv = make_tree_vec (count);
15629 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15631 c_parser_error (parser, "for statement expected");
15632 return NULL;
15634 for_loc = c_parser_peek_token (parser)->location;
15635 c_parser_consume_token (parser);
15637 for (i = 0; i < count; i++)
15639 int bracecount = 0;
15641 matching_parens parens;
15642 if (!parens.require_open (parser))
15643 goto pop_scopes;
15645 /* Parse the initialization declaration or expression. */
15646 if (c_parser_next_tokens_start_declaration (parser))
15648 if (i > 0)
15649 vec_safe_push (for_block, c_begin_compound_stmt (true));
15650 this_pre_body = push_stmt_list ();
15651 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15652 NULL, vNULL);
15653 if (this_pre_body)
15655 this_pre_body = pop_stmt_list (this_pre_body);
15656 if (pre_body)
15658 tree t = pre_body;
15659 pre_body = push_stmt_list ();
15660 add_stmt (t);
15661 add_stmt (this_pre_body);
15662 pre_body = pop_stmt_list (pre_body);
15664 else
15665 pre_body = this_pre_body;
15667 decl = check_for_loop_decls (for_loc, flag_isoc99);
15668 if (decl == NULL)
15669 goto error_init;
15670 if (DECL_INITIAL (decl) == error_mark_node)
15671 decl = error_mark_node;
15672 init = decl;
15674 else if (c_parser_next_token_is (parser, CPP_NAME)
15675 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15677 struct c_expr decl_exp;
15678 struct c_expr init_exp;
15679 location_t init_loc;
15681 decl_exp = c_parser_postfix_expression (parser);
15682 decl = decl_exp.value;
15684 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15686 init_loc = c_parser_peek_token (parser)->location;
15687 init_exp = c_parser_expr_no_commas (parser, NULL);
15688 init_exp = default_function_array_read_conversion (init_loc,
15689 init_exp);
15690 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15691 NOP_EXPR, init_loc, init_exp.value,
15692 init_exp.original_type);
15693 init = c_process_expr_stmt (init_loc, init);
15695 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15697 else
15699 error_init:
15700 c_parser_error (parser,
15701 "expected iteration declaration or initialization");
15702 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15703 "expected %<)%>");
15704 fail = true;
15705 goto parse_next;
15708 /* Parse the loop condition. */
15709 cond = NULL_TREE;
15710 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15712 location_t cond_loc = c_parser_peek_token (parser)->location;
15713 struct c_expr cond_expr
15714 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15716 cond = cond_expr.value;
15717 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15718 if (COMPARISON_CLASS_P (cond))
15720 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15721 op0 = c_fully_fold (op0, false, NULL);
15722 op1 = c_fully_fold (op1, false, NULL);
15723 TREE_OPERAND (cond, 0) = op0;
15724 TREE_OPERAND (cond, 1) = op1;
15726 switch (cond_expr.original_code)
15728 case GT_EXPR:
15729 case GE_EXPR:
15730 case LT_EXPR:
15731 case LE_EXPR:
15732 break;
15733 default:
15734 /* Can't be cond = error_mark_node, because we want to preserve
15735 the location until c_finish_omp_for. */
15736 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15737 break;
15739 protected_set_expr_location (cond, cond_loc);
15741 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15743 /* Parse the increment expression. */
15744 incr = NULL_TREE;
15745 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15747 location_t incr_loc = c_parser_peek_token (parser)->location;
15749 incr = c_process_expr_stmt (incr_loc,
15750 c_parser_expression (parser).value);
15752 parens.skip_until_found_close (parser);
15754 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15755 fail = true;
15756 else
15758 TREE_VEC_ELT (declv, i) = decl;
15759 TREE_VEC_ELT (initv, i) = init;
15760 TREE_VEC_ELT (condv, i) = cond;
15761 TREE_VEC_ELT (incrv, i) = incr;
15764 parse_next:
15765 if (i == count - 1)
15766 break;
15768 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15769 in between the collapsed for loops to be still considered perfectly
15770 nested. Hopefully the final version clarifies this.
15771 For now handle (multiple) {'s and empty statements. */
15774 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15776 c_parser_consume_token (parser);
15777 break;
15779 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15781 c_parser_consume_token (parser);
15782 bracecount++;
15784 else if (bracecount
15785 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15786 c_parser_consume_token (parser);
15787 else
15789 c_parser_error (parser, "not enough perfectly nested loops");
15790 if (bracecount)
15792 open_brace_parsed = true;
15793 bracecount--;
15795 fail = true;
15796 count = 0;
15797 break;
15800 while (1);
15802 nbraces += bracecount;
15805 if (nbraces)
15806 if_p = NULL;
15808 save_break = c_break_label;
15809 c_break_label = size_one_node;
15810 save_cont = c_cont_label;
15811 c_cont_label = NULL_TREE;
15812 body = push_stmt_list ();
15814 if (open_brace_parsed)
15816 location_t here = c_parser_peek_token (parser)->location;
15817 stmt = c_begin_compound_stmt (true);
15818 c_parser_compound_statement_nostart (parser);
15819 add_stmt (c_end_compound_stmt (here, stmt, true));
15821 else
15822 add_stmt (c_parser_c99_block_statement (parser, if_p));
15823 if (c_cont_label)
15825 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15826 SET_EXPR_LOCATION (t, loc);
15827 add_stmt (t);
15830 body = pop_stmt_list (body);
15831 c_break_label = save_break;
15832 c_cont_label = save_cont;
15834 while (nbraces)
15836 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15838 c_parser_consume_token (parser);
15839 nbraces--;
15841 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15842 c_parser_consume_token (parser);
15843 else
15845 c_parser_error (parser, "collapsed loops not perfectly nested");
15846 while (nbraces)
15848 location_t here = c_parser_peek_token (parser)->location;
15849 stmt = c_begin_compound_stmt (true);
15850 add_stmt (body);
15851 c_parser_compound_statement_nostart (parser);
15852 body = c_end_compound_stmt (here, stmt, true);
15853 nbraces--;
15855 goto pop_scopes;
15859 /* Only bother calling c_finish_omp_for if we haven't already generated
15860 an error from the initialization parsing. */
15861 if (!fail)
15863 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15864 incrv, body, pre_body);
15866 /* Check for iterators appearing in lb, b or incr expressions. */
15867 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15868 stmt = NULL_TREE;
15870 if (stmt)
15872 add_stmt (stmt);
15874 if (cclauses != NULL
15875 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15877 tree *c;
15878 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15879 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15880 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15881 c = &OMP_CLAUSE_CHAIN (*c);
15882 else
15884 for (i = 0; i < count; i++)
15885 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15886 break;
15887 if (i == count)
15888 c = &OMP_CLAUSE_CHAIN (*c);
15889 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15891 error_at (loc,
15892 "iteration variable %qD should not be firstprivate",
15893 OMP_CLAUSE_DECL (*c));
15894 *c = OMP_CLAUSE_CHAIN (*c);
15896 else
15898 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15899 tree l = *c;
15900 *c = OMP_CLAUSE_CHAIN (*c);
15901 if (code == OMP_SIMD)
15903 OMP_CLAUSE_CHAIN (l)
15904 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15905 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15907 else
15909 OMP_CLAUSE_CHAIN (l) = clauses;
15910 clauses = l;
15915 OMP_FOR_CLAUSES (stmt) = clauses;
15917 ret = stmt;
15919 pop_scopes:
15920 while (!for_block->is_empty ())
15922 /* FIXME diagnostics: LOC below should be the actual location of
15923 this particular for block. We need to build a list of
15924 locations to go along with FOR_BLOCK. */
15925 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15926 add_stmt (stmt);
15928 release_tree_vector (for_block);
15929 return ret;
15932 /* Helper function for OpenMP parsing, split clauses and call
15933 finish_omp_clauses on each of the set of clauses afterwards. */
15935 static void
15936 omp_split_clauses (location_t loc, enum tree_code code,
15937 omp_clause_mask mask, tree clauses, tree *cclauses)
15939 int i;
15940 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15941 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15942 if (cclauses[i])
15943 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15946 /* OpenMP 4.0:
15947 #pragma omp simd simd-clause[optseq] new-line
15948 for-loop
15950 LOC is the location of the #pragma token.
15953 #define OMP_SIMD_CLAUSE_MASK \
15954 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15963 static tree
15964 c_parser_omp_simd (location_t loc, c_parser *parser,
15965 char *p_name, omp_clause_mask mask, tree *cclauses,
15966 bool *if_p)
15968 tree block, clauses, ret;
15970 strcat (p_name, " simd");
15971 mask |= OMP_SIMD_CLAUSE_MASK;
15973 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15974 if (cclauses)
15976 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15977 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15978 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15979 OMP_CLAUSE_ORDERED);
15980 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15982 error_at (OMP_CLAUSE_LOCATION (c),
15983 "%<ordered%> clause with parameter may not be specified "
15984 "on %qs construct", p_name);
15985 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15989 block = c_begin_compound_stmt (true);
15990 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15991 block = c_end_compound_stmt (loc, block, true);
15992 add_stmt (block);
15994 return ret;
15997 /* OpenMP 2.5:
15998 #pragma omp for for-clause[optseq] new-line
15999 for-loop
16001 OpenMP 4.0:
16002 #pragma omp for simd for-simd-clause[optseq] new-line
16003 for-loop
16005 LOC is the location of the #pragma token.
16008 #define OMP_FOR_CLAUSE_MASK \
16009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
16015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
16016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16019 static tree
16020 c_parser_omp_for (location_t loc, c_parser *parser,
16021 char *p_name, omp_clause_mask mask, tree *cclauses,
16022 bool *if_p)
16024 tree block, clauses, ret;
16026 strcat (p_name, " for");
16027 mask |= OMP_FOR_CLAUSE_MASK;
16028 /* parallel for{, simd} disallows nowait clause, but for
16029 target {teams distribute ,}parallel for{, simd} it should be accepted. */
16030 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16031 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16032 /* Composite distribute parallel for{, simd} disallows ordered clause. */
16033 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16034 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16036 if (c_parser_next_token_is (parser, CPP_NAME))
16038 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16040 if (strcmp (p, "simd") == 0)
16042 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16043 if (cclauses == NULL)
16044 cclauses = cclauses_buf;
16046 c_parser_consume_token (parser);
16047 if (!flag_openmp) /* flag_openmp_simd */
16048 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16049 if_p);
16050 block = c_begin_compound_stmt (true);
16051 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16052 block = c_end_compound_stmt (loc, block, true);
16053 if (ret == NULL_TREE)
16054 return ret;
16055 ret = make_node (OMP_FOR);
16056 TREE_TYPE (ret) = void_type_node;
16057 OMP_FOR_BODY (ret) = block;
16058 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16059 SET_EXPR_LOCATION (ret, loc);
16060 add_stmt (ret);
16061 return ret;
16064 if (!flag_openmp) /* flag_openmp_simd */
16066 c_parser_skip_to_pragma_eol (parser, false);
16067 return NULL_TREE;
16070 /* Composite distribute parallel for disallows linear clause. */
16071 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16072 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16074 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16075 if (cclauses)
16077 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16078 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16081 block = c_begin_compound_stmt (true);
16082 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16083 block = c_end_compound_stmt (loc, block, true);
16084 add_stmt (block);
16086 return ret;
16089 /* OpenMP 2.5:
16090 # pragma omp master new-line
16091 structured-block
16093 LOC is the location of the #pragma token.
16096 static tree
16097 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16099 c_parser_skip_to_pragma_eol (parser);
16100 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16101 if_p));
16104 /* OpenMP 2.5:
16105 # pragma omp ordered new-line
16106 structured-block
16108 OpenMP 4.5:
16109 # pragma omp ordered ordered-clauses new-line
16110 structured-block
16112 # pragma omp ordered depend-clauses new-line */
16114 #define OMP_ORDERED_CLAUSE_MASK \
16115 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16118 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16119 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16121 static bool
16122 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16123 bool *if_p)
16125 location_t loc = c_parser_peek_token (parser)->location;
16126 c_parser_consume_pragma (parser);
16128 if (context != pragma_stmt && context != pragma_compound)
16130 c_parser_error (parser, "expected declaration specifiers");
16131 c_parser_skip_to_pragma_eol (parser, false);
16132 return false;
16135 if (c_parser_next_token_is (parser, CPP_NAME))
16137 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16139 if (!strcmp ("depend", p))
16141 if (!flag_openmp) /* flag_openmp_simd */
16143 c_parser_skip_to_pragma_eol (parser, false);
16144 return false;
16146 if (context == pragma_stmt)
16148 error_at (loc,
16149 "%<#pragma omp ordered%> with %<depend%> clause may "
16150 "only be used in compound statements");
16151 c_parser_skip_to_pragma_eol (parser, false);
16152 return false;
16155 tree clauses
16156 = c_parser_omp_all_clauses (parser,
16157 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16158 "#pragma omp ordered");
16159 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16160 return false;
16164 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16165 "#pragma omp ordered");
16167 if (!flag_openmp /* flag_openmp_simd */
16168 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16169 return false;
16171 c_finish_omp_ordered (loc, clauses,
16172 c_parser_omp_structured_block (parser, if_p));
16173 return true;
16176 /* OpenMP 2.5:
16178 section-scope:
16179 { section-sequence }
16181 section-sequence:
16182 section-directive[opt] structured-block
16183 section-sequence section-directive structured-block
16185 SECTIONS_LOC is the location of the #pragma omp sections. */
16187 static tree
16188 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16190 tree stmt, substmt;
16191 bool error_suppress = false;
16192 location_t loc;
16194 loc = c_parser_peek_token (parser)->location;
16195 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16197 /* Avoid skipping until the end of the block. */
16198 parser->error = false;
16199 return NULL_TREE;
16202 stmt = push_stmt_list ();
16204 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16206 substmt = c_parser_omp_structured_block (parser, NULL);
16207 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16208 SET_EXPR_LOCATION (substmt, loc);
16209 add_stmt (substmt);
16212 while (1)
16214 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16215 break;
16216 if (c_parser_next_token_is (parser, CPP_EOF))
16217 break;
16219 loc = c_parser_peek_token (parser)->location;
16220 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16222 c_parser_consume_pragma (parser);
16223 c_parser_skip_to_pragma_eol (parser);
16224 error_suppress = false;
16226 else if (!error_suppress)
16228 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16229 error_suppress = true;
16232 substmt = c_parser_omp_structured_block (parser, NULL);
16233 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16234 SET_EXPR_LOCATION (substmt, loc);
16235 add_stmt (substmt);
16237 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16238 "expected %<#pragma omp section%> or %<}%>");
16240 substmt = pop_stmt_list (stmt);
16242 stmt = make_node (OMP_SECTIONS);
16243 SET_EXPR_LOCATION (stmt, sections_loc);
16244 TREE_TYPE (stmt) = void_type_node;
16245 OMP_SECTIONS_BODY (stmt) = substmt;
16247 return add_stmt (stmt);
16250 /* OpenMP 2.5:
16251 # pragma omp sections sections-clause[optseq] newline
16252 sections-scope
16254 LOC is the location of the #pragma token.
16257 #define OMP_SECTIONS_CLAUSE_MASK \
16258 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16264 static tree
16265 c_parser_omp_sections (location_t loc, c_parser *parser,
16266 char *p_name, omp_clause_mask mask, tree *cclauses)
16268 tree block, clauses, ret;
16270 strcat (p_name, " sections");
16271 mask |= OMP_SECTIONS_CLAUSE_MASK;
16272 if (cclauses)
16273 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16275 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16276 if (cclauses)
16278 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16279 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16282 block = c_begin_compound_stmt (true);
16283 ret = c_parser_omp_sections_scope (loc, parser);
16284 if (ret)
16285 OMP_SECTIONS_CLAUSES (ret) = clauses;
16286 block = c_end_compound_stmt (loc, block, true);
16287 add_stmt (block);
16289 return ret;
16292 /* OpenMP 2.5:
16293 # pragma omp parallel parallel-clause[optseq] new-line
16294 structured-block
16295 # pragma omp parallel for parallel-for-clause[optseq] new-line
16296 structured-block
16297 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16298 structured-block
16300 OpenMP 4.0:
16301 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16302 structured-block
16304 LOC is the location of the #pragma token.
16307 #define OMP_PARALLEL_CLAUSE_MASK \
16308 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16318 static tree
16319 c_parser_omp_parallel (location_t loc, c_parser *parser,
16320 char *p_name, omp_clause_mask mask, tree *cclauses,
16321 bool *if_p)
16323 tree stmt, clauses, block;
16325 strcat (p_name, " parallel");
16326 mask |= OMP_PARALLEL_CLAUSE_MASK;
16327 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16328 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16329 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16330 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16332 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16334 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16335 if (cclauses == NULL)
16336 cclauses = cclauses_buf;
16338 c_parser_consume_token (parser);
16339 if (!flag_openmp) /* flag_openmp_simd */
16340 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16341 block = c_begin_omp_parallel ();
16342 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16343 stmt
16344 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16345 block);
16346 if (ret == NULL_TREE)
16347 return ret;
16348 OMP_PARALLEL_COMBINED (stmt) = 1;
16349 return stmt;
16351 /* When combined with distribute, parallel has to be followed by for.
16352 #pragma omp target parallel is allowed though. */
16353 else if (cclauses
16354 && (mask & (OMP_CLAUSE_MASK_1
16355 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16357 error_at (loc, "expected %<for%> after %qs", p_name);
16358 c_parser_skip_to_pragma_eol (parser);
16359 return NULL_TREE;
16361 else if (!flag_openmp) /* flag_openmp_simd */
16363 c_parser_skip_to_pragma_eol (parser, false);
16364 return NULL_TREE;
16366 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16368 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16369 if (strcmp (p, "sections") == 0)
16371 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16372 if (cclauses == NULL)
16373 cclauses = cclauses_buf;
16375 c_parser_consume_token (parser);
16376 block = c_begin_omp_parallel ();
16377 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16378 stmt = c_finish_omp_parallel (loc,
16379 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16380 block);
16381 OMP_PARALLEL_COMBINED (stmt) = 1;
16382 return stmt;
16386 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16387 if (cclauses)
16389 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16390 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16393 block = c_begin_omp_parallel ();
16394 c_parser_statement (parser, if_p);
16395 stmt = c_finish_omp_parallel (loc, clauses, block);
16397 return stmt;
16400 /* OpenMP 2.5:
16401 # pragma omp single single-clause[optseq] new-line
16402 structured-block
16404 LOC is the location of the #pragma.
16407 #define OMP_SINGLE_CLAUSE_MASK \
16408 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16413 static tree
16414 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16416 tree stmt = make_node (OMP_SINGLE);
16417 SET_EXPR_LOCATION (stmt, loc);
16418 TREE_TYPE (stmt) = void_type_node;
16420 OMP_SINGLE_CLAUSES (stmt)
16421 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16422 "#pragma omp single");
16423 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16425 return add_stmt (stmt);
16428 /* OpenMP 3.0:
16429 # pragma omp task task-clause[optseq] new-line
16431 LOC is the location of the #pragma.
16434 #define OMP_TASK_CLAUSE_MASK \
16435 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16446 static tree
16447 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16449 tree clauses, block;
16451 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16452 "#pragma omp task");
16454 block = c_begin_omp_task ();
16455 c_parser_statement (parser, if_p);
16456 return c_finish_omp_task (loc, clauses, block);
16459 /* OpenMP 3.0:
16460 # pragma omp taskwait new-line
16463 static void
16464 c_parser_omp_taskwait (c_parser *parser)
16466 location_t loc = c_parser_peek_token (parser)->location;
16467 c_parser_consume_pragma (parser);
16468 c_parser_skip_to_pragma_eol (parser);
16470 c_finish_omp_taskwait (loc);
16473 /* OpenMP 3.1:
16474 # pragma omp taskyield new-line
16477 static void
16478 c_parser_omp_taskyield (c_parser *parser)
16480 location_t loc = c_parser_peek_token (parser)->location;
16481 c_parser_consume_pragma (parser);
16482 c_parser_skip_to_pragma_eol (parser);
16484 c_finish_omp_taskyield (loc);
16487 /* OpenMP 4.0:
16488 # pragma omp taskgroup new-line
16491 static tree
16492 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16494 location_t loc = c_parser_peek_token (parser)->location;
16495 c_parser_skip_to_pragma_eol (parser);
16496 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16497 if_p));
16500 /* OpenMP 4.0:
16501 # pragma omp cancel cancel-clause[optseq] new-line
16503 LOC is the location of the #pragma.
16506 #define OMP_CANCEL_CLAUSE_MASK \
16507 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16513 static void
16514 c_parser_omp_cancel (c_parser *parser)
16516 location_t loc = c_parser_peek_token (parser)->location;
16518 c_parser_consume_pragma (parser);
16519 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16520 "#pragma omp cancel");
16522 c_finish_omp_cancel (loc, clauses);
16525 /* OpenMP 4.0:
16526 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16528 LOC is the location of the #pragma.
16531 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16532 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16537 static void
16538 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16540 location_t loc = c_parser_peek_token (parser)->location;
16541 tree clauses;
16542 bool point_seen = false;
16544 c_parser_consume_pragma (parser);
16545 if (c_parser_next_token_is (parser, CPP_NAME))
16547 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16548 if (strcmp (p, "point") == 0)
16550 c_parser_consume_token (parser);
16551 point_seen = true;
16554 if (!point_seen)
16556 c_parser_error (parser, "expected %<point%>");
16557 c_parser_skip_to_pragma_eol (parser);
16558 return;
16561 if (context != pragma_compound)
16563 if (context == pragma_stmt)
16564 error_at (loc,
16565 "%<#pragma %s%> may only be used in compound statements",
16566 "omp cancellation point");
16567 else
16568 c_parser_error (parser, "expected declaration specifiers");
16569 c_parser_skip_to_pragma_eol (parser, false);
16570 return;
16573 clauses
16574 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16575 "#pragma omp cancellation point");
16577 c_finish_omp_cancellation_point (loc, clauses);
16580 /* OpenMP 4.0:
16581 #pragma omp distribute distribute-clause[optseq] new-line
16582 for-loop */
16584 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16591 static tree
16592 c_parser_omp_distribute (location_t loc, c_parser *parser,
16593 char *p_name, omp_clause_mask mask, tree *cclauses,
16594 bool *if_p)
16596 tree clauses, block, ret;
16598 strcat (p_name, " distribute");
16599 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16601 if (c_parser_next_token_is (parser, CPP_NAME))
16603 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16604 bool simd = false;
16605 bool parallel = false;
16607 if (strcmp (p, "simd") == 0)
16608 simd = true;
16609 else
16610 parallel = strcmp (p, "parallel") == 0;
16611 if (parallel || simd)
16613 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16614 if (cclauses == NULL)
16615 cclauses = cclauses_buf;
16616 c_parser_consume_token (parser);
16617 if (!flag_openmp) /* flag_openmp_simd */
16619 if (simd)
16620 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16621 if_p);
16622 else
16623 return c_parser_omp_parallel (loc, parser, p_name, mask,
16624 cclauses, if_p);
16626 block = c_begin_compound_stmt (true);
16627 if (simd)
16628 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16629 if_p);
16630 else
16631 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16632 if_p);
16633 block = c_end_compound_stmt (loc, block, true);
16634 if (ret == NULL)
16635 return ret;
16636 ret = make_node (OMP_DISTRIBUTE);
16637 TREE_TYPE (ret) = void_type_node;
16638 OMP_FOR_BODY (ret) = block;
16639 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16640 SET_EXPR_LOCATION (ret, loc);
16641 add_stmt (ret);
16642 return ret;
16645 if (!flag_openmp) /* flag_openmp_simd */
16647 c_parser_skip_to_pragma_eol (parser, false);
16648 return NULL_TREE;
16651 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16652 if (cclauses)
16654 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16655 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16658 block = c_begin_compound_stmt (true);
16659 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16660 if_p);
16661 block = c_end_compound_stmt (loc, block, true);
16662 add_stmt (block);
16664 return ret;
16667 /* OpenMP 4.0:
16668 # pragma omp teams teams-clause[optseq] new-line
16669 structured-block */
16671 #define OMP_TEAMS_CLAUSE_MASK \
16672 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16680 static tree
16681 c_parser_omp_teams (location_t loc, c_parser *parser,
16682 char *p_name, omp_clause_mask mask, tree *cclauses,
16683 bool *if_p)
16685 tree clauses, block, ret;
16687 strcat (p_name, " teams");
16688 mask |= OMP_TEAMS_CLAUSE_MASK;
16690 if (c_parser_next_token_is (parser, CPP_NAME))
16692 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16693 if (strcmp (p, "distribute") == 0)
16695 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16696 if (cclauses == NULL)
16697 cclauses = cclauses_buf;
16699 c_parser_consume_token (parser);
16700 if (!flag_openmp) /* flag_openmp_simd */
16701 return c_parser_omp_distribute (loc, parser, p_name, mask,
16702 cclauses, if_p);
16703 block = c_begin_compound_stmt (true);
16704 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16705 if_p);
16706 block = c_end_compound_stmt (loc, block, true);
16707 if (ret == NULL)
16708 return ret;
16709 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16710 ret = make_node (OMP_TEAMS);
16711 TREE_TYPE (ret) = void_type_node;
16712 OMP_TEAMS_CLAUSES (ret) = clauses;
16713 OMP_TEAMS_BODY (ret) = block;
16714 OMP_TEAMS_COMBINED (ret) = 1;
16715 return add_stmt (ret);
16718 if (!flag_openmp) /* flag_openmp_simd */
16720 c_parser_skip_to_pragma_eol (parser, false);
16721 return NULL_TREE;
16724 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16725 if (cclauses)
16727 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16728 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16731 tree stmt = make_node (OMP_TEAMS);
16732 TREE_TYPE (stmt) = void_type_node;
16733 OMP_TEAMS_CLAUSES (stmt) = clauses;
16734 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16736 return add_stmt (stmt);
16739 /* OpenMP 4.0:
16740 # pragma omp target data target-data-clause[optseq] new-line
16741 structured-block */
16743 #define OMP_TARGET_DATA_CLAUSE_MASK \
16744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16749 static tree
16750 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16752 tree clauses
16753 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16754 "#pragma omp target data");
16755 int map_seen = 0;
16756 for (tree *pc = &clauses; *pc;)
16758 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16759 switch (OMP_CLAUSE_MAP_KIND (*pc))
16761 case GOMP_MAP_TO:
16762 case GOMP_MAP_ALWAYS_TO:
16763 case GOMP_MAP_FROM:
16764 case GOMP_MAP_ALWAYS_FROM:
16765 case GOMP_MAP_TOFROM:
16766 case GOMP_MAP_ALWAYS_TOFROM:
16767 case GOMP_MAP_ALLOC:
16768 map_seen = 3;
16769 break;
16770 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16771 case GOMP_MAP_ALWAYS_POINTER:
16772 break;
16773 default:
16774 map_seen |= 1;
16775 error_at (OMP_CLAUSE_LOCATION (*pc),
16776 "%<#pragma omp target data%> with map-type other "
16777 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16778 "on %<map%> clause");
16779 *pc = OMP_CLAUSE_CHAIN (*pc);
16780 continue;
16782 pc = &OMP_CLAUSE_CHAIN (*pc);
16785 if (map_seen != 3)
16787 if (map_seen == 0)
16788 error_at (loc,
16789 "%<#pragma omp target data%> must contain at least "
16790 "one %<map%> clause");
16791 return NULL_TREE;
16794 tree stmt = make_node (OMP_TARGET_DATA);
16795 TREE_TYPE (stmt) = void_type_node;
16796 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16797 keep_next_level ();
16798 tree block = c_begin_compound_stmt (true);
16799 add_stmt (c_parser_omp_structured_block (parser, if_p));
16800 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16802 SET_EXPR_LOCATION (stmt, loc);
16803 return add_stmt (stmt);
16806 /* OpenMP 4.0:
16807 # pragma omp target update target-update-clause[optseq] new-line */
16809 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16817 static bool
16818 c_parser_omp_target_update (location_t loc, c_parser *parser,
16819 enum pragma_context context)
16821 if (context == pragma_stmt)
16823 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16824 "omp target update");
16825 c_parser_skip_to_pragma_eol (parser, false);
16826 return false;
16829 tree clauses
16830 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16831 "#pragma omp target update");
16832 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16833 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16835 error_at (loc,
16836 "%<#pragma omp target update%> must contain at least one "
16837 "%<from%> or %<to%> clauses");
16838 return false;
16841 tree stmt = make_node (OMP_TARGET_UPDATE);
16842 TREE_TYPE (stmt) = void_type_node;
16843 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16844 SET_EXPR_LOCATION (stmt, loc);
16845 add_stmt (stmt);
16846 return false;
16849 /* OpenMP 4.5:
16850 # pragma omp target enter data target-data-clause[optseq] new-line */
16852 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16853 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16859 static tree
16860 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16861 enum pragma_context context)
16863 bool data_seen = false;
16864 if (c_parser_next_token_is (parser, CPP_NAME))
16866 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16867 if (strcmp (p, "data") == 0)
16869 c_parser_consume_token (parser);
16870 data_seen = true;
16873 if (!data_seen)
16875 c_parser_error (parser, "expected %<data%>");
16876 c_parser_skip_to_pragma_eol (parser);
16877 return NULL_TREE;
16880 if (context == pragma_stmt)
16882 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16883 "omp target enter data");
16884 c_parser_skip_to_pragma_eol (parser, false);
16885 return NULL_TREE;
16888 tree clauses
16889 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16890 "#pragma omp target enter data");
16891 int map_seen = 0;
16892 for (tree *pc = &clauses; *pc;)
16894 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16895 switch (OMP_CLAUSE_MAP_KIND (*pc))
16897 case GOMP_MAP_TO:
16898 case GOMP_MAP_ALWAYS_TO:
16899 case GOMP_MAP_ALLOC:
16900 map_seen = 3;
16901 break;
16902 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16903 case GOMP_MAP_ALWAYS_POINTER:
16904 break;
16905 default:
16906 map_seen |= 1;
16907 error_at (OMP_CLAUSE_LOCATION (*pc),
16908 "%<#pragma omp target enter data%> with map-type other "
16909 "than %<to%> or %<alloc%> on %<map%> clause");
16910 *pc = OMP_CLAUSE_CHAIN (*pc);
16911 continue;
16913 pc = &OMP_CLAUSE_CHAIN (*pc);
16916 if (map_seen != 3)
16918 if (map_seen == 0)
16919 error_at (loc,
16920 "%<#pragma omp target enter data%> must contain at least "
16921 "one %<map%> clause");
16922 return NULL_TREE;
16925 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16926 TREE_TYPE (stmt) = void_type_node;
16927 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16928 SET_EXPR_LOCATION (stmt, loc);
16929 add_stmt (stmt);
16930 return stmt;
16933 /* OpenMP 4.5:
16934 # pragma omp target exit data target-data-clause[optseq] new-line */
16936 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16943 static tree
16944 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16945 enum pragma_context context)
16947 bool data_seen = false;
16948 if (c_parser_next_token_is (parser, CPP_NAME))
16950 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16951 if (strcmp (p, "data") == 0)
16953 c_parser_consume_token (parser);
16954 data_seen = true;
16957 if (!data_seen)
16959 c_parser_error (parser, "expected %<data%>");
16960 c_parser_skip_to_pragma_eol (parser);
16961 return NULL_TREE;
16964 if (context == pragma_stmt)
16966 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16967 "omp target exit data");
16968 c_parser_skip_to_pragma_eol (parser, false);
16969 return NULL_TREE;
16972 tree clauses
16973 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16974 "#pragma omp target exit data");
16976 int map_seen = 0;
16977 for (tree *pc = &clauses; *pc;)
16979 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16980 switch (OMP_CLAUSE_MAP_KIND (*pc))
16982 case GOMP_MAP_FROM:
16983 case GOMP_MAP_ALWAYS_FROM:
16984 case GOMP_MAP_RELEASE:
16985 case GOMP_MAP_DELETE:
16986 map_seen = 3;
16987 break;
16988 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16989 case GOMP_MAP_ALWAYS_POINTER:
16990 break;
16991 default:
16992 map_seen |= 1;
16993 error_at (OMP_CLAUSE_LOCATION (*pc),
16994 "%<#pragma omp target exit data%> with map-type other "
16995 "than %<from%>, %<release%> or %<delete%> on %<map%>"
16996 " clause");
16997 *pc = OMP_CLAUSE_CHAIN (*pc);
16998 continue;
17000 pc = &OMP_CLAUSE_CHAIN (*pc);
17003 if (map_seen != 3)
17005 if (map_seen == 0)
17006 error_at (loc,
17007 "%<#pragma omp target exit data%> must contain at least one "
17008 "%<map%> clause");
17009 return NULL_TREE;
17012 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
17013 TREE_TYPE (stmt) = void_type_node;
17014 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
17015 SET_EXPR_LOCATION (stmt, loc);
17016 add_stmt (stmt);
17017 return stmt;
17020 /* OpenMP 4.0:
17021 # pragma omp target target-clause[optseq] new-line
17022 structured-block */
17024 #define OMP_TARGET_CLAUSE_MASK \
17025 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
17030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
17033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17035 static bool
17036 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17038 location_t loc = c_parser_peek_token (parser)->location;
17039 c_parser_consume_pragma (parser);
17040 tree *pc = NULL, stmt, block;
17042 if (context != pragma_stmt && context != pragma_compound)
17044 c_parser_error (parser, "expected declaration specifiers");
17045 c_parser_skip_to_pragma_eol (parser);
17046 return false;
17049 if (c_parser_next_token_is (parser, CPP_NAME))
17051 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17052 enum tree_code ccode = ERROR_MARK;
17054 if (strcmp (p, "teams") == 0)
17055 ccode = OMP_TEAMS;
17056 else if (strcmp (p, "parallel") == 0)
17057 ccode = OMP_PARALLEL;
17058 else if (strcmp (p, "simd") == 0)
17059 ccode = OMP_SIMD;
17060 if (ccode != ERROR_MARK)
17062 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17063 char p_name[sizeof ("#pragma omp target teams distribute "
17064 "parallel for simd")];
17066 c_parser_consume_token (parser);
17067 strcpy (p_name, "#pragma omp target");
17068 if (!flag_openmp) /* flag_openmp_simd */
17070 tree stmt;
17071 switch (ccode)
17073 case OMP_TEAMS:
17074 stmt = c_parser_omp_teams (loc, parser, p_name,
17075 OMP_TARGET_CLAUSE_MASK,
17076 cclauses, if_p);
17077 break;
17078 case OMP_PARALLEL:
17079 stmt = c_parser_omp_parallel (loc, parser, p_name,
17080 OMP_TARGET_CLAUSE_MASK,
17081 cclauses, if_p);
17082 break;
17083 case OMP_SIMD:
17084 stmt = c_parser_omp_simd (loc, parser, p_name,
17085 OMP_TARGET_CLAUSE_MASK,
17086 cclauses, if_p);
17087 break;
17088 default:
17089 gcc_unreachable ();
17091 return stmt != NULL_TREE;
17093 keep_next_level ();
17094 tree block = c_begin_compound_stmt (true), ret;
17095 switch (ccode)
17097 case OMP_TEAMS:
17098 ret = c_parser_omp_teams (loc, parser, p_name,
17099 OMP_TARGET_CLAUSE_MASK, cclauses,
17100 if_p);
17101 break;
17102 case OMP_PARALLEL:
17103 ret = c_parser_omp_parallel (loc, parser, p_name,
17104 OMP_TARGET_CLAUSE_MASK, cclauses,
17105 if_p);
17106 break;
17107 case OMP_SIMD:
17108 ret = c_parser_omp_simd (loc, parser, p_name,
17109 OMP_TARGET_CLAUSE_MASK, cclauses,
17110 if_p);
17111 break;
17112 default:
17113 gcc_unreachable ();
17115 block = c_end_compound_stmt (loc, block, true);
17116 if (ret == NULL_TREE)
17117 return false;
17118 if (ccode == OMP_TEAMS)
17120 /* For combined target teams, ensure the num_teams and
17121 thread_limit clause expressions are evaluated on the host,
17122 before entering the target construct. */
17123 tree c;
17124 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17125 c; c = OMP_CLAUSE_CHAIN (c))
17126 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17127 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17128 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17130 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17131 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17132 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17133 expr, NULL_TREE, NULL_TREE);
17134 add_stmt (expr);
17135 OMP_CLAUSE_OPERAND (c, 0) = expr;
17136 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17137 OMP_CLAUSE_FIRSTPRIVATE);
17138 OMP_CLAUSE_DECL (tc) = tmp;
17139 OMP_CLAUSE_CHAIN (tc)
17140 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17141 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17144 tree stmt = make_node (OMP_TARGET);
17145 TREE_TYPE (stmt) = void_type_node;
17146 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17147 OMP_TARGET_BODY (stmt) = block;
17148 OMP_TARGET_COMBINED (stmt) = 1;
17149 add_stmt (stmt);
17150 pc = &OMP_TARGET_CLAUSES (stmt);
17151 goto check_clauses;
17153 else if (!flag_openmp) /* flag_openmp_simd */
17155 c_parser_skip_to_pragma_eol (parser, false);
17156 return false;
17158 else if (strcmp (p, "data") == 0)
17160 c_parser_consume_token (parser);
17161 c_parser_omp_target_data (loc, parser, if_p);
17162 return true;
17164 else if (strcmp (p, "enter") == 0)
17166 c_parser_consume_token (parser);
17167 c_parser_omp_target_enter_data (loc, parser, context);
17168 return false;
17170 else if (strcmp (p, "exit") == 0)
17172 c_parser_consume_token (parser);
17173 c_parser_omp_target_exit_data (loc, parser, context);
17174 return false;
17176 else if (strcmp (p, "update") == 0)
17178 c_parser_consume_token (parser);
17179 return c_parser_omp_target_update (loc, parser, context);
17182 if (!flag_openmp) /* flag_openmp_simd */
17184 c_parser_skip_to_pragma_eol (parser, false);
17185 return false;
17188 stmt = make_node (OMP_TARGET);
17189 TREE_TYPE (stmt) = void_type_node;
17191 OMP_TARGET_CLAUSES (stmt)
17192 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17193 "#pragma omp target");
17194 pc = &OMP_TARGET_CLAUSES (stmt);
17195 keep_next_level ();
17196 block = c_begin_compound_stmt (true);
17197 add_stmt (c_parser_omp_structured_block (parser, if_p));
17198 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17200 SET_EXPR_LOCATION (stmt, loc);
17201 add_stmt (stmt);
17203 check_clauses:
17204 while (*pc)
17206 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17207 switch (OMP_CLAUSE_MAP_KIND (*pc))
17209 case GOMP_MAP_TO:
17210 case GOMP_MAP_ALWAYS_TO:
17211 case GOMP_MAP_FROM:
17212 case GOMP_MAP_ALWAYS_FROM:
17213 case GOMP_MAP_TOFROM:
17214 case GOMP_MAP_ALWAYS_TOFROM:
17215 case GOMP_MAP_ALLOC:
17216 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17217 case GOMP_MAP_ALWAYS_POINTER:
17218 break;
17219 default:
17220 error_at (OMP_CLAUSE_LOCATION (*pc),
17221 "%<#pragma omp target%> with map-type other "
17222 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17223 "on %<map%> clause");
17224 *pc = OMP_CLAUSE_CHAIN (*pc);
17225 continue;
17227 pc = &OMP_CLAUSE_CHAIN (*pc);
17229 return true;
17232 /* OpenMP 4.0:
17233 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17235 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17236 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17243 static void
17244 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17246 auto_vec<c_token> clauses;
17247 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17249 c_token *token = c_parser_peek_token (parser);
17250 if (token->type == CPP_EOF)
17252 c_parser_skip_to_pragma_eol (parser);
17253 return;
17255 clauses.safe_push (*token);
17256 c_parser_consume_token (parser);
17258 clauses.safe_push (*c_parser_peek_token (parser));
17259 c_parser_skip_to_pragma_eol (parser);
17261 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17263 if (c_parser_peek_token (parser)->pragma_kind
17264 != PRAGMA_OMP_DECLARE
17265 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17266 || strcmp (IDENTIFIER_POINTER
17267 (c_parser_peek_2nd_token (parser)->value),
17268 "simd") != 0)
17270 c_parser_error (parser,
17271 "%<#pragma omp declare simd%> must be followed by "
17272 "function declaration or definition or another "
17273 "%<#pragma omp declare simd%>");
17274 return;
17276 c_parser_consume_pragma (parser);
17277 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17279 c_token *token = c_parser_peek_token (parser);
17280 if (token->type == CPP_EOF)
17282 c_parser_skip_to_pragma_eol (parser);
17283 return;
17285 clauses.safe_push (*token);
17286 c_parser_consume_token (parser);
17288 clauses.safe_push (*c_parser_peek_token (parser));
17289 c_parser_skip_to_pragma_eol (parser);
17292 /* Make sure nothing tries to read past the end of the tokens. */
17293 c_token eof_token;
17294 memset (&eof_token, 0, sizeof (eof_token));
17295 eof_token.type = CPP_EOF;
17296 clauses.safe_push (eof_token);
17297 clauses.safe_push (eof_token);
17299 switch (context)
17301 case pragma_external:
17302 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17303 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17305 int ext = disable_extension_diagnostics ();
17307 c_parser_consume_token (parser);
17308 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17309 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17310 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17311 NULL, clauses);
17312 restore_extension_diagnostics (ext);
17314 else
17315 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17316 NULL, clauses);
17317 break;
17318 case pragma_struct:
17319 case pragma_param:
17320 case pragma_stmt:
17321 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17322 "function declaration or definition");
17323 break;
17324 case pragma_compound:
17325 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17326 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17328 int ext = disable_extension_diagnostics ();
17330 c_parser_consume_token (parser);
17331 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17332 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17333 if (c_parser_next_tokens_start_declaration (parser))
17335 c_parser_declaration_or_fndef (parser, true, true, true, true,
17336 true, NULL, clauses);
17337 restore_extension_diagnostics (ext);
17338 break;
17340 restore_extension_diagnostics (ext);
17342 else if (c_parser_next_tokens_start_declaration (parser))
17344 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17345 NULL, clauses);
17346 break;
17348 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17349 "function declaration or definition");
17350 break;
17351 default:
17352 gcc_unreachable ();
17356 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17357 and put that into "omp declare simd" attribute. */
17359 static void
17360 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17361 vec<c_token> clauses)
17363 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17364 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17365 has already processed the tokens. */
17366 if (clauses.exists () && clauses[0].type == CPP_EOF)
17367 return;
17368 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17370 error ("%<#pragma omp declare simd%> not immediately followed by "
17371 "a function declaration or definition");
17372 clauses[0].type = CPP_EOF;
17373 return;
17375 if (clauses.exists () && clauses[0].type != CPP_NAME)
17377 error_at (DECL_SOURCE_LOCATION (fndecl),
17378 "%<#pragma omp declare simd%> not immediately followed by "
17379 "a single function declaration or definition");
17380 clauses[0].type = CPP_EOF;
17381 return;
17384 if (parms == NULL_TREE)
17385 parms = DECL_ARGUMENTS (fndecl);
17387 unsigned int tokens_avail = parser->tokens_avail;
17388 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17391 parser->tokens = clauses.address ();
17392 parser->tokens_avail = clauses.length ();
17394 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17395 while (parser->tokens_avail > 3)
17397 c_token *token = c_parser_peek_token (parser);
17398 gcc_assert (token->type == CPP_NAME
17399 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17400 c_parser_consume_token (parser);
17401 parser->in_pragma = true;
17403 tree c = NULL_TREE;
17404 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17405 "#pragma omp declare simd");
17406 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17407 if (c != NULL_TREE)
17408 c = tree_cons (NULL_TREE, c, NULL_TREE);
17409 c = build_tree_list (get_identifier ("omp declare simd"), c);
17410 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17411 DECL_ATTRIBUTES (fndecl) = c;
17414 parser->tokens = &parser->tokens_buf[0];
17415 parser->tokens_avail = tokens_avail;
17416 if (clauses.exists ())
17417 clauses[0].type = CPP_PRAGMA;
17421 /* OpenMP 4.0:
17422 # pragma omp declare target new-line
17423 declarations and definitions
17424 # pragma omp end declare target new-line
17426 OpenMP 4.5:
17427 # pragma omp declare target ( extended-list ) new-line
17429 # pragma omp declare target declare-target-clauses[seq] new-line */
17431 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17432 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17435 static void
17436 c_parser_omp_declare_target (c_parser *parser)
17438 location_t loc = c_parser_peek_token (parser)->location;
17439 tree clauses = NULL_TREE;
17440 if (c_parser_next_token_is (parser, CPP_NAME))
17441 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17442 "#pragma omp declare target");
17443 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17445 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17446 clauses);
17447 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17448 c_parser_skip_to_pragma_eol (parser);
17450 else
17452 c_parser_skip_to_pragma_eol (parser);
17453 current_omp_declare_target_attribute++;
17454 return;
17456 if (current_omp_declare_target_attribute)
17457 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17458 "%<#pragma omp declare target%> without clauses and "
17459 "%<#pragma omp end declare target%>");
17460 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17462 tree t = OMP_CLAUSE_DECL (c), id;
17463 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17464 tree at2 = lookup_attribute ("omp declare target link",
17465 DECL_ATTRIBUTES (t));
17466 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17468 id = get_identifier ("omp declare target link");
17469 std::swap (at1, at2);
17471 else
17472 id = get_identifier ("omp declare target");
17473 if (at2)
17475 error_at (OMP_CLAUSE_LOCATION (c),
17476 "%qD specified both in declare target %<link%> and %<to%>"
17477 " clauses", t);
17478 continue;
17480 if (!at1)
17482 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17483 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17484 continue;
17486 symtab_node *node = symtab_node::get (t);
17487 if (node != NULL)
17489 node->offloadable = 1;
17490 if (ENABLE_OFFLOADING)
17492 g->have_offload = true;
17493 if (is_a <varpool_node *> (node))
17494 vec_safe_push (offload_vars, t);
17501 static void
17502 c_parser_omp_end_declare_target (c_parser *parser)
17504 location_t loc = c_parser_peek_token (parser)->location;
17505 c_parser_consume_pragma (parser);
17506 if (c_parser_next_token_is (parser, CPP_NAME)
17507 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17508 "declare") == 0)
17510 c_parser_consume_token (parser);
17511 if (c_parser_next_token_is (parser, CPP_NAME)
17512 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17513 "target") == 0)
17514 c_parser_consume_token (parser);
17515 else
17517 c_parser_error (parser, "expected %<target%>");
17518 c_parser_skip_to_pragma_eol (parser);
17519 return;
17522 else
17524 c_parser_error (parser, "expected %<declare%>");
17525 c_parser_skip_to_pragma_eol (parser);
17526 return;
17528 c_parser_skip_to_pragma_eol (parser);
17529 if (!current_omp_declare_target_attribute)
17530 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17531 "%<#pragma omp declare target%>");
17532 else
17533 current_omp_declare_target_attribute--;
17537 /* OpenMP 4.0
17538 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17539 initializer-clause[opt] new-line
17541 initializer-clause:
17542 initializer (omp_priv = initializer)
17543 initializer (function-name (argument-list)) */
17545 static void
17546 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17548 unsigned int tokens_avail = 0, i;
17549 vec<tree> types = vNULL;
17550 vec<c_token> clauses = vNULL;
17551 enum tree_code reduc_code = ERROR_MARK;
17552 tree reduc_id = NULL_TREE;
17553 tree type;
17554 location_t rloc = c_parser_peek_token (parser)->location;
17556 if (context == pragma_struct || context == pragma_param)
17558 error ("%<#pragma omp declare reduction%> not at file or block scope");
17559 goto fail;
17562 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17563 goto fail;
17565 switch (c_parser_peek_token (parser)->type)
17567 case CPP_PLUS:
17568 reduc_code = PLUS_EXPR;
17569 break;
17570 case CPP_MULT:
17571 reduc_code = MULT_EXPR;
17572 break;
17573 case CPP_MINUS:
17574 reduc_code = MINUS_EXPR;
17575 break;
17576 case CPP_AND:
17577 reduc_code = BIT_AND_EXPR;
17578 break;
17579 case CPP_XOR:
17580 reduc_code = BIT_XOR_EXPR;
17581 break;
17582 case CPP_OR:
17583 reduc_code = BIT_IOR_EXPR;
17584 break;
17585 case CPP_AND_AND:
17586 reduc_code = TRUTH_ANDIF_EXPR;
17587 break;
17588 case CPP_OR_OR:
17589 reduc_code = TRUTH_ORIF_EXPR;
17590 break;
17591 case CPP_NAME:
17592 const char *p;
17593 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17594 if (strcmp (p, "min") == 0)
17596 reduc_code = MIN_EXPR;
17597 break;
17599 if (strcmp (p, "max") == 0)
17601 reduc_code = MAX_EXPR;
17602 break;
17604 reduc_id = c_parser_peek_token (parser)->value;
17605 break;
17606 default:
17607 c_parser_error (parser,
17608 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17609 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17610 goto fail;
17613 tree orig_reduc_id, reduc_decl;
17614 orig_reduc_id = reduc_id;
17615 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17616 reduc_decl = c_omp_reduction_decl (reduc_id);
17617 c_parser_consume_token (parser);
17619 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17620 goto fail;
17622 while (true)
17624 location_t loc = c_parser_peek_token (parser)->location;
17625 struct c_type_name *ctype = c_parser_type_name (parser);
17626 if (ctype != NULL)
17628 type = groktypename (ctype, NULL, NULL);
17629 if (type == error_mark_node)
17631 else if ((INTEGRAL_TYPE_P (type)
17632 || TREE_CODE (type) == REAL_TYPE
17633 || TREE_CODE (type) == COMPLEX_TYPE)
17634 && orig_reduc_id == NULL_TREE)
17635 error_at (loc, "predeclared arithmetic type in "
17636 "%<#pragma omp declare reduction%>");
17637 else if (TREE_CODE (type) == FUNCTION_TYPE
17638 || TREE_CODE (type) == ARRAY_TYPE)
17639 error_at (loc, "function or array type in "
17640 "%<#pragma omp declare reduction%>");
17641 else if (TYPE_ATOMIC (type))
17642 error_at (loc, "%<_Atomic%> qualified type in "
17643 "%<#pragma omp declare reduction%>");
17644 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17645 error_at (loc, "const, volatile or restrict qualified type in "
17646 "%<#pragma omp declare reduction%>");
17647 else
17649 tree t;
17650 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17651 if (comptypes (TREE_PURPOSE (t), type))
17653 error_at (loc, "redeclaration of %qs "
17654 "%<#pragma omp declare reduction%> for "
17655 "type %qT",
17656 IDENTIFIER_POINTER (reduc_id)
17657 + sizeof ("omp declare reduction ") - 1,
17658 type);
17659 location_t ploc
17660 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17661 0));
17662 error_at (ploc, "previous %<#pragma omp declare "
17663 "reduction%>");
17664 break;
17666 if (t == NULL_TREE)
17667 types.safe_push (type);
17669 if (c_parser_next_token_is (parser, CPP_COMMA))
17670 c_parser_consume_token (parser);
17671 else
17672 break;
17674 else
17675 break;
17678 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17679 || types.is_empty ())
17681 fail:
17682 clauses.release ();
17683 types.release ();
17684 while (true)
17686 c_token *token = c_parser_peek_token (parser);
17687 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17688 break;
17689 c_parser_consume_token (parser);
17691 c_parser_skip_to_pragma_eol (parser);
17692 return;
17695 if (types.length () > 1)
17697 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17699 c_token *token = c_parser_peek_token (parser);
17700 if (token->type == CPP_EOF)
17701 goto fail;
17702 clauses.safe_push (*token);
17703 c_parser_consume_token (parser);
17705 clauses.safe_push (*c_parser_peek_token (parser));
17706 c_parser_skip_to_pragma_eol (parser);
17708 /* Make sure nothing tries to read past the end of the tokens. */
17709 c_token eof_token;
17710 memset (&eof_token, 0, sizeof (eof_token));
17711 eof_token.type = CPP_EOF;
17712 clauses.safe_push (eof_token);
17713 clauses.safe_push (eof_token);
17716 int errs = errorcount;
17717 FOR_EACH_VEC_ELT (types, i, type)
17719 tokens_avail = parser->tokens_avail;
17720 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17721 if (!clauses.is_empty ())
17723 parser->tokens = clauses.address ();
17724 parser->tokens_avail = clauses.length ();
17725 parser->in_pragma = true;
17728 bool nested = current_function_decl != NULL_TREE;
17729 if (nested)
17730 c_push_function_context ();
17731 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17732 reduc_id, default_function_type);
17733 current_function_decl = fndecl;
17734 allocate_struct_function (fndecl, true);
17735 push_scope ();
17736 tree stmt = push_stmt_list ();
17737 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17738 warn about these. */
17739 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17740 get_identifier ("omp_out"), type);
17741 DECL_ARTIFICIAL (omp_out) = 1;
17742 DECL_CONTEXT (omp_out) = fndecl;
17743 pushdecl (omp_out);
17744 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17745 get_identifier ("omp_in"), type);
17746 DECL_ARTIFICIAL (omp_in) = 1;
17747 DECL_CONTEXT (omp_in) = fndecl;
17748 pushdecl (omp_in);
17749 struct c_expr combiner = c_parser_expression (parser);
17750 struct c_expr initializer;
17751 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17752 bool bad = false;
17753 initializer.set_error ();
17754 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17755 bad = true;
17756 else if (c_parser_next_token_is (parser, CPP_NAME)
17757 && strcmp (IDENTIFIER_POINTER
17758 (c_parser_peek_token (parser)->value),
17759 "initializer") == 0)
17761 c_parser_consume_token (parser);
17762 pop_scope ();
17763 push_scope ();
17764 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17765 get_identifier ("omp_priv"), type);
17766 DECL_ARTIFICIAL (omp_priv) = 1;
17767 DECL_INITIAL (omp_priv) = error_mark_node;
17768 DECL_CONTEXT (omp_priv) = fndecl;
17769 pushdecl (omp_priv);
17770 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17771 get_identifier ("omp_orig"), type);
17772 DECL_ARTIFICIAL (omp_orig) = 1;
17773 DECL_CONTEXT (omp_orig) = fndecl;
17774 pushdecl (omp_orig);
17775 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17776 bad = true;
17777 else if (!c_parser_next_token_is (parser, CPP_NAME))
17779 c_parser_error (parser, "expected %<omp_priv%> or "
17780 "function-name");
17781 bad = true;
17783 else if (strcmp (IDENTIFIER_POINTER
17784 (c_parser_peek_token (parser)->value),
17785 "omp_priv") != 0)
17787 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17788 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17790 c_parser_error (parser, "expected function-name %<(%>");
17791 bad = true;
17793 else
17794 initializer = c_parser_postfix_expression (parser);
17795 if (initializer.value
17796 && TREE_CODE (initializer.value) == CALL_EXPR)
17798 int j;
17799 tree c = initializer.value;
17800 for (j = 0; j < call_expr_nargs (c); j++)
17802 tree a = CALL_EXPR_ARG (c, j);
17803 STRIP_NOPS (a);
17804 if (TREE_CODE (a) == ADDR_EXPR
17805 && TREE_OPERAND (a, 0) == omp_priv)
17806 break;
17808 if (j == call_expr_nargs (c))
17809 error ("one of the initializer call arguments should be "
17810 "%<&omp_priv%>");
17813 else
17815 c_parser_consume_token (parser);
17816 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17817 bad = true;
17818 else
17820 tree st = push_stmt_list ();
17821 location_t loc = c_parser_peek_token (parser)->location;
17822 rich_location richloc (line_table, loc);
17823 start_init (omp_priv, NULL_TREE, 0, &richloc);
17824 struct c_expr init = c_parser_initializer (parser);
17825 finish_init ();
17826 finish_decl (omp_priv, loc, init.value,
17827 init.original_type, NULL_TREE);
17828 pop_stmt_list (st);
17831 if (!bad
17832 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17833 bad = true;
17836 if (!bad)
17838 c_parser_skip_to_pragma_eol (parser);
17840 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17841 DECL_INITIAL (reduc_decl));
17842 DECL_INITIAL (reduc_decl) = t;
17843 DECL_SOURCE_LOCATION (omp_out) = rloc;
17844 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17845 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17846 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17847 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17848 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17849 if (omp_priv)
17851 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17852 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17853 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17854 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17855 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17856 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17857 walk_tree (&DECL_INITIAL (omp_priv),
17858 c_check_omp_declare_reduction_r,
17859 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17863 pop_stmt_list (stmt);
17864 pop_scope ();
17865 if (cfun->language != NULL)
17867 ggc_free (cfun->language);
17868 cfun->language = NULL;
17870 set_cfun (NULL);
17871 current_function_decl = NULL_TREE;
17872 if (nested)
17873 c_pop_function_context ();
17875 if (!clauses.is_empty ())
17877 parser->tokens = &parser->tokens_buf[0];
17878 parser->tokens_avail = tokens_avail;
17880 if (bad)
17881 goto fail;
17882 if (errs != errorcount)
17883 break;
17886 clauses.release ();
17887 types.release ();
17891 /* OpenMP 4.0
17892 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17893 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17894 initializer-clause[opt] new-line
17895 #pragma omp declare target new-line */
17897 static void
17898 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17900 c_parser_consume_pragma (parser);
17901 if (c_parser_next_token_is (parser, CPP_NAME))
17903 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17904 if (strcmp (p, "simd") == 0)
17906 /* c_parser_consume_token (parser); done in
17907 c_parser_omp_declare_simd. */
17908 c_parser_omp_declare_simd (parser, context);
17909 return;
17911 if (strcmp (p, "reduction") == 0)
17913 c_parser_consume_token (parser);
17914 c_parser_omp_declare_reduction (parser, context);
17915 return;
17917 if (!flag_openmp) /* flag_openmp_simd */
17919 c_parser_skip_to_pragma_eol (parser, false);
17920 return;
17922 if (strcmp (p, "target") == 0)
17924 c_parser_consume_token (parser);
17925 c_parser_omp_declare_target (parser);
17926 return;
17930 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17931 "or %<target%>");
17932 c_parser_skip_to_pragma_eol (parser);
17935 /* OpenMP 4.5:
17936 #pragma omp taskloop taskloop-clause[optseq] new-line
17937 for-loop
17939 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17940 for-loop */
17942 #define OMP_TASKLOOP_CLAUSE_MASK \
17943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17958 static tree
17959 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17960 char *p_name, omp_clause_mask mask, tree *cclauses,
17961 bool *if_p)
17963 tree clauses, block, ret;
17965 strcat (p_name, " taskloop");
17966 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17968 if (c_parser_next_token_is (parser, CPP_NAME))
17970 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17972 if (strcmp (p, "simd") == 0)
17974 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17975 if (cclauses == NULL)
17976 cclauses = cclauses_buf;
17977 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17978 c_parser_consume_token (parser);
17979 if (!flag_openmp) /* flag_openmp_simd */
17980 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17981 if_p);
17982 block = c_begin_compound_stmt (true);
17983 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17984 block = c_end_compound_stmt (loc, block, true);
17985 if (ret == NULL)
17986 return ret;
17987 ret = make_node (OMP_TASKLOOP);
17988 TREE_TYPE (ret) = void_type_node;
17989 OMP_FOR_BODY (ret) = block;
17990 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17991 SET_EXPR_LOCATION (ret, loc);
17992 add_stmt (ret);
17993 return ret;
17996 if (!flag_openmp) /* flag_openmp_simd */
17998 c_parser_skip_to_pragma_eol (parser, false);
17999 return NULL_TREE;
18002 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18003 if (cclauses)
18005 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
18006 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18009 block = c_begin_compound_stmt (true);
18010 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
18011 block = c_end_compound_stmt (loc, block, true);
18012 add_stmt (block);
18014 return ret;
18017 /* Main entry point to parsing most OpenMP pragmas. */
18019 static void
18020 c_parser_omp_construct (c_parser *parser, bool *if_p)
18022 enum pragma_kind p_kind;
18023 location_t loc;
18024 tree stmt;
18025 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18026 omp_clause_mask mask (0);
18028 loc = c_parser_peek_token (parser)->location;
18029 p_kind = c_parser_peek_token (parser)->pragma_kind;
18030 c_parser_consume_pragma (parser);
18032 switch (p_kind)
18034 case PRAGMA_OACC_ATOMIC:
18035 c_parser_omp_atomic (loc, parser);
18036 return;
18037 case PRAGMA_OACC_CACHE:
18038 strcpy (p_name, "#pragma acc");
18039 stmt = c_parser_oacc_cache (loc, parser);
18040 break;
18041 case PRAGMA_OACC_DATA:
18042 stmt = c_parser_oacc_data (loc, parser, if_p);
18043 break;
18044 case PRAGMA_OACC_HOST_DATA:
18045 stmt = c_parser_oacc_host_data (loc, parser, if_p);
18046 break;
18047 case PRAGMA_OACC_KERNELS:
18048 case PRAGMA_OACC_PARALLEL:
18049 strcpy (p_name, "#pragma acc");
18050 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18051 if_p);
18052 break;
18053 case PRAGMA_OACC_LOOP:
18054 strcpy (p_name, "#pragma acc");
18055 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18056 break;
18057 case PRAGMA_OACC_WAIT:
18058 strcpy (p_name, "#pragma wait");
18059 stmt = c_parser_oacc_wait (loc, parser, p_name);
18060 break;
18061 case PRAGMA_OMP_ATOMIC:
18062 c_parser_omp_atomic (loc, parser);
18063 return;
18064 case PRAGMA_OMP_CRITICAL:
18065 stmt = c_parser_omp_critical (loc, parser, if_p);
18066 break;
18067 case PRAGMA_OMP_DISTRIBUTE:
18068 strcpy (p_name, "#pragma omp");
18069 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18070 break;
18071 case PRAGMA_OMP_FOR:
18072 strcpy (p_name, "#pragma omp");
18073 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18074 break;
18075 case PRAGMA_OMP_MASTER:
18076 stmt = c_parser_omp_master (loc, parser, if_p);
18077 break;
18078 case PRAGMA_OMP_PARALLEL:
18079 strcpy (p_name, "#pragma omp");
18080 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18081 break;
18082 case PRAGMA_OMP_SECTIONS:
18083 strcpy (p_name, "#pragma omp");
18084 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18085 break;
18086 case PRAGMA_OMP_SIMD:
18087 strcpy (p_name, "#pragma omp");
18088 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18089 break;
18090 case PRAGMA_OMP_SINGLE:
18091 stmt = c_parser_omp_single (loc, parser, if_p);
18092 break;
18093 case PRAGMA_OMP_TASK:
18094 stmt = c_parser_omp_task (loc, parser, if_p);
18095 break;
18096 case PRAGMA_OMP_TASKGROUP:
18097 stmt = c_parser_omp_taskgroup (parser, if_p);
18098 break;
18099 case PRAGMA_OMP_TASKLOOP:
18100 strcpy (p_name, "#pragma omp");
18101 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18102 break;
18103 case PRAGMA_OMP_TEAMS:
18104 strcpy (p_name, "#pragma omp");
18105 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18106 break;
18107 default:
18108 gcc_unreachable ();
18111 if (stmt)
18112 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18116 /* OpenMP 2.5:
18117 # pragma omp threadprivate (variable-list) */
18119 static void
18120 c_parser_omp_threadprivate (c_parser *parser)
18122 tree vars, t;
18123 location_t loc;
18125 c_parser_consume_pragma (parser);
18126 loc = c_parser_peek_token (parser)->location;
18127 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18129 /* Mark every variable in VARS to be assigned thread local storage. */
18130 for (t = vars; t; t = TREE_CHAIN (t))
18132 tree v = TREE_PURPOSE (t);
18134 /* FIXME diagnostics: Ideally we should keep individual
18135 locations for all the variables in the var list to make the
18136 following errors more precise. Perhaps
18137 c_parser_omp_var_list_parens() should construct a list of
18138 locations to go along with the var list. */
18140 /* If V had already been marked threadprivate, it doesn't matter
18141 whether it had been used prior to this point. */
18142 if (!VAR_P (v))
18143 error_at (loc, "%qD is not a variable", v);
18144 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18145 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18146 else if (! is_global_var (v))
18147 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18148 else if (TREE_TYPE (v) == error_mark_node)
18150 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18151 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18152 else
18154 if (! DECL_THREAD_LOCAL_P (v))
18156 set_decl_tls_model (v, decl_default_tls_model (v));
18157 /* If rtl has been already set for this var, call
18158 make_decl_rtl once again, so that encode_section_info
18159 has a chance to look at the new decl flags. */
18160 if (DECL_RTL_SET_P (v))
18161 make_decl_rtl (v);
18163 C_DECL_THREADPRIVATE_P (v) = 1;
18167 c_parser_skip_to_pragma_eol (parser);
18170 /* Parse a transaction attribute (GCC Extension).
18172 transaction-attribute:
18173 attributes
18174 [ [ any-word ] ]
18176 The transactional memory language description is written for C++,
18177 and uses the C++0x attribute syntax. For compatibility, allow the
18178 bracket style for transactions in C as well. */
18180 static tree
18181 c_parser_transaction_attributes (c_parser *parser)
18183 tree attr_name, attr = NULL;
18185 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18186 return c_parser_attributes (parser);
18188 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18189 return NULL_TREE;
18190 c_parser_consume_token (parser);
18191 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18192 goto error1;
18194 attr_name = c_parser_attribute_any_word (parser);
18195 if (attr_name)
18197 c_parser_consume_token (parser);
18198 attr = build_tree_list (attr_name, NULL_TREE);
18200 else
18201 c_parser_error (parser, "expected identifier");
18203 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18204 error1:
18205 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18206 return attr;
18209 /* Parse a __transaction_atomic or __transaction_relaxed statement
18210 (GCC Extension).
18212 transaction-statement:
18213 __transaction_atomic transaction-attribute[opt] compound-statement
18214 __transaction_relaxed compound-statement
18216 Note that the only valid attribute is: "outer".
18219 static tree
18220 c_parser_transaction (c_parser *parser, enum rid keyword)
18222 unsigned int old_in = parser->in_transaction;
18223 unsigned int this_in = 1, new_in;
18224 location_t loc = c_parser_peek_token (parser)->location;
18225 tree stmt, attrs;
18227 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18228 || keyword == RID_TRANSACTION_RELAXED)
18229 && c_parser_next_token_is_keyword (parser, keyword));
18230 c_parser_consume_token (parser);
18232 if (keyword == RID_TRANSACTION_RELAXED)
18233 this_in |= TM_STMT_ATTR_RELAXED;
18234 else
18236 attrs = c_parser_transaction_attributes (parser);
18237 if (attrs)
18238 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18241 /* Keep track if we're in the lexical scope of an outer transaction. */
18242 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18244 parser->in_transaction = new_in;
18245 stmt = c_parser_compound_statement (parser);
18246 parser->in_transaction = old_in;
18248 if (flag_tm)
18249 stmt = c_finish_transaction (loc, stmt, this_in);
18250 else
18251 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18252 "%<__transaction_atomic%> without transactional memory support enabled"
18253 : "%<__transaction_relaxed %> "
18254 "without transactional memory support enabled"));
18256 return stmt;
18259 /* Parse a __transaction_atomic or __transaction_relaxed expression
18260 (GCC Extension).
18262 transaction-expression:
18263 __transaction_atomic ( expression )
18264 __transaction_relaxed ( expression )
18267 static struct c_expr
18268 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18270 struct c_expr ret;
18271 unsigned int old_in = parser->in_transaction;
18272 unsigned int this_in = 1;
18273 location_t loc = c_parser_peek_token (parser)->location;
18274 tree attrs;
18276 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18277 || keyword == RID_TRANSACTION_RELAXED)
18278 && c_parser_next_token_is_keyword (parser, keyword));
18279 c_parser_consume_token (parser);
18281 if (keyword == RID_TRANSACTION_RELAXED)
18282 this_in |= TM_STMT_ATTR_RELAXED;
18283 else
18285 attrs = c_parser_transaction_attributes (parser);
18286 if (attrs)
18287 this_in |= parse_tm_stmt_attr (attrs, 0);
18290 parser->in_transaction = this_in;
18291 matching_parens parens;
18292 if (parens.require_open (parser))
18294 tree expr = c_parser_expression (parser).value;
18295 ret.original_type = TREE_TYPE (expr);
18296 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18297 if (this_in & TM_STMT_ATTR_RELAXED)
18298 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18299 SET_EXPR_LOCATION (ret.value, loc);
18300 ret.original_code = TRANSACTION_EXPR;
18301 if (!parens.require_close (parser))
18303 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18304 goto error;
18307 else
18309 error:
18310 ret.set_error ();
18311 ret.original_code = ERROR_MARK;
18312 ret.original_type = NULL;
18314 parser->in_transaction = old_in;
18316 if (!flag_tm)
18317 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18318 "%<__transaction_atomic%> without transactional memory support enabled"
18319 : "%<__transaction_relaxed %> "
18320 "without transactional memory support enabled"));
18322 set_c_expr_source_range (&ret, loc, loc);
18324 return ret;
18327 /* Parse a __transaction_cancel statement (GCC Extension).
18329 transaction-cancel-statement:
18330 __transaction_cancel transaction-attribute[opt] ;
18332 Note that the only valid attribute is "outer".
18335 static tree
18336 c_parser_transaction_cancel (c_parser *parser)
18338 location_t loc = c_parser_peek_token (parser)->location;
18339 tree attrs;
18340 bool is_outer = false;
18342 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18343 c_parser_consume_token (parser);
18345 attrs = c_parser_transaction_attributes (parser);
18346 if (attrs)
18347 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18349 if (!flag_tm)
18351 error_at (loc, "%<__transaction_cancel%> without "
18352 "transactional memory support enabled");
18353 goto ret_error;
18355 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18357 error_at (loc, "%<__transaction_cancel%> within a "
18358 "%<__transaction_relaxed%>");
18359 goto ret_error;
18361 else if (is_outer)
18363 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18364 && !is_tm_may_cancel_outer (current_function_decl))
18366 error_at (loc, "outer %<__transaction_cancel%> not "
18367 "within outer %<__transaction_atomic%>");
18368 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18369 goto ret_error;
18372 else if (parser->in_transaction == 0)
18374 error_at (loc, "%<__transaction_cancel%> not within "
18375 "%<__transaction_atomic%>");
18376 goto ret_error;
18379 return add_stmt (build_tm_abort_call (loc, is_outer));
18381 ret_error:
18382 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18385 /* Parse a single source file. */
18387 void
18388 c_parse_file (void)
18390 /* Use local storage to begin. If the first token is a pragma, parse it.
18391 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18392 which will cause garbage collection. */
18393 c_parser tparser;
18395 memset (&tparser, 0, sizeof tparser);
18396 tparser.tokens = &tparser.tokens_buf[0];
18397 the_parser = &tparser;
18399 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18400 c_parser_pragma_pch_preprocess (&tparser);
18402 the_parser = ggc_alloc<c_parser> ();
18403 *the_parser = tparser;
18404 if (tparser.tokens == &tparser.tokens_buf[0])
18405 the_parser->tokens = &the_parser->tokens_buf[0];
18407 /* Initialize EH, if we've been told to do so. */
18408 if (flag_exceptions)
18409 using_eh_for_cleanups ();
18411 c_parser_translation_unit (the_parser);
18412 the_parser = NULL;
18415 /* Parse the body of a function declaration marked with "__RTL".
18417 The RTL parser works on the level of characters read from a
18418 FILE *, whereas c_parser works at the level of tokens.
18419 Square this circle by consuming all of the tokens up to and
18420 including the closing brace, recording the start/end of the RTL
18421 fragment, and reopening the file and re-reading the relevant
18422 lines within the RTL parser.
18424 This requires the opening and closing braces of the C function
18425 to be on separate lines from the RTL they wrap.
18427 Take ownership of START_WITH_PASS, if non-NULL. */
18429 void
18430 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18432 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18434 free (start_with_pass);
18435 return;
18438 location_t start_loc = c_parser_peek_token (parser)->location;
18440 /* Consume all tokens, up to the closing brace, handling
18441 matching pairs of braces in the rtl dump. */
18442 int num_open_braces = 1;
18443 while (1)
18445 switch (c_parser_peek_token (parser)->type)
18447 case CPP_OPEN_BRACE:
18448 num_open_braces++;
18449 break;
18450 case CPP_CLOSE_BRACE:
18451 if (--num_open_braces == 0)
18452 goto found_closing_brace;
18453 break;
18454 case CPP_EOF:
18455 error_at (start_loc, "no closing brace");
18456 free (start_with_pass);
18457 return;
18458 default:
18459 break;
18461 c_parser_consume_token (parser);
18464 found_closing_brace:
18465 /* At the closing brace; record its location. */
18466 location_t end_loc = c_parser_peek_token (parser)->location;
18468 /* Consume the closing brace. */
18469 c_parser_consume_token (parser);
18471 /* Invoke the RTL parser. */
18472 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18474 free (start_with_pass);
18475 return;
18478 /* If a pass name was provided for START_WITH_PASS, run the backend
18479 accordingly now, on the cfun created above, transferring
18480 ownership of START_WITH_PASS. */
18481 if (start_with_pass)
18482 run_rtl_passes (start_with_pass);
18485 #include "gt-c-c-parser.h"