compiler: give error for non-int arguments to make
[official-gcc.git] / gcc / c / c-parser.c
blob5e3512a9127ca163a21b1b3e1e6688e23dbe6f1b
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 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
1658 return;
1660 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1661 SET_EXPR_LOCATION (stmt, loc);
1662 add_stmt (stmt);
1665 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1666 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1667 is accepted; otherwise (old-style parameter declarations) only other
1668 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1669 assertion is accepted; otherwise (old-style parameter declarations)
1670 it is not. If NESTED is true, we are inside a function or parsing
1671 old-style parameter declarations; any functions encountered are
1672 nested functions and declaration specifiers are required; otherwise
1673 we are at top level and functions are normal functions and
1674 declaration specifiers may be optional. If EMPTY_OK is true, empty
1675 declarations are OK (subject to all other constraints); otherwise
1676 (old-style parameter declarations) they are diagnosed. If
1677 START_ATTR_OK is true, the declaration specifiers may start with
1678 attributes; otherwise they may not.
1679 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1680 declaration when parsing an Objective-C foreach statement.
1681 FALLTHRU_ATTR_P is used to signal whether this function parsed
1682 "__attribute__((fallthrough));".
1684 declaration:
1685 declaration-specifiers init-declarator-list[opt] ;
1686 static_assert-declaration
1688 function-definition:
1689 declaration-specifiers[opt] declarator declaration-list[opt]
1690 compound-statement
1692 declaration-list:
1693 declaration
1694 declaration-list declaration
1696 init-declarator-list:
1697 init-declarator
1698 init-declarator-list , init-declarator
1700 init-declarator:
1701 declarator simple-asm-expr[opt] attributes[opt]
1702 declarator simple-asm-expr[opt] attributes[opt] = initializer
1704 GNU extensions:
1706 nested-function-definition:
1707 declaration-specifiers declarator declaration-list[opt]
1708 compound-statement
1710 attribute ;
1712 Objective-C:
1713 attributes objc-class-definition
1714 attributes objc-category-definition
1715 attributes objc-protocol-definition
1717 The simple-asm-expr and attributes are GNU extensions.
1719 This function does not handle __extension__; that is handled in its
1720 callers. ??? Following the old parser, __extension__ may start
1721 external declarations, declarations in functions and declarations
1722 at the start of "for" loops, but not old-style parameter
1723 declarations.
1725 C99 requires declaration specifiers in a function definition; the
1726 absence is diagnosed through the diagnosis of implicit int. In GNU
1727 C we also allow but diagnose declarations without declaration
1728 specifiers, but only at top level (elsewhere they conflict with
1729 other syntax).
1731 In Objective-C, declarations of the looping variable in a foreach
1732 statement are exceptionally terminated by 'in' (for example, 'for
1733 (NSObject *object in array) { ... }').
1735 OpenMP:
1737 declaration:
1738 threadprivate-directive
1740 GIMPLE:
1742 gimple-function-definition:
1743 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1744 declaration-list[opt] compound-statement
1746 rtl-function-definition:
1747 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1748 declaration-list[opt] compound-statement */
1750 static void
1751 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1752 bool static_assert_ok, bool empty_ok,
1753 bool nested, bool start_attr_ok,
1754 tree *objc_foreach_object_declaration,
1755 vec<c_token> omp_declare_simd_clauses,
1756 struct oacc_routine_data *oacc_routine_data,
1757 bool *fallthru_attr_p)
1759 struct c_declspecs *specs;
1760 tree prefix_attrs;
1761 tree all_prefix_attrs;
1762 bool diagnosed_no_specs = false;
1763 location_t here = c_parser_peek_token (parser)->location;
1765 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1767 if (static_assert_ok
1768 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1770 c_parser_static_assert_declaration (parser);
1771 return;
1773 specs = build_null_declspecs ();
1775 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1776 if (c_parser_peek_token (parser)->type == CPP_NAME
1777 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1778 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1779 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1780 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1782 tree name = c_parser_peek_token (parser)->value;
1784 /* Issue a warning about NAME being an unknown type name, perhaps
1785 with some kind of hint.
1786 If the user forgot a "struct" etc, suggest inserting
1787 it. Otherwise, attempt to look for misspellings. */
1788 gcc_rich_location richloc (here);
1789 if (tag_exists_p (RECORD_TYPE, name))
1791 /* This is not C++ with its implicit typedef. */
1792 richloc.add_fixit_insert_before ("struct ");
1793 error_at (&richloc,
1794 "unknown type name %qE;"
1795 " use %<struct%> keyword to refer to the type",
1796 name);
1798 else if (tag_exists_p (UNION_TYPE, name))
1800 richloc.add_fixit_insert_before ("union ");
1801 error_at (&richloc,
1802 "unknown type name %qE;"
1803 " use %<union%> keyword to refer to the type",
1804 name);
1806 else if (tag_exists_p (ENUMERAL_TYPE, name))
1808 richloc.add_fixit_insert_before ("enum ");
1809 error_at (&richloc,
1810 "unknown type name %qE;"
1811 " use %<enum%> keyword to refer to the type",
1812 name);
1814 else
1816 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1817 here);
1818 if (hint)
1820 richloc.add_fixit_replace (hint.suggestion ());
1821 error_at (&richloc,
1822 "unknown type name %qE; did you mean %qs?",
1823 name, hint.suggestion ());
1825 else
1826 error_at (here, "unknown type name %qE", name);
1829 /* Parse declspecs normally to get a correct pointer type, but avoid
1830 a further "fails to be a type name" error. Refuse nested functions
1831 since it is not how the user likely wants us to recover. */
1832 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1833 c_parser_peek_token (parser)->keyword = RID_VOID;
1834 c_parser_peek_token (parser)->value = error_mark_node;
1835 fndef_ok = !nested;
1838 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1839 true, true, cla_nonabstract_decl);
1840 if (parser->error)
1842 c_parser_skip_to_end_of_block_or_statement (parser);
1843 return;
1845 if (nested && !specs->declspecs_seen_p)
1847 c_parser_error (parser, "expected declaration specifiers");
1848 c_parser_skip_to_end_of_block_or_statement (parser);
1849 return;
1852 finish_declspecs (specs);
1853 bool auto_type_p = specs->typespec_word == cts_auto_type;
1854 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1856 if (auto_type_p)
1857 error_at (here, "%<__auto_type%> in empty declaration");
1858 else if (specs->typespec_kind == ctsk_none
1859 && attribute_fallthrough_p (specs->attrs))
1861 if (fallthru_attr_p != NULL)
1862 *fallthru_attr_p = true;
1863 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1864 void_type_node, 0);
1865 add_stmt (fn);
1867 else if (empty_ok)
1868 shadow_tag (specs);
1869 else
1871 shadow_tag_warned (specs, 1);
1872 pedwarn (here, 0, "empty declaration");
1874 c_parser_consume_token (parser);
1875 if (oacc_routine_data)
1876 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1877 return;
1880 /* Provide better error recovery. Note that a type name here is usually
1881 better diagnosed as a redeclaration. */
1882 if (empty_ok
1883 && specs->typespec_kind == ctsk_tagdef
1884 && c_parser_next_token_starts_declspecs (parser)
1885 && !c_parser_next_token_is (parser, CPP_NAME))
1887 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1888 parser->error = false;
1889 shadow_tag_warned (specs, 1);
1890 return;
1892 else if (c_dialect_objc () && !auto_type_p)
1894 /* Prefix attributes are an error on method decls. */
1895 switch (c_parser_peek_token (parser)->type)
1897 case CPP_PLUS:
1898 case CPP_MINUS:
1899 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1900 return;
1901 if (specs->attrs)
1903 warning_at (c_parser_peek_token (parser)->location,
1904 OPT_Wattributes,
1905 "prefix attributes are ignored for methods");
1906 specs->attrs = NULL_TREE;
1908 if (fndef_ok)
1909 c_parser_objc_method_definition (parser);
1910 else
1911 c_parser_objc_methodproto (parser);
1912 return;
1913 break;
1914 default:
1915 break;
1917 /* This is where we parse 'attributes @interface ...',
1918 'attributes @implementation ...', 'attributes @protocol ...'
1919 (where attributes could be, for example, __attribute__
1920 ((deprecated)).
1922 switch (c_parser_peek_token (parser)->keyword)
1924 case RID_AT_INTERFACE:
1926 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1927 return;
1928 c_parser_objc_class_definition (parser, specs->attrs);
1929 return;
1931 break;
1932 case RID_AT_IMPLEMENTATION:
1934 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1935 return;
1936 if (specs->attrs)
1938 warning_at (c_parser_peek_token (parser)->location,
1939 OPT_Wattributes,
1940 "prefix attributes are ignored for implementations");
1941 specs->attrs = NULL_TREE;
1943 c_parser_objc_class_definition (parser, NULL_TREE);
1944 return;
1946 break;
1947 case RID_AT_PROTOCOL:
1949 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1950 return;
1951 c_parser_objc_protocol_definition (parser, specs->attrs);
1952 return;
1954 break;
1955 case RID_AT_ALIAS:
1956 case RID_AT_CLASS:
1957 case RID_AT_END:
1958 case RID_AT_PROPERTY:
1959 if (specs->attrs)
1961 c_parser_error (parser, "unexpected attribute");
1962 specs->attrs = NULL;
1964 break;
1965 default:
1966 break;
1969 else if (attribute_fallthrough_p (specs->attrs))
1970 warning_at (here, OPT_Wattributes,
1971 "%<fallthrough%> attribute not followed by %<;%>");
1973 pending_xref_error ();
1974 prefix_attrs = specs->attrs;
1975 all_prefix_attrs = prefix_attrs;
1976 specs->attrs = NULL_TREE;
1977 while (true)
1979 struct c_declarator *declarator;
1980 bool dummy = false;
1981 timevar_id_t tv;
1982 tree fnbody = NULL_TREE;
1983 /* Declaring either one or more declarators (in which case we
1984 should diagnose if there were no declaration specifiers) or a
1985 function definition (in which case the diagnostic for
1986 implicit int suffices). */
1987 declarator = c_parser_declarator (parser,
1988 specs->typespec_kind != ctsk_none,
1989 C_DTR_NORMAL, &dummy);
1990 if (declarator == NULL)
1992 if (omp_declare_simd_clauses.exists ())
1993 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1994 omp_declare_simd_clauses);
1995 if (oacc_routine_data)
1996 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1997 c_parser_skip_to_end_of_block_or_statement (parser);
1998 return;
2000 if (auto_type_p && declarator->kind != cdk_id)
2002 error_at (here,
2003 "%<__auto_type%> requires a plain identifier"
2004 " as declarator");
2005 c_parser_skip_to_end_of_block_or_statement (parser);
2006 return;
2008 if (c_parser_next_token_is (parser, CPP_EQ)
2009 || c_parser_next_token_is (parser, CPP_COMMA)
2010 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2011 || c_parser_next_token_is_keyword (parser, RID_ASM)
2012 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2013 || c_parser_next_token_is_keyword (parser, RID_IN))
2015 tree asm_name = NULL_TREE;
2016 tree postfix_attrs = NULL_TREE;
2017 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2019 diagnosed_no_specs = true;
2020 pedwarn (here, 0, "data definition has no type or storage class");
2022 /* Having seen a data definition, there cannot now be a
2023 function definition. */
2024 fndef_ok = false;
2025 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2026 asm_name = c_parser_simple_asm_expr (parser);
2027 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2029 postfix_attrs = c_parser_attributes (parser);
2030 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2032 /* This means there is an attribute specifier after
2033 the declarator in a function definition. Provide
2034 some more information for the user. */
2035 error_at (here, "attributes should be specified before the "
2036 "declarator in a function definition");
2037 c_parser_skip_to_end_of_block_or_statement (parser);
2038 return;
2041 if (c_parser_next_token_is (parser, CPP_EQ))
2043 tree d;
2044 struct c_expr init;
2045 location_t init_loc;
2046 c_parser_consume_token (parser);
2047 if (auto_type_p)
2049 init_loc = c_parser_peek_token (parser)->location;
2050 rich_location richloc (line_table, init_loc);
2051 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2052 /* A parameter is initialized, which is invalid. Don't
2053 attempt to instrument the initializer. */
2054 int flag_sanitize_save = flag_sanitize;
2055 if (nested && !empty_ok)
2056 flag_sanitize = 0;
2057 init = c_parser_expr_no_commas (parser, NULL);
2058 flag_sanitize = flag_sanitize_save;
2059 if (TREE_CODE (init.value) == COMPONENT_REF
2060 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2061 error_at (here,
2062 "%<__auto_type%> used with a bit-field"
2063 " initializer");
2064 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2065 tree init_type = TREE_TYPE (init.value);
2066 /* As with typeof, remove all qualifiers from atomic types. */
2067 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2068 init_type
2069 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2070 bool vm_type = variably_modified_type_p (init_type,
2071 NULL_TREE);
2072 if (vm_type)
2073 init.value = save_expr (init.value);
2074 finish_init ();
2075 specs->typespec_kind = ctsk_typeof;
2076 specs->locations[cdw_typedef] = init_loc;
2077 specs->typedef_p = true;
2078 specs->type = init_type;
2079 if (vm_type)
2081 bool maybe_const = true;
2082 tree type_expr = c_fully_fold (init.value, false,
2083 &maybe_const);
2084 specs->expr_const_operands &= maybe_const;
2085 if (specs->expr)
2086 specs->expr = build2 (COMPOUND_EXPR,
2087 TREE_TYPE (type_expr),
2088 specs->expr, type_expr);
2089 else
2090 specs->expr = type_expr;
2092 d = start_decl (declarator, specs, true,
2093 chainon (postfix_attrs, all_prefix_attrs));
2094 if (!d)
2095 d = error_mark_node;
2096 if (omp_declare_simd_clauses.exists ())
2097 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2098 omp_declare_simd_clauses);
2100 else
2102 /* The declaration of the variable is in effect while
2103 its initializer is parsed. */
2104 d = start_decl (declarator, specs, true,
2105 chainon (postfix_attrs, all_prefix_attrs));
2106 if (!d)
2107 d = error_mark_node;
2108 if (omp_declare_simd_clauses.exists ())
2109 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2110 omp_declare_simd_clauses);
2111 init_loc = c_parser_peek_token (parser)->location;
2112 rich_location richloc (line_table, init_loc);
2113 start_init (d, asm_name, global_bindings_p (), &richloc);
2114 /* A parameter is initialized, which is invalid. Don't
2115 attempt to instrument the initializer. */
2116 int flag_sanitize_save = flag_sanitize;
2117 if (TREE_CODE (d) == PARM_DECL)
2118 flag_sanitize = 0;
2119 init = c_parser_initializer (parser);
2120 flag_sanitize = flag_sanitize_save;
2121 finish_init ();
2123 if (oacc_routine_data)
2124 c_finish_oacc_routine (oacc_routine_data, d, false);
2125 if (d != error_mark_node)
2127 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2128 finish_decl (d, init_loc, init.value,
2129 init.original_type, asm_name);
2132 else
2134 if (auto_type_p)
2136 error_at (here,
2137 "%<__auto_type%> requires an initialized "
2138 "data declaration");
2139 c_parser_skip_to_end_of_block_or_statement (parser);
2140 return;
2142 tree d = start_decl (declarator, specs, false,
2143 chainon (postfix_attrs,
2144 all_prefix_attrs));
2145 if (d && TREE_CODE (d) == FUNCTION_DECL)
2146 if (declarator->kind == cdk_function)
2147 if (DECL_ARGUMENTS (d) == NULL_TREE)
2148 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2149 if (omp_declare_simd_clauses.exists ())
2151 tree parms = NULL_TREE;
2152 if (d && TREE_CODE (d) == FUNCTION_DECL)
2154 struct c_declarator *ce = declarator;
2155 while (ce != NULL)
2156 if (ce->kind == cdk_function)
2158 parms = ce->u.arg_info->parms;
2159 break;
2161 else
2162 ce = ce->declarator;
2164 if (parms)
2165 temp_store_parm_decls (d, parms);
2166 c_finish_omp_declare_simd (parser, d, parms,
2167 omp_declare_simd_clauses);
2168 if (parms)
2169 temp_pop_parm_decls ();
2171 if (oacc_routine_data)
2172 c_finish_oacc_routine (oacc_routine_data, d, false);
2173 if (d)
2174 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2175 NULL_TREE, asm_name);
2177 if (c_parser_next_token_is_keyword (parser, RID_IN))
2179 if (d)
2180 *objc_foreach_object_declaration = d;
2181 else
2182 *objc_foreach_object_declaration = error_mark_node;
2185 if (c_parser_next_token_is (parser, CPP_COMMA))
2187 if (auto_type_p)
2189 error_at (here,
2190 "%<__auto_type%> may only be used with"
2191 " a single declarator");
2192 c_parser_skip_to_end_of_block_or_statement (parser);
2193 return;
2195 c_parser_consume_token (parser);
2196 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2197 all_prefix_attrs = chainon (c_parser_attributes (parser),
2198 prefix_attrs);
2199 else
2200 all_prefix_attrs = prefix_attrs;
2201 continue;
2203 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2205 c_parser_consume_token (parser);
2206 return;
2208 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2210 /* This can only happen in Objective-C: we found the
2211 'in' that terminates the declaration inside an
2212 Objective-C foreach statement. Do not consume the
2213 token, so that the caller can use it to determine
2214 that this indeed is a foreach context. */
2215 return;
2217 else
2219 c_parser_error (parser, "expected %<,%> or %<;%>");
2220 c_parser_skip_to_end_of_block_or_statement (parser);
2221 return;
2224 else if (auto_type_p)
2226 error_at (here,
2227 "%<__auto_type%> requires an initialized data declaration");
2228 c_parser_skip_to_end_of_block_or_statement (parser);
2229 return;
2231 else if (!fndef_ok)
2233 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2234 "%<asm%> or %<__attribute__%>");
2235 c_parser_skip_to_end_of_block_or_statement (parser);
2236 return;
2238 /* Function definition (nested or otherwise). */
2239 if (nested)
2241 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2242 c_push_function_context ();
2244 if (!start_function (specs, declarator, all_prefix_attrs))
2246 /* At this point we've consumed:
2247 declaration-specifiers declarator
2248 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2249 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2250 but the
2251 declaration-specifiers declarator
2252 aren't grokkable as a function definition, so we have
2253 an error. */
2254 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2255 if (c_parser_next_token_starts_declspecs (parser))
2257 /* If we have
2258 declaration-specifiers declarator decl-specs
2259 then assume we have a missing semicolon, which would
2260 give us:
2261 declaration-specifiers declarator decl-specs
2264 <~~~~~~~~~ declaration ~~~~~~~~~~>
2265 Use c_parser_require to get an error with a fix-it hint. */
2266 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2267 parser->error = false;
2269 else
2271 /* This can appear in many cases looking nothing like a
2272 function definition, so we don't give a more specific
2273 error suggesting there was one. */
2274 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2275 "or %<__attribute__%>");
2277 if (nested)
2278 c_pop_function_context ();
2279 break;
2282 if (DECL_DECLARED_INLINE_P (current_function_decl))
2283 tv = TV_PARSE_INLINE;
2284 else
2285 tv = TV_PARSE_FUNC;
2286 auto_timevar at (g_timer, tv);
2288 /* Parse old-style parameter declarations. ??? Attributes are
2289 not allowed to start declaration specifiers here because of a
2290 syntax conflict between a function declaration with attribute
2291 suffix and a function definition with an attribute prefix on
2292 first old-style parameter declaration. Following the old
2293 parser, they are not accepted on subsequent old-style
2294 parameter declarations either. However, there is no
2295 ambiguity after the first declaration, nor indeed on the
2296 first as long as we don't allow postfix attributes after a
2297 declarator with a nonempty identifier list in a definition;
2298 and postfix attributes have never been accepted here in
2299 function definitions either. */
2300 while (c_parser_next_token_is_not (parser, CPP_EOF)
2301 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2302 c_parser_declaration_or_fndef (parser, false, false, false,
2303 true, false, NULL, vNULL);
2304 store_parm_decls ();
2305 if (omp_declare_simd_clauses.exists ())
2306 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2307 omp_declare_simd_clauses);
2308 if (oacc_routine_data)
2309 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2310 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2311 = c_parser_peek_token (parser)->location;
2313 /* If the definition was marked with __GIMPLE then parse the
2314 function body as GIMPLE. */
2315 if (specs->gimple_p)
2317 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2318 bool saved = in_late_binary_op;
2319 in_late_binary_op = true;
2320 c_parser_parse_gimple_body (parser);
2321 in_late_binary_op = saved;
2323 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2324 consuming the function body. */
2325 else if (specs->rtl_p)
2327 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2329 /* Normally, store_parm_decls sets next_is_function_body,
2330 anticipating a function body. We need a push_scope/pop_scope
2331 pair to flush out this state, or subsequent function parsing
2332 will go wrong. */
2333 push_scope ();
2334 pop_scope ();
2336 finish_function ();
2337 return;
2339 else
2340 fnbody = c_parser_compound_statement (parser);
2341 tree fndecl = current_function_decl;
2342 if (nested)
2344 tree decl = current_function_decl;
2345 /* Mark nested functions as needing static-chain initially.
2346 lower_nested_functions will recompute it but the
2347 DECL_STATIC_CHAIN flag is also used before that happens,
2348 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2349 DECL_STATIC_CHAIN (decl) = 1;
2350 add_stmt (fnbody);
2351 finish_function ();
2352 c_pop_function_context ();
2353 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2355 else
2357 if (fnbody)
2358 add_stmt (fnbody);
2359 finish_function ();
2361 /* Get rid of the empty stmt list for GIMPLE. */
2362 if (specs->gimple_p)
2363 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2365 break;
2369 /* Parse an asm-definition (asm() outside a function body). This is a
2370 GNU extension.
2372 asm-definition:
2373 simple-asm-expr ;
2376 static void
2377 c_parser_asm_definition (c_parser *parser)
2379 tree asm_str = c_parser_simple_asm_expr (parser);
2380 if (asm_str)
2381 symtab->finalize_toplevel_asm (asm_str);
2382 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2385 /* Parse a static assertion (C11 6.7.10).
2387 static_assert-declaration:
2388 static_assert-declaration-no-semi ;
2391 static void
2392 c_parser_static_assert_declaration (c_parser *parser)
2394 c_parser_static_assert_declaration_no_semi (parser);
2395 if (parser->error
2396 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2397 c_parser_skip_to_end_of_block_or_statement (parser);
2400 /* Parse a static assertion (C11 6.7.10), without the trailing
2401 semicolon.
2403 static_assert-declaration-no-semi:
2404 _Static_assert ( constant-expression , string-literal )
2407 static void
2408 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2410 location_t assert_loc, value_loc;
2411 tree value;
2412 tree string;
2414 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2415 assert_loc = c_parser_peek_token (parser)->location;
2416 if (flag_isoc99)
2417 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2418 "ISO C99 does not support %<_Static_assert%>");
2419 else
2420 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2421 "ISO C90 does not support %<_Static_assert%>");
2422 c_parser_consume_token (parser);
2423 matching_parens parens;
2424 if (!parens.require_open (parser))
2425 return;
2426 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2427 value = c_parser_expr_no_commas (parser, NULL).value;
2428 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2429 parser->lex_untranslated_string = true;
2430 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2432 parser->lex_untranslated_string = false;
2433 return;
2435 switch (c_parser_peek_token (parser)->type)
2437 case CPP_STRING:
2438 case CPP_STRING16:
2439 case CPP_STRING32:
2440 case CPP_WSTRING:
2441 case CPP_UTF8STRING:
2442 string = c_parser_peek_token (parser)->value;
2443 c_parser_consume_token (parser);
2444 parser->lex_untranslated_string = false;
2445 break;
2446 default:
2447 c_parser_error (parser, "expected string literal");
2448 parser->lex_untranslated_string = false;
2449 return;
2451 parens.require_close (parser);
2453 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2455 error_at (value_loc, "expression in static assertion is not an integer");
2456 return;
2458 if (TREE_CODE (value) != INTEGER_CST)
2460 value = c_fully_fold (value, false, NULL);
2461 /* Strip no-op conversions. */
2462 STRIP_TYPE_NOPS (value);
2463 if (TREE_CODE (value) == INTEGER_CST)
2464 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2465 "is not an integer constant expression");
2467 if (TREE_CODE (value) != INTEGER_CST)
2469 error_at (value_loc, "expression in static assertion is not constant");
2470 return;
2472 constant_expression_warning (value);
2473 if (integer_zerop (value))
2474 error_at (assert_loc, "static assertion failed: %E", string);
2477 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2478 6.7, C11 6.7), adding them to SPECS (which may already include some).
2479 Storage class specifiers are accepted iff SCSPEC_OK; type
2480 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2481 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2482 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2484 declaration-specifiers:
2485 storage-class-specifier declaration-specifiers[opt]
2486 type-specifier declaration-specifiers[opt]
2487 type-qualifier declaration-specifiers[opt]
2488 function-specifier declaration-specifiers[opt]
2489 alignment-specifier declaration-specifiers[opt]
2491 Function specifiers (inline) are from C99, and are currently
2492 handled as storage class specifiers, as is __thread. Alignment
2493 specifiers are from C11.
2495 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2496 storage-class-specifier:
2497 typedef
2498 extern
2499 static
2500 auto
2501 register
2502 _Thread_local
2504 (_Thread_local is new in C11.)
2506 C99 6.7.4, C11 6.7.4:
2507 function-specifier:
2508 inline
2509 _Noreturn
2511 (_Noreturn is new in C11.)
2513 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2514 type-specifier:
2515 void
2516 char
2517 short
2519 long
2520 float
2521 double
2522 signed
2523 unsigned
2524 _Bool
2525 _Complex
2526 [_Imaginary removed in C99 TC2]
2527 struct-or-union-specifier
2528 enum-specifier
2529 typedef-name
2530 atomic-type-specifier
2532 (_Bool and _Complex are new in C99.)
2533 (atomic-type-specifier is new in C11.)
2535 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2537 type-qualifier:
2538 const
2539 restrict
2540 volatile
2541 address-space-qualifier
2542 _Atomic
2544 (restrict is new in C99.)
2545 (_Atomic is new in C11.)
2547 GNU extensions:
2549 declaration-specifiers:
2550 attributes declaration-specifiers[opt]
2552 type-qualifier:
2553 address-space
2555 address-space:
2556 identifier recognized by the target
2558 storage-class-specifier:
2559 __thread
2561 type-specifier:
2562 typeof-specifier
2563 __auto_type
2564 __intN
2565 _Decimal32
2566 _Decimal64
2567 _Decimal128
2568 _Fract
2569 _Accum
2570 _Sat
2572 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2573 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2575 atomic-type-specifier
2576 _Atomic ( type-name )
2578 Objective-C:
2580 type-specifier:
2581 class-name objc-protocol-refs[opt]
2582 typedef-name objc-protocol-refs
2583 objc-protocol-refs
2586 void
2587 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2588 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2589 bool alignspec_ok, bool auto_type_ok,
2590 enum c_lookahead_kind la)
2592 bool attrs_ok = start_attr_ok;
2593 bool seen_type = specs->typespec_kind != ctsk_none;
2595 if (!typespec_ok)
2596 gcc_assert (la == cla_prefer_id);
2598 while (c_parser_next_token_is (parser, CPP_NAME)
2599 || c_parser_next_token_is (parser, CPP_KEYWORD)
2600 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2602 struct c_typespec t;
2603 tree attrs;
2604 tree align;
2605 location_t loc = c_parser_peek_token (parser)->location;
2607 /* If we cannot accept a type, exit if the next token must start
2608 one. Also, if we already have seen a tagged definition,
2609 a typename would be an error anyway and likely the user
2610 has simply forgotten a semicolon, so we exit. */
2611 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2612 && c_parser_next_tokens_start_typename (parser, la)
2613 && !c_parser_next_token_is_qualifier (parser)
2614 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2615 break;
2617 if (c_parser_next_token_is (parser, CPP_NAME))
2619 c_token *name_token = c_parser_peek_token (parser);
2620 tree value = name_token->value;
2621 c_id_kind kind = name_token->id_kind;
2623 if (kind == C_ID_ADDRSPACE)
2625 addr_space_t as
2626 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2627 declspecs_add_addrspace (name_token->location, specs, as);
2628 c_parser_consume_token (parser);
2629 attrs_ok = true;
2630 continue;
2633 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2635 /* If we cannot accept a type, and the next token must start one,
2636 exit. Do the same if we already have seen a tagged definition,
2637 since it would be an error anyway and likely the user has simply
2638 forgotten a semicolon. */
2639 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2640 break;
2642 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2643 a C_ID_CLASSNAME. */
2644 c_parser_consume_token (parser);
2645 seen_type = true;
2646 attrs_ok = true;
2647 if (kind == C_ID_ID)
2649 error_at (loc, "unknown type name %qE", value);
2650 t.kind = ctsk_typedef;
2651 t.spec = error_mark_node;
2653 else if (kind == C_ID_TYPENAME
2654 && (!c_dialect_objc ()
2655 || c_parser_next_token_is_not (parser, CPP_LESS)))
2657 t.kind = ctsk_typedef;
2658 /* For a typedef name, record the meaning, not the name.
2659 In case of 'foo foo, bar;'. */
2660 t.spec = lookup_name (value);
2662 else
2664 tree proto = NULL_TREE;
2665 gcc_assert (c_dialect_objc ());
2666 t.kind = ctsk_objc;
2667 if (c_parser_next_token_is (parser, CPP_LESS))
2668 proto = c_parser_objc_protocol_refs (parser);
2669 t.spec = objc_get_protocol_qualified_type (value, proto);
2671 t.expr = NULL_TREE;
2672 t.expr_const_operands = true;
2673 declspecs_add_type (name_token->location, specs, t);
2674 continue;
2676 if (c_parser_next_token_is (parser, CPP_LESS))
2678 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2679 nisse@lysator.liu.se. */
2680 tree proto;
2681 gcc_assert (c_dialect_objc ());
2682 if (!typespec_ok || seen_type)
2683 break;
2684 proto = c_parser_objc_protocol_refs (parser);
2685 t.kind = ctsk_objc;
2686 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2687 t.expr = NULL_TREE;
2688 t.expr_const_operands = true;
2689 declspecs_add_type (loc, specs, t);
2690 continue;
2692 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2693 switch (c_parser_peek_token (parser)->keyword)
2695 case RID_STATIC:
2696 case RID_EXTERN:
2697 case RID_REGISTER:
2698 case RID_TYPEDEF:
2699 case RID_INLINE:
2700 case RID_NORETURN:
2701 case RID_AUTO:
2702 case RID_THREAD:
2703 if (!scspec_ok)
2704 goto out;
2705 attrs_ok = true;
2706 /* TODO: Distinguish between function specifiers (inline, noreturn)
2707 and storage class specifiers, either here or in
2708 declspecs_add_scspec. */
2709 declspecs_add_scspec (loc, specs,
2710 c_parser_peek_token (parser)->value);
2711 c_parser_consume_token (parser);
2712 break;
2713 case RID_AUTO_TYPE:
2714 if (!auto_type_ok)
2715 goto out;
2716 /* Fall through. */
2717 case RID_UNSIGNED:
2718 case RID_LONG:
2719 case RID_SHORT:
2720 case RID_SIGNED:
2721 case RID_COMPLEX:
2722 case RID_INT:
2723 case RID_CHAR:
2724 case RID_FLOAT:
2725 case RID_DOUBLE:
2726 case RID_VOID:
2727 case RID_DFLOAT32:
2728 case RID_DFLOAT64:
2729 case RID_DFLOAT128:
2730 CASE_RID_FLOATN_NX:
2731 case RID_BOOL:
2732 case RID_FRACT:
2733 case RID_ACCUM:
2734 case RID_SAT:
2735 case RID_INT_N_0:
2736 case RID_INT_N_1:
2737 case RID_INT_N_2:
2738 case RID_INT_N_3:
2739 if (!typespec_ok)
2740 goto out;
2741 attrs_ok = true;
2742 seen_type = true;
2743 if (c_dialect_objc ())
2744 parser->objc_need_raw_identifier = true;
2745 t.kind = ctsk_resword;
2746 t.spec = c_parser_peek_token (parser)->value;
2747 t.expr = NULL_TREE;
2748 t.expr_const_operands = true;
2749 declspecs_add_type (loc, specs, t);
2750 c_parser_consume_token (parser);
2751 break;
2752 case RID_ENUM:
2753 if (!typespec_ok)
2754 goto out;
2755 attrs_ok = true;
2756 seen_type = true;
2757 t = c_parser_enum_specifier (parser);
2758 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2759 declspecs_add_type (loc, specs, t);
2760 break;
2761 case RID_STRUCT:
2762 case RID_UNION:
2763 if (!typespec_ok)
2764 goto out;
2765 attrs_ok = true;
2766 seen_type = true;
2767 t = c_parser_struct_or_union_specifier (parser);
2768 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2769 declspecs_add_type (loc, specs, t);
2770 break;
2771 case RID_TYPEOF:
2772 /* ??? The old parser rejected typeof after other type
2773 specifiers, but is a syntax error the best way of
2774 handling this? */
2775 if (!typespec_ok || seen_type)
2776 goto out;
2777 attrs_ok = true;
2778 seen_type = true;
2779 t = c_parser_typeof_specifier (parser);
2780 declspecs_add_type (loc, specs, t);
2781 break;
2782 case RID_ATOMIC:
2783 /* C parser handling of Objective-C constructs needs
2784 checking for correct lvalue-to-rvalue conversions, and
2785 the code in build_modify_expr handling various
2786 Objective-C cases, and that in build_unary_op handling
2787 Objective-C cases for increment / decrement, also needs
2788 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2789 and objc_types_are_equivalent may also need updates. */
2790 if (c_dialect_objc ())
2791 sorry ("%<_Atomic%> in Objective-C");
2792 if (flag_isoc99)
2793 pedwarn_c99 (loc, OPT_Wpedantic,
2794 "ISO C99 does not support the %<_Atomic%> qualifier");
2795 else
2796 pedwarn_c99 (loc, OPT_Wpedantic,
2797 "ISO C90 does not support the %<_Atomic%> qualifier");
2798 attrs_ok = true;
2799 tree value;
2800 value = c_parser_peek_token (parser)->value;
2801 c_parser_consume_token (parser);
2802 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2804 /* _Atomic ( type-name ). */
2805 seen_type = true;
2806 c_parser_consume_token (parser);
2807 struct c_type_name *type = c_parser_type_name (parser);
2808 t.kind = ctsk_typeof;
2809 t.spec = error_mark_node;
2810 t.expr = NULL_TREE;
2811 t.expr_const_operands = true;
2812 if (type != NULL)
2813 t.spec = groktypename (type, &t.expr,
2814 &t.expr_const_operands);
2815 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2816 "expected %<)%>");
2817 if (t.spec != error_mark_node)
2819 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2820 error_at (loc, "%<_Atomic%>-qualified array type");
2821 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2822 error_at (loc, "%<_Atomic%>-qualified function type");
2823 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2824 error_at (loc, "%<_Atomic%> applied to a qualified type");
2825 else
2826 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2828 declspecs_add_type (loc, specs, t);
2830 else
2831 declspecs_add_qual (loc, specs, value);
2832 break;
2833 case RID_CONST:
2834 case RID_VOLATILE:
2835 case RID_RESTRICT:
2836 attrs_ok = true;
2837 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2838 c_parser_consume_token (parser);
2839 break;
2840 case RID_ATTRIBUTE:
2841 if (!attrs_ok)
2842 goto out;
2843 attrs = c_parser_attributes (parser);
2844 declspecs_add_attrs (loc, specs, attrs);
2845 break;
2846 case RID_ALIGNAS:
2847 if (!alignspec_ok)
2848 goto out;
2849 align = c_parser_alignas_specifier (parser);
2850 declspecs_add_alignas (loc, specs, align);
2851 break;
2852 case RID_GIMPLE:
2853 if (! flag_gimple)
2854 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2855 c_parser_consume_token (parser);
2856 specs->gimple_p = true;
2857 specs->locations[cdw_gimple] = loc;
2858 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2859 break;
2860 case RID_RTL:
2861 c_parser_consume_token (parser);
2862 specs->rtl_p = true;
2863 specs->locations[cdw_rtl] = loc;
2864 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2865 break;
2866 default:
2867 goto out;
2870 out: ;
2873 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2875 enum-specifier:
2876 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2877 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2878 enum attributes[opt] identifier
2880 The form with trailing comma is new in C99. The forms with
2881 attributes are GNU extensions. In GNU C, we accept any expression
2882 without commas in the syntax (assignment expressions, not just
2883 conditional expressions); assignment expressions will be diagnosed
2884 as non-constant.
2886 enumerator-list:
2887 enumerator
2888 enumerator-list , enumerator
2890 enumerator:
2891 enumeration-constant
2892 enumeration-constant = constant-expression
2894 GNU Extensions:
2896 enumerator:
2897 enumeration-constant attributes[opt]
2898 enumeration-constant attributes[opt] = constant-expression
2902 static struct c_typespec
2903 c_parser_enum_specifier (c_parser *parser)
2905 struct c_typespec ret;
2906 tree attrs;
2907 tree ident = NULL_TREE;
2908 location_t enum_loc;
2909 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2910 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2911 c_parser_consume_token (parser);
2912 attrs = c_parser_attributes (parser);
2913 enum_loc = c_parser_peek_token (parser)->location;
2914 /* Set the location in case we create a decl now. */
2915 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2916 if (c_parser_next_token_is (parser, CPP_NAME))
2918 ident = c_parser_peek_token (parser)->value;
2919 ident_loc = c_parser_peek_token (parser)->location;
2920 enum_loc = ident_loc;
2921 c_parser_consume_token (parser);
2923 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2925 /* Parse an enum definition. */
2926 struct c_enum_contents the_enum;
2927 tree type;
2928 tree postfix_attrs;
2929 /* We chain the enumerators in reverse order, then put them in
2930 forward order at the end. */
2931 tree values;
2932 timevar_push (TV_PARSE_ENUM);
2933 type = start_enum (enum_loc, &the_enum, ident);
2934 values = NULL_TREE;
2935 c_parser_consume_token (parser);
2936 while (true)
2938 tree enum_id;
2939 tree enum_value;
2940 tree enum_decl;
2941 bool seen_comma;
2942 c_token *token;
2943 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2944 location_t decl_loc, value_loc;
2945 if (c_parser_next_token_is_not (parser, CPP_NAME))
2947 /* Give a nicer error for "enum {}". */
2948 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2949 && !parser->error)
2951 error_at (c_parser_peek_token (parser)->location,
2952 "empty enum is invalid");
2953 parser->error = true;
2955 else
2956 c_parser_error (parser, "expected identifier");
2957 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2958 values = error_mark_node;
2959 break;
2961 token = c_parser_peek_token (parser);
2962 enum_id = token->value;
2963 /* Set the location in case we create a decl now. */
2964 c_parser_set_source_position_from_token (token);
2965 decl_loc = value_loc = token->location;
2966 c_parser_consume_token (parser);
2967 /* Parse any specified attributes. */
2968 tree enum_attrs = c_parser_attributes (parser);
2969 if (c_parser_next_token_is (parser, CPP_EQ))
2971 c_parser_consume_token (parser);
2972 value_loc = c_parser_peek_token (parser)->location;
2973 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2975 else
2976 enum_value = NULL_TREE;
2977 enum_decl = build_enumerator (decl_loc, value_loc,
2978 &the_enum, enum_id, enum_value);
2979 if (enum_attrs)
2980 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2981 TREE_CHAIN (enum_decl) = values;
2982 values = enum_decl;
2983 seen_comma = false;
2984 if (c_parser_next_token_is (parser, CPP_COMMA))
2986 comma_loc = c_parser_peek_token (parser)->location;
2987 seen_comma = true;
2988 c_parser_consume_token (parser);
2990 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2992 if (seen_comma)
2993 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2994 "comma at end of enumerator list");
2995 c_parser_consume_token (parser);
2996 break;
2998 if (!seen_comma)
3000 c_parser_error (parser, "expected %<,%> or %<}%>");
3001 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3002 values = error_mark_node;
3003 break;
3006 postfix_attrs = c_parser_attributes (parser);
3007 ret.spec = finish_enum (type, nreverse (values),
3008 chainon (attrs, postfix_attrs));
3009 ret.kind = ctsk_tagdef;
3010 ret.expr = NULL_TREE;
3011 ret.expr_const_operands = true;
3012 timevar_pop (TV_PARSE_ENUM);
3013 return ret;
3015 else if (!ident)
3017 c_parser_error (parser, "expected %<{%>");
3018 ret.spec = error_mark_node;
3019 ret.kind = ctsk_tagref;
3020 ret.expr = NULL_TREE;
3021 ret.expr_const_operands = true;
3022 return ret;
3024 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3025 /* In ISO C, enumerated types can be referred to only if already
3026 defined. */
3027 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3029 gcc_assert (ident);
3030 pedwarn (enum_loc, OPT_Wpedantic,
3031 "ISO C forbids forward references to %<enum%> types");
3033 return ret;
3036 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3038 struct-or-union-specifier:
3039 struct-or-union attributes[opt] identifier[opt]
3040 { struct-contents } attributes[opt]
3041 struct-or-union attributes[opt] identifier
3043 struct-contents:
3044 struct-declaration-list
3046 struct-declaration-list:
3047 struct-declaration ;
3048 struct-declaration-list struct-declaration ;
3050 GNU extensions:
3052 struct-contents:
3053 empty
3054 struct-declaration
3055 struct-declaration-list struct-declaration
3057 struct-declaration-list:
3058 struct-declaration-list ;
3061 (Note that in the syntax here, unlike that in ISO C, the semicolons
3062 are included here rather than in struct-declaration, in order to
3063 describe the syntax with extra semicolons and missing semicolon at
3064 end.)
3066 Objective-C:
3068 struct-declaration-list:
3069 @defs ( class-name )
3071 (Note this does not include a trailing semicolon, but can be
3072 followed by further declarations, and gets a pedwarn-if-pedantic
3073 when followed by a semicolon.) */
3075 static struct c_typespec
3076 c_parser_struct_or_union_specifier (c_parser *parser)
3078 struct c_typespec ret;
3079 tree attrs;
3080 tree ident = NULL_TREE;
3081 location_t struct_loc;
3082 location_t ident_loc = UNKNOWN_LOCATION;
3083 enum tree_code code;
3084 switch (c_parser_peek_token (parser)->keyword)
3086 case RID_STRUCT:
3087 code = RECORD_TYPE;
3088 break;
3089 case RID_UNION:
3090 code = UNION_TYPE;
3091 break;
3092 default:
3093 gcc_unreachable ();
3095 struct_loc = c_parser_peek_token (parser)->location;
3096 c_parser_consume_token (parser);
3097 attrs = c_parser_attributes (parser);
3099 /* Set the location in case we create a decl now. */
3100 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3102 if (c_parser_next_token_is (parser, CPP_NAME))
3104 ident = c_parser_peek_token (parser)->value;
3105 ident_loc = c_parser_peek_token (parser)->location;
3106 struct_loc = ident_loc;
3107 c_parser_consume_token (parser);
3109 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3111 /* Parse a struct or union definition. Start the scope of the
3112 tag before parsing components. */
3113 struct c_struct_parse_info *struct_info;
3114 tree type = start_struct (struct_loc, code, ident, &struct_info);
3115 tree postfix_attrs;
3116 /* We chain the components in reverse order, then put them in
3117 forward order at the end. Each struct-declaration may
3118 declare multiple components (comma-separated), so we must use
3119 chainon to join them, although when parsing each
3120 struct-declaration we can use TREE_CHAIN directly.
3122 The theory behind all this is that there will be more
3123 semicolon separated fields than comma separated fields, and
3124 so we'll be minimizing the number of node traversals required
3125 by chainon. */
3126 tree contents;
3127 timevar_push (TV_PARSE_STRUCT);
3128 contents = NULL_TREE;
3129 c_parser_consume_token (parser);
3130 /* Handle the Objective-C @defs construct,
3131 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3132 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3134 tree name;
3135 gcc_assert (c_dialect_objc ());
3136 c_parser_consume_token (parser);
3137 matching_parens parens;
3138 if (!parens.require_open (parser))
3139 goto end_at_defs;
3140 if (c_parser_next_token_is (parser, CPP_NAME)
3141 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3143 name = c_parser_peek_token (parser)->value;
3144 c_parser_consume_token (parser);
3146 else
3148 c_parser_error (parser, "expected class name");
3149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3150 goto end_at_defs;
3152 parens.skip_until_found_close (parser);
3153 contents = nreverse (objc_get_class_ivars (name));
3155 end_at_defs:
3156 /* Parse the struct-declarations and semicolons. Problems with
3157 semicolons are diagnosed here; empty structures are diagnosed
3158 elsewhere. */
3159 while (true)
3161 tree decls;
3162 /* Parse any stray semicolon. */
3163 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3165 location_t semicolon_loc
3166 = c_parser_peek_token (parser)->location;
3167 gcc_rich_location richloc (semicolon_loc);
3168 richloc.add_fixit_remove ();
3169 pedwarn (&richloc, OPT_Wpedantic,
3170 "extra semicolon in struct or union specified");
3171 c_parser_consume_token (parser);
3172 continue;
3174 /* Stop if at the end of the struct or union contents. */
3175 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3177 c_parser_consume_token (parser);
3178 break;
3180 /* Accept #pragmas at struct scope. */
3181 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3183 c_parser_pragma (parser, pragma_struct, NULL);
3184 continue;
3186 /* Parse some comma-separated declarations, but not the
3187 trailing semicolon if any. */
3188 decls = c_parser_struct_declaration (parser);
3189 contents = chainon (decls, contents);
3190 /* If no semicolon follows, either we have a parse error or
3191 are at the end of the struct or union and should
3192 pedwarn. */
3193 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3194 c_parser_consume_token (parser);
3195 else
3197 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3198 pedwarn (c_parser_peek_token (parser)->location, 0,
3199 "no semicolon at end of struct or union");
3200 else if (parser->error
3201 || !c_parser_next_token_starts_declspecs (parser))
3203 c_parser_error (parser, "expected %<;%>");
3204 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3205 break;
3208 /* If we come here, we have already emitted an error
3209 for an expected `;', identifier or `(', and we also
3210 recovered already. Go on with the next field. */
3213 postfix_attrs = c_parser_attributes (parser);
3214 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3215 chainon (attrs, postfix_attrs), struct_info);
3216 ret.kind = ctsk_tagdef;
3217 ret.expr = NULL_TREE;
3218 ret.expr_const_operands = true;
3219 timevar_pop (TV_PARSE_STRUCT);
3220 return ret;
3222 else if (!ident)
3224 c_parser_error (parser, "expected %<{%>");
3225 ret.spec = error_mark_node;
3226 ret.kind = ctsk_tagref;
3227 ret.expr = NULL_TREE;
3228 ret.expr_const_operands = true;
3229 return ret;
3231 ret = parser_xref_tag (ident_loc, code, ident);
3232 return ret;
3235 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3236 *without* the trailing semicolon.
3238 struct-declaration:
3239 specifier-qualifier-list struct-declarator-list
3240 static_assert-declaration-no-semi
3242 specifier-qualifier-list:
3243 type-specifier specifier-qualifier-list[opt]
3244 type-qualifier specifier-qualifier-list[opt]
3245 alignment-specifier specifier-qualifier-list[opt]
3246 attributes specifier-qualifier-list[opt]
3248 struct-declarator-list:
3249 struct-declarator
3250 struct-declarator-list , attributes[opt] struct-declarator
3252 struct-declarator:
3253 declarator attributes[opt]
3254 declarator[opt] : constant-expression attributes[opt]
3256 GNU extensions:
3258 struct-declaration:
3259 __extension__ struct-declaration
3260 specifier-qualifier-list
3262 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3263 of attributes where shown is a GNU extension. In GNU C, we accept
3264 any expression without commas in the syntax (assignment
3265 expressions, not just conditional expressions); assignment
3266 expressions will be diagnosed as non-constant. */
3268 static tree
3269 c_parser_struct_declaration (c_parser *parser)
3271 struct c_declspecs *specs;
3272 tree prefix_attrs;
3273 tree all_prefix_attrs;
3274 tree decls;
3275 location_t decl_loc;
3276 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3278 int ext;
3279 tree decl;
3280 ext = disable_extension_diagnostics ();
3281 c_parser_consume_token (parser);
3282 decl = c_parser_struct_declaration (parser);
3283 restore_extension_diagnostics (ext);
3284 return decl;
3286 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3288 c_parser_static_assert_declaration_no_semi (parser);
3289 return NULL_TREE;
3291 specs = build_null_declspecs ();
3292 decl_loc = c_parser_peek_token (parser)->location;
3293 /* Strictly by the standard, we shouldn't allow _Alignas here,
3294 but it appears to have been intended to allow it there, so
3295 we're keeping it as it is until WG14 reaches a conclusion
3296 of N1731.
3297 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3298 c_parser_declspecs (parser, specs, false, true, true,
3299 true, false, cla_nonabstract_decl);
3300 if (parser->error)
3301 return NULL_TREE;
3302 if (!specs->declspecs_seen_p)
3304 c_parser_error (parser, "expected specifier-qualifier-list");
3305 return NULL_TREE;
3307 finish_declspecs (specs);
3308 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3309 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3311 tree ret;
3312 if (specs->typespec_kind == ctsk_none)
3314 pedwarn (decl_loc, OPT_Wpedantic,
3315 "ISO C forbids member declarations with no members");
3316 shadow_tag_warned (specs, pedantic);
3317 ret = NULL_TREE;
3319 else
3321 /* Support for unnamed structs or unions as members of
3322 structs or unions (which is [a] useful and [b] supports
3323 MS P-SDK). */
3324 tree attrs = NULL;
3326 ret = grokfield (c_parser_peek_token (parser)->location,
3327 build_id_declarator (NULL_TREE), specs,
3328 NULL_TREE, &attrs);
3329 if (ret)
3330 decl_attributes (&ret, attrs, 0);
3332 return ret;
3335 /* Provide better error recovery. Note that a type name here is valid,
3336 and will be treated as a field name. */
3337 if (specs->typespec_kind == ctsk_tagdef
3338 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3339 && c_parser_next_token_starts_declspecs (parser)
3340 && !c_parser_next_token_is (parser, CPP_NAME))
3342 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3343 parser->error = false;
3344 return NULL_TREE;
3347 pending_xref_error ();
3348 prefix_attrs = specs->attrs;
3349 all_prefix_attrs = prefix_attrs;
3350 specs->attrs = NULL_TREE;
3351 decls = NULL_TREE;
3352 while (true)
3354 /* Declaring one or more declarators or un-named bit-fields. */
3355 struct c_declarator *declarator;
3356 bool dummy = false;
3357 if (c_parser_next_token_is (parser, CPP_COLON))
3358 declarator = build_id_declarator (NULL_TREE);
3359 else
3360 declarator = c_parser_declarator (parser,
3361 specs->typespec_kind != ctsk_none,
3362 C_DTR_NORMAL, &dummy);
3363 if (declarator == NULL)
3365 c_parser_skip_to_end_of_block_or_statement (parser);
3366 break;
3368 if (c_parser_next_token_is (parser, CPP_COLON)
3369 || c_parser_next_token_is (parser, CPP_COMMA)
3370 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3371 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3372 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3374 tree postfix_attrs = NULL_TREE;
3375 tree width = NULL_TREE;
3376 tree d;
3377 if (c_parser_next_token_is (parser, CPP_COLON))
3379 c_parser_consume_token (parser);
3380 width = c_parser_expr_no_commas (parser, NULL).value;
3382 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3383 postfix_attrs = c_parser_attributes (parser);
3384 d = grokfield (c_parser_peek_token (parser)->location,
3385 declarator, specs, width, &all_prefix_attrs);
3386 decl_attributes (&d, chainon (postfix_attrs,
3387 all_prefix_attrs), 0);
3388 DECL_CHAIN (d) = decls;
3389 decls = d;
3390 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3391 all_prefix_attrs = chainon (c_parser_attributes (parser),
3392 prefix_attrs);
3393 else
3394 all_prefix_attrs = prefix_attrs;
3395 if (c_parser_next_token_is (parser, CPP_COMMA))
3396 c_parser_consume_token (parser);
3397 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3398 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3400 /* Semicolon consumed in caller. */
3401 break;
3403 else
3405 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3406 break;
3409 else
3411 c_parser_error (parser,
3412 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3413 "%<__attribute__%>");
3414 break;
3417 return decls;
3420 /* Parse a typeof specifier (a GNU extension).
3422 typeof-specifier:
3423 typeof ( expression )
3424 typeof ( type-name )
3427 static struct c_typespec
3428 c_parser_typeof_specifier (c_parser *parser)
3430 struct c_typespec ret;
3431 ret.kind = ctsk_typeof;
3432 ret.spec = error_mark_node;
3433 ret.expr = NULL_TREE;
3434 ret.expr_const_operands = true;
3435 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3436 c_parser_consume_token (parser);
3437 c_inhibit_evaluation_warnings++;
3438 in_typeof++;
3439 matching_parens parens;
3440 if (!parens.require_open (parser))
3442 c_inhibit_evaluation_warnings--;
3443 in_typeof--;
3444 return ret;
3446 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3448 struct c_type_name *type = c_parser_type_name (parser);
3449 c_inhibit_evaluation_warnings--;
3450 in_typeof--;
3451 if (type != NULL)
3453 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3454 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3457 else
3459 bool was_vm;
3460 location_t here = c_parser_peek_token (parser)->location;
3461 struct c_expr expr = c_parser_expression (parser);
3462 c_inhibit_evaluation_warnings--;
3463 in_typeof--;
3464 if (TREE_CODE (expr.value) == COMPONENT_REF
3465 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3466 error_at (here, "%<typeof%> applied to a bit-field");
3467 mark_exp_read (expr.value);
3468 ret.spec = TREE_TYPE (expr.value);
3469 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3470 /* This is returned with the type so that when the type is
3471 evaluated, this can be evaluated. */
3472 if (was_vm)
3473 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3474 pop_maybe_used (was_vm);
3475 /* For use in macros such as those in <stdatomic.h>, remove all
3476 qualifiers from atomic types. (const can be an issue for more macros
3477 using typeof than just the <stdatomic.h> ones.) */
3478 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3479 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3481 parens.skip_until_found_close (parser);
3482 return ret;
3485 /* Parse an alignment-specifier.
3487 C11 6.7.5:
3489 alignment-specifier:
3490 _Alignas ( type-name )
3491 _Alignas ( constant-expression )
3494 static tree
3495 c_parser_alignas_specifier (c_parser * parser)
3497 tree ret = error_mark_node;
3498 location_t loc = c_parser_peek_token (parser)->location;
3499 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3500 c_parser_consume_token (parser);
3501 if (flag_isoc99)
3502 pedwarn_c99 (loc, OPT_Wpedantic,
3503 "ISO C99 does not support %<_Alignas%>");
3504 else
3505 pedwarn_c99 (loc, OPT_Wpedantic,
3506 "ISO C90 does not support %<_Alignas%>");
3507 matching_parens parens;
3508 if (!parens.require_open (parser))
3509 return ret;
3510 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3512 struct c_type_name *type = c_parser_type_name (parser);
3513 if (type != NULL)
3514 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3515 false, true, 1);
3517 else
3518 ret = c_parser_expr_no_commas (parser, NULL).value;
3519 parens.skip_until_found_close (parser);
3520 return ret;
3523 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3524 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3525 a typedef name may be redeclared; otherwise it may not. KIND
3526 indicates which kind of declarator is wanted. Returns a valid
3527 declarator except in the case of a syntax error in which case NULL is
3528 returned. *SEEN_ID is set to true if an identifier being declared is
3529 seen; this is used to diagnose bad forms of abstract array declarators
3530 and to determine whether an identifier list is syntactically permitted.
3532 declarator:
3533 pointer[opt] direct-declarator
3535 direct-declarator:
3536 identifier
3537 ( attributes[opt] declarator )
3538 direct-declarator array-declarator
3539 direct-declarator ( parameter-type-list )
3540 direct-declarator ( identifier-list[opt] )
3542 pointer:
3543 * type-qualifier-list[opt]
3544 * type-qualifier-list[opt] pointer
3546 type-qualifier-list:
3547 type-qualifier
3548 attributes
3549 type-qualifier-list type-qualifier
3550 type-qualifier-list attributes
3552 array-declarator:
3553 [ type-qualifier-list[opt] assignment-expression[opt] ]
3554 [ static type-qualifier-list[opt] assignment-expression ]
3555 [ type-qualifier-list static assignment-expression ]
3556 [ type-qualifier-list[opt] * ]
3558 parameter-type-list:
3559 parameter-list
3560 parameter-list , ...
3562 parameter-list:
3563 parameter-declaration
3564 parameter-list , parameter-declaration
3566 parameter-declaration:
3567 declaration-specifiers declarator attributes[opt]
3568 declaration-specifiers abstract-declarator[opt] attributes[opt]
3570 identifier-list:
3571 identifier
3572 identifier-list , identifier
3574 abstract-declarator:
3575 pointer
3576 pointer[opt] direct-abstract-declarator
3578 direct-abstract-declarator:
3579 ( attributes[opt] abstract-declarator )
3580 direct-abstract-declarator[opt] array-declarator
3581 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3583 GNU extensions:
3585 direct-declarator:
3586 direct-declarator ( parameter-forward-declarations
3587 parameter-type-list[opt] )
3589 direct-abstract-declarator:
3590 direct-abstract-declarator[opt] ( parameter-forward-declarations
3591 parameter-type-list[opt] )
3593 parameter-forward-declarations:
3594 parameter-list ;
3595 parameter-forward-declarations parameter-list ;
3597 The uses of attributes shown above are GNU extensions.
3599 Some forms of array declarator are not included in C99 in the
3600 syntax for abstract declarators; these are disallowed elsewhere.
3601 This may be a defect (DR#289).
3603 This function also accepts an omitted abstract declarator as being
3604 an abstract declarator, although not part of the formal syntax. */
3606 struct c_declarator *
3607 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3608 bool *seen_id)
3610 /* Parse any initial pointer part. */
3611 if (c_parser_next_token_is (parser, CPP_MULT))
3613 struct c_declspecs *quals_attrs = build_null_declspecs ();
3614 struct c_declarator *inner;
3615 c_parser_consume_token (parser);
3616 c_parser_declspecs (parser, quals_attrs, false, false, true,
3617 false, false, cla_prefer_id);
3618 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3619 if (inner == NULL)
3620 return NULL;
3621 else
3622 return make_pointer_declarator (quals_attrs, inner);
3624 /* Now we have a direct declarator, direct abstract declarator or
3625 nothing (which counts as a direct abstract declarator here). */
3626 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3629 /* Parse a direct declarator or direct abstract declarator; arguments
3630 as c_parser_declarator. */
3632 static struct c_declarator *
3633 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3634 bool *seen_id)
3636 /* The direct declarator must start with an identifier (possibly
3637 omitted) or a parenthesized declarator (possibly abstract). In
3638 an ordinary declarator, initial parentheses must start a
3639 parenthesized declarator. In an abstract declarator or parameter
3640 declarator, they could start a parenthesized declarator or a
3641 parameter list. To tell which, the open parenthesis and any
3642 following attributes must be read. If a declaration specifier
3643 follows, then it is a parameter list; if the specifier is a
3644 typedef name, there might be an ambiguity about redeclaring it,
3645 which is resolved in the direction of treating it as a typedef
3646 name. If a close parenthesis follows, it is also an empty
3647 parameter list, as the syntax does not permit empty abstract
3648 declarators. Otherwise, it is a parenthesized declarator (in
3649 which case the analysis may be repeated inside it, recursively).
3651 ??? There is an ambiguity in a parameter declaration "int
3652 (__attribute__((foo)) x)", where x is not a typedef name: it
3653 could be an abstract declarator for a function, or declare x with
3654 parentheses. The proper resolution of this ambiguity needs
3655 documenting. At present we follow an accident of the old
3656 parser's implementation, whereby the first parameter must have
3657 some declaration specifiers other than just attributes. Thus as
3658 a parameter declaration it is treated as a parenthesized
3659 parameter named x, and as an abstract declarator it is
3660 rejected.
3662 ??? Also following the old parser, attributes inside an empty
3663 parameter list are ignored, making it a list not yielding a
3664 prototype, rather than giving an error or making it have one
3665 parameter with implicit type int.
3667 ??? Also following the old parser, typedef names may be
3668 redeclared in declarators, but not Objective-C class names. */
3670 if (kind != C_DTR_ABSTRACT
3671 && c_parser_next_token_is (parser, CPP_NAME)
3672 && ((type_seen_p
3673 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3674 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3675 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3677 struct c_declarator *inner
3678 = build_id_declarator (c_parser_peek_token (parser)->value);
3679 *seen_id = true;
3680 inner->id_loc = c_parser_peek_token (parser)->location;
3681 c_parser_consume_token (parser);
3682 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3685 if (kind != C_DTR_NORMAL
3686 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3688 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3689 inner->id_loc = c_parser_peek_token (parser)->location;
3690 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3693 /* Either we are at the end of an abstract declarator, or we have
3694 parentheses. */
3696 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3698 tree attrs;
3699 struct c_declarator *inner;
3700 c_parser_consume_token (parser);
3701 attrs = c_parser_attributes (parser);
3702 if (kind != C_DTR_NORMAL
3703 && (c_parser_next_token_starts_declspecs (parser)
3704 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3706 struct c_arg_info *args
3707 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3708 attrs);
3709 if (args == NULL)
3710 return NULL;
3711 else
3713 inner
3714 = build_function_declarator (args,
3715 build_id_declarator (NULL_TREE));
3716 return c_parser_direct_declarator_inner (parser, *seen_id,
3717 inner);
3720 /* A parenthesized declarator. */
3721 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3722 if (inner != NULL && attrs != NULL)
3723 inner = build_attrs_declarator (attrs, inner);
3724 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3726 c_parser_consume_token (parser);
3727 if (inner == NULL)
3728 return NULL;
3729 else
3730 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3732 else
3734 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3735 "expected %<)%>");
3736 return NULL;
3739 else
3741 if (kind == C_DTR_NORMAL)
3743 c_parser_error (parser, "expected identifier or %<(%>");
3744 return NULL;
3746 else
3747 return build_id_declarator (NULL_TREE);
3751 /* Parse part of a direct declarator or direct abstract declarator,
3752 given that some (in INNER) has already been parsed; ID_PRESENT is
3753 true if an identifier is present, false for an abstract
3754 declarator. */
3756 static struct c_declarator *
3757 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3758 struct c_declarator *inner)
3760 /* Parse a sequence of array declarators and parameter lists. */
3761 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3763 location_t brace_loc = c_parser_peek_token (parser)->location;
3764 struct c_declarator *declarator;
3765 struct c_declspecs *quals_attrs = build_null_declspecs ();
3766 bool static_seen;
3767 bool star_seen;
3768 struct c_expr dimen;
3769 dimen.value = NULL_TREE;
3770 dimen.original_code = ERROR_MARK;
3771 dimen.original_type = NULL_TREE;
3772 c_parser_consume_token (parser);
3773 c_parser_declspecs (parser, quals_attrs, false, false, true,
3774 false, false, cla_prefer_id);
3775 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3776 if (static_seen)
3777 c_parser_consume_token (parser);
3778 if (static_seen && !quals_attrs->declspecs_seen_p)
3779 c_parser_declspecs (parser, quals_attrs, false, false, true,
3780 false, false, cla_prefer_id);
3781 if (!quals_attrs->declspecs_seen_p)
3782 quals_attrs = NULL;
3783 /* If "static" is present, there must be an array dimension.
3784 Otherwise, there may be a dimension, "*", or no
3785 dimension. */
3786 if (static_seen)
3788 star_seen = false;
3789 dimen = c_parser_expr_no_commas (parser, NULL);
3791 else
3793 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3795 dimen.value = NULL_TREE;
3796 star_seen = false;
3798 else if (c_parser_next_token_is (parser, CPP_MULT))
3800 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3802 dimen.value = NULL_TREE;
3803 star_seen = true;
3804 c_parser_consume_token (parser);
3806 else
3808 star_seen = false;
3809 dimen = c_parser_expr_no_commas (parser, NULL);
3812 else
3814 star_seen = false;
3815 dimen = c_parser_expr_no_commas (parser, NULL);
3818 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3819 c_parser_consume_token (parser);
3820 else
3822 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3823 "expected %<]%>");
3824 return NULL;
3826 if (dimen.value)
3827 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3828 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3829 static_seen, star_seen);
3830 if (declarator == NULL)
3831 return NULL;
3832 inner = set_array_declarator_inner (declarator, inner);
3833 return c_parser_direct_declarator_inner (parser, id_present, inner);
3835 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3837 tree attrs;
3838 struct c_arg_info *args;
3839 c_parser_consume_token (parser);
3840 attrs = c_parser_attributes (parser);
3841 args = c_parser_parms_declarator (parser, id_present, attrs);
3842 if (args == NULL)
3843 return NULL;
3844 else
3846 inner = build_function_declarator (args, inner);
3847 return c_parser_direct_declarator_inner (parser, id_present, inner);
3850 return inner;
3853 /* Parse a parameter list or identifier list, including the closing
3854 parenthesis but not the opening one. ATTRS are the attributes at
3855 the start of the list. ID_LIST_OK is true if an identifier list is
3856 acceptable; such a list must not have attributes at the start. */
3858 static struct c_arg_info *
3859 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3861 push_scope ();
3862 declare_parm_level ();
3863 /* If the list starts with an identifier, it is an identifier list.
3864 Otherwise, it is either a prototype list or an empty list. */
3865 if (id_list_ok
3866 && !attrs
3867 && c_parser_next_token_is (parser, CPP_NAME)
3868 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3870 /* Look ahead to detect typos in type names. */
3871 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3872 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3873 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3874 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3875 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3877 tree list = NULL_TREE, *nextp = &list;
3878 while (c_parser_next_token_is (parser, CPP_NAME)
3879 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3881 *nextp = build_tree_list (NULL_TREE,
3882 c_parser_peek_token (parser)->value);
3883 nextp = & TREE_CHAIN (*nextp);
3884 c_parser_consume_token (parser);
3885 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3886 break;
3887 c_parser_consume_token (parser);
3888 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3890 c_parser_error (parser, "expected identifier");
3891 break;
3894 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3896 struct c_arg_info *ret = build_arg_info ();
3897 ret->types = list;
3898 c_parser_consume_token (parser);
3899 pop_scope ();
3900 return ret;
3902 else
3904 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3905 "expected %<)%>");
3906 pop_scope ();
3907 return NULL;
3910 else
3912 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3913 NULL);
3914 pop_scope ();
3915 return ret;
3919 /* Parse a parameter list (possibly empty), including the closing
3920 parenthesis but not the opening one. ATTRS are the attributes at
3921 the start of the list. EXPR is NULL or an expression that needs to
3922 be evaluated for the side effects of array size expressions in the
3923 parameters. */
3925 static struct c_arg_info *
3926 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3928 bool bad_parm = false;
3930 /* ??? Following the old parser, forward parameter declarations may
3931 use abstract declarators, and if no real parameter declarations
3932 follow the forward declarations then this is not diagnosed. Also
3933 note as above that attributes are ignored as the only contents of
3934 the parentheses, or as the only contents after forward
3935 declarations. */
3936 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3938 struct c_arg_info *ret = build_arg_info ();
3939 c_parser_consume_token (parser);
3940 return ret;
3942 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3944 struct c_arg_info *ret = build_arg_info ();
3946 if (flag_allow_parameterless_variadic_functions)
3948 /* F (...) is allowed. */
3949 ret->types = NULL_TREE;
3951 else
3953 /* Suppress -Wold-style-definition for this case. */
3954 ret->types = error_mark_node;
3955 error_at (c_parser_peek_token (parser)->location,
3956 "ISO C requires a named argument before %<...%>");
3958 c_parser_consume_token (parser);
3959 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3961 c_parser_consume_token (parser);
3962 return ret;
3964 else
3966 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3967 "expected %<)%>");
3968 return NULL;
3971 /* Nonempty list of parameters, either terminated with semicolon
3972 (forward declarations; recurse) or with close parenthesis (normal
3973 function) or with ", ... )" (variadic function). */
3974 while (true)
3976 /* Parse a parameter. */
3977 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3978 attrs = NULL_TREE;
3979 if (parm == NULL)
3980 bad_parm = true;
3981 else
3982 push_parm_decl (parm, &expr);
3983 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3985 tree new_attrs;
3986 c_parser_consume_token (parser);
3987 mark_forward_parm_decls ();
3988 new_attrs = c_parser_attributes (parser);
3989 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3991 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3993 c_parser_consume_token (parser);
3994 if (bad_parm)
3995 return NULL;
3996 else
3997 return get_parm_info (false, expr);
3999 if (!c_parser_require (parser, CPP_COMMA,
4000 "expected %<;%>, %<,%> or %<)%>",
4001 UNKNOWN_LOCATION, false))
4003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4004 return NULL;
4006 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4008 c_parser_consume_token (parser);
4009 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4011 c_parser_consume_token (parser);
4012 if (bad_parm)
4013 return NULL;
4014 else
4015 return get_parm_info (true, expr);
4017 else
4019 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4020 "expected %<)%>");
4021 return NULL;
4027 /* Parse a parameter declaration. ATTRS are the attributes at the
4028 start of the declaration if it is the first parameter. */
4030 static struct c_parm *
4031 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4033 struct c_declspecs *specs;
4034 struct c_declarator *declarator;
4035 tree prefix_attrs;
4036 tree postfix_attrs = NULL_TREE;
4037 bool dummy = false;
4039 /* Accept #pragmas between parameter declarations. */
4040 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4041 c_parser_pragma (parser, pragma_param, NULL);
4043 if (!c_parser_next_token_starts_declspecs (parser))
4045 c_token *token = c_parser_peek_token (parser);
4046 if (parser->error)
4047 return NULL;
4048 c_parser_set_source_position_from_token (token);
4049 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4051 name_hint hint = lookup_name_fuzzy (token->value,
4052 FUZZY_LOOKUP_TYPENAME,
4053 token->location);
4054 if (hint)
4056 gcc_rich_location richloc (token->location);
4057 richloc.add_fixit_replace (hint.suggestion ());
4058 error_at (&richloc,
4059 "unknown type name %qE; did you mean %qs?",
4060 token->value, hint.suggestion ());
4062 else
4063 error_at (token->location, "unknown type name %qE", token->value);
4064 parser->error = true;
4066 /* ??? In some Objective-C cases '...' isn't applicable so there
4067 should be a different message. */
4068 else
4069 c_parser_error (parser,
4070 "expected declaration specifiers or %<...%>");
4071 c_parser_skip_to_end_of_parameter (parser);
4072 return NULL;
4075 location_t start_loc = c_parser_peek_token (parser)->location;
4077 specs = build_null_declspecs ();
4078 if (attrs)
4080 declspecs_add_attrs (input_location, specs, attrs);
4081 attrs = NULL_TREE;
4083 c_parser_declspecs (parser, specs, true, true, true, true, false,
4084 cla_nonabstract_decl);
4085 finish_declspecs (specs);
4086 pending_xref_error ();
4087 prefix_attrs = specs->attrs;
4088 specs->attrs = NULL_TREE;
4089 declarator = c_parser_declarator (parser,
4090 specs->typespec_kind != ctsk_none,
4091 C_DTR_PARM, &dummy);
4092 if (declarator == NULL)
4094 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4095 return NULL;
4097 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4098 postfix_attrs = c_parser_attributes (parser);
4100 /* Generate a location for the parameter, ranging from the start of the
4101 initial token to the end of the final token.
4103 If we have a identifier, then use it for the caret location, e.g.
4105 extern int callee (int one, int (*two)(int, int), float three);
4106 ~~~~~~^~~~~~~~~~~~~~
4108 otherwise, reuse the start location for the caret location e.g.:
4110 extern int callee (int one, int (*)(int, int), float three);
4111 ^~~~~~~~~~~~~~~~~
4113 location_t end_loc = parser->last_token_location;
4115 /* Find any cdk_id declarator; determine if we have an identifier. */
4116 c_declarator *id_declarator = declarator;
4117 while (id_declarator && id_declarator->kind != cdk_id)
4118 id_declarator = id_declarator->declarator;
4119 location_t caret_loc = (id_declarator->u.id
4120 ? id_declarator->id_loc
4121 : start_loc);
4122 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4124 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4125 declarator, param_loc);
4128 /* Parse a string literal in an asm expression. It should not be
4129 translated, and wide string literals are an error although
4130 permitted by the syntax. This is a GNU extension.
4132 asm-string-literal:
4133 string-literal
4135 ??? At present, following the old parser, the caller needs to have
4136 set lex_untranslated_string to 1. It would be better to follow the
4137 C++ parser rather than using this kludge. */
4139 static tree
4140 c_parser_asm_string_literal (c_parser *parser)
4142 tree str;
4143 int save_flag = warn_overlength_strings;
4144 warn_overlength_strings = 0;
4145 if (c_parser_next_token_is (parser, CPP_STRING))
4147 str = c_parser_peek_token (parser)->value;
4148 c_parser_consume_token (parser);
4150 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4152 error_at (c_parser_peek_token (parser)->location,
4153 "wide string literal in %<asm%>");
4154 str = build_string (1, "");
4155 c_parser_consume_token (parser);
4157 else
4159 c_parser_error (parser, "expected string literal");
4160 str = NULL_TREE;
4162 warn_overlength_strings = save_flag;
4163 return str;
4166 /* Parse a simple asm expression. This is used in restricted
4167 contexts, where a full expression with inputs and outputs does not
4168 make sense. This is a GNU extension.
4170 simple-asm-expr:
4171 asm ( asm-string-literal )
4174 static tree
4175 c_parser_simple_asm_expr (c_parser *parser)
4177 tree str;
4178 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4179 /* ??? Follow the C++ parser rather than using the
4180 lex_untranslated_string kludge. */
4181 parser->lex_untranslated_string = true;
4182 c_parser_consume_token (parser);
4183 matching_parens parens;
4184 if (!parens.require_open (parser))
4186 parser->lex_untranslated_string = false;
4187 return NULL_TREE;
4189 str = c_parser_asm_string_literal (parser);
4190 parser->lex_untranslated_string = false;
4191 if (!parens.require_close (parser))
4193 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4194 return NULL_TREE;
4196 return str;
4199 static tree
4200 c_parser_attribute_any_word (c_parser *parser)
4202 tree attr_name = NULL_TREE;
4204 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4206 /* ??? See comment above about what keywords are accepted here. */
4207 bool ok;
4208 switch (c_parser_peek_token (parser)->keyword)
4210 case RID_STATIC:
4211 case RID_UNSIGNED:
4212 case RID_LONG:
4213 case RID_CONST:
4214 case RID_EXTERN:
4215 case RID_REGISTER:
4216 case RID_TYPEDEF:
4217 case RID_SHORT:
4218 case RID_INLINE:
4219 case RID_NORETURN:
4220 case RID_VOLATILE:
4221 case RID_SIGNED:
4222 case RID_AUTO:
4223 case RID_RESTRICT:
4224 case RID_COMPLEX:
4225 case RID_THREAD:
4226 case RID_INT:
4227 case RID_CHAR:
4228 case RID_FLOAT:
4229 case RID_DOUBLE:
4230 case RID_VOID:
4231 case RID_DFLOAT32:
4232 case RID_DFLOAT64:
4233 case RID_DFLOAT128:
4234 CASE_RID_FLOATN_NX:
4235 case RID_BOOL:
4236 case RID_FRACT:
4237 case RID_ACCUM:
4238 case RID_SAT:
4239 case RID_TRANSACTION_ATOMIC:
4240 case RID_TRANSACTION_CANCEL:
4241 case RID_ATOMIC:
4242 case RID_AUTO_TYPE:
4243 case RID_INT_N_0:
4244 case RID_INT_N_1:
4245 case RID_INT_N_2:
4246 case RID_INT_N_3:
4247 ok = true;
4248 break;
4249 default:
4250 ok = false;
4251 break;
4253 if (!ok)
4254 return NULL_TREE;
4256 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4257 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4259 else if (c_parser_next_token_is (parser, CPP_NAME))
4260 attr_name = c_parser_peek_token (parser)->value;
4262 return attr_name;
4265 /* Parse (possibly empty) attributes. This is a GNU extension.
4267 attributes:
4268 empty
4269 attributes attribute
4271 attribute:
4272 __attribute__ ( ( attribute-list ) )
4274 attribute-list:
4275 attrib
4276 attribute_list , attrib
4278 attrib:
4279 empty
4280 any-word
4281 any-word ( identifier )
4282 any-word ( identifier , nonempty-expr-list )
4283 any-word ( expr-list )
4285 where the "identifier" must not be declared as a type, and
4286 "any-word" may be any identifier (including one declared as a
4287 type), a reserved word storage class specifier, type specifier or
4288 type qualifier. ??? This still leaves out most reserved keywords
4289 (following the old parser), shouldn't we include them, and why not
4290 allow identifiers declared as types to start the arguments? */
4292 static tree
4293 c_parser_attributes (c_parser *parser)
4295 tree attrs = NULL_TREE;
4296 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4298 /* ??? Follow the C++ parser rather than using the
4299 lex_untranslated_string kludge. */
4300 parser->lex_untranslated_string = true;
4301 /* Consume the `__attribute__' keyword. */
4302 c_parser_consume_token (parser);
4303 /* Look for the two `(' tokens. */
4304 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4306 parser->lex_untranslated_string = false;
4307 return attrs;
4309 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4311 parser->lex_untranslated_string = false;
4312 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4313 return attrs;
4315 /* Parse the attribute list. */
4316 while (c_parser_next_token_is (parser, CPP_COMMA)
4317 || c_parser_next_token_is (parser, CPP_NAME)
4318 || c_parser_next_token_is (parser, CPP_KEYWORD))
4320 tree attr, attr_name, attr_args;
4321 vec<tree, va_gc> *expr_list;
4322 if (c_parser_next_token_is (parser, CPP_COMMA))
4324 c_parser_consume_token (parser);
4325 continue;
4328 attr_name = c_parser_attribute_any_word (parser);
4329 if (attr_name == NULL)
4330 break;
4331 attr_name = canonicalize_attr_name (attr_name);
4332 c_parser_consume_token (parser);
4333 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4335 attr = build_tree_list (attr_name, NULL_TREE);
4336 /* Add this attribute to the list. */
4337 attrs = chainon (attrs, attr);
4338 /* If the next token isn't a comma, we're done. */
4339 if (!c_parser_next_token_is (parser, CPP_COMMA))
4340 break;
4341 continue;
4343 c_parser_consume_token (parser);
4344 /* Parse the attribute contents. If they start with an
4345 identifier which is followed by a comma or close
4346 parenthesis, then the arguments start with that
4347 identifier; otherwise they are an expression list.
4348 In objective-c the identifier may be a classname. */
4349 if (c_parser_next_token_is (parser, CPP_NAME)
4350 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4351 || (c_dialect_objc ()
4352 && c_parser_peek_token (parser)->id_kind
4353 == C_ID_CLASSNAME))
4354 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4355 || (c_parser_peek_2nd_token (parser)->type
4356 == CPP_CLOSE_PAREN))
4357 && (attribute_takes_identifier_p (attr_name)
4358 || (c_dialect_objc ()
4359 && c_parser_peek_token (parser)->id_kind
4360 == C_ID_CLASSNAME)))
4362 tree arg1 = c_parser_peek_token (parser)->value;
4363 c_parser_consume_token (parser);
4364 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4365 attr_args = build_tree_list (NULL_TREE, arg1);
4366 else
4368 tree tree_list;
4369 c_parser_consume_token (parser);
4370 expr_list = c_parser_expr_list (parser, false, true,
4371 NULL, NULL, NULL, NULL);
4372 tree_list = build_tree_list_vec (expr_list);
4373 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4374 release_tree_vector (expr_list);
4377 else
4379 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4380 attr_args = NULL_TREE;
4381 else
4383 expr_list = c_parser_expr_list (parser, false, true,
4384 NULL, NULL, NULL, NULL);
4385 attr_args = build_tree_list_vec (expr_list);
4386 release_tree_vector (expr_list);
4390 attr = build_tree_list (attr_name, attr_args);
4391 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4392 c_parser_consume_token (parser);
4393 else
4395 parser->lex_untranslated_string = false;
4396 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4397 "expected %<)%>");
4398 return attrs;
4400 /* Add this attribute to the list. */
4401 attrs = chainon (attrs, attr);
4402 /* If the next token isn't a comma, we're done. */
4403 if (!c_parser_next_token_is (parser, CPP_COMMA))
4404 break;
4406 /* Look for the two `)' tokens. */
4407 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4408 c_parser_consume_token (parser);
4409 else
4411 parser->lex_untranslated_string = false;
4412 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4413 "expected %<)%>");
4414 return attrs;
4416 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4417 c_parser_consume_token (parser);
4418 else
4420 parser->lex_untranslated_string = false;
4421 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4422 "expected %<)%>");
4423 return attrs;
4425 parser->lex_untranslated_string = false;
4428 return attrs;
4431 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4432 says whether alignment specifiers are OK (only in cases that might
4433 be the type name of a compound literal).
4435 type-name:
4436 specifier-qualifier-list abstract-declarator[opt]
4439 struct c_type_name *
4440 c_parser_type_name (c_parser *parser, bool alignas_ok)
4442 struct c_declspecs *specs = build_null_declspecs ();
4443 struct c_declarator *declarator;
4444 struct c_type_name *ret;
4445 bool dummy = false;
4446 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4447 cla_prefer_type);
4448 if (!specs->declspecs_seen_p)
4450 c_parser_error (parser, "expected specifier-qualifier-list");
4451 return NULL;
4453 if (specs->type != error_mark_node)
4455 pending_xref_error ();
4456 finish_declspecs (specs);
4458 declarator = c_parser_declarator (parser,
4459 specs->typespec_kind != ctsk_none,
4460 C_DTR_ABSTRACT, &dummy);
4461 if (declarator == NULL)
4462 return NULL;
4463 ret = XOBNEW (&parser_obstack, struct c_type_name);
4464 ret->specs = specs;
4465 ret->declarator = declarator;
4466 return ret;
4469 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4471 initializer:
4472 assignment-expression
4473 { initializer-list }
4474 { initializer-list , }
4476 initializer-list:
4477 designation[opt] initializer
4478 initializer-list , designation[opt] initializer
4480 designation:
4481 designator-list =
4483 designator-list:
4484 designator
4485 designator-list designator
4487 designator:
4488 array-designator
4489 . identifier
4491 array-designator:
4492 [ constant-expression ]
4494 GNU extensions:
4496 initializer:
4499 designation:
4500 array-designator
4501 identifier :
4503 array-designator:
4504 [ constant-expression ... constant-expression ]
4506 Any expression without commas is accepted in the syntax for the
4507 constant-expressions, with non-constant expressions rejected later.
4509 This function is only used for top-level initializers; for nested
4510 ones, see c_parser_initval. */
4512 static struct c_expr
4513 c_parser_initializer (c_parser *parser)
4515 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4516 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4517 else
4519 struct c_expr ret;
4520 location_t loc = c_parser_peek_token (parser)->location;
4521 ret = c_parser_expr_no_commas (parser, NULL);
4522 if (TREE_CODE (ret.value) != STRING_CST
4523 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4524 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4525 return ret;
4529 /* The location of the last comma within the current initializer list,
4530 or UNKNOWN_LOCATION if not within one. */
4532 location_t last_init_list_comma;
4534 /* Parse a braced initializer list. TYPE is the type specified for a
4535 compound literal, and NULL_TREE for other initializers and for
4536 nested braced lists. NESTED_P is true for nested braced lists,
4537 false for the list of a compound literal or the list that is the
4538 top-level initializer in a declaration. */
4540 static struct c_expr
4541 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4542 struct obstack *outer_obstack)
4544 struct c_expr ret;
4545 struct obstack braced_init_obstack;
4546 location_t brace_loc = c_parser_peek_token (parser)->location;
4547 gcc_obstack_init (&braced_init_obstack);
4548 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4549 matching_braces braces;
4550 braces.consume_open (parser);
4551 if (nested_p)
4553 finish_implicit_inits (brace_loc, outer_obstack);
4554 push_init_level (brace_loc, 0, &braced_init_obstack);
4556 else
4557 really_start_incremental_init (type);
4558 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4560 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4562 else
4564 /* Parse a non-empty initializer list, possibly with a trailing
4565 comma. */
4566 while (true)
4568 c_parser_initelt (parser, &braced_init_obstack);
4569 if (parser->error)
4570 break;
4571 if (c_parser_next_token_is (parser, CPP_COMMA))
4573 last_init_list_comma = c_parser_peek_token (parser)->location;
4574 c_parser_consume_token (parser);
4576 else
4577 break;
4578 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4579 break;
4582 c_token *next_tok = c_parser_peek_token (parser);
4583 if (next_tok->type != CPP_CLOSE_BRACE)
4585 ret.set_error ();
4586 ret.original_code = ERROR_MARK;
4587 ret.original_type = NULL;
4588 braces.skip_until_found_close (parser);
4589 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4590 obstack_free (&braced_init_obstack, NULL);
4591 return ret;
4593 location_t close_loc = next_tok->location;
4594 c_parser_consume_token (parser);
4595 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4596 obstack_free (&braced_init_obstack, NULL);
4597 set_c_expr_source_range (&ret, brace_loc, close_loc);
4598 return ret;
4601 /* Parse a nested initializer, including designators. */
4603 static void
4604 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4606 /* Parse any designator or designator list. A single array
4607 designator may have the subsequent "=" omitted in GNU C, but a
4608 longer list or a structure member designator may not. */
4609 if (c_parser_next_token_is (parser, CPP_NAME)
4610 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4612 /* Old-style structure member designator. */
4613 set_init_label (c_parser_peek_token (parser)->location,
4614 c_parser_peek_token (parser)->value,
4615 c_parser_peek_token (parser)->location,
4616 braced_init_obstack);
4617 /* Use the colon as the error location. */
4618 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4619 "obsolete use of designated initializer with %<:%>");
4620 c_parser_consume_token (parser);
4621 c_parser_consume_token (parser);
4623 else
4625 /* des_seen is 0 if there have been no designators, 1 if there
4626 has been a single array designator and 2 otherwise. */
4627 int des_seen = 0;
4628 /* Location of a designator. */
4629 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4630 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4631 || c_parser_next_token_is (parser, CPP_DOT))
4633 int des_prev = des_seen;
4634 if (!des_seen)
4635 des_loc = c_parser_peek_token (parser)->location;
4636 if (des_seen < 2)
4637 des_seen++;
4638 if (c_parser_next_token_is (parser, CPP_DOT))
4640 des_seen = 2;
4641 c_parser_consume_token (parser);
4642 if (c_parser_next_token_is (parser, CPP_NAME))
4644 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4645 c_parser_peek_token (parser)->location,
4646 braced_init_obstack);
4647 c_parser_consume_token (parser);
4649 else
4651 struct c_expr init;
4652 init.set_error ();
4653 init.original_code = ERROR_MARK;
4654 init.original_type = NULL;
4655 c_parser_error (parser, "expected identifier");
4656 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4657 process_init_element (input_location, init, false,
4658 braced_init_obstack);
4659 return;
4662 else
4664 tree first, second;
4665 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4666 location_t array_index_loc = UNKNOWN_LOCATION;
4667 /* ??? Following the old parser, [ objc-receiver
4668 objc-message-args ] is accepted as an initializer,
4669 being distinguished from a designator by what follows
4670 the first assignment expression inside the square
4671 brackets, but after a first array designator a
4672 subsequent square bracket is for Objective-C taken to
4673 start an expression, using the obsolete form of
4674 designated initializer without '=', rather than
4675 possibly being a second level of designation: in LALR
4676 terms, the '[' is shifted rather than reducing
4677 designator to designator-list. */
4678 if (des_prev == 1 && c_dialect_objc ())
4680 des_seen = des_prev;
4681 break;
4683 if (des_prev == 0 && c_dialect_objc ())
4685 /* This might be an array designator or an
4686 Objective-C message expression. If the former,
4687 continue parsing here; if the latter, parse the
4688 remainder of the initializer given the starting
4689 primary-expression. ??? It might make sense to
4690 distinguish when des_prev == 1 as well; see
4691 previous comment. */
4692 tree rec, args;
4693 struct c_expr mexpr;
4694 c_parser_consume_token (parser);
4695 if (c_parser_peek_token (parser)->type == CPP_NAME
4696 && ((c_parser_peek_token (parser)->id_kind
4697 == C_ID_TYPENAME)
4698 || (c_parser_peek_token (parser)->id_kind
4699 == C_ID_CLASSNAME)))
4701 /* Type name receiver. */
4702 tree id = c_parser_peek_token (parser)->value;
4703 c_parser_consume_token (parser);
4704 rec = objc_get_class_reference (id);
4705 goto parse_message_args;
4707 first = c_parser_expr_no_commas (parser, NULL).value;
4708 mark_exp_read (first);
4709 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4710 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4711 goto array_desig_after_first;
4712 /* Expression receiver. So far only one part
4713 without commas has been parsed; there might be
4714 more of the expression. */
4715 rec = first;
4716 while (c_parser_next_token_is (parser, CPP_COMMA))
4718 struct c_expr next;
4719 location_t comma_loc, exp_loc;
4720 comma_loc = c_parser_peek_token (parser)->location;
4721 c_parser_consume_token (parser);
4722 exp_loc = c_parser_peek_token (parser)->location;
4723 next = c_parser_expr_no_commas (parser, NULL);
4724 next = convert_lvalue_to_rvalue (exp_loc, next,
4725 true, true);
4726 rec = build_compound_expr (comma_loc, rec, next.value);
4728 parse_message_args:
4729 /* Now parse the objc-message-args. */
4730 args = c_parser_objc_message_args (parser);
4731 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4732 "expected %<]%>");
4733 mexpr.value
4734 = objc_build_message_expr (rec, args);
4735 mexpr.original_code = ERROR_MARK;
4736 mexpr.original_type = NULL;
4737 /* Now parse and process the remainder of the
4738 initializer, starting with this message
4739 expression as a primary-expression. */
4740 c_parser_initval (parser, &mexpr, braced_init_obstack);
4741 return;
4743 c_parser_consume_token (parser);
4744 array_index_loc = c_parser_peek_token (parser)->location;
4745 first = c_parser_expr_no_commas (parser, NULL).value;
4746 mark_exp_read (first);
4747 array_desig_after_first:
4748 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4750 ellipsis_loc = c_parser_peek_token (parser)->location;
4751 c_parser_consume_token (parser);
4752 second = c_parser_expr_no_commas (parser, NULL).value;
4753 mark_exp_read (second);
4755 else
4756 second = NULL_TREE;
4757 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4759 c_parser_consume_token (parser);
4760 set_init_index (array_index_loc, first, second,
4761 braced_init_obstack);
4762 if (second)
4763 pedwarn (ellipsis_loc, OPT_Wpedantic,
4764 "ISO C forbids specifying range of elements to initialize");
4766 else
4767 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4768 "expected %<]%>");
4771 if (des_seen >= 1)
4773 if (c_parser_next_token_is (parser, CPP_EQ))
4775 pedwarn_c90 (des_loc, OPT_Wpedantic,
4776 "ISO C90 forbids specifying subobject "
4777 "to initialize");
4778 c_parser_consume_token (parser);
4780 else
4782 if (des_seen == 1)
4783 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4784 "obsolete use of designated initializer without %<=%>");
4785 else
4787 struct c_expr init;
4788 init.set_error ();
4789 init.original_code = ERROR_MARK;
4790 init.original_type = NULL;
4791 c_parser_error (parser, "expected %<=%>");
4792 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4793 process_init_element (input_location, init, false,
4794 braced_init_obstack);
4795 return;
4800 c_parser_initval (parser, NULL, braced_init_obstack);
4803 /* Parse a nested initializer; as c_parser_initializer but parses
4804 initializers within braced lists, after any designators have been
4805 applied. If AFTER is not NULL then it is an Objective-C message
4806 expression which is the primary-expression starting the
4807 initializer. */
4809 static void
4810 c_parser_initval (c_parser *parser, struct c_expr *after,
4811 struct obstack * braced_init_obstack)
4813 struct c_expr init;
4814 gcc_assert (!after || c_dialect_objc ());
4815 location_t loc = c_parser_peek_token (parser)->location;
4817 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4818 init = c_parser_braced_init (parser, NULL_TREE, true,
4819 braced_init_obstack);
4820 else
4822 init = c_parser_expr_no_commas (parser, after);
4823 if (init.value != NULL_TREE
4824 && TREE_CODE (init.value) != STRING_CST
4825 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4826 init = convert_lvalue_to_rvalue (loc, init, true, true);
4828 process_init_element (loc, init, false, braced_init_obstack);
4831 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4832 C99 6.8.2, C11 6.8.2).
4834 compound-statement:
4835 { block-item-list[opt] }
4836 { label-declarations block-item-list }
4838 block-item-list:
4839 block-item
4840 block-item-list block-item
4842 block-item:
4843 nested-declaration
4844 statement
4846 nested-declaration:
4847 declaration
4849 GNU extensions:
4851 compound-statement:
4852 { label-declarations block-item-list }
4854 nested-declaration:
4855 __extension__ nested-declaration
4856 nested-function-definition
4858 label-declarations:
4859 label-declaration
4860 label-declarations label-declaration
4862 label-declaration:
4863 __label__ identifier-list ;
4865 Allowing the mixing of declarations and code is new in C99. The
4866 GNU syntax also permits (not shown above) labels at the end of
4867 compound statements, which yield an error. We don't allow labels
4868 on declarations; this might seem like a natural extension, but
4869 there would be a conflict between attributes on the label and
4870 prefix attributes on the declaration. ??? The syntax follows the
4871 old parser in requiring something after label declarations.
4872 Although they are erroneous if the labels declared aren't defined,
4873 is it useful for the syntax to be this way?
4875 OpenACC:
4877 block-item:
4878 openacc-directive
4880 openacc-directive:
4881 update-directive
4883 OpenMP:
4885 block-item:
4886 openmp-directive
4888 openmp-directive:
4889 barrier-directive
4890 flush-directive
4891 taskwait-directive
4892 taskyield-directive
4893 cancel-directive
4894 cancellation-point-directive */
4896 static tree
4897 c_parser_compound_statement (c_parser *parser)
4899 tree stmt;
4900 location_t brace_loc;
4901 brace_loc = c_parser_peek_token (parser)->location;
4902 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4904 /* Ensure a scope is entered and left anyway to avoid confusion
4905 if we have just prepared to enter a function body. */
4906 stmt = c_begin_compound_stmt (true);
4907 c_end_compound_stmt (brace_loc, stmt, true);
4908 return error_mark_node;
4910 stmt = c_begin_compound_stmt (true);
4911 c_parser_compound_statement_nostart (parser);
4913 return c_end_compound_stmt (brace_loc, stmt, true);
4916 /* Parse a compound statement except for the opening brace. This is
4917 used for parsing both compound statements and statement expressions
4918 (which follow different paths to handling the opening). */
4920 static void
4921 c_parser_compound_statement_nostart (c_parser *parser)
4923 bool last_stmt = false;
4924 bool last_label = false;
4925 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4926 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4927 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4929 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4930 c_parser_consume_token (parser);
4931 return;
4933 mark_valid_location_for_stdc_pragma (true);
4934 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4936 /* Read zero or more forward-declarations for labels that nested
4937 functions can jump to. */
4938 mark_valid_location_for_stdc_pragma (false);
4939 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4941 label_loc = c_parser_peek_token (parser)->location;
4942 c_parser_consume_token (parser);
4943 /* Any identifiers, including those declared as type names,
4944 are OK here. */
4945 while (true)
4947 tree label;
4948 if (c_parser_next_token_is_not (parser, CPP_NAME))
4950 c_parser_error (parser, "expected identifier");
4951 break;
4953 label
4954 = declare_label (c_parser_peek_token (parser)->value);
4955 C_DECLARED_LABEL_FLAG (label) = 1;
4956 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4957 c_parser_consume_token (parser);
4958 if (c_parser_next_token_is (parser, CPP_COMMA))
4959 c_parser_consume_token (parser);
4960 else
4961 break;
4963 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4965 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4967 /* We must now have at least one statement, label or declaration. */
4968 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4970 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4971 c_parser_error (parser, "expected declaration or statement");
4972 c_parser_consume_token (parser);
4973 return;
4975 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4977 location_t loc = c_parser_peek_token (parser)->location;
4978 loc = expansion_point_location_if_in_system_header (loc);
4979 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4980 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4981 || (c_parser_next_token_is (parser, CPP_NAME)
4982 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4984 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4985 label_loc = c_parser_peek_2nd_token (parser)->location;
4986 else
4987 label_loc = c_parser_peek_token (parser)->location;
4988 last_label = true;
4989 last_stmt = false;
4990 mark_valid_location_for_stdc_pragma (false);
4991 c_parser_label (parser);
4993 else if (!last_label
4994 && c_parser_next_tokens_start_declaration (parser))
4996 last_label = false;
4997 mark_valid_location_for_stdc_pragma (false);
4998 bool fallthru_attr_p = false;
4999 c_parser_declaration_or_fndef (parser, true, true, true, true,
5000 true, NULL, vNULL, NULL,
5001 &fallthru_attr_p);
5002 if (last_stmt && !fallthru_attr_p)
5003 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5004 "ISO C90 forbids mixed declarations and code");
5005 last_stmt = fallthru_attr_p;
5007 else if (!last_label
5008 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5010 /* __extension__ can start a declaration, but is also an
5011 unary operator that can start an expression. Consume all
5012 but the last of a possible series of __extension__ to
5013 determine which. */
5014 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5015 && (c_parser_peek_2nd_token (parser)->keyword
5016 == RID_EXTENSION))
5017 c_parser_consume_token (parser);
5018 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5020 int ext;
5021 ext = disable_extension_diagnostics ();
5022 c_parser_consume_token (parser);
5023 last_label = false;
5024 mark_valid_location_for_stdc_pragma (false);
5025 c_parser_declaration_or_fndef (parser, true, true, true, true,
5026 true, NULL, vNULL);
5027 /* Following the old parser, __extension__ does not
5028 disable this diagnostic. */
5029 restore_extension_diagnostics (ext);
5030 if (last_stmt)
5031 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5032 "ISO C90 forbids mixed declarations and code");
5033 last_stmt = false;
5035 else
5036 goto statement;
5038 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5040 /* External pragmas, and some omp pragmas, are not associated
5041 with regular c code, and so are not to be considered statements
5042 syntactically. This ensures that the user doesn't put them
5043 places that would turn into syntax errors if the directive
5044 were ignored. */
5045 if (c_parser_pragma (parser,
5046 last_label ? pragma_stmt : pragma_compound,
5047 NULL))
5048 last_label = false, last_stmt = true;
5050 else if (c_parser_next_token_is (parser, CPP_EOF))
5052 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5053 c_parser_error (parser, "expected declaration or statement");
5054 return;
5056 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5058 if (parser->in_if_block)
5060 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5061 error_at (loc, "expected %<}%> before %<else%>");
5062 return;
5064 else
5066 error_at (loc, "%<else%> without a previous %<if%>");
5067 c_parser_consume_token (parser);
5068 continue;
5071 else
5073 statement:
5074 last_label = false;
5075 last_stmt = true;
5076 mark_valid_location_for_stdc_pragma (false);
5077 c_parser_statement_after_labels (parser, NULL);
5080 parser->error = false;
5082 if (last_label)
5083 error_at (label_loc, "label at end of compound statement");
5084 c_parser_consume_token (parser);
5085 /* Restore the value we started with. */
5086 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5089 /* Parse all consecutive labels. */
5091 static void
5092 c_parser_all_labels (c_parser *parser)
5094 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5095 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5096 || (c_parser_next_token_is (parser, CPP_NAME)
5097 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5098 c_parser_label (parser);
5101 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5103 label:
5104 identifier : attributes[opt]
5105 case constant-expression :
5106 default :
5108 GNU extensions:
5110 label:
5111 case constant-expression ... constant-expression :
5113 The use of attributes on labels is a GNU extension. The syntax in
5114 GNU C accepts any expressions without commas, non-constant
5115 expressions being rejected later. */
5117 static void
5118 c_parser_label (c_parser *parser)
5120 location_t loc1 = c_parser_peek_token (parser)->location;
5121 tree label = NULL_TREE;
5123 /* Remember whether this case or a user-defined label is allowed to fall
5124 through to. */
5125 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5127 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5129 tree exp1, exp2;
5130 c_parser_consume_token (parser);
5131 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5132 if (c_parser_next_token_is (parser, CPP_COLON))
5134 c_parser_consume_token (parser);
5135 label = do_case (loc1, exp1, NULL_TREE);
5137 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5139 c_parser_consume_token (parser);
5140 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5141 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5142 label = do_case (loc1, exp1, exp2);
5144 else
5145 c_parser_error (parser, "expected %<:%> or %<...%>");
5147 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5149 c_parser_consume_token (parser);
5150 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5151 label = do_case (loc1, NULL_TREE, NULL_TREE);
5153 else
5155 tree name = c_parser_peek_token (parser)->value;
5156 tree tlab;
5157 tree attrs;
5158 location_t loc2 = c_parser_peek_token (parser)->location;
5159 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5160 c_parser_consume_token (parser);
5161 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5162 c_parser_consume_token (parser);
5163 attrs = c_parser_attributes (parser);
5164 tlab = define_label (loc2, name);
5165 if (tlab)
5167 decl_attributes (&tlab, attrs, 0);
5168 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5171 if (label)
5173 if (TREE_CODE (label) == LABEL_EXPR)
5174 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5175 else
5176 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5178 /* Allow '__attribute__((fallthrough));'. */
5179 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5181 location_t loc = c_parser_peek_token (parser)->location;
5182 tree attrs = c_parser_attributes (parser);
5183 if (attribute_fallthrough_p (attrs))
5185 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5187 tree fn = build_call_expr_internal_loc (loc,
5188 IFN_FALLTHROUGH,
5189 void_type_node, 0);
5190 add_stmt (fn);
5192 else
5193 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5194 "not followed by %<;%>");
5196 else if (attrs != NULL_TREE)
5197 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5198 " can be applied to a null statement");
5200 if (c_parser_next_tokens_start_declaration (parser))
5202 error_at (c_parser_peek_token (parser)->location,
5203 "a label can only be part of a statement and "
5204 "a declaration is not a statement");
5205 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5206 /*static_assert_ok*/ true,
5207 /*empty_ok*/ true, /*nested*/ true,
5208 /*start_attr_ok*/ true, NULL,
5209 vNULL);
5214 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5216 statement:
5217 labeled-statement
5218 compound-statement
5219 expression-statement
5220 selection-statement
5221 iteration-statement
5222 jump-statement
5224 labeled-statement:
5225 label statement
5227 expression-statement:
5228 expression[opt] ;
5230 selection-statement:
5231 if-statement
5232 switch-statement
5234 iteration-statement:
5235 while-statement
5236 do-statement
5237 for-statement
5239 jump-statement:
5240 goto identifier ;
5241 continue ;
5242 break ;
5243 return expression[opt] ;
5245 GNU extensions:
5247 statement:
5248 asm-statement
5250 jump-statement:
5251 goto * expression ;
5253 expression-statement:
5254 attributes ;
5256 Objective-C:
5258 statement:
5259 objc-throw-statement
5260 objc-try-catch-statement
5261 objc-synchronized-statement
5263 objc-throw-statement:
5264 @throw expression ;
5265 @throw ;
5267 OpenACC:
5269 statement:
5270 openacc-construct
5272 openacc-construct:
5273 parallel-construct
5274 kernels-construct
5275 data-construct
5276 loop-construct
5278 parallel-construct:
5279 parallel-directive structured-block
5281 kernels-construct:
5282 kernels-directive structured-block
5284 data-construct:
5285 data-directive structured-block
5287 loop-construct:
5288 loop-directive structured-block
5290 OpenMP:
5292 statement:
5293 openmp-construct
5295 openmp-construct:
5296 parallel-construct
5297 for-construct
5298 simd-construct
5299 for-simd-construct
5300 sections-construct
5301 single-construct
5302 parallel-for-construct
5303 parallel-for-simd-construct
5304 parallel-sections-construct
5305 master-construct
5306 critical-construct
5307 atomic-construct
5308 ordered-construct
5310 parallel-construct:
5311 parallel-directive structured-block
5313 for-construct:
5314 for-directive iteration-statement
5316 simd-construct:
5317 simd-directive iteration-statements
5319 for-simd-construct:
5320 for-simd-directive iteration-statements
5322 sections-construct:
5323 sections-directive section-scope
5325 single-construct:
5326 single-directive structured-block
5328 parallel-for-construct:
5329 parallel-for-directive iteration-statement
5331 parallel-for-simd-construct:
5332 parallel-for-simd-directive iteration-statement
5334 parallel-sections-construct:
5335 parallel-sections-directive section-scope
5337 master-construct:
5338 master-directive structured-block
5340 critical-construct:
5341 critical-directive structured-block
5343 atomic-construct:
5344 atomic-directive expression-statement
5346 ordered-construct:
5347 ordered-directive structured-block
5349 Transactional Memory:
5351 statement:
5352 transaction-statement
5353 transaction-cancel-statement
5355 IF_P is used to track whether there's a (possibly labeled) if statement
5356 which is not enclosed in braces and has an else clause. This is used to
5357 implement -Wparentheses. */
5359 static void
5360 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5362 c_parser_all_labels (parser);
5363 if (loc_after_labels)
5364 *loc_after_labels = c_parser_peek_token (parser)->location;
5365 c_parser_statement_after_labels (parser, if_p, NULL);
5368 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5369 of if-else-if conditions.
5371 IF_P is used to track whether there's a (possibly labeled) if statement
5372 which is not enclosed in braces and has an else clause. This is used to
5373 implement -Wparentheses. */
5375 static void
5376 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5377 vec<tree> *chain)
5379 location_t loc = c_parser_peek_token (parser)->location;
5380 tree stmt = NULL_TREE;
5381 bool in_if_block = parser->in_if_block;
5382 parser->in_if_block = false;
5383 if (if_p != NULL)
5384 *if_p = false;
5386 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5387 add_debug_begin_stmt (loc);
5389 switch (c_parser_peek_token (parser)->type)
5391 case CPP_OPEN_BRACE:
5392 add_stmt (c_parser_compound_statement (parser));
5393 break;
5394 case CPP_KEYWORD:
5395 switch (c_parser_peek_token (parser)->keyword)
5397 case RID_IF:
5398 c_parser_if_statement (parser, if_p, chain);
5399 break;
5400 case RID_SWITCH:
5401 c_parser_switch_statement (parser, if_p);
5402 break;
5403 case RID_WHILE:
5404 c_parser_while_statement (parser, false, 0, if_p);
5405 break;
5406 case RID_DO:
5407 c_parser_do_statement (parser, 0, false);
5408 break;
5409 case RID_FOR:
5410 c_parser_for_statement (parser, false, 0, if_p);
5411 break;
5412 case RID_GOTO:
5413 c_parser_consume_token (parser);
5414 if (c_parser_next_token_is (parser, CPP_NAME))
5416 stmt = c_finish_goto_label (loc,
5417 c_parser_peek_token (parser)->value);
5418 c_parser_consume_token (parser);
5420 else if (c_parser_next_token_is (parser, CPP_MULT))
5422 struct c_expr val;
5424 c_parser_consume_token (parser);
5425 val = c_parser_expression (parser);
5426 val = convert_lvalue_to_rvalue (loc, val, false, true);
5427 stmt = c_finish_goto_ptr (loc, val.value);
5429 else
5430 c_parser_error (parser, "expected identifier or %<*%>");
5431 goto expect_semicolon;
5432 case RID_CONTINUE:
5433 c_parser_consume_token (parser);
5434 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5435 goto expect_semicolon;
5436 case RID_BREAK:
5437 c_parser_consume_token (parser);
5438 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5439 goto expect_semicolon;
5440 case RID_RETURN:
5441 c_parser_consume_token (parser);
5442 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5444 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5445 c_parser_consume_token (parser);
5447 else
5449 location_t xloc = c_parser_peek_token (parser)->location;
5450 struct c_expr expr = c_parser_expression_conv (parser);
5451 mark_exp_read (expr.value);
5452 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5453 expr.value, expr.original_type);
5454 goto expect_semicolon;
5456 break;
5457 case RID_ASM:
5458 stmt = c_parser_asm_statement (parser);
5459 break;
5460 case RID_TRANSACTION_ATOMIC:
5461 case RID_TRANSACTION_RELAXED:
5462 stmt = c_parser_transaction (parser,
5463 c_parser_peek_token (parser)->keyword);
5464 break;
5465 case RID_TRANSACTION_CANCEL:
5466 stmt = c_parser_transaction_cancel (parser);
5467 goto expect_semicolon;
5468 case RID_AT_THROW:
5469 gcc_assert (c_dialect_objc ());
5470 c_parser_consume_token (parser);
5471 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5473 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5474 c_parser_consume_token (parser);
5476 else
5478 struct c_expr expr = c_parser_expression (parser);
5479 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5480 expr.value = c_fully_fold (expr.value, false, NULL);
5481 stmt = objc_build_throw_stmt (loc, expr.value);
5482 goto expect_semicolon;
5484 break;
5485 case RID_AT_TRY:
5486 gcc_assert (c_dialect_objc ());
5487 c_parser_objc_try_catch_finally_statement (parser);
5488 break;
5489 case RID_AT_SYNCHRONIZED:
5490 gcc_assert (c_dialect_objc ());
5491 c_parser_objc_synchronized_statement (parser);
5492 break;
5493 case RID_ATTRIBUTE:
5495 /* Allow '__attribute__((fallthrough));'. */
5496 tree attrs = c_parser_attributes (parser);
5497 if (attribute_fallthrough_p (attrs))
5499 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5501 tree fn = build_call_expr_internal_loc (loc,
5502 IFN_FALLTHROUGH,
5503 void_type_node, 0);
5504 add_stmt (fn);
5505 /* Eat the ';'. */
5506 c_parser_consume_token (parser);
5508 else
5509 warning_at (loc, OPT_Wattributes,
5510 "%<fallthrough%> attribute not followed "
5511 "by %<;%>");
5513 else if (attrs != NULL_TREE)
5514 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5515 " can be applied to a null statement");
5516 break;
5518 default:
5519 goto expr_stmt;
5521 break;
5522 case CPP_SEMICOLON:
5523 c_parser_consume_token (parser);
5524 break;
5525 case CPP_CLOSE_PAREN:
5526 case CPP_CLOSE_SQUARE:
5527 /* Avoid infinite loop in error recovery:
5528 c_parser_skip_until_found stops at a closing nesting
5529 delimiter without consuming it, but here we need to consume
5530 it to proceed further. */
5531 c_parser_error (parser, "expected statement");
5532 c_parser_consume_token (parser);
5533 break;
5534 case CPP_PRAGMA:
5535 c_parser_pragma (parser, pragma_stmt, if_p);
5536 break;
5537 default:
5538 expr_stmt:
5539 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5540 expect_semicolon:
5541 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5542 break;
5544 /* Two cases cannot and do not have line numbers associated: If stmt
5545 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5546 cannot hold line numbers. But that's OK because the statement
5547 will either be changed to a MODIFY_EXPR during gimplification of
5548 the statement expr, or discarded. If stmt was compound, but
5549 without new variables, we will have skipped the creation of a
5550 BIND and will have a bare STATEMENT_LIST. But that's OK because
5551 (recursively) all of the component statements should already have
5552 line numbers assigned. ??? Can we discard no-op statements
5553 earlier? */
5554 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5555 protected_set_expr_location (stmt, loc);
5557 parser->in_if_block = in_if_block;
5560 /* Parse the condition from an if, do, while or for statements. */
5562 static tree
5563 c_parser_condition (c_parser *parser)
5565 location_t loc = c_parser_peek_token (parser)->location;
5566 tree cond;
5567 cond = c_parser_expression_conv (parser).value;
5568 cond = c_objc_common_truthvalue_conversion (loc, cond);
5569 cond = c_fully_fold (cond, false, NULL);
5570 if (warn_sequence_point)
5571 verify_sequence_points (cond);
5572 return cond;
5575 /* Parse a parenthesized condition from an if, do or while statement.
5577 condition:
5578 ( expression )
5580 static tree
5581 c_parser_paren_condition (c_parser *parser)
5583 tree cond;
5584 matching_parens parens;
5585 if (!parens.require_open (parser))
5586 return error_mark_node;
5587 cond = c_parser_condition (parser);
5588 parens.skip_until_found_close (parser);
5589 return cond;
5592 /* Parse a statement which is a block in C99.
5594 IF_P is used to track whether there's a (possibly labeled) if statement
5595 which is not enclosed in braces and has an else clause. This is used to
5596 implement -Wparentheses. */
5598 static tree
5599 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5600 location_t *loc_after_labels)
5602 tree block = c_begin_compound_stmt (flag_isoc99);
5603 location_t loc = c_parser_peek_token (parser)->location;
5604 c_parser_statement (parser, if_p, loc_after_labels);
5605 return c_end_compound_stmt (loc, block, flag_isoc99);
5608 /* Parse the body of an if statement. This is just parsing a
5609 statement but (a) it is a block in C99, (b) we track whether the
5610 body is an if statement for the sake of -Wparentheses warnings, (c)
5611 we handle an empty body specially for the sake of -Wempty-body
5612 warnings, and (d) we call parser_compound_statement directly
5613 because c_parser_statement_after_labels resets
5614 parser->in_if_block.
5616 IF_P is used to track whether there's a (possibly labeled) if statement
5617 which is not enclosed in braces and has an else clause. This is used to
5618 implement -Wparentheses. */
5620 static tree
5621 c_parser_if_body (c_parser *parser, bool *if_p,
5622 const token_indent_info &if_tinfo)
5624 tree block = c_begin_compound_stmt (flag_isoc99);
5625 location_t body_loc = c_parser_peek_token (parser)->location;
5626 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5627 token_indent_info body_tinfo
5628 = get_token_indent_info (c_parser_peek_token (parser));
5630 c_parser_all_labels (parser);
5631 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5633 location_t loc = c_parser_peek_token (parser)->location;
5634 add_stmt (build_empty_stmt (loc));
5635 c_parser_consume_token (parser);
5636 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5637 warning_at (loc, OPT_Wempty_body,
5638 "suggest braces around empty body in an %<if%> statement");
5640 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5641 add_stmt (c_parser_compound_statement (parser));
5642 else
5644 body_loc_after_labels = c_parser_peek_token (parser)->location;
5645 c_parser_statement_after_labels (parser, if_p);
5648 token_indent_info next_tinfo
5649 = get_token_indent_info (c_parser_peek_token (parser));
5650 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5651 if (body_loc_after_labels != UNKNOWN_LOCATION
5652 && next_tinfo.type != CPP_SEMICOLON)
5653 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5654 if_tinfo.location, RID_IF);
5656 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5659 /* Parse the else body of an if statement. This is just parsing a
5660 statement but (a) it is a block in C99, (b) we handle an empty body
5661 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5662 of if-else-if conditions. */
5664 static tree
5665 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5666 vec<tree> *chain)
5668 location_t body_loc = c_parser_peek_token (parser)->location;
5669 tree block = c_begin_compound_stmt (flag_isoc99);
5670 token_indent_info body_tinfo
5671 = get_token_indent_info (c_parser_peek_token (parser));
5672 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5674 c_parser_all_labels (parser);
5675 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5677 location_t loc = c_parser_peek_token (parser)->location;
5678 warning_at (loc,
5679 OPT_Wempty_body,
5680 "suggest braces around empty body in an %<else%> statement");
5681 add_stmt (build_empty_stmt (loc));
5682 c_parser_consume_token (parser);
5684 else
5686 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5687 body_loc_after_labels = c_parser_peek_token (parser)->location;
5688 c_parser_statement_after_labels (parser, NULL, chain);
5691 token_indent_info next_tinfo
5692 = get_token_indent_info (c_parser_peek_token (parser));
5693 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5694 if (body_loc_after_labels != UNKNOWN_LOCATION
5695 && next_tinfo.type != CPP_SEMICOLON)
5696 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5697 else_tinfo.location, RID_ELSE);
5699 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5702 /* We might need to reclassify any previously-lexed identifier, e.g.
5703 when we've left a for loop with an if-statement without else in the
5704 body - we might have used a wrong scope for the token. See PR67784. */
5706 static void
5707 c_parser_maybe_reclassify_token (c_parser *parser)
5709 if (c_parser_next_token_is (parser, CPP_NAME))
5711 c_token *token = c_parser_peek_token (parser);
5713 if (token->id_kind != C_ID_CLASSNAME)
5715 tree decl = lookup_name (token->value);
5717 token->id_kind = C_ID_ID;
5718 if (decl)
5720 if (TREE_CODE (decl) == TYPE_DECL)
5721 token->id_kind = C_ID_TYPENAME;
5723 else if (c_dialect_objc ())
5725 tree objc_interface_decl = objc_is_class_name (token->value);
5726 /* Objective-C class names are in the same namespace as
5727 variables and typedefs, and hence are shadowed by local
5728 declarations. */
5729 if (objc_interface_decl)
5731 token->value = objc_interface_decl;
5732 token->id_kind = C_ID_CLASSNAME;
5739 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5741 if-statement:
5742 if ( expression ) statement
5743 if ( expression ) statement else statement
5745 CHAIN is a vector of if-else-if conditions.
5746 IF_P is used to track whether there's a (possibly labeled) if statement
5747 which is not enclosed in braces and has an else clause. This is used to
5748 implement -Wparentheses. */
5750 static void
5751 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5753 tree block;
5754 location_t loc;
5755 tree cond;
5756 bool nested_if = false;
5757 tree first_body, second_body;
5758 bool in_if_block;
5760 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5761 token_indent_info if_tinfo
5762 = get_token_indent_info (c_parser_peek_token (parser));
5763 c_parser_consume_token (parser);
5764 block = c_begin_compound_stmt (flag_isoc99);
5765 loc = c_parser_peek_token (parser)->location;
5766 cond = c_parser_paren_condition (parser);
5767 in_if_block = parser->in_if_block;
5768 parser->in_if_block = true;
5769 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5770 parser->in_if_block = in_if_block;
5772 if (warn_duplicated_cond)
5773 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5775 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5777 token_indent_info else_tinfo
5778 = get_token_indent_info (c_parser_peek_token (parser));
5779 c_parser_consume_token (parser);
5780 if (warn_duplicated_cond)
5782 if (c_parser_next_token_is_keyword (parser, RID_IF)
5783 && chain == NULL)
5785 /* We've got "if (COND) else if (COND2)". Start the
5786 condition chain and add COND as the first element. */
5787 chain = new vec<tree> ();
5788 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5789 chain->safe_push (cond);
5791 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5793 /* This is if-else without subsequent if. Zap the condition
5794 chain; we would have already warned at this point. */
5795 delete chain;
5796 chain = NULL;
5799 second_body = c_parser_else_body (parser, else_tinfo, chain);
5800 /* Set IF_P to true to indicate that this if statement has an
5801 else clause. This may trigger the Wparentheses warning
5802 below when we get back up to the parent if statement. */
5803 if (if_p != NULL)
5804 *if_p = true;
5806 else
5808 second_body = NULL_TREE;
5810 /* Diagnose an ambiguous else if if-then-else is nested inside
5811 if-then. */
5812 if (nested_if)
5813 warning_at (loc, OPT_Wdangling_else,
5814 "suggest explicit braces to avoid ambiguous %<else%>");
5816 if (warn_duplicated_cond)
5818 /* This if statement does not have an else clause. We don't
5819 need the condition chain anymore. */
5820 delete chain;
5821 chain = NULL;
5824 c_finish_if_stmt (loc, cond, first_body, second_body);
5825 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5827 c_parser_maybe_reclassify_token (parser);
5830 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5832 switch-statement:
5833 switch (expression) statement
5836 static void
5837 c_parser_switch_statement (c_parser *parser, bool *if_p)
5839 struct c_expr ce;
5840 tree block, expr, body, save_break;
5841 location_t switch_loc = c_parser_peek_token (parser)->location;
5842 location_t switch_cond_loc;
5843 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5844 c_parser_consume_token (parser);
5845 block = c_begin_compound_stmt (flag_isoc99);
5846 bool explicit_cast_p = false;
5847 matching_parens parens;
5848 if (parens.require_open (parser))
5850 switch_cond_loc = c_parser_peek_token (parser)->location;
5851 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5852 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5853 explicit_cast_p = true;
5854 ce = c_parser_expression (parser);
5855 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5856 expr = ce.value;
5857 /* ??? expr has no valid location? */
5858 parens.skip_until_found_close (parser);
5860 else
5862 switch_cond_loc = UNKNOWN_LOCATION;
5863 expr = error_mark_node;
5864 ce.original_type = error_mark_node;
5866 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5867 save_break = c_break_label;
5868 c_break_label = NULL_TREE;
5869 location_t loc_after_labels;
5870 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5871 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5872 location_t next_loc = c_parser_peek_token (parser)->location;
5873 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5874 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5875 RID_SWITCH);
5876 if (c_break_label)
5878 location_t here = c_parser_peek_token (parser)->location;
5879 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5880 SET_EXPR_LOCATION (t, here);
5881 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5882 append_to_statement_list_force (t, &body);
5884 c_finish_case (body, ce.original_type);
5885 c_break_label = save_break;
5886 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5887 c_parser_maybe_reclassify_token (parser);
5890 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5892 while-statement:
5893 while (expression) statement
5895 IF_P is used to track whether there's a (possibly labeled) if statement
5896 which is not enclosed in braces and has an else clause. This is used to
5897 implement -Wparentheses. */
5899 static void
5900 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5901 bool *if_p)
5903 tree block, cond, body, save_break, save_cont;
5904 location_t loc;
5905 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5906 token_indent_info while_tinfo
5907 = get_token_indent_info (c_parser_peek_token (parser));
5908 c_parser_consume_token (parser);
5909 block = c_begin_compound_stmt (flag_isoc99);
5910 loc = c_parser_peek_token (parser)->location;
5911 cond = c_parser_paren_condition (parser);
5912 if (ivdep && cond != error_mark_node)
5913 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5914 build_int_cst (integer_type_node,
5915 annot_expr_ivdep_kind),
5916 integer_zero_node);
5917 if (unroll && cond != error_mark_node)
5918 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5919 build_int_cst (integer_type_node,
5920 annot_expr_unroll_kind),
5921 build_int_cst (integer_type_node, unroll));
5922 save_break = c_break_label;
5923 c_break_label = NULL_TREE;
5924 save_cont = c_cont_label;
5925 c_cont_label = NULL_TREE;
5927 token_indent_info body_tinfo
5928 = get_token_indent_info (c_parser_peek_token (parser));
5930 location_t loc_after_labels;
5931 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5932 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5933 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5934 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5935 c_parser_maybe_reclassify_token (parser);
5937 token_indent_info next_tinfo
5938 = get_token_indent_info (c_parser_peek_token (parser));
5939 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5941 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5942 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5943 while_tinfo.location, RID_WHILE);
5945 c_break_label = save_break;
5946 c_cont_label = save_cont;
5949 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5951 do-statement:
5952 do statement while ( expression ) ;
5955 static void
5956 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
5958 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5959 location_t loc;
5960 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5961 c_parser_consume_token (parser);
5962 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5963 warning_at (c_parser_peek_token (parser)->location,
5964 OPT_Wempty_body,
5965 "suggest braces around empty body in %<do%> statement");
5966 block = c_begin_compound_stmt (flag_isoc99);
5967 loc = c_parser_peek_token (parser)->location;
5968 save_break = c_break_label;
5969 c_break_label = NULL_TREE;
5970 save_cont = c_cont_label;
5971 c_cont_label = NULL_TREE;
5972 body = c_parser_c99_block_statement (parser, NULL);
5973 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5974 new_break = c_break_label;
5975 c_break_label = save_break;
5976 new_cont = c_cont_label;
5977 c_cont_label = save_cont;
5978 cond = c_parser_paren_condition (parser);
5979 if (ivdep && cond != error_mark_node)
5980 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5981 build_int_cst (integer_type_node,
5982 annot_expr_ivdep_kind),
5983 integer_zero_node);
5984 if (unroll && cond != error_mark_node)
5985 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5986 build_int_cst (integer_type_node,
5987 annot_expr_unroll_kind),
5988 build_int_cst (integer_type_node, unroll));
5989 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5990 c_parser_skip_to_end_of_block_or_statement (parser);
5991 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5992 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5995 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5997 for-statement:
5998 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5999 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6001 The form with a declaration is new in C99.
6003 ??? In accordance with the old parser, the declaration may be a
6004 nested function, which is then rejected in check_for_loop_decls,
6005 but does it make any sense for this to be included in the grammar?
6006 Note in particular that the nested function does not include a
6007 trailing ';', whereas the "declaration" production includes one.
6008 Also, can we reject bad declarations earlier and cheaper than
6009 check_for_loop_decls?
6011 In Objective-C, there are two additional variants:
6013 foreach-statement:
6014 for ( expression in expresssion ) statement
6015 for ( declaration in expression ) statement
6017 This is inconsistent with C, because the second variant is allowed
6018 even if c99 is not enabled.
6020 The rest of the comment documents these Objective-C foreach-statement.
6022 Here is the canonical example of the first variant:
6023 for (object in array) { do something with object }
6024 we call the first expression ("object") the "object_expression" and
6025 the second expression ("array") the "collection_expression".
6026 object_expression must be an lvalue of type "id" (a generic Objective-C
6027 object) because the loop works by assigning to object_expression the
6028 various objects from the collection_expression. collection_expression
6029 must evaluate to something of type "id" which responds to the method
6030 countByEnumeratingWithState:objects:count:.
6032 The canonical example of the second variant is:
6033 for (id object in array) { do something with object }
6034 which is completely equivalent to
6036 id object;
6037 for (object in array) { do something with object }
6039 Note that initizializing 'object' in some way (eg, "for ((object =
6040 xxx) in array) { do something with object }") is possibly
6041 technically valid, but completely pointless as 'object' will be
6042 assigned to something else as soon as the loop starts. We should
6043 most likely reject it (TODO).
6045 The beginning of the Objective-C foreach-statement looks exactly
6046 like the beginning of the for-statement, and we can tell it is a
6047 foreach-statement only because the initial declaration or
6048 expression is terminated by 'in' instead of ';'.
6050 IF_P is used to track whether there's a (possibly labeled) if statement
6051 which is not enclosed in braces and has an else clause. This is used to
6052 implement -Wparentheses. */
6054 static void
6055 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6056 bool *if_p)
6058 tree block, cond, incr, save_break, save_cont, body;
6059 /* The following are only used when parsing an ObjC foreach statement. */
6060 tree object_expression;
6061 /* Silence the bogus uninitialized warning. */
6062 tree collection_expression = NULL;
6063 location_t loc = c_parser_peek_token (parser)->location;
6064 location_t for_loc = c_parser_peek_token (parser)->location;
6065 bool is_foreach_statement = false;
6066 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6067 token_indent_info for_tinfo
6068 = get_token_indent_info (c_parser_peek_token (parser));
6069 c_parser_consume_token (parser);
6070 /* Open a compound statement in Objective-C as well, just in case this is
6071 as foreach expression. */
6072 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6073 cond = error_mark_node;
6074 incr = error_mark_node;
6075 matching_parens parens;
6076 if (parens.require_open (parser))
6078 /* Parse the initialization declaration or expression. */
6079 object_expression = error_mark_node;
6080 parser->objc_could_be_foreach_context = c_dialect_objc ();
6081 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6083 parser->objc_could_be_foreach_context = false;
6084 c_parser_consume_token (parser);
6085 c_finish_expr_stmt (loc, NULL_TREE);
6087 else if (c_parser_next_tokens_start_declaration (parser))
6089 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6090 &object_expression, vNULL);
6091 parser->objc_could_be_foreach_context = false;
6093 if (c_parser_next_token_is_keyword (parser, RID_IN))
6095 c_parser_consume_token (parser);
6096 is_foreach_statement = true;
6097 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6098 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6100 else
6101 check_for_loop_decls (for_loc, flag_isoc99);
6103 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6105 /* __extension__ can start a declaration, but is also an
6106 unary operator that can start an expression. Consume all
6107 but the last of a possible series of __extension__ to
6108 determine which. */
6109 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6110 && (c_parser_peek_2nd_token (parser)->keyword
6111 == RID_EXTENSION))
6112 c_parser_consume_token (parser);
6113 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6115 int ext;
6116 ext = disable_extension_diagnostics ();
6117 c_parser_consume_token (parser);
6118 c_parser_declaration_or_fndef (parser, true, true, true, true,
6119 true, &object_expression, vNULL);
6120 parser->objc_could_be_foreach_context = false;
6122 restore_extension_diagnostics (ext);
6123 if (c_parser_next_token_is_keyword (parser, RID_IN))
6125 c_parser_consume_token (parser);
6126 is_foreach_statement = true;
6127 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6128 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6130 else
6131 check_for_loop_decls (for_loc, flag_isoc99);
6133 else
6134 goto init_expr;
6136 else
6138 init_expr:
6140 struct c_expr ce;
6141 tree init_expression;
6142 ce = c_parser_expression (parser);
6143 init_expression = ce.value;
6144 parser->objc_could_be_foreach_context = false;
6145 if (c_parser_next_token_is_keyword (parser, RID_IN))
6147 c_parser_consume_token (parser);
6148 is_foreach_statement = true;
6149 if (! lvalue_p (init_expression))
6150 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6151 object_expression = c_fully_fold (init_expression, false, NULL);
6153 else
6155 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6156 init_expression = ce.value;
6157 c_finish_expr_stmt (loc, init_expression);
6158 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6162 /* Parse the loop condition. In the case of a foreach
6163 statement, there is no loop condition. */
6164 gcc_assert (!parser->objc_could_be_foreach_context);
6165 if (!is_foreach_statement)
6167 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6169 if (ivdep)
6171 c_parser_error (parser, "missing loop condition in loop with "
6172 "%<GCC ivdep%> pragma");
6173 cond = error_mark_node;
6175 else if (unroll)
6177 c_parser_error (parser, "missing loop condition in loop with "
6178 "%<GCC unroll%> pragma");
6179 cond = error_mark_node;
6181 else
6183 c_parser_consume_token (parser);
6184 cond = NULL_TREE;
6187 else
6189 cond = c_parser_condition (parser);
6190 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6191 "expected %<;%>");
6193 if (ivdep && cond != error_mark_node)
6194 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6195 build_int_cst (integer_type_node,
6196 annot_expr_ivdep_kind),
6197 integer_zero_node);
6198 if (unroll && cond != error_mark_node)
6199 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6200 build_int_cst (integer_type_node,
6201 annot_expr_unroll_kind),
6202 build_int_cst (integer_type_node, unroll));
6204 /* Parse the increment expression (the third expression in a
6205 for-statement). In the case of a foreach-statement, this is
6206 the expression that follows the 'in'. */
6207 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6209 if (is_foreach_statement)
6211 c_parser_error (parser, "missing collection in fast enumeration");
6212 collection_expression = error_mark_node;
6214 else
6215 incr = c_process_expr_stmt (loc, NULL_TREE);
6217 else
6219 if (is_foreach_statement)
6220 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6221 false, NULL);
6222 else
6224 struct c_expr ce = c_parser_expression (parser);
6225 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6226 incr = c_process_expr_stmt (loc, ce.value);
6229 parens.skip_until_found_close (parser);
6231 save_break = c_break_label;
6232 c_break_label = NULL_TREE;
6233 save_cont = c_cont_label;
6234 c_cont_label = NULL_TREE;
6236 token_indent_info body_tinfo
6237 = get_token_indent_info (c_parser_peek_token (parser));
6239 location_t loc_after_labels;
6240 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6241 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6243 if (is_foreach_statement)
6244 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6245 else
6246 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6247 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6248 c_parser_maybe_reclassify_token (parser);
6250 token_indent_info next_tinfo
6251 = get_token_indent_info (c_parser_peek_token (parser));
6252 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6254 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6255 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6256 for_tinfo.location, RID_FOR);
6258 c_break_label = save_break;
6259 c_cont_label = save_cont;
6262 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6263 statement with inputs, outputs, clobbers, and volatile tag
6264 allowed.
6266 asm-statement:
6267 asm type-qualifier[opt] ( asm-argument ) ;
6268 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6270 asm-argument:
6271 asm-string-literal
6272 asm-string-literal : asm-operands[opt]
6273 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6274 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6276 asm-goto-argument:
6277 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6278 : asm-goto-operands
6280 Qualifiers other than volatile are accepted in the syntax but
6281 warned for. */
6283 static tree
6284 c_parser_asm_statement (c_parser *parser)
6286 tree quals, str, outputs, inputs, clobbers, labels, ret;
6287 bool simple, is_goto;
6288 location_t asm_loc = c_parser_peek_token (parser)->location;
6289 int section, nsections;
6291 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6292 c_parser_consume_token (parser);
6293 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6295 quals = c_parser_peek_token (parser)->value;
6296 c_parser_consume_token (parser);
6298 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6299 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6301 warning_at (c_parser_peek_token (parser)->location,
6303 "%E qualifier ignored on asm",
6304 c_parser_peek_token (parser)->value);
6305 quals = NULL_TREE;
6306 c_parser_consume_token (parser);
6308 else
6309 quals = NULL_TREE;
6311 is_goto = false;
6312 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6314 c_parser_consume_token (parser);
6315 is_goto = true;
6318 /* ??? Follow the C++ parser rather than using the
6319 lex_untranslated_string kludge. */
6320 parser->lex_untranslated_string = true;
6321 ret = NULL;
6323 matching_parens parens;
6324 if (!parens.require_open (parser))
6325 goto error;
6327 str = c_parser_asm_string_literal (parser);
6328 if (str == NULL_TREE)
6329 goto error_close_paren;
6331 simple = true;
6332 outputs = NULL_TREE;
6333 inputs = NULL_TREE;
6334 clobbers = NULL_TREE;
6335 labels = NULL_TREE;
6337 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6338 goto done_asm;
6340 /* Parse each colon-delimited section of operands. */
6341 nsections = 3 + is_goto;
6342 for (section = 0; section < nsections; ++section)
6344 if (!c_parser_require (parser, CPP_COLON,
6345 is_goto
6346 ? G_("expected %<:%>")
6347 : G_("expected %<:%> or %<)%>"),
6348 UNKNOWN_LOCATION, is_goto))
6349 goto error_close_paren;
6351 /* Once past any colon, we're no longer a simple asm. */
6352 simple = false;
6354 if ((!c_parser_next_token_is (parser, CPP_COLON)
6355 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6356 || section == 3)
6357 switch (section)
6359 case 0:
6360 /* For asm goto, we don't allow output operands, but reserve
6361 the slot for a future extension that does allow them. */
6362 if (!is_goto)
6363 outputs = c_parser_asm_operands (parser);
6364 break;
6365 case 1:
6366 inputs = c_parser_asm_operands (parser);
6367 break;
6368 case 2:
6369 clobbers = c_parser_asm_clobbers (parser);
6370 break;
6371 case 3:
6372 labels = c_parser_asm_goto_operands (parser);
6373 break;
6374 default:
6375 gcc_unreachable ();
6378 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6379 goto done_asm;
6382 done_asm:
6383 if (!parens.require_close (parser))
6385 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6386 goto error;
6389 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6390 c_parser_skip_to_end_of_block_or_statement (parser);
6392 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6393 clobbers, labels, simple));
6395 error:
6396 parser->lex_untranslated_string = false;
6397 return ret;
6399 error_close_paren:
6400 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6401 goto error;
6404 /* Parse asm operands, a GNU extension.
6406 asm-operands:
6407 asm-operand
6408 asm-operands , asm-operand
6410 asm-operand:
6411 asm-string-literal ( expression )
6412 [ identifier ] asm-string-literal ( expression )
6415 static tree
6416 c_parser_asm_operands (c_parser *parser)
6418 tree list = NULL_TREE;
6419 while (true)
6421 tree name, str;
6422 struct c_expr expr;
6423 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6425 c_parser_consume_token (parser);
6426 if (c_parser_next_token_is (parser, CPP_NAME))
6428 tree id = c_parser_peek_token (parser)->value;
6429 c_parser_consume_token (parser);
6430 name = build_string (IDENTIFIER_LENGTH (id),
6431 IDENTIFIER_POINTER (id));
6433 else
6435 c_parser_error (parser, "expected identifier");
6436 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6437 return NULL_TREE;
6439 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6440 "expected %<]%>");
6442 else
6443 name = NULL_TREE;
6444 str = c_parser_asm_string_literal (parser);
6445 if (str == NULL_TREE)
6446 return NULL_TREE;
6447 parser->lex_untranslated_string = false;
6448 matching_parens parens;
6449 if (!parens.require_open (parser))
6451 parser->lex_untranslated_string = true;
6452 return NULL_TREE;
6454 expr = c_parser_expression (parser);
6455 mark_exp_read (expr.value);
6456 parser->lex_untranslated_string = true;
6457 if (!parens.require_close (parser))
6459 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6460 return NULL_TREE;
6462 list = chainon (list, build_tree_list (build_tree_list (name, str),
6463 expr.value));
6464 if (c_parser_next_token_is (parser, CPP_COMMA))
6465 c_parser_consume_token (parser);
6466 else
6467 break;
6469 return list;
6472 /* Parse asm clobbers, a GNU extension.
6474 asm-clobbers:
6475 asm-string-literal
6476 asm-clobbers , asm-string-literal
6479 static tree
6480 c_parser_asm_clobbers (c_parser *parser)
6482 tree list = NULL_TREE;
6483 while (true)
6485 tree str = c_parser_asm_string_literal (parser);
6486 if (str)
6487 list = tree_cons (NULL_TREE, str, list);
6488 else
6489 return NULL_TREE;
6490 if (c_parser_next_token_is (parser, CPP_COMMA))
6491 c_parser_consume_token (parser);
6492 else
6493 break;
6495 return list;
6498 /* Parse asm goto labels, a GNU extension.
6500 asm-goto-operands:
6501 identifier
6502 asm-goto-operands , identifier
6505 static tree
6506 c_parser_asm_goto_operands (c_parser *parser)
6508 tree list = NULL_TREE;
6509 while (true)
6511 tree name, label;
6513 if (c_parser_next_token_is (parser, CPP_NAME))
6515 c_token *tok = c_parser_peek_token (parser);
6516 name = tok->value;
6517 label = lookup_label_for_goto (tok->location, name);
6518 c_parser_consume_token (parser);
6519 TREE_USED (label) = 1;
6521 else
6523 c_parser_error (parser, "expected identifier");
6524 return NULL_TREE;
6527 name = build_string (IDENTIFIER_LENGTH (name),
6528 IDENTIFIER_POINTER (name));
6529 list = tree_cons (name, label, list);
6530 if (c_parser_next_token_is (parser, CPP_COMMA))
6531 c_parser_consume_token (parser);
6532 else
6533 return nreverse (list);
6537 /* Parse an expression other than a compound expression; that is, an
6538 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6539 AFTER is not NULL then it is an Objective-C message expression which
6540 is the primary-expression starting the expression as an initializer.
6542 assignment-expression:
6543 conditional-expression
6544 unary-expression assignment-operator assignment-expression
6546 assignment-operator: one of
6547 = *= /= %= += -= <<= >>= &= ^= |=
6549 In GNU C we accept any conditional expression on the LHS and
6550 diagnose the invalid lvalue rather than producing a syntax
6551 error. */
6553 static struct c_expr
6554 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6555 tree omp_atomic_lhs)
6557 struct c_expr lhs, rhs, ret;
6558 enum tree_code code;
6559 location_t op_location, exp_location;
6560 gcc_assert (!after || c_dialect_objc ());
6561 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6562 op_location = c_parser_peek_token (parser)->location;
6563 switch (c_parser_peek_token (parser)->type)
6565 case CPP_EQ:
6566 code = NOP_EXPR;
6567 break;
6568 case CPP_MULT_EQ:
6569 code = MULT_EXPR;
6570 break;
6571 case CPP_DIV_EQ:
6572 code = TRUNC_DIV_EXPR;
6573 break;
6574 case CPP_MOD_EQ:
6575 code = TRUNC_MOD_EXPR;
6576 break;
6577 case CPP_PLUS_EQ:
6578 code = PLUS_EXPR;
6579 break;
6580 case CPP_MINUS_EQ:
6581 code = MINUS_EXPR;
6582 break;
6583 case CPP_LSHIFT_EQ:
6584 code = LSHIFT_EXPR;
6585 break;
6586 case CPP_RSHIFT_EQ:
6587 code = RSHIFT_EXPR;
6588 break;
6589 case CPP_AND_EQ:
6590 code = BIT_AND_EXPR;
6591 break;
6592 case CPP_XOR_EQ:
6593 code = BIT_XOR_EXPR;
6594 break;
6595 case CPP_OR_EQ:
6596 code = BIT_IOR_EXPR;
6597 break;
6598 default:
6599 return lhs;
6601 c_parser_consume_token (parser);
6602 exp_location = c_parser_peek_token (parser)->location;
6603 rhs = c_parser_expr_no_commas (parser, NULL);
6604 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6606 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6607 code, exp_location, rhs.value,
6608 rhs.original_type);
6609 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6610 if (code == NOP_EXPR)
6611 ret.original_code = MODIFY_EXPR;
6612 else
6614 TREE_NO_WARNING (ret.value) = 1;
6615 ret.original_code = ERROR_MARK;
6617 ret.original_type = NULL;
6618 return ret;
6621 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6622 AFTER is not NULL then it is an Objective-C message expression which is
6623 the primary-expression starting the expression as an initializer.
6625 conditional-expression:
6626 logical-OR-expression
6627 logical-OR-expression ? expression : conditional-expression
6629 GNU extensions:
6631 conditional-expression:
6632 logical-OR-expression ? : conditional-expression
6635 static struct c_expr
6636 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6637 tree omp_atomic_lhs)
6639 struct c_expr cond, exp1, exp2, ret;
6640 location_t start, cond_loc, colon_loc;
6642 gcc_assert (!after || c_dialect_objc ());
6644 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6646 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6647 return cond;
6648 if (cond.value != error_mark_node)
6649 start = cond.get_start ();
6650 else
6651 start = UNKNOWN_LOCATION;
6652 cond_loc = c_parser_peek_token (parser)->location;
6653 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6654 c_parser_consume_token (parser);
6655 if (c_parser_next_token_is (parser, CPP_COLON))
6657 tree eptype = NULL_TREE;
6659 location_t middle_loc = c_parser_peek_token (parser)->location;
6660 pedwarn (middle_loc, OPT_Wpedantic,
6661 "ISO C forbids omitting the middle term of a ?: expression");
6662 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6664 eptype = TREE_TYPE (cond.value);
6665 cond.value = TREE_OPERAND (cond.value, 0);
6667 tree e = cond.value;
6668 while (TREE_CODE (e) == COMPOUND_EXPR)
6669 e = TREE_OPERAND (e, 1);
6670 warn_for_omitted_condop (middle_loc, e);
6671 /* Make sure first operand is calculated only once. */
6672 exp1.value = save_expr (default_conversion (cond.value));
6673 if (eptype)
6674 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6675 exp1.original_type = NULL;
6676 exp1.src_range = cond.src_range;
6677 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6678 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6680 else
6682 cond.value
6683 = c_objc_common_truthvalue_conversion
6684 (cond_loc, default_conversion (cond.value));
6685 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6686 exp1 = c_parser_expression_conv (parser);
6687 mark_exp_read (exp1.value);
6688 c_inhibit_evaluation_warnings +=
6689 ((cond.value == truthvalue_true_node)
6690 - (cond.value == truthvalue_false_node));
6693 colon_loc = c_parser_peek_token (parser)->location;
6694 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6696 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6697 ret.set_error ();
6698 ret.original_code = ERROR_MARK;
6699 ret.original_type = NULL;
6700 return ret;
6703 location_t exp2_loc = c_parser_peek_token (parser)->location;
6704 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6705 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6707 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6708 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6709 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6710 ret.value = build_conditional_expr (colon_loc, cond.value,
6711 cond.original_code == C_MAYBE_CONST_EXPR,
6712 exp1.value, exp1.original_type, loc1,
6713 exp2.value, exp2.original_type, loc2);
6714 ret.original_code = ERROR_MARK;
6715 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6716 ret.original_type = NULL;
6717 else
6719 tree t1, t2;
6721 /* If both sides are enum type, the default conversion will have
6722 made the type of the result be an integer type. We want to
6723 remember the enum types we started with. */
6724 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6725 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6726 ret.original_type = ((t1 != error_mark_node
6727 && t2 != error_mark_node
6728 && (TYPE_MAIN_VARIANT (t1)
6729 == TYPE_MAIN_VARIANT (t2)))
6730 ? t1
6731 : NULL);
6733 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6734 return ret;
6737 /* Parse a binary expression; that is, a logical-OR-expression (C90
6738 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6739 NULL then it is an Objective-C message expression which is the
6740 primary-expression starting the expression as an initializer.
6742 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6743 when it should be the unfolded lhs. In a valid OpenMP source,
6744 one of the operands of the toplevel binary expression must be equal
6745 to it. In that case, just return a build2 created binary operation
6746 rather than result of parser_build_binary_op.
6748 multiplicative-expression:
6749 cast-expression
6750 multiplicative-expression * cast-expression
6751 multiplicative-expression / cast-expression
6752 multiplicative-expression % cast-expression
6754 additive-expression:
6755 multiplicative-expression
6756 additive-expression + multiplicative-expression
6757 additive-expression - multiplicative-expression
6759 shift-expression:
6760 additive-expression
6761 shift-expression << additive-expression
6762 shift-expression >> additive-expression
6764 relational-expression:
6765 shift-expression
6766 relational-expression < shift-expression
6767 relational-expression > shift-expression
6768 relational-expression <= shift-expression
6769 relational-expression >= shift-expression
6771 equality-expression:
6772 relational-expression
6773 equality-expression == relational-expression
6774 equality-expression != relational-expression
6776 AND-expression:
6777 equality-expression
6778 AND-expression & equality-expression
6780 exclusive-OR-expression:
6781 AND-expression
6782 exclusive-OR-expression ^ AND-expression
6784 inclusive-OR-expression:
6785 exclusive-OR-expression
6786 inclusive-OR-expression | exclusive-OR-expression
6788 logical-AND-expression:
6789 inclusive-OR-expression
6790 logical-AND-expression && inclusive-OR-expression
6792 logical-OR-expression:
6793 logical-AND-expression
6794 logical-OR-expression || logical-AND-expression
6797 static struct c_expr
6798 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6799 tree omp_atomic_lhs)
6801 /* A binary expression is parsed using operator-precedence parsing,
6802 with the operands being cast expressions. All the binary
6803 operators are left-associative. Thus a binary expression is of
6804 form:
6806 E0 op1 E1 op2 E2 ...
6808 which we represent on a stack. On the stack, the precedence
6809 levels are strictly increasing. When a new operator is
6810 encountered of higher precedence than that at the top of the
6811 stack, it is pushed; its LHS is the top expression, and its RHS
6812 is everything parsed until it is popped. When a new operator is
6813 encountered with precedence less than or equal to that at the top
6814 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6815 by the result of the operation until the operator at the top of
6816 the stack has lower precedence than the new operator or there is
6817 only one element on the stack; then the top expression is the LHS
6818 of the new operator. In the case of logical AND and OR
6819 expressions, we also need to adjust c_inhibit_evaluation_warnings
6820 as appropriate when the operators are pushed and popped. */
6822 struct {
6823 /* The expression at this stack level. */
6824 struct c_expr expr;
6825 /* The precedence of the operator on its left, PREC_NONE at the
6826 bottom of the stack. */
6827 enum c_parser_prec prec;
6828 /* The operation on its left. */
6829 enum tree_code op;
6830 /* The source location of this operation. */
6831 location_t loc;
6832 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6833 tree sizeof_arg;
6834 } stack[NUM_PRECS];
6835 int sp;
6836 /* Location of the binary operator. */
6837 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6838 #define POP \
6839 do { \
6840 switch (stack[sp].op) \
6842 case TRUTH_ANDIF_EXPR: \
6843 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6844 == truthvalue_false_node); \
6845 break; \
6846 case TRUTH_ORIF_EXPR: \
6847 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6848 == truthvalue_true_node); \
6849 break; \
6850 case TRUNC_DIV_EXPR: \
6851 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6852 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6854 tree type0 = stack[sp - 1].sizeof_arg; \
6855 tree type1 = stack[sp].sizeof_arg; \
6856 tree first_arg = type0; \
6857 if (!TYPE_P (type0)) \
6858 type0 = TREE_TYPE (type0); \
6859 if (!TYPE_P (type1)) \
6860 type1 = TREE_TYPE (type1); \
6861 if (POINTER_TYPE_P (type0) \
6862 && comptypes (TREE_TYPE (type0), type1) \
6863 && !(TREE_CODE (first_arg) == PARM_DECL \
6864 && C_ARRAY_PARAMETER (first_arg) \
6865 && warn_sizeof_array_argument)) \
6866 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6867 "division %<sizeof (%T) / sizeof (%T)%> does " \
6868 "not compute the number of array elements", \
6869 type0, type1)) \
6870 if (DECL_P (first_arg)) \
6871 inform (DECL_SOURCE_LOCATION (first_arg), \
6872 "first %<sizeof%> operand was declared here"); \
6874 break; \
6875 default: \
6876 break; \
6878 stack[sp - 1].expr \
6879 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6880 stack[sp - 1].expr, true, true); \
6881 stack[sp].expr \
6882 = convert_lvalue_to_rvalue (stack[sp].loc, \
6883 stack[sp].expr, true, true); \
6884 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6885 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6886 && ((1 << stack[sp].prec) \
6887 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6888 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6889 && stack[sp].op != TRUNC_MOD_EXPR \
6890 && stack[0].expr.value != error_mark_node \
6891 && stack[1].expr.value != error_mark_node \
6892 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6893 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6894 stack[0].expr.value \
6895 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6896 stack[0].expr.value, stack[1].expr.value); \
6897 else \
6898 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6899 stack[sp].op, \
6900 stack[sp - 1].expr, \
6901 stack[sp].expr); \
6902 sp--; \
6903 } while (0)
6904 gcc_assert (!after || c_dialect_objc ());
6905 stack[0].loc = c_parser_peek_token (parser)->location;
6906 stack[0].expr = c_parser_cast_expression (parser, after);
6907 stack[0].prec = PREC_NONE;
6908 stack[0].sizeof_arg = c_last_sizeof_arg;
6909 sp = 0;
6910 while (true)
6912 enum c_parser_prec oprec;
6913 enum tree_code ocode;
6914 source_range src_range;
6915 if (parser->error)
6916 goto out;
6917 switch (c_parser_peek_token (parser)->type)
6919 case CPP_MULT:
6920 oprec = PREC_MULT;
6921 ocode = MULT_EXPR;
6922 break;
6923 case CPP_DIV:
6924 oprec = PREC_MULT;
6925 ocode = TRUNC_DIV_EXPR;
6926 break;
6927 case CPP_MOD:
6928 oprec = PREC_MULT;
6929 ocode = TRUNC_MOD_EXPR;
6930 break;
6931 case CPP_PLUS:
6932 oprec = PREC_ADD;
6933 ocode = PLUS_EXPR;
6934 break;
6935 case CPP_MINUS:
6936 oprec = PREC_ADD;
6937 ocode = MINUS_EXPR;
6938 break;
6939 case CPP_LSHIFT:
6940 oprec = PREC_SHIFT;
6941 ocode = LSHIFT_EXPR;
6942 break;
6943 case CPP_RSHIFT:
6944 oprec = PREC_SHIFT;
6945 ocode = RSHIFT_EXPR;
6946 break;
6947 case CPP_LESS:
6948 oprec = PREC_REL;
6949 ocode = LT_EXPR;
6950 break;
6951 case CPP_GREATER:
6952 oprec = PREC_REL;
6953 ocode = GT_EXPR;
6954 break;
6955 case CPP_LESS_EQ:
6956 oprec = PREC_REL;
6957 ocode = LE_EXPR;
6958 break;
6959 case CPP_GREATER_EQ:
6960 oprec = PREC_REL;
6961 ocode = GE_EXPR;
6962 break;
6963 case CPP_EQ_EQ:
6964 oprec = PREC_EQ;
6965 ocode = EQ_EXPR;
6966 break;
6967 case CPP_NOT_EQ:
6968 oprec = PREC_EQ;
6969 ocode = NE_EXPR;
6970 break;
6971 case CPP_AND:
6972 oprec = PREC_BITAND;
6973 ocode = BIT_AND_EXPR;
6974 break;
6975 case CPP_XOR:
6976 oprec = PREC_BITXOR;
6977 ocode = BIT_XOR_EXPR;
6978 break;
6979 case CPP_OR:
6980 oprec = PREC_BITOR;
6981 ocode = BIT_IOR_EXPR;
6982 break;
6983 case CPP_AND_AND:
6984 oprec = PREC_LOGAND;
6985 ocode = TRUTH_ANDIF_EXPR;
6986 break;
6987 case CPP_OR_OR:
6988 oprec = PREC_LOGOR;
6989 ocode = TRUTH_ORIF_EXPR;
6990 break;
6991 default:
6992 /* Not a binary operator, so end of the binary
6993 expression. */
6994 goto out;
6996 binary_loc = c_parser_peek_token (parser)->location;
6997 while (oprec <= stack[sp].prec)
6998 POP;
6999 c_parser_consume_token (parser);
7000 switch (ocode)
7002 case TRUTH_ANDIF_EXPR:
7003 src_range = stack[sp].expr.src_range;
7004 stack[sp].expr
7005 = convert_lvalue_to_rvalue (stack[sp].loc,
7006 stack[sp].expr, true, true);
7007 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7008 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7009 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7010 == truthvalue_false_node);
7011 set_c_expr_source_range (&stack[sp].expr, src_range);
7012 break;
7013 case TRUTH_ORIF_EXPR:
7014 src_range = stack[sp].expr.src_range;
7015 stack[sp].expr
7016 = convert_lvalue_to_rvalue (stack[sp].loc,
7017 stack[sp].expr, true, true);
7018 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7019 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7020 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7021 == truthvalue_true_node);
7022 set_c_expr_source_range (&stack[sp].expr, src_range);
7023 break;
7024 default:
7025 break;
7027 sp++;
7028 stack[sp].loc = binary_loc;
7029 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7030 stack[sp].prec = oprec;
7031 stack[sp].op = ocode;
7032 stack[sp].sizeof_arg = c_last_sizeof_arg;
7034 out:
7035 while (sp > 0)
7036 POP;
7037 return stack[0].expr;
7038 #undef POP
7041 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7042 is not NULL then it is an Objective-C message expression which is the
7043 primary-expression starting the expression as an initializer.
7045 cast-expression:
7046 unary-expression
7047 ( type-name ) unary-expression
7050 static struct c_expr
7051 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7053 location_t cast_loc = c_parser_peek_token (parser)->location;
7054 gcc_assert (!after || c_dialect_objc ());
7055 if (after)
7056 return c_parser_postfix_expression_after_primary (parser,
7057 cast_loc, *after);
7058 /* If the expression begins with a parenthesized type name, it may
7059 be either a cast or a compound literal; we need to see whether
7060 the next character is '{' to tell the difference. If not, it is
7061 an unary expression. Full detection of unknown typenames here
7062 would require a 3-token lookahead. */
7063 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7064 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7066 struct c_type_name *type_name;
7067 struct c_expr ret;
7068 struct c_expr expr;
7069 matching_parens parens;
7070 parens.consume_open (parser);
7071 type_name = c_parser_type_name (parser, true);
7072 parens.skip_until_found_close (parser);
7073 if (type_name == NULL)
7075 ret.set_error ();
7076 ret.original_code = ERROR_MARK;
7077 ret.original_type = NULL;
7078 return ret;
7081 /* Save casted types in the function's used types hash table. */
7082 used_types_insert (type_name->specs->type);
7084 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7085 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7086 cast_loc);
7087 if (type_name->specs->alignas_p)
7088 error_at (type_name->specs->locations[cdw_alignas],
7089 "alignment specified for type name in cast");
7091 location_t expr_loc = c_parser_peek_token (parser)->location;
7092 expr = c_parser_cast_expression (parser, NULL);
7093 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7095 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7096 if (ret.value && expr.value)
7097 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7098 ret.original_code = ERROR_MARK;
7099 ret.original_type = NULL;
7100 return ret;
7102 else
7103 return c_parser_unary_expression (parser);
7106 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7108 unary-expression:
7109 postfix-expression
7110 ++ unary-expression
7111 -- unary-expression
7112 unary-operator cast-expression
7113 sizeof unary-expression
7114 sizeof ( type-name )
7116 unary-operator: one of
7117 & * + - ~ !
7119 GNU extensions:
7121 unary-expression:
7122 __alignof__ unary-expression
7123 __alignof__ ( type-name )
7124 && identifier
7126 (C11 permits _Alignof with type names only.)
7128 unary-operator: one of
7129 __extension__ __real__ __imag__
7131 Transactional Memory:
7133 unary-expression:
7134 transaction-expression
7136 In addition, the GNU syntax treats ++ and -- as unary operators, so
7137 they may be applied to cast expressions with errors for non-lvalues
7138 given later. */
7140 static struct c_expr
7141 c_parser_unary_expression (c_parser *parser)
7143 int ext;
7144 struct c_expr ret, op;
7145 location_t op_loc = c_parser_peek_token (parser)->location;
7146 location_t exp_loc;
7147 location_t finish;
7148 ret.original_code = ERROR_MARK;
7149 ret.original_type = NULL;
7150 switch (c_parser_peek_token (parser)->type)
7152 case CPP_PLUS_PLUS:
7153 c_parser_consume_token (parser);
7154 exp_loc = c_parser_peek_token (parser)->location;
7155 op = c_parser_cast_expression (parser, NULL);
7157 op = default_function_array_read_conversion (exp_loc, op);
7158 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7159 case CPP_MINUS_MINUS:
7160 c_parser_consume_token (parser);
7161 exp_loc = c_parser_peek_token (parser)->location;
7162 op = c_parser_cast_expression (parser, NULL);
7164 op = default_function_array_read_conversion (exp_loc, op);
7165 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7166 case CPP_AND:
7167 c_parser_consume_token (parser);
7168 op = c_parser_cast_expression (parser, NULL);
7169 mark_exp_read (op.value);
7170 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7171 case CPP_MULT:
7173 c_parser_consume_token (parser);
7174 exp_loc = c_parser_peek_token (parser)->location;
7175 op = c_parser_cast_expression (parser, NULL);
7176 finish = op.get_finish ();
7177 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7178 location_t combined_loc = make_location (op_loc, op_loc, finish);
7179 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7180 ret.src_range.m_start = op_loc;
7181 ret.src_range.m_finish = finish;
7182 return ret;
7184 case CPP_PLUS:
7185 if (!c_dialect_objc () && !in_system_header_at (input_location))
7186 warning_at (op_loc,
7187 OPT_Wtraditional,
7188 "traditional C rejects the unary plus operator");
7189 c_parser_consume_token (parser);
7190 exp_loc = c_parser_peek_token (parser)->location;
7191 op = c_parser_cast_expression (parser, NULL);
7192 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7193 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7194 case CPP_MINUS:
7195 c_parser_consume_token (parser);
7196 exp_loc = c_parser_peek_token (parser)->location;
7197 op = c_parser_cast_expression (parser, NULL);
7198 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7199 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7200 case CPP_COMPL:
7201 c_parser_consume_token (parser);
7202 exp_loc = c_parser_peek_token (parser)->location;
7203 op = c_parser_cast_expression (parser, NULL);
7204 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7205 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7206 case CPP_NOT:
7207 c_parser_consume_token (parser);
7208 exp_loc = c_parser_peek_token (parser)->location;
7209 op = c_parser_cast_expression (parser, NULL);
7210 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7211 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7212 case CPP_AND_AND:
7213 /* Refer to the address of a label as a pointer. */
7214 c_parser_consume_token (parser);
7215 if (c_parser_next_token_is (parser, CPP_NAME))
7217 ret.value = finish_label_address_expr
7218 (c_parser_peek_token (parser)->value, op_loc);
7219 set_c_expr_source_range (&ret, op_loc,
7220 c_parser_peek_token (parser)->get_finish ());
7221 c_parser_consume_token (parser);
7223 else
7225 c_parser_error (parser, "expected identifier");
7226 ret.set_error ();
7228 return ret;
7229 case CPP_KEYWORD:
7230 switch (c_parser_peek_token (parser)->keyword)
7232 case RID_SIZEOF:
7233 return c_parser_sizeof_expression (parser);
7234 case RID_ALIGNOF:
7235 return c_parser_alignof_expression (parser);
7236 case RID_EXTENSION:
7237 c_parser_consume_token (parser);
7238 ext = disable_extension_diagnostics ();
7239 ret = c_parser_cast_expression (parser, NULL);
7240 restore_extension_diagnostics (ext);
7241 return ret;
7242 case RID_REALPART:
7243 c_parser_consume_token (parser);
7244 exp_loc = c_parser_peek_token (parser)->location;
7245 op = c_parser_cast_expression (parser, NULL);
7246 op = default_function_array_conversion (exp_loc, op);
7247 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7248 case RID_IMAGPART:
7249 c_parser_consume_token (parser);
7250 exp_loc = c_parser_peek_token (parser)->location;
7251 op = c_parser_cast_expression (parser, NULL);
7252 op = default_function_array_conversion (exp_loc, op);
7253 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7254 case RID_TRANSACTION_ATOMIC:
7255 case RID_TRANSACTION_RELAXED:
7256 return c_parser_transaction_expression (parser,
7257 c_parser_peek_token (parser)->keyword);
7258 default:
7259 return c_parser_postfix_expression (parser);
7261 default:
7262 return c_parser_postfix_expression (parser);
7266 /* Parse a sizeof expression. */
7268 static struct c_expr
7269 c_parser_sizeof_expression (c_parser *parser)
7271 struct c_expr expr;
7272 struct c_expr result;
7273 location_t expr_loc;
7274 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7276 location_t start;
7277 location_t finish = UNKNOWN_LOCATION;
7279 start = c_parser_peek_token (parser)->location;
7281 c_parser_consume_token (parser);
7282 c_inhibit_evaluation_warnings++;
7283 in_sizeof++;
7284 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7285 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7287 /* Either sizeof ( type-name ) or sizeof unary-expression
7288 starting with a compound literal. */
7289 struct c_type_name *type_name;
7290 matching_parens parens;
7291 parens.consume_open (parser);
7292 expr_loc = c_parser_peek_token (parser)->location;
7293 type_name = c_parser_type_name (parser, true);
7294 parens.skip_until_found_close (parser);
7295 finish = parser->tokens_buf[0].location;
7296 if (type_name == NULL)
7298 struct c_expr ret;
7299 c_inhibit_evaluation_warnings--;
7300 in_sizeof--;
7301 ret.set_error ();
7302 ret.original_code = ERROR_MARK;
7303 ret.original_type = NULL;
7304 return ret;
7306 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7308 expr = c_parser_postfix_expression_after_paren_type (parser,
7309 type_name,
7310 expr_loc);
7311 finish = expr.get_finish ();
7312 goto sizeof_expr;
7314 /* sizeof ( type-name ). */
7315 if (type_name->specs->alignas_p)
7316 error_at (type_name->specs->locations[cdw_alignas],
7317 "alignment specified for type name in %<sizeof%>");
7318 c_inhibit_evaluation_warnings--;
7319 in_sizeof--;
7320 result = c_expr_sizeof_type (expr_loc, type_name);
7322 else
7324 expr_loc = c_parser_peek_token (parser)->location;
7325 expr = c_parser_unary_expression (parser);
7326 finish = expr.get_finish ();
7327 sizeof_expr:
7328 c_inhibit_evaluation_warnings--;
7329 in_sizeof--;
7330 mark_exp_read (expr.value);
7331 if (TREE_CODE (expr.value) == COMPONENT_REF
7332 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7333 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7334 result = c_expr_sizeof_expr (expr_loc, expr);
7336 if (finish != UNKNOWN_LOCATION)
7337 set_c_expr_source_range (&result, start, finish);
7338 return result;
7341 /* Parse an alignof expression. */
7343 static struct c_expr
7344 c_parser_alignof_expression (c_parser *parser)
7346 struct c_expr expr;
7347 location_t start_loc = c_parser_peek_token (parser)->location;
7348 location_t end_loc;
7349 tree alignof_spelling = c_parser_peek_token (parser)->value;
7350 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7351 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7352 "_Alignof") == 0;
7353 /* A diagnostic is not required for the use of this identifier in
7354 the implementation namespace; only diagnose it for the C11
7355 spelling because of existing code using the other spellings. */
7356 if (is_c11_alignof)
7358 if (flag_isoc99)
7359 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7360 alignof_spelling);
7361 else
7362 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7363 alignof_spelling);
7365 c_parser_consume_token (parser);
7366 c_inhibit_evaluation_warnings++;
7367 in_alignof++;
7368 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7369 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7371 /* Either __alignof__ ( type-name ) or __alignof__
7372 unary-expression starting with a compound literal. */
7373 location_t loc;
7374 struct c_type_name *type_name;
7375 struct c_expr ret;
7376 matching_parens parens;
7377 parens.consume_open (parser);
7378 loc = c_parser_peek_token (parser)->location;
7379 type_name = c_parser_type_name (parser, true);
7380 end_loc = c_parser_peek_token (parser)->location;
7381 parens.skip_until_found_close (parser);
7382 if (type_name == NULL)
7384 struct c_expr ret;
7385 c_inhibit_evaluation_warnings--;
7386 in_alignof--;
7387 ret.set_error ();
7388 ret.original_code = ERROR_MARK;
7389 ret.original_type = NULL;
7390 return ret;
7392 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7394 expr = c_parser_postfix_expression_after_paren_type (parser,
7395 type_name,
7396 loc);
7397 goto alignof_expr;
7399 /* alignof ( type-name ). */
7400 if (type_name->specs->alignas_p)
7401 error_at (type_name->specs->locations[cdw_alignas],
7402 "alignment specified for type name in %qE",
7403 alignof_spelling);
7404 c_inhibit_evaluation_warnings--;
7405 in_alignof--;
7406 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7407 NULL, NULL),
7408 false, is_c11_alignof, 1);
7409 ret.original_code = ERROR_MARK;
7410 ret.original_type = NULL;
7411 set_c_expr_source_range (&ret, start_loc, end_loc);
7412 return ret;
7414 else
7416 struct c_expr ret;
7417 expr = c_parser_unary_expression (parser);
7418 end_loc = expr.src_range.m_finish;
7419 alignof_expr:
7420 mark_exp_read (expr.value);
7421 c_inhibit_evaluation_warnings--;
7422 in_alignof--;
7423 if (is_c11_alignof)
7424 pedwarn (start_loc,
7425 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7426 alignof_spelling);
7427 ret.value = c_alignof_expr (start_loc, expr.value);
7428 ret.original_code = ERROR_MARK;
7429 ret.original_type = NULL;
7430 set_c_expr_source_range (&ret, start_loc, end_loc);
7431 return ret;
7435 /* Helper function to read arguments of builtins which are interfaces
7436 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7437 others. The name of the builtin is passed using BNAME parameter.
7438 Function returns true if there were no errors while parsing and
7439 stores the arguments in CEXPR_LIST. If it returns true,
7440 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7441 parenthesis. */
7442 static bool
7443 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7444 vec<c_expr_t, va_gc> **ret_cexpr_list,
7445 bool choose_expr_p,
7446 location_t *out_close_paren_loc)
7448 location_t loc = c_parser_peek_token (parser)->location;
7449 vec<c_expr_t, va_gc> *cexpr_list;
7450 c_expr_t expr;
7451 bool saved_force_folding_builtin_constant_p;
7453 *ret_cexpr_list = NULL;
7454 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7456 error_at (loc, "cannot take address of %qs", bname);
7457 return false;
7460 c_parser_consume_token (parser);
7462 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7464 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7465 c_parser_consume_token (parser);
7466 return true;
7469 saved_force_folding_builtin_constant_p
7470 = force_folding_builtin_constant_p;
7471 force_folding_builtin_constant_p |= choose_expr_p;
7472 expr = c_parser_expr_no_commas (parser, NULL);
7473 force_folding_builtin_constant_p
7474 = saved_force_folding_builtin_constant_p;
7475 vec_alloc (cexpr_list, 1);
7476 vec_safe_push (cexpr_list, expr);
7477 while (c_parser_next_token_is (parser, CPP_COMMA))
7479 c_parser_consume_token (parser);
7480 expr = c_parser_expr_no_commas (parser, NULL);
7481 vec_safe_push (cexpr_list, expr);
7484 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7485 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7486 return false;
7488 *ret_cexpr_list = cexpr_list;
7489 return true;
7492 /* This represents a single generic-association. */
7494 struct c_generic_association
7496 /* The location of the starting token of the type. */
7497 location_t type_location;
7498 /* The association's type, or NULL_TREE for 'default'. */
7499 tree type;
7500 /* The association's expression. */
7501 struct c_expr expression;
7504 /* Parse a generic-selection. (C11 6.5.1.1).
7506 generic-selection:
7507 _Generic ( assignment-expression , generic-assoc-list )
7509 generic-assoc-list:
7510 generic-association
7511 generic-assoc-list , generic-association
7513 generic-association:
7514 type-name : assignment-expression
7515 default : assignment-expression
7518 static struct c_expr
7519 c_parser_generic_selection (c_parser *parser)
7521 struct c_expr selector, error_expr;
7522 tree selector_type;
7523 struct c_generic_association matched_assoc;
7524 bool match_found = false;
7525 location_t generic_loc, selector_loc;
7527 error_expr.original_code = ERROR_MARK;
7528 error_expr.original_type = NULL;
7529 error_expr.set_error ();
7530 matched_assoc.type_location = UNKNOWN_LOCATION;
7531 matched_assoc.type = NULL_TREE;
7532 matched_assoc.expression = error_expr;
7534 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7535 generic_loc = c_parser_peek_token (parser)->location;
7536 c_parser_consume_token (parser);
7537 if (flag_isoc99)
7538 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7539 "ISO C99 does not support %<_Generic%>");
7540 else
7541 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7542 "ISO C90 does not support %<_Generic%>");
7544 matching_parens parens;
7545 if (!parens.require_open (parser))
7546 return error_expr;
7548 c_inhibit_evaluation_warnings++;
7549 selector_loc = c_parser_peek_token (parser)->location;
7550 selector = c_parser_expr_no_commas (parser, NULL);
7551 selector = default_function_array_conversion (selector_loc, selector);
7552 c_inhibit_evaluation_warnings--;
7554 if (selector.value == error_mark_node)
7556 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7557 return selector;
7559 selector_type = TREE_TYPE (selector.value);
7560 /* In ISO C terms, rvalues (including the controlling expression of
7561 _Generic) do not have qualified types. */
7562 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7563 selector_type = TYPE_MAIN_VARIANT (selector_type);
7564 /* In ISO C terms, _Noreturn is not part of the type of expressions
7565 such as &abort, but in GCC it is represented internally as a type
7566 qualifier. */
7567 if (FUNCTION_POINTER_TYPE_P (selector_type)
7568 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7569 selector_type
7570 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7572 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7575 return error_expr;
7578 auto_vec<c_generic_association> associations;
7579 while (1)
7581 struct c_generic_association assoc, *iter;
7582 unsigned int ix;
7583 c_token *token = c_parser_peek_token (parser);
7585 assoc.type_location = token->location;
7586 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7588 c_parser_consume_token (parser);
7589 assoc.type = NULL_TREE;
7591 else
7593 struct c_type_name *type_name;
7595 type_name = c_parser_type_name (parser);
7596 if (type_name == NULL)
7598 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7599 return error_expr;
7601 assoc.type = groktypename (type_name, NULL, NULL);
7602 if (assoc.type == error_mark_node)
7604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7605 return error_expr;
7608 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7609 error_at (assoc.type_location,
7610 "%<_Generic%> association has function type");
7611 else if (!COMPLETE_TYPE_P (assoc.type))
7612 error_at (assoc.type_location,
7613 "%<_Generic%> association has incomplete type");
7615 if (variably_modified_type_p (assoc.type, NULL_TREE))
7616 error_at (assoc.type_location,
7617 "%<_Generic%> association has "
7618 "variable length type");
7621 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7623 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7624 return error_expr;
7627 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7628 if (assoc.expression.value == error_mark_node)
7630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7631 return error_expr;
7634 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7636 if (assoc.type == NULL_TREE)
7638 if (iter->type == NULL_TREE)
7640 error_at (assoc.type_location,
7641 "duplicate %<default%> case in %<_Generic%>");
7642 inform (iter->type_location, "original %<default%> is here");
7645 else if (iter->type != NULL_TREE)
7647 if (comptypes (assoc.type, iter->type))
7649 error_at (assoc.type_location,
7650 "%<_Generic%> specifies two compatible types");
7651 inform (iter->type_location, "compatible type is here");
7656 if (assoc.type == NULL_TREE)
7658 if (!match_found)
7660 matched_assoc = assoc;
7661 match_found = true;
7664 else if (comptypes (assoc.type, selector_type))
7666 if (!match_found || matched_assoc.type == NULL_TREE)
7668 matched_assoc = assoc;
7669 match_found = true;
7671 else
7673 error_at (assoc.type_location,
7674 "%<_Generic%> selector matches multiple associations");
7675 inform (matched_assoc.type_location,
7676 "other match is here");
7680 associations.safe_push (assoc);
7682 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7683 break;
7684 c_parser_consume_token (parser);
7687 if (!parens.require_close (parser))
7689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7690 return error_expr;
7693 if (!match_found)
7695 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7696 "compatible with any association",
7697 selector_type);
7698 return error_expr;
7701 return matched_assoc.expression;
7704 /* Check the validity of a function pointer argument *EXPR (argument
7705 position POS) to __builtin_tgmath. Return the number of function
7706 arguments if possibly valid; return 0 having reported an error if
7707 not valid. */
7709 static unsigned int
7710 check_tgmath_function (c_expr *expr, unsigned int pos)
7712 tree type = TREE_TYPE (expr->value);
7713 if (!FUNCTION_POINTER_TYPE_P (type))
7715 error_at (expr->get_location (),
7716 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7717 pos);
7718 return 0;
7720 type = TREE_TYPE (type);
7721 if (!prototype_p (type))
7723 error_at (expr->get_location (),
7724 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7725 return 0;
7727 if (stdarg_p (type))
7729 error_at (expr->get_location (),
7730 "argument %u of %<__builtin_tgmath%> has variable arguments",
7731 pos);
7732 return 0;
7734 unsigned int nargs = 0;
7735 function_args_iterator iter;
7736 tree t;
7737 FOREACH_FUNCTION_ARGS (type, t, iter)
7739 if (t == void_type_node)
7740 break;
7741 nargs++;
7743 if (nargs == 0)
7745 error_at (expr->get_location (),
7746 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7747 return 0;
7749 return nargs;
7752 /* Ways in which a parameter or return value of a type-generic macro
7753 may vary between the different functions the macro may call. */
7754 enum tgmath_parm_kind
7756 tgmath_fixed, tgmath_real, tgmath_complex
7759 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7760 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7761 call c_parser_postfix_expression_after_paren_type on encountering them.
7763 postfix-expression:
7764 primary-expression
7765 postfix-expression [ expression ]
7766 postfix-expression ( argument-expression-list[opt] )
7767 postfix-expression . identifier
7768 postfix-expression -> identifier
7769 postfix-expression ++
7770 postfix-expression --
7771 ( type-name ) { initializer-list }
7772 ( type-name ) { initializer-list , }
7774 argument-expression-list:
7775 argument-expression
7776 argument-expression-list , argument-expression
7778 primary-expression:
7779 identifier
7780 constant
7781 string-literal
7782 ( expression )
7783 generic-selection
7785 GNU extensions:
7787 primary-expression:
7788 __func__
7789 (treated as a keyword in GNU C)
7790 __FUNCTION__
7791 __PRETTY_FUNCTION__
7792 ( compound-statement )
7793 __builtin_va_arg ( assignment-expression , type-name )
7794 __builtin_offsetof ( type-name , offsetof-member-designator )
7795 __builtin_choose_expr ( assignment-expression ,
7796 assignment-expression ,
7797 assignment-expression )
7798 __builtin_types_compatible_p ( type-name , type-name )
7799 __builtin_tgmath ( expr-list )
7800 __builtin_complex ( assignment-expression , assignment-expression )
7801 __builtin_shuffle ( assignment-expression , assignment-expression )
7802 __builtin_shuffle ( assignment-expression ,
7803 assignment-expression ,
7804 assignment-expression, )
7806 offsetof-member-designator:
7807 identifier
7808 offsetof-member-designator . identifier
7809 offsetof-member-designator [ expression ]
7811 Objective-C:
7813 primary-expression:
7814 [ objc-receiver objc-message-args ]
7815 @selector ( objc-selector-arg )
7816 @protocol ( identifier )
7817 @encode ( type-name )
7818 objc-string-literal
7819 Classname . identifier
7822 static struct c_expr
7823 c_parser_postfix_expression (c_parser *parser)
7825 struct c_expr expr, e1;
7826 struct c_type_name *t1, *t2;
7827 location_t loc = c_parser_peek_token (parser)->location;
7828 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7829 expr.original_code = ERROR_MARK;
7830 expr.original_type = NULL;
7831 switch (c_parser_peek_token (parser)->type)
7833 case CPP_NUMBER:
7834 expr.value = c_parser_peek_token (parser)->value;
7835 set_c_expr_source_range (&expr, tok_range);
7836 loc = c_parser_peek_token (parser)->location;
7837 c_parser_consume_token (parser);
7838 if (TREE_CODE (expr.value) == FIXED_CST
7839 && !targetm.fixed_point_supported_p ())
7841 error_at (loc, "fixed-point types not supported for this target");
7842 expr.set_error ();
7844 break;
7845 case CPP_CHAR:
7846 case CPP_CHAR16:
7847 case CPP_CHAR32:
7848 case CPP_WCHAR:
7849 expr.value = c_parser_peek_token (parser)->value;
7850 /* For the purpose of warning when a pointer is compared with
7851 a zero character constant. */
7852 expr.original_type = char_type_node;
7853 set_c_expr_source_range (&expr, tok_range);
7854 c_parser_consume_token (parser);
7855 break;
7856 case CPP_STRING:
7857 case CPP_STRING16:
7858 case CPP_STRING32:
7859 case CPP_WSTRING:
7860 case CPP_UTF8STRING:
7861 expr.value = c_parser_peek_token (parser)->value;
7862 set_c_expr_source_range (&expr, tok_range);
7863 expr.original_code = STRING_CST;
7864 c_parser_consume_token (parser);
7865 break;
7866 case CPP_OBJC_STRING:
7867 gcc_assert (c_dialect_objc ());
7868 expr.value
7869 = objc_build_string_object (c_parser_peek_token (parser)->value);
7870 set_c_expr_source_range (&expr, tok_range);
7871 c_parser_consume_token (parser);
7872 break;
7873 case CPP_NAME:
7874 switch (c_parser_peek_token (parser)->id_kind)
7876 case C_ID_ID:
7878 tree id = c_parser_peek_token (parser)->value;
7879 c_parser_consume_token (parser);
7880 expr.value = build_external_ref (loc, id,
7881 (c_parser_peek_token (parser)->type
7882 == CPP_OPEN_PAREN),
7883 &expr.original_type);
7884 set_c_expr_source_range (&expr, tok_range);
7885 break;
7887 case C_ID_CLASSNAME:
7889 /* Here we parse the Objective-C 2.0 Class.name dot
7890 syntax. */
7891 tree class_name = c_parser_peek_token (parser)->value;
7892 tree component;
7893 c_parser_consume_token (parser);
7894 gcc_assert (c_dialect_objc ());
7895 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7897 expr.set_error ();
7898 break;
7900 if (c_parser_next_token_is_not (parser, CPP_NAME))
7902 c_parser_error (parser, "expected identifier");
7903 expr.set_error ();
7904 break;
7906 c_token *component_tok = c_parser_peek_token (parser);
7907 component = component_tok->value;
7908 location_t end_loc = component_tok->get_finish ();
7909 c_parser_consume_token (parser);
7910 expr.value = objc_build_class_component_ref (class_name,
7911 component);
7912 set_c_expr_source_range (&expr, loc, end_loc);
7913 break;
7915 default:
7916 c_parser_error (parser, "expected expression");
7917 expr.set_error ();
7918 break;
7920 break;
7921 case CPP_OPEN_PAREN:
7922 /* A parenthesized expression, statement expression or compound
7923 literal. */
7924 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7926 /* A statement expression. */
7927 tree stmt;
7928 location_t brace_loc;
7929 c_parser_consume_token (parser);
7930 brace_loc = c_parser_peek_token (parser)->location;
7931 c_parser_consume_token (parser);
7932 if (!building_stmt_list_p ())
7934 error_at (loc, "braced-group within expression allowed "
7935 "only inside a function");
7936 parser->error = true;
7937 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7938 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7939 expr.set_error ();
7940 break;
7942 stmt = c_begin_stmt_expr ();
7943 c_parser_compound_statement_nostart (parser);
7944 location_t close_loc = c_parser_peek_token (parser)->location;
7945 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7946 "expected %<)%>");
7947 pedwarn (loc, OPT_Wpedantic,
7948 "ISO C forbids braced-groups within expressions");
7949 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7950 set_c_expr_source_range (&expr, loc, close_loc);
7951 mark_exp_read (expr.value);
7953 else
7955 /* A parenthesized expression. */
7956 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7957 c_parser_consume_token (parser);
7958 expr = c_parser_expression (parser);
7959 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7960 TREE_NO_WARNING (expr.value) = 1;
7961 if (expr.original_code != C_MAYBE_CONST_EXPR
7962 && expr.original_code != SIZEOF_EXPR)
7963 expr.original_code = ERROR_MARK;
7964 /* Don't change EXPR.ORIGINAL_TYPE. */
7965 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7966 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7967 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7968 "expected %<)%>", loc_open_paren);
7970 break;
7971 case CPP_KEYWORD:
7972 switch (c_parser_peek_token (parser)->keyword)
7974 case RID_FUNCTION_NAME:
7975 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7976 "%<__FUNCTION__%> predefined identifier");
7977 expr.value = fname_decl (loc,
7978 c_parser_peek_token (parser)->keyword,
7979 c_parser_peek_token (parser)->value);
7980 set_c_expr_source_range (&expr, loc, loc);
7981 c_parser_consume_token (parser);
7982 break;
7983 case RID_PRETTY_FUNCTION_NAME:
7984 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7985 "%<__PRETTY_FUNCTION__%> predefined identifier");
7986 expr.value = fname_decl (loc,
7987 c_parser_peek_token (parser)->keyword,
7988 c_parser_peek_token (parser)->value);
7989 set_c_expr_source_range (&expr, loc, loc);
7990 c_parser_consume_token (parser);
7991 break;
7992 case RID_C99_FUNCTION_NAME:
7993 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7994 "%<__func__%> predefined identifier");
7995 expr.value = fname_decl (loc,
7996 c_parser_peek_token (parser)->keyword,
7997 c_parser_peek_token (parser)->value);
7998 set_c_expr_source_range (&expr, loc, loc);
7999 c_parser_consume_token (parser);
8000 break;
8001 case RID_VA_ARG:
8003 location_t start_loc = loc;
8004 c_parser_consume_token (parser);
8005 matching_parens parens;
8006 if (!parens.require_open (parser))
8008 expr.set_error ();
8009 break;
8011 e1 = c_parser_expr_no_commas (parser, NULL);
8012 mark_exp_read (e1.value);
8013 e1.value = c_fully_fold (e1.value, false, NULL);
8014 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8016 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8017 expr.set_error ();
8018 break;
8020 loc = c_parser_peek_token (parser)->location;
8021 t1 = c_parser_type_name (parser);
8022 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8023 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8024 "expected %<)%>");
8025 if (t1 == NULL)
8027 expr.set_error ();
8029 else
8031 tree type_expr = NULL_TREE;
8032 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8033 groktypename (t1, &type_expr, NULL));
8034 if (type_expr)
8036 expr.value = build2 (C_MAYBE_CONST_EXPR,
8037 TREE_TYPE (expr.value), type_expr,
8038 expr.value);
8039 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8041 set_c_expr_source_range (&expr, start_loc, end_loc);
8044 break;
8045 case RID_OFFSETOF:
8047 c_parser_consume_token (parser);
8048 matching_parens parens;
8049 if (!parens.require_open (parser))
8051 expr.set_error ();
8052 break;
8054 t1 = c_parser_type_name (parser);
8055 if (t1 == NULL)
8056 parser->error = true;
8057 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8058 gcc_assert (parser->error);
8059 if (parser->error)
8061 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8062 expr.set_error ();
8063 break;
8065 tree type = groktypename (t1, NULL, NULL);
8066 tree offsetof_ref;
8067 if (type == error_mark_node)
8068 offsetof_ref = error_mark_node;
8069 else
8071 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8072 SET_EXPR_LOCATION (offsetof_ref, loc);
8074 /* Parse the second argument to __builtin_offsetof. We
8075 must have one identifier, and beyond that we want to
8076 accept sub structure and sub array references. */
8077 if (c_parser_next_token_is (parser, CPP_NAME))
8079 c_token *comp_tok = c_parser_peek_token (parser);
8080 offsetof_ref = build_component_ref
8081 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8082 c_parser_consume_token (parser);
8083 while (c_parser_next_token_is (parser, CPP_DOT)
8084 || c_parser_next_token_is (parser,
8085 CPP_OPEN_SQUARE)
8086 || c_parser_next_token_is (parser,
8087 CPP_DEREF))
8089 if (c_parser_next_token_is (parser, CPP_DEREF))
8091 loc = c_parser_peek_token (parser)->location;
8092 offsetof_ref = build_array_ref (loc,
8093 offsetof_ref,
8094 integer_zero_node);
8095 goto do_dot;
8097 else if (c_parser_next_token_is (parser, CPP_DOT))
8099 do_dot:
8100 c_parser_consume_token (parser);
8101 if (c_parser_next_token_is_not (parser,
8102 CPP_NAME))
8104 c_parser_error (parser, "expected identifier");
8105 break;
8107 c_token *comp_tok = c_parser_peek_token (parser);
8108 offsetof_ref = build_component_ref
8109 (loc, offsetof_ref, comp_tok->value,
8110 comp_tok->location);
8111 c_parser_consume_token (parser);
8113 else
8115 struct c_expr ce;
8116 tree idx;
8117 loc = c_parser_peek_token (parser)->location;
8118 c_parser_consume_token (parser);
8119 ce = c_parser_expression (parser);
8120 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8121 idx = ce.value;
8122 idx = c_fully_fold (idx, false, NULL);
8123 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8124 "expected %<]%>");
8125 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8129 else
8130 c_parser_error (parser, "expected identifier");
8131 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8132 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8133 "expected %<)%>");
8134 expr.value = fold_offsetof (offsetof_ref);
8135 set_c_expr_source_range (&expr, loc, end_loc);
8137 break;
8138 case RID_CHOOSE_EXPR:
8140 vec<c_expr_t, va_gc> *cexpr_list;
8141 c_expr_t *e1_p, *e2_p, *e3_p;
8142 tree c;
8143 location_t close_paren_loc;
8145 c_parser_consume_token (parser);
8146 if (!c_parser_get_builtin_args (parser,
8147 "__builtin_choose_expr",
8148 &cexpr_list, true,
8149 &close_paren_loc))
8151 expr.set_error ();
8152 break;
8155 if (vec_safe_length (cexpr_list) != 3)
8157 error_at (loc, "wrong number of arguments to "
8158 "%<__builtin_choose_expr%>");
8159 expr.set_error ();
8160 break;
8163 e1_p = &(*cexpr_list)[0];
8164 e2_p = &(*cexpr_list)[1];
8165 e3_p = &(*cexpr_list)[2];
8167 c = e1_p->value;
8168 mark_exp_read (e2_p->value);
8169 mark_exp_read (e3_p->value);
8170 if (TREE_CODE (c) != INTEGER_CST
8171 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8172 error_at (loc,
8173 "first argument to %<__builtin_choose_expr%> not"
8174 " a constant");
8175 constant_expression_warning (c);
8176 expr = integer_zerop (c) ? *e3_p : *e2_p;
8177 set_c_expr_source_range (&expr, loc, close_paren_loc);
8178 break;
8180 case RID_TYPES_COMPATIBLE_P:
8182 c_parser_consume_token (parser);
8183 matching_parens parens;
8184 if (!parens.require_open (parser))
8186 expr.set_error ();
8187 break;
8189 t1 = c_parser_type_name (parser);
8190 if (t1 == NULL)
8192 expr.set_error ();
8193 break;
8195 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8197 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8198 expr.set_error ();
8199 break;
8201 t2 = c_parser_type_name (parser);
8202 if (t2 == NULL)
8204 expr.set_error ();
8205 break;
8207 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8208 parens.skip_until_found_close (parser);
8209 tree e1, e2;
8210 e1 = groktypename (t1, NULL, NULL);
8211 e2 = groktypename (t2, NULL, NULL);
8212 if (e1 == error_mark_node || e2 == error_mark_node)
8214 expr.set_error ();
8215 break;
8218 e1 = TYPE_MAIN_VARIANT (e1);
8219 e2 = TYPE_MAIN_VARIANT (e2);
8221 expr.value
8222 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8223 set_c_expr_source_range (&expr, loc, close_paren_loc);
8225 break;
8226 case RID_BUILTIN_TGMATH:
8228 vec<c_expr_t, va_gc> *cexpr_list;
8229 location_t close_paren_loc;
8231 c_parser_consume_token (parser);
8232 if (!c_parser_get_builtin_args (parser,
8233 "__builtin_tgmath",
8234 &cexpr_list, false,
8235 &close_paren_loc))
8237 expr.set_error ();
8238 break;
8241 if (vec_safe_length (cexpr_list) < 3)
8243 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8244 expr.set_error ();
8245 break;
8248 unsigned int i;
8249 c_expr_t *p;
8250 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8251 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8252 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8253 if (nargs == 0)
8255 expr.set_error ();
8256 break;
8258 if (vec_safe_length (cexpr_list) < nargs)
8260 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8261 expr.set_error ();
8262 break;
8264 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8265 if (num_functions < 2)
8267 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8268 expr.set_error ();
8269 break;
8272 /* The first NUM_FUNCTIONS expressions are the function
8273 pointers. The remaining NARGS expressions are the
8274 arguments that are to be passed to one of those
8275 functions, chosen following <tgmath.h> rules. */
8276 for (unsigned int j = 1; j < num_functions; j++)
8278 unsigned int this_nargs
8279 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8280 if (this_nargs == 0)
8282 expr.set_error ();
8283 goto out;
8285 if (this_nargs != nargs)
8287 error_at ((*cexpr_list)[j].get_location (),
8288 "argument %u of %<__builtin_tgmath%> has "
8289 "wrong number of arguments", j + 1);
8290 expr.set_error ();
8291 goto out;
8295 /* The functions all have the same number of arguments.
8296 Determine whether arguments and return types vary in
8297 ways permitted for <tgmath.h> functions. */
8298 /* The first entry in each of these vectors is for the
8299 return type, subsequent entries for parameter
8300 types. */
8301 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8302 auto_vec<tree> parm_first (nargs + 1);
8303 auto_vec<bool> parm_complex (nargs + 1);
8304 auto_vec<bool> parm_varies (nargs + 1);
8305 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8306 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8307 parm_first.quick_push (first_ret);
8308 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8309 parm_varies.quick_push (false);
8310 function_args_iterator iter;
8311 tree t;
8312 unsigned int argpos;
8313 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8315 if (t == void_type_node)
8316 break;
8317 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8318 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8319 parm_varies.quick_push (false);
8321 for (unsigned int j = 1; j < num_functions; j++)
8323 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8324 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8325 if (ret != parm_first[0])
8327 parm_varies[0] = true;
8328 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8329 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8331 error_at ((*cexpr_list)[0].get_location (),
8332 "invalid type-generic return type for "
8333 "argument %u of %<__builtin_tgmath%>",
8335 expr.set_error ();
8336 goto out;
8338 if (!SCALAR_FLOAT_TYPE_P (ret)
8339 && !COMPLEX_FLOAT_TYPE_P (ret))
8341 error_at ((*cexpr_list)[j].get_location (),
8342 "invalid type-generic return type for "
8343 "argument %u of %<__builtin_tgmath%>",
8344 j + 1);
8345 expr.set_error ();
8346 goto out;
8349 if (TREE_CODE (ret) == COMPLEX_TYPE)
8350 parm_complex[0] = true;
8351 argpos = 1;
8352 FOREACH_FUNCTION_ARGS (type, t, iter)
8354 if (t == void_type_node)
8355 break;
8356 t = TYPE_MAIN_VARIANT (t);
8357 if (t != parm_first[argpos])
8359 parm_varies[argpos] = true;
8360 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8361 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8363 error_at ((*cexpr_list)[0].get_location (),
8364 "invalid type-generic type for "
8365 "argument %u of argument %u of "
8366 "%<__builtin_tgmath%>", argpos, 1);
8367 expr.set_error ();
8368 goto out;
8370 if (!SCALAR_FLOAT_TYPE_P (t)
8371 && !COMPLEX_FLOAT_TYPE_P (t))
8373 error_at ((*cexpr_list)[j].get_location (),
8374 "invalid type-generic type for "
8375 "argument %u of argument %u of "
8376 "%<__builtin_tgmath%>", argpos, j + 1);
8377 expr.set_error ();
8378 goto out;
8381 if (TREE_CODE (t) == COMPLEX_TYPE)
8382 parm_complex[argpos] = true;
8383 argpos++;
8386 enum tgmath_parm_kind max_variation = tgmath_fixed;
8387 for (unsigned int j = 0; j <= nargs; j++)
8389 enum tgmath_parm_kind this_kind;
8390 if (parm_varies[j])
8392 if (parm_complex[j])
8393 max_variation = this_kind = tgmath_complex;
8394 else
8396 this_kind = tgmath_real;
8397 if (max_variation != tgmath_complex)
8398 max_variation = tgmath_real;
8401 else
8402 this_kind = tgmath_fixed;
8403 parm_kind.quick_push (this_kind);
8405 if (max_variation == tgmath_fixed)
8407 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8408 "all have the same type");
8409 expr.set_error ();
8410 break;
8413 /* Identify a parameter (not the return type) that varies,
8414 including with complex types if any variation includes
8415 complex types; there must be at least one such
8416 parameter. */
8417 unsigned int tgarg = 0;
8418 for (unsigned int j = 1; j <= nargs; j++)
8419 if (parm_kind[j] == max_variation)
8421 tgarg = j;
8422 break;
8424 if (tgarg == 0)
8426 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8427 "lack type-generic parameter");
8428 expr.set_error ();
8429 break;
8432 /* Determine the type of the relevant parameter for each
8433 function. */
8434 auto_vec<tree> tg_type (num_functions);
8435 for (unsigned int j = 0; j < num_functions; j++)
8437 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8438 argpos = 1;
8439 FOREACH_FUNCTION_ARGS (type, t, iter)
8441 if (argpos == tgarg)
8443 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8444 break;
8446 argpos++;
8450 /* Verify that the corresponding types are different for
8451 all the listed functions. Also determine whether all
8452 the types are complex, whether all the types are
8453 standard or binary, and whether all the types are
8454 decimal. */
8455 bool all_complex = true;
8456 bool all_binary = true;
8457 bool all_decimal = true;
8458 hash_set<tree> tg_types;
8459 FOR_EACH_VEC_ELT (tg_type, i, t)
8461 if (TREE_CODE (t) == COMPLEX_TYPE)
8462 all_decimal = false;
8463 else
8465 all_complex = false;
8466 if (DECIMAL_FLOAT_TYPE_P (t))
8467 all_binary = false;
8468 else
8469 all_decimal = false;
8471 if (tg_types.add (t))
8473 error_at ((*cexpr_list)[i].get_location (),
8474 "duplicate type-generic parameter type for "
8475 "function argument %u of %<__builtin_tgmath%>",
8476 i + 1);
8477 expr.set_error ();
8478 goto out;
8482 /* Verify that other parameters and the return type whose
8483 types vary have their types varying in the correct
8484 way. */
8485 for (unsigned int j = 0; j < num_functions; j++)
8487 tree exp_type = tg_type[j];
8488 tree exp_real_type = exp_type;
8489 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8490 exp_real_type = TREE_TYPE (exp_type);
8491 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8492 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8493 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8494 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8496 error_at ((*cexpr_list)[j].get_location (),
8497 "bad return type for function argument %u "
8498 "of %<__builtin_tgmath%>", j + 1);
8499 expr.set_error ();
8500 goto out;
8502 argpos = 1;
8503 FOREACH_FUNCTION_ARGS (type, t, iter)
8505 if (t == void_type_node)
8506 break;
8507 t = TYPE_MAIN_VARIANT (t);
8508 if ((parm_kind[argpos] == tgmath_complex
8509 && t != exp_type)
8510 || (parm_kind[argpos] == tgmath_real
8511 && t != exp_real_type))
8513 error_at ((*cexpr_list)[j].get_location (),
8514 "bad type for argument %u of "
8515 "function argument %u of "
8516 "%<__builtin_tgmath%>", argpos, j + 1);
8517 expr.set_error ();
8518 goto out;
8520 argpos++;
8524 /* The functions listed are a valid set of functions for a
8525 <tgmath.h> macro to select between. Identify the
8526 matching function, if any. First, the argument types
8527 must be combined following <tgmath.h> rules. Integer
8528 types are treated as _Decimal64 if any type-generic
8529 argument is decimal, or if the only alternatives for
8530 type-generic arguments are of decimal types, and are
8531 otherwise treated as double (or _Complex double for
8532 complex integer types). After that adjustment, types
8533 are combined following the usual arithmetic
8534 conversions. If the function only accepts complex
8535 arguments, a complex type is produced. */
8536 bool arg_complex = all_complex;
8537 bool arg_binary = all_binary;
8538 bool arg_int_decimal = all_decimal;
8539 for (unsigned int j = 1; j <= nargs; j++)
8541 if (parm_kind[j] == tgmath_fixed)
8542 continue;
8543 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8544 tree type = TREE_TYPE (ce->value);
8545 if (!INTEGRAL_TYPE_P (type)
8546 && !SCALAR_FLOAT_TYPE_P (type)
8547 && TREE_CODE (type) != COMPLEX_TYPE)
8549 error_at (ce->get_location (),
8550 "invalid type of argument %u of type-generic "
8551 "function", j);
8552 expr.set_error ();
8553 goto out;
8555 if (DECIMAL_FLOAT_TYPE_P (type))
8557 arg_int_decimal = true;
8558 if (all_complex)
8560 error_at (ce->get_location (),
8561 "decimal floating-point argument %u to "
8562 "complex-only type-generic function", j);
8563 expr.set_error ();
8564 goto out;
8566 else if (all_binary)
8568 error_at (ce->get_location (),
8569 "decimal floating-point argument %u to "
8570 "binary-only type-generic function", j);
8571 expr.set_error ();
8572 goto out;
8574 else if (arg_complex)
8576 error_at (ce->get_location (),
8577 "both complex and decimal floating-point "
8578 "arguments to type-generic function");
8579 expr.set_error ();
8580 goto out;
8582 else if (arg_binary)
8584 error_at (ce->get_location (),
8585 "both binary and decimal floating-point "
8586 "arguments to type-generic function");
8587 expr.set_error ();
8588 goto out;
8591 else if (TREE_CODE (type) == COMPLEX_TYPE)
8593 arg_complex = true;
8594 if (COMPLEX_FLOAT_TYPE_P (type))
8595 arg_binary = true;
8596 if (all_decimal)
8598 error_at (ce->get_location (),
8599 "complex argument %u to "
8600 "decimal-only type-generic function", j);
8601 expr.set_error ();
8602 goto out;
8604 else if (arg_int_decimal)
8606 error_at (ce->get_location (),
8607 "both complex and decimal floating-point "
8608 "arguments to type-generic function");
8609 expr.set_error ();
8610 goto out;
8613 else if (SCALAR_FLOAT_TYPE_P (type))
8615 arg_binary = true;
8616 if (all_decimal)
8618 error_at (ce->get_location (),
8619 "binary argument %u to "
8620 "decimal-only type-generic function", j);
8621 expr.set_error ();
8622 goto out;
8624 else if (arg_int_decimal)
8626 error_at (ce->get_location (),
8627 "both binary and decimal floating-point "
8628 "arguments to type-generic function");
8629 expr.set_error ();
8630 goto out;
8634 tree arg_real = NULL_TREE;
8635 for (unsigned int j = 1; j <= nargs; j++)
8637 if (parm_kind[j] == tgmath_fixed)
8638 continue;
8639 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8640 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8641 if (TREE_CODE (type) == COMPLEX_TYPE)
8642 type = TREE_TYPE (type);
8643 if (INTEGRAL_TYPE_P (type))
8644 type = (arg_int_decimal
8645 ? dfloat64_type_node
8646 : double_type_node);
8647 if (arg_real == NULL_TREE)
8648 arg_real = type;
8649 else
8650 arg_real = common_type (arg_real, type);
8651 if (arg_real == error_mark_node)
8653 expr.set_error ();
8654 goto out;
8657 tree arg_type = (arg_complex
8658 ? build_complex_type (arg_real)
8659 : arg_real);
8661 /* Look for a function to call with type-generic parameter
8662 type ARG_TYPE. */
8663 c_expr_t *fn = NULL;
8664 for (unsigned int j = 0; j < num_functions; j++)
8666 if (tg_type[j] == arg_type)
8668 fn = &(*cexpr_list)[j];
8669 break;
8672 if (fn == NULL
8673 && parm_kind[0] == tgmath_fixed
8674 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8676 /* Presume this is a macro that rounds its result to a
8677 narrower type, and look for the first function with
8678 at least the range and precision of the argument
8679 type. */
8680 for (unsigned int j = 0; j < num_functions; j++)
8682 if (arg_complex
8683 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8684 continue;
8685 tree real_tg_type = (arg_complex
8686 ? TREE_TYPE (tg_type[j])
8687 : tg_type[j]);
8688 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8689 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8690 continue;
8691 scalar_float_mode arg_mode
8692 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8693 scalar_float_mode tg_mode
8694 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8695 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8696 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8697 if (arg_fmt->b == tg_fmt->b
8698 && arg_fmt->p <= tg_fmt->p
8699 && arg_fmt->emax <= tg_fmt->emax
8700 && (arg_fmt->emin - arg_fmt->p
8701 >= tg_fmt->emin - tg_fmt->p))
8703 fn = &(*cexpr_list)[j];
8704 break;
8708 if (fn == NULL)
8710 error_at (loc, "no matching function for type-generic call");
8711 expr.set_error ();
8712 break;
8715 /* Construct a call to FN. */
8716 vec<tree, va_gc> *args;
8717 vec_alloc (args, nargs);
8718 vec<tree, va_gc> *origtypes;
8719 vec_alloc (origtypes, nargs);
8720 auto_vec<location_t> arg_loc (nargs);
8721 for (unsigned int j = 0; j < nargs; j++)
8723 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8724 args->quick_push (ce->value);
8725 arg_loc.quick_push (ce->get_location ());
8726 origtypes->quick_push (ce->original_type);
8728 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8729 args, origtypes);
8730 set_c_expr_source_range (&expr, loc, close_paren_loc);
8731 break;
8733 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8735 vec<c_expr_t, va_gc> *cexpr_list;
8736 c_expr_t *e2_p;
8737 tree chain_value;
8738 location_t close_paren_loc;
8740 c_parser_consume_token (parser);
8741 if (!c_parser_get_builtin_args (parser,
8742 "__builtin_call_with_static_chain",
8743 &cexpr_list, false,
8744 &close_paren_loc))
8746 expr.set_error ();
8747 break;
8749 if (vec_safe_length (cexpr_list) != 2)
8751 error_at (loc, "wrong number of arguments to "
8752 "%<__builtin_call_with_static_chain%>");
8753 expr.set_error ();
8754 break;
8757 expr = (*cexpr_list)[0];
8758 e2_p = &(*cexpr_list)[1];
8759 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8760 chain_value = e2_p->value;
8761 mark_exp_read (chain_value);
8763 if (TREE_CODE (expr.value) != CALL_EXPR)
8764 error_at (loc, "first argument to "
8765 "%<__builtin_call_with_static_chain%> "
8766 "must be a call expression");
8767 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8768 error_at (loc, "second argument to "
8769 "%<__builtin_call_with_static_chain%> "
8770 "must be a pointer type");
8771 else
8772 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8773 set_c_expr_source_range (&expr, loc, close_paren_loc);
8774 break;
8776 case RID_BUILTIN_COMPLEX:
8778 vec<c_expr_t, va_gc> *cexpr_list;
8779 c_expr_t *e1_p, *e2_p;
8780 location_t close_paren_loc;
8782 c_parser_consume_token (parser);
8783 if (!c_parser_get_builtin_args (parser,
8784 "__builtin_complex",
8785 &cexpr_list, false,
8786 &close_paren_loc))
8788 expr.set_error ();
8789 break;
8792 if (vec_safe_length (cexpr_list) != 2)
8794 error_at (loc, "wrong number of arguments to "
8795 "%<__builtin_complex%>");
8796 expr.set_error ();
8797 break;
8800 e1_p = &(*cexpr_list)[0];
8801 e2_p = &(*cexpr_list)[1];
8803 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8804 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8805 e1_p->value = convert (TREE_TYPE (e1_p->value),
8806 TREE_OPERAND (e1_p->value, 0));
8807 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8808 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8809 e2_p->value = convert (TREE_TYPE (e2_p->value),
8810 TREE_OPERAND (e2_p->value, 0));
8811 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8812 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8813 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8814 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8816 error_at (loc, "%<__builtin_complex%> operand "
8817 "not of real binary floating-point type");
8818 expr.set_error ();
8819 break;
8821 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8822 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8824 error_at (loc,
8825 "%<__builtin_complex%> operands of different types");
8826 expr.set_error ();
8827 break;
8829 pedwarn_c90 (loc, OPT_Wpedantic,
8830 "ISO C90 does not support complex types");
8831 expr.value = build2_loc (loc, COMPLEX_EXPR,
8832 build_complex_type
8833 (TYPE_MAIN_VARIANT
8834 (TREE_TYPE (e1_p->value))),
8835 e1_p->value, e2_p->value);
8836 set_c_expr_source_range (&expr, loc, close_paren_loc);
8837 break;
8839 case RID_BUILTIN_SHUFFLE:
8841 vec<c_expr_t, va_gc> *cexpr_list;
8842 unsigned int i;
8843 c_expr_t *p;
8844 location_t close_paren_loc;
8846 c_parser_consume_token (parser);
8847 if (!c_parser_get_builtin_args (parser,
8848 "__builtin_shuffle",
8849 &cexpr_list, false,
8850 &close_paren_loc))
8852 expr.set_error ();
8853 break;
8856 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8857 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8859 if (vec_safe_length (cexpr_list) == 2)
8860 expr.value =
8861 c_build_vec_perm_expr
8862 (loc, (*cexpr_list)[0].value,
8863 NULL_TREE, (*cexpr_list)[1].value);
8865 else if (vec_safe_length (cexpr_list) == 3)
8866 expr.value =
8867 c_build_vec_perm_expr
8868 (loc, (*cexpr_list)[0].value,
8869 (*cexpr_list)[1].value,
8870 (*cexpr_list)[2].value);
8871 else
8873 error_at (loc, "wrong number of arguments to "
8874 "%<__builtin_shuffle%>");
8875 expr.set_error ();
8877 set_c_expr_source_range (&expr, loc, close_paren_loc);
8878 break;
8880 case RID_AT_SELECTOR:
8882 gcc_assert (c_dialect_objc ());
8883 c_parser_consume_token (parser);
8884 matching_parens parens;
8885 if (!parens.require_open (parser))
8887 expr.set_error ();
8888 break;
8890 tree sel = c_parser_objc_selector_arg (parser);
8891 location_t close_loc = c_parser_peek_token (parser)->location;
8892 parens.skip_until_found_close (parser);
8893 expr.value = objc_build_selector_expr (loc, sel);
8894 set_c_expr_source_range (&expr, loc, close_loc);
8896 break;
8897 case RID_AT_PROTOCOL:
8899 gcc_assert (c_dialect_objc ());
8900 c_parser_consume_token (parser);
8901 matching_parens parens;
8902 if (!parens.require_open (parser))
8904 expr.set_error ();
8905 break;
8907 if (c_parser_next_token_is_not (parser, CPP_NAME))
8909 c_parser_error (parser, "expected identifier");
8910 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8911 expr.set_error ();
8912 break;
8914 tree id = c_parser_peek_token (parser)->value;
8915 c_parser_consume_token (parser);
8916 location_t close_loc = c_parser_peek_token (parser)->location;
8917 parens.skip_until_found_close (parser);
8918 expr.value = objc_build_protocol_expr (id);
8919 set_c_expr_source_range (&expr, loc, close_loc);
8921 break;
8922 case RID_AT_ENCODE:
8924 /* Extension to support C-structures in the archiver. */
8925 gcc_assert (c_dialect_objc ());
8926 c_parser_consume_token (parser);
8927 matching_parens parens;
8928 if (!parens.require_open (parser))
8930 expr.set_error ();
8931 break;
8933 t1 = c_parser_type_name (parser);
8934 if (t1 == NULL)
8936 expr.set_error ();
8937 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8938 break;
8940 location_t close_loc = c_parser_peek_token (parser)->location;
8941 parens.skip_until_found_close (parser);
8942 tree type = groktypename (t1, NULL, NULL);
8943 expr.value = objc_build_encode_expr (type);
8944 set_c_expr_source_range (&expr, loc, close_loc);
8946 break;
8947 case RID_GENERIC:
8948 expr = c_parser_generic_selection (parser);
8949 break;
8950 default:
8951 c_parser_error (parser, "expected expression");
8952 expr.set_error ();
8953 break;
8955 break;
8956 case CPP_OPEN_SQUARE:
8957 if (c_dialect_objc ())
8959 tree receiver, args;
8960 c_parser_consume_token (parser);
8961 receiver = c_parser_objc_receiver (parser);
8962 args = c_parser_objc_message_args (parser);
8963 location_t close_loc = c_parser_peek_token (parser)->location;
8964 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8965 "expected %<]%>");
8966 expr.value = objc_build_message_expr (receiver, args);
8967 set_c_expr_source_range (&expr, loc, close_loc);
8968 break;
8970 /* Else fall through to report error. */
8971 /* FALLTHRU */
8972 default:
8973 c_parser_error (parser, "expected expression");
8974 expr.set_error ();
8975 break;
8977 out:
8978 return c_parser_postfix_expression_after_primary
8979 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8982 /* Parse a postfix expression after a parenthesized type name: the
8983 brace-enclosed initializer of a compound literal, possibly followed
8984 by some postfix operators. This is separate because it is not
8985 possible to tell until after the type name whether a cast
8986 expression has a cast or a compound literal, or whether the operand
8987 of sizeof is a parenthesized type name or starts with a compound
8988 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8989 location of the first token after the parentheses around the type
8990 name. */
8992 static struct c_expr
8993 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8994 struct c_type_name *type_name,
8995 location_t type_loc)
8997 tree type;
8998 struct c_expr init;
8999 bool non_const;
9000 struct c_expr expr;
9001 location_t start_loc;
9002 tree type_expr = NULL_TREE;
9003 bool type_expr_const = true;
9004 check_compound_literal_type (type_loc, type_name);
9005 rich_location richloc (line_table, type_loc);
9006 start_init (NULL_TREE, NULL, 0, &richloc);
9007 type = groktypename (type_name, &type_expr, &type_expr_const);
9008 start_loc = c_parser_peek_token (parser)->location;
9009 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9011 error_at (type_loc, "compound literal has variable size");
9012 type = error_mark_node;
9014 init = c_parser_braced_init (parser, type, false, NULL);
9015 finish_init ();
9016 maybe_warn_string_init (type_loc, type, init);
9018 if (type != error_mark_node
9019 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9020 && current_function_decl)
9022 error ("compound literal qualified by address-space qualifier");
9023 type = error_mark_node;
9026 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9027 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9028 ? CONSTRUCTOR_NON_CONST (init.value)
9029 : init.original_code == C_MAYBE_CONST_EXPR);
9030 non_const |= !type_expr_const;
9031 unsigned int alignas_align = 0;
9032 if (type != error_mark_node
9033 && type_name->specs->align_log != -1)
9035 alignas_align = 1U << type_name->specs->align_log;
9036 if (alignas_align < min_align_of_type (type))
9038 error_at (type_name->specs->locations[cdw_alignas],
9039 "%<_Alignas%> specifiers cannot reduce "
9040 "alignment of compound literal");
9041 alignas_align = 0;
9044 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9045 alignas_align);
9046 set_c_expr_source_range (&expr, init.src_range);
9047 expr.original_code = ERROR_MARK;
9048 expr.original_type = NULL;
9049 if (type != error_mark_node
9050 && expr.value != error_mark_node
9051 && type_expr)
9053 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9055 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9056 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9058 else
9060 gcc_assert (!non_const);
9061 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9062 type_expr, expr.value);
9065 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9068 /* Callback function for sizeof_pointer_memaccess_warning to compare
9069 types. */
9071 static bool
9072 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9074 return comptypes (type1, type2) == 1;
9077 /* Parse a postfix expression after the initial primary or compound
9078 literal; that is, parse a series of postfix operators.
9080 EXPR_LOC is the location of the primary expression. */
9082 static struct c_expr
9083 c_parser_postfix_expression_after_primary (c_parser *parser,
9084 location_t expr_loc,
9085 struct c_expr expr)
9087 struct c_expr orig_expr;
9088 tree ident, idx;
9089 location_t sizeof_arg_loc[3], comp_loc;
9090 tree sizeof_arg[3];
9091 unsigned int literal_zero_mask;
9092 unsigned int i;
9093 vec<tree, va_gc> *exprlist;
9094 vec<tree, va_gc> *origtypes = NULL;
9095 vec<location_t> arg_loc = vNULL;
9096 location_t start;
9097 location_t finish;
9099 while (true)
9101 location_t op_loc = c_parser_peek_token (parser)->location;
9102 switch (c_parser_peek_token (parser)->type)
9104 case CPP_OPEN_SQUARE:
9105 /* Array reference. */
9106 c_parser_consume_token (parser);
9107 idx = c_parser_expression (parser).value;
9108 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9109 "expected %<]%>");
9110 start = expr.get_start ();
9111 finish = parser->tokens_buf[0].location;
9112 expr.value = build_array_ref (op_loc, expr.value, idx);
9113 set_c_expr_source_range (&expr, start, finish);
9114 expr.original_code = ERROR_MARK;
9115 expr.original_type = NULL;
9116 break;
9117 case CPP_OPEN_PAREN:
9118 /* Function call. */
9119 c_parser_consume_token (parser);
9120 for (i = 0; i < 3; i++)
9122 sizeof_arg[i] = NULL_TREE;
9123 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9125 literal_zero_mask = 0;
9126 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9127 exprlist = NULL;
9128 else
9129 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9130 sizeof_arg_loc, sizeof_arg,
9131 &arg_loc, &literal_zero_mask);
9132 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9133 "expected %<)%>");
9134 orig_expr = expr;
9135 mark_exp_read (expr.value);
9136 if (warn_sizeof_pointer_memaccess)
9137 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9138 expr.value, exprlist,
9139 sizeof_arg,
9140 sizeof_ptr_memacc_comptypes);
9141 if (TREE_CODE (expr.value) == FUNCTION_DECL
9142 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9143 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9144 && vec_safe_length (exprlist) == 3)
9146 tree arg0 = (*exprlist)[0];
9147 tree arg2 = (*exprlist)[2];
9148 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9151 start = expr.get_start ();
9152 finish = parser->tokens_buf[0].get_finish ();
9153 expr.value
9154 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9155 exprlist, origtypes);
9156 set_c_expr_source_range (&expr, start, finish);
9158 expr.original_code = ERROR_MARK;
9159 if (TREE_CODE (expr.value) == INTEGER_CST
9160 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9161 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9162 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9163 expr.original_code = C_MAYBE_CONST_EXPR;
9164 expr.original_type = NULL;
9165 if (exprlist)
9167 release_tree_vector (exprlist);
9168 release_tree_vector (origtypes);
9170 arg_loc.release ();
9171 break;
9172 case CPP_DOT:
9173 /* Structure element reference. */
9174 c_parser_consume_token (parser);
9175 expr = default_function_array_conversion (expr_loc, expr);
9176 if (c_parser_next_token_is (parser, CPP_NAME))
9178 c_token *comp_tok = c_parser_peek_token (parser);
9179 ident = comp_tok->value;
9180 comp_loc = comp_tok->location;
9182 else
9184 c_parser_error (parser, "expected identifier");
9185 expr.set_error ();
9186 expr.original_code = ERROR_MARK;
9187 expr.original_type = NULL;
9188 return expr;
9190 start = expr.get_start ();
9191 finish = c_parser_peek_token (parser)->get_finish ();
9192 c_parser_consume_token (parser);
9193 expr.value = build_component_ref (op_loc, expr.value, ident,
9194 comp_loc);
9195 set_c_expr_source_range (&expr, start, finish);
9196 expr.original_code = ERROR_MARK;
9197 if (TREE_CODE (expr.value) != COMPONENT_REF)
9198 expr.original_type = NULL;
9199 else
9201 /* Remember the original type of a bitfield. */
9202 tree field = TREE_OPERAND (expr.value, 1);
9203 if (TREE_CODE (field) != FIELD_DECL)
9204 expr.original_type = NULL;
9205 else
9206 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9208 break;
9209 case CPP_DEREF:
9210 /* Structure element reference. */
9211 c_parser_consume_token (parser);
9212 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9213 if (c_parser_next_token_is (parser, CPP_NAME))
9215 c_token *comp_tok = c_parser_peek_token (parser);
9216 ident = comp_tok->value;
9217 comp_loc = comp_tok->location;
9219 else
9221 c_parser_error (parser, "expected identifier");
9222 expr.set_error ();
9223 expr.original_code = ERROR_MARK;
9224 expr.original_type = NULL;
9225 return expr;
9227 start = expr.get_start ();
9228 finish = c_parser_peek_token (parser)->get_finish ();
9229 c_parser_consume_token (parser);
9230 expr.value = build_component_ref (op_loc,
9231 build_indirect_ref (op_loc,
9232 expr.value,
9233 RO_ARROW),
9234 ident, comp_loc);
9235 set_c_expr_source_range (&expr, start, finish);
9236 expr.original_code = ERROR_MARK;
9237 if (TREE_CODE (expr.value) != COMPONENT_REF)
9238 expr.original_type = NULL;
9239 else
9241 /* Remember the original type of a bitfield. */
9242 tree field = TREE_OPERAND (expr.value, 1);
9243 if (TREE_CODE (field) != FIELD_DECL)
9244 expr.original_type = NULL;
9245 else
9246 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9248 break;
9249 case CPP_PLUS_PLUS:
9250 /* Postincrement. */
9251 start = expr.get_start ();
9252 finish = c_parser_peek_token (parser)->get_finish ();
9253 c_parser_consume_token (parser);
9254 expr = default_function_array_read_conversion (expr_loc, expr);
9255 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9256 expr.value, false);
9257 set_c_expr_source_range (&expr, start, finish);
9258 expr.original_code = ERROR_MARK;
9259 expr.original_type = NULL;
9260 break;
9261 case CPP_MINUS_MINUS:
9262 /* Postdecrement. */
9263 start = expr.get_start ();
9264 finish = c_parser_peek_token (parser)->get_finish ();
9265 c_parser_consume_token (parser);
9266 expr = default_function_array_read_conversion (expr_loc, expr);
9267 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9268 expr.value, false);
9269 set_c_expr_source_range (&expr, start, finish);
9270 expr.original_code = ERROR_MARK;
9271 expr.original_type = NULL;
9272 break;
9273 default:
9274 return expr;
9279 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9281 expression:
9282 assignment-expression
9283 expression , assignment-expression
9286 static struct c_expr
9287 c_parser_expression (c_parser *parser)
9289 location_t tloc = c_parser_peek_token (parser)->location;
9290 struct c_expr expr;
9291 expr = c_parser_expr_no_commas (parser, NULL);
9292 if (c_parser_next_token_is (parser, CPP_COMMA))
9293 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9294 while (c_parser_next_token_is (parser, CPP_COMMA))
9296 struct c_expr next;
9297 tree lhsval;
9298 location_t loc = c_parser_peek_token (parser)->location;
9299 location_t expr_loc;
9300 c_parser_consume_token (parser);
9301 expr_loc = c_parser_peek_token (parser)->location;
9302 lhsval = expr.value;
9303 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9304 lhsval = TREE_OPERAND (lhsval, 1);
9305 if (DECL_P (lhsval) || handled_component_p (lhsval))
9306 mark_exp_read (lhsval);
9307 next = c_parser_expr_no_commas (parser, NULL);
9308 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9309 expr.value = build_compound_expr (loc, expr.value, next.value);
9310 expr.original_code = COMPOUND_EXPR;
9311 expr.original_type = next.original_type;
9313 return expr;
9316 /* Parse an expression and convert functions or arrays to pointers and
9317 lvalues to rvalues. */
9319 static struct c_expr
9320 c_parser_expression_conv (c_parser *parser)
9322 struct c_expr expr;
9323 location_t loc = c_parser_peek_token (parser)->location;
9324 expr = c_parser_expression (parser);
9325 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9326 return expr;
9329 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9330 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9332 static inline void
9333 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9334 unsigned int idx)
9336 if (idx >= HOST_BITS_PER_INT)
9337 return;
9339 c_token *tok = c_parser_peek_token (parser);
9340 switch (tok->type)
9342 case CPP_NUMBER:
9343 case CPP_CHAR:
9344 case CPP_WCHAR:
9345 case CPP_CHAR16:
9346 case CPP_CHAR32:
9347 /* If a parameter is literal zero alone, remember it
9348 for -Wmemset-transposed-args warning. */
9349 if (integer_zerop (tok->value)
9350 && !TREE_OVERFLOW (tok->value)
9351 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9352 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9353 *literal_zero_mask |= 1U << idx;
9354 default:
9355 break;
9359 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9360 functions and arrays to pointers and lvalues to rvalues. If
9361 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9362 locations of function arguments into this vector.
9364 nonempty-expr-list:
9365 assignment-expression
9366 nonempty-expr-list , assignment-expression
9369 static vec<tree, va_gc> *
9370 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9371 vec<tree, va_gc> **p_orig_types,
9372 location_t *sizeof_arg_loc, tree *sizeof_arg,
9373 vec<location_t> *locations,
9374 unsigned int *literal_zero_mask)
9376 vec<tree, va_gc> *ret;
9377 vec<tree, va_gc> *orig_types;
9378 struct c_expr expr;
9379 unsigned int idx = 0;
9381 ret = make_tree_vector ();
9382 if (p_orig_types == NULL)
9383 orig_types = NULL;
9384 else
9385 orig_types = make_tree_vector ();
9387 if (literal_zero_mask)
9388 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9389 expr = c_parser_expr_no_commas (parser, NULL);
9390 if (convert_p)
9391 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9392 if (fold_p)
9393 expr.value = c_fully_fold (expr.value, false, NULL);
9394 ret->quick_push (expr.value);
9395 if (orig_types)
9396 orig_types->quick_push (expr.original_type);
9397 if (locations)
9398 locations->safe_push (expr.get_location ());
9399 if (sizeof_arg != NULL
9400 && expr.original_code == SIZEOF_EXPR)
9402 sizeof_arg[0] = c_last_sizeof_arg;
9403 sizeof_arg_loc[0] = c_last_sizeof_loc;
9405 while (c_parser_next_token_is (parser, CPP_COMMA))
9407 c_parser_consume_token (parser);
9408 if (literal_zero_mask)
9409 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9410 expr = c_parser_expr_no_commas (parser, NULL);
9411 if (convert_p)
9412 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9413 true);
9414 if (fold_p)
9415 expr.value = c_fully_fold (expr.value, false, NULL);
9416 vec_safe_push (ret, expr.value);
9417 if (orig_types)
9418 vec_safe_push (orig_types, expr.original_type);
9419 if (locations)
9420 locations->safe_push (expr.get_location ());
9421 if (++idx < 3
9422 && sizeof_arg != NULL
9423 && expr.original_code == SIZEOF_EXPR)
9425 sizeof_arg[idx] = c_last_sizeof_arg;
9426 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9429 if (orig_types)
9430 *p_orig_types = orig_types;
9431 return ret;
9434 /* Parse Objective-C-specific constructs. */
9436 /* Parse an objc-class-definition.
9438 objc-class-definition:
9439 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9440 objc-class-instance-variables[opt] objc-methodprotolist @end
9441 @implementation identifier objc-superclass[opt]
9442 objc-class-instance-variables[opt]
9443 @interface identifier ( identifier ) objc-protocol-refs[opt]
9444 objc-methodprotolist @end
9445 @interface identifier ( ) objc-protocol-refs[opt]
9446 objc-methodprotolist @end
9447 @implementation identifier ( identifier )
9449 objc-superclass:
9450 : identifier
9452 "@interface identifier (" must start "@interface identifier (
9453 identifier ) ...": objc-methodprotolist in the first production may
9454 not start with a parenthesized identifier as a declarator of a data
9455 definition with no declaration specifiers if the objc-superclass,
9456 objc-protocol-refs and objc-class-instance-variables are omitted. */
9458 static void
9459 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9461 bool iface_p;
9462 tree id1;
9463 tree superclass;
9464 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9465 iface_p = true;
9466 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9467 iface_p = false;
9468 else
9469 gcc_unreachable ();
9471 c_parser_consume_token (parser);
9472 if (c_parser_next_token_is_not (parser, CPP_NAME))
9474 c_parser_error (parser, "expected identifier");
9475 return;
9477 id1 = c_parser_peek_token (parser)->value;
9478 c_parser_consume_token (parser);
9479 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9481 /* We have a category or class extension. */
9482 tree id2;
9483 tree proto = NULL_TREE;
9484 matching_parens parens;
9485 parens.consume_open (parser);
9486 if (c_parser_next_token_is_not (parser, CPP_NAME))
9488 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9490 /* We have a class extension. */
9491 id2 = NULL_TREE;
9493 else
9495 c_parser_error (parser, "expected identifier or %<)%>");
9496 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9497 return;
9500 else
9502 id2 = c_parser_peek_token (parser)->value;
9503 c_parser_consume_token (parser);
9505 parens.skip_until_found_close (parser);
9506 if (!iface_p)
9508 objc_start_category_implementation (id1, id2);
9509 return;
9511 if (c_parser_next_token_is (parser, CPP_LESS))
9512 proto = c_parser_objc_protocol_refs (parser);
9513 objc_start_category_interface (id1, id2, proto, attributes);
9514 c_parser_objc_methodprotolist (parser);
9515 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9516 objc_finish_interface ();
9517 return;
9519 if (c_parser_next_token_is (parser, CPP_COLON))
9521 c_parser_consume_token (parser);
9522 if (c_parser_next_token_is_not (parser, CPP_NAME))
9524 c_parser_error (parser, "expected identifier");
9525 return;
9527 superclass = c_parser_peek_token (parser)->value;
9528 c_parser_consume_token (parser);
9530 else
9531 superclass = NULL_TREE;
9532 if (iface_p)
9534 tree proto = NULL_TREE;
9535 if (c_parser_next_token_is (parser, CPP_LESS))
9536 proto = c_parser_objc_protocol_refs (parser);
9537 objc_start_class_interface (id1, superclass, proto, attributes);
9539 else
9540 objc_start_class_implementation (id1, superclass);
9541 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9542 c_parser_objc_class_instance_variables (parser);
9543 if (iface_p)
9545 objc_continue_interface ();
9546 c_parser_objc_methodprotolist (parser);
9547 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9548 objc_finish_interface ();
9550 else
9552 objc_continue_implementation ();
9553 return;
9557 /* Parse objc-class-instance-variables.
9559 objc-class-instance-variables:
9560 { objc-instance-variable-decl-list[opt] }
9562 objc-instance-variable-decl-list:
9563 objc-visibility-spec
9564 objc-instance-variable-decl ;
9566 objc-instance-variable-decl-list objc-visibility-spec
9567 objc-instance-variable-decl-list objc-instance-variable-decl ;
9568 objc-instance-variable-decl-list ;
9570 objc-visibility-spec:
9571 @private
9572 @protected
9573 @public
9575 objc-instance-variable-decl:
9576 struct-declaration
9579 static void
9580 c_parser_objc_class_instance_variables (c_parser *parser)
9582 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9583 c_parser_consume_token (parser);
9584 while (c_parser_next_token_is_not (parser, CPP_EOF))
9586 tree decls;
9587 /* Parse any stray semicolon. */
9588 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9590 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9591 "extra semicolon");
9592 c_parser_consume_token (parser);
9593 continue;
9595 /* Stop if at the end of the instance variables. */
9596 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9598 c_parser_consume_token (parser);
9599 break;
9601 /* Parse any objc-visibility-spec. */
9602 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9604 c_parser_consume_token (parser);
9605 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9606 continue;
9608 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9610 c_parser_consume_token (parser);
9611 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9612 continue;
9614 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9616 c_parser_consume_token (parser);
9617 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9618 continue;
9620 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9622 c_parser_consume_token (parser);
9623 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9624 continue;
9626 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9628 c_parser_pragma (parser, pragma_external, NULL);
9629 continue;
9632 /* Parse some comma-separated declarations. */
9633 decls = c_parser_struct_declaration (parser);
9634 if (decls == NULL)
9636 /* There is a syntax error. We want to skip the offending
9637 tokens up to the next ';' (included) or '}'
9638 (excluded). */
9640 /* First, skip manually a ')' or ']'. This is because they
9641 reduce the nesting level, so c_parser_skip_until_found()
9642 wouldn't be able to skip past them. */
9643 c_token *token = c_parser_peek_token (parser);
9644 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9645 c_parser_consume_token (parser);
9647 /* Then, do the standard skipping. */
9648 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9650 /* We hopefully recovered. Start normal parsing again. */
9651 parser->error = false;
9652 continue;
9654 else
9656 /* Comma-separated instance variables are chained together
9657 in reverse order; add them one by one. */
9658 tree ivar = nreverse (decls);
9659 for (; ivar; ivar = DECL_CHAIN (ivar))
9660 objc_add_instance_variable (copy_node (ivar));
9662 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9666 /* Parse an objc-class-declaration.
9668 objc-class-declaration:
9669 @class identifier-list ;
9672 static void
9673 c_parser_objc_class_declaration (c_parser *parser)
9675 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9676 c_parser_consume_token (parser);
9677 /* Any identifiers, including those declared as type names, are OK
9678 here. */
9679 while (true)
9681 tree id;
9682 if (c_parser_next_token_is_not (parser, CPP_NAME))
9684 c_parser_error (parser, "expected identifier");
9685 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9686 parser->error = false;
9687 return;
9689 id = c_parser_peek_token (parser)->value;
9690 objc_declare_class (id);
9691 c_parser_consume_token (parser);
9692 if (c_parser_next_token_is (parser, CPP_COMMA))
9693 c_parser_consume_token (parser);
9694 else
9695 break;
9697 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9700 /* Parse an objc-alias-declaration.
9702 objc-alias-declaration:
9703 @compatibility_alias identifier identifier ;
9706 static void
9707 c_parser_objc_alias_declaration (c_parser *parser)
9709 tree id1, id2;
9710 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9711 c_parser_consume_token (parser);
9712 if (c_parser_next_token_is_not (parser, CPP_NAME))
9714 c_parser_error (parser, "expected identifier");
9715 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9716 return;
9718 id1 = c_parser_peek_token (parser)->value;
9719 c_parser_consume_token (parser);
9720 if (c_parser_next_token_is_not (parser, CPP_NAME))
9722 c_parser_error (parser, "expected identifier");
9723 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9724 return;
9726 id2 = c_parser_peek_token (parser)->value;
9727 c_parser_consume_token (parser);
9728 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9729 objc_declare_alias (id1, id2);
9732 /* Parse an objc-protocol-definition.
9734 objc-protocol-definition:
9735 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9736 @protocol identifier-list ;
9738 "@protocol identifier ;" should be resolved as "@protocol
9739 identifier-list ;": objc-methodprotolist may not start with a
9740 semicolon in the first alternative if objc-protocol-refs are
9741 omitted. */
9743 static void
9744 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9746 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9748 c_parser_consume_token (parser);
9749 if (c_parser_next_token_is_not (parser, CPP_NAME))
9751 c_parser_error (parser, "expected identifier");
9752 return;
9754 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9755 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9757 /* Any identifiers, including those declared as type names, are
9758 OK here. */
9759 while (true)
9761 tree id;
9762 if (c_parser_next_token_is_not (parser, CPP_NAME))
9764 c_parser_error (parser, "expected identifier");
9765 break;
9767 id = c_parser_peek_token (parser)->value;
9768 objc_declare_protocol (id, attributes);
9769 c_parser_consume_token (parser);
9770 if (c_parser_next_token_is (parser, CPP_COMMA))
9771 c_parser_consume_token (parser);
9772 else
9773 break;
9775 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9777 else
9779 tree id = c_parser_peek_token (parser)->value;
9780 tree proto = NULL_TREE;
9781 c_parser_consume_token (parser);
9782 if (c_parser_next_token_is (parser, CPP_LESS))
9783 proto = c_parser_objc_protocol_refs (parser);
9784 parser->objc_pq_context = true;
9785 objc_start_protocol (id, proto, attributes);
9786 c_parser_objc_methodprotolist (parser);
9787 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9788 parser->objc_pq_context = false;
9789 objc_finish_interface ();
9793 /* Parse an objc-method-type.
9795 objc-method-type:
9799 Return true if it is a class method (+) and false if it is
9800 an instance method (-).
9802 static inline bool
9803 c_parser_objc_method_type (c_parser *parser)
9805 switch (c_parser_peek_token (parser)->type)
9807 case CPP_PLUS:
9808 c_parser_consume_token (parser);
9809 return true;
9810 case CPP_MINUS:
9811 c_parser_consume_token (parser);
9812 return false;
9813 default:
9814 gcc_unreachable ();
9818 /* Parse an objc-method-definition.
9820 objc-method-definition:
9821 objc-method-type objc-method-decl ;[opt] compound-statement
9824 static void
9825 c_parser_objc_method_definition (c_parser *parser)
9827 bool is_class_method = c_parser_objc_method_type (parser);
9828 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9829 parser->objc_pq_context = true;
9830 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9831 &expr);
9832 if (decl == error_mark_node)
9833 return; /* Bail here. */
9835 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9837 c_parser_consume_token (parser);
9838 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9839 "extra semicolon in method definition specified");
9842 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9844 c_parser_error (parser, "expected %<{%>");
9845 return;
9848 parser->objc_pq_context = false;
9849 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9851 add_stmt (c_parser_compound_statement (parser));
9852 objc_finish_method_definition (current_function_decl);
9854 else
9856 /* This code is executed when we find a method definition
9857 outside of an @implementation context (or invalid for other
9858 reasons). Parse the method (to keep going) but do not emit
9859 any code.
9861 c_parser_compound_statement (parser);
9865 /* Parse an objc-methodprotolist.
9867 objc-methodprotolist:
9868 empty
9869 objc-methodprotolist objc-methodproto
9870 objc-methodprotolist declaration
9871 objc-methodprotolist ;
9872 @optional
9873 @required
9875 The declaration is a data definition, which may be missing
9876 declaration specifiers under the same rules and diagnostics as
9877 other data definitions outside functions, and the stray semicolon
9878 is diagnosed the same way as a stray semicolon outside a
9879 function. */
9881 static void
9882 c_parser_objc_methodprotolist (c_parser *parser)
9884 while (true)
9886 /* The list is terminated by @end. */
9887 switch (c_parser_peek_token (parser)->type)
9889 case CPP_SEMICOLON:
9890 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9891 "ISO C does not allow extra %<;%> outside of a function");
9892 c_parser_consume_token (parser);
9893 break;
9894 case CPP_PLUS:
9895 case CPP_MINUS:
9896 c_parser_objc_methodproto (parser);
9897 break;
9898 case CPP_PRAGMA:
9899 c_parser_pragma (parser, pragma_external, NULL);
9900 break;
9901 case CPP_EOF:
9902 return;
9903 default:
9904 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9905 return;
9906 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9907 c_parser_objc_at_property_declaration (parser);
9908 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9910 objc_set_method_opt (true);
9911 c_parser_consume_token (parser);
9913 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9915 objc_set_method_opt (false);
9916 c_parser_consume_token (parser);
9918 else
9919 c_parser_declaration_or_fndef (parser, false, false, true,
9920 false, true, NULL, vNULL);
9921 break;
9926 /* Parse an objc-methodproto.
9928 objc-methodproto:
9929 objc-method-type objc-method-decl ;
9932 static void
9933 c_parser_objc_methodproto (c_parser *parser)
9935 bool is_class_method = c_parser_objc_method_type (parser);
9936 tree decl, attributes = NULL_TREE;
9938 /* Remember protocol qualifiers in prototypes. */
9939 parser->objc_pq_context = true;
9940 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9941 NULL);
9942 /* Forget protocol qualifiers now. */
9943 parser->objc_pq_context = false;
9945 /* Do not allow the presence of attributes to hide an erroneous
9946 method implementation in the interface section. */
9947 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9949 c_parser_error (parser, "expected %<;%>");
9950 return;
9953 if (decl != error_mark_node)
9954 objc_add_method_declaration (is_class_method, decl, attributes);
9956 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9959 /* If we are at a position that method attributes may be present, check that
9960 there are not any parsed already (a syntax error) and then collect any
9961 specified at the current location. Finally, if new attributes were present,
9962 check that the next token is legal ( ';' for decls and '{' for defs). */
9964 static bool
9965 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9967 bool bad = false;
9968 if (*attributes)
9970 c_parser_error (parser,
9971 "method attributes must be specified at the end only");
9972 *attributes = NULL_TREE;
9973 bad = true;
9976 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9977 *attributes = c_parser_attributes (parser);
9979 /* If there were no attributes here, just report any earlier error. */
9980 if (*attributes == NULL_TREE || bad)
9981 return bad;
9983 /* If the attributes are followed by a ; or {, then just report any earlier
9984 error. */
9985 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9986 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9987 return bad;
9989 /* We've got attributes, but not at the end. */
9990 c_parser_error (parser,
9991 "expected %<;%> or %<{%> after method attribute definition");
9992 return true;
9995 /* Parse an objc-method-decl.
9997 objc-method-decl:
9998 ( objc-type-name ) objc-selector
9999 objc-selector
10000 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10001 objc-keyword-selector objc-optparmlist
10002 attributes
10004 objc-keyword-selector:
10005 objc-keyword-decl
10006 objc-keyword-selector objc-keyword-decl
10008 objc-keyword-decl:
10009 objc-selector : ( objc-type-name ) identifier
10010 objc-selector : identifier
10011 : ( objc-type-name ) identifier
10012 : identifier
10014 objc-optparmlist:
10015 objc-optparms objc-optellipsis
10017 objc-optparms:
10018 empty
10019 objc-opt-parms , parameter-declaration
10021 objc-optellipsis:
10022 empty
10023 , ...
10026 static tree
10027 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10028 tree *attributes, tree *expr)
10030 tree type = NULL_TREE;
10031 tree sel;
10032 tree parms = NULL_TREE;
10033 bool ellipsis = false;
10034 bool attr_err = false;
10036 *attributes = NULL_TREE;
10037 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10039 matching_parens parens;
10040 parens.consume_open (parser);
10041 type = c_parser_objc_type_name (parser);
10042 parens.skip_until_found_close (parser);
10044 sel = c_parser_objc_selector (parser);
10045 /* If there is no selector, or a colon follows, we have an
10046 objc-keyword-selector. If there is a selector, and a colon does
10047 not follow, that selector ends the objc-method-decl. */
10048 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10050 tree tsel = sel;
10051 tree list = NULL_TREE;
10052 while (true)
10054 tree atype = NULL_TREE, id, keyworddecl;
10055 tree param_attr = NULL_TREE;
10056 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10057 break;
10058 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10060 c_parser_consume_token (parser);
10061 atype = c_parser_objc_type_name (parser);
10062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10063 "expected %<)%>");
10065 /* New ObjC allows attributes on method parameters. */
10066 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10067 param_attr = c_parser_attributes (parser);
10068 if (c_parser_next_token_is_not (parser, CPP_NAME))
10070 c_parser_error (parser, "expected identifier");
10071 return error_mark_node;
10073 id = c_parser_peek_token (parser)->value;
10074 c_parser_consume_token (parser);
10075 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10076 list = chainon (list, keyworddecl);
10077 tsel = c_parser_objc_selector (parser);
10078 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10079 break;
10082 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10084 /* Parse the optional parameter list. Optional Objective-C
10085 method parameters follow the C syntax, and may include '...'
10086 to denote a variable number of arguments. */
10087 parms = make_node (TREE_LIST);
10088 while (c_parser_next_token_is (parser, CPP_COMMA))
10090 struct c_parm *parm;
10091 c_parser_consume_token (parser);
10092 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10094 ellipsis = true;
10095 c_parser_consume_token (parser);
10096 attr_err |= c_parser_objc_maybe_method_attributes
10097 (parser, attributes) ;
10098 break;
10100 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10101 if (parm == NULL)
10102 break;
10103 parms = chainon (parms,
10104 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10106 sel = list;
10108 else
10109 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10111 if (sel == NULL)
10113 c_parser_error (parser, "objective-c method declaration is expected");
10114 return error_mark_node;
10117 if (attr_err)
10118 return error_mark_node;
10120 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10123 /* Parse an objc-type-name.
10125 objc-type-name:
10126 objc-type-qualifiers[opt] type-name
10127 objc-type-qualifiers[opt]
10129 objc-type-qualifiers:
10130 objc-type-qualifier
10131 objc-type-qualifiers objc-type-qualifier
10133 objc-type-qualifier: one of
10134 in out inout bycopy byref oneway
10137 static tree
10138 c_parser_objc_type_name (c_parser *parser)
10140 tree quals = NULL_TREE;
10141 struct c_type_name *type_name = NULL;
10142 tree type = NULL_TREE;
10143 while (true)
10145 c_token *token = c_parser_peek_token (parser);
10146 if (token->type == CPP_KEYWORD
10147 && (token->keyword == RID_IN
10148 || token->keyword == RID_OUT
10149 || token->keyword == RID_INOUT
10150 || token->keyword == RID_BYCOPY
10151 || token->keyword == RID_BYREF
10152 || token->keyword == RID_ONEWAY))
10154 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10155 c_parser_consume_token (parser);
10157 else
10158 break;
10160 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10161 type_name = c_parser_type_name (parser);
10162 if (type_name)
10163 type = groktypename (type_name, NULL, NULL);
10165 /* If the type is unknown, and error has already been produced and
10166 we need to recover from the error. In that case, use NULL_TREE
10167 for the type, as if no type had been specified; this will use the
10168 default type ('id') which is good for error recovery. */
10169 if (type == error_mark_node)
10170 type = NULL_TREE;
10172 return build_tree_list (quals, type);
10175 /* Parse objc-protocol-refs.
10177 objc-protocol-refs:
10178 < identifier-list >
10181 static tree
10182 c_parser_objc_protocol_refs (c_parser *parser)
10184 tree list = NULL_TREE;
10185 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10186 c_parser_consume_token (parser);
10187 /* Any identifiers, including those declared as type names, are OK
10188 here. */
10189 while (true)
10191 tree id;
10192 if (c_parser_next_token_is_not (parser, CPP_NAME))
10194 c_parser_error (parser, "expected identifier");
10195 break;
10197 id = c_parser_peek_token (parser)->value;
10198 list = chainon (list, build_tree_list (NULL_TREE, id));
10199 c_parser_consume_token (parser);
10200 if (c_parser_next_token_is (parser, CPP_COMMA))
10201 c_parser_consume_token (parser);
10202 else
10203 break;
10205 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10206 return list;
10209 /* Parse an objc-try-catch-finally-statement.
10211 objc-try-catch-finally-statement:
10212 @try compound-statement objc-catch-list[opt]
10213 @try compound-statement objc-catch-list[opt] @finally compound-statement
10215 objc-catch-list:
10216 @catch ( objc-catch-parameter-declaration ) compound-statement
10217 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10219 objc-catch-parameter-declaration:
10220 parameter-declaration
10221 '...'
10223 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10225 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10226 for C++. Keep them in sync. */
10228 static void
10229 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10231 location_t location;
10232 tree stmt;
10234 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10235 c_parser_consume_token (parser);
10236 location = c_parser_peek_token (parser)->location;
10237 objc_maybe_warn_exceptions (location);
10238 stmt = c_parser_compound_statement (parser);
10239 objc_begin_try_stmt (location, stmt);
10241 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10243 struct c_parm *parm;
10244 tree parameter_declaration = error_mark_node;
10245 bool seen_open_paren = false;
10247 c_parser_consume_token (parser);
10248 matching_parens parens;
10249 if (!parens.require_open (parser))
10250 seen_open_paren = true;
10251 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10253 /* We have "@catch (...)" (where the '...' are literally
10254 what is in the code). Skip the '...'.
10255 parameter_declaration is set to NULL_TREE, and
10256 objc_being_catch_clauses() knows that that means
10257 '...'. */
10258 c_parser_consume_token (parser);
10259 parameter_declaration = NULL_TREE;
10261 else
10263 /* We have "@catch (NSException *exception)" or something
10264 like that. Parse the parameter declaration. */
10265 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10266 if (parm == NULL)
10267 parameter_declaration = error_mark_node;
10268 else
10269 parameter_declaration = grokparm (parm, NULL);
10271 if (seen_open_paren)
10272 parens.require_close (parser);
10273 else
10275 /* If there was no open parenthesis, we are recovering from
10276 an error, and we are trying to figure out what mistake
10277 the user has made. */
10279 /* If there is an immediate closing parenthesis, the user
10280 probably forgot the opening one (ie, they typed "@catch
10281 NSException *e)". Parse the closing parenthesis and keep
10282 going. */
10283 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10284 c_parser_consume_token (parser);
10286 /* If these is no immediate closing parenthesis, the user
10287 probably doesn't know that parenthesis are required at
10288 all (ie, they typed "@catch NSException *e"). So, just
10289 forget about the closing parenthesis and keep going. */
10291 objc_begin_catch_clause (parameter_declaration);
10292 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10293 c_parser_compound_statement_nostart (parser);
10294 objc_finish_catch_clause ();
10296 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10298 c_parser_consume_token (parser);
10299 location = c_parser_peek_token (parser)->location;
10300 stmt = c_parser_compound_statement (parser);
10301 objc_build_finally_clause (location, stmt);
10303 objc_finish_try_stmt ();
10306 /* Parse an objc-synchronized-statement.
10308 objc-synchronized-statement:
10309 @synchronized ( expression ) compound-statement
10312 static void
10313 c_parser_objc_synchronized_statement (c_parser *parser)
10315 location_t loc;
10316 tree expr, stmt;
10317 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10318 c_parser_consume_token (parser);
10319 loc = c_parser_peek_token (parser)->location;
10320 objc_maybe_warn_exceptions (loc);
10321 matching_parens parens;
10322 if (parens.require_open (parser))
10324 struct c_expr ce = c_parser_expression (parser);
10325 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10326 expr = ce.value;
10327 expr = c_fully_fold (expr, false, NULL);
10328 parens.skip_until_found_close (parser);
10330 else
10331 expr = error_mark_node;
10332 stmt = c_parser_compound_statement (parser);
10333 objc_build_synchronized (loc, expr, stmt);
10336 /* Parse an objc-selector; return NULL_TREE without an error if the
10337 next token is not an objc-selector.
10339 objc-selector:
10340 identifier
10341 one of
10342 enum struct union if else while do for switch case default
10343 break continue return goto asm sizeof typeof __alignof
10344 unsigned long const short volatile signed restrict _Complex
10345 in out inout bycopy byref oneway int char float double void _Bool
10346 _Atomic
10348 ??? Why this selection of keywords but not, for example, storage
10349 class specifiers? */
10351 static tree
10352 c_parser_objc_selector (c_parser *parser)
10354 c_token *token = c_parser_peek_token (parser);
10355 tree value = token->value;
10356 if (token->type == CPP_NAME)
10358 c_parser_consume_token (parser);
10359 return value;
10361 if (token->type != CPP_KEYWORD)
10362 return NULL_TREE;
10363 switch (token->keyword)
10365 case RID_ENUM:
10366 case RID_STRUCT:
10367 case RID_UNION:
10368 case RID_IF:
10369 case RID_ELSE:
10370 case RID_WHILE:
10371 case RID_DO:
10372 case RID_FOR:
10373 case RID_SWITCH:
10374 case RID_CASE:
10375 case RID_DEFAULT:
10376 case RID_BREAK:
10377 case RID_CONTINUE:
10378 case RID_RETURN:
10379 case RID_GOTO:
10380 case RID_ASM:
10381 case RID_SIZEOF:
10382 case RID_TYPEOF:
10383 case RID_ALIGNOF:
10384 case RID_UNSIGNED:
10385 case RID_LONG:
10386 case RID_CONST:
10387 case RID_SHORT:
10388 case RID_VOLATILE:
10389 case RID_SIGNED:
10390 case RID_RESTRICT:
10391 case RID_COMPLEX:
10392 case RID_IN:
10393 case RID_OUT:
10394 case RID_INOUT:
10395 case RID_BYCOPY:
10396 case RID_BYREF:
10397 case RID_ONEWAY:
10398 case RID_INT:
10399 case RID_CHAR:
10400 case RID_FLOAT:
10401 case RID_DOUBLE:
10402 CASE_RID_FLOATN_NX:
10403 case RID_VOID:
10404 case RID_BOOL:
10405 case RID_ATOMIC:
10406 case RID_AUTO_TYPE:
10407 case RID_INT_N_0:
10408 case RID_INT_N_1:
10409 case RID_INT_N_2:
10410 case RID_INT_N_3:
10411 c_parser_consume_token (parser);
10412 return value;
10413 default:
10414 return NULL_TREE;
10418 /* Parse an objc-selector-arg.
10420 objc-selector-arg:
10421 objc-selector
10422 objc-keywordname-list
10424 objc-keywordname-list:
10425 objc-keywordname
10426 objc-keywordname-list objc-keywordname
10428 objc-keywordname:
10429 objc-selector :
10433 static tree
10434 c_parser_objc_selector_arg (c_parser *parser)
10436 tree sel = c_parser_objc_selector (parser);
10437 tree list = NULL_TREE;
10438 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10439 return sel;
10440 while (true)
10442 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10443 return list;
10444 list = chainon (list, build_tree_list (sel, NULL_TREE));
10445 sel = c_parser_objc_selector (parser);
10446 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10447 break;
10449 return list;
10452 /* Parse an objc-receiver.
10454 objc-receiver:
10455 expression
10456 class-name
10457 type-name
10460 static tree
10461 c_parser_objc_receiver (c_parser *parser)
10463 location_t loc = c_parser_peek_token (parser)->location;
10465 if (c_parser_peek_token (parser)->type == CPP_NAME
10466 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10467 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10469 tree id = c_parser_peek_token (parser)->value;
10470 c_parser_consume_token (parser);
10471 return objc_get_class_reference (id);
10473 struct c_expr ce = c_parser_expression (parser);
10474 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10475 return c_fully_fold (ce.value, false, NULL);
10478 /* Parse objc-message-args.
10480 objc-message-args:
10481 objc-selector
10482 objc-keywordarg-list
10484 objc-keywordarg-list:
10485 objc-keywordarg
10486 objc-keywordarg-list objc-keywordarg
10488 objc-keywordarg:
10489 objc-selector : objc-keywordexpr
10490 : objc-keywordexpr
10493 static tree
10494 c_parser_objc_message_args (c_parser *parser)
10496 tree sel = c_parser_objc_selector (parser);
10497 tree list = NULL_TREE;
10498 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10499 return sel;
10500 while (true)
10502 tree keywordexpr;
10503 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10504 return error_mark_node;
10505 keywordexpr = c_parser_objc_keywordexpr (parser);
10506 list = chainon (list, build_tree_list (sel, keywordexpr));
10507 sel = c_parser_objc_selector (parser);
10508 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10509 break;
10511 return list;
10514 /* Parse an objc-keywordexpr.
10516 objc-keywordexpr:
10517 nonempty-expr-list
10520 static tree
10521 c_parser_objc_keywordexpr (c_parser *parser)
10523 tree ret;
10524 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10525 NULL, NULL, NULL, NULL);
10526 if (vec_safe_length (expr_list) == 1)
10528 /* Just return the expression, remove a level of
10529 indirection. */
10530 ret = (*expr_list)[0];
10532 else
10534 /* We have a comma expression, we will collapse later. */
10535 ret = build_tree_list_vec (expr_list);
10537 release_tree_vector (expr_list);
10538 return ret;
10541 /* A check, needed in several places, that ObjC interface, implementation or
10542 method definitions are not prefixed by incorrect items. */
10543 static bool
10544 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10545 struct c_declspecs *specs)
10547 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10548 || specs->typespec_kind != ctsk_none)
10550 c_parser_error (parser,
10551 "no type or storage class may be specified here,");
10552 c_parser_skip_to_end_of_block_or_statement (parser);
10553 return true;
10555 return false;
10558 /* Parse an Objective-C @property declaration. The syntax is:
10560 objc-property-declaration:
10561 '@property' objc-property-attributes[opt] struct-declaration ;
10563 objc-property-attributes:
10564 '(' objc-property-attribute-list ')'
10566 objc-property-attribute-list:
10567 objc-property-attribute
10568 objc-property-attribute-list, objc-property-attribute
10570 objc-property-attribute
10571 'getter' = identifier
10572 'setter' = identifier
10573 'readonly'
10574 'readwrite'
10575 'assign'
10576 'retain'
10577 'copy'
10578 'nonatomic'
10580 For example:
10581 @property NSString *name;
10582 @property (readonly) id object;
10583 @property (retain, nonatomic, getter=getTheName) id name;
10584 @property int a, b, c;
10586 PS: This function is identical to cp_parser_objc_at_propery_declaration
10587 for C++. Keep them in sync. */
10588 static void
10589 c_parser_objc_at_property_declaration (c_parser *parser)
10591 /* The following variables hold the attributes of the properties as
10592 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10593 seen. When we see an attribute, we set them to 'true' (if they
10594 are boolean properties) or to the identifier (if they have an
10595 argument, ie, for getter and setter). Note that here we only
10596 parse the list of attributes, check the syntax and accumulate the
10597 attributes that we find. objc_add_property_declaration() will
10598 then process the information. */
10599 bool property_assign = false;
10600 bool property_copy = false;
10601 tree property_getter_ident = NULL_TREE;
10602 bool property_nonatomic = false;
10603 bool property_readonly = false;
10604 bool property_readwrite = false;
10605 bool property_retain = false;
10606 tree property_setter_ident = NULL_TREE;
10608 /* 'properties' is the list of properties that we read. Usually a
10609 single one, but maybe more (eg, in "@property int a, b, c;" there
10610 are three). */
10611 tree properties;
10612 location_t loc;
10614 loc = c_parser_peek_token (parser)->location;
10615 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10617 c_parser_consume_token (parser); /* Eat '@property'. */
10619 /* Parse the optional attribute list... */
10620 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10622 matching_parens parens;
10624 /* Eat the '(' */
10625 parens.consume_open (parser);
10627 /* Property attribute keywords are valid now. */
10628 parser->objc_property_attr_context = true;
10630 while (true)
10632 bool syntax_error = false;
10633 c_token *token = c_parser_peek_token (parser);
10634 enum rid keyword;
10636 if (token->type != CPP_KEYWORD)
10638 if (token->type == CPP_CLOSE_PAREN)
10639 c_parser_error (parser, "expected identifier");
10640 else
10642 c_parser_consume_token (parser);
10643 c_parser_error (parser, "unknown property attribute");
10645 break;
10647 keyword = token->keyword;
10648 c_parser_consume_token (parser);
10649 switch (keyword)
10651 case RID_ASSIGN: property_assign = true; break;
10652 case RID_COPY: property_copy = true; break;
10653 case RID_NONATOMIC: property_nonatomic = true; break;
10654 case RID_READONLY: property_readonly = true; break;
10655 case RID_READWRITE: property_readwrite = true; break;
10656 case RID_RETAIN: property_retain = true; break;
10658 case RID_GETTER:
10659 case RID_SETTER:
10660 if (c_parser_next_token_is_not (parser, CPP_EQ))
10662 if (keyword == RID_GETTER)
10663 c_parser_error (parser,
10664 "missing %<=%> (after %<getter%> attribute)");
10665 else
10666 c_parser_error (parser,
10667 "missing %<=%> (after %<setter%> attribute)");
10668 syntax_error = true;
10669 break;
10671 c_parser_consume_token (parser); /* eat the = */
10672 if (c_parser_next_token_is_not (parser, CPP_NAME))
10674 c_parser_error (parser, "expected identifier");
10675 syntax_error = true;
10676 break;
10678 if (keyword == RID_SETTER)
10680 if (property_setter_ident != NULL_TREE)
10681 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10682 else
10683 property_setter_ident = c_parser_peek_token (parser)->value;
10684 c_parser_consume_token (parser);
10685 if (c_parser_next_token_is_not (parser, CPP_COLON))
10686 c_parser_error (parser, "setter name must terminate with %<:%>");
10687 else
10688 c_parser_consume_token (parser);
10690 else
10692 if (property_getter_ident != NULL_TREE)
10693 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10694 else
10695 property_getter_ident = c_parser_peek_token (parser)->value;
10696 c_parser_consume_token (parser);
10698 break;
10699 default:
10700 c_parser_error (parser, "unknown property attribute");
10701 syntax_error = true;
10702 break;
10705 if (syntax_error)
10706 break;
10708 if (c_parser_next_token_is (parser, CPP_COMMA))
10709 c_parser_consume_token (parser);
10710 else
10711 break;
10713 parser->objc_property_attr_context = false;
10714 parens.skip_until_found_close (parser);
10716 /* ... and the property declaration(s). */
10717 properties = c_parser_struct_declaration (parser);
10719 if (properties == error_mark_node)
10721 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10722 parser->error = false;
10723 return;
10726 if (properties == NULL_TREE)
10727 c_parser_error (parser, "expected identifier");
10728 else
10730 /* Comma-separated properties are chained together in
10731 reverse order; add them one by one. */
10732 properties = nreverse (properties);
10734 for (; properties; properties = TREE_CHAIN (properties))
10735 objc_add_property_declaration (loc, copy_node (properties),
10736 property_readonly, property_readwrite,
10737 property_assign, property_retain,
10738 property_copy, property_nonatomic,
10739 property_getter_ident, property_setter_ident);
10742 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10743 parser->error = false;
10746 /* Parse an Objective-C @synthesize declaration. The syntax is:
10748 objc-synthesize-declaration:
10749 @synthesize objc-synthesize-identifier-list ;
10751 objc-synthesize-identifier-list:
10752 objc-synthesize-identifier
10753 objc-synthesize-identifier-list, objc-synthesize-identifier
10755 objc-synthesize-identifier
10756 identifier
10757 identifier = identifier
10759 For example:
10760 @synthesize MyProperty;
10761 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10763 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10764 for C++. Keep them in sync.
10766 static void
10767 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10769 tree list = NULL_TREE;
10770 location_t loc;
10771 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10772 loc = c_parser_peek_token (parser)->location;
10774 c_parser_consume_token (parser);
10775 while (true)
10777 tree property, ivar;
10778 if (c_parser_next_token_is_not (parser, CPP_NAME))
10780 c_parser_error (parser, "expected identifier");
10781 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10782 /* Once we find the semicolon, we can resume normal parsing.
10783 We have to reset parser->error manually because
10784 c_parser_skip_until_found() won't reset it for us if the
10785 next token is precisely a semicolon. */
10786 parser->error = false;
10787 return;
10789 property = c_parser_peek_token (parser)->value;
10790 c_parser_consume_token (parser);
10791 if (c_parser_next_token_is (parser, CPP_EQ))
10793 c_parser_consume_token (parser);
10794 if (c_parser_next_token_is_not (parser, CPP_NAME))
10796 c_parser_error (parser, "expected identifier");
10797 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10798 parser->error = false;
10799 return;
10801 ivar = c_parser_peek_token (parser)->value;
10802 c_parser_consume_token (parser);
10804 else
10805 ivar = NULL_TREE;
10806 list = chainon (list, build_tree_list (ivar, property));
10807 if (c_parser_next_token_is (parser, CPP_COMMA))
10808 c_parser_consume_token (parser);
10809 else
10810 break;
10812 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10813 objc_add_synthesize_declaration (loc, list);
10816 /* Parse an Objective-C @dynamic declaration. The syntax is:
10818 objc-dynamic-declaration:
10819 @dynamic identifier-list ;
10821 For example:
10822 @dynamic MyProperty;
10823 @dynamic MyProperty, AnotherProperty;
10825 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10826 for C++. Keep them in sync.
10828 static void
10829 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10831 tree list = NULL_TREE;
10832 location_t loc;
10833 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10834 loc = c_parser_peek_token (parser)->location;
10836 c_parser_consume_token (parser);
10837 while (true)
10839 tree property;
10840 if (c_parser_next_token_is_not (parser, CPP_NAME))
10842 c_parser_error (parser, "expected identifier");
10843 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10844 parser->error = false;
10845 return;
10847 property = c_parser_peek_token (parser)->value;
10848 list = chainon (list, build_tree_list (NULL_TREE, property));
10849 c_parser_consume_token (parser);
10850 if (c_parser_next_token_is (parser, CPP_COMMA))
10851 c_parser_consume_token (parser);
10852 else
10853 break;
10855 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10856 objc_add_dynamic_declaration (loc, list);
10860 /* Parse a pragma GCC ivdep. */
10862 static bool
10863 c_parse_pragma_ivdep (c_parser *parser)
10865 c_parser_consume_pragma (parser);
10866 c_parser_skip_to_pragma_eol (parser);
10867 return true;
10870 /* Parse a pragma GCC unroll. */
10872 static unsigned short
10873 c_parser_pragma_unroll (c_parser *parser)
10875 unsigned short unroll;
10876 c_parser_consume_pragma (parser);
10877 location_t location = c_parser_peek_token (parser)->location;
10878 tree expr = c_parser_expr_no_commas (parser, NULL).value;
10879 mark_exp_read (expr);
10880 expr = c_fully_fold (expr, false, NULL);
10881 HOST_WIDE_INT lunroll = 0;
10882 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
10883 || TREE_CODE (expr) != INTEGER_CST
10884 || (lunroll = tree_to_shwi (expr)) < 0
10885 || lunroll >= USHRT_MAX)
10887 error_at (location, "%<#pragma GCC unroll%> requires an"
10888 " assignment-expression that evaluates to a non-negative"
10889 " integral constant less than %u", USHRT_MAX);
10890 unroll = 0;
10892 else
10894 unroll = (unsigned short)lunroll;
10895 if (unroll == 0)
10896 unroll = 1;
10899 c_parser_skip_to_pragma_eol (parser);
10900 return unroll;
10903 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10904 should be considered, statements. ALLOW_STMT is true if we're within
10905 the context of a function and such pragmas are to be allowed. Returns
10906 true if we actually parsed such a pragma. */
10908 static bool
10909 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10911 unsigned int id;
10912 const char *construct = NULL;
10914 id = c_parser_peek_token (parser)->pragma_kind;
10915 gcc_assert (id != PRAGMA_NONE);
10917 switch (id)
10919 case PRAGMA_OACC_DECLARE:
10920 c_parser_oacc_declare (parser);
10921 return false;
10923 case PRAGMA_OACC_ENTER_DATA:
10924 if (context != pragma_compound)
10926 construct = "acc enter data";
10927 in_compound:
10928 if (context == pragma_stmt)
10930 error_at (c_parser_peek_token (parser)->location,
10931 "%<#pragma %s%> may only be used in compound "
10932 "statements", construct);
10933 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10934 return false;
10936 goto bad_stmt;
10938 c_parser_oacc_enter_exit_data (parser, true);
10939 return false;
10941 case PRAGMA_OACC_EXIT_DATA:
10942 if (context != pragma_compound)
10944 construct = "acc exit data";
10945 goto in_compound;
10947 c_parser_oacc_enter_exit_data (parser, false);
10948 return false;
10950 case PRAGMA_OACC_ROUTINE:
10951 if (context != pragma_external)
10953 error_at (c_parser_peek_token (parser)->location,
10954 "%<#pragma acc routine%> must be at file scope");
10955 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10956 return false;
10958 c_parser_oacc_routine (parser, context);
10959 return false;
10961 case PRAGMA_OACC_UPDATE:
10962 if (context != pragma_compound)
10964 construct = "acc update";
10965 goto in_compound;
10967 c_parser_oacc_update (parser);
10968 return false;
10970 case PRAGMA_OMP_BARRIER:
10971 if (context != pragma_compound)
10973 construct = "omp barrier";
10974 goto in_compound;
10976 c_parser_omp_barrier (parser);
10977 return false;
10979 case PRAGMA_OMP_FLUSH:
10980 if (context != pragma_compound)
10982 construct = "omp flush";
10983 goto in_compound;
10985 c_parser_omp_flush (parser);
10986 return false;
10988 case PRAGMA_OMP_TASKWAIT:
10989 if (context != pragma_compound)
10991 construct = "omp taskwait";
10992 goto in_compound;
10994 c_parser_omp_taskwait (parser);
10995 return false;
10997 case PRAGMA_OMP_TASKYIELD:
10998 if (context != pragma_compound)
11000 construct = "omp taskyield";
11001 goto in_compound;
11003 c_parser_omp_taskyield (parser);
11004 return false;
11006 case PRAGMA_OMP_CANCEL:
11007 if (context != pragma_compound)
11009 construct = "omp cancel";
11010 goto in_compound;
11012 c_parser_omp_cancel (parser);
11013 return false;
11015 case PRAGMA_OMP_CANCELLATION_POINT:
11016 c_parser_omp_cancellation_point (parser, context);
11017 return false;
11019 case PRAGMA_OMP_THREADPRIVATE:
11020 c_parser_omp_threadprivate (parser);
11021 return false;
11023 case PRAGMA_OMP_TARGET:
11024 return c_parser_omp_target (parser, context, if_p);
11026 case PRAGMA_OMP_END_DECLARE_TARGET:
11027 c_parser_omp_end_declare_target (parser);
11028 return false;
11030 case PRAGMA_OMP_SECTION:
11031 error_at (c_parser_peek_token (parser)->location,
11032 "%<#pragma omp section%> may only be used in "
11033 "%<#pragma omp sections%> construct");
11034 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11035 return false;
11037 case PRAGMA_OMP_DECLARE:
11038 c_parser_omp_declare (parser, context);
11039 return false;
11041 case PRAGMA_OMP_ORDERED:
11042 return c_parser_omp_ordered (parser, context, if_p);
11044 case PRAGMA_IVDEP:
11046 const bool ivdep = c_parse_pragma_ivdep (parser);
11047 unsigned short unroll;
11048 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11049 unroll = c_parser_pragma_unroll (parser);
11050 else
11051 unroll = 0;
11052 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11053 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11054 && !c_parser_next_token_is_keyword (parser, RID_DO))
11056 c_parser_error (parser, "for, while or do statement expected");
11057 return false;
11059 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11060 c_parser_for_statement (parser, ivdep, unroll, if_p);
11061 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11062 c_parser_while_statement (parser, ivdep, unroll, if_p);
11063 else
11064 c_parser_do_statement (parser, ivdep, unroll);
11066 return false;
11068 case PRAGMA_UNROLL:
11070 unsigned short unroll = c_parser_pragma_unroll (parser);
11071 bool ivdep;
11072 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11073 ivdep = c_parse_pragma_ivdep (parser);
11074 else
11075 ivdep = false;
11076 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11077 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11078 && !c_parser_next_token_is_keyword (parser, RID_DO))
11080 c_parser_error (parser, "for, while or do statement expected");
11081 return false;
11083 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11084 c_parser_for_statement (parser, ivdep, unroll, if_p);
11085 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11086 c_parser_while_statement (parser, ivdep, unroll, if_p);
11087 else
11088 c_parser_do_statement (parser, ivdep, unroll);
11090 return false;
11092 case PRAGMA_GCC_PCH_PREPROCESS:
11093 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11094 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11095 return false;
11097 case PRAGMA_OACC_WAIT:
11098 if (context != pragma_compound)
11100 construct = "acc wait";
11101 goto in_compound;
11103 /* FALL THROUGH. */
11105 default:
11106 if (id < PRAGMA_FIRST_EXTERNAL)
11108 if (context != pragma_stmt && context != pragma_compound)
11110 bad_stmt:
11111 c_parser_error (parser, "expected declaration specifiers");
11112 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11113 return false;
11115 c_parser_omp_construct (parser, if_p);
11116 return true;
11118 break;
11121 c_parser_consume_pragma (parser);
11122 c_invoke_pragma_handler (id);
11124 /* Skip to EOL, but suppress any error message. Those will have been
11125 generated by the handler routine through calling error, as opposed
11126 to calling c_parser_error. */
11127 parser->error = true;
11128 c_parser_skip_to_pragma_eol (parser);
11130 return false;
11133 /* The interface the pragma parsers have to the lexer. */
11135 enum cpp_ttype
11136 pragma_lex (tree *value, location_t *loc)
11138 c_token *tok = c_parser_peek_token (the_parser);
11139 enum cpp_ttype ret = tok->type;
11141 *value = tok->value;
11142 if (loc)
11143 *loc = tok->location;
11145 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11146 ret = CPP_EOF;
11147 else
11149 if (ret == CPP_KEYWORD)
11150 ret = CPP_NAME;
11151 c_parser_consume_token (the_parser);
11154 return ret;
11157 static void
11158 c_parser_pragma_pch_preprocess (c_parser *parser)
11160 tree name = NULL;
11162 c_parser_consume_pragma (parser);
11163 if (c_parser_next_token_is (parser, CPP_STRING))
11165 name = c_parser_peek_token (parser)->value;
11166 c_parser_consume_token (parser);
11168 else
11169 c_parser_error (parser, "expected string literal");
11170 c_parser_skip_to_pragma_eol (parser);
11172 if (name)
11173 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11176 /* OpenACC and OpenMP parsing routines. */
11178 /* Returns name of the next clause.
11179 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11180 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11181 returned and the token is consumed. */
11183 static pragma_omp_clause
11184 c_parser_omp_clause_name (c_parser *parser)
11186 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11188 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11189 result = PRAGMA_OACC_CLAUSE_AUTO;
11190 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11191 result = PRAGMA_OMP_CLAUSE_IF;
11192 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11193 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11194 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11195 result = PRAGMA_OMP_CLAUSE_FOR;
11196 else if (c_parser_next_token_is (parser, CPP_NAME))
11198 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11200 switch (p[0])
11202 case 'a':
11203 if (!strcmp ("aligned", p))
11204 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11205 else if (!strcmp ("async", p))
11206 result = PRAGMA_OACC_CLAUSE_ASYNC;
11207 break;
11208 case 'c':
11209 if (!strcmp ("collapse", p))
11210 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11211 else if (!strcmp ("copy", p))
11212 result = PRAGMA_OACC_CLAUSE_COPY;
11213 else if (!strcmp ("copyin", p))
11214 result = PRAGMA_OMP_CLAUSE_COPYIN;
11215 else if (!strcmp ("copyout", p))
11216 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11217 else if (!strcmp ("copyprivate", p))
11218 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11219 else if (!strcmp ("create", p))
11220 result = PRAGMA_OACC_CLAUSE_CREATE;
11221 break;
11222 case 'd':
11223 if (!strcmp ("defaultmap", p))
11224 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11225 else if (!strcmp ("delete", p))
11226 result = PRAGMA_OACC_CLAUSE_DELETE;
11227 else if (!strcmp ("depend", p))
11228 result = PRAGMA_OMP_CLAUSE_DEPEND;
11229 else if (!strcmp ("device", p))
11230 result = PRAGMA_OMP_CLAUSE_DEVICE;
11231 else if (!strcmp ("deviceptr", p))
11232 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11233 else if (!strcmp ("device_resident", p))
11234 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11235 else if (!strcmp ("dist_schedule", p))
11236 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11237 break;
11238 case 'f':
11239 if (!strcmp ("final", p))
11240 result = PRAGMA_OMP_CLAUSE_FINAL;
11241 else if (!strcmp ("firstprivate", p))
11242 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11243 else if (!strcmp ("from", p))
11244 result = PRAGMA_OMP_CLAUSE_FROM;
11245 break;
11246 case 'g':
11247 if (!strcmp ("gang", p))
11248 result = PRAGMA_OACC_CLAUSE_GANG;
11249 else if (!strcmp ("grainsize", p))
11250 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11251 break;
11252 case 'h':
11253 if (!strcmp ("hint", p))
11254 result = PRAGMA_OMP_CLAUSE_HINT;
11255 else if (!strcmp ("host", p))
11256 result = PRAGMA_OACC_CLAUSE_HOST;
11257 break;
11258 case 'i':
11259 if (!strcmp ("inbranch", p))
11260 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11261 else if (!strcmp ("independent", p))
11262 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11263 else if (!strcmp ("is_device_ptr", p))
11264 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11265 break;
11266 case 'l':
11267 if (!strcmp ("lastprivate", p))
11268 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11269 else if (!strcmp ("linear", p))
11270 result = PRAGMA_OMP_CLAUSE_LINEAR;
11271 else if (!strcmp ("link", p))
11272 result = PRAGMA_OMP_CLAUSE_LINK;
11273 break;
11274 case 'm':
11275 if (!strcmp ("map", p))
11276 result = PRAGMA_OMP_CLAUSE_MAP;
11277 else if (!strcmp ("mergeable", p))
11278 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11279 break;
11280 case 'n':
11281 if (!strcmp ("nogroup", p))
11282 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11283 else if (!strcmp ("notinbranch", p))
11284 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11285 else if (!strcmp ("nowait", p))
11286 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11287 else if (!strcmp ("num_gangs", p))
11288 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11289 else if (!strcmp ("num_tasks", p))
11290 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11291 else if (!strcmp ("num_teams", p))
11292 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11293 else if (!strcmp ("num_threads", p))
11294 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11295 else if (!strcmp ("num_workers", p))
11296 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11297 break;
11298 case 'o':
11299 if (!strcmp ("ordered", p))
11300 result = PRAGMA_OMP_CLAUSE_ORDERED;
11301 break;
11302 case 'p':
11303 if (!strcmp ("parallel", p))
11304 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11305 else if (!strcmp ("present", p))
11306 result = PRAGMA_OACC_CLAUSE_PRESENT;
11307 else if (!strcmp ("present_or_copy", p)
11308 || !strcmp ("pcopy", p))
11309 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
11310 else if (!strcmp ("present_or_copyin", p)
11311 || !strcmp ("pcopyin", p))
11312 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
11313 else if (!strcmp ("present_or_copyout", p)
11314 || !strcmp ("pcopyout", p))
11315 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
11316 else if (!strcmp ("present_or_create", p)
11317 || !strcmp ("pcreate", p))
11318 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
11319 else if (!strcmp ("priority", p))
11320 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11321 else if (!strcmp ("private", p))
11322 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11323 else if (!strcmp ("proc_bind", p))
11324 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11325 break;
11326 case 'r':
11327 if (!strcmp ("reduction", p))
11328 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11329 break;
11330 case 's':
11331 if (!strcmp ("safelen", p))
11332 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11333 else if (!strcmp ("schedule", p))
11334 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11335 else if (!strcmp ("sections", p))
11336 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11337 else if (!strcmp ("seq", p))
11338 result = PRAGMA_OACC_CLAUSE_SEQ;
11339 else if (!strcmp ("shared", p))
11340 result = PRAGMA_OMP_CLAUSE_SHARED;
11341 else if (!strcmp ("simd", p))
11342 result = PRAGMA_OMP_CLAUSE_SIMD;
11343 else if (!strcmp ("simdlen", p))
11344 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11345 else if (!strcmp ("self", p))
11346 result = PRAGMA_OACC_CLAUSE_SELF;
11347 break;
11348 case 't':
11349 if (!strcmp ("taskgroup", p))
11350 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11351 else if (!strcmp ("thread_limit", p))
11352 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11353 else if (!strcmp ("threads", p))
11354 result = PRAGMA_OMP_CLAUSE_THREADS;
11355 else if (!strcmp ("tile", p))
11356 result = PRAGMA_OACC_CLAUSE_TILE;
11357 else if (!strcmp ("to", p))
11358 result = PRAGMA_OMP_CLAUSE_TO;
11359 break;
11360 case 'u':
11361 if (!strcmp ("uniform", p))
11362 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11363 else if (!strcmp ("untied", p))
11364 result = PRAGMA_OMP_CLAUSE_UNTIED;
11365 else if (!strcmp ("use_device", p))
11366 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11367 else if (!strcmp ("use_device_ptr", p))
11368 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11369 break;
11370 case 'v':
11371 if (!strcmp ("vector", p))
11372 result = PRAGMA_OACC_CLAUSE_VECTOR;
11373 else if (!strcmp ("vector_length", p))
11374 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11375 break;
11376 case 'w':
11377 if (!strcmp ("wait", p))
11378 result = PRAGMA_OACC_CLAUSE_WAIT;
11379 else if (!strcmp ("worker", p))
11380 result = PRAGMA_OACC_CLAUSE_WORKER;
11381 break;
11385 if (result != PRAGMA_OMP_CLAUSE_NONE)
11386 c_parser_consume_token (parser);
11388 return result;
11391 /* Validate that a clause of the given type does not already exist. */
11393 static void
11394 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11395 const char *name)
11397 tree c;
11399 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11400 if (OMP_CLAUSE_CODE (c) == code)
11402 location_t loc = OMP_CLAUSE_LOCATION (c);
11403 error_at (loc, "too many %qs clauses", name);
11404 break;
11408 /* OpenACC 2.0
11409 Parse wait clause or wait directive parameters. */
11411 static tree
11412 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11414 vec<tree, va_gc> *args;
11415 tree t, args_tree;
11417 matching_parens parens;
11418 if (!parens.require_open (parser))
11419 return list;
11421 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11423 if (args->length () == 0)
11425 c_parser_error (parser, "expected integer expression before ')'");
11426 release_tree_vector (args);
11427 return list;
11430 args_tree = build_tree_list_vec (args);
11432 for (t = args_tree; t; t = TREE_CHAIN (t))
11434 tree targ = TREE_VALUE (t);
11436 if (targ != error_mark_node)
11438 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11440 c_parser_error (parser, "expression must be integral");
11441 targ = error_mark_node;
11443 else
11445 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11447 OMP_CLAUSE_DECL (c) = targ;
11448 OMP_CLAUSE_CHAIN (c) = list;
11449 list = c;
11454 release_tree_vector (args);
11455 parens.require_close (parser);
11456 return list;
11459 /* OpenACC 2.0, OpenMP 2.5:
11460 variable-list:
11461 identifier
11462 variable-list , identifier
11464 If KIND is nonzero, create the appropriate node and install the
11465 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11466 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11468 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11469 return the list created. */
11471 static tree
11472 c_parser_omp_variable_list (c_parser *parser,
11473 location_t clause_loc,
11474 enum omp_clause_code kind, tree list)
11476 if (c_parser_next_token_is_not (parser, CPP_NAME)
11477 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11478 c_parser_error (parser, "expected identifier");
11480 while (c_parser_next_token_is (parser, CPP_NAME)
11481 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11483 tree t = lookup_name (c_parser_peek_token (parser)->value);
11485 if (t == NULL_TREE)
11487 undeclared_variable (c_parser_peek_token (parser)->location,
11488 c_parser_peek_token (parser)->value);
11489 t = error_mark_node;
11492 c_parser_consume_token (parser);
11494 if (t == error_mark_node)
11496 else if (kind != 0)
11498 switch (kind)
11500 case OMP_CLAUSE__CACHE_:
11501 /* The OpenACC cache directive explicitly only allows "array
11502 elements or subarrays". */
11503 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11505 c_parser_error (parser, "expected %<[%>");
11506 t = error_mark_node;
11507 break;
11509 /* FALLTHROUGH */
11510 case OMP_CLAUSE_MAP:
11511 case OMP_CLAUSE_FROM:
11512 case OMP_CLAUSE_TO:
11513 while (c_parser_next_token_is (parser, CPP_DOT))
11515 location_t op_loc = c_parser_peek_token (parser)->location;
11516 c_parser_consume_token (parser);
11517 if (!c_parser_next_token_is (parser, CPP_NAME))
11519 c_parser_error (parser, "expected identifier");
11520 t = error_mark_node;
11521 break;
11524 c_token *comp_tok = c_parser_peek_token (parser);
11525 tree ident = comp_tok->value;
11526 location_t comp_loc = comp_tok->location;
11527 c_parser_consume_token (parser);
11528 t = build_component_ref (op_loc, t, ident, comp_loc);
11530 /* FALLTHROUGH */
11531 case OMP_CLAUSE_DEPEND:
11532 case OMP_CLAUSE_REDUCTION:
11533 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11535 tree low_bound = NULL_TREE, length = NULL_TREE;
11537 c_parser_consume_token (parser);
11538 if (!c_parser_next_token_is (parser, CPP_COLON))
11540 location_t expr_loc
11541 = c_parser_peek_token (parser)->location;
11542 c_expr expr = c_parser_expression (parser);
11543 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11544 false, true);
11545 low_bound = expr.value;
11547 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11548 length = integer_one_node;
11549 else
11551 /* Look for `:'. */
11552 if (!c_parser_require (parser, CPP_COLON,
11553 "expected %<:%>"))
11555 t = error_mark_node;
11556 break;
11558 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11560 location_t expr_loc
11561 = c_parser_peek_token (parser)->location;
11562 c_expr expr = c_parser_expression (parser);
11563 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11564 false, true);
11565 length = expr.value;
11568 /* Look for the closing `]'. */
11569 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11570 "expected %<]%>"))
11572 t = error_mark_node;
11573 break;
11576 t = tree_cons (low_bound, length, t);
11578 break;
11579 default:
11580 break;
11583 if (t != error_mark_node)
11585 tree u = build_omp_clause (clause_loc, kind);
11586 OMP_CLAUSE_DECL (u) = t;
11587 OMP_CLAUSE_CHAIN (u) = list;
11588 list = u;
11591 else
11592 list = tree_cons (t, NULL_TREE, list);
11594 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11595 break;
11597 c_parser_consume_token (parser);
11600 return list;
11603 /* Similarly, but expect leading and trailing parenthesis. This is a very
11604 common case for OpenACC and OpenMP clauses. */
11606 static tree
11607 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11608 tree list)
11610 /* The clauses location. */
11611 location_t loc = c_parser_peek_token (parser)->location;
11613 matching_parens parens;
11614 if (parens.require_open (parser))
11616 list = c_parser_omp_variable_list (parser, loc, kind, list);
11617 parens.skip_until_found_close (parser);
11619 return list;
11622 /* OpenACC 2.0:
11623 copy ( variable-list )
11624 copyin ( variable-list )
11625 copyout ( variable-list )
11626 create ( variable-list )
11627 delete ( variable-list )
11628 present ( variable-list )
11629 present_or_copy ( variable-list )
11630 pcopy ( variable-list )
11631 present_or_copyin ( variable-list )
11632 pcopyin ( variable-list )
11633 present_or_copyout ( variable-list )
11634 pcopyout ( variable-list )
11635 present_or_create ( variable-list )
11636 pcreate ( variable-list ) */
11638 static tree
11639 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11640 tree list)
11642 enum gomp_map_kind kind;
11643 switch (c_kind)
11645 case PRAGMA_OACC_CLAUSE_COPY:
11646 kind = GOMP_MAP_FORCE_TOFROM;
11647 break;
11648 case PRAGMA_OACC_CLAUSE_COPYIN:
11649 kind = GOMP_MAP_FORCE_TO;
11650 break;
11651 case PRAGMA_OACC_CLAUSE_COPYOUT:
11652 kind = GOMP_MAP_FORCE_FROM;
11653 break;
11654 case PRAGMA_OACC_CLAUSE_CREATE:
11655 kind = GOMP_MAP_FORCE_ALLOC;
11656 break;
11657 case PRAGMA_OACC_CLAUSE_DELETE:
11658 kind = GOMP_MAP_DELETE;
11659 break;
11660 case PRAGMA_OACC_CLAUSE_DEVICE:
11661 kind = GOMP_MAP_FORCE_TO;
11662 break;
11663 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11664 kind = GOMP_MAP_DEVICE_RESIDENT;
11665 break;
11666 case PRAGMA_OACC_CLAUSE_HOST:
11667 case PRAGMA_OACC_CLAUSE_SELF:
11668 kind = GOMP_MAP_FORCE_FROM;
11669 break;
11670 case PRAGMA_OACC_CLAUSE_LINK:
11671 kind = GOMP_MAP_LINK;
11672 break;
11673 case PRAGMA_OACC_CLAUSE_PRESENT:
11674 kind = GOMP_MAP_FORCE_PRESENT;
11675 break;
11676 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11677 kind = GOMP_MAP_TOFROM;
11678 break;
11679 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11680 kind = GOMP_MAP_TO;
11681 break;
11682 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11683 kind = GOMP_MAP_FROM;
11684 break;
11685 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11686 kind = GOMP_MAP_ALLOC;
11687 break;
11688 default:
11689 gcc_unreachable ();
11691 tree nl, c;
11692 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11694 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11695 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11697 return nl;
11700 /* OpenACC 2.0:
11701 deviceptr ( variable-list ) */
11703 static tree
11704 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11706 location_t loc = c_parser_peek_token (parser)->location;
11707 tree vars, t;
11709 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11710 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11711 variable-list must only allow for pointer variables. */
11712 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11713 for (t = vars; t && t; t = TREE_CHAIN (t))
11715 tree v = TREE_PURPOSE (t);
11717 /* FIXME diagnostics: Ideally we should keep individual
11718 locations for all the variables in the var list to make the
11719 following errors more precise. Perhaps
11720 c_parser_omp_var_list_parens() should construct a list of
11721 locations to go along with the var list. */
11723 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11724 error_at (loc, "%qD is not a variable", v);
11725 else if (TREE_TYPE (v) == error_mark_node)
11727 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11728 error_at (loc, "%qD is not a pointer variable", v);
11730 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11731 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11732 OMP_CLAUSE_DECL (u) = v;
11733 OMP_CLAUSE_CHAIN (u) = list;
11734 list = u;
11737 return list;
11740 /* OpenACC 2.0, OpenMP 3.0:
11741 collapse ( constant-expression ) */
11743 static tree
11744 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11746 tree c, num = error_mark_node;
11747 HOST_WIDE_INT n;
11748 location_t loc;
11750 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11751 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11753 loc = c_parser_peek_token (parser)->location;
11754 matching_parens parens;
11755 if (parens.require_open (parser))
11757 num = c_parser_expr_no_commas (parser, NULL).value;
11758 parens.skip_until_found_close (parser);
11760 if (num == error_mark_node)
11761 return list;
11762 mark_exp_read (num);
11763 num = c_fully_fold (num, false, NULL);
11764 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11765 || !tree_fits_shwi_p (num)
11766 || (n = tree_to_shwi (num)) <= 0
11767 || (int) n != n)
11769 error_at (loc,
11770 "collapse argument needs positive constant integer expression");
11771 return list;
11773 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11774 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11775 OMP_CLAUSE_CHAIN (c) = list;
11776 return c;
11779 /* OpenMP 2.5:
11780 copyin ( variable-list ) */
11782 static tree
11783 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11785 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11788 /* OpenMP 2.5:
11789 copyprivate ( variable-list ) */
11791 static tree
11792 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11794 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11797 /* OpenMP 2.5:
11798 default ( none | shared )
11800 OpenACC:
11801 default ( none | present ) */
11803 static tree
11804 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11806 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11807 location_t loc = c_parser_peek_token (parser)->location;
11808 tree c;
11810 matching_parens parens;
11811 if (!parens.require_open (parser))
11812 return list;
11813 if (c_parser_next_token_is (parser, CPP_NAME))
11815 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11817 switch (p[0])
11819 case 'n':
11820 if (strcmp ("none", p) != 0)
11821 goto invalid_kind;
11822 kind = OMP_CLAUSE_DEFAULT_NONE;
11823 break;
11825 case 'p':
11826 if (strcmp ("present", p) != 0 || !is_oacc)
11827 goto invalid_kind;
11828 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11829 break;
11831 case 's':
11832 if (strcmp ("shared", p) != 0 || is_oacc)
11833 goto invalid_kind;
11834 kind = OMP_CLAUSE_DEFAULT_SHARED;
11835 break;
11837 default:
11838 goto invalid_kind;
11841 c_parser_consume_token (parser);
11843 else
11845 invalid_kind:
11846 if (is_oacc)
11847 c_parser_error (parser, "expected %<none%> or %<present%>");
11848 else
11849 c_parser_error (parser, "expected %<none%> or %<shared%>");
11851 parens.skip_until_found_close (parser);
11853 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11854 return list;
11856 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11857 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11858 OMP_CLAUSE_CHAIN (c) = list;
11859 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11861 return c;
11864 /* OpenMP 2.5:
11865 firstprivate ( variable-list ) */
11867 static tree
11868 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11870 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11873 /* OpenMP 3.1:
11874 final ( expression ) */
11876 static tree
11877 c_parser_omp_clause_final (c_parser *parser, tree list)
11879 location_t loc = c_parser_peek_token (parser)->location;
11880 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11882 tree t = c_parser_paren_condition (parser);
11883 tree c;
11885 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11887 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11888 OMP_CLAUSE_FINAL_EXPR (c) = t;
11889 OMP_CLAUSE_CHAIN (c) = list;
11890 list = c;
11892 else
11893 c_parser_error (parser, "expected %<(%>");
11895 return list;
11898 /* OpenACC, OpenMP 2.5:
11899 if ( expression )
11901 OpenMP 4.5:
11902 if ( directive-name-modifier : expression )
11904 directive-name-modifier:
11905 parallel | task | taskloop | target data | target | target update
11906 | target enter data | target exit data */
11908 static tree
11909 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11911 location_t location = c_parser_peek_token (parser)->location;
11912 enum tree_code if_modifier = ERROR_MARK;
11914 matching_parens parens;
11915 if (!parens.require_open (parser))
11916 return list;
11918 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11920 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11921 int n = 2;
11922 if (strcmp (p, "parallel") == 0)
11923 if_modifier = OMP_PARALLEL;
11924 else if (strcmp (p, "task") == 0)
11925 if_modifier = OMP_TASK;
11926 else if (strcmp (p, "taskloop") == 0)
11927 if_modifier = OMP_TASKLOOP;
11928 else if (strcmp (p, "target") == 0)
11930 if_modifier = OMP_TARGET;
11931 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11933 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11934 if (strcmp ("data", p) == 0)
11935 if_modifier = OMP_TARGET_DATA;
11936 else if (strcmp ("update", p) == 0)
11937 if_modifier = OMP_TARGET_UPDATE;
11938 else if (strcmp ("enter", p) == 0)
11939 if_modifier = OMP_TARGET_ENTER_DATA;
11940 else if (strcmp ("exit", p) == 0)
11941 if_modifier = OMP_TARGET_EXIT_DATA;
11942 if (if_modifier != OMP_TARGET)
11944 n = 3;
11945 c_parser_consume_token (parser);
11947 else
11949 location_t loc = c_parser_peek_2nd_token (parser)->location;
11950 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11951 "or %<exit%>");
11952 if_modifier = ERROR_MARK;
11954 if (if_modifier == OMP_TARGET_ENTER_DATA
11955 || if_modifier == OMP_TARGET_EXIT_DATA)
11957 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11959 p = IDENTIFIER_POINTER
11960 (c_parser_peek_2nd_token (parser)->value);
11961 if (strcmp ("data", p) == 0)
11962 n = 4;
11964 if (n == 4)
11965 c_parser_consume_token (parser);
11966 else
11968 location_t loc
11969 = c_parser_peek_2nd_token (parser)->location;
11970 error_at (loc, "expected %<data%>");
11971 if_modifier = ERROR_MARK;
11976 if (if_modifier != ERROR_MARK)
11978 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11980 c_parser_consume_token (parser);
11981 c_parser_consume_token (parser);
11983 else
11985 if (n > 2)
11987 location_t loc = c_parser_peek_2nd_token (parser)->location;
11988 error_at (loc, "expected %<:%>");
11990 if_modifier = ERROR_MARK;
11995 tree t = c_parser_condition (parser), c;
11996 parens.skip_until_found_close (parser);
11998 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11999 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12001 if (if_modifier != ERROR_MARK
12002 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12004 const char *p = NULL;
12005 switch (if_modifier)
12007 case OMP_PARALLEL: p = "parallel"; break;
12008 case OMP_TASK: p = "task"; break;
12009 case OMP_TASKLOOP: p = "taskloop"; break;
12010 case OMP_TARGET_DATA: p = "target data"; break;
12011 case OMP_TARGET: p = "target"; break;
12012 case OMP_TARGET_UPDATE: p = "target update"; break;
12013 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12014 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12015 default: gcc_unreachable ();
12017 error_at (location, "too many %<if%> clauses with %qs modifier",
12019 return list;
12021 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12023 if (!is_omp)
12024 error_at (location, "too many %<if%> clauses");
12025 else
12026 error_at (location, "too many %<if%> clauses without modifier");
12027 return list;
12029 else if (if_modifier == ERROR_MARK
12030 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12032 error_at (location, "if any %<if%> clause has modifier, then all "
12033 "%<if%> clauses have to use modifier");
12034 return list;
12038 c = build_omp_clause (location, OMP_CLAUSE_IF);
12039 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12040 OMP_CLAUSE_IF_EXPR (c) = t;
12041 OMP_CLAUSE_CHAIN (c) = list;
12042 return c;
12045 /* OpenMP 2.5:
12046 lastprivate ( variable-list ) */
12048 static tree
12049 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12051 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12054 /* OpenMP 3.1:
12055 mergeable */
12057 static tree
12058 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12060 tree c;
12062 /* FIXME: Should we allow duplicates? */
12063 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12065 c = build_omp_clause (c_parser_peek_token (parser)->location,
12066 OMP_CLAUSE_MERGEABLE);
12067 OMP_CLAUSE_CHAIN (c) = list;
12069 return c;
12072 /* OpenMP 2.5:
12073 nowait */
12075 static tree
12076 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12078 tree c;
12079 location_t loc = c_parser_peek_token (parser)->location;
12081 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12083 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12084 OMP_CLAUSE_CHAIN (c) = list;
12085 return c;
12088 /* OpenMP 2.5:
12089 num_threads ( expression ) */
12091 static tree
12092 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12094 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12095 matching_parens parens;
12096 if (parens.require_open (parser))
12098 location_t expr_loc = c_parser_peek_token (parser)->location;
12099 c_expr expr = c_parser_expression (parser);
12100 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12101 tree c, t = expr.value;
12102 t = c_fully_fold (t, false, NULL);
12104 parens.skip_until_found_close (parser);
12106 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12108 c_parser_error (parser, "expected integer expression");
12109 return list;
12112 /* Attempt to statically determine when the number isn't positive. */
12113 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12114 build_int_cst (TREE_TYPE (t), 0));
12115 protected_set_expr_location (c, expr_loc);
12116 if (c == boolean_true_node)
12118 warning_at (expr_loc, 0,
12119 "%<num_threads%> value must be positive");
12120 t = integer_one_node;
12123 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12125 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12126 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12127 OMP_CLAUSE_CHAIN (c) = list;
12128 list = c;
12131 return list;
12134 /* OpenMP 4.5:
12135 num_tasks ( expression ) */
12137 static tree
12138 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12140 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12141 matching_parens parens;
12142 if (parens.require_open (parser))
12144 location_t expr_loc = c_parser_peek_token (parser)->location;
12145 c_expr expr = c_parser_expression (parser);
12146 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12147 tree c, t = expr.value;
12148 t = c_fully_fold (t, false, NULL);
12150 parens.skip_until_found_close (parser);
12152 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12154 c_parser_error (parser, "expected integer expression");
12155 return list;
12158 /* Attempt to statically determine when the number isn't positive. */
12159 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12160 build_int_cst (TREE_TYPE (t), 0));
12161 if (CAN_HAVE_LOCATION_P (c))
12162 SET_EXPR_LOCATION (c, expr_loc);
12163 if (c == boolean_true_node)
12165 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12166 t = integer_one_node;
12169 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12171 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12172 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12173 OMP_CLAUSE_CHAIN (c) = list;
12174 list = c;
12177 return list;
12180 /* OpenMP 4.5:
12181 grainsize ( expression ) */
12183 static tree
12184 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12186 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12187 matching_parens parens;
12188 if (parens.require_open (parser))
12190 location_t expr_loc = c_parser_peek_token (parser)->location;
12191 c_expr expr = c_parser_expression (parser);
12192 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12193 tree c, t = expr.value;
12194 t = c_fully_fold (t, false, NULL);
12196 parens.skip_until_found_close (parser);
12198 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12200 c_parser_error (parser, "expected integer expression");
12201 return list;
12204 /* Attempt to statically determine when the number isn't positive. */
12205 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12206 build_int_cst (TREE_TYPE (t), 0));
12207 if (CAN_HAVE_LOCATION_P (c))
12208 SET_EXPR_LOCATION (c, expr_loc);
12209 if (c == boolean_true_node)
12211 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12212 t = integer_one_node;
12215 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12217 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12218 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12219 OMP_CLAUSE_CHAIN (c) = list;
12220 list = c;
12223 return list;
12226 /* OpenMP 4.5:
12227 priority ( expression ) */
12229 static tree
12230 c_parser_omp_clause_priority (c_parser *parser, tree list)
12232 location_t priority_loc = c_parser_peek_token (parser)->location;
12233 matching_parens parens;
12234 if (parens.require_open (parser))
12236 location_t expr_loc = c_parser_peek_token (parser)->location;
12237 c_expr expr = c_parser_expression (parser);
12238 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12239 tree c, t = expr.value;
12240 t = c_fully_fold (t, false, NULL);
12242 parens.skip_until_found_close (parser);
12244 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12246 c_parser_error (parser, "expected integer expression");
12247 return list;
12250 /* Attempt to statically determine when the number isn't
12251 non-negative. */
12252 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12253 build_int_cst (TREE_TYPE (t), 0));
12254 if (CAN_HAVE_LOCATION_P (c))
12255 SET_EXPR_LOCATION (c, expr_loc);
12256 if (c == boolean_true_node)
12258 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12259 t = integer_one_node;
12262 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12264 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12265 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12266 OMP_CLAUSE_CHAIN (c) = list;
12267 list = c;
12270 return list;
12273 /* OpenMP 4.5:
12274 hint ( expression ) */
12276 static tree
12277 c_parser_omp_clause_hint (c_parser *parser, tree list)
12279 location_t hint_loc = c_parser_peek_token (parser)->location;
12280 matching_parens parens;
12281 if (parens.require_open (parser))
12283 location_t expr_loc = c_parser_peek_token (parser)->location;
12284 c_expr expr = c_parser_expression (parser);
12285 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12286 tree c, t = expr.value;
12287 t = c_fully_fold (t, false, NULL);
12289 parens.skip_until_found_close (parser);
12291 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12293 c_parser_error (parser, "expected integer expression");
12294 return list;
12297 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12299 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12300 OMP_CLAUSE_HINT_EXPR (c) = t;
12301 OMP_CLAUSE_CHAIN (c) = list;
12302 list = c;
12305 return list;
12308 /* OpenMP 4.5:
12309 defaultmap ( tofrom : scalar ) */
12311 static tree
12312 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12314 location_t loc = c_parser_peek_token (parser)->location;
12315 tree c;
12316 const char *p;
12318 matching_parens parens;
12319 if (!parens.require_open (parser))
12320 return list;
12321 if (!c_parser_next_token_is (parser, CPP_NAME))
12323 c_parser_error (parser, "expected %<tofrom%>");
12324 goto out_err;
12326 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12327 if (strcmp (p, "tofrom") != 0)
12329 c_parser_error (parser, "expected %<tofrom%>");
12330 goto out_err;
12332 c_parser_consume_token (parser);
12333 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12334 goto out_err;
12335 if (!c_parser_next_token_is (parser, CPP_NAME))
12337 c_parser_error (parser, "expected %<scalar%>");
12338 goto out_err;
12340 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12341 if (strcmp (p, "scalar") != 0)
12343 c_parser_error (parser, "expected %<scalar%>");
12344 goto out_err;
12346 c_parser_consume_token (parser);
12347 parens.skip_until_found_close (parser);
12348 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12349 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12350 OMP_CLAUSE_CHAIN (c) = list;
12351 return c;
12353 out_err:
12354 parens.skip_until_found_close (parser);
12355 return list;
12358 /* OpenACC 2.0:
12359 use_device ( variable-list )
12361 OpenMP 4.5:
12362 use_device_ptr ( variable-list ) */
12364 static tree
12365 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12367 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12368 list);
12371 /* OpenMP 4.5:
12372 is_device_ptr ( variable-list ) */
12374 static tree
12375 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12377 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12380 /* OpenACC:
12381 num_gangs ( expression )
12382 num_workers ( expression )
12383 vector_length ( expression ) */
12385 static tree
12386 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12387 tree list)
12389 location_t loc = c_parser_peek_token (parser)->location;
12391 matching_parens parens;
12392 if (!parens.require_open (parser))
12393 return list;
12395 location_t expr_loc = c_parser_peek_token (parser)->location;
12396 c_expr expr = c_parser_expression (parser);
12397 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12398 tree c, t = expr.value;
12399 t = c_fully_fold (t, false, NULL);
12401 parens.skip_until_found_close (parser);
12403 if (t == error_mark_node)
12404 return list;
12405 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12407 error_at (expr_loc, "%qs expression must be integral",
12408 omp_clause_code_name[code]);
12409 return list;
12412 /* Attempt to statically determine when the number isn't positive. */
12413 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12414 build_int_cst (TREE_TYPE (t), 0));
12415 protected_set_expr_location (c, expr_loc);
12416 if (c == boolean_true_node)
12418 warning_at (expr_loc, 0,
12419 "%qs value must be positive",
12420 omp_clause_code_name[code]);
12421 t = integer_one_node;
12424 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12426 c = build_omp_clause (loc, code);
12427 OMP_CLAUSE_OPERAND (c, 0) = t;
12428 OMP_CLAUSE_CHAIN (c) = list;
12429 return c;
12432 /* OpenACC:
12434 gang [( gang-arg-list )]
12435 worker [( [num:] int-expr )]
12436 vector [( [length:] int-expr )]
12438 where gang-arg is one of:
12440 [num:] int-expr
12441 static: size-expr
12443 and size-expr may be:
12446 int-expr
12449 static tree
12450 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12451 const char *str, tree list)
12453 const char *id = "num";
12454 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12455 location_t loc = c_parser_peek_token (parser)->location;
12457 if (kind == OMP_CLAUSE_VECTOR)
12458 id = "length";
12460 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12462 c_parser_consume_token (parser);
12466 c_token *next = c_parser_peek_token (parser);
12467 int idx = 0;
12469 /* Gang static argument. */
12470 if (kind == OMP_CLAUSE_GANG
12471 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12473 c_parser_consume_token (parser);
12475 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12476 goto cleanup_error;
12478 idx = 1;
12479 if (ops[idx] != NULL_TREE)
12481 c_parser_error (parser, "too many %<static%> arguments");
12482 goto cleanup_error;
12485 /* Check for the '*' argument. */
12486 if (c_parser_next_token_is (parser, CPP_MULT)
12487 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12488 || c_parser_peek_2nd_token (parser)->type
12489 == CPP_CLOSE_PAREN))
12491 c_parser_consume_token (parser);
12492 ops[idx] = integer_minus_one_node;
12494 if (c_parser_next_token_is (parser, CPP_COMMA))
12496 c_parser_consume_token (parser);
12497 continue;
12499 else
12500 break;
12503 /* Worker num: argument and vector length: arguments. */
12504 else if (c_parser_next_token_is (parser, CPP_NAME)
12505 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12506 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12508 c_parser_consume_token (parser); /* id */
12509 c_parser_consume_token (parser); /* ':' */
12512 /* Now collect the actual argument. */
12513 if (ops[idx] != NULL_TREE)
12515 c_parser_error (parser, "unexpected argument");
12516 goto cleanup_error;
12519 location_t expr_loc = c_parser_peek_token (parser)->location;
12520 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12521 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12522 tree expr = cexpr.value;
12523 if (expr == error_mark_node)
12524 goto cleanup_error;
12526 expr = c_fully_fold (expr, false, NULL);
12528 /* Attempt to statically determine when the number isn't a
12529 positive integer. */
12531 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12533 c_parser_error (parser, "expected integer expression");
12534 return list;
12537 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12538 build_int_cst (TREE_TYPE (expr), 0));
12539 if (c == boolean_true_node)
12541 warning_at (loc, 0,
12542 "%qs value must be positive", str);
12543 expr = integer_one_node;
12546 ops[idx] = expr;
12548 if (kind == OMP_CLAUSE_GANG
12549 && c_parser_next_token_is (parser, CPP_COMMA))
12551 c_parser_consume_token (parser);
12552 continue;
12554 break;
12556 while (1);
12558 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12559 goto cleanup_error;
12562 check_no_duplicate_clause (list, kind, str);
12564 c = build_omp_clause (loc, kind);
12566 if (ops[1])
12567 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12569 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12570 OMP_CLAUSE_CHAIN (c) = list;
12572 return c;
12574 cleanup_error:
12575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12576 return list;
12579 /* OpenACC:
12580 auto
12581 independent
12582 nohost
12583 seq */
12585 static tree
12586 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12587 tree list)
12589 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12591 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12592 OMP_CLAUSE_CHAIN (c) = list;
12594 return c;
12597 /* OpenACC:
12598 async [( int-expr )] */
12600 static tree
12601 c_parser_oacc_clause_async (c_parser *parser, tree list)
12603 tree c, t;
12604 location_t loc = c_parser_peek_token (parser)->location;
12606 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12608 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12610 c_parser_consume_token (parser);
12612 t = c_parser_expression (parser).value;
12613 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12614 c_parser_error (parser, "expected integer expression");
12615 else if (t == error_mark_node
12616 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12617 return list;
12619 else
12620 t = c_fully_fold (t, false, NULL);
12622 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12624 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12625 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12626 OMP_CLAUSE_CHAIN (c) = list;
12627 list = c;
12629 return list;
12632 /* OpenACC 2.0:
12633 tile ( size-expr-list ) */
12635 static tree
12636 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12638 tree c, expr = error_mark_node;
12639 location_t loc;
12640 tree tile = NULL_TREE;
12642 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12643 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12645 loc = c_parser_peek_token (parser)->location;
12646 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12647 return list;
12651 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12652 return list;
12654 if (c_parser_next_token_is (parser, CPP_MULT)
12655 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12656 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12658 c_parser_consume_token (parser);
12659 expr = integer_zero_node;
12661 else
12663 location_t expr_loc = c_parser_peek_token (parser)->location;
12664 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12665 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12666 expr = cexpr.value;
12668 if (expr == error_mark_node)
12670 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12671 "expected %<)%>");
12672 return list;
12675 expr = c_fully_fold (expr, false, NULL);
12677 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12678 || !tree_fits_shwi_p (expr)
12679 || tree_to_shwi (expr) <= 0)
12681 error_at (expr_loc, "%<tile%> argument needs positive"
12682 " integral constant");
12683 expr = integer_zero_node;
12687 tile = tree_cons (NULL_TREE, expr, tile);
12689 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12691 /* Consume the trailing ')'. */
12692 c_parser_consume_token (parser);
12694 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12695 tile = nreverse (tile);
12696 OMP_CLAUSE_TILE_LIST (c) = tile;
12697 OMP_CLAUSE_CHAIN (c) = list;
12698 return c;
12701 /* OpenACC:
12702 wait ( int-expr-list ) */
12704 static tree
12705 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12707 location_t clause_loc = c_parser_peek_token (parser)->location;
12709 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12710 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12712 return list;
12715 /* OpenMP 2.5:
12716 ordered
12718 OpenMP 4.5:
12719 ordered ( constant-expression ) */
12721 static tree
12722 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12724 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12726 tree c, num = NULL_TREE;
12727 HOST_WIDE_INT n;
12728 location_t loc = c_parser_peek_token (parser)->location;
12729 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12731 matching_parens parens;
12732 parens.consume_open (parser);
12733 num = c_parser_expr_no_commas (parser, NULL).value;
12734 parens.skip_until_found_close (parser);
12736 if (num == error_mark_node)
12737 return list;
12738 if (num)
12740 mark_exp_read (num);
12741 num = c_fully_fold (num, false, NULL);
12742 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12743 || !tree_fits_shwi_p (num)
12744 || (n = tree_to_shwi (num)) <= 0
12745 || (int) n != n)
12747 error_at (loc, "ordered argument needs positive "
12748 "constant integer expression");
12749 return list;
12752 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12753 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12754 OMP_CLAUSE_CHAIN (c) = list;
12755 return c;
12758 /* OpenMP 2.5:
12759 private ( variable-list ) */
12761 static tree
12762 c_parser_omp_clause_private (c_parser *parser, tree list)
12764 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12767 /* OpenMP 2.5:
12768 reduction ( reduction-operator : variable-list )
12770 reduction-operator:
12771 One of: + * - & ^ | && ||
12773 OpenMP 3.1:
12775 reduction-operator:
12776 One of: + * - & ^ | && || max min
12778 OpenMP 4.0:
12780 reduction-operator:
12781 One of: + * - & ^ | && ||
12782 identifier */
12784 static tree
12785 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12787 location_t clause_loc = c_parser_peek_token (parser)->location;
12788 matching_parens parens;
12789 if (parens.require_open (parser))
12791 enum tree_code code = ERROR_MARK;
12792 tree reduc_id = NULL_TREE;
12794 switch (c_parser_peek_token (parser)->type)
12796 case CPP_PLUS:
12797 code = PLUS_EXPR;
12798 break;
12799 case CPP_MULT:
12800 code = MULT_EXPR;
12801 break;
12802 case CPP_MINUS:
12803 code = MINUS_EXPR;
12804 break;
12805 case CPP_AND:
12806 code = BIT_AND_EXPR;
12807 break;
12808 case CPP_XOR:
12809 code = BIT_XOR_EXPR;
12810 break;
12811 case CPP_OR:
12812 code = BIT_IOR_EXPR;
12813 break;
12814 case CPP_AND_AND:
12815 code = TRUTH_ANDIF_EXPR;
12816 break;
12817 case CPP_OR_OR:
12818 code = TRUTH_ORIF_EXPR;
12819 break;
12820 case CPP_NAME:
12822 const char *p
12823 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12824 if (strcmp (p, "min") == 0)
12826 code = MIN_EXPR;
12827 break;
12829 if (strcmp (p, "max") == 0)
12831 code = MAX_EXPR;
12832 break;
12834 reduc_id = c_parser_peek_token (parser)->value;
12835 break;
12837 default:
12838 c_parser_error (parser,
12839 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12840 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12841 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12842 return list;
12844 c_parser_consume_token (parser);
12845 reduc_id = c_omp_reduction_id (code, reduc_id);
12846 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12848 tree nl, c;
12850 nl = c_parser_omp_variable_list (parser, clause_loc,
12851 OMP_CLAUSE_REDUCTION, list);
12852 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12854 tree d = OMP_CLAUSE_DECL (c), type;
12855 if (TREE_CODE (d) != TREE_LIST)
12856 type = TREE_TYPE (d);
12857 else
12859 int cnt = 0;
12860 tree t;
12861 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12862 cnt++;
12863 type = TREE_TYPE (t);
12864 while (cnt > 0)
12866 if (TREE_CODE (type) != POINTER_TYPE
12867 && TREE_CODE (type) != ARRAY_TYPE)
12868 break;
12869 type = TREE_TYPE (type);
12870 cnt--;
12873 while (TREE_CODE (type) == ARRAY_TYPE)
12874 type = TREE_TYPE (type);
12875 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12876 if (code == ERROR_MARK
12877 || !(INTEGRAL_TYPE_P (type)
12878 || TREE_CODE (type) == REAL_TYPE
12879 || TREE_CODE (type) == COMPLEX_TYPE))
12880 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12881 = c_omp_reduction_lookup (reduc_id,
12882 TYPE_MAIN_VARIANT (type));
12885 list = nl;
12887 parens.skip_until_found_close (parser);
12889 return list;
12892 /* OpenMP 2.5:
12893 schedule ( schedule-kind )
12894 schedule ( schedule-kind , expression )
12896 schedule-kind:
12897 static | dynamic | guided | runtime | auto
12899 OpenMP 4.5:
12900 schedule ( schedule-modifier : schedule-kind )
12901 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12903 schedule-modifier:
12904 simd
12905 monotonic
12906 nonmonotonic */
12908 static tree
12909 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12911 tree c, t;
12912 location_t loc = c_parser_peek_token (parser)->location;
12913 int modifiers = 0, nmodifiers = 0;
12915 matching_parens parens;
12916 if (!parens.require_open (parser))
12917 return list;
12919 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12921 while (c_parser_next_token_is (parser, CPP_NAME))
12923 tree kind = c_parser_peek_token (parser)->value;
12924 const char *p = IDENTIFIER_POINTER (kind);
12925 if (strcmp ("simd", p) == 0)
12926 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12927 else if (strcmp ("monotonic", p) == 0)
12928 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12929 else if (strcmp ("nonmonotonic", p) == 0)
12930 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12931 else
12932 break;
12933 c_parser_consume_token (parser);
12934 if (nmodifiers++ == 0
12935 && c_parser_next_token_is (parser, CPP_COMMA))
12936 c_parser_consume_token (parser);
12937 else
12939 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12940 break;
12944 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12945 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12946 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12947 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12949 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12950 "specified");
12951 modifiers = 0;
12954 if (c_parser_next_token_is (parser, CPP_NAME))
12956 tree kind = c_parser_peek_token (parser)->value;
12957 const char *p = IDENTIFIER_POINTER (kind);
12959 switch (p[0])
12961 case 'd':
12962 if (strcmp ("dynamic", p) != 0)
12963 goto invalid_kind;
12964 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12965 break;
12967 case 'g':
12968 if (strcmp ("guided", p) != 0)
12969 goto invalid_kind;
12970 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12971 break;
12973 case 'r':
12974 if (strcmp ("runtime", p) != 0)
12975 goto invalid_kind;
12976 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12977 break;
12979 default:
12980 goto invalid_kind;
12983 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12984 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12985 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12986 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12987 else
12988 goto invalid_kind;
12990 c_parser_consume_token (parser);
12991 if (c_parser_next_token_is (parser, CPP_COMMA))
12993 location_t here;
12994 c_parser_consume_token (parser);
12996 here = c_parser_peek_token (parser)->location;
12997 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12998 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12999 t = expr.value;
13000 t = c_fully_fold (t, false, NULL);
13002 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13003 error_at (here, "schedule %<runtime%> does not take "
13004 "a %<chunk_size%> parameter");
13005 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13006 error_at (here,
13007 "schedule %<auto%> does not take "
13008 "a %<chunk_size%> parameter");
13009 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13011 /* Attempt to statically determine when the number isn't
13012 positive. */
13013 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13014 build_int_cst (TREE_TYPE (t), 0));
13015 protected_set_expr_location (s, loc);
13016 if (s == boolean_true_node)
13018 warning_at (loc, 0,
13019 "chunk size value must be positive");
13020 t = integer_one_node;
13022 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13024 else
13025 c_parser_error (parser, "expected integer expression");
13027 parens.skip_until_found_close (parser);
13029 else
13030 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13031 "expected %<,%> or %<)%>");
13033 OMP_CLAUSE_SCHEDULE_KIND (c)
13034 = (enum omp_clause_schedule_kind)
13035 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13037 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13038 OMP_CLAUSE_CHAIN (c) = list;
13039 return c;
13041 invalid_kind:
13042 c_parser_error (parser, "invalid schedule kind");
13043 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13044 return list;
13047 /* OpenMP 2.5:
13048 shared ( variable-list ) */
13050 static tree
13051 c_parser_omp_clause_shared (c_parser *parser, tree list)
13053 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13056 /* OpenMP 3.0:
13057 untied */
13059 static tree
13060 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13062 tree c;
13064 /* FIXME: Should we allow duplicates? */
13065 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13067 c = build_omp_clause (c_parser_peek_token (parser)->location,
13068 OMP_CLAUSE_UNTIED);
13069 OMP_CLAUSE_CHAIN (c) = list;
13071 return c;
13074 /* OpenMP 4.0:
13075 inbranch
13076 notinbranch */
13078 static tree
13079 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13080 enum omp_clause_code code, tree list)
13082 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13084 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13085 OMP_CLAUSE_CHAIN (c) = list;
13087 return c;
13090 /* OpenMP 4.0:
13091 parallel
13093 sections
13094 taskgroup */
13096 static tree
13097 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13098 enum omp_clause_code code, tree list)
13100 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13101 OMP_CLAUSE_CHAIN (c) = list;
13103 return c;
13106 /* OpenMP 4.5:
13107 nogroup */
13109 static tree
13110 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13112 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13113 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13114 OMP_CLAUSE_NOGROUP);
13115 OMP_CLAUSE_CHAIN (c) = list;
13116 return c;
13119 /* OpenMP 4.5:
13120 simd
13121 threads */
13123 static tree
13124 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13125 enum omp_clause_code code, tree list)
13127 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13128 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13129 OMP_CLAUSE_CHAIN (c) = list;
13130 return c;
13133 /* OpenMP 4.0:
13134 num_teams ( expression ) */
13136 static tree
13137 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13139 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13140 matching_parens parens;
13141 if (parens.require_open (parser))
13143 location_t expr_loc = c_parser_peek_token (parser)->location;
13144 c_expr expr = c_parser_expression (parser);
13145 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13146 tree c, t = expr.value;
13147 t = c_fully_fold (t, false, NULL);
13149 parens.skip_until_found_close (parser);
13151 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13153 c_parser_error (parser, "expected integer expression");
13154 return list;
13157 /* Attempt to statically determine when the number isn't positive. */
13158 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13159 build_int_cst (TREE_TYPE (t), 0));
13160 protected_set_expr_location (c, expr_loc);
13161 if (c == boolean_true_node)
13163 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13164 t = integer_one_node;
13167 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13169 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13170 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13171 OMP_CLAUSE_CHAIN (c) = list;
13172 list = c;
13175 return list;
13178 /* OpenMP 4.0:
13179 thread_limit ( expression ) */
13181 static tree
13182 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13184 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13185 matching_parens parens;
13186 if (parens.require_open (parser))
13188 location_t expr_loc = c_parser_peek_token (parser)->location;
13189 c_expr expr = c_parser_expression (parser);
13190 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13191 tree c, t = expr.value;
13192 t = c_fully_fold (t, false, NULL);
13194 parens.skip_until_found_close (parser);
13196 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13198 c_parser_error (parser, "expected integer expression");
13199 return list;
13202 /* Attempt to statically determine when the number isn't positive. */
13203 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13204 build_int_cst (TREE_TYPE (t), 0));
13205 protected_set_expr_location (c, expr_loc);
13206 if (c == boolean_true_node)
13208 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13209 t = integer_one_node;
13212 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13213 "thread_limit");
13215 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13216 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13217 OMP_CLAUSE_CHAIN (c) = list;
13218 list = c;
13221 return list;
13224 /* OpenMP 4.0:
13225 aligned ( variable-list )
13226 aligned ( variable-list : constant-expression ) */
13228 static tree
13229 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13231 location_t clause_loc = c_parser_peek_token (parser)->location;
13232 tree nl, c;
13234 matching_parens parens;
13235 if (!parens.require_open (parser))
13236 return list;
13238 nl = c_parser_omp_variable_list (parser, clause_loc,
13239 OMP_CLAUSE_ALIGNED, list);
13241 if (c_parser_next_token_is (parser, CPP_COLON))
13243 c_parser_consume_token (parser);
13244 location_t expr_loc = c_parser_peek_token (parser)->location;
13245 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13246 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13247 tree alignment = expr.value;
13248 alignment = c_fully_fold (alignment, false, NULL);
13249 if (TREE_CODE (alignment) != INTEGER_CST
13250 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13251 || tree_int_cst_sgn (alignment) != 1)
13253 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13254 "be positive constant integer expression");
13255 alignment = NULL_TREE;
13258 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13259 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13262 parens.skip_until_found_close (parser);
13263 return nl;
13266 /* OpenMP 4.0:
13267 linear ( variable-list )
13268 linear ( variable-list : expression )
13270 OpenMP 4.5:
13271 linear ( modifier ( variable-list ) )
13272 linear ( modifier ( variable-list ) : expression ) */
13274 static tree
13275 c_parser_omp_clause_linear (c_parser *parser, tree list)
13277 location_t clause_loc = c_parser_peek_token (parser)->location;
13278 tree nl, c, step;
13279 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13281 matching_parens parens;
13282 if (!parens.require_open (parser))
13283 return list;
13285 if (c_parser_next_token_is (parser, CPP_NAME))
13287 c_token *tok = c_parser_peek_token (parser);
13288 const char *p = IDENTIFIER_POINTER (tok->value);
13289 if (strcmp ("val", p) == 0)
13290 kind = OMP_CLAUSE_LINEAR_VAL;
13291 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13292 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13293 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13295 c_parser_consume_token (parser);
13296 c_parser_consume_token (parser);
13300 nl = c_parser_omp_variable_list (parser, clause_loc,
13301 OMP_CLAUSE_LINEAR, list);
13303 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13304 parens.skip_until_found_close (parser);
13306 if (c_parser_next_token_is (parser, CPP_COLON))
13308 c_parser_consume_token (parser);
13309 location_t expr_loc = c_parser_peek_token (parser)->location;
13310 c_expr expr = c_parser_expression (parser);
13311 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13312 step = expr.value;
13313 step = c_fully_fold (step, false, NULL);
13314 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13316 error_at (clause_loc, "%<linear%> clause step expression must "
13317 "be integral");
13318 step = integer_one_node;
13322 else
13323 step = integer_one_node;
13325 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13327 OMP_CLAUSE_LINEAR_STEP (c) = step;
13328 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13331 parens.skip_until_found_close (parser);
13332 return nl;
13335 /* OpenMP 4.0:
13336 safelen ( constant-expression ) */
13338 static tree
13339 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13341 location_t clause_loc = c_parser_peek_token (parser)->location;
13342 tree c, t;
13344 matching_parens parens;
13345 if (!parens.require_open (parser))
13346 return list;
13348 location_t expr_loc = c_parser_peek_token (parser)->location;
13349 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13350 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13351 t = expr.value;
13352 t = c_fully_fold (t, false, NULL);
13353 if (TREE_CODE (t) != INTEGER_CST
13354 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13355 || tree_int_cst_sgn (t) != 1)
13357 error_at (clause_loc, "%<safelen%> clause expression must "
13358 "be positive constant integer expression");
13359 t = NULL_TREE;
13362 parens.skip_until_found_close (parser);
13363 if (t == NULL_TREE || t == error_mark_node)
13364 return list;
13366 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13368 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13369 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13370 OMP_CLAUSE_CHAIN (c) = list;
13371 return c;
13374 /* OpenMP 4.0:
13375 simdlen ( constant-expression ) */
13377 static tree
13378 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13380 location_t clause_loc = c_parser_peek_token (parser)->location;
13381 tree c, t;
13383 matching_parens parens;
13384 if (!parens.require_open (parser))
13385 return list;
13387 location_t expr_loc = c_parser_peek_token (parser)->location;
13388 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13389 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13390 t = expr.value;
13391 t = c_fully_fold (t, false, NULL);
13392 if (TREE_CODE (t) != INTEGER_CST
13393 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13394 || tree_int_cst_sgn (t) != 1)
13396 error_at (clause_loc, "%<simdlen%> clause expression must "
13397 "be positive constant integer expression");
13398 t = NULL_TREE;
13401 parens.skip_until_found_close (parser);
13402 if (t == NULL_TREE || t == error_mark_node)
13403 return list;
13405 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13407 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13408 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13409 OMP_CLAUSE_CHAIN (c) = list;
13410 return c;
13413 /* OpenMP 4.5:
13414 vec:
13415 identifier [+/- integer]
13416 vec , identifier [+/- integer]
13419 static tree
13420 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13421 tree list)
13423 tree vec = NULL;
13424 if (c_parser_next_token_is_not (parser, CPP_NAME)
13425 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13427 c_parser_error (parser, "expected identifier");
13428 return list;
13431 while (c_parser_next_token_is (parser, CPP_NAME)
13432 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13434 tree t = lookup_name (c_parser_peek_token (parser)->value);
13435 tree addend = NULL;
13437 if (t == NULL_TREE)
13439 undeclared_variable (c_parser_peek_token (parser)->location,
13440 c_parser_peek_token (parser)->value);
13441 t = error_mark_node;
13444 c_parser_consume_token (parser);
13446 bool neg = false;
13447 if (c_parser_next_token_is (parser, CPP_MINUS))
13448 neg = true;
13449 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13451 addend = integer_zero_node;
13452 neg = false;
13453 goto add_to_vector;
13455 c_parser_consume_token (parser);
13457 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13459 c_parser_error (parser, "expected integer");
13460 return list;
13463 addend = c_parser_peek_token (parser)->value;
13464 if (TREE_CODE (addend) != INTEGER_CST)
13466 c_parser_error (parser, "expected integer");
13467 return list;
13469 c_parser_consume_token (parser);
13471 add_to_vector:
13472 if (t != error_mark_node)
13474 vec = tree_cons (addend, t, vec);
13475 if (neg)
13476 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13479 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13480 break;
13482 c_parser_consume_token (parser);
13485 if (vec == NULL_TREE)
13486 return list;
13488 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13489 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13490 OMP_CLAUSE_DECL (u) = nreverse (vec);
13491 OMP_CLAUSE_CHAIN (u) = list;
13492 return u;
13495 /* OpenMP 4.0:
13496 depend ( depend-kind: variable-list )
13498 depend-kind:
13499 in | out | inout
13501 OpenMP 4.5:
13502 depend ( source )
13504 depend ( sink : vec ) */
13506 static tree
13507 c_parser_omp_clause_depend (c_parser *parser, tree list)
13509 location_t clause_loc = c_parser_peek_token (parser)->location;
13510 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13511 tree nl, c;
13513 matching_parens parens;
13514 if (!parens.require_open (parser))
13515 return list;
13517 if (c_parser_next_token_is (parser, CPP_NAME))
13519 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13520 if (strcmp ("in", p) == 0)
13521 kind = OMP_CLAUSE_DEPEND_IN;
13522 else if (strcmp ("inout", p) == 0)
13523 kind = OMP_CLAUSE_DEPEND_INOUT;
13524 else if (strcmp ("out", p) == 0)
13525 kind = OMP_CLAUSE_DEPEND_OUT;
13526 else if (strcmp ("source", p) == 0)
13527 kind = OMP_CLAUSE_DEPEND_SOURCE;
13528 else if (strcmp ("sink", p) == 0)
13529 kind = OMP_CLAUSE_DEPEND_SINK;
13530 else
13531 goto invalid_kind;
13533 else
13534 goto invalid_kind;
13536 c_parser_consume_token (parser);
13538 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13540 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13541 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13542 OMP_CLAUSE_DECL (c) = NULL_TREE;
13543 OMP_CLAUSE_CHAIN (c) = list;
13544 parens.skip_until_found_close (parser);
13545 return c;
13548 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13549 goto resync_fail;
13551 if (kind == OMP_CLAUSE_DEPEND_SINK)
13552 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13553 else
13555 nl = c_parser_omp_variable_list (parser, clause_loc,
13556 OMP_CLAUSE_DEPEND, list);
13558 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13559 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13562 parens.skip_until_found_close (parser);
13563 return nl;
13565 invalid_kind:
13566 c_parser_error (parser, "invalid depend kind");
13567 resync_fail:
13568 parens.skip_until_found_close (parser);
13569 return list;
13572 /* OpenMP 4.0:
13573 map ( map-kind: variable-list )
13574 map ( variable-list )
13576 map-kind:
13577 alloc | to | from | tofrom
13579 OpenMP 4.5:
13580 map-kind:
13581 alloc | to | from | tofrom | release | delete
13583 map ( always [,] map-kind: variable-list ) */
13585 static tree
13586 c_parser_omp_clause_map (c_parser *parser, tree list)
13588 location_t clause_loc = c_parser_peek_token (parser)->location;
13589 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13590 int always = 0;
13591 enum c_id_kind always_id_kind = C_ID_NONE;
13592 location_t always_loc = UNKNOWN_LOCATION;
13593 tree always_id = NULL_TREE;
13594 tree nl, c;
13596 matching_parens parens;
13597 if (!parens.require_open (parser))
13598 return list;
13600 if (c_parser_next_token_is (parser, CPP_NAME))
13602 c_token *tok = c_parser_peek_token (parser);
13603 const char *p = IDENTIFIER_POINTER (tok->value);
13604 always_id_kind = tok->id_kind;
13605 always_loc = tok->location;
13606 always_id = tok->value;
13607 if (strcmp ("always", p) == 0)
13609 c_token *sectok = c_parser_peek_2nd_token (parser);
13610 if (sectok->type == CPP_COMMA)
13612 c_parser_consume_token (parser);
13613 c_parser_consume_token (parser);
13614 always = 2;
13616 else if (sectok->type == CPP_NAME)
13618 p = IDENTIFIER_POINTER (sectok->value);
13619 if (strcmp ("alloc", p) == 0
13620 || strcmp ("to", p) == 0
13621 || strcmp ("from", p) == 0
13622 || strcmp ("tofrom", p) == 0
13623 || strcmp ("release", p) == 0
13624 || strcmp ("delete", p) == 0)
13626 c_parser_consume_token (parser);
13627 always = 1;
13633 if (c_parser_next_token_is (parser, CPP_NAME)
13634 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13636 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13637 if (strcmp ("alloc", p) == 0)
13638 kind = GOMP_MAP_ALLOC;
13639 else if (strcmp ("to", p) == 0)
13640 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13641 else if (strcmp ("from", p) == 0)
13642 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13643 else if (strcmp ("tofrom", p) == 0)
13644 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13645 else if (strcmp ("release", p) == 0)
13646 kind = GOMP_MAP_RELEASE;
13647 else if (strcmp ("delete", p) == 0)
13648 kind = GOMP_MAP_DELETE;
13649 else
13651 c_parser_error (parser, "invalid map kind");
13652 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13653 "expected %<)%>");
13654 return list;
13656 c_parser_consume_token (parser);
13657 c_parser_consume_token (parser);
13659 else if (always)
13661 if (always_id_kind != C_ID_ID)
13663 c_parser_error (parser, "expected identifier");
13664 parens.skip_until_found_close (parser);
13665 return list;
13668 tree t = lookup_name (always_id);
13669 if (t == NULL_TREE)
13671 undeclared_variable (always_loc, always_id);
13672 t = error_mark_node;
13674 if (t != error_mark_node)
13676 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13677 OMP_CLAUSE_DECL (u) = t;
13678 OMP_CLAUSE_CHAIN (u) = list;
13679 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13680 list = u;
13682 if (always == 1)
13684 parens.skip_until_found_close (parser);
13685 return list;
13689 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13691 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13692 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13694 parens.skip_until_found_close (parser);
13695 return nl;
13698 /* OpenMP 4.0:
13699 device ( expression ) */
13701 static tree
13702 c_parser_omp_clause_device (c_parser *parser, tree list)
13704 location_t clause_loc = c_parser_peek_token (parser)->location;
13705 matching_parens parens;
13706 if (parens.require_open (parser))
13708 location_t expr_loc = c_parser_peek_token (parser)->location;
13709 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13710 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13711 tree c, t = expr.value;
13712 t = c_fully_fold (t, false, NULL);
13714 parens.skip_until_found_close (parser);
13716 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13718 c_parser_error (parser, "expected integer expression");
13719 return list;
13722 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13724 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13725 OMP_CLAUSE_DEVICE_ID (c) = t;
13726 OMP_CLAUSE_CHAIN (c) = list;
13727 list = c;
13730 return list;
13733 /* OpenMP 4.0:
13734 dist_schedule ( static )
13735 dist_schedule ( static , expression ) */
13737 static tree
13738 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13740 tree c, t = NULL_TREE;
13741 location_t loc = c_parser_peek_token (parser)->location;
13743 matching_parens parens;
13744 if (!parens.require_open (parser))
13745 return list;
13747 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13749 c_parser_error (parser, "invalid dist_schedule kind");
13750 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13751 "expected %<)%>");
13752 return list;
13755 c_parser_consume_token (parser);
13756 if (c_parser_next_token_is (parser, CPP_COMMA))
13758 c_parser_consume_token (parser);
13760 location_t expr_loc = c_parser_peek_token (parser)->location;
13761 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13762 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13763 t = expr.value;
13764 t = c_fully_fold (t, false, NULL);
13765 parens.skip_until_found_close (parser);
13767 else
13768 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13769 "expected %<,%> or %<)%>");
13771 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13772 if (t == error_mark_node)
13773 return list;
13775 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13776 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13777 OMP_CLAUSE_CHAIN (c) = list;
13778 return c;
13781 /* OpenMP 4.0:
13782 proc_bind ( proc-bind-kind )
13784 proc-bind-kind:
13785 master | close | spread */
13787 static tree
13788 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13790 location_t clause_loc = c_parser_peek_token (parser)->location;
13791 enum omp_clause_proc_bind_kind kind;
13792 tree c;
13794 matching_parens parens;
13795 if (!parens.require_open (parser))
13796 return list;
13798 if (c_parser_next_token_is (parser, CPP_NAME))
13800 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13801 if (strcmp ("master", p) == 0)
13802 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13803 else if (strcmp ("close", p) == 0)
13804 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13805 else if (strcmp ("spread", p) == 0)
13806 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13807 else
13808 goto invalid_kind;
13810 else
13811 goto invalid_kind;
13813 c_parser_consume_token (parser);
13814 parens.skip_until_found_close (parser);
13815 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13816 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13817 OMP_CLAUSE_CHAIN (c) = list;
13818 return c;
13820 invalid_kind:
13821 c_parser_error (parser, "invalid proc_bind kind");
13822 parens.skip_until_found_close (parser);
13823 return list;
13826 /* OpenMP 4.0:
13827 to ( variable-list ) */
13829 static tree
13830 c_parser_omp_clause_to (c_parser *parser, tree list)
13832 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13835 /* OpenMP 4.0:
13836 from ( variable-list ) */
13838 static tree
13839 c_parser_omp_clause_from (c_parser *parser, tree list)
13841 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13844 /* OpenMP 4.0:
13845 uniform ( variable-list ) */
13847 static tree
13848 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13850 /* The clauses location. */
13851 location_t loc = c_parser_peek_token (parser)->location;
13853 matching_parens parens;
13854 if (parens.require_open (parser))
13856 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13857 list);
13858 parens.skip_until_found_close (parser);
13860 return list;
13863 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13864 is a bitmask in MASK. Return the list of clauses found. */
13866 static tree
13867 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13868 const char *where, bool finish_p = true)
13870 tree clauses = NULL;
13871 bool first = true;
13873 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13875 location_t here;
13876 pragma_omp_clause c_kind;
13877 const char *c_name;
13878 tree prev = clauses;
13880 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13881 c_parser_consume_token (parser);
13883 here = c_parser_peek_token (parser)->location;
13884 c_kind = c_parser_omp_clause_name (parser);
13886 switch (c_kind)
13888 case PRAGMA_OACC_CLAUSE_ASYNC:
13889 clauses = c_parser_oacc_clause_async (parser, clauses);
13890 c_name = "async";
13891 break;
13892 case PRAGMA_OACC_CLAUSE_AUTO:
13893 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13894 clauses);
13895 c_name = "auto";
13896 break;
13897 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13898 clauses = c_parser_omp_clause_collapse (parser, clauses);
13899 c_name = "collapse";
13900 break;
13901 case PRAGMA_OACC_CLAUSE_COPY:
13902 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13903 c_name = "copy";
13904 break;
13905 case PRAGMA_OACC_CLAUSE_COPYIN:
13906 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13907 c_name = "copyin";
13908 break;
13909 case PRAGMA_OACC_CLAUSE_COPYOUT:
13910 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13911 c_name = "copyout";
13912 break;
13913 case PRAGMA_OACC_CLAUSE_CREATE:
13914 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13915 c_name = "create";
13916 break;
13917 case PRAGMA_OACC_CLAUSE_DELETE:
13918 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13919 c_name = "delete";
13920 break;
13921 case PRAGMA_OMP_CLAUSE_DEFAULT:
13922 clauses = c_parser_omp_clause_default (parser, clauses, true);
13923 c_name = "default";
13924 break;
13925 case PRAGMA_OACC_CLAUSE_DEVICE:
13926 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13927 c_name = "device";
13928 break;
13929 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13930 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13931 c_name = "deviceptr";
13932 break;
13933 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13934 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13935 c_name = "device_resident";
13936 break;
13937 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13938 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13939 c_name = "firstprivate";
13940 break;
13941 case PRAGMA_OACC_CLAUSE_GANG:
13942 c_name = "gang";
13943 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13944 c_name, clauses);
13945 break;
13946 case PRAGMA_OACC_CLAUSE_HOST:
13947 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13948 c_name = "host";
13949 break;
13950 case PRAGMA_OACC_CLAUSE_IF:
13951 clauses = c_parser_omp_clause_if (parser, clauses, false);
13952 c_name = "if";
13953 break;
13954 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13955 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13956 clauses);
13957 c_name = "independent";
13958 break;
13959 case PRAGMA_OACC_CLAUSE_LINK:
13960 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13961 c_name = "link";
13962 break;
13963 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13964 clauses = c_parser_oacc_single_int_clause (parser,
13965 OMP_CLAUSE_NUM_GANGS,
13966 clauses);
13967 c_name = "num_gangs";
13968 break;
13969 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13970 clauses = c_parser_oacc_single_int_clause (parser,
13971 OMP_CLAUSE_NUM_WORKERS,
13972 clauses);
13973 c_name = "num_workers";
13974 break;
13975 case PRAGMA_OACC_CLAUSE_PRESENT:
13976 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13977 c_name = "present";
13978 break;
13979 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13980 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13981 c_name = "present_or_copy";
13982 break;
13983 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13984 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13985 c_name = "present_or_copyin";
13986 break;
13987 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13988 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13989 c_name = "present_or_copyout";
13990 break;
13991 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13992 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13993 c_name = "present_or_create";
13994 break;
13995 case PRAGMA_OACC_CLAUSE_PRIVATE:
13996 clauses = c_parser_omp_clause_private (parser, clauses);
13997 c_name = "private";
13998 break;
13999 case PRAGMA_OACC_CLAUSE_REDUCTION:
14000 clauses = c_parser_omp_clause_reduction (parser, clauses);
14001 c_name = "reduction";
14002 break;
14003 case PRAGMA_OACC_CLAUSE_SELF:
14004 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14005 c_name = "self";
14006 break;
14007 case PRAGMA_OACC_CLAUSE_SEQ:
14008 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14009 clauses);
14010 c_name = "seq";
14011 break;
14012 case PRAGMA_OACC_CLAUSE_TILE:
14013 clauses = c_parser_oacc_clause_tile (parser, clauses);
14014 c_name = "tile";
14015 break;
14016 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14017 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14018 c_name = "use_device";
14019 break;
14020 case PRAGMA_OACC_CLAUSE_VECTOR:
14021 c_name = "vector";
14022 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14023 c_name, clauses);
14024 break;
14025 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14026 clauses = c_parser_oacc_single_int_clause (parser,
14027 OMP_CLAUSE_VECTOR_LENGTH,
14028 clauses);
14029 c_name = "vector_length";
14030 break;
14031 case PRAGMA_OACC_CLAUSE_WAIT:
14032 clauses = c_parser_oacc_clause_wait (parser, clauses);
14033 c_name = "wait";
14034 break;
14035 case PRAGMA_OACC_CLAUSE_WORKER:
14036 c_name = "worker";
14037 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14038 c_name, clauses);
14039 break;
14040 default:
14041 c_parser_error (parser, "expected %<#pragma acc%> clause");
14042 goto saw_error;
14045 first = false;
14047 if (((mask >> c_kind) & 1) == 0)
14049 /* Remove the invalid clause(s) from the list to avoid
14050 confusing the rest of the compiler. */
14051 clauses = prev;
14052 error_at (here, "%qs is not valid for %qs", c_name, where);
14056 saw_error:
14057 c_parser_skip_to_pragma_eol (parser);
14059 if (finish_p)
14060 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14062 return clauses;
14065 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14066 is a bitmask in MASK. Return the list of clauses found. */
14068 static tree
14069 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14070 const char *where, bool finish_p = true)
14072 tree clauses = NULL;
14073 bool first = true;
14075 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14077 location_t here;
14078 pragma_omp_clause c_kind;
14079 const char *c_name;
14080 tree prev = clauses;
14082 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14083 c_parser_consume_token (parser);
14085 here = c_parser_peek_token (parser)->location;
14086 c_kind = c_parser_omp_clause_name (parser);
14088 switch (c_kind)
14090 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14091 clauses = c_parser_omp_clause_collapse (parser, clauses);
14092 c_name = "collapse";
14093 break;
14094 case PRAGMA_OMP_CLAUSE_COPYIN:
14095 clauses = c_parser_omp_clause_copyin (parser, clauses);
14096 c_name = "copyin";
14097 break;
14098 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14099 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14100 c_name = "copyprivate";
14101 break;
14102 case PRAGMA_OMP_CLAUSE_DEFAULT:
14103 clauses = c_parser_omp_clause_default (parser, clauses, false);
14104 c_name = "default";
14105 break;
14106 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14107 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14108 c_name = "firstprivate";
14109 break;
14110 case PRAGMA_OMP_CLAUSE_FINAL:
14111 clauses = c_parser_omp_clause_final (parser, clauses);
14112 c_name = "final";
14113 break;
14114 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14115 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14116 c_name = "grainsize";
14117 break;
14118 case PRAGMA_OMP_CLAUSE_HINT:
14119 clauses = c_parser_omp_clause_hint (parser, clauses);
14120 c_name = "hint";
14121 break;
14122 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14123 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14124 c_name = "defaultmap";
14125 break;
14126 case PRAGMA_OMP_CLAUSE_IF:
14127 clauses = c_parser_omp_clause_if (parser, clauses, true);
14128 c_name = "if";
14129 break;
14130 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14131 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14132 c_name = "lastprivate";
14133 break;
14134 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14135 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14136 c_name = "mergeable";
14137 break;
14138 case PRAGMA_OMP_CLAUSE_NOWAIT:
14139 clauses = c_parser_omp_clause_nowait (parser, clauses);
14140 c_name = "nowait";
14141 break;
14142 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14143 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14144 c_name = "num_tasks";
14145 break;
14146 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14147 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14148 c_name = "num_threads";
14149 break;
14150 case PRAGMA_OMP_CLAUSE_ORDERED:
14151 clauses = c_parser_omp_clause_ordered (parser, clauses);
14152 c_name = "ordered";
14153 break;
14154 case PRAGMA_OMP_CLAUSE_PRIORITY:
14155 clauses = c_parser_omp_clause_priority (parser, clauses);
14156 c_name = "priority";
14157 break;
14158 case PRAGMA_OMP_CLAUSE_PRIVATE:
14159 clauses = c_parser_omp_clause_private (parser, clauses);
14160 c_name = "private";
14161 break;
14162 case PRAGMA_OMP_CLAUSE_REDUCTION:
14163 clauses = c_parser_omp_clause_reduction (parser, clauses);
14164 c_name = "reduction";
14165 break;
14166 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14167 clauses = c_parser_omp_clause_schedule (parser, clauses);
14168 c_name = "schedule";
14169 break;
14170 case PRAGMA_OMP_CLAUSE_SHARED:
14171 clauses = c_parser_omp_clause_shared (parser, clauses);
14172 c_name = "shared";
14173 break;
14174 case PRAGMA_OMP_CLAUSE_UNTIED:
14175 clauses = c_parser_omp_clause_untied (parser, clauses);
14176 c_name = "untied";
14177 break;
14178 case PRAGMA_OMP_CLAUSE_INBRANCH:
14179 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14180 clauses);
14181 c_name = "inbranch";
14182 break;
14183 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14184 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14185 clauses);
14186 c_name = "notinbranch";
14187 break;
14188 case PRAGMA_OMP_CLAUSE_PARALLEL:
14189 clauses
14190 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14191 clauses);
14192 c_name = "parallel";
14193 if (!first)
14195 clause_not_first:
14196 error_at (here, "%qs must be the first clause of %qs",
14197 c_name, where);
14198 clauses = prev;
14200 break;
14201 case PRAGMA_OMP_CLAUSE_FOR:
14202 clauses
14203 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14204 clauses);
14205 c_name = "for";
14206 if (!first)
14207 goto clause_not_first;
14208 break;
14209 case PRAGMA_OMP_CLAUSE_SECTIONS:
14210 clauses
14211 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14212 clauses);
14213 c_name = "sections";
14214 if (!first)
14215 goto clause_not_first;
14216 break;
14217 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14218 clauses
14219 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14220 clauses);
14221 c_name = "taskgroup";
14222 if (!first)
14223 goto clause_not_first;
14224 break;
14225 case PRAGMA_OMP_CLAUSE_LINK:
14226 clauses
14227 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14228 c_name = "link";
14229 break;
14230 case PRAGMA_OMP_CLAUSE_TO:
14231 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14232 clauses
14233 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14234 clauses);
14235 else
14236 clauses = c_parser_omp_clause_to (parser, clauses);
14237 c_name = "to";
14238 break;
14239 case PRAGMA_OMP_CLAUSE_FROM:
14240 clauses = c_parser_omp_clause_from (parser, clauses);
14241 c_name = "from";
14242 break;
14243 case PRAGMA_OMP_CLAUSE_UNIFORM:
14244 clauses = c_parser_omp_clause_uniform (parser, clauses);
14245 c_name = "uniform";
14246 break;
14247 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14248 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14249 c_name = "num_teams";
14250 break;
14251 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14252 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14253 c_name = "thread_limit";
14254 break;
14255 case PRAGMA_OMP_CLAUSE_ALIGNED:
14256 clauses = c_parser_omp_clause_aligned (parser, clauses);
14257 c_name = "aligned";
14258 break;
14259 case PRAGMA_OMP_CLAUSE_LINEAR:
14260 clauses = c_parser_omp_clause_linear (parser, clauses);
14261 c_name = "linear";
14262 break;
14263 case PRAGMA_OMP_CLAUSE_DEPEND:
14264 clauses = c_parser_omp_clause_depend (parser, clauses);
14265 c_name = "depend";
14266 break;
14267 case PRAGMA_OMP_CLAUSE_MAP:
14268 clauses = c_parser_omp_clause_map (parser, clauses);
14269 c_name = "map";
14270 break;
14271 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14272 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14273 c_name = "use_device_ptr";
14274 break;
14275 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14276 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14277 c_name = "is_device_ptr";
14278 break;
14279 case PRAGMA_OMP_CLAUSE_DEVICE:
14280 clauses = c_parser_omp_clause_device (parser, clauses);
14281 c_name = "device";
14282 break;
14283 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14284 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14285 c_name = "dist_schedule";
14286 break;
14287 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14288 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14289 c_name = "proc_bind";
14290 break;
14291 case PRAGMA_OMP_CLAUSE_SAFELEN:
14292 clauses = c_parser_omp_clause_safelen (parser, clauses);
14293 c_name = "safelen";
14294 break;
14295 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14296 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14297 c_name = "simdlen";
14298 break;
14299 case PRAGMA_OMP_CLAUSE_NOGROUP:
14300 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14301 c_name = "nogroup";
14302 break;
14303 case PRAGMA_OMP_CLAUSE_THREADS:
14304 clauses
14305 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14306 clauses);
14307 c_name = "threads";
14308 break;
14309 case PRAGMA_OMP_CLAUSE_SIMD:
14310 clauses
14311 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14312 clauses);
14313 c_name = "simd";
14314 break;
14315 default:
14316 c_parser_error (parser, "expected %<#pragma omp%> clause");
14317 goto saw_error;
14320 first = false;
14322 if (((mask >> c_kind) & 1) == 0)
14324 /* Remove the invalid clause(s) from the list to avoid
14325 confusing the rest of the compiler. */
14326 clauses = prev;
14327 error_at (here, "%qs is not valid for %qs", c_name, where);
14331 saw_error:
14332 c_parser_skip_to_pragma_eol (parser);
14334 if (finish_p)
14336 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14337 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14338 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14341 return clauses;
14344 /* OpenACC 2.0, OpenMP 2.5:
14345 structured-block:
14346 statement
14348 In practice, we're also interested in adding the statement to an
14349 outer node. So it is convenient if we work around the fact that
14350 c_parser_statement calls add_stmt. */
14352 static tree
14353 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14355 tree stmt = push_stmt_list ();
14356 c_parser_statement (parser, if_p);
14357 return pop_stmt_list (stmt);
14360 /* OpenACC 2.0:
14361 # pragma acc cache (variable-list) new-line
14363 LOC is the location of the #pragma token.
14366 static tree
14367 c_parser_oacc_cache (location_t loc, c_parser *parser)
14369 tree stmt, clauses;
14371 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14372 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14374 c_parser_skip_to_pragma_eol (parser);
14376 stmt = make_node (OACC_CACHE);
14377 TREE_TYPE (stmt) = void_type_node;
14378 OACC_CACHE_CLAUSES (stmt) = clauses;
14379 SET_EXPR_LOCATION (stmt, loc);
14380 add_stmt (stmt);
14382 return stmt;
14385 /* OpenACC 2.0:
14386 # pragma acc data oacc-data-clause[optseq] new-line
14387 structured-block
14389 LOC is the location of the #pragma token.
14392 #define OACC_DATA_CLAUSE_MASK \
14393 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14405 static tree
14406 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14408 tree stmt, clauses, block;
14410 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14411 "#pragma acc data");
14413 block = c_begin_omp_parallel ();
14414 add_stmt (c_parser_omp_structured_block (parser, if_p));
14416 stmt = c_finish_oacc_data (loc, clauses, block);
14418 return stmt;
14421 /* OpenACC 2.0:
14422 # pragma acc declare oacc-data-clause[optseq] new-line
14425 #define OACC_DECLARE_CLAUSE_MASK \
14426 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14439 static void
14440 c_parser_oacc_declare (c_parser *parser)
14442 location_t pragma_loc = c_parser_peek_token (parser)->location;
14443 tree clauses, stmt, t, decl;
14445 bool error = false;
14447 c_parser_consume_pragma (parser);
14449 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14450 "#pragma acc declare");
14451 if (!clauses)
14453 error_at (pragma_loc,
14454 "no valid clauses specified in %<#pragma acc declare%>");
14455 return;
14458 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14460 location_t loc = OMP_CLAUSE_LOCATION (t);
14461 decl = OMP_CLAUSE_DECL (t);
14462 if (!DECL_P (decl))
14464 error_at (loc, "array section in %<#pragma acc declare%>");
14465 error = true;
14466 continue;
14469 switch (OMP_CLAUSE_MAP_KIND (t))
14471 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14472 case GOMP_MAP_FORCE_ALLOC:
14473 case GOMP_MAP_FORCE_TO:
14474 case GOMP_MAP_FORCE_DEVICEPTR:
14475 case GOMP_MAP_DEVICE_RESIDENT:
14476 break;
14478 case GOMP_MAP_LINK:
14479 if (!global_bindings_p ()
14480 && (TREE_STATIC (decl)
14481 || !DECL_EXTERNAL (decl)))
14483 error_at (loc,
14484 "%qD must be a global variable in "
14485 "%<#pragma acc declare link%>",
14486 decl);
14487 error = true;
14488 continue;
14490 break;
14492 default:
14493 if (global_bindings_p ())
14495 error_at (loc, "invalid OpenACC clause at file scope");
14496 error = true;
14497 continue;
14499 if (DECL_EXTERNAL (decl))
14501 error_at (loc,
14502 "invalid use of %<extern%> variable %qD "
14503 "in %<#pragma acc declare%>", decl);
14504 error = true;
14505 continue;
14507 else if (TREE_PUBLIC (decl))
14509 error_at (loc,
14510 "invalid use of %<global%> variable %qD "
14511 "in %<#pragma acc declare%>", decl);
14512 error = true;
14513 continue;
14515 break;
14518 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14519 || lookup_attribute ("omp declare target link",
14520 DECL_ATTRIBUTES (decl)))
14522 error_at (loc, "variable %qD used more than once with "
14523 "%<#pragma acc declare%>", decl);
14524 error = true;
14525 continue;
14528 if (!error)
14530 tree id;
14532 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14533 id = get_identifier ("omp declare target link");
14534 else
14535 id = get_identifier ("omp declare target");
14537 DECL_ATTRIBUTES (decl)
14538 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14540 if (global_bindings_p ())
14542 symtab_node *node = symtab_node::get (decl);
14543 if (node != NULL)
14545 node->offloadable = 1;
14546 if (ENABLE_OFFLOADING)
14548 g->have_offload = true;
14549 if (is_a <varpool_node *> (node))
14550 vec_safe_push (offload_vars, decl);
14557 if (error || global_bindings_p ())
14558 return;
14560 stmt = make_node (OACC_DECLARE);
14561 TREE_TYPE (stmt) = void_type_node;
14562 OACC_DECLARE_CLAUSES (stmt) = clauses;
14563 SET_EXPR_LOCATION (stmt, pragma_loc);
14565 add_stmt (stmt);
14567 return;
14570 /* OpenACC 2.0:
14571 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14575 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14578 LOC is the location of the #pragma token.
14581 #define OACC_ENTER_DATA_CLAUSE_MASK \
14582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14590 #define OACC_EXIT_DATA_CLAUSE_MASK \
14591 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14597 static void
14598 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14600 location_t loc = c_parser_peek_token (parser)->location;
14601 tree clauses, stmt;
14602 const char *p = "";
14604 c_parser_consume_pragma (parser);
14606 if (c_parser_next_token_is (parser, CPP_NAME))
14608 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14609 c_parser_consume_token (parser);
14612 if (strcmp (p, "data") != 0)
14614 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14615 enter ? "enter" : "exit");
14616 parser->error = true;
14617 c_parser_skip_to_pragma_eol (parser);
14618 return;
14621 if (enter)
14622 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14623 "#pragma acc enter data");
14624 else
14625 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14626 "#pragma acc exit data");
14628 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14630 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14631 enter ? "enter" : "exit");
14632 return;
14635 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14636 TREE_TYPE (stmt) = void_type_node;
14637 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14638 SET_EXPR_LOCATION (stmt, loc);
14639 add_stmt (stmt);
14643 /* OpenACC 2.0:
14644 # pragma acc host_data oacc-data-clause[optseq] new-line
14645 structured-block
14648 #define OACC_HOST_DATA_CLAUSE_MASK \
14649 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14651 static tree
14652 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14654 tree stmt, clauses, block;
14656 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14657 "#pragma acc host_data");
14659 block = c_begin_omp_parallel ();
14660 add_stmt (c_parser_omp_structured_block (parser, if_p));
14661 stmt = c_finish_oacc_host_data (loc, clauses, block);
14662 return stmt;
14666 /* OpenACC 2.0:
14668 # pragma acc loop oacc-loop-clause[optseq] new-line
14669 structured-block
14671 LOC is the location of the #pragma token.
14674 #define OACC_LOOP_CLAUSE_MASK \
14675 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14685 static tree
14686 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14687 omp_clause_mask mask, tree *cclauses, bool *if_p)
14689 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14691 strcat (p_name, " loop");
14692 mask |= OACC_LOOP_CLAUSE_MASK;
14694 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14695 cclauses == NULL);
14696 if (cclauses)
14698 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14699 if (*cclauses)
14700 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14701 if (clauses)
14702 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14705 tree block = c_begin_compound_stmt (true);
14706 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14707 if_p);
14708 block = c_end_compound_stmt (loc, block, true);
14709 add_stmt (block);
14711 return stmt;
14714 /* OpenACC 2.0:
14715 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14716 structured-block
14720 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14721 structured-block
14723 LOC is the location of the #pragma token.
14726 #define OACC_KERNELS_CLAUSE_MASK \
14727 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14745 #define OACC_PARALLEL_CLAUSE_MASK \
14746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14767 static tree
14768 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14769 enum pragma_kind p_kind, char *p_name,
14770 bool *if_p)
14772 omp_clause_mask mask;
14773 enum tree_code code;
14774 switch (p_kind)
14776 case PRAGMA_OACC_KERNELS:
14777 strcat (p_name, " kernels");
14778 mask = OACC_KERNELS_CLAUSE_MASK;
14779 code = OACC_KERNELS;
14780 break;
14781 case PRAGMA_OACC_PARALLEL:
14782 strcat (p_name, " parallel");
14783 mask = OACC_PARALLEL_CLAUSE_MASK;
14784 code = OACC_PARALLEL;
14785 break;
14786 default:
14787 gcc_unreachable ();
14790 if (c_parser_next_token_is (parser, CPP_NAME))
14792 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14793 if (strcmp (p, "loop") == 0)
14795 c_parser_consume_token (parser);
14796 tree block = c_begin_omp_parallel ();
14797 tree clauses;
14798 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14799 return c_finish_omp_construct (loc, code, block, clauses);
14803 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14805 tree block = c_begin_omp_parallel ();
14806 add_stmt (c_parser_omp_structured_block (parser, if_p));
14808 return c_finish_omp_construct (loc, code, block, clauses);
14811 /* OpenACC 2.0:
14812 # pragma acc routine oacc-routine-clause[optseq] new-line
14813 function-definition
14815 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14818 #define OACC_ROUTINE_CLAUSE_MASK \
14819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14824 /* Parse an OpenACC routine directive. For named directives, we apply
14825 immediately to the named function. For unnamed ones we then parse
14826 a declaration or definition, which must be for a function. */
14828 static void
14829 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14831 gcc_checking_assert (context == pragma_external);
14833 oacc_routine_data data;
14834 data.error_seen = false;
14835 data.fndecl_seen = false;
14836 data.clauses = NULL_TREE;
14837 data.loc = c_parser_peek_token (parser)->location;
14839 c_parser_consume_pragma (parser);
14841 /* Look for optional '( name )'. */
14842 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14844 c_parser_consume_token (parser); /* '(' */
14846 tree decl = NULL_TREE;
14847 c_token *name_token = c_parser_peek_token (parser);
14848 location_t name_loc = name_token->location;
14849 if (name_token->type == CPP_NAME
14850 && (name_token->id_kind == C_ID_ID
14851 || name_token->id_kind == C_ID_TYPENAME))
14853 decl = lookup_name (name_token->value);
14854 if (!decl)
14855 error_at (name_loc,
14856 "%qE has not been declared", name_token->value);
14857 c_parser_consume_token (parser);
14859 else
14860 c_parser_error (parser, "expected function name");
14862 if (!decl
14863 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14865 c_parser_skip_to_pragma_eol (parser, false);
14866 return;
14869 data.clauses
14870 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14871 "#pragma acc routine");
14873 if (TREE_CODE (decl) != FUNCTION_DECL)
14875 error_at (name_loc, "%qD does not refer to a function", decl);
14876 return;
14879 c_finish_oacc_routine (&data, decl, false);
14881 else /* No optional '( name )'. */
14883 data.clauses
14884 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14885 "#pragma acc routine");
14887 /* Emit a helpful diagnostic if there's another pragma following this
14888 one. Also don't allow a static assertion declaration, as in the
14889 following we'll just parse a *single* "declaration or function
14890 definition", and the static assertion counts an one. */
14891 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14892 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14894 error_at (data.loc,
14895 "%<#pragma acc routine%> not immediately followed by"
14896 " function declaration or definition");
14897 /* ..., and then just keep going. */
14898 return;
14901 /* We only have to consider the pragma_external case here. */
14902 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14903 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14905 int ext = disable_extension_diagnostics ();
14907 c_parser_consume_token (parser);
14908 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14909 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14910 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14911 NULL, vNULL, &data);
14912 restore_extension_diagnostics (ext);
14914 else
14915 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14916 NULL, vNULL, &data);
14920 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14921 IS_DEFN is true if we're applying it to the definition. */
14923 static void
14924 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14925 bool is_defn)
14927 /* Keep going if we're in error reporting mode. */
14928 if (data->error_seen
14929 || fndecl == error_mark_node)
14930 return;
14932 if (data->fndecl_seen)
14934 error_at (data->loc,
14935 "%<#pragma acc routine%> not immediately followed by"
14936 " a single function declaration or definition");
14937 data->error_seen = true;
14938 return;
14940 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14942 error_at (data->loc,
14943 "%<#pragma acc routine%> not immediately followed by"
14944 " function declaration or definition");
14945 data->error_seen = true;
14946 return;
14949 if (oacc_get_fn_attrib (fndecl))
14951 error_at (data->loc,
14952 "%<#pragma acc routine%> already applied to %qD", fndecl);
14953 data->error_seen = true;
14954 return;
14957 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14959 error_at (data->loc,
14960 TREE_USED (fndecl)
14961 ? G_("%<#pragma acc routine%> must be applied before use")
14962 : G_("%<#pragma acc routine%> must be applied before "
14963 "definition"));
14964 data->error_seen = true;
14965 return;
14968 /* Process the routine's dimension clauses. */
14969 tree dims = oacc_build_routine_dims (data->clauses);
14970 oacc_replace_fn_attrib (fndecl, dims);
14972 /* Add an "omp declare target" attribute. */
14973 DECL_ATTRIBUTES (fndecl)
14974 = tree_cons (get_identifier ("omp declare target"),
14975 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14977 /* Remember that we've used this "#pragma acc routine". */
14978 data->fndecl_seen = true;
14981 /* OpenACC 2.0:
14982 # pragma acc update oacc-update-clause[optseq] new-line
14985 #define OACC_UPDATE_CLAUSE_MASK \
14986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14993 static void
14994 c_parser_oacc_update (c_parser *parser)
14996 location_t loc = c_parser_peek_token (parser)->location;
14998 c_parser_consume_pragma (parser);
15000 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15001 "#pragma acc update");
15002 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15004 error_at (loc,
15005 "%<#pragma acc update%> must contain at least one "
15006 "%<device%> or %<host%> or %<self%> clause");
15007 return;
15010 if (parser->error)
15011 return;
15013 tree stmt = make_node (OACC_UPDATE);
15014 TREE_TYPE (stmt) = void_type_node;
15015 OACC_UPDATE_CLAUSES (stmt) = clauses;
15016 SET_EXPR_LOCATION (stmt, loc);
15017 add_stmt (stmt);
15020 /* OpenACC 2.0:
15021 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15023 LOC is the location of the #pragma token.
15026 #define OACC_WAIT_CLAUSE_MASK \
15027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15029 static tree
15030 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15032 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15034 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15035 list = c_parser_oacc_wait_list (parser, loc, list);
15037 strcpy (p_name, " wait");
15038 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15039 stmt = c_finish_oacc_wait (loc, list, clauses);
15040 add_stmt (stmt);
15042 return stmt;
15045 /* OpenMP 2.5:
15046 # pragma omp atomic new-line
15047 expression-stmt
15049 expression-stmt:
15050 x binop= expr | x++ | ++x | x-- | --x
15051 binop:
15052 +, *, -, /, &, ^, |, <<, >>
15054 where x is an lvalue expression with scalar type.
15056 OpenMP 3.1:
15057 # pragma omp atomic new-line
15058 update-stmt
15060 # pragma omp atomic read new-line
15061 read-stmt
15063 # pragma omp atomic write new-line
15064 write-stmt
15066 # pragma omp atomic update new-line
15067 update-stmt
15069 # pragma omp atomic capture new-line
15070 capture-stmt
15072 # pragma omp atomic capture new-line
15073 capture-block
15075 read-stmt:
15076 v = x
15077 write-stmt:
15078 x = expr
15079 update-stmt:
15080 expression-stmt | x = x binop expr
15081 capture-stmt:
15082 v = expression-stmt
15083 capture-block:
15084 { v = x; update-stmt; } | { update-stmt; v = x; }
15086 OpenMP 4.0:
15087 update-stmt:
15088 expression-stmt | x = x binop expr | x = expr binop x
15089 capture-stmt:
15090 v = update-stmt
15091 capture-block:
15092 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15094 where x and v are lvalue expressions with scalar type.
15096 LOC is the location of the #pragma token. */
15098 static void
15099 c_parser_omp_atomic (location_t loc, c_parser *parser)
15101 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15102 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15103 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15104 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15105 struct c_expr expr;
15106 location_t eloc;
15107 bool structured_block = false;
15108 bool swapped = false;
15109 bool seq_cst = false;
15110 bool non_lvalue_p;
15112 if (c_parser_next_token_is (parser, CPP_NAME))
15114 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15115 if (!strcmp (p, "seq_cst"))
15117 seq_cst = true;
15118 c_parser_consume_token (parser);
15119 if (c_parser_next_token_is (parser, CPP_COMMA)
15120 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15121 c_parser_consume_token (parser);
15124 if (c_parser_next_token_is (parser, CPP_NAME))
15126 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15128 if (!strcmp (p, "read"))
15129 code = OMP_ATOMIC_READ;
15130 else if (!strcmp (p, "write"))
15131 code = NOP_EXPR;
15132 else if (!strcmp (p, "update"))
15133 code = OMP_ATOMIC;
15134 else if (!strcmp (p, "capture"))
15135 code = OMP_ATOMIC_CAPTURE_NEW;
15136 else
15137 p = NULL;
15138 if (p)
15139 c_parser_consume_token (parser);
15141 if (!seq_cst)
15143 if (c_parser_next_token_is (parser, CPP_COMMA)
15144 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15145 c_parser_consume_token (parser);
15147 if (c_parser_next_token_is (parser, CPP_NAME))
15149 const char *p
15150 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15151 if (!strcmp (p, "seq_cst"))
15153 seq_cst = true;
15154 c_parser_consume_token (parser);
15158 c_parser_skip_to_pragma_eol (parser);
15160 switch (code)
15162 case OMP_ATOMIC_READ:
15163 case NOP_EXPR: /* atomic write */
15164 v = c_parser_cast_expression (parser, NULL).value;
15165 non_lvalue_p = !lvalue_p (v);
15166 v = c_fully_fold (v, false, NULL, true);
15167 if (v == error_mark_node)
15168 goto saw_error;
15169 if (non_lvalue_p)
15170 v = non_lvalue (v);
15171 loc = c_parser_peek_token (parser)->location;
15172 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15173 goto saw_error;
15174 if (code == NOP_EXPR)
15176 lhs = c_parser_expression (parser).value;
15177 lhs = c_fully_fold (lhs, false, NULL);
15178 if (lhs == error_mark_node)
15179 goto saw_error;
15181 else
15183 lhs = c_parser_cast_expression (parser, NULL).value;
15184 non_lvalue_p = !lvalue_p (lhs);
15185 lhs = c_fully_fold (lhs, false, NULL, true);
15186 if (lhs == error_mark_node)
15187 goto saw_error;
15188 if (non_lvalue_p)
15189 lhs = non_lvalue (lhs);
15191 if (code == NOP_EXPR)
15193 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15194 opcode. */
15195 code = OMP_ATOMIC;
15196 rhs = lhs;
15197 lhs = v;
15198 v = NULL_TREE;
15200 goto done;
15201 case OMP_ATOMIC_CAPTURE_NEW:
15202 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15204 c_parser_consume_token (parser);
15205 structured_block = true;
15207 else
15209 v = c_parser_cast_expression (parser, NULL).value;
15210 non_lvalue_p = !lvalue_p (v);
15211 v = c_fully_fold (v, false, NULL, true);
15212 if (v == error_mark_node)
15213 goto saw_error;
15214 if (non_lvalue_p)
15215 v = non_lvalue (v);
15216 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15217 goto saw_error;
15219 break;
15220 default:
15221 break;
15224 /* For structured_block case we don't know yet whether
15225 old or new x should be captured. */
15226 restart:
15227 eloc = c_parser_peek_token (parser)->location;
15228 expr = c_parser_cast_expression (parser, NULL);
15229 lhs = expr.value;
15230 expr = default_function_array_conversion (eloc, expr);
15231 unfolded_lhs = expr.value;
15232 lhs = c_fully_fold (lhs, false, NULL, true);
15233 orig_lhs = lhs;
15234 switch (TREE_CODE (lhs))
15236 case ERROR_MARK:
15237 saw_error:
15238 c_parser_skip_to_end_of_block_or_statement (parser);
15239 if (structured_block)
15241 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15242 c_parser_consume_token (parser);
15243 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15245 c_parser_skip_to_end_of_block_or_statement (parser);
15246 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15247 c_parser_consume_token (parser);
15250 return;
15252 case POSTINCREMENT_EXPR:
15253 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15254 code = OMP_ATOMIC_CAPTURE_OLD;
15255 /* FALLTHROUGH */
15256 case PREINCREMENT_EXPR:
15257 lhs = TREE_OPERAND (lhs, 0);
15258 unfolded_lhs = NULL_TREE;
15259 opcode = PLUS_EXPR;
15260 rhs = integer_one_node;
15261 break;
15263 case POSTDECREMENT_EXPR:
15264 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15265 code = OMP_ATOMIC_CAPTURE_OLD;
15266 /* FALLTHROUGH */
15267 case PREDECREMENT_EXPR:
15268 lhs = TREE_OPERAND (lhs, 0);
15269 unfolded_lhs = NULL_TREE;
15270 opcode = MINUS_EXPR;
15271 rhs = integer_one_node;
15272 break;
15274 case COMPOUND_EXPR:
15275 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15276 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15277 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15278 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15279 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15280 (TREE_OPERAND (lhs, 1), 0), 0)))
15281 == BOOLEAN_TYPE)
15282 /* Undo effects of boolean_increment for post {in,de}crement. */
15283 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15284 /* FALLTHRU */
15285 case MODIFY_EXPR:
15286 if (TREE_CODE (lhs) == MODIFY_EXPR
15287 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15289 /* Undo effects of boolean_increment. */
15290 if (integer_onep (TREE_OPERAND (lhs, 1)))
15292 /* This is pre or post increment. */
15293 rhs = TREE_OPERAND (lhs, 1);
15294 lhs = TREE_OPERAND (lhs, 0);
15295 unfolded_lhs = NULL_TREE;
15296 opcode = NOP_EXPR;
15297 if (code == OMP_ATOMIC_CAPTURE_NEW
15298 && !structured_block
15299 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15300 code = OMP_ATOMIC_CAPTURE_OLD;
15301 break;
15303 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15304 && TREE_OPERAND (lhs, 0)
15305 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15307 /* This is pre or post decrement. */
15308 rhs = TREE_OPERAND (lhs, 1);
15309 lhs = TREE_OPERAND (lhs, 0);
15310 unfolded_lhs = NULL_TREE;
15311 opcode = NOP_EXPR;
15312 if (code == OMP_ATOMIC_CAPTURE_NEW
15313 && !structured_block
15314 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15315 code = OMP_ATOMIC_CAPTURE_OLD;
15316 break;
15319 /* FALLTHRU */
15320 default:
15321 if (!lvalue_p (unfolded_lhs))
15322 lhs = non_lvalue (lhs);
15323 switch (c_parser_peek_token (parser)->type)
15325 case CPP_MULT_EQ:
15326 opcode = MULT_EXPR;
15327 break;
15328 case CPP_DIV_EQ:
15329 opcode = TRUNC_DIV_EXPR;
15330 break;
15331 case CPP_PLUS_EQ:
15332 opcode = PLUS_EXPR;
15333 break;
15334 case CPP_MINUS_EQ:
15335 opcode = MINUS_EXPR;
15336 break;
15337 case CPP_LSHIFT_EQ:
15338 opcode = LSHIFT_EXPR;
15339 break;
15340 case CPP_RSHIFT_EQ:
15341 opcode = RSHIFT_EXPR;
15342 break;
15343 case CPP_AND_EQ:
15344 opcode = BIT_AND_EXPR;
15345 break;
15346 case CPP_OR_EQ:
15347 opcode = BIT_IOR_EXPR;
15348 break;
15349 case CPP_XOR_EQ:
15350 opcode = BIT_XOR_EXPR;
15351 break;
15352 case CPP_EQ:
15353 c_parser_consume_token (parser);
15354 eloc = c_parser_peek_token (parser)->location;
15355 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15356 rhs1 = expr.value;
15357 switch (TREE_CODE (rhs1))
15359 case MULT_EXPR:
15360 case TRUNC_DIV_EXPR:
15361 case RDIV_EXPR:
15362 case PLUS_EXPR:
15363 case MINUS_EXPR:
15364 case LSHIFT_EXPR:
15365 case RSHIFT_EXPR:
15366 case BIT_AND_EXPR:
15367 case BIT_IOR_EXPR:
15368 case BIT_XOR_EXPR:
15369 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15371 opcode = TREE_CODE (rhs1);
15372 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15373 true);
15374 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15375 true);
15376 goto stmt_done;
15378 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15380 opcode = TREE_CODE (rhs1);
15381 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15382 true);
15383 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15384 true);
15385 swapped = !commutative_tree_code (opcode);
15386 goto stmt_done;
15388 break;
15389 case ERROR_MARK:
15390 goto saw_error;
15391 default:
15392 break;
15394 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15396 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15398 code = OMP_ATOMIC_CAPTURE_OLD;
15399 v = lhs;
15400 lhs = NULL_TREE;
15401 expr = default_function_array_read_conversion (eloc, expr);
15402 unfolded_lhs1 = expr.value;
15403 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15404 rhs1 = NULL_TREE;
15405 c_parser_consume_token (parser);
15406 goto restart;
15408 if (structured_block)
15410 opcode = NOP_EXPR;
15411 expr = default_function_array_read_conversion (eloc, expr);
15412 rhs = c_fully_fold (expr.value, false, NULL, true);
15413 rhs1 = NULL_TREE;
15414 goto stmt_done;
15417 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15418 goto saw_error;
15419 default:
15420 c_parser_error (parser,
15421 "invalid operator for %<#pragma omp atomic%>");
15422 goto saw_error;
15425 /* Arrange to pass the location of the assignment operator to
15426 c_finish_omp_atomic. */
15427 loc = c_parser_peek_token (parser)->location;
15428 c_parser_consume_token (parser);
15429 eloc = c_parser_peek_token (parser)->location;
15430 expr = c_parser_expression (parser);
15431 expr = default_function_array_read_conversion (eloc, expr);
15432 rhs = expr.value;
15433 rhs = c_fully_fold (rhs, false, NULL, true);
15434 break;
15436 stmt_done:
15437 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15439 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15440 goto saw_error;
15441 v = c_parser_cast_expression (parser, NULL).value;
15442 non_lvalue_p = !lvalue_p (v);
15443 v = c_fully_fold (v, false, NULL, true);
15444 if (v == error_mark_node)
15445 goto saw_error;
15446 if (non_lvalue_p)
15447 v = non_lvalue (v);
15448 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15449 goto saw_error;
15450 eloc = c_parser_peek_token (parser)->location;
15451 expr = c_parser_cast_expression (parser, NULL);
15452 lhs1 = expr.value;
15453 expr = default_function_array_read_conversion (eloc, expr);
15454 unfolded_lhs1 = expr.value;
15455 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15456 if (lhs1 == error_mark_node)
15457 goto saw_error;
15458 if (!lvalue_p (unfolded_lhs1))
15459 lhs1 = non_lvalue (lhs1);
15461 if (structured_block)
15463 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15464 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15466 done:
15467 if (unfolded_lhs && unfolded_lhs1
15468 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15470 error ("%<#pragma omp atomic capture%> uses two different "
15471 "expressions for memory");
15472 stmt = error_mark_node;
15474 else
15475 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15476 swapped, seq_cst);
15477 if (stmt != error_mark_node)
15478 add_stmt (stmt);
15480 if (!structured_block)
15481 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15485 /* OpenMP 2.5:
15486 # pragma omp barrier new-line
15489 static void
15490 c_parser_omp_barrier (c_parser *parser)
15492 location_t loc = c_parser_peek_token (parser)->location;
15493 c_parser_consume_pragma (parser);
15494 c_parser_skip_to_pragma_eol (parser);
15496 c_finish_omp_barrier (loc);
15499 /* OpenMP 2.5:
15500 # pragma omp critical [(name)] new-line
15501 structured-block
15503 OpenMP 4.5:
15504 # pragma omp critical [(name) [hint(expression)]] new-line
15506 LOC is the location of the #pragma itself. */
15508 #define OMP_CRITICAL_CLAUSE_MASK \
15509 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15511 static tree
15512 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15514 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15516 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15518 c_parser_consume_token (parser);
15519 if (c_parser_next_token_is (parser, CPP_NAME))
15521 name = c_parser_peek_token (parser)->value;
15522 c_parser_consume_token (parser);
15523 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15525 else
15526 c_parser_error (parser, "expected identifier");
15528 clauses = c_parser_omp_all_clauses (parser,
15529 OMP_CRITICAL_CLAUSE_MASK,
15530 "#pragma omp critical");
15532 else
15534 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15535 c_parser_error (parser, "expected %<(%> or end of line");
15536 c_parser_skip_to_pragma_eol (parser);
15539 stmt = c_parser_omp_structured_block (parser, if_p);
15540 return c_finish_omp_critical (loc, stmt, name, clauses);
15543 /* OpenMP 2.5:
15544 # pragma omp flush flush-vars[opt] new-line
15546 flush-vars:
15547 ( variable-list ) */
15549 static void
15550 c_parser_omp_flush (c_parser *parser)
15552 location_t loc = c_parser_peek_token (parser)->location;
15553 c_parser_consume_pragma (parser);
15554 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15555 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15556 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15557 c_parser_error (parser, "expected %<(%> or end of line");
15558 c_parser_skip_to_pragma_eol (parser);
15560 c_finish_omp_flush (loc);
15563 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15564 The real trick here is to determine the loop control variable early
15565 so that we can push a new decl if necessary to make it private.
15566 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15567 respectively. */
15569 static tree
15570 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15571 tree clauses, tree *cclauses, bool *if_p)
15573 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15574 tree declv, condv, incrv, initv, ret = NULL_TREE;
15575 tree pre_body = NULL_TREE, this_pre_body;
15576 tree ordered_cl = NULL_TREE;
15577 bool fail = false, open_brace_parsed = false;
15578 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15579 location_t for_loc;
15580 bool tiling = false;
15581 vec<tree, va_gc> *for_block = make_tree_vector ();
15583 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15584 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15585 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15586 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15588 tiling = true;
15589 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15591 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15592 && OMP_CLAUSE_ORDERED_EXPR (cl))
15594 ordered_cl = cl;
15595 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15598 if (ordered && ordered < collapse)
15600 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15601 "%<ordered%> clause parameter is less than %<collapse%>");
15602 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15603 = build_int_cst (NULL_TREE, collapse);
15604 ordered = collapse;
15606 if (ordered)
15608 for (tree *pc = &clauses; *pc; )
15609 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15611 error_at (OMP_CLAUSE_LOCATION (*pc),
15612 "%<linear%> clause may not be specified together "
15613 "with %<ordered%> clause with a parameter");
15614 *pc = OMP_CLAUSE_CHAIN (*pc);
15616 else
15617 pc = &OMP_CLAUSE_CHAIN (*pc);
15620 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15621 count = ordered ? ordered : collapse;
15623 declv = make_tree_vec (count);
15624 initv = make_tree_vec (count);
15625 condv = make_tree_vec (count);
15626 incrv = make_tree_vec (count);
15628 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15630 c_parser_error (parser, "for statement expected");
15631 return NULL;
15633 for_loc = c_parser_peek_token (parser)->location;
15634 c_parser_consume_token (parser);
15636 for (i = 0; i < count; i++)
15638 int bracecount = 0;
15640 matching_parens parens;
15641 if (!parens.require_open (parser))
15642 goto pop_scopes;
15644 /* Parse the initialization declaration or expression. */
15645 if (c_parser_next_tokens_start_declaration (parser))
15647 if (i > 0)
15648 vec_safe_push (for_block, c_begin_compound_stmt (true));
15649 this_pre_body = push_stmt_list ();
15650 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15651 NULL, vNULL);
15652 if (this_pre_body)
15654 this_pre_body = pop_stmt_list (this_pre_body);
15655 if (pre_body)
15657 tree t = pre_body;
15658 pre_body = push_stmt_list ();
15659 add_stmt (t);
15660 add_stmt (this_pre_body);
15661 pre_body = pop_stmt_list (pre_body);
15663 else
15664 pre_body = this_pre_body;
15666 decl = check_for_loop_decls (for_loc, flag_isoc99);
15667 if (decl == NULL)
15668 goto error_init;
15669 if (DECL_INITIAL (decl) == error_mark_node)
15670 decl = error_mark_node;
15671 init = decl;
15673 else if (c_parser_next_token_is (parser, CPP_NAME)
15674 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15676 struct c_expr decl_exp;
15677 struct c_expr init_exp;
15678 location_t init_loc;
15680 decl_exp = c_parser_postfix_expression (parser);
15681 decl = decl_exp.value;
15683 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15685 init_loc = c_parser_peek_token (parser)->location;
15686 init_exp = c_parser_expr_no_commas (parser, NULL);
15687 init_exp = default_function_array_read_conversion (init_loc,
15688 init_exp);
15689 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15690 NOP_EXPR, init_loc, init_exp.value,
15691 init_exp.original_type);
15692 init = c_process_expr_stmt (init_loc, init);
15694 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15696 else
15698 error_init:
15699 c_parser_error (parser,
15700 "expected iteration declaration or initialization");
15701 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15702 "expected %<)%>");
15703 fail = true;
15704 goto parse_next;
15707 /* Parse the loop condition. */
15708 cond = NULL_TREE;
15709 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15711 location_t cond_loc = c_parser_peek_token (parser)->location;
15712 struct c_expr cond_expr
15713 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15715 cond = cond_expr.value;
15716 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15717 if (COMPARISON_CLASS_P (cond))
15719 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15720 op0 = c_fully_fold (op0, false, NULL);
15721 op1 = c_fully_fold (op1, false, NULL);
15722 TREE_OPERAND (cond, 0) = op0;
15723 TREE_OPERAND (cond, 1) = op1;
15725 switch (cond_expr.original_code)
15727 case GT_EXPR:
15728 case GE_EXPR:
15729 case LT_EXPR:
15730 case LE_EXPR:
15731 break;
15732 default:
15733 /* Can't be cond = error_mark_node, because we want to preserve
15734 the location until c_finish_omp_for. */
15735 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15736 break;
15738 protected_set_expr_location (cond, cond_loc);
15740 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15742 /* Parse the increment expression. */
15743 incr = NULL_TREE;
15744 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15746 location_t incr_loc = c_parser_peek_token (parser)->location;
15748 incr = c_process_expr_stmt (incr_loc,
15749 c_parser_expression (parser).value);
15751 parens.skip_until_found_close (parser);
15753 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15754 fail = true;
15755 else
15757 TREE_VEC_ELT (declv, i) = decl;
15758 TREE_VEC_ELT (initv, i) = init;
15759 TREE_VEC_ELT (condv, i) = cond;
15760 TREE_VEC_ELT (incrv, i) = incr;
15763 parse_next:
15764 if (i == count - 1)
15765 break;
15767 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15768 in between the collapsed for loops to be still considered perfectly
15769 nested. Hopefully the final version clarifies this.
15770 For now handle (multiple) {'s and empty statements. */
15773 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15775 c_parser_consume_token (parser);
15776 break;
15778 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15780 c_parser_consume_token (parser);
15781 bracecount++;
15783 else if (bracecount
15784 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15785 c_parser_consume_token (parser);
15786 else
15788 c_parser_error (parser, "not enough perfectly nested loops");
15789 if (bracecount)
15791 open_brace_parsed = true;
15792 bracecount--;
15794 fail = true;
15795 count = 0;
15796 break;
15799 while (1);
15801 nbraces += bracecount;
15804 if (nbraces)
15805 if_p = NULL;
15807 save_break = c_break_label;
15808 c_break_label = size_one_node;
15809 save_cont = c_cont_label;
15810 c_cont_label = NULL_TREE;
15811 body = push_stmt_list ();
15813 if (open_brace_parsed)
15815 location_t here = c_parser_peek_token (parser)->location;
15816 stmt = c_begin_compound_stmt (true);
15817 c_parser_compound_statement_nostart (parser);
15818 add_stmt (c_end_compound_stmt (here, stmt, true));
15820 else
15821 add_stmt (c_parser_c99_block_statement (parser, if_p));
15822 if (c_cont_label)
15824 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15825 SET_EXPR_LOCATION (t, loc);
15826 add_stmt (t);
15829 body = pop_stmt_list (body);
15830 c_break_label = save_break;
15831 c_cont_label = save_cont;
15833 while (nbraces)
15835 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15837 c_parser_consume_token (parser);
15838 nbraces--;
15840 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15841 c_parser_consume_token (parser);
15842 else
15844 c_parser_error (parser, "collapsed loops not perfectly nested");
15845 while (nbraces)
15847 location_t here = c_parser_peek_token (parser)->location;
15848 stmt = c_begin_compound_stmt (true);
15849 add_stmt (body);
15850 c_parser_compound_statement_nostart (parser);
15851 body = c_end_compound_stmt (here, stmt, true);
15852 nbraces--;
15854 goto pop_scopes;
15858 /* Only bother calling c_finish_omp_for if we haven't already generated
15859 an error from the initialization parsing. */
15860 if (!fail)
15862 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15863 incrv, body, pre_body);
15865 /* Check for iterators appearing in lb, b or incr expressions. */
15866 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15867 stmt = NULL_TREE;
15869 if (stmt)
15871 add_stmt (stmt);
15873 if (cclauses != NULL
15874 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15876 tree *c;
15877 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15878 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15879 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15880 c = &OMP_CLAUSE_CHAIN (*c);
15881 else
15883 for (i = 0; i < count; i++)
15884 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15885 break;
15886 if (i == count)
15887 c = &OMP_CLAUSE_CHAIN (*c);
15888 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15890 error_at (loc,
15891 "iteration variable %qD should not be firstprivate",
15892 OMP_CLAUSE_DECL (*c));
15893 *c = OMP_CLAUSE_CHAIN (*c);
15895 else
15897 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15898 tree l = *c;
15899 *c = OMP_CLAUSE_CHAIN (*c);
15900 if (code == OMP_SIMD)
15902 OMP_CLAUSE_CHAIN (l)
15903 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15904 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15906 else
15908 OMP_CLAUSE_CHAIN (l) = clauses;
15909 clauses = l;
15914 OMP_FOR_CLAUSES (stmt) = clauses;
15916 ret = stmt;
15918 pop_scopes:
15919 while (!for_block->is_empty ())
15921 /* FIXME diagnostics: LOC below should be the actual location of
15922 this particular for block. We need to build a list of
15923 locations to go along with FOR_BLOCK. */
15924 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15925 add_stmt (stmt);
15927 release_tree_vector (for_block);
15928 return ret;
15931 /* Helper function for OpenMP parsing, split clauses and call
15932 finish_omp_clauses on each of the set of clauses afterwards. */
15934 static void
15935 omp_split_clauses (location_t loc, enum tree_code code,
15936 omp_clause_mask mask, tree clauses, tree *cclauses)
15938 int i;
15939 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15940 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15941 if (cclauses[i])
15942 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15945 /* OpenMP 4.0:
15946 #pragma omp simd simd-clause[optseq] new-line
15947 for-loop
15949 LOC is the location of the #pragma token.
15952 #define OMP_SIMD_CLAUSE_MASK \
15953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15962 static tree
15963 c_parser_omp_simd (location_t loc, c_parser *parser,
15964 char *p_name, omp_clause_mask mask, tree *cclauses,
15965 bool *if_p)
15967 tree block, clauses, ret;
15969 strcat (p_name, " simd");
15970 mask |= OMP_SIMD_CLAUSE_MASK;
15972 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15973 if (cclauses)
15975 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15976 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15977 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15978 OMP_CLAUSE_ORDERED);
15979 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15981 error_at (OMP_CLAUSE_LOCATION (c),
15982 "%<ordered%> clause with parameter may not be specified "
15983 "on %qs construct", p_name);
15984 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15988 block = c_begin_compound_stmt (true);
15989 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15990 block = c_end_compound_stmt (loc, block, true);
15991 add_stmt (block);
15993 return ret;
15996 /* OpenMP 2.5:
15997 #pragma omp for for-clause[optseq] new-line
15998 for-loop
16000 OpenMP 4.0:
16001 #pragma omp for simd for-simd-clause[optseq] new-line
16002 for-loop
16004 LOC is the location of the #pragma token.
16007 #define OMP_FOR_CLAUSE_MASK \
16008 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
16014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
16015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16018 static tree
16019 c_parser_omp_for (location_t loc, c_parser *parser,
16020 char *p_name, omp_clause_mask mask, tree *cclauses,
16021 bool *if_p)
16023 tree block, clauses, ret;
16025 strcat (p_name, " for");
16026 mask |= OMP_FOR_CLAUSE_MASK;
16027 /* parallel for{, simd} disallows nowait clause, but for
16028 target {teams distribute ,}parallel for{, simd} it should be accepted. */
16029 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16030 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16031 /* Composite distribute parallel for{, simd} disallows ordered clause. */
16032 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16033 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16035 if (c_parser_next_token_is (parser, CPP_NAME))
16037 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16039 if (strcmp (p, "simd") == 0)
16041 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16042 if (cclauses == NULL)
16043 cclauses = cclauses_buf;
16045 c_parser_consume_token (parser);
16046 if (!flag_openmp) /* flag_openmp_simd */
16047 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16048 if_p);
16049 block = c_begin_compound_stmt (true);
16050 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16051 block = c_end_compound_stmt (loc, block, true);
16052 if (ret == NULL_TREE)
16053 return ret;
16054 ret = make_node (OMP_FOR);
16055 TREE_TYPE (ret) = void_type_node;
16056 OMP_FOR_BODY (ret) = block;
16057 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16058 SET_EXPR_LOCATION (ret, loc);
16059 add_stmt (ret);
16060 return ret;
16063 if (!flag_openmp) /* flag_openmp_simd */
16065 c_parser_skip_to_pragma_eol (parser, false);
16066 return NULL_TREE;
16069 /* Composite distribute parallel for disallows linear clause. */
16070 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16071 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16073 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16074 if (cclauses)
16076 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16077 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16080 block = c_begin_compound_stmt (true);
16081 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16082 block = c_end_compound_stmt (loc, block, true);
16083 add_stmt (block);
16085 return ret;
16088 /* OpenMP 2.5:
16089 # pragma omp master new-line
16090 structured-block
16092 LOC is the location of the #pragma token.
16095 static tree
16096 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16098 c_parser_skip_to_pragma_eol (parser);
16099 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16100 if_p));
16103 /* OpenMP 2.5:
16104 # pragma omp ordered new-line
16105 structured-block
16107 OpenMP 4.5:
16108 # pragma omp ordered ordered-clauses new-line
16109 structured-block
16111 # pragma omp ordered depend-clauses new-line */
16113 #define OMP_ORDERED_CLAUSE_MASK \
16114 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16117 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16118 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16120 static bool
16121 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16122 bool *if_p)
16124 location_t loc = c_parser_peek_token (parser)->location;
16125 c_parser_consume_pragma (parser);
16127 if (context != pragma_stmt && context != pragma_compound)
16129 c_parser_error (parser, "expected declaration specifiers");
16130 c_parser_skip_to_pragma_eol (parser, false);
16131 return false;
16134 if (c_parser_next_token_is (parser, CPP_NAME))
16136 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16138 if (!strcmp ("depend", p))
16140 if (!flag_openmp) /* flag_openmp_simd */
16142 c_parser_skip_to_pragma_eol (parser, false);
16143 return false;
16145 if (context == pragma_stmt)
16147 error_at (loc,
16148 "%<#pragma omp ordered%> with %<depend%> clause may "
16149 "only be used in compound statements");
16150 c_parser_skip_to_pragma_eol (parser, false);
16151 return false;
16154 tree clauses
16155 = c_parser_omp_all_clauses (parser,
16156 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16157 "#pragma omp ordered");
16158 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16159 return false;
16163 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16164 "#pragma omp ordered");
16166 if (!flag_openmp /* flag_openmp_simd */
16167 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16168 return false;
16170 c_finish_omp_ordered (loc, clauses,
16171 c_parser_omp_structured_block (parser, if_p));
16172 return true;
16175 /* OpenMP 2.5:
16177 section-scope:
16178 { section-sequence }
16180 section-sequence:
16181 section-directive[opt] structured-block
16182 section-sequence section-directive structured-block
16184 SECTIONS_LOC is the location of the #pragma omp sections. */
16186 static tree
16187 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16189 tree stmt, substmt;
16190 bool error_suppress = false;
16191 location_t loc;
16193 loc = c_parser_peek_token (parser)->location;
16194 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16196 /* Avoid skipping until the end of the block. */
16197 parser->error = false;
16198 return NULL_TREE;
16201 stmt = push_stmt_list ();
16203 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16205 substmt = c_parser_omp_structured_block (parser, NULL);
16206 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16207 SET_EXPR_LOCATION (substmt, loc);
16208 add_stmt (substmt);
16211 while (1)
16213 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16214 break;
16215 if (c_parser_next_token_is (parser, CPP_EOF))
16216 break;
16218 loc = c_parser_peek_token (parser)->location;
16219 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16221 c_parser_consume_pragma (parser);
16222 c_parser_skip_to_pragma_eol (parser);
16223 error_suppress = false;
16225 else if (!error_suppress)
16227 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16228 error_suppress = true;
16231 substmt = c_parser_omp_structured_block (parser, NULL);
16232 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16233 SET_EXPR_LOCATION (substmt, loc);
16234 add_stmt (substmt);
16236 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16237 "expected %<#pragma omp section%> or %<}%>");
16239 substmt = pop_stmt_list (stmt);
16241 stmt = make_node (OMP_SECTIONS);
16242 SET_EXPR_LOCATION (stmt, sections_loc);
16243 TREE_TYPE (stmt) = void_type_node;
16244 OMP_SECTIONS_BODY (stmt) = substmt;
16246 return add_stmt (stmt);
16249 /* OpenMP 2.5:
16250 # pragma omp sections sections-clause[optseq] newline
16251 sections-scope
16253 LOC is the location of the #pragma token.
16256 #define OMP_SECTIONS_CLAUSE_MASK \
16257 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16263 static tree
16264 c_parser_omp_sections (location_t loc, c_parser *parser,
16265 char *p_name, omp_clause_mask mask, tree *cclauses)
16267 tree block, clauses, ret;
16269 strcat (p_name, " sections");
16270 mask |= OMP_SECTIONS_CLAUSE_MASK;
16271 if (cclauses)
16272 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16274 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16275 if (cclauses)
16277 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16278 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16281 block = c_begin_compound_stmt (true);
16282 ret = c_parser_omp_sections_scope (loc, parser);
16283 if (ret)
16284 OMP_SECTIONS_CLAUSES (ret) = clauses;
16285 block = c_end_compound_stmt (loc, block, true);
16286 add_stmt (block);
16288 return ret;
16291 /* OpenMP 2.5:
16292 # pragma omp parallel parallel-clause[optseq] new-line
16293 structured-block
16294 # pragma omp parallel for parallel-for-clause[optseq] new-line
16295 structured-block
16296 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16297 structured-block
16299 OpenMP 4.0:
16300 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16301 structured-block
16303 LOC is the location of the #pragma token.
16306 #define OMP_PARALLEL_CLAUSE_MASK \
16307 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16317 static tree
16318 c_parser_omp_parallel (location_t loc, c_parser *parser,
16319 char *p_name, omp_clause_mask mask, tree *cclauses,
16320 bool *if_p)
16322 tree stmt, clauses, block;
16324 strcat (p_name, " parallel");
16325 mask |= OMP_PARALLEL_CLAUSE_MASK;
16326 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16327 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16328 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16329 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16331 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16333 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16334 if (cclauses == NULL)
16335 cclauses = cclauses_buf;
16337 c_parser_consume_token (parser);
16338 if (!flag_openmp) /* flag_openmp_simd */
16339 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16340 block = c_begin_omp_parallel ();
16341 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16342 stmt
16343 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16344 block);
16345 if (ret == NULL_TREE)
16346 return ret;
16347 OMP_PARALLEL_COMBINED (stmt) = 1;
16348 return stmt;
16350 /* When combined with distribute, parallel has to be followed by for.
16351 #pragma omp target parallel is allowed though. */
16352 else if (cclauses
16353 && (mask & (OMP_CLAUSE_MASK_1
16354 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16356 error_at (loc, "expected %<for%> after %qs", p_name);
16357 c_parser_skip_to_pragma_eol (parser);
16358 return NULL_TREE;
16360 else if (!flag_openmp) /* flag_openmp_simd */
16362 c_parser_skip_to_pragma_eol (parser, false);
16363 return NULL_TREE;
16365 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16367 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16368 if (strcmp (p, "sections") == 0)
16370 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16371 if (cclauses == NULL)
16372 cclauses = cclauses_buf;
16374 c_parser_consume_token (parser);
16375 block = c_begin_omp_parallel ();
16376 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16377 stmt = c_finish_omp_parallel (loc,
16378 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16379 block);
16380 OMP_PARALLEL_COMBINED (stmt) = 1;
16381 return stmt;
16385 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16386 if (cclauses)
16388 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16389 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16392 block = c_begin_omp_parallel ();
16393 c_parser_statement (parser, if_p);
16394 stmt = c_finish_omp_parallel (loc, clauses, block);
16396 return stmt;
16399 /* OpenMP 2.5:
16400 # pragma omp single single-clause[optseq] new-line
16401 structured-block
16403 LOC is the location of the #pragma.
16406 #define OMP_SINGLE_CLAUSE_MASK \
16407 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16412 static tree
16413 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16415 tree stmt = make_node (OMP_SINGLE);
16416 SET_EXPR_LOCATION (stmt, loc);
16417 TREE_TYPE (stmt) = void_type_node;
16419 OMP_SINGLE_CLAUSES (stmt)
16420 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16421 "#pragma omp single");
16422 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16424 return add_stmt (stmt);
16427 /* OpenMP 3.0:
16428 # pragma omp task task-clause[optseq] new-line
16430 LOC is the location of the #pragma.
16433 #define OMP_TASK_CLAUSE_MASK \
16434 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16445 static tree
16446 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16448 tree clauses, block;
16450 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16451 "#pragma omp task");
16453 block = c_begin_omp_task ();
16454 c_parser_statement (parser, if_p);
16455 return c_finish_omp_task (loc, clauses, block);
16458 /* OpenMP 3.0:
16459 # pragma omp taskwait new-line
16462 static void
16463 c_parser_omp_taskwait (c_parser *parser)
16465 location_t loc = c_parser_peek_token (parser)->location;
16466 c_parser_consume_pragma (parser);
16467 c_parser_skip_to_pragma_eol (parser);
16469 c_finish_omp_taskwait (loc);
16472 /* OpenMP 3.1:
16473 # pragma omp taskyield new-line
16476 static void
16477 c_parser_omp_taskyield (c_parser *parser)
16479 location_t loc = c_parser_peek_token (parser)->location;
16480 c_parser_consume_pragma (parser);
16481 c_parser_skip_to_pragma_eol (parser);
16483 c_finish_omp_taskyield (loc);
16486 /* OpenMP 4.0:
16487 # pragma omp taskgroup new-line
16490 static tree
16491 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16493 location_t loc = c_parser_peek_token (parser)->location;
16494 c_parser_skip_to_pragma_eol (parser);
16495 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16496 if_p));
16499 /* OpenMP 4.0:
16500 # pragma omp cancel cancel-clause[optseq] new-line
16502 LOC is the location of the #pragma.
16505 #define OMP_CANCEL_CLAUSE_MASK \
16506 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16512 static void
16513 c_parser_omp_cancel (c_parser *parser)
16515 location_t loc = c_parser_peek_token (parser)->location;
16517 c_parser_consume_pragma (parser);
16518 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16519 "#pragma omp cancel");
16521 c_finish_omp_cancel (loc, clauses);
16524 /* OpenMP 4.0:
16525 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16527 LOC is the location of the #pragma.
16530 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16536 static void
16537 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16539 location_t loc = c_parser_peek_token (parser)->location;
16540 tree clauses;
16541 bool point_seen = false;
16543 c_parser_consume_pragma (parser);
16544 if (c_parser_next_token_is (parser, CPP_NAME))
16546 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16547 if (strcmp (p, "point") == 0)
16549 c_parser_consume_token (parser);
16550 point_seen = true;
16553 if (!point_seen)
16555 c_parser_error (parser, "expected %<point%>");
16556 c_parser_skip_to_pragma_eol (parser);
16557 return;
16560 if (context != pragma_compound)
16562 if (context == pragma_stmt)
16563 error_at (loc,
16564 "%<#pragma %s%> may only be used in compound statements",
16565 "omp cancellation point");
16566 else
16567 c_parser_error (parser, "expected declaration specifiers");
16568 c_parser_skip_to_pragma_eol (parser, false);
16569 return;
16572 clauses
16573 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16574 "#pragma omp cancellation point");
16576 c_finish_omp_cancellation_point (loc, clauses);
16579 /* OpenMP 4.0:
16580 #pragma omp distribute distribute-clause[optseq] new-line
16581 for-loop */
16583 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16584 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16590 static tree
16591 c_parser_omp_distribute (location_t loc, c_parser *parser,
16592 char *p_name, omp_clause_mask mask, tree *cclauses,
16593 bool *if_p)
16595 tree clauses, block, ret;
16597 strcat (p_name, " distribute");
16598 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16600 if (c_parser_next_token_is (parser, CPP_NAME))
16602 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16603 bool simd = false;
16604 bool parallel = false;
16606 if (strcmp (p, "simd") == 0)
16607 simd = true;
16608 else
16609 parallel = strcmp (p, "parallel") == 0;
16610 if (parallel || simd)
16612 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16613 if (cclauses == NULL)
16614 cclauses = cclauses_buf;
16615 c_parser_consume_token (parser);
16616 if (!flag_openmp) /* flag_openmp_simd */
16618 if (simd)
16619 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16620 if_p);
16621 else
16622 return c_parser_omp_parallel (loc, parser, p_name, mask,
16623 cclauses, if_p);
16625 block = c_begin_compound_stmt (true);
16626 if (simd)
16627 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16628 if_p);
16629 else
16630 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16631 if_p);
16632 block = c_end_compound_stmt (loc, block, true);
16633 if (ret == NULL)
16634 return ret;
16635 ret = make_node (OMP_DISTRIBUTE);
16636 TREE_TYPE (ret) = void_type_node;
16637 OMP_FOR_BODY (ret) = block;
16638 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16639 SET_EXPR_LOCATION (ret, loc);
16640 add_stmt (ret);
16641 return ret;
16644 if (!flag_openmp) /* flag_openmp_simd */
16646 c_parser_skip_to_pragma_eol (parser, false);
16647 return NULL_TREE;
16650 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16651 if (cclauses)
16653 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16654 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16657 block = c_begin_compound_stmt (true);
16658 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16659 if_p);
16660 block = c_end_compound_stmt (loc, block, true);
16661 add_stmt (block);
16663 return ret;
16666 /* OpenMP 4.0:
16667 # pragma omp teams teams-clause[optseq] new-line
16668 structured-block */
16670 #define OMP_TEAMS_CLAUSE_MASK \
16671 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16679 static tree
16680 c_parser_omp_teams (location_t loc, c_parser *parser,
16681 char *p_name, omp_clause_mask mask, tree *cclauses,
16682 bool *if_p)
16684 tree clauses, block, ret;
16686 strcat (p_name, " teams");
16687 mask |= OMP_TEAMS_CLAUSE_MASK;
16689 if (c_parser_next_token_is (parser, CPP_NAME))
16691 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16692 if (strcmp (p, "distribute") == 0)
16694 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16695 if (cclauses == NULL)
16696 cclauses = cclauses_buf;
16698 c_parser_consume_token (parser);
16699 if (!flag_openmp) /* flag_openmp_simd */
16700 return c_parser_omp_distribute (loc, parser, p_name, mask,
16701 cclauses, if_p);
16702 block = c_begin_compound_stmt (true);
16703 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16704 if_p);
16705 block = c_end_compound_stmt (loc, block, true);
16706 if (ret == NULL)
16707 return ret;
16708 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16709 ret = make_node (OMP_TEAMS);
16710 TREE_TYPE (ret) = void_type_node;
16711 OMP_TEAMS_CLAUSES (ret) = clauses;
16712 OMP_TEAMS_BODY (ret) = block;
16713 OMP_TEAMS_COMBINED (ret) = 1;
16714 return add_stmt (ret);
16717 if (!flag_openmp) /* flag_openmp_simd */
16719 c_parser_skip_to_pragma_eol (parser, false);
16720 return NULL_TREE;
16723 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16724 if (cclauses)
16726 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16727 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16730 tree stmt = make_node (OMP_TEAMS);
16731 TREE_TYPE (stmt) = void_type_node;
16732 OMP_TEAMS_CLAUSES (stmt) = clauses;
16733 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16735 return add_stmt (stmt);
16738 /* OpenMP 4.0:
16739 # pragma omp target data target-data-clause[optseq] new-line
16740 structured-block */
16742 #define OMP_TARGET_DATA_CLAUSE_MASK \
16743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16748 static tree
16749 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16751 tree clauses
16752 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16753 "#pragma omp target data");
16754 int map_seen = 0;
16755 for (tree *pc = &clauses; *pc;)
16757 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16758 switch (OMP_CLAUSE_MAP_KIND (*pc))
16760 case GOMP_MAP_TO:
16761 case GOMP_MAP_ALWAYS_TO:
16762 case GOMP_MAP_FROM:
16763 case GOMP_MAP_ALWAYS_FROM:
16764 case GOMP_MAP_TOFROM:
16765 case GOMP_MAP_ALWAYS_TOFROM:
16766 case GOMP_MAP_ALLOC:
16767 map_seen = 3;
16768 break;
16769 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16770 case GOMP_MAP_ALWAYS_POINTER:
16771 break;
16772 default:
16773 map_seen |= 1;
16774 error_at (OMP_CLAUSE_LOCATION (*pc),
16775 "%<#pragma omp target data%> with map-type other "
16776 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16777 "on %<map%> clause");
16778 *pc = OMP_CLAUSE_CHAIN (*pc);
16779 continue;
16781 pc = &OMP_CLAUSE_CHAIN (*pc);
16784 if (map_seen != 3)
16786 if (map_seen == 0)
16787 error_at (loc,
16788 "%<#pragma omp target data%> must contain at least "
16789 "one %<map%> clause");
16790 return NULL_TREE;
16793 tree stmt = make_node (OMP_TARGET_DATA);
16794 TREE_TYPE (stmt) = void_type_node;
16795 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16796 keep_next_level ();
16797 tree block = c_begin_compound_stmt (true);
16798 add_stmt (c_parser_omp_structured_block (parser, if_p));
16799 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16801 SET_EXPR_LOCATION (stmt, loc);
16802 return add_stmt (stmt);
16805 /* OpenMP 4.0:
16806 # pragma omp target update target-update-clause[optseq] new-line */
16808 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16809 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16816 static bool
16817 c_parser_omp_target_update (location_t loc, c_parser *parser,
16818 enum pragma_context context)
16820 if (context == pragma_stmt)
16822 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16823 "omp target update");
16824 c_parser_skip_to_pragma_eol (parser, false);
16825 return false;
16828 tree clauses
16829 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16830 "#pragma omp target update");
16831 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16832 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16834 error_at (loc,
16835 "%<#pragma omp target update%> must contain at least one "
16836 "%<from%> or %<to%> clauses");
16837 return false;
16840 tree stmt = make_node (OMP_TARGET_UPDATE);
16841 TREE_TYPE (stmt) = void_type_node;
16842 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16843 SET_EXPR_LOCATION (stmt, loc);
16844 add_stmt (stmt);
16845 return false;
16848 /* OpenMP 4.5:
16849 # pragma omp target enter data target-data-clause[optseq] new-line */
16851 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16852 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16858 static tree
16859 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16860 enum pragma_context context)
16862 bool data_seen = false;
16863 if (c_parser_next_token_is (parser, CPP_NAME))
16865 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16866 if (strcmp (p, "data") == 0)
16868 c_parser_consume_token (parser);
16869 data_seen = true;
16872 if (!data_seen)
16874 c_parser_error (parser, "expected %<data%>");
16875 c_parser_skip_to_pragma_eol (parser);
16876 return NULL_TREE;
16879 if (context == pragma_stmt)
16881 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16882 "omp target enter data");
16883 c_parser_skip_to_pragma_eol (parser, false);
16884 return NULL_TREE;
16887 tree clauses
16888 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16889 "#pragma omp target enter data");
16890 int map_seen = 0;
16891 for (tree *pc = &clauses; *pc;)
16893 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16894 switch (OMP_CLAUSE_MAP_KIND (*pc))
16896 case GOMP_MAP_TO:
16897 case GOMP_MAP_ALWAYS_TO:
16898 case GOMP_MAP_ALLOC:
16899 map_seen = 3;
16900 break;
16901 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16902 case GOMP_MAP_ALWAYS_POINTER:
16903 break;
16904 default:
16905 map_seen |= 1;
16906 error_at (OMP_CLAUSE_LOCATION (*pc),
16907 "%<#pragma omp target enter data%> with map-type other "
16908 "than %<to%> or %<alloc%> on %<map%> clause");
16909 *pc = OMP_CLAUSE_CHAIN (*pc);
16910 continue;
16912 pc = &OMP_CLAUSE_CHAIN (*pc);
16915 if (map_seen != 3)
16917 if (map_seen == 0)
16918 error_at (loc,
16919 "%<#pragma omp target enter data%> must contain at least "
16920 "one %<map%> clause");
16921 return NULL_TREE;
16924 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16925 TREE_TYPE (stmt) = void_type_node;
16926 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16927 SET_EXPR_LOCATION (stmt, loc);
16928 add_stmt (stmt);
16929 return stmt;
16932 /* OpenMP 4.5:
16933 # pragma omp target exit data target-data-clause[optseq] new-line */
16935 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16936 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16942 static tree
16943 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16944 enum pragma_context context)
16946 bool data_seen = false;
16947 if (c_parser_next_token_is (parser, CPP_NAME))
16949 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16950 if (strcmp (p, "data") == 0)
16952 c_parser_consume_token (parser);
16953 data_seen = true;
16956 if (!data_seen)
16958 c_parser_error (parser, "expected %<data%>");
16959 c_parser_skip_to_pragma_eol (parser);
16960 return NULL_TREE;
16963 if (context == pragma_stmt)
16965 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16966 "omp target exit data");
16967 c_parser_skip_to_pragma_eol (parser, false);
16968 return NULL_TREE;
16971 tree clauses
16972 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16973 "#pragma omp target exit data");
16975 int map_seen = 0;
16976 for (tree *pc = &clauses; *pc;)
16978 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16979 switch (OMP_CLAUSE_MAP_KIND (*pc))
16981 case GOMP_MAP_FROM:
16982 case GOMP_MAP_ALWAYS_FROM:
16983 case GOMP_MAP_RELEASE:
16984 case GOMP_MAP_DELETE:
16985 map_seen = 3;
16986 break;
16987 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16988 case GOMP_MAP_ALWAYS_POINTER:
16989 break;
16990 default:
16991 map_seen |= 1;
16992 error_at (OMP_CLAUSE_LOCATION (*pc),
16993 "%<#pragma omp target exit data%> with map-type other "
16994 "than %<from%>, %<release%> or %<delete%> on %<map%>"
16995 " clause");
16996 *pc = OMP_CLAUSE_CHAIN (*pc);
16997 continue;
16999 pc = &OMP_CLAUSE_CHAIN (*pc);
17002 if (map_seen != 3)
17004 if (map_seen == 0)
17005 error_at (loc,
17006 "%<#pragma omp target exit data%> must contain at least one "
17007 "%<map%> clause");
17008 return NULL_TREE;
17011 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
17012 TREE_TYPE (stmt) = void_type_node;
17013 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
17014 SET_EXPR_LOCATION (stmt, loc);
17015 add_stmt (stmt);
17016 return stmt;
17019 /* OpenMP 4.0:
17020 # pragma omp target target-clause[optseq] new-line
17021 structured-block */
17023 #define OMP_TARGET_CLAUSE_MASK \
17024 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
17029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
17032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17034 static bool
17035 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17037 location_t loc = c_parser_peek_token (parser)->location;
17038 c_parser_consume_pragma (parser);
17039 tree *pc = NULL, stmt, block;
17041 if (context != pragma_stmt && context != pragma_compound)
17043 c_parser_error (parser, "expected declaration specifiers");
17044 c_parser_skip_to_pragma_eol (parser);
17045 return false;
17048 if (c_parser_next_token_is (parser, CPP_NAME))
17050 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17051 enum tree_code ccode = ERROR_MARK;
17053 if (strcmp (p, "teams") == 0)
17054 ccode = OMP_TEAMS;
17055 else if (strcmp (p, "parallel") == 0)
17056 ccode = OMP_PARALLEL;
17057 else if (strcmp (p, "simd") == 0)
17058 ccode = OMP_SIMD;
17059 if (ccode != ERROR_MARK)
17061 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17062 char p_name[sizeof ("#pragma omp target teams distribute "
17063 "parallel for simd")];
17065 c_parser_consume_token (parser);
17066 strcpy (p_name, "#pragma omp target");
17067 if (!flag_openmp) /* flag_openmp_simd */
17069 tree stmt;
17070 switch (ccode)
17072 case OMP_TEAMS:
17073 stmt = c_parser_omp_teams (loc, parser, p_name,
17074 OMP_TARGET_CLAUSE_MASK,
17075 cclauses, if_p);
17076 break;
17077 case OMP_PARALLEL:
17078 stmt = c_parser_omp_parallel (loc, parser, p_name,
17079 OMP_TARGET_CLAUSE_MASK,
17080 cclauses, if_p);
17081 break;
17082 case OMP_SIMD:
17083 stmt = c_parser_omp_simd (loc, parser, p_name,
17084 OMP_TARGET_CLAUSE_MASK,
17085 cclauses, if_p);
17086 break;
17087 default:
17088 gcc_unreachable ();
17090 return stmt != NULL_TREE;
17092 keep_next_level ();
17093 tree block = c_begin_compound_stmt (true), ret;
17094 switch (ccode)
17096 case OMP_TEAMS:
17097 ret = c_parser_omp_teams (loc, parser, p_name,
17098 OMP_TARGET_CLAUSE_MASK, cclauses,
17099 if_p);
17100 break;
17101 case OMP_PARALLEL:
17102 ret = c_parser_omp_parallel (loc, parser, p_name,
17103 OMP_TARGET_CLAUSE_MASK, cclauses,
17104 if_p);
17105 break;
17106 case OMP_SIMD:
17107 ret = c_parser_omp_simd (loc, parser, p_name,
17108 OMP_TARGET_CLAUSE_MASK, cclauses,
17109 if_p);
17110 break;
17111 default:
17112 gcc_unreachable ();
17114 block = c_end_compound_stmt (loc, block, true);
17115 if (ret == NULL_TREE)
17116 return false;
17117 if (ccode == OMP_TEAMS)
17119 /* For combined target teams, ensure the num_teams and
17120 thread_limit clause expressions are evaluated on the host,
17121 before entering the target construct. */
17122 tree c;
17123 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17124 c; c = OMP_CLAUSE_CHAIN (c))
17125 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17126 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17127 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17129 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17130 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17131 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17132 expr, NULL_TREE, NULL_TREE);
17133 add_stmt (expr);
17134 OMP_CLAUSE_OPERAND (c, 0) = expr;
17135 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17136 OMP_CLAUSE_FIRSTPRIVATE);
17137 OMP_CLAUSE_DECL (tc) = tmp;
17138 OMP_CLAUSE_CHAIN (tc)
17139 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17140 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17143 tree stmt = make_node (OMP_TARGET);
17144 TREE_TYPE (stmt) = void_type_node;
17145 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17146 OMP_TARGET_BODY (stmt) = block;
17147 OMP_TARGET_COMBINED (stmt) = 1;
17148 add_stmt (stmt);
17149 pc = &OMP_TARGET_CLAUSES (stmt);
17150 goto check_clauses;
17152 else if (!flag_openmp) /* flag_openmp_simd */
17154 c_parser_skip_to_pragma_eol (parser, false);
17155 return false;
17157 else if (strcmp (p, "data") == 0)
17159 c_parser_consume_token (parser);
17160 c_parser_omp_target_data (loc, parser, if_p);
17161 return true;
17163 else if (strcmp (p, "enter") == 0)
17165 c_parser_consume_token (parser);
17166 c_parser_omp_target_enter_data (loc, parser, context);
17167 return false;
17169 else if (strcmp (p, "exit") == 0)
17171 c_parser_consume_token (parser);
17172 c_parser_omp_target_exit_data (loc, parser, context);
17173 return false;
17175 else if (strcmp (p, "update") == 0)
17177 c_parser_consume_token (parser);
17178 return c_parser_omp_target_update (loc, parser, context);
17181 if (!flag_openmp) /* flag_openmp_simd */
17183 c_parser_skip_to_pragma_eol (parser, false);
17184 return false;
17187 stmt = make_node (OMP_TARGET);
17188 TREE_TYPE (stmt) = void_type_node;
17190 OMP_TARGET_CLAUSES (stmt)
17191 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17192 "#pragma omp target");
17193 pc = &OMP_TARGET_CLAUSES (stmt);
17194 keep_next_level ();
17195 block = c_begin_compound_stmt (true);
17196 add_stmt (c_parser_omp_structured_block (parser, if_p));
17197 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17199 SET_EXPR_LOCATION (stmt, loc);
17200 add_stmt (stmt);
17202 check_clauses:
17203 while (*pc)
17205 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17206 switch (OMP_CLAUSE_MAP_KIND (*pc))
17208 case GOMP_MAP_TO:
17209 case GOMP_MAP_ALWAYS_TO:
17210 case GOMP_MAP_FROM:
17211 case GOMP_MAP_ALWAYS_FROM:
17212 case GOMP_MAP_TOFROM:
17213 case GOMP_MAP_ALWAYS_TOFROM:
17214 case GOMP_MAP_ALLOC:
17215 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17216 case GOMP_MAP_ALWAYS_POINTER:
17217 break;
17218 default:
17219 error_at (OMP_CLAUSE_LOCATION (*pc),
17220 "%<#pragma omp target%> with map-type other "
17221 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17222 "on %<map%> clause");
17223 *pc = OMP_CLAUSE_CHAIN (*pc);
17224 continue;
17226 pc = &OMP_CLAUSE_CHAIN (*pc);
17228 return true;
17231 /* OpenMP 4.0:
17232 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17234 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17235 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17242 static void
17243 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17245 auto_vec<c_token> clauses;
17246 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17248 c_token *token = c_parser_peek_token (parser);
17249 if (token->type == CPP_EOF)
17251 c_parser_skip_to_pragma_eol (parser);
17252 return;
17254 clauses.safe_push (*token);
17255 c_parser_consume_token (parser);
17257 clauses.safe_push (*c_parser_peek_token (parser));
17258 c_parser_skip_to_pragma_eol (parser);
17260 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17262 if (c_parser_peek_token (parser)->pragma_kind
17263 != PRAGMA_OMP_DECLARE
17264 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17265 || strcmp (IDENTIFIER_POINTER
17266 (c_parser_peek_2nd_token (parser)->value),
17267 "simd") != 0)
17269 c_parser_error (parser,
17270 "%<#pragma omp declare simd%> must be followed by "
17271 "function declaration or definition or another "
17272 "%<#pragma omp declare simd%>");
17273 return;
17275 c_parser_consume_pragma (parser);
17276 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17278 c_token *token = c_parser_peek_token (parser);
17279 if (token->type == CPP_EOF)
17281 c_parser_skip_to_pragma_eol (parser);
17282 return;
17284 clauses.safe_push (*token);
17285 c_parser_consume_token (parser);
17287 clauses.safe_push (*c_parser_peek_token (parser));
17288 c_parser_skip_to_pragma_eol (parser);
17291 /* Make sure nothing tries to read past the end of the tokens. */
17292 c_token eof_token;
17293 memset (&eof_token, 0, sizeof (eof_token));
17294 eof_token.type = CPP_EOF;
17295 clauses.safe_push (eof_token);
17296 clauses.safe_push (eof_token);
17298 switch (context)
17300 case pragma_external:
17301 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17302 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17304 int ext = disable_extension_diagnostics ();
17306 c_parser_consume_token (parser);
17307 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17308 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17309 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17310 NULL, clauses);
17311 restore_extension_diagnostics (ext);
17313 else
17314 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17315 NULL, clauses);
17316 break;
17317 case pragma_struct:
17318 case pragma_param:
17319 case pragma_stmt:
17320 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17321 "function declaration or definition");
17322 break;
17323 case pragma_compound:
17324 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17325 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17327 int ext = disable_extension_diagnostics ();
17329 c_parser_consume_token (parser);
17330 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17331 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17332 if (c_parser_next_tokens_start_declaration (parser))
17334 c_parser_declaration_or_fndef (parser, true, true, true, true,
17335 true, NULL, clauses);
17336 restore_extension_diagnostics (ext);
17337 break;
17339 restore_extension_diagnostics (ext);
17341 else if (c_parser_next_tokens_start_declaration (parser))
17343 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17344 NULL, clauses);
17345 break;
17347 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17348 "function declaration or definition");
17349 break;
17350 default:
17351 gcc_unreachable ();
17355 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17356 and put that into "omp declare simd" attribute. */
17358 static void
17359 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17360 vec<c_token> clauses)
17362 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17363 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17364 has already processed the tokens. */
17365 if (clauses.exists () && clauses[0].type == CPP_EOF)
17366 return;
17367 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17369 error ("%<#pragma omp declare simd%> not immediately followed by "
17370 "a function declaration or definition");
17371 clauses[0].type = CPP_EOF;
17372 return;
17374 if (clauses.exists () && clauses[0].type != CPP_NAME)
17376 error_at (DECL_SOURCE_LOCATION (fndecl),
17377 "%<#pragma omp declare simd%> not immediately followed by "
17378 "a single function declaration or definition");
17379 clauses[0].type = CPP_EOF;
17380 return;
17383 if (parms == NULL_TREE)
17384 parms = DECL_ARGUMENTS (fndecl);
17386 unsigned int tokens_avail = parser->tokens_avail;
17387 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17390 parser->tokens = clauses.address ();
17391 parser->tokens_avail = clauses.length ();
17393 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17394 while (parser->tokens_avail > 3)
17396 c_token *token = c_parser_peek_token (parser);
17397 gcc_assert (token->type == CPP_NAME
17398 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17399 c_parser_consume_token (parser);
17400 parser->in_pragma = true;
17402 tree c = NULL_TREE;
17403 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17404 "#pragma omp declare simd");
17405 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17406 if (c != NULL_TREE)
17407 c = tree_cons (NULL_TREE, c, NULL_TREE);
17408 c = build_tree_list (get_identifier ("omp declare simd"), c);
17409 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17410 DECL_ATTRIBUTES (fndecl) = c;
17413 parser->tokens = &parser->tokens_buf[0];
17414 parser->tokens_avail = tokens_avail;
17415 if (clauses.exists ())
17416 clauses[0].type = CPP_PRAGMA;
17420 /* OpenMP 4.0:
17421 # pragma omp declare target new-line
17422 declarations and definitions
17423 # pragma omp end declare target new-line
17425 OpenMP 4.5:
17426 # pragma omp declare target ( extended-list ) new-line
17428 # pragma omp declare target declare-target-clauses[seq] new-line */
17430 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17431 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17434 static void
17435 c_parser_omp_declare_target (c_parser *parser)
17437 location_t loc = c_parser_peek_token (parser)->location;
17438 tree clauses = NULL_TREE;
17439 if (c_parser_next_token_is (parser, CPP_NAME))
17440 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17441 "#pragma omp declare target");
17442 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17444 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17445 clauses);
17446 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17447 c_parser_skip_to_pragma_eol (parser);
17449 else
17451 c_parser_skip_to_pragma_eol (parser);
17452 current_omp_declare_target_attribute++;
17453 return;
17455 if (current_omp_declare_target_attribute)
17456 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17457 "%<#pragma omp declare target%> without clauses and "
17458 "%<#pragma omp end declare target%>");
17459 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17461 tree t = OMP_CLAUSE_DECL (c), id;
17462 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17463 tree at2 = lookup_attribute ("omp declare target link",
17464 DECL_ATTRIBUTES (t));
17465 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17467 id = get_identifier ("omp declare target link");
17468 std::swap (at1, at2);
17470 else
17471 id = get_identifier ("omp declare target");
17472 if (at2)
17474 error_at (OMP_CLAUSE_LOCATION (c),
17475 "%qD specified both in declare target %<link%> and %<to%>"
17476 " clauses", t);
17477 continue;
17479 if (!at1)
17481 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17482 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17483 continue;
17485 symtab_node *node = symtab_node::get (t);
17486 if (node != NULL)
17488 node->offloadable = 1;
17489 if (ENABLE_OFFLOADING)
17491 g->have_offload = true;
17492 if (is_a <varpool_node *> (node))
17493 vec_safe_push (offload_vars, t);
17500 static void
17501 c_parser_omp_end_declare_target (c_parser *parser)
17503 location_t loc = c_parser_peek_token (parser)->location;
17504 c_parser_consume_pragma (parser);
17505 if (c_parser_next_token_is (parser, CPP_NAME)
17506 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17507 "declare") == 0)
17509 c_parser_consume_token (parser);
17510 if (c_parser_next_token_is (parser, CPP_NAME)
17511 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17512 "target") == 0)
17513 c_parser_consume_token (parser);
17514 else
17516 c_parser_error (parser, "expected %<target%>");
17517 c_parser_skip_to_pragma_eol (parser);
17518 return;
17521 else
17523 c_parser_error (parser, "expected %<declare%>");
17524 c_parser_skip_to_pragma_eol (parser);
17525 return;
17527 c_parser_skip_to_pragma_eol (parser);
17528 if (!current_omp_declare_target_attribute)
17529 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17530 "%<#pragma omp declare target%>");
17531 else
17532 current_omp_declare_target_attribute--;
17536 /* OpenMP 4.0
17537 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17538 initializer-clause[opt] new-line
17540 initializer-clause:
17541 initializer (omp_priv = initializer)
17542 initializer (function-name (argument-list)) */
17544 static void
17545 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17547 unsigned int tokens_avail = 0, i;
17548 vec<tree> types = vNULL;
17549 vec<c_token> clauses = vNULL;
17550 enum tree_code reduc_code = ERROR_MARK;
17551 tree reduc_id = NULL_TREE;
17552 tree type;
17553 location_t rloc = c_parser_peek_token (parser)->location;
17555 if (context == pragma_struct || context == pragma_param)
17557 error ("%<#pragma omp declare reduction%> not at file or block scope");
17558 goto fail;
17561 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17562 goto fail;
17564 switch (c_parser_peek_token (parser)->type)
17566 case CPP_PLUS:
17567 reduc_code = PLUS_EXPR;
17568 break;
17569 case CPP_MULT:
17570 reduc_code = MULT_EXPR;
17571 break;
17572 case CPP_MINUS:
17573 reduc_code = MINUS_EXPR;
17574 break;
17575 case CPP_AND:
17576 reduc_code = BIT_AND_EXPR;
17577 break;
17578 case CPP_XOR:
17579 reduc_code = BIT_XOR_EXPR;
17580 break;
17581 case CPP_OR:
17582 reduc_code = BIT_IOR_EXPR;
17583 break;
17584 case CPP_AND_AND:
17585 reduc_code = TRUTH_ANDIF_EXPR;
17586 break;
17587 case CPP_OR_OR:
17588 reduc_code = TRUTH_ORIF_EXPR;
17589 break;
17590 case CPP_NAME:
17591 const char *p;
17592 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17593 if (strcmp (p, "min") == 0)
17595 reduc_code = MIN_EXPR;
17596 break;
17598 if (strcmp (p, "max") == 0)
17600 reduc_code = MAX_EXPR;
17601 break;
17603 reduc_id = c_parser_peek_token (parser)->value;
17604 break;
17605 default:
17606 c_parser_error (parser,
17607 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17608 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17609 goto fail;
17612 tree orig_reduc_id, reduc_decl;
17613 orig_reduc_id = reduc_id;
17614 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17615 reduc_decl = c_omp_reduction_decl (reduc_id);
17616 c_parser_consume_token (parser);
17618 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17619 goto fail;
17621 while (true)
17623 location_t loc = c_parser_peek_token (parser)->location;
17624 struct c_type_name *ctype = c_parser_type_name (parser);
17625 if (ctype != NULL)
17627 type = groktypename (ctype, NULL, NULL);
17628 if (type == error_mark_node)
17630 else if ((INTEGRAL_TYPE_P (type)
17631 || TREE_CODE (type) == REAL_TYPE
17632 || TREE_CODE (type) == COMPLEX_TYPE)
17633 && orig_reduc_id == NULL_TREE)
17634 error_at (loc, "predeclared arithmetic type in "
17635 "%<#pragma omp declare reduction%>");
17636 else if (TREE_CODE (type) == FUNCTION_TYPE
17637 || TREE_CODE (type) == ARRAY_TYPE)
17638 error_at (loc, "function or array type in "
17639 "%<#pragma omp declare reduction%>");
17640 else if (TYPE_ATOMIC (type))
17641 error_at (loc, "%<_Atomic%> qualified type in "
17642 "%<#pragma omp declare reduction%>");
17643 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17644 error_at (loc, "const, volatile or restrict qualified type in "
17645 "%<#pragma omp declare reduction%>");
17646 else
17648 tree t;
17649 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17650 if (comptypes (TREE_PURPOSE (t), type))
17652 error_at (loc, "redeclaration of %qs "
17653 "%<#pragma omp declare reduction%> for "
17654 "type %qT",
17655 IDENTIFIER_POINTER (reduc_id)
17656 + sizeof ("omp declare reduction ") - 1,
17657 type);
17658 location_t ploc
17659 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17660 0));
17661 error_at (ploc, "previous %<#pragma omp declare "
17662 "reduction%>");
17663 break;
17665 if (t == NULL_TREE)
17666 types.safe_push (type);
17668 if (c_parser_next_token_is (parser, CPP_COMMA))
17669 c_parser_consume_token (parser);
17670 else
17671 break;
17673 else
17674 break;
17677 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17678 || types.is_empty ())
17680 fail:
17681 clauses.release ();
17682 types.release ();
17683 while (true)
17685 c_token *token = c_parser_peek_token (parser);
17686 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17687 break;
17688 c_parser_consume_token (parser);
17690 c_parser_skip_to_pragma_eol (parser);
17691 return;
17694 if (types.length () > 1)
17696 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17698 c_token *token = c_parser_peek_token (parser);
17699 if (token->type == CPP_EOF)
17700 goto fail;
17701 clauses.safe_push (*token);
17702 c_parser_consume_token (parser);
17704 clauses.safe_push (*c_parser_peek_token (parser));
17705 c_parser_skip_to_pragma_eol (parser);
17707 /* Make sure nothing tries to read past the end of the tokens. */
17708 c_token eof_token;
17709 memset (&eof_token, 0, sizeof (eof_token));
17710 eof_token.type = CPP_EOF;
17711 clauses.safe_push (eof_token);
17712 clauses.safe_push (eof_token);
17715 int errs = errorcount;
17716 FOR_EACH_VEC_ELT (types, i, type)
17718 tokens_avail = parser->tokens_avail;
17719 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17720 if (!clauses.is_empty ())
17722 parser->tokens = clauses.address ();
17723 parser->tokens_avail = clauses.length ();
17724 parser->in_pragma = true;
17727 bool nested = current_function_decl != NULL_TREE;
17728 if (nested)
17729 c_push_function_context ();
17730 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17731 reduc_id, default_function_type);
17732 current_function_decl = fndecl;
17733 allocate_struct_function (fndecl, true);
17734 push_scope ();
17735 tree stmt = push_stmt_list ();
17736 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17737 warn about these. */
17738 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17739 get_identifier ("omp_out"), type);
17740 DECL_ARTIFICIAL (omp_out) = 1;
17741 DECL_CONTEXT (omp_out) = fndecl;
17742 pushdecl (omp_out);
17743 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17744 get_identifier ("omp_in"), type);
17745 DECL_ARTIFICIAL (omp_in) = 1;
17746 DECL_CONTEXT (omp_in) = fndecl;
17747 pushdecl (omp_in);
17748 struct c_expr combiner = c_parser_expression (parser);
17749 struct c_expr initializer;
17750 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17751 bool bad = false;
17752 initializer.set_error ();
17753 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17754 bad = true;
17755 else if (c_parser_next_token_is (parser, CPP_NAME)
17756 && strcmp (IDENTIFIER_POINTER
17757 (c_parser_peek_token (parser)->value),
17758 "initializer") == 0)
17760 c_parser_consume_token (parser);
17761 pop_scope ();
17762 push_scope ();
17763 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17764 get_identifier ("omp_priv"), type);
17765 DECL_ARTIFICIAL (omp_priv) = 1;
17766 DECL_INITIAL (omp_priv) = error_mark_node;
17767 DECL_CONTEXT (omp_priv) = fndecl;
17768 pushdecl (omp_priv);
17769 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17770 get_identifier ("omp_orig"), type);
17771 DECL_ARTIFICIAL (omp_orig) = 1;
17772 DECL_CONTEXT (omp_orig) = fndecl;
17773 pushdecl (omp_orig);
17774 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17775 bad = true;
17776 else if (!c_parser_next_token_is (parser, CPP_NAME))
17778 c_parser_error (parser, "expected %<omp_priv%> or "
17779 "function-name");
17780 bad = true;
17782 else if (strcmp (IDENTIFIER_POINTER
17783 (c_parser_peek_token (parser)->value),
17784 "omp_priv") != 0)
17786 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17787 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17789 c_parser_error (parser, "expected function-name %<(%>");
17790 bad = true;
17792 else
17793 initializer = c_parser_postfix_expression (parser);
17794 if (initializer.value
17795 && TREE_CODE (initializer.value) == CALL_EXPR)
17797 int j;
17798 tree c = initializer.value;
17799 for (j = 0; j < call_expr_nargs (c); j++)
17801 tree a = CALL_EXPR_ARG (c, j);
17802 STRIP_NOPS (a);
17803 if (TREE_CODE (a) == ADDR_EXPR
17804 && TREE_OPERAND (a, 0) == omp_priv)
17805 break;
17807 if (j == call_expr_nargs (c))
17808 error ("one of the initializer call arguments should be "
17809 "%<&omp_priv%>");
17812 else
17814 c_parser_consume_token (parser);
17815 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17816 bad = true;
17817 else
17819 tree st = push_stmt_list ();
17820 location_t loc = c_parser_peek_token (parser)->location;
17821 rich_location richloc (line_table, loc);
17822 start_init (omp_priv, NULL_TREE, 0, &richloc);
17823 struct c_expr init = c_parser_initializer (parser);
17824 finish_init ();
17825 finish_decl (omp_priv, loc, init.value,
17826 init.original_type, NULL_TREE);
17827 pop_stmt_list (st);
17830 if (!bad
17831 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17832 bad = true;
17835 if (!bad)
17837 c_parser_skip_to_pragma_eol (parser);
17839 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17840 DECL_INITIAL (reduc_decl));
17841 DECL_INITIAL (reduc_decl) = t;
17842 DECL_SOURCE_LOCATION (omp_out) = rloc;
17843 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17844 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17845 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17846 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17847 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17848 if (omp_priv)
17850 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17851 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17852 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17853 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17854 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17855 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17856 walk_tree (&DECL_INITIAL (omp_priv),
17857 c_check_omp_declare_reduction_r,
17858 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17862 pop_stmt_list (stmt);
17863 pop_scope ();
17864 if (cfun->language != NULL)
17866 ggc_free (cfun->language);
17867 cfun->language = NULL;
17869 set_cfun (NULL);
17870 current_function_decl = NULL_TREE;
17871 if (nested)
17872 c_pop_function_context ();
17874 if (!clauses.is_empty ())
17876 parser->tokens = &parser->tokens_buf[0];
17877 parser->tokens_avail = tokens_avail;
17879 if (bad)
17880 goto fail;
17881 if (errs != errorcount)
17882 break;
17885 clauses.release ();
17886 types.release ();
17890 /* OpenMP 4.0
17891 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17892 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17893 initializer-clause[opt] new-line
17894 #pragma omp declare target new-line */
17896 static void
17897 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17899 c_parser_consume_pragma (parser);
17900 if (c_parser_next_token_is (parser, CPP_NAME))
17902 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17903 if (strcmp (p, "simd") == 0)
17905 /* c_parser_consume_token (parser); done in
17906 c_parser_omp_declare_simd. */
17907 c_parser_omp_declare_simd (parser, context);
17908 return;
17910 if (strcmp (p, "reduction") == 0)
17912 c_parser_consume_token (parser);
17913 c_parser_omp_declare_reduction (parser, context);
17914 return;
17916 if (!flag_openmp) /* flag_openmp_simd */
17918 c_parser_skip_to_pragma_eol (parser, false);
17919 return;
17921 if (strcmp (p, "target") == 0)
17923 c_parser_consume_token (parser);
17924 c_parser_omp_declare_target (parser);
17925 return;
17929 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17930 "or %<target%>");
17931 c_parser_skip_to_pragma_eol (parser);
17934 /* OpenMP 4.5:
17935 #pragma omp taskloop taskloop-clause[optseq] new-line
17936 for-loop
17938 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17939 for-loop */
17941 #define OMP_TASKLOOP_CLAUSE_MASK \
17942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17957 static tree
17958 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17959 char *p_name, omp_clause_mask mask, tree *cclauses,
17960 bool *if_p)
17962 tree clauses, block, ret;
17964 strcat (p_name, " taskloop");
17965 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17967 if (c_parser_next_token_is (parser, CPP_NAME))
17969 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17971 if (strcmp (p, "simd") == 0)
17973 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17974 if (cclauses == NULL)
17975 cclauses = cclauses_buf;
17976 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17977 c_parser_consume_token (parser);
17978 if (!flag_openmp) /* flag_openmp_simd */
17979 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17980 if_p);
17981 block = c_begin_compound_stmt (true);
17982 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17983 block = c_end_compound_stmt (loc, block, true);
17984 if (ret == NULL)
17985 return ret;
17986 ret = make_node (OMP_TASKLOOP);
17987 TREE_TYPE (ret) = void_type_node;
17988 OMP_FOR_BODY (ret) = block;
17989 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17990 SET_EXPR_LOCATION (ret, loc);
17991 add_stmt (ret);
17992 return ret;
17995 if (!flag_openmp) /* flag_openmp_simd */
17997 c_parser_skip_to_pragma_eol (parser, false);
17998 return NULL_TREE;
18001 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18002 if (cclauses)
18004 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
18005 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18008 block = c_begin_compound_stmt (true);
18009 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
18010 block = c_end_compound_stmt (loc, block, true);
18011 add_stmt (block);
18013 return ret;
18016 /* Main entry point to parsing most OpenMP pragmas. */
18018 static void
18019 c_parser_omp_construct (c_parser *parser, bool *if_p)
18021 enum pragma_kind p_kind;
18022 location_t loc;
18023 tree stmt;
18024 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18025 omp_clause_mask mask (0);
18027 loc = c_parser_peek_token (parser)->location;
18028 p_kind = c_parser_peek_token (parser)->pragma_kind;
18029 c_parser_consume_pragma (parser);
18031 switch (p_kind)
18033 case PRAGMA_OACC_ATOMIC:
18034 c_parser_omp_atomic (loc, parser);
18035 return;
18036 case PRAGMA_OACC_CACHE:
18037 strcpy (p_name, "#pragma acc");
18038 stmt = c_parser_oacc_cache (loc, parser);
18039 break;
18040 case PRAGMA_OACC_DATA:
18041 stmt = c_parser_oacc_data (loc, parser, if_p);
18042 break;
18043 case PRAGMA_OACC_HOST_DATA:
18044 stmt = c_parser_oacc_host_data (loc, parser, if_p);
18045 break;
18046 case PRAGMA_OACC_KERNELS:
18047 case PRAGMA_OACC_PARALLEL:
18048 strcpy (p_name, "#pragma acc");
18049 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18050 if_p);
18051 break;
18052 case PRAGMA_OACC_LOOP:
18053 strcpy (p_name, "#pragma acc");
18054 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18055 break;
18056 case PRAGMA_OACC_WAIT:
18057 strcpy (p_name, "#pragma wait");
18058 stmt = c_parser_oacc_wait (loc, parser, p_name);
18059 break;
18060 case PRAGMA_OMP_ATOMIC:
18061 c_parser_omp_atomic (loc, parser);
18062 return;
18063 case PRAGMA_OMP_CRITICAL:
18064 stmt = c_parser_omp_critical (loc, parser, if_p);
18065 break;
18066 case PRAGMA_OMP_DISTRIBUTE:
18067 strcpy (p_name, "#pragma omp");
18068 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18069 break;
18070 case PRAGMA_OMP_FOR:
18071 strcpy (p_name, "#pragma omp");
18072 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18073 break;
18074 case PRAGMA_OMP_MASTER:
18075 stmt = c_parser_omp_master (loc, parser, if_p);
18076 break;
18077 case PRAGMA_OMP_PARALLEL:
18078 strcpy (p_name, "#pragma omp");
18079 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18080 break;
18081 case PRAGMA_OMP_SECTIONS:
18082 strcpy (p_name, "#pragma omp");
18083 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18084 break;
18085 case PRAGMA_OMP_SIMD:
18086 strcpy (p_name, "#pragma omp");
18087 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18088 break;
18089 case PRAGMA_OMP_SINGLE:
18090 stmt = c_parser_omp_single (loc, parser, if_p);
18091 break;
18092 case PRAGMA_OMP_TASK:
18093 stmt = c_parser_omp_task (loc, parser, if_p);
18094 break;
18095 case PRAGMA_OMP_TASKGROUP:
18096 stmt = c_parser_omp_taskgroup (parser, if_p);
18097 break;
18098 case PRAGMA_OMP_TASKLOOP:
18099 strcpy (p_name, "#pragma omp");
18100 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18101 break;
18102 case PRAGMA_OMP_TEAMS:
18103 strcpy (p_name, "#pragma omp");
18104 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18105 break;
18106 default:
18107 gcc_unreachable ();
18110 if (stmt)
18111 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18115 /* OpenMP 2.5:
18116 # pragma omp threadprivate (variable-list) */
18118 static void
18119 c_parser_omp_threadprivate (c_parser *parser)
18121 tree vars, t;
18122 location_t loc;
18124 c_parser_consume_pragma (parser);
18125 loc = c_parser_peek_token (parser)->location;
18126 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18128 /* Mark every variable in VARS to be assigned thread local storage. */
18129 for (t = vars; t; t = TREE_CHAIN (t))
18131 tree v = TREE_PURPOSE (t);
18133 /* FIXME diagnostics: Ideally we should keep individual
18134 locations for all the variables in the var list to make the
18135 following errors more precise. Perhaps
18136 c_parser_omp_var_list_parens() should construct a list of
18137 locations to go along with the var list. */
18139 /* If V had already been marked threadprivate, it doesn't matter
18140 whether it had been used prior to this point. */
18141 if (!VAR_P (v))
18142 error_at (loc, "%qD is not a variable", v);
18143 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18144 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18145 else if (! is_global_var (v))
18146 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18147 else if (TREE_TYPE (v) == error_mark_node)
18149 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18150 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18151 else
18153 if (! DECL_THREAD_LOCAL_P (v))
18155 set_decl_tls_model (v, decl_default_tls_model (v));
18156 /* If rtl has been already set for this var, call
18157 make_decl_rtl once again, so that encode_section_info
18158 has a chance to look at the new decl flags. */
18159 if (DECL_RTL_SET_P (v))
18160 make_decl_rtl (v);
18162 C_DECL_THREADPRIVATE_P (v) = 1;
18166 c_parser_skip_to_pragma_eol (parser);
18169 /* Parse a transaction attribute (GCC Extension).
18171 transaction-attribute:
18172 attributes
18173 [ [ any-word ] ]
18175 The transactional memory language description is written for C++,
18176 and uses the C++0x attribute syntax. For compatibility, allow the
18177 bracket style for transactions in C as well. */
18179 static tree
18180 c_parser_transaction_attributes (c_parser *parser)
18182 tree attr_name, attr = NULL;
18184 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18185 return c_parser_attributes (parser);
18187 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18188 return NULL_TREE;
18189 c_parser_consume_token (parser);
18190 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18191 goto error1;
18193 attr_name = c_parser_attribute_any_word (parser);
18194 if (attr_name)
18196 c_parser_consume_token (parser);
18197 attr = build_tree_list (attr_name, NULL_TREE);
18199 else
18200 c_parser_error (parser, "expected identifier");
18202 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18203 error1:
18204 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18205 return attr;
18208 /* Parse a __transaction_atomic or __transaction_relaxed statement
18209 (GCC Extension).
18211 transaction-statement:
18212 __transaction_atomic transaction-attribute[opt] compound-statement
18213 __transaction_relaxed compound-statement
18215 Note that the only valid attribute is: "outer".
18218 static tree
18219 c_parser_transaction (c_parser *parser, enum rid keyword)
18221 unsigned int old_in = parser->in_transaction;
18222 unsigned int this_in = 1, new_in;
18223 location_t loc = c_parser_peek_token (parser)->location;
18224 tree stmt, attrs;
18226 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18227 || keyword == RID_TRANSACTION_RELAXED)
18228 && c_parser_next_token_is_keyword (parser, keyword));
18229 c_parser_consume_token (parser);
18231 if (keyword == RID_TRANSACTION_RELAXED)
18232 this_in |= TM_STMT_ATTR_RELAXED;
18233 else
18235 attrs = c_parser_transaction_attributes (parser);
18236 if (attrs)
18237 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18240 /* Keep track if we're in the lexical scope of an outer transaction. */
18241 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18243 parser->in_transaction = new_in;
18244 stmt = c_parser_compound_statement (parser);
18245 parser->in_transaction = old_in;
18247 if (flag_tm)
18248 stmt = c_finish_transaction (loc, stmt, this_in);
18249 else
18250 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18251 "%<__transaction_atomic%> without transactional memory support enabled"
18252 : "%<__transaction_relaxed %> "
18253 "without transactional memory support enabled"));
18255 return stmt;
18258 /* Parse a __transaction_atomic or __transaction_relaxed expression
18259 (GCC Extension).
18261 transaction-expression:
18262 __transaction_atomic ( expression )
18263 __transaction_relaxed ( expression )
18266 static struct c_expr
18267 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18269 struct c_expr ret;
18270 unsigned int old_in = parser->in_transaction;
18271 unsigned int this_in = 1;
18272 location_t loc = c_parser_peek_token (parser)->location;
18273 tree attrs;
18275 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18276 || keyword == RID_TRANSACTION_RELAXED)
18277 && c_parser_next_token_is_keyword (parser, keyword));
18278 c_parser_consume_token (parser);
18280 if (keyword == RID_TRANSACTION_RELAXED)
18281 this_in |= TM_STMT_ATTR_RELAXED;
18282 else
18284 attrs = c_parser_transaction_attributes (parser);
18285 if (attrs)
18286 this_in |= parse_tm_stmt_attr (attrs, 0);
18289 parser->in_transaction = this_in;
18290 matching_parens parens;
18291 if (parens.require_open (parser))
18293 tree expr = c_parser_expression (parser).value;
18294 ret.original_type = TREE_TYPE (expr);
18295 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18296 if (this_in & TM_STMT_ATTR_RELAXED)
18297 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18298 SET_EXPR_LOCATION (ret.value, loc);
18299 ret.original_code = TRANSACTION_EXPR;
18300 if (!parens.require_close (parser))
18302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18303 goto error;
18306 else
18308 error:
18309 ret.set_error ();
18310 ret.original_code = ERROR_MARK;
18311 ret.original_type = NULL;
18313 parser->in_transaction = old_in;
18315 if (!flag_tm)
18316 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18317 "%<__transaction_atomic%> without transactional memory support enabled"
18318 : "%<__transaction_relaxed %> "
18319 "without transactional memory support enabled"));
18321 set_c_expr_source_range (&ret, loc, loc);
18323 return ret;
18326 /* Parse a __transaction_cancel statement (GCC Extension).
18328 transaction-cancel-statement:
18329 __transaction_cancel transaction-attribute[opt] ;
18331 Note that the only valid attribute is "outer".
18334 static tree
18335 c_parser_transaction_cancel (c_parser *parser)
18337 location_t loc = c_parser_peek_token (parser)->location;
18338 tree attrs;
18339 bool is_outer = false;
18341 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18342 c_parser_consume_token (parser);
18344 attrs = c_parser_transaction_attributes (parser);
18345 if (attrs)
18346 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18348 if (!flag_tm)
18350 error_at (loc, "%<__transaction_cancel%> without "
18351 "transactional memory support enabled");
18352 goto ret_error;
18354 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18356 error_at (loc, "%<__transaction_cancel%> within a "
18357 "%<__transaction_relaxed%>");
18358 goto ret_error;
18360 else if (is_outer)
18362 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18363 && !is_tm_may_cancel_outer (current_function_decl))
18365 error_at (loc, "outer %<__transaction_cancel%> not "
18366 "within outer %<__transaction_atomic%>");
18367 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18368 goto ret_error;
18371 else if (parser->in_transaction == 0)
18373 error_at (loc, "%<__transaction_cancel%> not within "
18374 "%<__transaction_atomic%>");
18375 goto ret_error;
18378 return add_stmt (build_tm_abort_call (loc, is_outer));
18380 ret_error:
18381 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18384 /* Parse a single source file. */
18386 void
18387 c_parse_file (void)
18389 /* Use local storage to begin. If the first token is a pragma, parse it.
18390 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18391 which will cause garbage collection. */
18392 c_parser tparser;
18394 memset (&tparser, 0, sizeof tparser);
18395 tparser.tokens = &tparser.tokens_buf[0];
18396 the_parser = &tparser;
18398 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18399 c_parser_pragma_pch_preprocess (&tparser);
18401 the_parser = ggc_alloc<c_parser> ();
18402 *the_parser = tparser;
18403 if (tparser.tokens == &tparser.tokens_buf[0])
18404 the_parser->tokens = &the_parser->tokens_buf[0];
18406 /* Initialize EH, if we've been told to do so. */
18407 if (flag_exceptions)
18408 using_eh_for_cleanups ();
18410 c_parser_translation_unit (the_parser);
18411 the_parser = NULL;
18414 /* Parse the body of a function declaration marked with "__RTL".
18416 The RTL parser works on the level of characters read from a
18417 FILE *, whereas c_parser works at the level of tokens.
18418 Square this circle by consuming all of the tokens up to and
18419 including the closing brace, recording the start/end of the RTL
18420 fragment, and reopening the file and re-reading the relevant
18421 lines within the RTL parser.
18423 This requires the opening and closing braces of the C function
18424 to be on separate lines from the RTL they wrap.
18426 Take ownership of START_WITH_PASS, if non-NULL. */
18428 void
18429 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18431 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18433 free (start_with_pass);
18434 return;
18437 location_t start_loc = c_parser_peek_token (parser)->location;
18439 /* Consume all tokens, up to the closing brace, handling
18440 matching pairs of braces in the rtl dump. */
18441 int num_open_braces = 1;
18442 while (1)
18444 switch (c_parser_peek_token (parser)->type)
18446 case CPP_OPEN_BRACE:
18447 num_open_braces++;
18448 break;
18449 case CPP_CLOSE_BRACE:
18450 if (--num_open_braces == 0)
18451 goto found_closing_brace;
18452 break;
18453 case CPP_EOF:
18454 error_at (start_loc, "no closing brace");
18455 free (start_with_pass);
18456 return;
18457 default:
18458 break;
18460 c_parser_consume_token (parser);
18463 found_closing_brace:
18464 /* At the closing brace; record its location. */
18465 location_t end_loc = c_parser_peek_token (parser)->location;
18467 /* Consume the closing brace. */
18468 c_parser_consume_token (parser);
18470 /* Invoke the RTL parser. */
18471 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18473 free (start_with_pass);
18474 return;
18477 /* If a pass name was provided for START_WITH_PASS, run the backend
18478 accordingly now, on the cfun created above, transferring
18479 ownership of START_WITH_PASS. */
18480 if (start_with_pass)
18481 run_rtl_passes (start_with_pass);
18484 #include "gt-c-c-parser.h"