CWG 616, 1213 - value category of subobject references.
[official-gcc.git] / gcc / c / c-parser.c
blob6b41a615dbd480559792aa62adcc1e7acb91d57b
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
72 /* We need to walk over decls with incomplete struct/union/enum types
73 after parsing the whole translation unit.
74 In finish_decl(), if the decl is static, has incomplete
75 struct/union/enum type, it is appeneded to incomplete_record_decls.
76 In c_parser_translation_unit(), we iterate over incomplete_record_decls
77 and report error if any of the decls are still incomplete. */
79 vec<tree> incomplete_record_decls;
81 void
82 set_c_expr_source_range (c_expr *expr,
83 location_t start, location_t finish)
85 expr->src_range.m_start = start;
86 expr->src_range.m_finish = finish;
87 if (expr->value)
88 set_source_range (expr->value, start, finish);
91 void
92 set_c_expr_source_range (c_expr *expr,
93 source_range src_range)
95 expr->src_range = src_range;
96 if (expr->value)
97 set_source_range (expr->value, src_range);
101 /* Initialization routine for this file. */
103 void
104 c_parse_init (void)
106 /* The only initialization required is of the reserved word
107 identifiers. */
108 unsigned int i;
109 tree id;
110 int mask = 0;
112 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
113 the c_token structure. */
114 gcc_assert (RID_MAX <= 255);
116 mask |= D_CXXONLY;
117 if (!flag_isoc99)
118 mask |= D_C99;
119 if (flag_no_asm)
121 mask |= D_ASM | D_EXT;
122 if (!flag_isoc99)
123 mask |= D_EXT89;
125 if (!c_dialect_objc ())
126 mask |= D_OBJC | D_CXX_OBJC;
128 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
129 for (i = 0; i < num_c_common_reswords; i++)
131 /* If a keyword is disabled, do not enter it into the table
132 and so create a canonical spelling that isn't a keyword. */
133 if (c_common_reswords[i].disable & mask)
135 if (warn_cxx_compat
136 && (c_common_reswords[i].disable & D_CXXWARN))
138 id = get_identifier (c_common_reswords[i].word);
139 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
140 C_IS_RESERVED_WORD (id) = 1;
142 continue;
145 id = get_identifier (c_common_reswords[i].word);
146 C_SET_RID_CODE (id, c_common_reswords[i].rid);
147 C_IS_RESERVED_WORD (id) = 1;
148 ridpointers [(int) c_common_reswords[i].rid] = id;
151 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 /* We always create the symbols but they aren't always supported. */
154 char name[50];
155 sprintf (name, "__int%d", int_n_data[i].bitsize);
156 id = get_identifier (name);
157 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
158 C_IS_RESERVED_WORD (id) = 1;
162 /* A parser structure recording information about the state and
163 context of parsing. Includes lexer information with up to two
164 tokens of look-ahead; more are not needed for C. */
165 struct GTY(()) c_parser {
166 /* The look-ahead tokens. */
167 c_token * GTY((skip)) tokens;
168 /* Buffer for look-ahead tokens. */
169 c_token tokens_buf[4];
170 /* How many look-ahead tokens are available (0 - 4, or
171 more if parsing from pre-lexed tokens). */
172 unsigned int tokens_avail;
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
185 /* Objective-C specific parser/lexer information. */
187 /* True if we are in a context where the Objective-C "PQ" keywords
188 are considered keywords. */
189 BOOL_BITFIELD objc_pq_context : 1;
190 /* True if we are parsing a (potential) Objective-C foreach
191 statement. This is set to true after we parsed 'for (' and while
192 we wait for 'in' or ';' to decide if it's a standard C for loop or an
193 Objective-C foreach loop. */
194 BOOL_BITFIELD objc_could_be_foreach_context : 1;
195 /* The following flag is needed to contextualize Objective-C lexical
196 analysis. In some cases (e.g., 'int NSObject;'), it is
197 undesirable to bind an identifier to an Objective-C class, even
198 if a class with that name exists. */
199 BOOL_BITFIELD objc_need_raw_identifier : 1;
200 /* Nonzero if we're processing a __transaction statement. The value
201 is 1 | TM_STMT_ATTR_*. */
202 unsigned int in_transaction : 4;
203 /* True if we are in a context where the Objective-C "Property attribute"
204 keywords are valid. */
205 BOOL_BITFIELD objc_property_attr_context : 1;
207 /* Location of the last consumed token. */
208 location_t last_token_location;
211 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
213 c_token *
214 c_parser_tokens_buf (c_parser *parser, unsigned n)
216 return &parser->tokens_buf[n];
219 /* Return the error state of PARSER. */
221 bool
222 c_parser_error (c_parser *parser)
224 return parser->error;
227 /* Set the error state of PARSER to ERR. */
229 void
230 c_parser_set_error (c_parser *parser, bool err)
232 parser->error = err;
236 /* The actual parser and external interface. ??? Does this need to be
237 garbage-collected? */
239 static GTY (()) c_parser *the_parser;
241 /* Read in and lex a single token, storing it in *TOKEN. */
243 static void
244 c_lex_one_token (c_parser *parser, c_token *token)
246 timevar_push (TV_LEX);
248 token->type = c_lex_with_flags (&token->value, &token->location,
249 &token->flags,
250 (parser->lex_untranslated_string
251 ? C_LEX_STRING_NO_TRANSLATE : 0));
252 token->id_kind = C_ID_NONE;
253 token->keyword = RID_MAX;
254 token->pragma_kind = PRAGMA_NONE;
256 switch (token->type)
258 case CPP_NAME:
260 tree decl;
262 bool objc_force_identifier = parser->objc_need_raw_identifier;
263 if (c_dialect_objc ())
264 parser->objc_need_raw_identifier = false;
266 if (C_IS_RESERVED_WORD (token->value))
268 enum rid rid_code = C_RID_CODE (token->value);
270 if (rid_code == RID_CXX_COMPAT_WARN)
272 warning_at (token->location,
273 OPT_Wc___compat,
274 "identifier %qE conflicts with C++ keyword",
275 token->value);
277 else if (rid_code >= RID_FIRST_ADDR_SPACE
278 && rid_code <= RID_LAST_ADDR_SPACE)
280 addr_space_t as;
281 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
282 targetm.addr_space.diagnose_usage (as, token->location);
283 token->id_kind = C_ID_ADDRSPACE;
284 token->keyword = rid_code;
285 break;
287 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
289 /* We found an Objective-C "pq" keyword (in, out,
290 inout, bycopy, byref, oneway). They need special
291 care because the interpretation depends on the
292 context. */
293 if (parser->objc_pq_context)
295 token->type = CPP_KEYWORD;
296 token->keyword = rid_code;
297 break;
299 else if (parser->objc_could_be_foreach_context
300 && rid_code == RID_IN)
302 /* We are in Objective-C, inside a (potential)
303 foreach context (which means after having
304 parsed 'for (', but before having parsed ';'),
305 and we found 'in'. We consider it the keyword
306 which terminates the declaration at the
307 beginning of a foreach-statement. Note that
308 this means you can't use 'in' for anything else
309 in that context; in particular, in Objective-C
310 you can't use 'in' as the name of the running
311 variable in a C for loop. We could potentially
312 try to add code here to disambiguate, but it
313 seems a reasonable limitation. */
314 token->type = CPP_KEYWORD;
315 token->keyword = rid_code;
316 break;
318 /* Else, "pq" keywords outside of the "pq" context are
319 not keywords, and we fall through to the code for
320 normal tokens. */
322 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
324 /* We found an Objective-C "property attribute"
325 keyword (getter, setter, readonly, etc). These are
326 only valid in the property context. */
327 if (parser->objc_property_attr_context)
329 token->type = CPP_KEYWORD;
330 token->keyword = rid_code;
331 break;
333 /* Else they are not special keywords.
336 else if (c_dialect_objc ()
337 && (OBJC_IS_AT_KEYWORD (rid_code)
338 || OBJC_IS_CXX_KEYWORD (rid_code)))
340 /* We found one of the Objective-C "@" keywords (defs,
341 selector, synchronized, etc) or one of the
342 Objective-C "cxx" keywords (class, private,
343 protected, public, try, catch, throw) without a
344 preceding '@' sign. Do nothing and fall through to
345 the code for normal tokens (in C++ we would still
346 consider the CXX ones keywords, but not in C). */
349 else
351 token->type = CPP_KEYWORD;
352 token->keyword = rid_code;
353 break;
357 decl = lookup_name (token->value);
358 if (decl)
360 if (TREE_CODE (decl) == TYPE_DECL)
362 token->id_kind = C_ID_TYPENAME;
363 break;
366 else if (c_dialect_objc ())
368 tree objc_interface_decl = objc_is_class_name (token->value);
369 /* Objective-C class names are in the same namespace as
370 variables and typedefs, and hence are shadowed by local
371 declarations. */
372 if (objc_interface_decl
373 && (!objc_force_identifier || global_bindings_p ()))
375 token->value = objc_interface_decl;
376 token->id_kind = C_ID_CLASSNAME;
377 break;
380 token->id_kind = C_ID_ID;
382 break;
383 case CPP_AT_NAME:
384 /* This only happens in Objective-C; it must be a keyword. */
385 token->type = CPP_KEYWORD;
386 switch (C_RID_CODE (token->value))
388 /* Replace 'class' with '@class', 'private' with '@private',
389 etc. This prevents confusion with the C++ keyword
390 'class', and makes the tokens consistent with other
391 Objective-C 'AT' keywords. For example '@class' is
392 reported as RID_AT_CLASS which is consistent with
393 '@synchronized', which is reported as
394 RID_AT_SYNCHRONIZED.
396 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
397 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
398 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
399 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
400 case RID_THROW: token->keyword = RID_AT_THROW; break;
401 case RID_TRY: token->keyword = RID_AT_TRY; break;
402 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
403 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
404 default: token->keyword = C_RID_CODE (token->value);
406 break;
407 case CPP_COLON:
408 case CPP_COMMA:
409 case CPP_CLOSE_PAREN:
410 case CPP_SEMICOLON:
411 /* These tokens may affect the interpretation of any identifiers
412 following, if doing Objective-C. */
413 if (c_dialect_objc ())
414 parser->objc_need_raw_identifier = false;
415 break;
416 case CPP_PRAGMA:
417 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
418 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
419 token->value = NULL;
420 break;
421 default:
422 break;
424 timevar_pop (TV_LEX);
427 /* Return a pointer to the next token from PARSER, reading it in if
428 necessary. */
430 c_token *
431 c_parser_peek_token (c_parser *parser)
433 if (parser->tokens_avail == 0)
435 c_lex_one_token (parser, &parser->tokens[0]);
436 parser->tokens_avail = 1;
438 return &parser->tokens[0];
441 /* Return a pointer to the next-but-one token from PARSER, reading it
442 in if necessary. The next token is already read in. */
444 c_token *
445 c_parser_peek_2nd_token (c_parser *parser)
447 if (parser->tokens_avail >= 2)
448 return &parser->tokens[1];
449 gcc_assert (parser->tokens_avail == 1);
450 gcc_assert (parser->tokens[0].type != CPP_EOF);
451 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
452 c_lex_one_token (parser, &parser->tokens[1]);
453 parser->tokens_avail = 2;
454 return &parser->tokens[1];
457 /* Return a pointer to the Nth token from PARSER, reading it
458 in if necessary. The N-1th token is already read in. */
460 c_token *
461 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
463 /* N is 1-based, not zero-based. */
464 gcc_assert (n > 0);
466 if (parser->tokens_avail >= n)
467 return &parser->tokens[n - 1];
468 gcc_assert (parser->tokens_avail == n - 1);
469 c_lex_one_token (parser, &parser->tokens[n - 1]);
470 parser->tokens_avail = n;
471 return &parser->tokens[n - 1];
474 bool
475 c_keyword_starts_typename (enum rid keyword)
477 switch (keyword)
479 case RID_UNSIGNED:
480 case RID_LONG:
481 case RID_SHORT:
482 case RID_SIGNED:
483 case RID_COMPLEX:
484 case RID_INT:
485 case RID_CHAR:
486 case RID_FLOAT:
487 case RID_DOUBLE:
488 case RID_VOID:
489 case RID_DFLOAT32:
490 case RID_DFLOAT64:
491 case RID_DFLOAT128:
492 CASE_RID_FLOATN_NX:
493 case RID_BOOL:
494 case RID_ENUM:
495 case RID_STRUCT:
496 case RID_UNION:
497 case RID_TYPEOF:
498 case RID_CONST:
499 case RID_ATOMIC:
500 case RID_VOLATILE:
501 case RID_RESTRICT:
502 case RID_ATTRIBUTE:
503 case RID_FRACT:
504 case RID_ACCUM:
505 case RID_SAT:
506 case RID_AUTO_TYPE:
507 case RID_ALIGNAS:
508 return true;
509 default:
510 if (keyword >= RID_FIRST_INT_N
511 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
512 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
513 return true;
514 return false;
518 /* Return true if TOKEN can start a type name,
519 false otherwise. */
520 bool
521 c_token_starts_typename (c_token *token)
523 switch (token->type)
525 case CPP_NAME:
526 switch (token->id_kind)
528 case C_ID_ID:
529 return false;
530 case C_ID_ADDRSPACE:
531 return true;
532 case C_ID_TYPENAME:
533 return true;
534 case C_ID_CLASSNAME:
535 gcc_assert (c_dialect_objc ());
536 return true;
537 default:
538 gcc_unreachable ();
540 case CPP_KEYWORD:
541 return c_keyword_starts_typename (token->keyword);
542 case CPP_LESS:
543 if (c_dialect_objc ())
544 return true;
545 return false;
546 default:
547 return false;
551 /* Return true if the next token from PARSER can start a type name,
552 false otherwise. LA specifies how to do lookahead in order to
553 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
555 static inline bool
556 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
558 c_token *token = c_parser_peek_token (parser);
559 if (c_token_starts_typename (token))
560 return true;
562 /* Try a bit harder to detect an unknown typename. */
563 if (la != cla_prefer_id
564 && token->type == CPP_NAME
565 && token->id_kind == C_ID_ID
567 /* Do not try too hard when we could have "object in array". */
568 && !parser->objc_could_be_foreach_context
570 && (la == cla_prefer_type
571 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
572 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
574 /* Only unknown identifiers. */
575 && !lookup_name (token->value))
576 return true;
578 return false;
581 /* Return true if TOKEN is a type qualifier, false otherwise. */
582 static bool
583 c_token_is_qualifier (c_token *token)
585 switch (token->type)
587 case CPP_NAME:
588 switch (token->id_kind)
590 case C_ID_ADDRSPACE:
591 return true;
592 default:
593 return false;
595 case CPP_KEYWORD:
596 switch (token->keyword)
598 case RID_CONST:
599 case RID_VOLATILE:
600 case RID_RESTRICT:
601 case RID_ATTRIBUTE:
602 case RID_ATOMIC:
603 return true;
604 default:
605 return false;
607 case CPP_LESS:
608 return false;
609 default:
610 gcc_unreachable ();
614 /* Return true if the next token from PARSER is a type qualifier,
615 false otherwise. */
616 static inline bool
617 c_parser_next_token_is_qualifier (c_parser *parser)
619 c_token *token = c_parser_peek_token (parser);
620 return c_token_is_qualifier (token);
623 /* Return true if TOKEN can start declaration specifiers, false
624 otherwise. */
625 static bool
626 c_token_starts_declspecs (c_token *token)
628 switch (token->type)
630 case CPP_NAME:
631 switch (token->id_kind)
633 case C_ID_ID:
634 return false;
635 case C_ID_ADDRSPACE:
636 return true;
637 case C_ID_TYPENAME:
638 return true;
639 case C_ID_CLASSNAME:
640 gcc_assert (c_dialect_objc ());
641 return true;
642 default:
643 gcc_unreachable ();
645 case CPP_KEYWORD:
646 switch (token->keyword)
648 case RID_STATIC:
649 case RID_EXTERN:
650 case RID_REGISTER:
651 case RID_TYPEDEF:
652 case RID_INLINE:
653 case RID_NORETURN:
654 case RID_AUTO:
655 case RID_THREAD:
656 case RID_UNSIGNED:
657 case RID_LONG:
658 case RID_SHORT:
659 case RID_SIGNED:
660 case RID_COMPLEX:
661 case RID_INT:
662 case RID_CHAR:
663 case RID_FLOAT:
664 case RID_DOUBLE:
665 case RID_VOID:
666 case RID_DFLOAT32:
667 case RID_DFLOAT64:
668 case RID_DFLOAT128:
669 CASE_RID_FLOATN_NX:
670 case RID_BOOL:
671 case RID_ENUM:
672 case RID_STRUCT:
673 case RID_UNION:
674 case RID_TYPEOF:
675 case RID_CONST:
676 case RID_VOLATILE:
677 case RID_RESTRICT:
678 case RID_ATTRIBUTE:
679 case RID_FRACT:
680 case RID_ACCUM:
681 case RID_SAT:
682 case RID_ALIGNAS:
683 case RID_ATOMIC:
684 case RID_AUTO_TYPE:
685 return true;
686 default:
687 if (token->keyword >= RID_FIRST_INT_N
688 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
689 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
690 return true;
691 return false;
693 case CPP_LESS:
694 if (c_dialect_objc ())
695 return true;
696 return false;
697 default:
698 return false;
703 /* Return true if TOKEN can start declaration specifiers or a static
704 assertion, false otherwise. */
705 static bool
706 c_token_starts_declaration (c_token *token)
708 if (c_token_starts_declspecs (token)
709 || token->keyword == RID_STATIC_ASSERT)
710 return true;
711 else
712 return false;
715 /* Return true if the next token from PARSER can start declaration
716 specifiers, false otherwise. */
717 bool
718 c_parser_next_token_starts_declspecs (c_parser *parser)
720 c_token *token = c_parser_peek_token (parser);
722 /* In Objective-C, a classname normally starts a declspecs unless it
723 is immediately followed by a dot. In that case, it is the
724 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
725 setter/getter on the class. c_token_starts_declspecs() can't
726 differentiate between the two cases because it only checks the
727 current token, so we have a special check here. */
728 if (c_dialect_objc ()
729 && token->type == CPP_NAME
730 && token->id_kind == C_ID_CLASSNAME
731 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
732 return false;
734 return c_token_starts_declspecs (token);
737 /* Return true if the next tokens from PARSER can start declaration
738 specifiers or a static assertion, false otherwise. */
739 bool
740 c_parser_next_tokens_start_declaration (c_parser *parser)
742 c_token *token = c_parser_peek_token (parser);
744 /* Same as above. */
745 if (c_dialect_objc ()
746 && token->type == CPP_NAME
747 && token->id_kind == C_ID_CLASSNAME
748 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
749 return false;
751 /* Labels do not start declarations. */
752 if (token->type == CPP_NAME
753 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
754 return false;
756 if (c_token_starts_declaration (token))
757 return true;
759 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
760 return true;
762 return false;
765 /* Consume the next token from PARSER. */
767 void
768 c_parser_consume_token (c_parser *parser)
770 gcc_assert (parser->tokens_avail >= 1);
771 gcc_assert (parser->tokens[0].type != CPP_EOF);
772 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
773 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
774 parser->last_token_location = parser->tokens[0].location;
775 if (parser->tokens != &parser->tokens_buf[0])
776 parser->tokens++;
777 else if (parser->tokens_avail == 2)
778 parser->tokens[0] = parser->tokens[1];
779 parser->tokens_avail--;
782 /* Expect the current token to be a #pragma. Consume it and remember
783 that we've begun parsing a pragma. */
785 static void
786 c_parser_consume_pragma (c_parser *parser)
788 gcc_assert (!parser->in_pragma);
789 gcc_assert (parser->tokens_avail >= 1);
790 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
791 if (parser->tokens != &parser->tokens_buf[0])
792 parser->tokens++;
793 else if (parser->tokens_avail == 2)
794 parser->tokens[0] = parser->tokens[1];
795 parser->tokens_avail--;
796 parser->in_pragma = true;
799 /* Update the global input_location from TOKEN. */
800 static inline void
801 c_parser_set_source_position_from_token (c_token *token)
803 if (token->type != CPP_EOF)
805 input_location = token->location;
809 /* Helper function for c_parser_error.
810 Having peeked a token of kind TOK1_KIND that might signify
811 a conflict marker, peek successor tokens to determine
812 if we actually do have a conflict marker.
813 Specifically, we consider a run of 7 '<', '=' or '>' characters
814 at the start of a line as a conflict marker.
815 These come through the lexer as three pairs and a single,
816 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
817 If it returns true, *OUT_LOC is written to with the location/range
818 of the marker. */
820 static bool
821 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
822 location_t *out_loc)
824 c_token *token2 = c_parser_peek_2nd_token (parser);
825 if (token2->type != tok1_kind)
826 return false;
827 c_token *token3 = c_parser_peek_nth_token (parser, 3);
828 if (token3->type != tok1_kind)
829 return false;
830 c_token *token4 = c_parser_peek_nth_token (parser, 4);
831 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
832 return false;
834 /* It must be at the start of the line. */
835 location_t start_loc = c_parser_peek_token (parser)->location;
836 if (LOCATION_COLUMN (start_loc) != 1)
837 return false;
839 /* We have a conflict marker. Construct a location of the form:
840 <<<<<<<
841 ^~~~~~~
842 with start == caret, finishing at the end of the marker. */
843 location_t finish_loc = get_finish (token4->location);
844 *out_loc = make_location (start_loc, start_loc, finish_loc);
846 return true;
849 /* Issue a diagnostic of the form
850 FILE:LINE: MESSAGE before TOKEN
851 where TOKEN is the next token in the input stream of PARSER.
852 MESSAGE (specified by the caller) is usually of the form "expected
853 OTHER-TOKEN".
855 Use RICHLOC as the location of the diagnostic.
857 Do not issue a diagnostic if still recovering from an error.
859 Return true iff an error was actually emitted.
861 ??? This is taken from the C++ parser, but building up messages in
862 this way is not i18n-friendly and some other approach should be
863 used. */
865 static bool
866 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
867 rich_location *richloc)
869 c_token *token = c_parser_peek_token (parser);
870 if (parser->error)
871 return false;
872 parser->error = true;
873 if (!gmsgid)
874 return false;
876 /* If this is actually a conflict marker, report it as such. */
877 if (token->type == CPP_LSHIFT
878 || token->type == CPP_RSHIFT
879 || token->type == CPP_EQ_EQ)
881 location_t loc;
882 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
884 error_at (loc, "version control conflict marker in file");
885 return true;
889 c_parse_error (gmsgid,
890 /* Because c_parse_error does not understand
891 CPP_KEYWORD, keywords are treated like
892 identifiers. */
893 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
894 /* ??? The C parser does not save the cpp flags of a
895 token, we need to pass 0 here and we will not get
896 the source spelling of some tokens but rather the
897 canonical spelling. */
898 token->value, /*flags=*/0, richloc);
899 return true;
902 /* As c_parser_error_richloc, but issue the message at the
903 location of PARSER's next token, or at input_location
904 if the next token is EOF. */
906 bool
907 c_parser_error (c_parser *parser, const char *gmsgid)
909 c_token *token = c_parser_peek_token (parser);
910 c_parser_set_source_position_from_token (token);
911 rich_location richloc (line_table, input_location);
912 return c_parser_error_richloc (parser, gmsgid, &richloc);
915 /* Some tokens naturally come in pairs e.g.'(' and ')'.
916 This class is for tracking such a matching pair of symbols.
917 In particular, it tracks the location of the first token,
918 so that if the second token is missing, we can highlight the
919 location of the first token when notifying the user about the
920 problem. */
922 template <typename traits_t>
923 class token_pair
925 public:
926 /* token_pair's ctor. */
927 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
929 /* If the next token is the opening symbol for this pair, consume it and
930 return true.
931 Otherwise, issue an error and return false.
932 In either case, record the location of the opening token. */
934 bool require_open (c_parser *parser)
936 c_token *token = c_parser_peek_token (parser);
937 if (token)
938 m_open_loc = token->location;
940 return c_parser_require (parser, traits_t::open_token_type,
941 traits_t::open_gmsgid);
944 /* Consume the next token from PARSER, recording its location as
945 that of the opening token within the pair. */
947 void consume_open (c_parser *parser)
949 c_token *token = c_parser_peek_token (parser);
950 gcc_assert (token->type == traits_t::open_token_type);
951 m_open_loc = token->location;
952 c_parser_consume_token (parser);
955 /* If the next token is the closing symbol for this pair, consume it
956 and return true.
957 Otherwise, issue an error, highlighting the location of the
958 corresponding opening token, and return false. */
960 bool require_close (c_parser *parser) const
962 return c_parser_require (parser, traits_t::close_token_type,
963 traits_t::close_gmsgid, m_open_loc);
966 /* Like token_pair::require_close, except that tokens will be skipped
967 until the desired token is found. An error message is still produced
968 if the next token is not as expected. */
970 void skip_until_found_close (c_parser *parser) const
972 c_parser_skip_until_found (parser, traits_t::close_token_type,
973 traits_t::close_gmsgid, m_open_loc);
976 private:
977 location_t m_open_loc;
980 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
982 struct matching_paren_traits
984 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
985 static const char * const open_gmsgid;
986 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
987 static const char * const close_gmsgid;
990 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
991 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
993 /* "matching_parens" is a token_pair<T> class for tracking matching
994 pairs of parentheses. */
996 typedef token_pair<matching_paren_traits> matching_parens;
998 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1000 struct matching_brace_traits
1002 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1003 static const char * const open_gmsgid;
1004 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1005 static const char * const close_gmsgid;
1008 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1009 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1011 /* "matching_braces" is a token_pair<T> class for tracking matching
1012 pairs of braces. */
1014 typedef token_pair<matching_brace_traits> matching_braces;
1016 /* Get a description of the matching symbol to TYPE e.g. "(" for
1017 CPP_CLOSE_PAREN. */
1019 static const char *
1020 get_matching_symbol (enum cpp_ttype type)
1022 switch (type)
1024 default:
1025 gcc_unreachable ();
1026 return "";
1027 case CPP_CLOSE_PAREN:
1028 return "(";
1029 case CPP_CLOSE_BRACE:
1030 return "{";
1034 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1035 issue the error MSGID. If MSGID is NULL then a message has already
1036 been produced and no message will be produced this time. Returns
1037 true if found, false otherwise.
1039 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1040 within any error as the location of an "opening" token matching
1041 the close token TYPE (e.g. the location of the '(' when TYPE is
1042 CPP_CLOSE_PAREN).
1044 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1045 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1046 attempt to generate a fix-it hint for the problem.
1047 Otherwise msgid describes multiple token types (e.g.
1048 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1049 generate a fix-it hint. */
1051 bool
1052 c_parser_require (c_parser *parser,
1053 enum cpp_ttype type,
1054 const char *msgid,
1055 location_t matching_location,
1056 bool type_is_unique)
1058 if (c_parser_next_token_is (parser, type))
1060 c_parser_consume_token (parser);
1061 return true;
1063 else
1065 location_t next_token_loc = c_parser_peek_token (parser)->location;
1066 gcc_rich_location richloc (next_token_loc);
1068 /* Potentially supply a fix-it hint, suggesting to add the
1069 missing token immediately after the *previous* token.
1070 This may move the primary location within richloc. */
1071 if (!parser->error && type_is_unique)
1072 maybe_suggest_missing_token_insertion (&richloc, type,
1073 parser->last_token_location);
1075 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1076 Attempt to consolidate diagnostics by printing it as a
1077 secondary range within the main diagnostic. */
1078 bool added_matching_location = false;
1079 if (matching_location != UNKNOWN_LOCATION)
1080 added_matching_location
1081 = richloc.add_location_if_nearby (matching_location);
1083 if (c_parser_error_richloc (parser, msgid, &richloc))
1084 /* If we weren't able to consolidate matching_location, then
1085 print it as a secondary diagnostic. */
1086 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1087 inform (matching_location, "to match this %qs",
1088 get_matching_symbol (type));
1090 return false;
1094 /* If the next token is the indicated keyword, consume it. Otherwise,
1095 issue the error MSGID. Returns true if found, false otherwise. */
1097 static bool
1098 c_parser_require_keyword (c_parser *parser,
1099 enum rid keyword,
1100 const char *msgid)
1102 if (c_parser_next_token_is_keyword (parser, keyword))
1104 c_parser_consume_token (parser);
1105 return true;
1107 else
1109 c_parser_error (parser, msgid);
1110 return false;
1114 /* Like c_parser_require, except that tokens will be skipped until the
1115 desired token is found. An error message is still produced if the
1116 next token is not as expected. If MSGID is NULL then a message has
1117 already been produced and no message will be produced this
1118 time.
1120 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1121 within any error as the location of an "opening" token matching
1122 the close token TYPE (e.g. the location of the '(' when TYPE is
1123 CPP_CLOSE_PAREN). */
1125 void
1126 c_parser_skip_until_found (c_parser *parser,
1127 enum cpp_ttype type,
1128 const char *msgid,
1129 location_t matching_location)
1131 unsigned nesting_depth = 0;
1133 if (c_parser_require (parser, type, msgid, matching_location))
1134 return;
1136 /* Skip tokens until the desired token is found. */
1137 while (true)
1139 /* Peek at the next token. */
1140 c_token *token = c_parser_peek_token (parser);
1141 /* If we've reached the token we want, consume it and stop. */
1142 if (token->type == type && !nesting_depth)
1144 c_parser_consume_token (parser);
1145 break;
1148 /* If we've run out of tokens, stop. */
1149 if (token->type == CPP_EOF)
1150 return;
1151 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1152 return;
1153 if (token->type == CPP_OPEN_BRACE
1154 || token->type == CPP_OPEN_PAREN
1155 || token->type == CPP_OPEN_SQUARE)
1156 ++nesting_depth;
1157 else if (token->type == CPP_CLOSE_BRACE
1158 || token->type == CPP_CLOSE_PAREN
1159 || token->type == CPP_CLOSE_SQUARE)
1161 if (nesting_depth-- == 0)
1162 break;
1164 /* Consume this token. */
1165 c_parser_consume_token (parser);
1167 parser->error = false;
1170 /* Skip tokens until the end of a parameter is found, but do not
1171 consume the comma, semicolon or closing delimiter. */
1173 static void
1174 c_parser_skip_to_end_of_parameter (c_parser *parser)
1176 unsigned nesting_depth = 0;
1178 while (true)
1180 c_token *token = c_parser_peek_token (parser);
1181 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1182 && !nesting_depth)
1183 break;
1184 /* If we've run out of tokens, stop. */
1185 if (token->type == CPP_EOF)
1186 return;
1187 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1188 return;
1189 if (token->type == CPP_OPEN_BRACE
1190 || token->type == CPP_OPEN_PAREN
1191 || token->type == CPP_OPEN_SQUARE)
1192 ++nesting_depth;
1193 else if (token->type == CPP_CLOSE_BRACE
1194 || token->type == CPP_CLOSE_PAREN
1195 || token->type == CPP_CLOSE_SQUARE)
1197 if (nesting_depth-- == 0)
1198 break;
1200 /* Consume this token. */
1201 c_parser_consume_token (parser);
1203 parser->error = false;
1206 /* Expect to be at the end of the pragma directive and consume an
1207 end of line marker. */
1209 static void
1210 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1212 gcc_assert (parser->in_pragma);
1213 parser->in_pragma = false;
1215 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1216 c_parser_error (parser, "expected end of line");
1218 cpp_ttype token_type;
1221 c_token *token = c_parser_peek_token (parser);
1222 token_type = token->type;
1223 if (token_type == CPP_EOF)
1224 break;
1225 c_parser_consume_token (parser);
1227 while (token_type != CPP_PRAGMA_EOL);
1229 parser->error = false;
1232 /* Skip tokens until we have consumed an entire block, or until we
1233 have consumed a non-nested ';'. */
1235 static void
1236 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1238 unsigned nesting_depth = 0;
1239 bool save_error = parser->error;
1241 while (true)
1243 c_token *token;
1245 /* Peek at the next token. */
1246 token = c_parser_peek_token (parser);
1248 switch (token->type)
1250 case CPP_EOF:
1251 return;
1253 case CPP_PRAGMA_EOL:
1254 if (parser->in_pragma)
1255 return;
1256 break;
1258 case CPP_SEMICOLON:
1259 /* If the next token is a ';', we have reached the
1260 end of the statement. */
1261 if (!nesting_depth)
1263 /* Consume the ';'. */
1264 c_parser_consume_token (parser);
1265 goto finished;
1267 break;
1269 case CPP_CLOSE_BRACE:
1270 /* If the next token is a non-nested '}', then we have
1271 reached the end of the current block. */
1272 if (nesting_depth == 0 || --nesting_depth == 0)
1274 c_parser_consume_token (parser);
1275 goto finished;
1277 break;
1279 case CPP_OPEN_BRACE:
1280 /* If it the next token is a '{', then we are entering a new
1281 block. Consume the entire block. */
1282 ++nesting_depth;
1283 break;
1285 case CPP_PRAGMA:
1286 /* If we see a pragma, consume the whole thing at once. We
1287 have some safeguards against consuming pragmas willy-nilly.
1288 Normally, we'd expect to be here with parser->error set,
1289 which disables these safeguards. But it's possible to get
1290 here for secondary error recovery, after parser->error has
1291 been cleared. */
1292 c_parser_consume_pragma (parser);
1293 c_parser_skip_to_pragma_eol (parser);
1294 parser->error = save_error;
1295 continue;
1297 default:
1298 break;
1301 c_parser_consume_token (parser);
1304 finished:
1305 parser->error = false;
1308 /* CPP's options (initialized by c-opts.c). */
1309 extern cpp_options *cpp_opts;
1311 /* Save the warning flags which are controlled by __extension__. */
1313 static inline int
1314 disable_extension_diagnostics (void)
1316 int ret = (pedantic
1317 | (warn_pointer_arith << 1)
1318 | (warn_traditional << 2)
1319 | (flag_iso << 3)
1320 | (warn_long_long << 4)
1321 | (warn_cxx_compat << 5)
1322 | (warn_overlength_strings << 6)
1323 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1324 play tricks to properly restore it. */
1325 | ((warn_c90_c99_compat == 1) << 7)
1326 | ((warn_c90_c99_compat == -1) << 8)
1327 /* Similarly for warn_c99_c11_compat. */
1328 | ((warn_c99_c11_compat == 1) << 9)
1329 | ((warn_c99_c11_compat == -1) << 10)
1331 cpp_opts->cpp_pedantic = pedantic = 0;
1332 warn_pointer_arith = 0;
1333 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1334 flag_iso = 0;
1335 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1336 warn_cxx_compat = 0;
1337 warn_overlength_strings = 0;
1338 warn_c90_c99_compat = 0;
1339 warn_c99_c11_compat = 0;
1340 return ret;
1343 /* Restore the warning flags which are controlled by __extension__.
1344 FLAGS is the return value from disable_extension_diagnostics. */
1346 static inline void
1347 restore_extension_diagnostics (int flags)
1349 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1350 warn_pointer_arith = (flags >> 1) & 1;
1351 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1352 flag_iso = (flags >> 3) & 1;
1353 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1354 warn_cxx_compat = (flags >> 5) & 1;
1355 warn_overlength_strings = (flags >> 6) & 1;
1356 /* See above for why is this needed. */
1357 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1358 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1361 /* Helper data structure for parsing #pragma acc routine. */
1362 struct oacc_routine_data {
1363 bool error_seen; /* Set if error has been reported. */
1364 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1365 tree clauses;
1366 location_t loc;
1369 static void c_parser_external_declaration (c_parser *);
1370 static void c_parser_asm_definition (c_parser *);
1371 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1372 bool, bool, tree *, vec<c_token>,
1373 struct oacc_routine_data * = NULL,
1374 bool * = NULL);
1375 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1376 static void c_parser_static_assert_declaration (c_parser *);
1377 static struct c_typespec c_parser_enum_specifier (c_parser *);
1378 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1379 static tree c_parser_struct_declaration (c_parser *);
1380 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1381 static tree c_parser_alignas_specifier (c_parser *);
1382 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1383 c_dtr_syn, bool *);
1384 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1385 bool,
1386 struct c_declarator *);
1387 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1388 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1389 tree);
1390 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1391 static tree c_parser_simple_asm_expr (c_parser *);
1392 static tree c_parser_attributes (c_parser *);
1393 static struct c_expr c_parser_initializer (c_parser *);
1394 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1395 struct obstack *);
1396 static void c_parser_initelt (c_parser *, struct obstack *);
1397 static void c_parser_initval (c_parser *, struct c_expr *,
1398 struct obstack *);
1399 static tree c_parser_compound_statement (c_parser *);
1400 static void c_parser_compound_statement_nostart (c_parser *);
1401 static void c_parser_label (c_parser *);
1402 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1403 static void c_parser_statement_after_labels (c_parser *, bool *,
1404 vec<tree> * = NULL);
1405 static tree c_parser_c99_block_statement (c_parser *, bool *,
1406 location_t * = NULL);
1407 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1408 static void c_parser_switch_statement (c_parser *, bool *);
1409 static void c_parser_while_statement (c_parser *, bool, unsigned short, bool *);
1410 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1411 static void c_parser_for_statement (c_parser *, bool, unsigned short, bool *);
1412 static tree c_parser_asm_statement (c_parser *);
1413 static tree c_parser_asm_operands (c_parser *);
1414 static tree c_parser_asm_goto_operands (c_parser *);
1415 static tree c_parser_asm_clobbers (c_parser *);
1416 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1417 tree = NULL_TREE);
1418 static struct c_expr c_parser_conditional_expression (c_parser *,
1419 struct c_expr *, tree);
1420 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1421 tree);
1422 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1423 static struct c_expr c_parser_unary_expression (c_parser *);
1424 static struct c_expr c_parser_sizeof_expression (c_parser *);
1425 static struct c_expr c_parser_alignof_expression (c_parser *);
1426 static struct c_expr c_parser_postfix_expression (c_parser *);
1427 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1428 struct c_type_name *,
1429 location_t);
1430 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1431 location_t loc,
1432 struct c_expr);
1433 static tree c_parser_transaction (c_parser *, enum rid);
1434 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1435 static tree c_parser_transaction_cancel (c_parser *);
1436 static struct c_expr c_parser_expression (c_parser *);
1437 static struct c_expr c_parser_expression_conv (c_parser *);
1438 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1439 vec<tree, va_gc> **, location_t *,
1440 tree *, vec<location_t> *,
1441 unsigned int * = NULL);
1442 static void c_parser_oacc_declare (c_parser *);
1443 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1444 static void c_parser_oacc_update (c_parser *);
1445 static void c_parser_omp_construct (c_parser *, bool *);
1446 static void c_parser_omp_threadprivate (c_parser *);
1447 static void c_parser_omp_barrier (c_parser *);
1448 static void c_parser_omp_flush (c_parser *);
1449 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1450 tree, tree *, bool *);
1451 static void c_parser_omp_taskwait (c_parser *);
1452 static void c_parser_omp_taskyield (c_parser *);
1453 static void c_parser_omp_cancel (c_parser *);
1455 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1456 pragma_stmt, pragma_compound };
1457 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1458 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1459 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1460 static void c_parser_omp_end_declare_target (c_parser *);
1461 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1462 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1463 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1465 /* These Objective-C parser functions are only ever called when
1466 compiling Objective-C. */
1467 static void c_parser_objc_class_definition (c_parser *, tree);
1468 static void c_parser_objc_class_instance_variables (c_parser *);
1469 static void c_parser_objc_class_declaration (c_parser *);
1470 static void c_parser_objc_alias_declaration (c_parser *);
1471 static void c_parser_objc_protocol_definition (c_parser *, tree);
1472 static bool c_parser_objc_method_type (c_parser *);
1473 static void c_parser_objc_method_definition (c_parser *);
1474 static void c_parser_objc_methodprotolist (c_parser *);
1475 static void c_parser_objc_methodproto (c_parser *);
1476 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1477 static tree c_parser_objc_type_name (c_parser *);
1478 static tree c_parser_objc_protocol_refs (c_parser *);
1479 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1480 static void c_parser_objc_synchronized_statement (c_parser *);
1481 static tree c_parser_objc_selector (c_parser *);
1482 static tree c_parser_objc_selector_arg (c_parser *);
1483 static tree c_parser_objc_receiver (c_parser *);
1484 static tree c_parser_objc_message_args (c_parser *);
1485 static tree c_parser_objc_keywordexpr (c_parser *);
1486 static void c_parser_objc_at_property_declaration (c_parser *);
1487 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1488 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1489 static bool c_parser_objc_diagnose_bad_element_prefix
1490 (c_parser *, struct c_declspecs *);
1492 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1494 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1496 translation-unit:
1497 external-declarations
1499 external-declarations:
1500 external-declaration
1501 external-declarations external-declaration
1503 GNU extensions:
1505 translation-unit:
1506 empty
1509 static void
1510 c_parser_translation_unit (c_parser *parser)
1512 if (c_parser_next_token_is (parser, CPP_EOF))
1514 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1515 "ISO C forbids an empty translation unit");
1517 else
1519 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1520 mark_valid_location_for_stdc_pragma (false);
1523 ggc_collect ();
1524 c_parser_external_declaration (parser);
1525 obstack_free (&parser_obstack, obstack_position);
1527 while (c_parser_next_token_is_not (parser, CPP_EOF));
1530 unsigned int i;
1531 tree decl;
1532 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1533 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1534 error ("storage size of %q+D isn%'t known", decl);
1537 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1539 external-declaration:
1540 function-definition
1541 declaration
1543 GNU extensions:
1545 external-declaration:
1546 asm-definition
1548 __extension__ external-declaration
1550 Objective-C:
1552 external-declaration:
1553 objc-class-definition
1554 objc-class-declaration
1555 objc-alias-declaration
1556 objc-protocol-definition
1557 objc-method-definition
1558 @end
1561 static void
1562 c_parser_external_declaration (c_parser *parser)
1564 int ext;
1565 switch (c_parser_peek_token (parser)->type)
1567 case CPP_KEYWORD:
1568 switch (c_parser_peek_token (parser)->keyword)
1570 case RID_EXTENSION:
1571 ext = disable_extension_diagnostics ();
1572 c_parser_consume_token (parser);
1573 c_parser_external_declaration (parser);
1574 restore_extension_diagnostics (ext);
1575 break;
1576 case RID_ASM:
1577 c_parser_asm_definition (parser);
1578 break;
1579 case RID_AT_INTERFACE:
1580 case RID_AT_IMPLEMENTATION:
1581 gcc_assert (c_dialect_objc ());
1582 c_parser_objc_class_definition (parser, NULL_TREE);
1583 break;
1584 case RID_AT_CLASS:
1585 gcc_assert (c_dialect_objc ());
1586 c_parser_objc_class_declaration (parser);
1587 break;
1588 case RID_AT_ALIAS:
1589 gcc_assert (c_dialect_objc ());
1590 c_parser_objc_alias_declaration (parser);
1591 break;
1592 case RID_AT_PROTOCOL:
1593 gcc_assert (c_dialect_objc ());
1594 c_parser_objc_protocol_definition (parser, NULL_TREE);
1595 break;
1596 case RID_AT_PROPERTY:
1597 gcc_assert (c_dialect_objc ());
1598 c_parser_objc_at_property_declaration (parser);
1599 break;
1600 case RID_AT_SYNTHESIZE:
1601 gcc_assert (c_dialect_objc ());
1602 c_parser_objc_at_synthesize_declaration (parser);
1603 break;
1604 case RID_AT_DYNAMIC:
1605 gcc_assert (c_dialect_objc ());
1606 c_parser_objc_at_dynamic_declaration (parser);
1607 break;
1608 case RID_AT_END:
1609 gcc_assert (c_dialect_objc ());
1610 c_parser_consume_token (parser);
1611 objc_finish_implementation ();
1612 break;
1613 default:
1614 goto decl_or_fndef;
1616 break;
1617 case CPP_SEMICOLON:
1618 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1619 "ISO C does not allow extra %<;%> outside of a function");
1620 c_parser_consume_token (parser);
1621 break;
1622 case CPP_PRAGMA:
1623 mark_valid_location_for_stdc_pragma (true);
1624 c_parser_pragma (parser, pragma_external, NULL);
1625 mark_valid_location_for_stdc_pragma (false);
1626 break;
1627 case CPP_PLUS:
1628 case CPP_MINUS:
1629 if (c_dialect_objc ())
1631 c_parser_objc_method_definition (parser);
1632 break;
1634 /* Else fall through, and yield a syntax error trying to parse
1635 as a declaration or function definition. */
1636 /* FALLTHRU */
1637 default:
1638 decl_or_fndef:
1639 /* A declaration or a function definition (or, in Objective-C,
1640 an @interface or @protocol with prefix attributes). We can
1641 only tell which after parsing the declaration specifiers, if
1642 any, and the first declarator. */
1643 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1644 NULL, vNULL);
1645 break;
1649 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1650 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1652 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
1654 static void
1655 add_debug_begin_stmt (location_t loc)
1657 /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721. */
1658 if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1659 return;
1661 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1662 SET_EXPR_LOCATION (stmt, loc);
1663 add_stmt (stmt);
1666 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1667 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1668 is accepted; otherwise (old-style parameter declarations) only other
1669 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1670 assertion is accepted; otherwise (old-style parameter declarations)
1671 it is not. If NESTED is true, we are inside a function or parsing
1672 old-style parameter declarations; any functions encountered are
1673 nested functions and declaration specifiers are required; otherwise
1674 we are at top level and functions are normal functions and
1675 declaration specifiers may be optional. If EMPTY_OK is true, empty
1676 declarations are OK (subject to all other constraints); otherwise
1677 (old-style parameter declarations) they are diagnosed. If
1678 START_ATTR_OK is true, the declaration specifiers may start with
1679 attributes; otherwise they may not.
1680 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1681 declaration when parsing an Objective-C foreach statement.
1682 FALLTHRU_ATTR_P is used to signal whether this function parsed
1683 "__attribute__((fallthrough));".
1685 declaration:
1686 declaration-specifiers init-declarator-list[opt] ;
1687 static_assert-declaration
1689 function-definition:
1690 declaration-specifiers[opt] declarator declaration-list[opt]
1691 compound-statement
1693 declaration-list:
1694 declaration
1695 declaration-list declaration
1697 init-declarator-list:
1698 init-declarator
1699 init-declarator-list , init-declarator
1701 init-declarator:
1702 declarator simple-asm-expr[opt] attributes[opt]
1703 declarator simple-asm-expr[opt] attributes[opt] = initializer
1705 GNU extensions:
1707 nested-function-definition:
1708 declaration-specifiers declarator declaration-list[opt]
1709 compound-statement
1711 attribute ;
1713 Objective-C:
1714 attributes objc-class-definition
1715 attributes objc-category-definition
1716 attributes objc-protocol-definition
1718 The simple-asm-expr and attributes are GNU extensions.
1720 This function does not handle __extension__; that is handled in its
1721 callers. ??? Following the old parser, __extension__ may start
1722 external declarations, declarations in functions and declarations
1723 at the start of "for" loops, but not old-style parameter
1724 declarations.
1726 C99 requires declaration specifiers in a function definition; the
1727 absence is diagnosed through the diagnosis of implicit int. In GNU
1728 C we also allow but diagnose declarations without declaration
1729 specifiers, but only at top level (elsewhere they conflict with
1730 other syntax).
1732 In Objective-C, declarations of the looping variable in a foreach
1733 statement are exceptionally terminated by 'in' (for example, 'for
1734 (NSObject *object in array) { ... }').
1736 OpenMP:
1738 declaration:
1739 threadprivate-directive
1741 GIMPLE:
1743 gimple-function-definition:
1744 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1745 declaration-list[opt] compound-statement
1747 rtl-function-definition:
1748 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1749 declaration-list[opt] compound-statement */
1751 static void
1752 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1753 bool static_assert_ok, bool empty_ok,
1754 bool nested, bool start_attr_ok,
1755 tree *objc_foreach_object_declaration,
1756 vec<c_token> omp_declare_simd_clauses,
1757 struct oacc_routine_data *oacc_routine_data,
1758 bool *fallthru_attr_p)
1760 struct c_declspecs *specs;
1761 tree prefix_attrs;
1762 tree all_prefix_attrs;
1763 bool diagnosed_no_specs = false;
1764 location_t here = c_parser_peek_token (parser)->location;
1766 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1768 if (static_assert_ok
1769 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1771 c_parser_static_assert_declaration (parser);
1772 return;
1774 specs = build_null_declspecs ();
1776 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1777 if (c_parser_peek_token (parser)->type == CPP_NAME
1778 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1779 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1780 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1781 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1783 tree name = c_parser_peek_token (parser)->value;
1785 /* Issue a warning about NAME being an unknown type name, perhaps
1786 with some kind of hint.
1787 If the user forgot a "struct" etc, suggest inserting
1788 it. Otherwise, attempt to look for misspellings. */
1789 gcc_rich_location richloc (here);
1790 if (tag_exists_p (RECORD_TYPE, name))
1792 /* This is not C++ with its implicit typedef. */
1793 richloc.add_fixit_insert_before ("struct ");
1794 error_at (&richloc,
1795 "unknown type name %qE;"
1796 " use %<struct%> keyword to refer to the type",
1797 name);
1799 else if (tag_exists_p (UNION_TYPE, name))
1801 richloc.add_fixit_insert_before ("union ");
1802 error_at (&richloc,
1803 "unknown type name %qE;"
1804 " use %<union%> keyword to refer to the type",
1805 name);
1807 else if (tag_exists_p (ENUMERAL_TYPE, name))
1809 richloc.add_fixit_insert_before ("enum ");
1810 error_at (&richloc,
1811 "unknown type name %qE;"
1812 " use %<enum%> keyword to refer to the type",
1813 name);
1815 else
1817 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1818 here);
1819 if (hint)
1821 richloc.add_fixit_replace (hint.suggestion ());
1822 error_at (&richloc,
1823 "unknown type name %qE; did you mean %qs?",
1824 name, hint.suggestion ());
1826 else
1827 error_at (here, "unknown type name %qE", name);
1830 /* Parse declspecs normally to get a correct pointer type, but avoid
1831 a further "fails to be a type name" error. Refuse nested functions
1832 since it is not how the user likely wants us to recover. */
1833 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1834 c_parser_peek_token (parser)->keyword = RID_VOID;
1835 c_parser_peek_token (parser)->value = error_mark_node;
1836 fndef_ok = !nested;
1839 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1840 true, true, cla_nonabstract_decl);
1841 if (parser->error)
1843 c_parser_skip_to_end_of_block_or_statement (parser);
1844 return;
1846 if (nested && !specs->declspecs_seen_p)
1848 c_parser_error (parser, "expected declaration specifiers");
1849 c_parser_skip_to_end_of_block_or_statement (parser);
1850 return;
1853 finish_declspecs (specs);
1854 bool auto_type_p = specs->typespec_word == cts_auto_type;
1855 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1857 if (auto_type_p)
1858 error_at (here, "%<__auto_type%> in empty declaration");
1859 else if (specs->typespec_kind == ctsk_none
1860 && attribute_fallthrough_p (specs->attrs))
1862 if (fallthru_attr_p != NULL)
1863 *fallthru_attr_p = true;
1864 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1865 void_type_node, 0);
1866 add_stmt (fn);
1868 else if (empty_ok)
1869 shadow_tag (specs);
1870 else
1872 shadow_tag_warned (specs, 1);
1873 pedwarn (here, 0, "empty declaration");
1875 c_parser_consume_token (parser);
1876 if (oacc_routine_data)
1877 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1878 return;
1881 /* Provide better error recovery. Note that a type name here is usually
1882 better diagnosed as a redeclaration. */
1883 if (empty_ok
1884 && specs->typespec_kind == ctsk_tagdef
1885 && c_parser_next_token_starts_declspecs (parser)
1886 && !c_parser_next_token_is (parser, CPP_NAME))
1888 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1889 parser->error = false;
1890 shadow_tag_warned (specs, 1);
1891 return;
1893 else if (c_dialect_objc () && !auto_type_p)
1895 /* Prefix attributes are an error on method decls. */
1896 switch (c_parser_peek_token (parser)->type)
1898 case CPP_PLUS:
1899 case CPP_MINUS:
1900 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1901 return;
1902 if (specs->attrs)
1904 warning_at (c_parser_peek_token (parser)->location,
1905 OPT_Wattributes,
1906 "prefix attributes are ignored for methods");
1907 specs->attrs = NULL_TREE;
1909 if (fndef_ok)
1910 c_parser_objc_method_definition (parser);
1911 else
1912 c_parser_objc_methodproto (parser);
1913 return;
1914 break;
1915 default:
1916 break;
1918 /* This is where we parse 'attributes @interface ...',
1919 'attributes @implementation ...', 'attributes @protocol ...'
1920 (where attributes could be, for example, __attribute__
1921 ((deprecated)).
1923 switch (c_parser_peek_token (parser)->keyword)
1925 case RID_AT_INTERFACE:
1927 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1928 return;
1929 c_parser_objc_class_definition (parser, specs->attrs);
1930 return;
1932 break;
1933 case RID_AT_IMPLEMENTATION:
1935 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1936 return;
1937 if (specs->attrs)
1939 warning_at (c_parser_peek_token (parser)->location,
1940 OPT_Wattributes,
1941 "prefix attributes are ignored for implementations");
1942 specs->attrs = NULL_TREE;
1944 c_parser_objc_class_definition (parser, NULL_TREE);
1945 return;
1947 break;
1948 case RID_AT_PROTOCOL:
1950 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1951 return;
1952 c_parser_objc_protocol_definition (parser, specs->attrs);
1953 return;
1955 break;
1956 case RID_AT_ALIAS:
1957 case RID_AT_CLASS:
1958 case RID_AT_END:
1959 case RID_AT_PROPERTY:
1960 if (specs->attrs)
1962 c_parser_error (parser, "unexpected attribute");
1963 specs->attrs = NULL;
1965 break;
1966 default:
1967 break;
1970 else if (attribute_fallthrough_p (specs->attrs))
1971 warning_at (here, OPT_Wattributes,
1972 "%<fallthrough%> attribute not followed by %<;%>");
1974 pending_xref_error ();
1975 prefix_attrs = specs->attrs;
1976 all_prefix_attrs = prefix_attrs;
1977 specs->attrs = NULL_TREE;
1978 while (true)
1980 struct c_declarator *declarator;
1981 bool dummy = false;
1982 timevar_id_t tv;
1983 tree fnbody = NULL_TREE;
1984 /* Declaring either one or more declarators (in which case we
1985 should diagnose if there were no declaration specifiers) or a
1986 function definition (in which case the diagnostic for
1987 implicit int suffices). */
1988 declarator = c_parser_declarator (parser,
1989 specs->typespec_kind != ctsk_none,
1990 C_DTR_NORMAL, &dummy);
1991 if (declarator == NULL)
1993 if (omp_declare_simd_clauses.exists ())
1994 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1995 omp_declare_simd_clauses);
1996 if (oacc_routine_data)
1997 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1998 c_parser_skip_to_end_of_block_or_statement (parser);
1999 return;
2001 if (auto_type_p && declarator->kind != cdk_id)
2003 error_at (here,
2004 "%<__auto_type%> requires a plain identifier"
2005 " as declarator");
2006 c_parser_skip_to_end_of_block_or_statement (parser);
2007 return;
2009 if (c_parser_next_token_is (parser, CPP_EQ)
2010 || c_parser_next_token_is (parser, CPP_COMMA)
2011 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2012 || c_parser_next_token_is_keyword (parser, RID_ASM)
2013 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2014 || c_parser_next_token_is_keyword (parser, RID_IN))
2016 tree asm_name = NULL_TREE;
2017 tree postfix_attrs = NULL_TREE;
2018 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2020 diagnosed_no_specs = true;
2021 pedwarn (here, 0, "data definition has no type or storage class");
2023 /* Having seen a data definition, there cannot now be a
2024 function definition. */
2025 fndef_ok = false;
2026 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2027 asm_name = c_parser_simple_asm_expr (parser);
2028 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2030 postfix_attrs = c_parser_attributes (parser);
2031 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2033 /* This means there is an attribute specifier after
2034 the declarator in a function definition. Provide
2035 some more information for the user. */
2036 error_at (here, "attributes should be specified before the "
2037 "declarator in a function definition");
2038 c_parser_skip_to_end_of_block_or_statement (parser);
2039 return;
2042 if (c_parser_next_token_is (parser, CPP_EQ))
2044 tree d;
2045 struct c_expr init;
2046 location_t init_loc;
2047 c_parser_consume_token (parser);
2048 if (auto_type_p)
2050 init_loc = c_parser_peek_token (parser)->location;
2051 rich_location richloc (line_table, init_loc);
2052 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2053 /* A parameter is initialized, which is invalid. Don't
2054 attempt to instrument the initializer. */
2055 int flag_sanitize_save = flag_sanitize;
2056 if (nested && !empty_ok)
2057 flag_sanitize = 0;
2058 init = c_parser_expr_no_commas (parser, NULL);
2059 flag_sanitize = flag_sanitize_save;
2060 if (TREE_CODE (init.value) == COMPONENT_REF
2061 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2062 error_at (here,
2063 "%<__auto_type%> used with a bit-field"
2064 " initializer");
2065 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2066 tree init_type = TREE_TYPE (init.value);
2067 /* As with typeof, remove all qualifiers from atomic types. */
2068 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2069 init_type
2070 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2071 bool vm_type = variably_modified_type_p (init_type,
2072 NULL_TREE);
2073 if (vm_type)
2074 init.value = save_expr (init.value);
2075 finish_init ();
2076 specs->typespec_kind = ctsk_typeof;
2077 specs->locations[cdw_typedef] = init_loc;
2078 specs->typedef_p = true;
2079 specs->type = init_type;
2080 if (vm_type)
2082 bool maybe_const = true;
2083 tree type_expr = c_fully_fold (init.value, false,
2084 &maybe_const);
2085 specs->expr_const_operands &= maybe_const;
2086 if (specs->expr)
2087 specs->expr = build2 (COMPOUND_EXPR,
2088 TREE_TYPE (type_expr),
2089 specs->expr, type_expr);
2090 else
2091 specs->expr = type_expr;
2093 d = start_decl (declarator, specs, true,
2094 chainon (postfix_attrs, all_prefix_attrs));
2095 if (!d)
2096 d = error_mark_node;
2097 if (omp_declare_simd_clauses.exists ())
2098 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2099 omp_declare_simd_clauses);
2101 else
2103 /* The declaration of the variable is in effect while
2104 its initializer is parsed. */
2105 d = start_decl (declarator, specs, true,
2106 chainon (postfix_attrs, all_prefix_attrs));
2107 if (!d)
2108 d = error_mark_node;
2109 if (omp_declare_simd_clauses.exists ())
2110 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2111 omp_declare_simd_clauses);
2112 init_loc = c_parser_peek_token (parser)->location;
2113 rich_location richloc (line_table, init_loc);
2114 start_init (d, asm_name, global_bindings_p (), &richloc);
2115 /* A parameter is initialized, which is invalid. Don't
2116 attempt to instrument the initializer. */
2117 int flag_sanitize_save = flag_sanitize;
2118 if (TREE_CODE (d) == PARM_DECL)
2119 flag_sanitize = 0;
2120 init = c_parser_initializer (parser);
2121 flag_sanitize = flag_sanitize_save;
2122 finish_init ();
2124 if (oacc_routine_data)
2125 c_finish_oacc_routine (oacc_routine_data, d, false);
2126 if (d != error_mark_node)
2128 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2129 finish_decl (d, init_loc, init.value,
2130 init.original_type, asm_name);
2133 else
2135 if (auto_type_p)
2137 error_at (here,
2138 "%<__auto_type%> requires an initialized "
2139 "data declaration");
2140 c_parser_skip_to_end_of_block_or_statement (parser);
2141 return;
2143 tree d = start_decl (declarator, specs, false,
2144 chainon (postfix_attrs,
2145 all_prefix_attrs));
2146 if (d && TREE_CODE (d) == FUNCTION_DECL)
2147 if (declarator->kind == cdk_function)
2148 if (DECL_ARGUMENTS (d) == NULL_TREE)
2149 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2150 if (omp_declare_simd_clauses.exists ())
2152 tree parms = NULL_TREE;
2153 if (d && TREE_CODE (d) == FUNCTION_DECL)
2155 struct c_declarator *ce = declarator;
2156 while (ce != NULL)
2157 if (ce->kind == cdk_function)
2159 parms = ce->u.arg_info->parms;
2160 break;
2162 else
2163 ce = ce->declarator;
2165 if (parms)
2166 temp_store_parm_decls (d, parms);
2167 c_finish_omp_declare_simd (parser, d, parms,
2168 omp_declare_simd_clauses);
2169 if (parms)
2170 temp_pop_parm_decls ();
2172 if (oacc_routine_data)
2173 c_finish_oacc_routine (oacc_routine_data, d, false);
2174 if (d)
2175 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2176 NULL_TREE, asm_name);
2178 if (c_parser_next_token_is_keyword (parser, RID_IN))
2180 if (d)
2181 *objc_foreach_object_declaration = d;
2182 else
2183 *objc_foreach_object_declaration = error_mark_node;
2186 if (c_parser_next_token_is (parser, CPP_COMMA))
2188 if (auto_type_p)
2190 error_at (here,
2191 "%<__auto_type%> may only be used with"
2192 " a single declarator");
2193 c_parser_skip_to_end_of_block_or_statement (parser);
2194 return;
2196 c_parser_consume_token (parser);
2197 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2198 all_prefix_attrs = chainon (c_parser_attributes (parser),
2199 prefix_attrs);
2200 else
2201 all_prefix_attrs = prefix_attrs;
2202 continue;
2204 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2206 c_parser_consume_token (parser);
2207 return;
2209 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2211 /* This can only happen in Objective-C: we found the
2212 'in' that terminates the declaration inside an
2213 Objective-C foreach statement. Do not consume the
2214 token, so that the caller can use it to determine
2215 that this indeed is a foreach context. */
2216 return;
2218 else
2220 c_parser_error (parser, "expected %<,%> or %<;%>");
2221 c_parser_skip_to_end_of_block_or_statement (parser);
2222 return;
2225 else if (auto_type_p)
2227 error_at (here,
2228 "%<__auto_type%> requires an initialized data declaration");
2229 c_parser_skip_to_end_of_block_or_statement (parser);
2230 return;
2232 else if (!fndef_ok)
2234 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2235 "%<asm%> or %<__attribute__%>");
2236 c_parser_skip_to_end_of_block_or_statement (parser);
2237 return;
2239 /* Function definition (nested or otherwise). */
2240 if (nested)
2242 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2243 c_push_function_context ();
2245 if (!start_function (specs, declarator, all_prefix_attrs))
2247 /* At this point we've consumed:
2248 declaration-specifiers declarator
2249 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2250 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2251 but the
2252 declaration-specifiers declarator
2253 aren't grokkable as a function definition, so we have
2254 an error. */
2255 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2256 if (c_parser_next_token_starts_declspecs (parser))
2258 /* If we have
2259 declaration-specifiers declarator decl-specs
2260 then assume we have a missing semicolon, which would
2261 give us:
2262 declaration-specifiers declarator decl-specs
2265 <~~~~~~~~~ declaration ~~~~~~~~~~>
2266 Use c_parser_require to get an error with a fix-it hint. */
2267 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2268 parser->error = false;
2270 else
2272 /* This can appear in many cases looking nothing like a
2273 function definition, so we don't give a more specific
2274 error suggesting there was one. */
2275 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2276 "or %<__attribute__%>");
2278 if (nested)
2279 c_pop_function_context ();
2280 break;
2283 if (DECL_DECLARED_INLINE_P (current_function_decl))
2284 tv = TV_PARSE_INLINE;
2285 else
2286 tv = TV_PARSE_FUNC;
2287 auto_timevar at (g_timer, tv);
2289 /* Parse old-style parameter declarations. ??? Attributes are
2290 not allowed to start declaration specifiers here because of a
2291 syntax conflict between a function declaration with attribute
2292 suffix and a function definition with an attribute prefix on
2293 first old-style parameter declaration. Following the old
2294 parser, they are not accepted on subsequent old-style
2295 parameter declarations either. However, there is no
2296 ambiguity after the first declaration, nor indeed on the
2297 first as long as we don't allow postfix attributes after a
2298 declarator with a nonempty identifier list in a definition;
2299 and postfix attributes have never been accepted here in
2300 function definitions either. */
2301 while (c_parser_next_token_is_not (parser, CPP_EOF)
2302 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2303 c_parser_declaration_or_fndef (parser, false, false, false,
2304 true, false, NULL, vNULL);
2305 store_parm_decls ();
2306 if (omp_declare_simd_clauses.exists ())
2307 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2308 omp_declare_simd_clauses);
2309 if (oacc_routine_data)
2310 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2311 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2312 = c_parser_peek_token (parser)->location;
2314 /* If the definition was marked with __GIMPLE then parse the
2315 function body as GIMPLE. */
2316 if (specs->gimple_p)
2318 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2319 bool saved = in_late_binary_op;
2320 in_late_binary_op = true;
2321 c_parser_parse_gimple_body (parser);
2322 in_late_binary_op = saved;
2324 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2325 consuming the function body. */
2326 else if (specs->rtl_p)
2328 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2330 /* Normally, store_parm_decls sets next_is_function_body,
2331 anticipating a function body. We need a push_scope/pop_scope
2332 pair to flush out this state, or subsequent function parsing
2333 will go wrong. */
2334 push_scope ();
2335 pop_scope ();
2337 finish_function ();
2338 return;
2340 else
2341 fnbody = c_parser_compound_statement (parser);
2342 tree fndecl = current_function_decl;
2343 if (nested)
2345 tree decl = current_function_decl;
2346 /* Mark nested functions as needing static-chain initially.
2347 lower_nested_functions will recompute it but the
2348 DECL_STATIC_CHAIN flag is also used before that happens,
2349 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2350 DECL_STATIC_CHAIN (decl) = 1;
2351 add_stmt (fnbody);
2352 finish_function ();
2353 c_pop_function_context ();
2354 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2356 else
2358 if (fnbody)
2359 add_stmt (fnbody);
2360 finish_function ();
2362 /* Get rid of the empty stmt list for GIMPLE. */
2363 if (specs->gimple_p)
2364 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2366 break;
2370 /* Parse an asm-definition (asm() outside a function body). This is a
2371 GNU extension.
2373 asm-definition:
2374 simple-asm-expr ;
2377 static void
2378 c_parser_asm_definition (c_parser *parser)
2380 tree asm_str = c_parser_simple_asm_expr (parser);
2381 if (asm_str)
2382 symtab->finalize_toplevel_asm (asm_str);
2383 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2386 /* Parse a static assertion (C11 6.7.10).
2388 static_assert-declaration:
2389 static_assert-declaration-no-semi ;
2392 static void
2393 c_parser_static_assert_declaration (c_parser *parser)
2395 c_parser_static_assert_declaration_no_semi (parser);
2396 if (parser->error
2397 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2398 c_parser_skip_to_end_of_block_or_statement (parser);
2401 /* Parse a static assertion (C11 6.7.10), without the trailing
2402 semicolon.
2404 static_assert-declaration-no-semi:
2405 _Static_assert ( constant-expression , string-literal )
2408 static void
2409 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2411 location_t assert_loc, value_loc;
2412 tree value;
2413 tree string;
2415 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2416 assert_loc = c_parser_peek_token (parser)->location;
2417 if (flag_isoc99)
2418 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2419 "ISO C99 does not support %<_Static_assert%>");
2420 else
2421 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2422 "ISO C90 does not support %<_Static_assert%>");
2423 c_parser_consume_token (parser);
2424 matching_parens parens;
2425 if (!parens.require_open (parser))
2426 return;
2427 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2428 value = c_parser_expr_no_commas (parser, NULL).value;
2429 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2430 parser->lex_untranslated_string = true;
2431 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2433 parser->lex_untranslated_string = false;
2434 return;
2436 switch (c_parser_peek_token (parser)->type)
2438 case CPP_STRING:
2439 case CPP_STRING16:
2440 case CPP_STRING32:
2441 case CPP_WSTRING:
2442 case CPP_UTF8STRING:
2443 string = c_parser_peek_token (parser)->value;
2444 c_parser_consume_token (parser);
2445 parser->lex_untranslated_string = false;
2446 break;
2447 default:
2448 c_parser_error (parser, "expected string literal");
2449 parser->lex_untranslated_string = false;
2450 return;
2452 parens.require_close (parser);
2454 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2456 error_at (value_loc, "expression in static assertion is not an integer");
2457 return;
2459 if (TREE_CODE (value) != INTEGER_CST)
2461 value = c_fully_fold (value, false, NULL);
2462 /* Strip no-op conversions. */
2463 STRIP_TYPE_NOPS (value);
2464 if (TREE_CODE (value) == INTEGER_CST)
2465 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2466 "is not an integer constant expression");
2468 if (TREE_CODE (value) != INTEGER_CST)
2470 error_at (value_loc, "expression in static assertion is not constant");
2471 return;
2473 constant_expression_warning (value);
2474 if (integer_zerop (value))
2475 error_at (assert_loc, "static assertion failed: %E", string);
2478 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2479 6.7, C11 6.7), adding them to SPECS (which may already include some).
2480 Storage class specifiers are accepted iff SCSPEC_OK; type
2481 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2482 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2483 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2485 declaration-specifiers:
2486 storage-class-specifier declaration-specifiers[opt]
2487 type-specifier declaration-specifiers[opt]
2488 type-qualifier declaration-specifiers[opt]
2489 function-specifier declaration-specifiers[opt]
2490 alignment-specifier declaration-specifiers[opt]
2492 Function specifiers (inline) are from C99, and are currently
2493 handled as storage class specifiers, as is __thread. Alignment
2494 specifiers are from C11.
2496 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2497 storage-class-specifier:
2498 typedef
2499 extern
2500 static
2501 auto
2502 register
2503 _Thread_local
2505 (_Thread_local is new in C11.)
2507 C99 6.7.4, C11 6.7.4:
2508 function-specifier:
2509 inline
2510 _Noreturn
2512 (_Noreturn is new in C11.)
2514 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2515 type-specifier:
2516 void
2517 char
2518 short
2520 long
2521 float
2522 double
2523 signed
2524 unsigned
2525 _Bool
2526 _Complex
2527 [_Imaginary removed in C99 TC2]
2528 struct-or-union-specifier
2529 enum-specifier
2530 typedef-name
2531 atomic-type-specifier
2533 (_Bool and _Complex are new in C99.)
2534 (atomic-type-specifier is new in C11.)
2536 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2538 type-qualifier:
2539 const
2540 restrict
2541 volatile
2542 address-space-qualifier
2543 _Atomic
2545 (restrict is new in C99.)
2546 (_Atomic is new in C11.)
2548 GNU extensions:
2550 declaration-specifiers:
2551 attributes declaration-specifiers[opt]
2553 type-qualifier:
2554 address-space
2556 address-space:
2557 identifier recognized by the target
2559 storage-class-specifier:
2560 __thread
2562 type-specifier:
2563 typeof-specifier
2564 __auto_type
2565 __intN
2566 _Decimal32
2567 _Decimal64
2568 _Decimal128
2569 _Fract
2570 _Accum
2571 _Sat
2573 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2574 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2576 atomic-type-specifier
2577 _Atomic ( type-name )
2579 Objective-C:
2581 type-specifier:
2582 class-name objc-protocol-refs[opt]
2583 typedef-name objc-protocol-refs
2584 objc-protocol-refs
2587 void
2588 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2589 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2590 bool alignspec_ok, bool auto_type_ok,
2591 enum c_lookahead_kind la)
2593 bool attrs_ok = start_attr_ok;
2594 bool seen_type = specs->typespec_kind != ctsk_none;
2596 if (!typespec_ok)
2597 gcc_assert (la == cla_prefer_id);
2599 while (c_parser_next_token_is (parser, CPP_NAME)
2600 || c_parser_next_token_is (parser, CPP_KEYWORD)
2601 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2603 struct c_typespec t;
2604 tree attrs;
2605 tree align;
2606 location_t loc = c_parser_peek_token (parser)->location;
2608 /* If we cannot accept a type, exit if the next token must start
2609 one. Also, if we already have seen a tagged definition,
2610 a typename would be an error anyway and likely the user
2611 has simply forgotten a semicolon, so we exit. */
2612 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2613 && c_parser_next_tokens_start_typename (parser, la)
2614 && !c_parser_next_token_is_qualifier (parser)
2615 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2616 break;
2618 if (c_parser_next_token_is (parser, CPP_NAME))
2620 c_token *name_token = c_parser_peek_token (parser);
2621 tree value = name_token->value;
2622 c_id_kind kind = name_token->id_kind;
2624 if (kind == C_ID_ADDRSPACE)
2626 addr_space_t as
2627 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2628 declspecs_add_addrspace (name_token->location, specs, as);
2629 c_parser_consume_token (parser);
2630 attrs_ok = true;
2631 continue;
2634 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2636 /* If we cannot accept a type, and the next token must start one,
2637 exit. Do the same if we already have seen a tagged definition,
2638 since it would be an error anyway and likely the user has simply
2639 forgotten a semicolon. */
2640 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2641 break;
2643 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2644 a C_ID_CLASSNAME. */
2645 c_parser_consume_token (parser);
2646 seen_type = true;
2647 attrs_ok = true;
2648 if (kind == C_ID_ID)
2650 error_at (loc, "unknown type name %qE", value);
2651 t.kind = ctsk_typedef;
2652 t.spec = error_mark_node;
2654 else if (kind == C_ID_TYPENAME
2655 && (!c_dialect_objc ()
2656 || c_parser_next_token_is_not (parser, CPP_LESS)))
2658 t.kind = ctsk_typedef;
2659 /* For a typedef name, record the meaning, not the name.
2660 In case of 'foo foo, bar;'. */
2661 t.spec = lookup_name (value);
2663 else
2665 tree proto = NULL_TREE;
2666 gcc_assert (c_dialect_objc ());
2667 t.kind = ctsk_objc;
2668 if (c_parser_next_token_is (parser, CPP_LESS))
2669 proto = c_parser_objc_protocol_refs (parser);
2670 t.spec = objc_get_protocol_qualified_type (value, proto);
2672 t.expr = NULL_TREE;
2673 t.expr_const_operands = true;
2674 declspecs_add_type (name_token->location, specs, t);
2675 continue;
2677 if (c_parser_next_token_is (parser, CPP_LESS))
2679 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2680 nisse@lysator.liu.se. */
2681 tree proto;
2682 gcc_assert (c_dialect_objc ());
2683 if (!typespec_ok || seen_type)
2684 break;
2685 proto = c_parser_objc_protocol_refs (parser);
2686 t.kind = ctsk_objc;
2687 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2688 t.expr = NULL_TREE;
2689 t.expr_const_operands = true;
2690 declspecs_add_type (loc, specs, t);
2691 continue;
2693 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2694 switch (c_parser_peek_token (parser)->keyword)
2696 case RID_STATIC:
2697 case RID_EXTERN:
2698 case RID_REGISTER:
2699 case RID_TYPEDEF:
2700 case RID_INLINE:
2701 case RID_NORETURN:
2702 case RID_AUTO:
2703 case RID_THREAD:
2704 if (!scspec_ok)
2705 goto out;
2706 attrs_ok = true;
2707 /* TODO: Distinguish between function specifiers (inline, noreturn)
2708 and storage class specifiers, either here or in
2709 declspecs_add_scspec. */
2710 declspecs_add_scspec (loc, specs,
2711 c_parser_peek_token (parser)->value);
2712 c_parser_consume_token (parser);
2713 break;
2714 case RID_AUTO_TYPE:
2715 if (!auto_type_ok)
2716 goto out;
2717 /* Fall through. */
2718 case RID_UNSIGNED:
2719 case RID_LONG:
2720 case RID_SHORT:
2721 case RID_SIGNED:
2722 case RID_COMPLEX:
2723 case RID_INT:
2724 case RID_CHAR:
2725 case RID_FLOAT:
2726 case RID_DOUBLE:
2727 case RID_VOID:
2728 case RID_DFLOAT32:
2729 case RID_DFLOAT64:
2730 case RID_DFLOAT128:
2731 CASE_RID_FLOATN_NX:
2732 case RID_BOOL:
2733 case RID_FRACT:
2734 case RID_ACCUM:
2735 case RID_SAT:
2736 case RID_INT_N_0:
2737 case RID_INT_N_1:
2738 case RID_INT_N_2:
2739 case RID_INT_N_3:
2740 if (!typespec_ok)
2741 goto out;
2742 attrs_ok = true;
2743 seen_type = true;
2744 if (c_dialect_objc ())
2745 parser->objc_need_raw_identifier = true;
2746 t.kind = ctsk_resword;
2747 t.spec = c_parser_peek_token (parser)->value;
2748 t.expr = NULL_TREE;
2749 t.expr_const_operands = true;
2750 declspecs_add_type (loc, specs, t);
2751 c_parser_consume_token (parser);
2752 break;
2753 case RID_ENUM:
2754 if (!typespec_ok)
2755 goto out;
2756 attrs_ok = true;
2757 seen_type = true;
2758 t = c_parser_enum_specifier (parser);
2759 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2760 declspecs_add_type (loc, specs, t);
2761 break;
2762 case RID_STRUCT:
2763 case RID_UNION:
2764 if (!typespec_ok)
2765 goto out;
2766 attrs_ok = true;
2767 seen_type = true;
2768 t = c_parser_struct_or_union_specifier (parser);
2769 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2770 declspecs_add_type (loc, specs, t);
2771 break;
2772 case RID_TYPEOF:
2773 /* ??? The old parser rejected typeof after other type
2774 specifiers, but is a syntax error the best way of
2775 handling this? */
2776 if (!typespec_ok || seen_type)
2777 goto out;
2778 attrs_ok = true;
2779 seen_type = true;
2780 t = c_parser_typeof_specifier (parser);
2781 declspecs_add_type (loc, specs, t);
2782 break;
2783 case RID_ATOMIC:
2784 /* C parser handling of Objective-C constructs needs
2785 checking for correct lvalue-to-rvalue conversions, and
2786 the code in build_modify_expr handling various
2787 Objective-C cases, and that in build_unary_op handling
2788 Objective-C cases for increment / decrement, also needs
2789 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2790 and objc_types_are_equivalent may also need updates. */
2791 if (c_dialect_objc ())
2792 sorry ("%<_Atomic%> in Objective-C");
2793 if (flag_isoc99)
2794 pedwarn_c99 (loc, OPT_Wpedantic,
2795 "ISO C99 does not support the %<_Atomic%> qualifier");
2796 else
2797 pedwarn_c99 (loc, OPT_Wpedantic,
2798 "ISO C90 does not support the %<_Atomic%> qualifier");
2799 attrs_ok = true;
2800 tree value;
2801 value = c_parser_peek_token (parser)->value;
2802 c_parser_consume_token (parser);
2803 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2805 /* _Atomic ( type-name ). */
2806 seen_type = true;
2807 c_parser_consume_token (parser);
2808 struct c_type_name *type = c_parser_type_name (parser);
2809 t.kind = ctsk_typeof;
2810 t.spec = error_mark_node;
2811 t.expr = NULL_TREE;
2812 t.expr_const_operands = true;
2813 if (type != NULL)
2814 t.spec = groktypename (type, &t.expr,
2815 &t.expr_const_operands);
2816 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2817 "expected %<)%>");
2818 if (t.spec != error_mark_node)
2820 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2821 error_at (loc, "%<_Atomic%>-qualified array type");
2822 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2823 error_at (loc, "%<_Atomic%>-qualified function type");
2824 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2825 error_at (loc, "%<_Atomic%> applied to a qualified type");
2826 else
2827 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2829 declspecs_add_type (loc, specs, t);
2831 else
2832 declspecs_add_qual (loc, specs, value);
2833 break;
2834 case RID_CONST:
2835 case RID_VOLATILE:
2836 case RID_RESTRICT:
2837 attrs_ok = true;
2838 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2839 c_parser_consume_token (parser);
2840 break;
2841 case RID_ATTRIBUTE:
2842 if (!attrs_ok)
2843 goto out;
2844 attrs = c_parser_attributes (parser);
2845 declspecs_add_attrs (loc, specs, attrs);
2846 break;
2847 case RID_ALIGNAS:
2848 if (!alignspec_ok)
2849 goto out;
2850 align = c_parser_alignas_specifier (parser);
2851 declspecs_add_alignas (loc, specs, align);
2852 break;
2853 case RID_GIMPLE:
2854 if (! flag_gimple)
2855 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2856 c_parser_consume_token (parser);
2857 specs->gimple_p = true;
2858 specs->locations[cdw_gimple] = loc;
2859 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2860 break;
2861 case RID_RTL:
2862 c_parser_consume_token (parser);
2863 specs->rtl_p = true;
2864 specs->locations[cdw_rtl] = loc;
2865 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2866 break;
2867 default:
2868 goto out;
2871 out: ;
2874 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2876 enum-specifier:
2877 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2878 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2879 enum attributes[opt] identifier
2881 The form with trailing comma is new in C99. The forms with
2882 attributes are GNU extensions. In GNU C, we accept any expression
2883 without commas in the syntax (assignment expressions, not just
2884 conditional expressions); assignment expressions will be diagnosed
2885 as non-constant.
2887 enumerator-list:
2888 enumerator
2889 enumerator-list , enumerator
2891 enumerator:
2892 enumeration-constant
2893 enumeration-constant = constant-expression
2895 GNU Extensions:
2897 enumerator:
2898 enumeration-constant attributes[opt]
2899 enumeration-constant attributes[opt] = constant-expression
2903 static struct c_typespec
2904 c_parser_enum_specifier (c_parser *parser)
2906 struct c_typespec ret;
2907 tree attrs;
2908 tree ident = NULL_TREE;
2909 location_t enum_loc;
2910 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2911 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2912 c_parser_consume_token (parser);
2913 attrs = c_parser_attributes (parser);
2914 enum_loc = c_parser_peek_token (parser)->location;
2915 /* Set the location in case we create a decl now. */
2916 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2917 if (c_parser_next_token_is (parser, CPP_NAME))
2919 ident = c_parser_peek_token (parser)->value;
2920 ident_loc = c_parser_peek_token (parser)->location;
2921 enum_loc = ident_loc;
2922 c_parser_consume_token (parser);
2924 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2926 /* Parse an enum definition. */
2927 struct c_enum_contents the_enum;
2928 tree type;
2929 tree postfix_attrs;
2930 /* We chain the enumerators in reverse order, then put them in
2931 forward order at the end. */
2932 tree values;
2933 timevar_push (TV_PARSE_ENUM);
2934 type = start_enum (enum_loc, &the_enum, ident);
2935 values = NULL_TREE;
2936 c_parser_consume_token (parser);
2937 while (true)
2939 tree enum_id;
2940 tree enum_value;
2941 tree enum_decl;
2942 bool seen_comma;
2943 c_token *token;
2944 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2945 location_t decl_loc, value_loc;
2946 if (c_parser_next_token_is_not (parser, CPP_NAME))
2948 /* Give a nicer error for "enum {}". */
2949 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2950 && !parser->error)
2952 error_at (c_parser_peek_token (parser)->location,
2953 "empty enum is invalid");
2954 parser->error = true;
2956 else
2957 c_parser_error (parser, "expected identifier");
2958 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2959 values = error_mark_node;
2960 break;
2962 token = c_parser_peek_token (parser);
2963 enum_id = token->value;
2964 /* Set the location in case we create a decl now. */
2965 c_parser_set_source_position_from_token (token);
2966 decl_loc = value_loc = token->location;
2967 c_parser_consume_token (parser);
2968 /* Parse any specified attributes. */
2969 tree enum_attrs = c_parser_attributes (parser);
2970 if (c_parser_next_token_is (parser, CPP_EQ))
2972 c_parser_consume_token (parser);
2973 value_loc = c_parser_peek_token (parser)->location;
2974 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2976 else
2977 enum_value = NULL_TREE;
2978 enum_decl = build_enumerator (decl_loc, value_loc,
2979 &the_enum, enum_id, enum_value);
2980 if (enum_attrs)
2981 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2982 TREE_CHAIN (enum_decl) = values;
2983 values = enum_decl;
2984 seen_comma = false;
2985 if (c_parser_next_token_is (parser, CPP_COMMA))
2987 comma_loc = c_parser_peek_token (parser)->location;
2988 seen_comma = true;
2989 c_parser_consume_token (parser);
2991 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2993 if (seen_comma)
2994 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2995 "comma at end of enumerator list");
2996 c_parser_consume_token (parser);
2997 break;
2999 if (!seen_comma)
3001 c_parser_error (parser, "expected %<,%> or %<}%>");
3002 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3003 values = error_mark_node;
3004 break;
3007 postfix_attrs = c_parser_attributes (parser);
3008 ret.spec = finish_enum (type, nreverse (values),
3009 chainon (attrs, postfix_attrs));
3010 ret.kind = ctsk_tagdef;
3011 ret.expr = NULL_TREE;
3012 ret.expr_const_operands = true;
3013 timevar_pop (TV_PARSE_ENUM);
3014 return ret;
3016 else if (!ident)
3018 c_parser_error (parser, "expected %<{%>");
3019 ret.spec = error_mark_node;
3020 ret.kind = ctsk_tagref;
3021 ret.expr = NULL_TREE;
3022 ret.expr_const_operands = true;
3023 return ret;
3025 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3026 /* In ISO C, enumerated types can be referred to only if already
3027 defined. */
3028 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3030 gcc_assert (ident);
3031 pedwarn (enum_loc, OPT_Wpedantic,
3032 "ISO C forbids forward references to %<enum%> types");
3034 return ret;
3037 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3039 struct-or-union-specifier:
3040 struct-or-union attributes[opt] identifier[opt]
3041 { struct-contents } attributes[opt]
3042 struct-or-union attributes[opt] identifier
3044 struct-contents:
3045 struct-declaration-list
3047 struct-declaration-list:
3048 struct-declaration ;
3049 struct-declaration-list struct-declaration ;
3051 GNU extensions:
3053 struct-contents:
3054 empty
3055 struct-declaration
3056 struct-declaration-list struct-declaration
3058 struct-declaration-list:
3059 struct-declaration-list ;
3062 (Note that in the syntax here, unlike that in ISO C, the semicolons
3063 are included here rather than in struct-declaration, in order to
3064 describe the syntax with extra semicolons and missing semicolon at
3065 end.)
3067 Objective-C:
3069 struct-declaration-list:
3070 @defs ( class-name )
3072 (Note this does not include a trailing semicolon, but can be
3073 followed by further declarations, and gets a pedwarn-if-pedantic
3074 when followed by a semicolon.) */
3076 static struct c_typespec
3077 c_parser_struct_or_union_specifier (c_parser *parser)
3079 struct c_typespec ret;
3080 tree attrs;
3081 tree ident = NULL_TREE;
3082 location_t struct_loc;
3083 location_t ident_loc = UNKNOWN_LOCATION;
3084 enum tree_code code;
3085 switch (c_parser_peek_token (parser)->keyword)
3087 case RID_STRUCT:
3088 code = RECORD_TYPE;
3089 break;
3090 case RID_UNION:
3091 code = UNION_TYPE;
3092 break;
3093 default:
3094 gcc_unreachable ();
3096 struct_loc = c_parser_peek_token (parser)->location;
3097 c_parser_consume_token (parser);
3098 attrs = c_parser_attributes (parser);
3100 /* Set the location in case we create a decl now. */
3101 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3103 if (c_parser_next_token_is (parser, CPP_NAME))
3105 ident = c_parser_peek_token (parser)->value;
3106 ident_loc = c_parser_peek_token (parser)->location;
3107 struct_loc = ident_loc;
3108 c_parser_consume_token (parser);
3110 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3112 /* Parse a struct or union definition. Start the scope of the
3113 tag before parsing components. */
3114 struct c_struct_parse_info *struct_info;
3115 tree type = start_struct (struct_loc, code, ident, &struct_info);
3116 tree postfix_attrs;
3117 /* We chain the components in reverse order, then put them in
3118 forward order at the end. Each struct-declaration may
3119 declare multiple components (comma-separated), so we must use
3120 chainon to join them, although when parsing each
3121 struct-declaration we can use TREE_CHAIN directly.
3123 The theory behind all this is that there will be more
3124 semicolon separated fields than comma separated fields, and
3125 so we'll be minimizing the number of node traversals required
3126 by chainon. */
3127 tree contents;
3128 timevar_push (TV_PARSE_STRUCT);
3129 contents = NULL_TREE;
3130 c_parser_consume_token (parser);
3131 /* Handle the Objective-C @defs construct,
3132 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3133 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3135 tree name;
3136 gcc_assert (c_dialect_objc ());
3137 c_parser_consume_token (parser);
3138 matching_parens parens;
3139 if (!parens.require_open (parser))
3140 goto end_at_defs;
3141 if (c_parser_next_token_is (parser, CPP_NAME)
3142 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3144 name = c_parser_peek_token (parser)->value;
3145 c_parser_consume_token (parser);
3147 else
3149 c_parser_error (parser, "expected class name");
3150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3151 goto end_at_defs;
3153 parens.skip_until_found_close (parser);
3154 contents = nreverse (objc_get_class_ivars (name));
3156 end_at_defs:
3157 /* Parse the struct-declarations and semicolons. Problems with
3158 semicolons are diagnosed here; empty structures are diagnosed
3159 elsewhere. */
3160 while (true)
3162 tree decls;
3163 /* Parse any stray semicolon. */
3164 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3166 location_t semicolon_loc
3167 = c_parser_peek_token (parser)->location;
3168 gcc_rich_location richloc (semicolon_loc);
3169 richloc.add_fixit_remove ();
3170 pedwarn (&richloc, OPT_Wpedantic,
3171 "extra semicolon in struct or union specified");
3172 c_parser_consume_token (parser);
3173 continue;
3175 /* Stop if at the end of the struct or union contents. */
3176 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3178 c_parser_consume_token (parser);
3179 break;
3181 /* Accept #pragmas at struct scope. */
3182 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3184 c_parser_pragma (parser, pragma_struct, NULL);
3185 continue;
3187 /* Parse some comma-separated declarations, but not the
3188 trailing semicolon if any. */
3189 decls = c_parser_struct_declaration (parser);
3190 contents = chainon (decls, contents);
3191 /* If no semicolon follows, either we have a parse error or
3192 are at the end of the struct or union and should
3193 pedwarn. */
3194 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3195 c_parser_consume_token (parser);
3196 else
3198 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3199 pedwarn (c_parser_peek_token (parser)->location, 0,
3200 "no semicolon at end of struct or union");
3201 else if (parser->error
3202 || !c_parser_next_token_starts_declspecs (parser))
3204 c_parser_error (parser, "expected %<;%>");
3205 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3206 break;
3209 /* If we come here, we have already emitted an error
3210 for an expected `;', identifier or `(', and we also
3211 recovered already. Go on with the next field. */
3214 postfix_attrs = c_parser_attributes (parser);
3215 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3216 chainon (attrs, postfix_attrs), struct_info);
3217 ret.kind = ctsk_tagdef;
3218 ret.expr = NULL_TREE;
3219 ret.expr_const_operands = true;
3220 timevar_pop (TV_PARSE_STRUCT);
3221 return ret;
3223 else if (!ident)
3225 c_parser_error (parser, "expected %<{%>");
3226 ret.spec = error_mark_node;
3227 ret.kind = ctsk_tagref;
3228 ret.expr = NULL_TREE;
3229 ret.expr_const_operands = true;
3230 return ret;
3232 ret = parser_xref_tag (ident_loc, code, ident);
3233 return ret;
3236 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3237 *without* the trailing semicolon.
3239 struct-declaration:
3240 specifier-qualifier-list struct-declarator-list
3241 static_assert-declaration-no-semi
3243 specifier-qualifier-list:
3244 type-specifier specifier-qualifier-list[opt]
3245 type-qualifier specifier-qualifier-list[opt]
3246 alignment-specifier specifier-qualifier-list[opt]
3247 attributes specifier-qualifier-list[opt]
3249 struct-declarator-list:
3250 struct-declarator
3251 struct-declarator-list , attributes[opt] struct-declarator
3253 struct-declarator:
3254 declarator attributes[opt]
3255 declarator[opt] : constant-expression attributes[opt]
3257 GNU extensions:
3259 struct-declaration:
3260 __extension__ struct-declaration
3261 specifier-qualifier-list
3263 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3264 of attributes where shown is a GNU extension. In GNU C, we accept
3265 any expression without commas in the syntax (assignment
3266 expressions, not just conditional expressions); assignment
3267 expressions will be diagnosed as non-constant. */
3269 static tree
3270 c_parser_struct_declaration (c_parser *parser)
3272 struct c_declspecs *specs;
3273 tree prefix_attrs;
3274 tree all_prefix_attrs;
3275 tree decls;
3276 location_t decl_loc;
3277 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3279 int ext;
3280 tree decl;
3281 ext = disable_extension_diagnostics ();
3282 c_parser_consume_token (parser);
3283 decl = c_parser_struct_declaration (parser);
3284 restore_extension_diagnostics (ext);
3285 return decl;
3287 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3289 c_parser_static_assert_declaration_no_semi (parser);
3290 return NULL_TREE;
3292 specs = build_null_declspecs ();
3293 decl_loc = c_parser_peek_token (parser)->location;
3294 /* Strictly by the standard, we shouldn't allow _Alignas here,
3295 but it appears to have been intended to allow it there, so
3296 we're keeping it as it is until WG14 reaches a conclusion
3297 of N1731.
3298 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3299 c_parser_declspecs (parser, specs, false, true, true,
3300 true, false, cla_nonabstract_decl);
3301 if (parser->error)
3302 return NULL_TREE;
3303 if (!specs->declspecs_seen_p)
3305 c_parser_error (parser, "expected specifier-qualifier-list");
3306 return NULL_TREE;
3308 finish_declspecs (specs);
3309 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3310 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3312 tree ret;
3313 if (specs->typespec_kind == ctsk_none)
3315 pedwarn (decl_loc, OPT_Wpedantic,
3316 "ISO C forbids member declarations with no members");
3317 shadow_tag_warned (specs, pedantic);
3318 ret = NULL_TREE;
3320 else
3322 /* Support for unnamed structs or unions as members of
3323 structs or unions (which is [a] useful and [b] supports
3324 MS P-SDK). */
3325 tree attrs = NULL;
3327 ret = grokfield (c_parser_peek_token (parser)->location,
3328 build_id_declarator (NULL_TREE), specs,
3329 NULL_TREE, &attrs);
3330 if (ret)
3331 decl_attributes (&ret, attrs, 0);
3333 return ret;
3336 /* Provide better error recovery. Note that a type name here is valid,
3337 and will be treated as a field name. */
3338 if (specs->typespec_kind == ctsk_tagdef
3339 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3340 && c_parser_next_token_starts_declspecs (parser)
3341 && !c_parser_next_token_is (parser, CPP_NAME))
3343 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3344 parser->error = false;
3345 return NULL_TREE;
3348 pending_xref_error ();
3349 prefix_attrs = specs->attrs;
3350 all_prefix_attrs = prefix_attrs;
3351 specs->attrs = NULL_TREE;
3352 decls = NULL_TREE;
3353 while (true)
3355 /* Declaring one or more declarators or un-named bit-fields. */
3356 struct c_declarator *declarator;
3357 bool dummy = false;
3358 if (c_parser_next_token_is (parser, CPP_COLON))
3359 declarator = build_id_declarator (NULL_TREE);
3360 else
3361 declarator = c_parser_declarator (parser,
3362 specs->typespec_kind != ctsk_none,
3363 C_DTR_NORMAL, &dummy);
3364 if (declarator == NULL)
3366 c_parser_skip_to_end_of_block_or_statement (parser);
3367 break;
3369 if (c_parser_next_token_is (parser, CPP_COLON)
3370 || c_parser_next_token_is (parser, CPP_COMMA)
3371 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3372 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3373 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3375 tree postfix_attrs = NULL_TREE;
3376 tree width = NULL_TREE;
3377 tree d;
3378 if (c_parser_next_token_is (parser, CPP_COLON))
3380 c_parser_consume_token (parser);
3381 width = c_parser_expr_no_commas (parser, NULL).value;
3383 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3384 postfix_attrs = c_parser_attributes (parser);
3385 d = grokfield (c_parser_peek_token (parser)->location,
3386 declarator, specs, width, &all_prefix_attrs);
3387 decl_attributes (&d, chainon (postfix_attrs,
3388 all_prefix_attrs), 0);
3389 DECL_CHAIN (d) = decls;
3390 decls = d;
3391 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3392 all_prefix_attrs = chainon (c_parser_attributes (parser),
3393 prefix_attrs);
3394 else
3395 all_prefix_attrs = prefix_attrs;
3396 if (c_parser_next_token_is (parser, CPP_COMMA))
3397 c_parser_consume_token (parser);
3398 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3399 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3401 /* Semicolon consumed in caller. */
3402 break;
3404 else
3406 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3407 break;
3410 else
3412 c_parser_error (parser,
3413 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3414 "%<__attribute__%>");
3415 break;
3418 return decls;
3421 /* Parse a typeof specifier (a GNU extension).
3423 typeof-specifier:
3424 typeof ( expression )
3425 typeof ( type-name )
3428 static struct c_typespec
3429 c_parser_typeof_specifier (c_parser *parser)
3431 struct c_typespec ret;
3432 ret.kind = ctsk_typeof;
3433 ret.spec = error_mark_node;
3434 ret.expr = NULL_TREE;
3435 ret.expr_const_operands = true;
3436 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3437 c_parser_consume_token (parser);
3438 c_inhibit_evaluation_warnings++;
3439 in_typeof++;
3440 matching_parens parens;
3441 if (!parens.require_open (parser))
3443 c_inhibit_evaluation_warnings--;
3444 in_typeof--;
3445 return ret;
3447 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3449 struct c_type_name *type = c_parser_type_name (parser);
3450 c_inhibit_evaluation_warnings--;
3451 in_typeof--;
3452 if (type != NULL)
3454 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3455 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3458 else
3460 bool was_vm;
3461 location_t here = c_parser_peek_token (parser)->location;
3462 struct c_expr expr = c_parser_expression (parser);
3463 c_inhibit_evaluation_warnings--;
3464 in_typeof--;
3465 if (TREE_CODE (expr.value) == COMPONENT_REF
3466 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3467 error_at (here, "%<typeof%> applied to a bit-field");
3468 mark_exp_read (expr.value);
3469 ret.spec = TREE_TYPE (expr.value);
3470 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3471 /* This is returned with the type so that when the type is
3472 evaluated, this can be evaluated. */
3473 if (was_vm)
3474 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3475 pop_maybe_used (was_vm);
3476 /* For use in macros such as those in <stdatomic.h>, remove all
3477 qualifiers from atomic types. (const can be an issue for more macros
3478 using typeof than just the <stdatomic.h> ones.) */
3479 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3480 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3482 parens.skip_until_found_close (parser);
3483 return ret;
3486 /* Parse an alignment-specifier.
3488 C11 6.7.5:
3490 alignment-specifier:
3491 _Alignas ( type-name )
3492 _Alignas ( constant-expression )
3495 static tree
3496 c_parser_alignas_specifier (c_parser * parser)
3498 tree ret = error_mark_node;
3499 location_t loc = c_parser_peek_token (parser)->location;
3500 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3501 c_parser_consume_token (parser);
3502 if (flag_isoc99)
3503 pedwarn_c99 (loc, OPT_Wpedantic,
3504 "ISO C99 does not support %<_Alignas%>");
3505 else
3506 pedwarn_c99 (loc, OPT_Wpedantic,
3507 "ISO C90 does not support %<_Alignas%>");
3508 matching_parens parens;
3509 if (!parens.require_open (parser))
3510 return ret;
3511 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3513 struct c_type_name *type = c_parser_type_name (parser);
3514 if (type != NULL)
3515 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3516 false, true, 1);
3518 else
3519 ret = c_parser_expr_no_commas (parser, NULL).value;
3520 parens.skip_until_found_close (parser);
3521 return ret;
3524 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3525 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3526 a typedef name may be redeclared; otherwise it may not. KIND
3527 indicates which kind of declarator is wanted. Returns a valid
3528 declarator except in the case of a syntax error in which case NULL is
3529 returned. *SEEN_ID is set to true if an identifier being declared is
3530 seen; this is used to diagnose bad forms of abstract array declarators
3531 and to determine whether an identifier list is syntactically permitted.
3533 declarator:
3534 pointer[opt] direct-declarator
3536 direct-declarator:
3537 identifier
3538 ( attributes[opt] declarator )
3539 direct-declarator array-declarator
3540 direct-declarator ( parameter-type-list )
3541 direct-declarator ( identifier-list[opt] )
3543 pointer:
3544 * type-qualifier-list[opt]
3545 * type-qualifier-list[opt] pointer
3547 type-qualifier-list:
3548 type-qualifier
3549 attributes
3550 type-qualifier-list type-qualifier
3551 type-qualifier-list attributes
3553 array-declarator:
3554 [ type-qualifier-list[opt] assignment-expression[opt] ]
3555 [ static type-qualifier-list[opt] assignment-expression ]
3556 [ type-qualifier-list static assignment-expression ]
3557 [ type-qualifier-list[opt] * ]
3559 parameter-type-list:
3560 parameter-list
3561 parameter-list , ...
3563 parameter-list:
3564 parameter-declaration
3565 parameter-list , parameter-declaration
3567 parameter-declaration:
3568 declaration-specifiers declarator attributes[opt]
3569 declaration-specifiers abstract-declarator[opt] attributes[opt]
3571 identifier-list:
3572 identifier
3573 identifier-list , identifier
3575 abstract-declarator:
3576 pointer
3577 pointer[opt] direct-abstract-declarator
3579 direct-abstract-declarator:
3580 ( attributes[opt] abstract-declarator )
3581 direct-abstract-declarator[opt] array-declarator
3582 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3584 GNU extensions:
3586 direct-declarator:
3587 direct-declarator ( parameter-forward-declarations
3588 parameter-type-list[opt] )
3590 direct-abstract-declarator:
3591 direct-abstract-declarator[opt] ( parameter-forward-declarations
3592 parameter-type-list[opt] )
3594 parameter-forward-declarations:
3595 parameter-list ;
3596 parameter-forward-declarations parameter-list ;
3598 The uses of attributes shown above are GNU extensions.
3600 Some forms of array declarator are not included in C99 in the
3601 syntax for abstract declarators; these are disallowed elsewhere.
3602 This may be a defect (DR#289).
3604 This function also accepts an omitted abstract declarator as being
3605 an abstract declarator, although not part of the formal syntax. */
3607 struct c_declarator *
3608 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3609 bool *seen_id)
3611 /* Parse any initial pointer part. */
3612 if (c_parser_next_token_is (parser, CPP_MULT))
3614 struct c_declspecs *quals_attrs = build_null_declspecs ();
3615 struct c_declarator *inner;
3616 c_parser_consume_token (parser);
3617 c_parser_declspecs (parser, quals_attrs, false, false, true,
3618 false, false, cla_prefer_id);
3619 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3620 if (inner == NULL)
3621 return NULL;
3622 else
3623 return make_pointer_declarator (quals_attrs, inner);
3625 /* Now we have a direct declarator, direct abstract declarator or
3626 nothing (which counts as a direct abstract declarator here). */
3627 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3630 /* Parse a direct declarator or direct abstract declarator; arguments
3631 as c_parser_declarator. */
3633 static struct c_declarator *
3634 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3635 bool *seen_id)
3637 /* The direct declarator must start with an identifier (possibly
3638 omitted) or a parenthesized declarator (possibly abstract). In
3639 an ordinary declarator, initial parentheses must start a
3640 parenthesized declarator. In an abstract declarator or parameter
3641 declarator, they could start a parenthesized declarator or a
3642 parameter list. To tell which, the open parenthesis and any
3643 following attributes must be read. If a declaration specifier
3644 follows, then it is a parameter list; if the specifier is a
3645 typedef name, there might be an ambiguity about redeclaring it,
3646 which is resolved in the direction of treating it as a typedef
3647 name. If a close parenthesis follows, it is also an empty
3648 parameter list, as the syntax does not permit empty abstract
3649 declarators. Otherwise, it is a parenthesized declarator (in
3650 which case the analysis may be repeated inside it, recursively).
3652 ??? There is an ambiguity in a parameter declaration "int
3653 (__attribute__((foo)) x)", where x is not a typedef name: it
3654 could be an abstract declarator for a function, or declare x with
3655 parentheses. The proper resolution of this ambiguity needs
3656 documenting. At present we follow an accident of the old
3657 parser's implementation, whereby the first parameter must have
3658 some declaration specifiers other than just attributes. Thus as
3659 a parameter declaration it is treated as a parenthesized
3660 parameter named x, and as an abstract declarator it is
3661 rejected.
3663 ??? Also following the old parser, attributes inside an empty
3664 parameter list are ignored, making it a list not yielding a
3665 prototype, rather than giving an error or making it have one
3666 parameter with implicit type int.
3668 ??? Also following the old parser, typedef names may be
3669 redeclared in declarators, but not Objective-C class names. */
3671 if (kind != C_DTR_ABSTRACT
3672 && c_parser_next_token_is (parser, CPP_NAME)
3673 && ((type_seen_p
3674 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3675 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3676 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3678 struct c_declarator *inner
3679 = build_id_declarator (c_parser_peek_token (parser)->value);
3680 *seen_id = true;
3681 inner->id_loc = c_parser_peek_token (parser)->location;
3682 c_parser_consume_token (parser);
3683 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3686 if (kind != C_DTR_NORMAL
3687 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3689 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3690 inner->id_loc = c_parser_peek_token (parser)->location;
3691 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3694 /* Either we are at the end of an abstract declarator, or we have
3695 parentheses. */
3697 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3699 tree attrs;
3700 struct c_declarator *inner;
3701 c_parser_consume_token (parser);
3702 attrs = c_parser_attributes (parser);
3703 if (kind != C_DTR_NORMAL
3704 && (c_parser_next_token_starts_declspecs (parser)
3705 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3707 struct c_arg_info *args
3708 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3709 attrs);
3710 if (args == NULL)
3711 return NULL;
3712 else
3714 inner
3715 = build_function_declarator (args,
3716 build_id_declarator (NULL_TREE));
3717 return c_parser_direct_declarator_inner (parser, *seen_id,
3718 inner);
3721 /* A parenthesized declarator. */
3722 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3723 if (inner != NULL && attrs != NULL)
3724 inner = build_attrs_declarator (attrs, inner);
3725 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3727 c_parser_consume_token (parser);
3728 if (inner == NULL)
3729 return NULL;
3730 else
3731 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3733 else
3735 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3736 "expected %<)%>");
3737 return NULL;
3740 else
3742 if (kind == C_DTR_NORMAL)
3744 c_parser_error (parser, "expected identifier or %<(%>");
3745 return NULL;
3747 else
3748 return build_id_declarator (NULL_TREE);
3752 /* Parse part of a direct declarator or direct abstract declarator,
3753 given that some (in INNER) has already been parsed; ID_PRESENT is
3754 true if an identifier is present, false for an abstract
3755 declarator. */
3757 static struct c_declarator *
3758 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3759 struct c_declarator *inner)
3761 /* Parse a sequence of array declarators and parameter lists. */
3762 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3764 location_t brace_loc = c_parser_peek_token (parser)->location;
3765 struct c_declarator *declarator;
3766 struct c_declspecs *quals_attrs = build_null_declspecs ();
3767 bool static_seen;
3768 bool star_seen;
3769 struct c_expr dimen;
3770 dimen.value = NULL_TREE;
3771 dimen.original_code = ERROR_MARK;
3772 dimen.original_type = NULL_TREE;
3773 c_parser_consume_token (parser);
3774 c_parser_declspecs (parser, quals_attrs, false, false, true,
3775 false, false, cla_prefer_id);
3776 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3777 if (static_seen)
3778 c_parser_consume_token (parser);
3779 if (static_seen && !quals_attrs->declspecs_seen_p)
3780 c_parser_declspecs (parser, quals_attrs, false, false, true,
3781 false, false, cla_prefer_id);
3782 if (!quals_attrs->declspecs_seen_p)
3783 quals_attrs = NULL;
3784 /* If "static" is present, there must be an array dimension.
3785 Otherwise, there may be a dimension, "*", or no
3786 dimension. */
3787 if (static_seen)
3789 star_seen = false;
3790 dimen = c_parser_expr_no_commas (parser, NULL);
3792 else
3794 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3796 dimen.value = NULL_TREE;
3797 star_seen = false;
3799 else if (c_parser_next_token_is (parser, CPP_MULT))
3801 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3803 dimen.value = NULL_TREE;
3804 star_seen = true;
3805 c_parser_consume_token (parser);
3807 else
3809 star_seen = false;
3810 dimen = c_parser_expr_no_commas (parser, NULL);
3813 else
3815 star_seen = false;
3816 dimen = c_parser_expr_no_commas (parser, NULL);
3819 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3820 c_parser_consume_token (parser);
3821 else
3823 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3824 "expected %<]%>");
3825 return NULL;
3827 if (dimen.value)
3828 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3829 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3830 static_seen, star_seen);
3831 if (declarator == NULL)
3832 return NULL;
3833 inner = set_array_declarator_inner (declarator, inner);
3834 return c_parser_direct_declarator_inner (parser, id_present, inner);
3836 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3838 tree attrs;
3839 struct c_arg_info *args;
3840 c_parser_consume_token (parser);
3841 attrs = c_parser_attributes (parser);
3842 args = c_parser_parms_declarator (parser, id_present, attrs);
3843 if (args == NULL)
3844 return NULL;
3845 else
3847 inner = build_function_declarator (args, inner);
3848 return c_parser_direct_declarator_inner (parser, id_present, inner);
3851 return inner;
3854 /* Parse a parameter list or identifier list, including the closing
3855 parenthesis but not the opening one. ATTRS are the attributes at
3856 the start of the list. ID_LIST_OK is true if an identifier list is
3857 acceptable; such a list must not have attributes at the start. */
3859 static struct c_arg_info *
3860 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3862 push_scope ();
3863 declare_parm_level ();
3864 /* If the list starts with an identifier, it is an identifier list.
3865 Otherwise, it is either a prototype list or an empty list. */
3866 if (id_list_ok
3867 && !attrs
3868 && c_parser_next_token_is (parser, CPP_NAME)
3869 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3871 /* Look ahead to detect typos in type names. */
3872 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3873 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3874 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3875 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3876 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3878 tree list = NULL_TREE, *nextp = &list;
3879 while (c_parser_next_token_is (parser, CPP_NAME)
3880 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3882 *nextp = build_tree_list (NULL_TREE,
3883 c_parser_peek_token (parser)->value);
3884 nextp = & TREE_CHAIN (*nextp);
3885 c_parser_consume_token (parser);
3886 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3887 break;
3888 c_parser_consume_token (parser);
3889 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3891 c_parser_error (parser, "expected identifier");
3892 break;
3895 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3897 struct c_arg_info *ret = build_arg_info ();
3898 ret->types = list;
3899 c_parser_consume_token (parser);
3900 pop_scope ();
3901 return ret;
3903 else
3905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3906 "expected %<)%>");
3907 pop_scope ();
3908 return NULL;
3911 else
3913 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3914 NULL);
3915 pop_scope ();
3916 return ret;
3920 /* Parse a parameter list (possibly empty), including the closing
3921 parenthesis but not the opening one. ATTRS are the attributes at
3922 the start of the list. EXPR is NULL or an expression that needs to
3923 be evaluated for the side effects of array size expressions in the
3924 parameters. */
3926 static struct c_arg_info *
3927 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3929 bool bad_parm = false;
3931 /* ??? Following the old parser, forward parameter declarations may
3932 use abstract declarators, and if no real parameter declarations
3933 follow the forward declarations then this is not diagnosed. Also
3934 note as above that attributes are ignored as the only contents of
3935 the parentheses, or as the only contents after forward
3936 declarations. */
3937 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3939 struct c_arg_info *ret = build_arg_info ();
3940 c_parser_consume_token (parser);
3941 return ret;
3943 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3945 struct c_arg_info *ret = build_arg_info ();
3947 if (flag_allow_parameterless_variadic_functions)
3949 /* F (...) is allowed. */
3950 ret->types = NULL_TREE;
3952 else
3954 /* Suppress -Wold-style-definition for this case. */
3955 ret->types = error_mark_node;
3956 error_at (c_parser_peek_token (parser)->location,
3957 "ISO C requires a named argument before %<...%>");
3959 c_parser_consume_token (parser);
3960 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3962 c_parser_consume_token (parser);
3963 return ret;
3965 else
3967 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3968 "expected %<)%>");
3969 return NULL;
3972 /* Nonempty list of parameters, either terminated with semicolon
3973 (forward declarations; recurse) or with close parenthesis (normal
3974 function) or with ", ... )" (variadic function). */
3975 while (true)
3977 /* Parse a parameter. */
3978 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3979 attrs = NULL_TREE;
3980 if (parm == NULL)
3981 bad_parm = true;
3982 else
3983 push_parm_decl (parm, &expr);
3984 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3986 tree new_attrs;
3987 c_parser_consume_token (parser);
3988 mark_forward_parm_decls ();
3989 new_attrs = c_parser_attributes (parser);
3990 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3992 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3994 c_parser_consume_token (parser);
3995 if (bad_parm)
3996 return NULL;
3997 else
3998 return get_parm_info (false, expr);
4000 if (!c_parser_require (parser, CPP_COMMA,
4001 "expected %<;%>, %<,%> or %<)%>",
4002 UNKNOWN_LOCATION, false))
4004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4005 return NULL;
4007 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4009 c_parser_consume_token (parser);
4010 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4012 c_parser_consume_token (parser);
4013 if (bad_parm)
4014 return NULL;
4015 else
4016 return get_parm_info (true, expr);
4018 else
4020 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4021 "expected %<)%>");
4022 return NULL;
4028 /* Parse a parameter declaration. ATTRS are the attributes at the
4029 start of the declaration if it is the first parameter. */
4031 static struct c_parm *
4032 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4034 struct c_declspecs *specs;
4035 struct c_declarator *declarator;
4036 tree prefix_attrs;
4037 tree postfix_attrs = NULL_TREE;
4038 bool dummy = false;
4040 /* Accept #pragmas between parameter declarations. */
4041 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4042 c_parser_pragma (parser, pragma_param, NULL);
4044 if (!c_parser_next_token_starts_declspecs (parser))
4046 c_token *token = c_parser_peek_token (parser);
4047 if (parser->error)
4048 return NULL;
4049 c_parser_set_source_position_from_token (token);
4050 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4052 name_hint hint = lookup_name_fuzzy (token->value,
4053 FUZZY_LOOKUP_TYPENAME,
4054 token->location);
4055 if (hint)
4057 gcc_rich_location richloc (token->location);
4058 richloc.add_fixit_replace (hint.suggestion ());
4059 error_at (&richloc,
4060 "unknown type name %qE; did you mean %qs?",
4061 token->value, hint.suggestion ());
4063 else
4064 error_at (token->location, "unknown type name %qE", token->value);
4065 parser->error = true;
4067 /* ??? In some Objective-C cases '...' isn't applicable so there
4068 should be a different message. */
4069 else
4070 c_parser_error (parser,
4071 "expected declaration specifiers or %<...%>");
4072 c_parser_skip_to_end_of_parameter (parser);
4073 return NULL;
4076 location_t start_loc = c_parser_peek_token (parser)->location;
4078 specs = build_null_declspecs ();
4079 if (attrs)
4081 declspecs_add_attrs (input_location, specs, attrs);
4082 attrs = NULL_TREE;
4084 c_parser_declspecs (parser, specs, true, true, true, true, false,
4085 cla_nonabstract_decl);
4086 finish_declspecs (specs);
4087 pending_xref_error ();
4088 prefix_attrs = specs->attrs;
4089 specs->attrs = NULL_TREE;
4090 declarator = c_parser_declarator (parser,
4091 specs->typespec_kind != ctsk_none,
4092 C_DTR_PARM, &dummy);
4093 if (declarator == NULL)
4095 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4096 return NULL;
4098 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4099 postfix_attrs = c_parser_attributes (parser);
4101 /* Generate a location for the parameter, ranging from the start of the
4102 initial token to the end of the final token.
4104 If we have a identifier, then use it for the caret location, e.g.
4106 extern int callee (int one, int (*two)(int, int), float three);
4107 ~~~~~~^~~~~~~~~~~~~~
4109 otherwise, reuse the start location for the caret location e.g.:
4111 extern int callee (int one, int (*)(int, int), float three);
4112 ^~~~~~~~~~~~~~~~~
4114 location_t end_loc = parser->last_token_location;
4116 /* Find any cdk_id declarator; determine if we have an identifier. */
4117 c_declarator *id_declarator = declarator;
4118 while (id_declarator && id_declarator->kind != cdk_id)
4119 id_declarator = id_declarator->declarator;
4120 location_t caret_loc = (id_declarator->u.id
4121 ? id_declarator->id_loc
4122 : start_loc);
4123 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4125 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4126 declarator, param_loc);
4129 /* Parse a string literal in an asm expression. It should not be
4130 translated, and wide string literals are an error although
4131 permitted by the syntax. This is a GNU extension.
4133 asm-string-literal:
4134 string-literal
4136 ??? At present, following the old parser, the caller needs to have
4137 set lex_untranslated_string to 1. It would be better to follow the
4138 C++ parser rather than using this kludge. */
4140 static tree
4141 c_parser_asm_string_literal (c_parser *parser)
4143 tree str;
4144 int save_flag = warn_overlength_strings;
4145 warn_overlength_strings = 0;
4146 if (c_parser_next_token_is (parser, CPP_STRING))
4148 str = c_parser_peek_token (parser)->value;
4149 c_parser_consume_token (parser);
4151 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4153 error_at (c_parser_peek_token (parser)->location,
4154 "wide string literal in %<asm%>");
4155 str = build_string (1, "");
4156 c_parser_consume_token (parser);
4158 else
4160 c_parser_error (parser, "expected string literal");
4161 str = NULL_TREE;
4163 warn_overlength_strings = save_flag;
4164 return str;
4167 /* Parse a simple asm expression. This is used in restricted
4168 contexts, where a full expression with inputs and outputs does not
4169 make sense. This is a GNU extension.
4171 simple-asm-expr:
4172 asm ( asm-string-literal )
4175 static tree
4176 c_parser_simple_asm_expr (c_parser *parser)
4178 tree str;
4179 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4180 /* ??? Follow the C++ parser rather than using the
4181 lex_untranslated_string kludge. */
4182 parser->lex_untranslated_string = true;
4183 c_parser_consume_token (parser);
4184 matching_parens parens;
4185 if (!parens.require_open (parser))
4187 parser->lex_untranslated_string = false;
4188 return NULL_TREE;
4190 str = c_parser_asm_string_literal (parser);
4191 parser->lex_untranslated_string = false;
4192 if (!parens.require_close (parser))
4194 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4195 return NULL_TREE;
4197 return str;
4200 static tree
4201 c_parser_attribute_any_word (c_parser *parser)
4203 tree attr_name = NULL_TREE;
4205 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4207 /* ??? See comment above about what keywords are accepted here. */
4208 bool ok;
4209 switch (c_parser_peek_token (parser)->keyword)
4211 case RID_STATIC:
4212 case RID_UNSIGNED:
4213 case RID_LONG:
4214 case RID_CONST:
4215 case RID_EXTERN:
4216 case RID_REGISTER:
4217 case RID_TYPEDEF:
4218 case RID_SHORT:
4219 case RID_INLINE:
4220 case RID_NORETURN:
4221 case RID_VOLATILE:
4222 case RID_SIGNED:
4223 case RID_AUTO:
4224 case RID_RESTRICT:
4225 case RID_COMPLEX:
4226 case RID_THREAD:
4227 case RID_INT:
4228 case RID_CHAR:
4229 case RID_FLOAT:
4230 case RID_DOUBLE:
4231 case RID_VOID:
4232 case RID_DFLOAT32:
4233 case RID_DFLOAT64:
4234 case RID_DFLOAT128:
4235 CASE_RID_FLOATN_NX:
4236 case RID_BOOL:
4237 case RID_FRACT:
4238 case RID_ACCUM:
4239 case RID_SAT:
4240 case RID_TRANSACTION_ATOMIC:
4241 case RID_TRANSACTION_CANCEL:
4242 case RID_ATOMIC:
4243 case RID_AUTO_TYPE:
4244 case RID_INT_N_0:
4245 case RID_INT_N_1:
4246 case RID_INT_N_2:
4247 case RID_INT_N_3:
4248 ok = true;
4249 break;
4250 default:
4251 ok = false;
4252 break;
4254 if (!ok)
4255 return NULL_TREE;
4257 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4258 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4260 else if (c_parser_next_token_is (parser, CPP_NAME))
4261 attr_name = c_parser_peek_token (parser)->value;
4263 return attr_name;
4266 /* Parse (possibly empty) attributes. This is a GNU extension.
4268 attributes:
4269 empty
4270 attributes attribute
4272 attribute:
4273 __attribute__ ( ( attribute-list ) )
4275 attribute-list:
4276 attrib
4277 attribute_list , attrib
4279 attrib:
4280 empty
4281 any-word
4282 any-word ( identifier )
4283 any-word ( identifier , nonempty-expr-list )
4284 any-word ( expr-list )
4286 where the "identifier" must not be declared as a type, and
4287 "any-word" may be any identifier (including one declared as a
4288 type), a reserved word storage class specifier, type specifier or
4289 type qualifier. ??? This still leaves out most reserved keywords
4290 (following the old parser), shouldn't we include them, and why not
4291 allow identifiers declared as types to start the arguments? */
4293 static tree
4294 c_parser_attributes (c_parser *parser)
4296 tree attrs = NULL_TREE;
4297 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4299 /* ??? Follow the C++ parser rather than using the
4300 lex_untranslated_string kludge. */
4301 parser->lex_untranslated_string = true;
4302 /* Consume the `__attribute__' keyword. */
4303 c_parser_consume_token (parser);
4304 /* Look for the two `(' tokens. */
4305 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4307 parser->lex_untranslated_string = false;
4308 return attrs;
4310 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4312 parser->lex_untranslated_string = false;
4313 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4314 return attrs;
4316 /* Parse the attribute list. */
4317 while (c_parser_next_token_is (parser, CPP_COMMA)
4318 || c_parser_next_token_is (parser, CPP_NAME)
4319 || c_parser_next_token_is (parser, CPP_KEYWORD))
4321 tree attr, attr_name, attr_args;
4322 vec<tree, va_gc> *expr_list;
4323 if (c_parser_next_token_is (parser, CPP_COMMA))
4325 c_parser_consume_token (parser);
4326 continue;
4329 attr_name = c_parser_attribute_any_word (parser);
4330 if (attr_name == NULL)
4331 break;
4332 attr_name = canonicalize_attr_name (attr_name);
4333 c_parser_consume_token (parser);
4334 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4336 attr = build_tree_list (attr_name, NULL_TREE);
4337 /* Add this attribute to the list. */
4338 attrs = chainon (attrs, attr);
4339 /* If the next token isn't a comma, we're done. */
4340 if (!c_parser_next_token_is (parser, CPP_COMMA))
4341 break;
4342 continue;
4344 c_parser_consume_token (parser);
4345 /* Parse the attribute contents. If they start with an
4346 identifier which is followed by a comma or close
4347 parenthesis, then the arguments start with that
4348 identifier; otherwise they are an expression list.
4349 In objective-c the identifier may be a classname. */
4350 if (c_parser_next_token_is (parser, CPP_NAME)
4351 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4352 || (c_dialect_objc ()
4353 && c_parser_peek_token (parser)->id_kind
4354 == C_ID_CLASSNAME))
4355 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4356 || (c_parser_peek_2nd_token (parser)->type
4357 == CPP_CLOSE_PAREN))
4358 && (attribute_takes_identifier_p (attr_name)
4359 || (c_dialect_objc ()
4360 && c_parser_peek_token (parser)->id_kind
4361 == C_ID_CLASSNAME)))
4363 tree arg1 = c_parser_peek_token (parser)->value;
4364 c_parser_consume_token (parser);
4365 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4366 attr_args = build_tree_list (NULL_TREE, arg1);
4367 else
4369 tree tree_list;
4370 c_parser_consume_token (parser);
4371 expr_list = c_parser_expr_list (parser, false, true,
4372 NULL, NULL, NULL, NULL);
4373 tree_list = build_tree_list_vec (expr_list);
4374 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4375 release_tree_vector (expr_list);
4378 else
4380 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4381 attr_args = NULL_TREE;
4382 else
4384 expr_list = c_parser_expr_list (parser, false, true,
4385 NULL, NULL, NULL, NULL);
4386 attr_args = build_tree_list_vec (expr_list);
4387 release_tree_vector (expr_list);
4391 attr = build_tree_list (attr_name, attr_args);
4392 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4393 c_parser_consume_token (parser);
4394 else
4396 parser->lex_untranslated_string = false;
4397 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4398 "expected %<)%>");
4399 return attrs;
4401 /* Add this attribute to the list. */
4402 attrs = chainon (attrs, attr);
4403 /* If the next token isn't a comma, we're done. */
4404 if (!c_parser_next_token_is (parser, CPP_COMMA))
4405 break;
4407 /* Look for the two `)' tokens. */
4408 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4409 c_parser_consume_token (parser);
4410 else
4412 parser->lex_untranslated_string = false;
4413 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4414 "expected %<)%>");
4415 return attrs;
4417 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4418 c_parser_consume_token (parser);
4419 else
4421 parser->lex_untranslated_string = false;
4422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4423 "expected %<)%>");
4424 return attrs;
4426 parser->lex_untranslated_string = false;
4429 return attrs;
4432 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4433 says whether alignment specifiers are OK (only in cases that might
4434 be the type name of a compound literal).
4436 type-name:
4437 specifier-qualifier-list abstract-declarator[opt]
4440 struct c_type_name *
4441 c_parser_type_name (c_parser *parser, bool alignas_ok)
4443 struct c_declspecs *specs = build_null_declspecs ();
4444 struct c_declarator *declarator;
4445 struct c_type_name *ret;
4446 bool dummy = false;
4447 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4448 cla_prefer_type);
4449 if (!specs->declspecs_seen_p)
4451 c_parser_error (parser, "expected specifier-qualifier-list");
4452 return NULL;
4454 if (specs->type != error_mark_node)
4456 pending_xref_error ();
4457 finish_declspecs (specs);
4459 declarator = c_parser_declarator (parser,
4460 specs->typespec_kind != ctsk_none,
4461 C_DTR_ABSTRACT, &dummy);
4462 if (declarator == NULL)
4463 return NULL;
4464 ret = XOBNEW (&parser_obstack, struct c_type_name);
4465 ret->specs = specs;
4466 ret->declarator = declarator;
4467 return ret;
4470 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4472 initializer:
4473 assignment-expression
4474 { initializer-list }
4475 { initializer-list , }
4477 initializer-list:
4478 designation[opt] initializer
4479 initializer-list , designation[opt] initializer
4481 designation:
4482 designator-list =
4484 designator-list:
4485 designator
4486 designator-list designator
4488 designator:
4489 array-designator
4490 . identifier
4492 array-designator:
4493 [ constant-expression ]
4495 GNU extensions:
4497 initializer:
4500 designation:
4501 array-designator
4502 identifier :
4504 array-designator:
4505 [ constant-expression ... constant-expression ]
4507 Any expression without commas is accepted in the syntax for the
4508 constant-expressions, with non-constant expressions rejected later.
4510 This function is only used for top-level initializers; for nested
4511 ones, see c_parser_initval. */
4513 static struct c_expr
4514 c_parser_initializer (c_parser *parser)
4516 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4517 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4518 else
4520 struct c_expr ret;
4521 location_t loc = c_parser_peek_token (parser)->location;
4522 ret = c_parser_expr_no_commas (parser, NULL);
4523 if (TREE_CODE (ret.value) != STRING_CST
4524 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4525 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4526 return ret;
4530 /* The location of the last comma within the current initializer list,
4531 or UNKNOWN_LOCATION if not within one. */
4533 location_t last_init_list_comma;
4535 /* Parse a braced initializer list. TYPE is the type specified for a
4536 compound literal, and NULL_TREE for other initializers and for
4537 nested braced lists. NESTED_P is true for nested braced lists,
4538 false for the list of a compound literal or the list that is the
4539 top-level initializer in a declaration. */
4541 static struct c_expr
4542 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4543 struct obstack *outer_obstack)
4545 struct c_expr ret;
4546 struct obstack braced_init_obstack;
4547 location_t brace_loc = c_parser_peek_token (parser)->location;
4548 gcc_obstack_init (&braced_init_obstack);
4549 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4550 matching_braces braces;
4551 braces.consume_open (parser);
4552 if (nested_p)
4554 finish_implicit_inits (brace_loc, outer_obstack);
4555 push_init_level (brace_loc, 0, &braced_init_obstack);
4557 else
4558 really_start_incremental_init (type);
4559 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4561 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4563 else
4565 /* Parse a non-empty initializer list, possibly with a trailing
4566 comma. */
4567 while (true)
4569 c_parser_initelt (parser, &braced_init_obstack);
4570 if (parser->error)
4571 break;
4572 if (c_parser_next_token_is (parser, CPP_COMMA))
4574 last_init_list_comma = c_parser_peek_token (parser)->location;
4575 c_parser_consume_token (parser);
4577 else
4578 break;
4579 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4580 break;
4583 c_token *next_tok = c_parser_peek_token (parser);
4584 if (next_tok->type != CPP_CLOSE_BRACE)
4586 ret.set_error ();
4587 ret.original_code = ERROR_MARK;
4588 ret.original_type = NULL;
4589 braces.skip_until_found_close (parser);
4590 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4591 obstack_free (&braced_init_obstack, NULL);
4592 return ret;
4594 location_t close_loc = next_tok->location;
4595 c_parser_consume_token (parser);
4596 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4597 obstack_free (&braced_init_obstack, NULL);
4598 set_c_expr_source_range (&ret, brace_loc, close_loc);
4599 return ret;
4602 /* Parse a nested initializer, including designators. */
4604 static void
4605 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4607 /* Parse any designator or designator list. A single array
4608 designator may have the subsequent "=" omitted in GNU C, but a
4609 longer list or a structure member designator may not. */
4610 if (c_parser_next_token_is (parser, CPP_NAME)
4611 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4613 /* Old-style structure member designator. */
4614 set_init_label (c_parser_peek_token (parser)->location,
4615 c_parser_peek_token (parser)->value,
4616 c_parser_peek_token (parser)->location,
4617 braced_init_obstack);
4618 /* Use the colon as the error location. */
4619 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4620 "obsolete use of designated initializer with %<:%>");
4621 c_parser_consume_token (parser);
4622 c_parser_consume_token (parser);
4624 else
4626 /* des_seen is 0 if there have been no designators, 1 if there
4627 has been a single array designator and 2 otherwise. */
4628 int des_seen = 0;
4629 /* Location of a designator. */
4630 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4631 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4632 || c_parser_next_token_is (parser, CPP_DOT))
4634 int des_prev = des_seen;
4635 if (!des_seen)
4636 des_loc = c_parser_peek_token (parser)->location;
4637 if (des_seen < 2)
4638 des_seen++;
4639 if (c_parser_next_token_is (parser, CPP_DOT))
4641 des_seen = 2;
4642 c_parser_consume_token (parser);
4643 if (c_parser_next_token_is (parser, CPP_NAME))
4645 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4646 c_parser_peek_token (parser)->location,
4647 braced_init_obstack);
4648 c_parser_consume_token (parser);
4650 else
4652 struct c_expr init;
4653 init.set_error ();
4654 init.original_code = ERROR_MARK;
4655 init.original_type = NULL;
4656 c_parser_error (parser, "expected identifier");
4657 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4658 process_init_element (input_location, init, false,
4659 braced_init_obstack);
4660 return;
4663 else
4665 tree first, second;
4666 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4667 location_t array_index_loc = UNKNOWN_LOCATION;
4668 /* ??? Following the old parser, [ objc-receiver
4669 objc-message-args ] is accepted as an initializer,
4670 being distinguished from a designator by what follows
4671 the first assignment expression inside the square
4672 brackets, but after a first array designator a
4673 subsequent square bracket is for Objective-C taken to
4674 start an expression, using the obsolete form of
4675 designated initializer without '=', rather than
4676 possibly being a second level of designation: in LALR
4677 terms, the '[' is shifted rather than reducing
4678 designator to designator-list. */
4679 if (des_prev == 1 && c_dialect_objc ())
4681 des_seen = des_prev;
4682 break;
4684 if (des_prev == 0 && c_dialect_objc ())
4686 /* This might be an array designator or an
4687 Objective-C message expression. If the former,
4688 continue parsing here; if the latter, parse the
4689 remainder of the initializer given the starting
4690 primary-expression. ??? It might make sense to
4691 distinguish when des_prev == 1 as well; see
4692 previous comment. */
4693 tree rec, args;
4694 struct c_expr mexpr;
4695 c_parser_consume_token (parser);
4696 if (c_parser_peek_token (parser)->type == CPP_NAME
4697 && ((c_parser_peek_token (parser)->id_kind
4698 == C_ID_TYPENAME)
4699 || (c_parser_peek_token (parser)->id_kind
4700 == C_ID_CLASSNAME)))
4702 /* Type name receiver. */
4703 tree id = c_parser_peek_token (parser)->value;
4704 c_parser_consume_token (parser);
4705 rec = objc_get_class_reference (id);
4706 goto parse_message_args;
4708 first = c_parser_expr_no_commas (parser, NULL).value;
4709 mark_exp_read (first);
4710 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4711 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4712 goto array_desig_after_first;
4713 /* Expression receiver. So far only one part
4714 without commas has been parsed; there might be
4715 more of the expression. */
4716 rec = first;
4717 while (c_parser_next_token_is (parser, CPP_COMMA))
4719 struct c_expr next;
4720 location_t comma_loc, exp_loc;
4721 comma_loc = c_parser_peek_token (parser)->location;
4722 c_parser_consume_token (parser);
4723 exp_loc = c_parser_peek_token (parser)->location;
4724 next = c_parser_expr_no_commas (parser, NULL);
4725 next = convert_lvalue_to_rvalue (exp_loc, next,
4726 true, true);
4727 rec = build_compound_expr (comma_loc, rec, next.value);
4729 parse_message_args:
4730 /* Now parse the objc-message-args. */
4731 args = c_parser_objc_message_args (parser);
4732 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4733 "expected %<]%>");
4734 mexpr.value
4735 = objc_build_message_expr (rec, args);
4736 mexpr.original_code = ERROR_MARK;
4737 mexpr.original_type = NULL;
4738 /* Now parse and process the remainder of the
4739 initializer, starting with this message
4740 expression as a primary-expression. */
4741 c_parser_initval (parser, &mexpr, braced_init_obstack);
4742 return;
4744 c_parser_consume_token (parser);
4745 array_index_loc = c_parser_peek_token (parser)->location;
4746 first = c_parser_expr_no_commas (parser, NULL).value;
4747 mark_exp_read (first);
4748 array_desig_after_first:
4749 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4751 ellipsis_loc = c_parser_peek_token (parser)->location;
4752 c_parser_consume_token (parser);
4753 second = c_parser_expr_no_commas (parser, NULL).value;
4754 mark_exp_read (second);
4756 else
4757 second = NULL_TREE;
4758 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4760 c_parser_consume_token (parser);
4761 set_init_index (array_index_loc, first, second,
4762 braced_init_obstack);
4763 if (second)
4764 pedwarn (ellipsis_loc, OPT_Wpedantic,
4765 "ISO C forbids specifying range of elements to initialize");
4767 else
4768 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4769 "expected %<]%>");
4772 if (des_seen >= 1)
4774 if (c_parser_next_token_is (parser, CPP_EQ))
4776 pedwarn_c90 (des_loc, OPT_Wpedantic,
4777 "ISO C90 forbids specifying subobject "
4778 "to initialize");
4779 c_parser_consume_token (parser);
4781 else
4783 if (des_seen == 1)
4784 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4785 "obsolete use of designated initializer without %<=%>");
4786 else
4788 struct c_expr init;
4789 init.set_error ();
4790 init.original_code = ERROR_MARK;
4791 init.original_type = NULL;
4792 c_parser_error (parser, "expected %<=%>");
4793 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4794 process_init_element (input_location, init, false,
4795 braced_init_obstack);
4796 return;
4801 c_parser_initval (parser, NULL, braced_init_obstack);
4804 /* Parse a nested initializer; as c_parser_initializer but parses
4805 initializers within braced lists, after any designators have been
4806 applied. If AFTER is not NULL then it is an Objective-C message
4807 expression which is the primary-expression starting the
4808 initializer. */
4810 static void
4811 c_parser_initval (c_parser *parser, struct c_expr *after,
4812 struct obstack * braced_init_obstack)
4814 struct c_expr init;
4815 gcc_assert (!after || c_dialect_objc ());
4816 location_t loc = c_parser_peek_token (parser)->location;
4818 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4819 init = c_parser_braced_init (parser, NULL_TREE, true,
4820 braced_init_obstack);
4821 else
4823 init = c_parser_expr_no_commas (parser, after);
4824 if (init.value != NULL_TREE
4825 && TREE_CODE (init.value) != STRING_CST
4826 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4827 init = convert_lvalue_to_rvalue (loc, init, true, true);
4829 process_init_element (loc, init, false, braced_init_obstack);
4832 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4833 C99 6.8.2, C11 6.8.2).
4835 compound-statement:
4836 { block-item-list[opt] }
4837 { label-declarations block-item-list }
4839 block-item-list:
4840 block-item
4841 block-item-list block-item
4843 block-item:
4844 nested-declaration
4845 statement
4847 nested-declaration:
4848 declaration
4850 GNU extensions:
4852 compound-statement:
4853 { label-declarations block-item-list }
4855 nested-declaration:
4856 __extension__ nested-declaration
4857 nested-function-definition
4859 label-declarations:
4860 label-declaration
4861 label-declarations label-declaration
4863 label-declaration:
4864 __label__ identifier-list ;
4866 Allowing the mixing of declarations and code is new in C99. The
4867 GNU syntax also permits (not shown above) labels at the end of
4868 compound statements, which yield an error. We don't allow labels
4869 on declarations; this might seem like a natural extension, but
4870 there would be a conflict between attributes on the label and
4871 prefix attributes on the declaration. ??? The syntax follows the
4872 old parser in requiring something after label declarations.
4873 Although they are erroneous if the labels declared aren't defined,
4874 is it useful for the syntax to be this way?
4876 OpenACC:
4878 block-item:
4879 openacc-directive
4881 openacc-directive:
4882 update-directive
4884 OpenMP:
4886 block-item:
4887 openmp-directive
4889 openmp-directive:
4890 barrier-directive
4891 flush-directive
4892 taskwait-directive
4893 taskyield-directive
4894 cancel-directive
4895 cancellation-point-directive */
4897 static tree
4898 c_parser_compound_statement (c_parser *parser)
4900 tree stmt;
4901 location_t brace_loc;
4902 brace_loc = c_parser_peek_token (parser)->location;
4903 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4905 /* Ensure a scope is entered and left anyway to avoid confusion
4906 if we have just prepared to enter a function body. */
4907 stmt = c_begin_compound_stmt (true);
4908 c_end_compound_stmt (brace_loc, stmt, true);
4909 return error_mark_node;
4911 stmt = c_begin_compound_stmt (true);
4912 c_parser_compound_statement_nostart (parser);
4914 return c_end_compound_stmt (brace_loc, stmt, true);
4917 /* Parse a compound statement except for the opening brace. This is
4918 used for parsing both compound statements and statement expressions
4919 (which follow different paths to handling the opening). */
4921 static void
4922 c_parser_compound_statement_nostart (c_parser *parser)
4924 bool last_stmt = false;
4925 bool last_label = false;
4926 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4927 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4928 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4930 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4931 c_parser_consume_token (parser);
4932 return;
4934 mark_valid_location_for_stdc_pragma (true);
4935 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4937 /* Read zero or more forward-declarations for labels that nested
4938 functions can jump to. */
4939 mark_valid_location_for_stdc_pragma (false);
4940 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4942 label_loc = c_parser_peek_token (parser)->location;
4943 c_parser_consume_token (parser);
4944 /* Any identifiers, including those declared as type names,
4945 are OK here. */
4946 while (true)
4948 tree label;
4949 if (c_parser_next_token_is_not (parser, CPP_NAME))
4951 c_parser_error (parser, "expected identifier");
4952 break;
4954 label
4955 = declare_label (c_parser_peek_token (parser)->value);
4956 C_DECLARED_LABEL_FLAG (label) = 1;
4957 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4958 c_parser_consume_token (parser);
4959 if (c_parser_next_token_is (parser, CPP_COMMA))
4960 c_parser_consume_token (parser);
4961 else
4962 break;
4964 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4966 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4968 /* We must now have at least one statement, label or declaration. */
4969 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4971 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4972 c_parser_error (parser, "expected declaration or statement");
4973 c_parser_consume_token (parser);
4974 return;
4976 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4978 location_t loc = c_parser_peek_token (parser)->location;
4979 loc = expansion_point_location_if_in_system_header (loc);
4980 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4981 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4982 || (c_parser_next_token_is (parser, CPP_NAME)
4983 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4985 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4986 label_loc = c_parser_peek_2nd_token (parser)->location;
4987 else
4988 label_loc = c_parser_peek_token (parser)->location;
4989 last_label = true;
4990 last_stmt = false;
4991 mark_valid_location_for_stdc_pragma (false);
4992 c_parser_label (parser);
4994 else if (!last_label
4995 && c_parser_next_tokens_start_declaration (parser))
4997 last_label = false;
4998 mark_valid_location_for_stdc_pragma (false);
4999 bool fallthru_attr_p = false;
5000 c_parser_declaration_or_fndef (parser, true, true, true, true,
5001 true, NULL, vNULL, NULL,
5002 &fallthru_attr_p);
5003 if (last_stmt && !fallthru_attr_p)
5004 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5005 "ISO C90 forbids mixed declarations and code");
5006 last_stmt = fallthru_attr_p;
5008 else if (!last_label
5009 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5011 /* __extension__ can start a declaration, but is also an
5012 unary operator that can start an expression. Consume all
5013 but the last of a possible series of __extension__ to
5014 determine which. */
5015 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5016 && (c_parser_peek_2nd_token (parser)->keyword
5017 == RID_EXTENSION))
5018 c_parser_consume_token (parser);
5019 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5021 int ext;
5022 ext = disable_extension_diagnostics ();
5023 c_parser_consume_token (parser);
5024 last_label = false;
5025 mark_valid_location_for_stdc_pragma (false);
5026 c_parser_declaration_or_fndef (parser, true, true, true, true,
5027 true, NULL, vNULL);
5028 /* Following the old parser, __extension__ does not
5029 disable this diagnostic. */
5030 restore_extension_diagnostics (ext);
5031 if (last_stmt)
5032 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5033 "ISO C90 forbids mixed declarations and code");
5034 last_stmt = false;
5036 else
5037 goto statement;
5039 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5041 /* External pragmas, and some omp pragmas, are not associated
5042 with regular c code, and so are not to be considered statements
5043 syntactically. This ensures that the user doesn't put them
5044 places that would turn into syntax errors if the directive
5045 were ignored. */
5046 if (c_parser_pragma (parser,
5047 last_label ? pragma_stmt : pragma_compound,
5048 NULL))
5049 last_label = false, last_stmt = true;
5051 else if (c_parser_next_token_is (parser, CPP_EOF))
5053 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5054 c_parser_error (parser, "expected declaration or statement");
5055 return;
5057 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5059 if (parser->in_if_block)
5061 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5062 error_at (loc, "expected %<}%> before %<else%>");
5063 return;
5065 else
5067 error_at (loc, "%<else%> without a previous %<if%>");
5068 c_parser_consume_token (parser);
5069 continue;
5072 else
5074 statement:
5075 last_label = false;
5076 last_stmt = true;
5077 mark_valid_location_for_stdc_pragma (false);
5078 c_parser_statement_after_labels (parser, NULL);
5081 parser->error = false;
5083 if (last_label)
5084 error_at (label_loc, "label at end of compound statement");
5085 c_parser_consume_token (parser);
5086 /* Restore the value we started with. */
5087 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5090 /* Parse all consecutive labels. */
5092 static void
5093 c_parser_all_labels (c_parser *parser)
5095 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5096 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5097 || (c_parser_next_token_is (parser, CPP_NAME)
5098 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5099 c_parser_label (parser);
5102 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5104 label:
5105 identifier : attributes[opt]
5106 case constant-expression :
5107 default :
5109 GNU extensions:
5111 label:
5112 case constant-expression ... constant-expression :
5114 The use of attributes on labels is a GNU extension. The syntax in
5115 GNU C accepts any expressions without commas, non-constant
5116 expressions being rejected later. */
5118 static void
5119 c_parser_label (c_parser *parser)
5121 location_t loc1 = c_parser_peek_token (parser)->location;
5122 tree label = NULL_TREE;
5124 /* Remember whether this case or a user-defined label is allowed to fall
5125 through to. */
5126 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5128 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5130 tree exp1, exp2;
5131 c_parser_consume_token (parser);
5132 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5133 if (c_parser_next_token_is (parser, CPP_COLON))
5135 c_parser_consume_token (parser);
5136 label = do_case (loc1, exp1, NULL_TREE);
5138 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5140 c_parser_consume_token (parser);
5141 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5142 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5143 label = do_case (loc1, exp1, exp2);
5145 else
5146 c_parser_error (parser, "expected %<:%> or %<...%>");
5148 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5150 c_parser_consume_token (parser);
5151 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5152 label = do_case (loc1, NULL_TREE, NULL_TREE);
5154 else
5156 tree name = c_parser_peek_token (parser)->value;
5157 tree tlab;
5158 tree attrs;
5159 location_t loc2 = c_parser_peek_token (parser)->location;
5160 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5161 c_parser_consume_token (parser);
5162 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5163 c_parser_consume_token (parser);
5164 attrs = c_parser_attributes (parser);
5165 tlab = define_label (loc2, name);
5166 if (tlab)
5168 decl_attributes (&tlab, attrs, 0);
5169 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5172 if (label)
5174 if (TREE_CODE (label) == LABEL_EXPR)
5175 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5176 else
5177 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5179 /* Allow '__attribute__((fallthrough));'. */
5180 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5182 location_t loc = c_parser_peek_token (parser)->location;
5183 tree attrs = c_parser_attributes (parser);
5184 if (attribute_fallthrough_p (attrs))
5186 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5188 tree fn = build_call_expr_internal_loc (loc,
5189 IFN_FALLTHROUGH,
5190 void_type_node, 0);
5191 add_stmt (fn);
5193 else
5194 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5195 "not followed by %<;%>");
5197 else if (attrs != NULL_TREE)
5198 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5199 " can be applied to a null statement");
5201 if (c_parser_next_tokens_start_declaration (parser))
5203 error_at (c_parser_peek_token (parser)->location,
5204 "a label can only be part of a statement and "
5205 "a declaration is not a statement");
5206 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5207 /*static_assert_ok*/ true,
5208 /*empty_ok*/ true, /*nested*/ true,
5209 /*start_attr_ok*/ true, NULL,
5210 vNULL);
5215 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5217 statement:
5218 labeled-statement
5219 compound-statement
5220 expression-statement
5221 selection-statement
5222 iteration-statement
5223 jump-statement
5225 labeled-statement:
5226 label statement
5228 expression-statement:
5229 expression[opt] ;
5231 selection-statement:
5232 if-statement
5233 switch-statement
5235 iteration-statement:
5236 while-statement
5237 do-statement
5238 for-statement
5240 jump-statement:
5241 goto identifier ;
5242 continue ;
5243 break ;
5244 return expression[opt] ;
5246 GNU extensions:
5248 statement:
5249 asm-statement
5251 jump-statement:
5252 goto * expression ;
5254 expression-statement:
5255 attributes ;
5257 Objective-C:
5259 statement:
5260 objc-throw-statement
5261 objc-try-catch-statement
5262 objc-synchronized-statement
5264 objc-throw-statement:
5265 @throw expression ;
5266 @throw ;
5268 OpenACC:
5270 statement:
5271 openacc-construct
5273 openacc-construct:
5274 parallel-construct
5275 kernels-construct
5276 data-construct
5277 loop-construct
5279 parallel-construct:
5280 parallel-directive structured-block
5282 kernels-construct:
5283 kernels-directive structured-block
5285 data-construct:
5286 data-directive structured-block
5288 loop-construct:
5289 loop-directive structured-block
5291 OpenMP:
5293 statement:
5294 openmp-construct
5296 openmp-construct:
5297 parallel-construct
5298 for-construct
5299 simd-construct
5300 for-simd-construct
5301 sections-construct
5302 single-construct
5303 parallel-for-construct
5304 parallel-for-simd-construct
5305 parallel-sections-construct
5306 master-construct
5307 critical-construct
5308 atomic-construct
5309 ordered-construct
5311 parallel-construct:
5312 parallel-directive structured-block
5314 for-construct:
5315 for-directive iteration-statement
5317 simd-construct:
5318 simd-directive iteration-statements
5320 for-simd-construct:
5321 for-simd-directive iteration-statements
5323 sections-construct:
5324 sections-directive section-scope
5326 single-construct:
5327 single-directive structured-block
5329 parallel-for-construct:
5330 parallel-for-directive iteration-statement
5332 parallel-for-simd-construct:
5333 parallel-for-simd-directive iteration-statement
5335 parallel-sections-construct:
5336 parallel-sections-directive section-scope
5338 master-construct:
5339 master-directive structured-block
5341 critical-construct:
5342 critical-directive structured-block
5344 atomic-construct:
5345 atomic-directive expression-statement
5347 ordered-construct:
5348 ordered-directive structured-block
5350 Transactional Memory:
5352 statement:
5353 transaction-statement
5354 transaction-cancel-statement
5356 IF_P is used to track whether there's a (possibly labeled) if statement
5357 which is not enclosed in braces and has an else clause. This is used to
5358 implement -Wparentheses. */
5360 static void
5361 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5363 c_parser_all_labels (parser);
5364 if (loc_after_labels)
5365 *loc_after_labels = c_parser_peek_token (parser)->location;
5366 c_parser_statement_after_labels (parser, if_p, NULL);
5369 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5370 of if-else-if conditions.
5372 IF_P is used to track whether there's a (possibly labeled) if statement
5373 which is not enclosed in braces and has an else clause. This is used to
5374 implement -Wparentheses. */
5376 static void
5377 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5378 vec<tree> *chain)
5380 location_t loc = c_parser_peek_token (parser)->location;
5381 tree stmt = NULL_TREE;
5382 bool in_if_block = parser->in_if_block;
5383 parser->in_if_block = false;
5384 if (if_p != NULL)
5385 *if_p = false;
5387 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5388 add_debug_begin_stmt (loc);
5390 switch (c_parser_peek_token (parser)->type)
5392 case CPP_OPEN_BRACE:
5393 add_stmt (c_parser_compound_statement (parser));
5394 break;
5395 case CPP_KEYWORD:
5396 switch (c_parser_peek_token (parser)->keyword)
5398 case RID_IF:
5399 c_parser_if_statement (parser, if_p, chain);
5400 break;
5401 case RID_SWITCH:
5402 c_parser_switch_statement (parser, if_p);
5403 break;
5404 case RID_WHILE:
5405 c_parser_while_statement (parser, false, 0, if_p);
5406 break;
5407 case RID_DO:
5408 c_parser_do_statement (parser, 0, false);
5409 break;
5410 case RID_FOR:
5411 c_parser_for_statement (parser, false, 0, if_p);
5412 break;
5413 case RID_GOTO:
5414 c_parser_consume_token (parser);
5415 if (c_parser_next_token_is (parser, CPP_NAME))
5417 stmt = c_finish_goto_label (loc,
5418 c_parser_peek_token (parser)->value);
5419 c_parser_consume_token (parser);
5421 else if (c_parser_next_token_is (parser, CPP_MULT))
5423 struct c_expr val;
5425 c_parser_consume_token (parser);
5426 val = c_parser_expression (parser);
5427 val = convert_lvalue_to_rvalue (loc, val, false, true);
5428 stmt = c_finish_goto_ptr (loc, val.value);
5430 else
5431 c_parser_error (parser, "expected identifier or %<*%>");
5432 goto expect_semicolon;
5433 case RID_CONTINUE:
5434 c_parser_consume_token (parser);
5435 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5436 goto expect_semicolon;
5437 case RID_BREAK:
5438 c_parser_consume_token (parser);
5439 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5440 goto expect_semicolon;
5441 case RID_RETURN:
5442 c_parser_consume_token (parser);
5443 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5445 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5446 c_parser_consume_token (parser);
5448 else
5450 location_t xloc = c_parser_peek_token (parser)->location;
5451 struct c_expr expr = c_parser_expression_conv (parser);
5452 mark_exp_read (expr.value);
5453 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5454 expr.value, expr.original_type);
5455 goto expect_semicolon;
5457 break;
5458 case RID_ASM:
5459 stmt = c_parser_asm_statement (parser);
5460 break;
5461 case RID_TRANSACTION_ATOMIC:
5462 case RID_TRANSACTION_RELAXED:
5463 stmt = c_parser_transaction (parser,
5464 c_parser_peek_token (parser)->keyword);
5465 break;
5466 case RID_TRANSACTION_CANCEL:
5467 stmt = c_parser_transaction_cancel (parser);
5468 goto expect_semicolon;
5469 case RID_AT_THROW:
5470 gcc_assert (c_dialect_objc ());
5471 c_parser_consume_token (parser);
5472 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5474 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5475 c_parser_consume_token (parser);
5477 else
5479 struct c_expr expr = c_parser_expression (parser);
5480 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5481 expr.value = c_fully_fold (expr.value, false, NULL);
5482 stmt = objc_build_throw_stmt (loc, expr.value);
5483 goto expect_semicolon;
5485 break;
5486 case RID_AT_TRY:
5487 gcc_assert (c_dialect_objc ());
5488 c_parser_objc_try_catch_finally_statement (parser);
5489 break;
5490 case RID_AT_SYNCHRONIZED:
5491 gcc_assert (c_dialect_objc ());
5492 c_parser_objc_synchronized_statement (parser);
5493 break;
5494 case RID_ATTRIBUTE:
5496 /* Allow '__attribute__((fallthrough));'. */
5497 tree attrs = c_parser_attributes (parser);
5498 if (attribute_fallthrough_p (attrs))
5500 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5502 tree fn = build_call_expr_internal_loc (loc,
5503 IFN_FALLTHROUGH,
5504 void_type_node, 0);
5505 add_stmt (fn);
5506 /* Eat the ';'. */
5507 c_parser_consume_token (parser);
5509 else
5510 warning_at (loc, OPT_Wattributes,
5511 "%<fallthrough%> attribute not followed "
5512 "by %<;%>");
5514 else if (attrs != NULL_TREE)
5515 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5516 " can be applied to a null statement");
5517 break;
5519 default:
5520 goto expr_stmt;
5522 break;
5523 case CPP_SEMICOLON:
5524 c_parser_consume_token (parser);
5525 break;
5526 case CPP_CLOSE_PAREN:
5527 case CPP_CLOSE_SQUARE:
5528 /* Avoid infinite loop in error recovery:
5529 c_parser_skip_until_found stops at a closing nesting
5530 delimiter without consuming it, but here we need to consume
5531 it to proceed further. */
5532 c_parser_error (parser, "expected statement");
5533 c_parser_consume_token (parser);
5534 break;
5535 case CPP_PRAGMA:
5536 c_parser_pragma (parser, pragma_stmt, if_p);
5537 break;
5538 default:
5539 expr_stmt:
5540 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5541 expect_semicolon:
5542 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5543 break;
5545 /* Two cases cannot and do not have line numbers associated: If stmt
5546 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5547 cannot hold line numbers. But that's OK because the statement
5548 will either be changed to a MODIFY_EXPR during gimplification of
5549 the statement expr, or discarded. If stmt was compound, but
5550 without new variables, we will have skipped the creation of a
5551 BIND and will have a bare STATEMENT_LIST. But that's OK because
5552 (recursively) all of the component statements should already have
5553 line numbers assigned. ??? Can we discard no-op statements
5554 earlier? */
5555 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5556 protected_set_expr_location (stmt, loc);
5558 parser->in_if_block = in_if_block;
5561 /* Parse the condition from an if, do, while or for statements. */
5563 static tree
5564 c_parser_condition (c_parser *parser)
5566 location_t loc = c_parser_peek_token (parser)->location;
5567 tree cond;
5568 cond = c_parser_expression_conv (parser).value;
5569 cond = c_objc_common_truthvalue_conversion (loc, cond);
5570 cond = c_fully_fold (cond, false, NULL);
5571 if (warn_sequence_point)
5572 verify_sequence_points (cond);
5573 return cond;
5576 /* Parse a parenthesized condition from an if, do or while statement.
5578 condition:
5579 ( expression )
5581 static tree
5582 c_parser_paren_condition (c_parser *parser)
5584 tree cond;
5585 matching_parens parens;
5586 if (!parens.require_open (parser))
5587 return error_mark_node;
5588 cond = c_parser_condition (parser);
5589 parens.skip_until_found_close (parser);
5590 return cond;
5593 /* Parse a statement which is a block in C99.
5595 IF_P is used to track whether there's a (possibly labeled) if statement
5596 which is not enclosed in braces and has an else clause. This is used to
5597 implement -Wparentheses. */
5599 static tree
5600 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5601 location_t *loc_after_labels)
5603 tree block = c_begin_compound_stmt (flag_isoc99);
5604 location_t loc = c_parser_peek_token (parser)->location;
5605 c_parser_statement (parser, if_p, loc_after_labels);
5606 return c_end_compound_stmt (loc, block, flag_isoc99);
5609 /* Parse the body of an if statement. This is just parsing a
5610 statement but (a) it is a block in C99, (b) we track whether the
5611 body is an if statement for the sake of -Wparentheses warnings, (c)
5612 we handle an empty body specially for the sake of -Wempty-body
5613 warnings, and (d) we call parser_compound_statement directly
5614 because c_parser_statement_after_labels resets
5615 parser->in_if_block.
5617 IF_P is used to track whether there's a (possibly labeled) if statement
5618 which is not enclosed in braces and has an else clause. This is used to
5619 implement -Wparentheses. */
5621 static tree
5622 c_parser_if_body (c_parser *parser, bool *if_p,
5623 const token_indent_info &if_tinfo)
5625 tree block = c_begin_compound_stmt (flag_isoc99);
5626 location_t body_loc = c_parser_peek_token (parser)->location;
5627 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5628 token_indent_info body_tinfo
5629 = get_token_indent_info (c_parser_peek_token (parser));
5631 c_parser_all_labels (parser);
5632 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5634 location_t loc = c_parser_peek_token (parser)->location;
5635 add_stmt (build_empty_stmt (loc));
5636 c_parser_consume_token (parser);
5637 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5638 warning_at (loc, OPT_Wempty_body,
5639 "suggest braces around empty body in an %<if%> statement");
5641 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5642 add_stmt (c_parser_compound_statement (parser));
5643 else
5645 body_loc_after_labels = c_parser_peek_token (parser)->location;
5646 c_parser_statement_after_labels (parser, if_p);
5649 token_indent_info next_tinfo
5650 = get_token_indent_info (c_parser_peek_token (parser));
5651 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5652 if (body_loc_after_labels != UNKNOWN_LOCATION
5653 && next_tinfo.type != CPP_SEMICOLON)
5654 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5655 if_tinfo.location, RID_IF);
5657 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5660 /* Parse the else body of an if statement. This is just parsing a
5661 statement but (a) it is a block in C99, (b) we handle an empty body
5662 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5663 of if-else-if conditions. */
5665 static tree
5666 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5667 vec<tree> *chain)
5669 location_t body_loc = c_parser_peek_token (parser)->location;
5670 tree block = c_begin_compound_stmt (flag_isoc99);
5671 token_indent_info body_tinfo
5672 = get_token_indent_info (c_parser_peek_token (parser));
5673 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5675 c_parser_all_labels (parser);
5676 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5678 location_t loc = c_parser_peek_token (parser)->location;
5679 warning_at (loc,
5680 OPT_Wempty_body,
5681 "suggest braces around empty body in an %<else%> statement");
5682 add_stmt (build_empty_stmt (loc));
5683 c_parser_consume_token (parser);
5685 else
5687 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5688 body_loc_after_labels = c_parser_peek_token (parser)->location;
5689 c_parser_statement_after_labels (parser, NULL, chain);
5692 token_indent_info next_tinfo
5693 = get_token_indent_info (c_parser_peek_token (parser));
5694 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5695 if (body_loc_after_labels != UNKNOWN_LOCATION
5696 && next_tinfo.type != CPP_SEMICOLON)
5697 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5698 else_tinfo.location, RID_ELSE);
5700 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5703 /* We might need to reclassify any previously-lexed identifier, e.g.
5704 when we've left a for loop with an if-statement without else in the
5705 body - we might have used a wrong scope for the token. See PR67784. */
5707 static void
5708 c_parser_maybe_reclassify_token (c_parser *parser)
5710 if (c_parser_next_token_is (parser, CPP_NAME))
5712 c_token *token = c_parser_peek_token (parser);
5714 if (token->id_kind != C_ID_CLASSNAME)
5716 tree decl = lookup_name (token->value);
5718 token->id_kind = C_ID_ID;
5719 if (decl)
5721 if (TREE_CODE (decl) == TYPE_DECL)
5722 token->id_kind = C_ID_TYPENAME;
5724 else if (c_dialect_objc ())
5726 tree objc_interface_decl = objc_is_class_name (token->value);
5727 /* Objective-C class names are in the same namespace as
5728 variables and typedefs, and hence are shadowed by local
5729 declarations. */
5730 if (objc_interface_decl)
5732 token->value = objc_interface_decl;
5733 token->id_kind = C_ID_CLASSNAME;
5740 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5742 if-statement:
5743 if ( expression ) statement
5744 if ( expression ) statement else statement
5746 CHAIN is a vector of if-else-if conditions.
5747 IF_P is used to track whether there's a (possibly labeled) if statement
5748 which is not enclosed in braces and has an else clause. This is used to
5749 implement -Wparentheses. */
5751 static void
5752 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5754 tree block;
5755 location_t loc;
5756 tree cond;
5757 bool nested_if = false;
5758 tree first_body, second_body;
5759 bool in_if_block;
5761 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5762 token_indent_info if_tinfo
5763 = get_token_indent_info (c_parser_peek_token (parser));
5764 c_parser_consume_token (parser);
5765 block = c_begin_compound_stmt (flag_isoc99);
5766 loc = c_parser_peek_token (parser)->location;
5767 cond = c_parser_paren_condition (parser);
5768 in_if_block = parser->in_if_block;
5769 parser->in_if_block = true;
5770 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5771 parser->in_if_block = in_if_block;
5773 if (warn_duplicated_cond)
5774 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5776 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5778 token_indent_info else_tinfo
5779 = get_token_indent_info (c_parser_peek_token (parser));
5780 c_parser_consume_token (parser);
5781 if (warn_duplicated_cond)
5783 if (c_parser_next_token_is_keyword (parser, RID_IF)
5784 && chain == NULL)
5786 /* We've got "if (COND) else if (COND2)". Start the
5787 condition chain and add COND as the first element. */
5788 chain = new vec<tree> ();
5789 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5790 chain->safe_push (cond);
5792 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5794 /* This is if-else without subsequent if. Zap the condition
5795 chain; we would have already warned at this point. */
5796 delete chain;
5797 chain = NULL;
5800 second_body = c_parser_else_body (parser, else_tinfo, chain);
5801 /* Set IF_P to true to indicate that this if statement has an
5802 else clause. This may trigger the Wparentheses warning
5803 below when we get back up to the parent if statement. */
5804 if (if_p != NULL)
5805 *if_p = true;
5807 else
5809 second_body = NULL_TREE;
5811 /* Diagnose an ambiguous else if if-then-else is nested inside
5812 if-then. */
5813 if (nested_if)
5814 warning_at (loc, OPT_Wdangling_else,
5815 "suggest explicit braces to avoid ambiguous %<else%>");
5817 if (warn_duplicated_cond)
5819 /* This if statement does not have an else clause. We don't
5820 need the condition chain anymore. */
5821 delete chain;
5822 chain = NULL;
5825 c_finish_if_stmt (loc, cond, first_body, second_body);
5826 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5828 c_parser_maybe_reclassify_token (parser);
5831 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5833 switch-statement:
5834 switch (expression) statement
5837 static void
5838 c_parser_switch_statement (c_parser *parser, bool *if_p)
5840 struct c_expr ce;
5841 tree block, expr, body, save_break;
5842 location_t switch_loc = c_parser_peek_token (parser)->location;
5843 location_t switch_cond_loc;
5844 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5845 c_parser_consume_token (parser);
5846 block = c_begin_compound_stmt (flag_isoc99);
5847 bool explicit_cast_p = false;
5848 matching_parens parens;
5849 if (parens.require_open (parser))
5851 switch_cond_loc = c_parser_peek_token (parser)->location;
5852 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5853 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5854 explicit_cast_p = true;
5855 ce = c_parser_expression (parser);
5856 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5857 expr = ce.value;
5858 /* ??? expr has no valid location? */
5859 parens.skip_until_found_close (parser);
5861 else
5863 switch_cond_loc = UNKNOWN_LOCATION;
5864 expr = error_mark_node;
5865 ce.original_type = error_mark_node;
5867 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5868 save_break = c_break_label;
5869 c_break_label = NULL_TREE;
5870 location_t loc_after_labels;
5871 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5872 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5873 location_t next_loc = c_parser_peek_token (parser)->location;
5874 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5875 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5876 RID_SWITCH);
5877 if (c_break_label)
5879 location_t here = c_parser_peek_token (parser)->location;
5880 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5881 SET_EXPR_LOCATION (t, here);
5882 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5883 append_to_statement_list_force (t, &body);
5885 c_finish_case (body, ce.original_type);
5886 c_break_label = save_break;
5887 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5888 c_parser_maybe_reclassify_token (parser);
5891 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5893 while-statement:
5894 while (expression) statement
5896 IF_P is used to track whether there's a (possibly labeled) if statement
5897 which is not enclosed in braces and has an else clause. This is used to
5898 implement -Wparentheses. */
5900 static void
5901 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5902 bool *if_p)
5904 tree block, cond, body, save_break, save_cont;
5905 location_t loc;
5906 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5907 token_indent_info while_tinfo
5908 = get_token_indent_info (c_parser_peek_token (parser));
5909 c_parser_consume_token (parser);
5910 block = c_begin_compound_stmt (flag_isoc99);
5911 loc = c_parser_peek_token (parser)->location;
5912 cond = c_parser_paren_condition (parser);
5913 if (ivdep && cond != error_mark_node)
5914 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5915 build_int_cst (integer_type_node,
5916 annot_expr_ivdep_kind),
5917 integer_zero_node);
5918 if (unroll && cond != error_mark_node)
5919 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5920 build_int_cst (integer_type_node,
5921 annot_expr_unroll_kind),
5922 build_int_cst (integer_type_node, unroll));
5923 save_break = c_break_label;
5924 c_break_label = NULL_TREE;
5925 save_cont = c_cont_label;
5926 c_cont_label = NULL_TREE;
5928 token_indent_info body_tinfo
5929 = get_token_indent_info (c_parser_peek_token (parser));
5931 location_t loc_after_labels;
5932 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5933 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5934 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5935 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5936 c_parser_maybe_reclassify_token (parser);
5938 token_indent_info next_tinfo
5939 = get_token_indent_info (c_parser_peek_token (parser));
5940 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5942 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5943 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5944 while_tinfo.location, RID_WHILE);
5946 c_break_label = save_break;
5947 c_cont_label = save_cont;
5950 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5952 do-statement:
5953 do statement while ( expression ) ;
5956 static void
5957 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
5959 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5960 location_t loc;
5961 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5962 c_parser_consume_token (parser);
5963 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5964 warning_at (c_parser_peek_token (parser)->location,
5965 OPT_Wempty_body,
5966 "suggest braces around empty body in %<do%> statement");
5967 block = c_begin_compound_stmt (flag_isoc99);
5968 loc = c_parser_peek_token (parser)->location;
5969 save_break = c_break_label;
5970 c_break_label = NULL_TREE;
5971 save_cont = c_cont_label;
5972 c_cont_label = NULL_TREE;
5973 body = c_parser_c99_block_statement (parser, NULL);
5974 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5975 new_break = c_break_label;
5976 c_break_label = save_break;
5977 new_cont = c_cont_label;
5978 c_cont_label = save_cont;
5979 cond = c_parser_paren_condition (parser);
5980 if (ivdep && cond != error_mark_node)
5981 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5982 build_int_cst (integer_type_node,
5983 annot_expr_ivdep_kind),
5984 integer_zero_node);
5985 if (unroll && cond != error_mark_node)
5986 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5987 build_int_cst (integer_type_node,
5988 annot_expr_unroll_kind),
5989 build_int_cst (integer_type_node, unroll));
5990 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5991 c_parser_skip_to_end_of_block_or_statement (parser);
5992 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5993 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5996 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5998 for-statement:
5999 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6000 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6002 The form with a declaration is new in C99.
6004 ??? In accordance with the old parser, the declaration may be a
6005 nested function, which is then rejected in check_for_loop_decls,
6006 but does it make any sense for this to be included in the grammar?
6007 Note in particular that the nested function does not include a
6008 trailing ';', whereas the "declaration" production includes one.
6009 Also, can we reject bad declarations earlier and cheaper than
6010 check_for_loop_decls?
6012 In Objective-C, there are two additional variants:
6014 foreach-statement:
6015 for ( expression in expresssion ) statement
6016 for ( declaration in expression ) statement
6018 This is inconsistent with C, because the second variant is allowed
6019 even if c99 is not enabled.
6021 The rest of the comment documents these Objective-C foreach-statement.
6023 Here is the canonical example of the first variant:
6024 for (object in array) { do something with object }
6025 we call the first expression ("object") the "object_expression" and
6026 the second expression ("array") the "collection_expression".
6027 object_expression must be an lvalue of type "id" (a generic Objective-C
6028 object) because the loop works by assigning to object_expression the
6029 various objects from the collection_expression. collection_expression
6030 must evaluate to something of type "id" which responds to the method
6031 countByEnumeratingWithState:objects:count:.
6033 The canonical example of the second variant is:
6034 for (id object in array) { do something with object }
6035 which is completely equivalent to
6037 id object;
6038 for (object in array) { do something with object }
6040 Note that initizializing 'object' in some way (eg, "for ((object =
6041 xxx) in array) { do something with object }") is possibly
6042 technically valid, but completely pointless as 'object' will be
6043 assigned to something else as soon as the loop starts. We should
6044 most likely reject it (TODO).
6046 The beginning of the Objective-C foreach-statement looks exactly
6047 like the beginning of the for-statement, and we can tell it is a
6048 foreach-statement only because the initial declaration or
6049 expression is terminated by 'in' instead of ';'.
6051 IF_P is used to track whether there's a (possibly labeled) if statement
6052 which is not enclosed in braces and has an else clause. This is used to
6053 implement -Wparentheses. */
6055 static void
6056 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6057 bool *if_p)
6059 tree block, cond, incr, save_break, save_cont, body;
6060 /* The following are only used when parsing an ObjC foreach statement. */
6061 tree object_expression;
6062 /* Silence the bogus uninitialized warning. */
6063 tree collection_expression = NULL;
6064 location_t loc = c_parser_peek_token (parser)->location;
6065 location_t for_loc = c_parser_peek_token (parser)->location;
6066 bool is_foreach_statement = false;
6067 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6068 token_indent_info for_tinfo
6069 = get_token_indent_info (c_parser_peek_token (parser));
6070 c_parser_consume_token (parser);
6071 /* Open a compound statement in Objective-C as well, just in case this is
6072 as foreach expression. */
6073 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6074 cond = error_mark_node;
6075 incr = error_mark_node;
6076 matching_parens parens;
6077 if (parens.require_open (parser))
6079 /* Parse the initialization declaration or expression. */
6080 object_expression = error_mark_node;
6081 parser->objc_could_be_foreach_context = c_dialect_objc ();
6082 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6084 parser->objc_could_be_foreach_context = false;
6085 c_parser_consume_token (parser);
6086 c_finish_expr_stmt (loc, NULL_TREE);
6088 else if (c_parser_next_tokens_start_declaration (parser))
6090 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6091 &object_expression, vNULL);
6092 parser->objc_could_be_foreach_context = false;
6094 if (c_parser_next_token_is_keyword (parser, RID_IN))
6096 c_parser_consume_token (parser);
6097 is_foreach_statement = true;
6098 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6099 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6101 else
6102 check_for_loop_decls (for_loc, flag_isoc99);
6104 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6106 /* __extension__ can start a declaration, but is also an
6107 unary operator that can start an expression. Consume all
6108 but the last of a possible series of __extension__ to
6109 determine which. */
6110 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6111 && (c_parser_peek_2nd_token (parser)->keyword
6112 == RID_EXTENSION))
6113 c_parser_consume_token (parser);
6114 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6116 int ext;
6117 ext = disable_extension_diagnostics ();
6118 c_parser_consume_token (parser);
6119 c_parser_declaration_or_fndef (parser, true, true, true, true,
6120 true, &object_expression, vNULL);
6121 parser->objc_could_be_foreach_context = false;
6123 restore_extension_diagnostics (ext);
6124 if (c_parser_next_token_is_keyword (parser, RID_IN))
6126 c_parser_consume_token (parser);
6127 is_foreach_statement = true;
6128 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6129 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6131 else
6132 check_for_loop_decls (for_loc, flag_isoc99);
6134 else
6135 goto init_expr;
6137 else
6139 init_expr:
6141 struct c_expr ce;
6142 tree init_expression;
6143 ce = c_parser_expression (parser);
6144 init_expression = ce.value;
6145 parser->objc_could_be_foreach_context = false;
6146 if (c_parser_next_token_is_keyword (parser, RID_IN))
6148 c_parser_consume_token (parser);
6149 is_foreach_statement = true;
6150 if (! lvalue_p (init_expression))
6151 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6152 object_expression = c_fully_fold (init_expression, false, NULL);
6154 else
6156 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6157 init_expression = ce.value;
6158 c_finish_expr_stmt (loc, init_expression);
6159 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6163 /* Parse the loop condition. In the case of a foreach
6164 statement, there is no loop condition. */
6165 gcc_assert (!parser->objc_could_be_foreach_context);
6166 if (!is_foreach_statement)
6168 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6170 if (ivdep)
6172 c_parser_error (parser, "missing loop condition in loop with "
6173 "%<GCC ivdep%> pragma");
6174 cond = error_mark_node;
6176 else if (unroll)
6178 c_parser_error (parser, "missing loop condition in loop with "
6179 "%<GCC unroll%> pragma");
6180 cond = error_mark_node;
6182 else
6184 c_parser_consume_token (parser);
6185 cond = NULL_TREE;
6188 else
6190 cond = c_parser_condition (parser);
6191 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6192 "expected %<;%>");
6194 if (ivdep && cond != error_mark_node)
6195 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6196 build_int_cst (integer_type_node,
6197 annot_expr_ivdep_kind),
6198 integer_zero_node);
6199 if (unroll && cond != error_mark_node)
6200 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6201 build_int_cst (integer_type_node,
6202 annot_expr_unroll_kind),
6203 build_int_cst (integer_type_node, unroll));
6205 /* Parse the increment expression (the third expression in a
6206 for-statement). In the case of a foreach-statement, this is
6207 the expression that follows the 'in'. */
6208 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6210 if (is_foreach_statement)
6212 c_parser_error (parser, "missing collection in fast enumeration");
6213 collection_expression = error_mark_node;
6215 else
6216 incr = c_process_expr_stmt (loc, NULL_TREE);
6218 else
6220 if (is_foreach_statement)
6221 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6222 false, NULL);
6223 else
6225 struct c_expr ce = c_parser_expression (parser);
6226 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6227 incr = c_process_expr_stmt (loc, ce.value);
6230 parens.skip_until_found_close (parser);
6232 save_break = c_break_label;
6233 c_break_label = NULL_TREE;
6234 save_cont = c_cont_label;
6235 c_cont_label = NULL_TREE;
6237 token_indent_info body_tinfo
6238 = get_token_indent_info (c_parser_peek_token (parser));
6240 location_t loc_after_labels;
6241 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6242 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6244 if (is_foreach_statement)
6245 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6246 else
6247 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6248 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6249 c_parser_maybe_reclassify_token (parser);
6251 token_indent_info next_tinfo
6252 = get_token_indent_info (c_parser_peek_token (parser));
6253 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6255 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6256 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6257 for_tinfo.location, RID_FOR);
6259 c_break_label = save_break;
6260 c_cont_label = save_cont;
6263 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6264 statement with inputs, outputs, clobbers, and volatile tag
6265 allowed.
6267 asm-statement:
6268 asm type-qualifier[opt] ( asm-argument ) ;
6269 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6271 asm-argument:
6272 asm-string-literal
6273 asm-string-literal : asm-operands[opt]
6274 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6275 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6277 asm-goto-argument:
6278 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6279 : asm-goto-operands
6281 Qualifiers other than volatile are accepted in the syntax but
6282 warned for. */
6284 static tree
6285 c_parser_asm_statement (c_parser *parser)
6287 tree quals, str, outputs, inputs, clobbers, labels, ret;
6288 bool simple, is_goto;
6289 location_t asm_loc = c_parser_peek_token (parser)->location;
6290 int section, nsections;
6292 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6293 c_parser_consume_token (parser);
6294 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6296 quals = c_parser_peek_token (parser)->value;
6297 c_parser_consume_token (parser);
6299 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6300 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6302 warning_at (c_parser_peek_token (parser)->location,
6304 "%E qualifier ignored on asm",
6305 c_parser_peek_token (parser)->value);
6306 quals = NULL_TREE;
6307 c_parser_consume_token (parser);
6309 else
6310 quals = NULL_TREE;
6312 is_goto = false;
6313 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6315 c_parser_consume_token (parser);
6316 is_goto = true;
6319 /* ??? Follow the C++ parser rather than using the
6320 lex_untranslated_string kludge. */
6321 parser->lex_untranslated_string = true;
6322 ret = NULL;
6324 matching_parens parens;
6325 if (!parens.require_open (parser))
6326 goto error;
6328 str = c_parser_asm_string_literal (parser);
6329 if (str == NULL_TREE)
6330 goto error_close_paren;
6332 simple = true;
6333 outputs = NULL_TREE;
6334 inputs = NULL_TREE;
6335 clobbers = NULL_TREE;
6336 labels = NULL_TREE;
6338 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6339 goto done_asm;
6341 /* Parse each colon-delimited section of operands. */
6342 nsections = 3 + is_goto;
6343 for (section = 0; section < nsections; ++section)
6345 if (!c_parser_require (parser, CPP_COLON,
6346 is_goto
6347 ? G_("expected %<:%>")
6348 : G_("expected %<:%> or %<)%>"),
6349 UNKNOWN_LOCATION, is_goto))
6350 goto error_close_paren;
6352 /* Once past any colon, we're no longer a simple asm. */
6353 simple = false;
6355 if ((!c_parser_next_token_is (parser, CPP_COLON)
6356 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6357 || section == 3)
6358 switch (section)
6360 case 0:
6361 /* For asm goto, we don't allow output operands, but reserve
6362 the slot for a future extension that does allow them. */
6363 if (!is_goto)
6364 outputs = c_parser_asm_operands (parser);
6365 break;
6366 case 1:
6367 inputs = c_parser_asm_operands (parser);
6368 break;
6369 case 2:
6370 clobbers = c_parser_asm_clobbers (parser);
6371 break;
6372 case 3:
6373 labels = c_parser_asm_goto_operands (parser);
6374 break;
6375 default:
6376 gcc_unreachable ();
6379 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6380 goto done_asm;
6383 done_asm:
6384 if (!parens.require_close (parser))
6386 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6387 goto error;
6390 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6391 c_parser_skip_to_end_of_block_or_statement (parser);
6393 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6394 clobbers, labels, simple));
6396 error:
6397 parser->lex_untranslated_string = false;
6398 return ret;
6400 error_close_paren:
6401 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6402 goto error;
6405 /* Parse asm operands, a GNU extension.
6407 asm-operands:
6408 asm-operand
6409 asm-operands , asm-operand
6411 asm-operand:
6412 asm-string-literal ( expression )
6413 [ identifier ] asm-string-literal ( expression )
6416 static tree
6417 c_parser_asm_operands (c_parser *parser)
6419 tree list = NULL_TREE;
6420 while (true)
6422 tree name, str;
6423 struct c_expr expr;
6424 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6426 c_parser_consume_token (parser);
6427 if (c_parser_next_token_is (parser, CPP_NAME))
6429 tree id = c_parser_peek_token (parser)->value;
6430 c_parser_consume_token (parser);
6431 name = build_string (IDENTIFIER_LENGTH (id),
6432 IDENTIFIER_POINTER (id));
6434 else
6436 c_parser_error (parser, "expected identifier");
6437 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6438 return NULL_TREE;
6440 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6441 "expected %<]%>");
6443 else
6444 name = NULL_TREE;
6445 str = c_parser_asm_string_literal (parser);
6446 if (str == NULL_TREE)
6447 return NULL_TREE;
6448 parser->lex_untranslated_string = false;
6449 matching_parens parens;
6450 if (!parens.require_open (parser))
6452 parser->lex_untranslated_string = true;
6453 return NULL_TREE;
6455 expr = c_parser_expression (parser);
6456 mark_exp_read (expr.value);
6457 parser->lex_untranslated_string = true;
6458 if (!parens.require_close (parser))
6460 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6461 return NULL_TREE;
6463 list = chainon (list, build_tree_list (build_tree_list (name, str),
6464 expr.value));
6465 if (c_parser_next_token_is (parser, CPP_COMMA))
6466 c_parser_consume_token (parser);
6467 else
6468 break;
6470 return list;
6473 /* Parse asm clobbers, a GNU extension.
6475 asm-clobbers:
6476 asm-string-literal
6477 asm-clobbers , asm-string-literal
6480 static tree
6481 c_parser_asm_clobbers (c_parser *parser)
6483 tree list = NULL_TREE;
6484 while (true)
6486 tree str = c_parser_asm_string_literal (parser);
6487 if (str)
6488 list = tree_cons (NULL_TREE, str, list);
6489 else
6490 return NULL_TREE;
6491 if (c_parser_next_token_is (parser, CPP_COMMA))
6492 c_parser_consume_token (parser);
6493 else
6494 break;
6496 return list;
6499 /* Parse asm goto labels, a GNU extension.
6501 asm-goto-operands:
6502 identifier
6503 asm-goto-operands , identifier
6506 static tree
6507 c_parser_asm_goto_operands (c_parser *parser)
6509 tree list = NULL_TREE;
6510 while (true)
6512 tree name, label;
6514 if (c_parser_next_token_is (parser, CPP_NAME))
6516 c_token *tok = c_parser_peek_token (parser);
6517 name = tok->value;
6518 label = lookup_label_for_goto (tok->location, name);
6519 c_parser_consume_token (parser);
6520 TREE_USED (label) = 1;
6522 else
6524 c_parser_error (parser, "expected identifier");
6525 return NULL_TREE;
6528 name = build_string (IDENTIFIER_LENGTH (name),
6529 IDENTIFIER_POINTER (name));
6530 list = tree_cons (name, label, list);
6531 if (c_parser_next_token_is (parser, CPP_COMMA))
6532 c_parser_consume_token (parser);
6533 else
6534 return nreverse (list);
6538 /* Parse an expression other than a compound expression; that is, an
6539 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6540 AFTER is not NULL then it is an Objective-C message expression which
6541 is the primary-expression starting the expression as an initializer.
6543 assignment-expression:
6544 conditional-expression
6545 unary-expression assignment-operator assignment-expression
6547 assignment-operator: one of
6548 = *= /= %= += -= <<= >>= &= ^= |=
6550 In GNU C we accept any conditional expression on the LHS and
6551 diagnose the invalid lvalue rather than producing a syntax
6552 error. */
6554 static struct c_expr
6555 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6556 tree omp_atomic_lhs)
6558 struct c_expr lhs, rhs, ret;
6559 enum tree_code code;
6560 location_t op_location, exp_location;
6561 gcc_assert (!after || c_dialect_objc ());
6562 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6563 op_location = c_parser_peek_token (parser)->location;
6564 switch (c_parser_peek_token (parser)->type)
6566 case CPP_EQ:
6567 code = NOP_EXPR;
6568 break;
6569 case CPP_MULT_EQ:
6570 code = MULT_EXPR;
6571 break;
6572 case CPP_DIV_EQ:
6573 code = TRUNC_DIV_EXPR;
6574 break;
6575 case CPP_MOD_EQ:
6576 code = TRUNC_MOD_EXPR;
6577 break;
6578 case CPP_PLUS_EQ:
6579 code = PLUS_EXPR;
6580 break;
6581 case CPP_MINUS_EQ:
6582 code = MINUS_EXPR;
6583 break;
6584 case CPP_LSHIFT_EQ:
6585 code = LSHIFT_EXPR;
6586 break;
6587 case CPP_RSHIFT_EQ:
6588 code = RSHIFT_EXPR;
6589 break;
6590 case CPP_AND_EQ:
6591 code = BIT_AND_EXPR;
6592 break;
6593 case CPP_XOR_EQ:
6594 code = BIT_XOR_EXPR;
6595 break;
6596 case CPP_OR_EQ:
6597 code = BIT_IOR_EXPR;
6598 break;
6599 default:
6600 return lhs;
6602 c_parser_consume_token (parser);
6603 exp_location = c_parser_peek_token (parser)->location;
6604 rhs = c_parser_expr_no_commas (parser, NULL);
6605 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6607 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6608 code, exp_location, rhs.value,
6609 rhs.original_type);
6610 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6611 if (code == NOP_EXPR)
6612 ret.original_code = MODIFY_EXPR;
6613 else
6615 TREE_NO_WARNING (ret.value) = 1;
6616 ret.original_code = ERROR_MARK;
6618 ret.original_type = NULL;
6619 return ret;
6622 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6623 AFTER is not NULL then it is an Objective-C message expression which is
6624 the primary-expression starting the expression as an initializer.
6626 conditional-expression:
6627 logical-OR-expression
6628 logical-OR-expression ? expression : conditional-expression
6630 GNU extensions:
6632 conditional-expression:
6633 logical-OR-expression ? : conditional-expression
6636 static struct c_expr
6637 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6638 tree omp_atomic_lhs)
6640 struct c_expr cond, exp1, exp2, ret;
6641 location_t start, cond_loc, colon_loc;
6643 gcc_assert (!after || c_dialect_objc ());
6645 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6647 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6648 return cond;
6649 if (cond.value != error_mark_node)
6650 start = cond.get_start ();
6651 else
6652 start = UNKNOWN_LOCATION;
6653 cond_loc = c_parser_peek_token (parser)->location;
6654 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6655 c_parser_consume_token (parser);
6656 if (c_parser_next_token_is (parser, CPP_COLON))
6658 tree eptype = NULL_TREE;
6660 location_t middle_loc = c_parser_peek_token (parser)->location;
6661 pedwarn (middle_loc, OPT_Wpedantic,
6662 "ISO C forbids omitting the middle term of a ?: expression");
6663 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6665 eptype = TREE_TYPE (cond.value);
6666 cond.value = TREE_OPERAND (cond.value, 0);
6668 tree e = cond.value;
6669 while (TREE_CODE (e) == COMPOUND_EXPR)
6670 e = TREE_OPERAND (e, 1);
6671 warn_for_omitted_condop (middle_loc, e);
6672 /* Make sure first operand is calculated only once. */
6673 exp1.value = save_expr (default_conversion (cond.value));
6674 if (eptype)
6675 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6676 exp1.original_type = NULL;
6677 exp1.src_range = cond.src_range;
6678 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6679 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6681 else
6683 cond.value
6684 = c_objc_common_truthvalue_conversion
6685 (cond_loc, default_conversion (cond.value));
6686 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6687 exp1 = c_parser_expression_conv (parser);
6688 mark_exp_read (exp1.value);
6689 c_inhibit_evaluation_warnings +=
6690 ((cond.value == truthvalue_true_node)
6691 - (cond.value == truthvalue_false_node));
6694 colon_loc = c_parser_peek_token (parser)->location;
6695 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6697 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6698 ret.set_error ();
6699 ret.original_code = ERROR_MARK;
6700 ret.original_type = NULL;
6701 return ret;
6704 location_t exp2_loc = c_parser_peek_token (parser)->location;
6705 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6706 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6708 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6709 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6710 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6711 ret.value = build_conditional_expr (colon_loc, cond.value,
6712 cond.original_code == C_MAYBE_CONST_EXPR,
6713 exp1.value, exp1.original_type, loc1,
6714 exp2.value, exp2.original_type, loc2);
6715 ret.original_code = ERROR_MARK;
6716 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6717 ret.original_type = NULL;
6718 else
6720 tree t1, t2;
6722 /* If both sides are enum type, the default conversion will have
6723 made the type of the result be an integer type. We want to
6724 remember the enum types we started with. */
6725 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6726 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6727 ret.original_type = ((t1 != error_mark_node
6728 && t2 != error_mark_node
6729 && (TYPE_MAIN_VARIANT (t1)
6730 == TYPE_MAIN_VARIANT (t2)))
6731 ? t1
6732 : NULL);
6734 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6735 return ret;
6738 /* Parse a binary expression; that is, a logical-OR-expression (C90
6739 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6740 NULL then it is an Objective-C message expression which is the
6741 primary-expression starting the expression as an initializer.
6743 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6744 when it should be the unfolded lhs. In a valid OpenMP source,
6745 one of the operands of the toplevel binary expression must be equal
6746 to it. In that case, just return a build2 created binary operation
6747 rather than result of parser_build_binary_op.
6749 multiplicative-expression:
6750 cast-expression
6751 multiplicative-expression * cast-expression
6752 multiplicative-expression / cast-expression
6753 multiplicative-expression % cast-expression
6755 additive-expression:
6756 multiplicative-expression
6757 additive-expression + multiplicative-expression
6758 additive-expression - multiplicative-expression
6760 shift-expression:
6761 additive-expression
6762 shift-expression << additive-expression
6763 shift-expression >> additive-expression
6765 relational-expression:
6766 shift-expression
6767 relational-expression < shift-expression
6768 relational-expression > shift-expression
6769 relational-expression <= shift-expression
6770 relational-expression >= shift-expression
6772 equality-expression:
6773 relational-expression
6774 equality-expression == relational-expression
6775 equality-expression != relational-expression
6777 AND-expression:
6778 equality-expression
6779 AND-expression & equality-expression
6781 exclusive-OR-expression:
6782 AND-expression
6783 exclusive-OR-expression ^ AND-expression
6785 inclusive-OR-expression:
6786 exclusive-OR-expression
6787 inclusive-OR-expression | exclusive-OR-expression
6789 logical-AND-expression:
6790 inclusive-OR-expression
6791 logical-AND-expression && inclusive-OR-expression
6793 logical-OR-expression:
6794 logical-AND-expression
6795 logical-OR-expression || logical-AND-expression
6798 static struct c_expr
6799 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6800 tree omp_atomic_lhs)
6802 /* A binary expression is parsed using operator-precedence parsing,
6803 with the operands being cast expressions. All the binary
6804 operators are left-associative. Thus a binary expression is of
6805 form:
6807 E0 op1 E1 op2 E2 ...
6809 which we represent on a stack. On the stack, the precedence
6810 levels are strictly increasing. When a new operator is
6811 encountered of higher precedence than that at the top of the
6812 stack, it is pushed; its LHS is the top expression, and its RHS
6813 is everything parsed until it is popped. When a new operator is
6814 encountered with precedence less than or equal to that at the top
6815 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6816 by the result of the operation until the operator at the top of
6817 the stack has lower precedence than the new operator or there is
6818 only one element on the stack; then the top expression is the LHS
6819 of the new operator. In the case of logical AND and OR
6820 expressions, we also need to adjust c_inhibit_evaluation_warnings
6821 as appropriate when the operators are pushed and popped. */
6823 struct {
6824 /* The expression at this stack level. */
6825 struct c_expr expr;
6826 /* The precedence of the operator on its left, PREC_NONE at the
6827 bottom of the stack. */
6828 enum c_parser_prec prec;
6829 /* The operation on its left. */
6830 enum tree_code op;
6831 /* The source location of this operation. */
6832 location_t loc;
6833 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6834 tree sizeof_arg;
6835 } stack[NUM_PRECS];
6836 int sp;
6837 /* Location of the binary operator. */
6838 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6839 #define POP \
6840 do { \
6841 switch (stack[sp].op) \
6843 case TRUTH_ANDIF_EXPR: \
6844 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6845 == truthvalue_false_node); \
6846 break; \
6847 case TRUTH_ORIF_EXPR: \
6848 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6849 == truthvalue_true_node); \
6850 break; \
6851 case TRUNC_DIV_EXPR: \
6852 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6853 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6855 tree type0 = stack[sp - 1].sizeof_arg; \
6856 tree type1 = stack[sp].sizeof_arg; \
6857 tree first_arg = type0; \
6858 if (!TYPE_P (type0)) \
6859 type0 = TREE_TYPE (type0); \
6860 if (!TYPE_P (type1)) \
6861 type1 = TREE_TYPE (type1); \
6862 if (POINTER_TYPE_P (type0) \
6863 && comptypes (TREE_TYPE (type0), type1) \
6864 && !(TREE_CODE (first_arg) == PARM_DECL \
6865 && C_ARRAY_PARAMETER (first_arg) \
6866 && warn_sizeof_array_argument)) \
6867 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6868 "division %<sizeof (%T) / sizeof (%T)%> does " \
6869 "not compute the number of array elements", \
6870 type0, type1)) \
6871 if (DECL_P (first_arg)) \
6872 inform (DECL_SOURCE_LOCATION (first_arg), \
6873 "first %<sizeof%> operand was declared here"); \
6875 break; \
6876 default: \
6877 break; \
6879 stack[sp - 1].expr \
6880 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6881 stack[sp - 1].expr, true, true); \
6882 stack[sp].expr \
6883 = convert_lvalue_to_rvalue (stack[sp].loc, \
6884 stack[sp].expr, true, true); \
6885 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6886 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6887 && ((1 << stack[sp].prec) \
6888 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6889 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6890 && stack[sp].op != TRUNC_MOD_EXPR \
6891 && stack[0].expr.value != error_mark_node \
6892 && stack[1].expr.value != error_mark_node \
6893 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6894 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6895 stack[0].expr.value \
6896 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6897 stack[0].expr.value, stack[1].expr.value); \
6898 else \
6899 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6900 stack[sp].op, \
6901 stack[sp - 1].expr, \
6902 stack[sp].expr); \
6903 sp--; \
6904 } while (0)
6905 gcc_assert (!after || c_dialect_objc ());
6906 stack[0].loc = c_parser_peek_token (parser)->location;
6907 stack[0].expr = c_parser_cast_expression (parser, after);
6908 stack[0].prec = PREC_NONE;
6909 stack[0].sizeof_arg = c_last_sizeof_arg;
6910 sp = 0;
6911 while (true)
6913 enum c_parser_prec oprec;
6914 enum tree_code ocode;
6915 source_range src_range;
6916 if (parser->error)
6917 goto out;
6918 switch (c_parser_peek_token (parser)->type)
6920 case CPP_MULT:
6921 oprec = PREC_MULT;
6922 ocode = MULT_EXPR;
6923 break;
6924 case CPP_DIV:
6925 oprec = PREC_MULT;
6926 ocode = TRUNC_DIV_EXPR;
6927 break;
6928 case CPP_MOD:
6929 oprec = PREC_MULT;
6930 ocode = TRUNC_MOD_EXPR;
6931 break;
6932 case CPP_PLUS:
6933 oprec = PREC_ADD;
6934 ocode = PLUS_EXPR;
6935 break;
6936 case CPP_MINUS:
6937 oprec = PREC_ADD;
6938 ocode = MINUS_EXPR;
6939 break;
6940 case CPP_LSHIFT:
6941 oprec = PREC_SHIFT;
6942 ocode = LSHIFT_EXPR;
6943 break;
6944 case CPP_RSHIFT:
6945 oprec = PREC_SHIFT;
6946 ocode = RSHIFT_EXPR;
6947 break;
6948 case CPP_LESS:
6949 oprec = PREC_REL;
6950 ocode = LT_EXPR;
6951 break;
6952 case CPP_GREATER:
6953 oprec = PREC_REL;
6954 ocode = GT_EXPR;
6955 break;
6956 case CPP_LESS_EQ:
6957 oprec = PREC_REL;
6958 ocode = LE_EXPR;
6959 break;
6960 case CPP_GREATER_EQ:
6961 oprec = PREC_REL;
6962 ocode = GE_EXPR;
6963 break;
6964 case CPP_EQ_EQ:
6965 oprec = PREC_EQ;
6966 ocode = EQ_EXPR;
6967 break;
6968 case CPP_NOT_EQ:
6969 oprec = PREC_EQ;
6970 ocode = NE_EXPR;
6971 break;
6972 case CPP_AND:
6973 oprec = PREC_BITAND;
6974 ocode = BIT_AND_EXPR;
6975 break;
6976 case CPP_XOR:
6977 oprec = PREC_BITXOR;
6978 ocode = BIT_XOR_EXPR;
6979 break;
6980 case CPP_OR:
6981 oprec = PREC_BITOR;
6982 ocode = BIT_IOR_EXPR;
6983 break;
6984 case CPP_AND_AND:
6985 oprec = PREC_LOGAND;
6986 ocode = TRUTH_ANDIF_EXPR;
6987 break;
6988 case CPP_OR_OR:
6989 oprec = PREC_LOGOR;
6990 ocode = TRUTH_ORIF_EXPR;
6991 break;
6992 default:
6993 /* Not a binary operator, so end of the binary
6994 expression. */
6995 goto out;
6997 binary_loc = c_parser_peek_token (parser)->location;
6998 while (oprec <= stack[sp].prec)
6999 POP;
7000 c_parser_consume_token (parser);
7001 switch (ocode)
7003 case TRUTH_ANDIF_EXPR:
7004 src_range = stack[sp].expr.src_range;
7005 stack[sp].expr
7006 = convert_lvalue_to_rvalue (stack[sp].loc,
7007 stack[sp].expr, true, true);
7008 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7009 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7010 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7011 == truthvalue_false_node);
7012 set_c_expr_source_range (&stack[sp].expr, src_range);
7013 break;
7014 case TRUTH_ORIF_EXPR:
7015 src_range = stack[sp].expr.src_range;
7016 stack[sp].expr
7017 = convert_lvalue_to_rvalue (stack[sp].loc,
7018 stack[sp].expr, true, true);
7019 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7020 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7021 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7022 == truthvalue_true_node);
7023 set_c_expr_source_range (&stack[sp].expr, src_range);
7024 break;
7025 default:
7026 break;
7028 sp++;
7029 stack[sp].loc = binary_loc;
7030 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7031 stack[sp].prec = oprec;
7032 stack[sp].op = ocode;
7033 stack[sp].sizeof_arg = c_last_sizeof_arg;
7035 out:
7036 while (sp > 0)
7037 POP;
7038 return stack[0].expr;
7039 #undef POP
7042 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7043 is not NULL then it is an Objective-C message expression which is the
7044 primary-expression starting the expression as an initializer.
7046 cast-expression:
7047 unary-expression
7048 ( type-name ) unary-expression
7051 static struct c_expr
7052 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7054 location_t cast_loc = c_parser_peek_token (parser)->location;
7055 gcc_assert (!after || c_dialect_objc ());
7056 if (after)
7057 return c_parser_postfix_expression_after_primary (parser,
7058 cast_loc, *after);
7059 /* If the expression begins with a parenthesized type name, it may
7060 be either a cast or a compound literal; we need to see whether
7061 the next character is '{' to tell the difference. If not, it is
7062 an unary expression. Full detection of unknown typenames here
7063 would require a 3-token lookahead. */
7064 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7065 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7067 struct c_type_name *type_name;
7068 struct c_expr ret;
7069 struct c_expr expr;
7070 matching_parens parens;
7071 parens.consume_open (parser);
7072 type_name = c_parser_type_name (parser, true);
7073 parens.skip_until_found_close (parser);
7074 if (type_name == NULL)
7076 ret.set_error ();
7077 ret.original_code = ERROR_MARK;
7078 ret.original_type = NULL;
7079 return ret;
7082 /* Save casted types in the function's used types hash table. */
7083 used_types_insert (type_name->specs->type);
7085 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7086 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7087 cast_loc);
7088 if (type_name->specs->alignas_p)
7089 error_at (type_name->specs->locations[cdw_alignas],
7090 "alignment specified for type name in cast");
7092 location_t expr_loc = c_parser_peek_token (parser)->location;
7093 expr = c_parser_cast_expression (parser, NULL);
7094 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7096 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7097 if (ret.value && expr.value)
7098 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7099 ret.original_code = ERROR_MARK;
7100 ret.original_type = NULL;
7101 return ret;
7103 else
7104 return c_parser_unary_expression (parser);
7107 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7109 unary-expression:
7110 postfix-expression
7111 ++ unary-expression
7112 -- unary-expression
7113 unary-operator cast-expression
7114 sizeof unary-expression
7115 sizeof ( type-name )
7117 unary-operator: one of
7118 & * + - ~ !
7120 GNU extensions:
7122 unary-expression:
7123 __alignof__ unary-expression
7124 __alignof__ ( type-name )
7125 && identifier
7127 (C11 permits _Alignof with type names only.)
7129 unary-operator: one of
7130 __extension__ __real__ __imag__
7132 Transactional Memory:
7134 unary-expression:
7135 transaction-expression
7137 In addition, the GNU syntax treats ++ and -- as unary operators, so
7138 they may be applied to cast expressions with errors for non-lvalues
7139 given later. */
7141 static struct c_expr
7142 c_parser_unary_expression (c_parser *parser)
7144 int ext;
7145 struct c_expr ret, op;
7146 location_t op_loc = c_parser_peek_token (parser)->location;
7147 location_t exp_loc;
7148 location_t finish;
7149 ret.original_code = ERROR_MARK;
7150 ret.original_type = NULL;
7151 switch (c_parser_peek_token (parser)->type)
7153 case CPP_PLUS_PLUS:
7154 c_parser_consume_token (parser);
7155 exp_loc = c_parser_peek_token (parser)->location;
7156 op = c_parser_cast_expression (parser, NULL);
7158 op = default_function_array_read_conversion (exp_loc, op);
7159 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7160 case CPP_MINUS_MINUS:
7161 c_parser_consume_token (parser);
7162 exp_loc = c_parser_peek_token (parser)->location;
7163 op = c_parser_cast_expression (parser, NULL);
7165 op = default_function_array_read_conversion (exp_loc, op);
7166 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7167 case CPP_AND:
7168 c_parser_consume_token (parser);
7169 op = c_parser_cast_expression (parser, NULL);
7170 mark_exp_read (op.value);
7171 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7172 case CPP_MULT:
7174 c_parser_consume_token (parser);
7175 exp_loc = c_parser_peek_token (parser)->location;
7176 op = c_parser_cast_expression (parser, NULL);
7177 finish = op.get_finish ();
7178 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7179 location_t combined_loc = make_location (op_loc, op_loc, finish);
7180 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7181 ret.src_range.m_start = op_loc;
7182 ret.src_range.m_finish = finish;
7183 return ret;
7185 case CPP_PLUS:
7186 if (!c_dialect_objc () && !in_system_header_at (input_location))
7187 warning_at (op_loc,
7188 OPT_Wtraditional,
7189 "traditional C rejects the unary plus operator");
7190 c_parser_consume_token (parser);
7191 exp_loc = c_parser_peek_token (parser)->location;
7192 op = c_parser_cast_expression (parser, NULL);
7193 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7194 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7195 case CPP_MINUS:
7196 c_parser_consume_token (parser);
7197 exp_loc = c_parser_peek_token (parser)->location;
7198 op = c_parser_cast_expression (parser, NULL);
7199 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7200 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7201 case CPP_COMPL:
7202 c_parser_consume_token (parser);
7203 exp_loc = c_parser_peek_token (parser)->location;
7204 op = c_parser_cast_expression (parser, NULL);
7205 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7206 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7207 case CPP_NOT:
7208 c_parser_consume_token (parser);
7209 exp_loc = c_parser_peek_token (parser)->location;
7210 op = c_parser_cast_expression (parser, NULL);
7211 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7212 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7213 case CPP_AND_AND:
7214 /* Refer to the address of a label as a pointer. */
7215 c_parser_consume_token (parser);
7216 if (c_parser_next_token_is (parser, CPP_NAME))
7218 ret.value = finish_label_address_expr
7219 (c_parser_peek_token (parser)->value, op_loc);
7220 set_c_expr_source_range (&ret, op_loc,
7221 c_parser_peek_token (parser)->get_finish ());
7222 c_parser_consume_token (parser);
7224 else
7226 c_parser_error (parser, "expected identifier");
7227 ret.set_error ();
7229 return ret;
7230 case CPP_KEYWORD:
7231 switch (c_parser_peek_token (parser)->keyword)
7233 case RID_SIZEOF:
7234 return c_parser_sizeof_expression (parser);
7235 case RID_ALIGNOF:
7236 return c_parser_alignof_expression (parser);
7237 case RID_EXTENSION:
7238 c_parser_consume_token (parser);
7239 ext = disable_extension_diagnostics ();
7240 ret = c_parser_cast_expression (parser, NULL);
7241 restore_extension_diagnostics (ext);
7242 return ret;
7243 case RID_REALPART:
7244 c_parser_consume_token (parser);
7245 exp_loc = c_parser_peek_token (parser)->location;
7246 op = c_parser_cast_expression (parser, NULL);
7247 op = default_function_array_conversion (exp_loc, op);
7248 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7249 case RID_IMAGPART:
7250 c_parser_consume_token (parser);
7251 exp_loc = c_parser_peek_token (parser)->location;
7252 op = c_parser_cast_expression (parser, NULL);
7253 op = default_function_array_conversion (exp_loc, op);
7254 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7255 case RID_TRANSACTION_ATOMIC:
7256 case RID_TRANSACTION_RELAXED:
7257 return c_parser_transaction_expression (parser,
7258 c_parser_peek_token (parser)->keyword);
7259 default:
7260 return c_parser_postfix_expression (parser);
7262 default:
7263 return c_parser_postfix_expression (parser);
7267 /* Parse a sizeof expression. */
7269 static struct c_expr
7270 c_parser_sizeof_expression (c_parser *parser)
7272 struct c_expr expr;
7273 struct c_expr result;
7274 location_t expr_loc;
7275 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7277 location_t start;
7278 location_t finish = UNKNOWN_LOCATION;
7280 start = c_parser_peek_token (parser)->location;
7282 c_parser_consume_token (parser);
7283 c_inhibit_evaluation_warnings++;
7284 in_sizeof++;
7285 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7286 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7288 /* Either sizeof ( type-name ) or sizeof unary-expression
7289 starting with a compound literal. */
7290 struct c_type_name *type_name;
7291 matching_parens parens;
7292 parens.consume_open (parser);
7293 expr_loc = c_parser_peek_token (parser)->location;
7294 type_name = c_parser_type_name (parser, true);
7295 parens.skip_until_found_close (parser);
7296 finish = parser->tokens_buf[0].location;
7297 if (type_name == NULL)
7299 struct c_expr ret;
7300 c_inhibit_evaluation_warnings--;
7301 in_sizeof--;
7302 ret.set_error ();
7303 ret.original_code = ERROR_MARK;
7304 ret.original_type = NULL;
7305 return ret;
7307 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7309 expr = c_parser_postfix_expression_after_paren_type (parser,
7310 type_name,
7311 expr_loc);
7312 finish = expr.get_finish ();
7313 goto sizeof_expr;
7315 /* sizeof ( type-name ). */
7316 if (type_name->specs->alignas_p)
7317 error_at (type_name->specs->locations[cdw_alignas],
7318 "alignment specified for type name in %<sizeof%>");
7319 c_inhibit_evaluation_warnings--;
7320 in_sizeof--;
7321 result = c_expr_sizeof_type (expr_loc, type_name);
7323 else
7325 expr_loc = c_parser_peek_token (parser)->location;
7326 expr = c_parser_unary_expression (parser);
7327 finish = expr.get_finish ();
7328 sizeof_expr:
7329 c_inhibit_evaluation_warnings--;
7330 in_sizeof--;
7331 mark_exp_read (expr.value);
7332 if (TREE_CODE (expr.value) == COMPONENT_REF
7333 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7334 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7335 result = c_expr_sizeof_expr (expr_loc, expr);
7337 if (finish != UNKNOWN_LOCATION)
7338 set_c_expr_source_range (&result, start, finish);
7339 return result;
7342 /* Parse an alignof expression. */
7344 static struct c_expr
7345 c_parser_alignof_expression (c_parser *parser)
7347 struct c_expr expr;
7348 location_t start_loc = c_parser_peek_token (parser)->location;
7349 location_t end_loc;
7350 tree alignof_spelling = c_parser_peek_token (parser)->value;
7351 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7352 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7353 "_Alignof") == 0;
7354 /* A diagnostic is not required for the use of this identifier in
7355 the implementation namespace; only diagnose it for the C11
7356 spelling because of existing code using the other spellings. */
7357 if (is_c11_alignof)
7359 if (flag_isoc99)
7360 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7361 alignof_spelling);
7362 else
7363 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7364 alignof_spelling);
7366 c_parser_consume_token (parser);
7367 c_inhibit_evaluation_warnings++;
7368 in_alignof++;
7369 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7370 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7372 /* Either __alignof__ ( type-name ) or __alignof__
7373 unary-expression starting with a compound literal. */
7374 location_t loc;
7375 struct c_type_name *type_name;
7376 struct c_expr ret;
7377 matching_parens parens;
7378 parens.consume_open (parser);
7379 loc = c_parser_peek_token (parser)->location;
7380 type_name = c_parser_type_name (parser, true);
7381 end_loc = c_parser_peek_token (parser)->location;
7382 parens.skip_until_found_close (parser);
7383 if (type_name == NULL)
7385 struct c_expr ret;
7386 c_inhibit_evaluation_warnings--;
7387 in_alignof--;
7388 ret.set_error ();
7389 ret.original_code = ERROR_MARK;
7390 ret.original_type = NULL;
7391 return ret;
7393 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7395 expr = c_parser_postfix_expression_after_paren_type (parser,
7396 type_name,
7397 loc);
7398 goto alignof_expr;
7400 /* alignof ( type-name ). */
7401 if (type_name->specs->alignas_p)
7402 error_at (type_name->specs->locations[cdw_alignas],
7403 "alignment specified for type name in %qE",
7404 alignof_spelling);
7405 c_inhibit_evaluation_warnings--;
7406 in_alignof--;
7407 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7408 NULL, NULL),
7409 false, is_c11_alignof, 1);
7410 ret.original_code = ERROR_MARK;
7411 ret.original_type = NULL;
7412 set_c_expr_source_range (&ret, start_loc, end_loc);
7413 return ret;
7415 else
7417 struct c_expr ret;
7418 expr = c_parser_unary_expression (parser);
7419 end_loc = expr.src_range.m_finish;
7420 alignof_expr:
7421 mark_exp_read (expr.value);
7422 c_inhibit_evaluation_warnings--;
7423 in_alignof--;
7424 if (is_c11_alignof)
7425 pedwarn (start_loc,
7426 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7427 alignof_spelling);
7428 ret.value = c_alignof_expr (start_loc, expr.value);
7429 ret.original_code = ERROR_MARK;
7430 ret.original_type = NULL;
7431 set_c_expr_source_range (&ret, start_loc, end_loc);
7432 return ret;
7436 /* Helper function to read arguments of builtins which are interfaces
7437 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7438 others. The name of the builtin is passed using BNAME parameter.
7439 Function returns true if there were no errors while parsing and
7440 stores the arguments in CEXPR_LIST. If it returns true,
7441 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7442 parenthesis. */
7443 static bool
7444 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7445 vec<c_expr_t, va_gc> **ret_cexpr_list,
7446 bool choose_expr_p,
7447 location_t *out_close_paren_loc)
7449 location_t loc = c_parser_peek_token (parser)->location;
7450 vec<c_expr_t, va_gc> *cexpr_list;
7451 c_expr_t expr;
7452 bool saved_force_folding_builtin_constant_p;
7454 *ret_cexpr_list = NULL;
7455 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7457 error_at (loc, "cannot take address of %qs", bname);
7458 return false;
7461 c_parser_consume_token (parser);
7463 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7465 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7466 c_parser_consume_token (parser);
7467 return true;
7470 saved_force_folding_builtin_constant_p
7471 = force_folding_builtin_constant_p;
7472 force_folding_builtin_constant_p |= choose_expr_p;
7473 expr = c_parser_expr_no_commas (parser, NULL);
7474 force_folding_builtin_constant_p
7475 = saved_force_folding_builtin_constant_p;
7476 vec_alloc (cexpr_list, 1);
7477 vec_safe_push (cexpr_list, expr);
7478 while (c_parser_next_token_is (parser, CPP_COMMA))
7480 c_parser_consume_token (parser);
7481 expr = c_parser_expr_no_commas (parser, NULL);
7482 vec_safe_push (cexpr_list, expr);
7485 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7486 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7487 return false;
7489 *ret_cexpr_list = cexpr_list;
7490 return true;
7493 /* This represents a single generic-association. */
7495 struct c_generic_association
7497 /* The location of the starting token of the type. */
7498 location_t type_location;
7499 /* The association's type, or NULL_TREE for 'default'. */
7500 tree type;
7501 /* The association's expression. */
7502 struct c_expr expression;
7505 /* Parse a generic-selection. (C11 6.5.1.1).
7507 generic-selection:
7508 _Generic ( assignment-expression , generic-assoc-list )
7510 generic-assoc-list:
7511 generic-association
7512 generic-assoc-list , generic-association
7514 generic-association:
7515 type-name : assignment-expression
7516 default : assignment-expression
7519 static struct c_expr
7520 c_parser_generic_selection (c_parser *parser)
7522 struct c_expr selector, error_expr;
7523 tree selector_type;
7524 struct c_generic_association matched_assoc;
7525 bool match_found = false;
7526 location_t generic_loc, selector_loc;
7528 error_expr.original_code = ERROR_MARK;
7529 error_expr.original_type = NULL;
7530 error_expr.set_error ();
7531 matched_assoc.type_location = UNKNOWN_LOCATION;
7532 matched_assoc.type = NULL_TREE;
7533 matched_assoc.expression = error_expr;
7535 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7536 generic_loc = c_parser_peek_token (parser)->location;
7537 c_parser_consume_token (parser);
7538 if (flag_isoc99)
7539 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7540 "ISO C99 does not support %<_Generic%>");
7541 else
7542 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7543 "ISO C90 does not support %<_Generic%>");
7545 matching_parens parens;
7546 if (!parens.require_open (parser))
7547 return error_expr;
7549 c_inhibit_evaluation_warnings++;
7550 selector_loc = c_parser_peek_token (parser)->location;
7551 selector = c_parser_expr_no_commas (parser, NULL);
7552 selector = default_function_array_conversion (selector_loc, selector);
7553 c_inhibit_evaluation_warnings--;
7555 if (selector.value == error_mark_node)
7557 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7558 return selector;
7560 selector_type = TREE_TYPE (selector.value);
7561 /* In ISO C terms, rvalues (including the controlling expression of
7562 _Generic) do not have qualified types. */
7563 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7564 selector_type = TYPE_MAIN_VARIANT (selector_type);
7565 /* In ISO C terms, _Noreturn is not part of the type of expressions
7566 such as &abort, but in GCC it is represented internally as a type
7567 qualifier. */
7568 if (FUNCTION_POINTER_TYPE_P (selector_type)
7569 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7570 selector_type
7571 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7573 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7576 return error_expr;
7579 auto_vec<c_generic_association> associations;
7580 while (1)
7582 struct c_generic_association assoc, *iter;
7583 unsigned int ix;
7584 c_token *token = c_parser_peek_token (parser);
7586 assoc.type_location = token->location;
7587 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7589 c_parser_consume_token (parser);
7590 assoc.type = NULL_TREE;
7592 else
7594 struct c_type_name *type_name;
7596 type_name = c_parser_type_name (parser);
7597 if (type_name == NULL)
7599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7600 return error_expr;
7602 assoc.type = groktypename (type_name, NULL, NULL);
7603 if (assoc.type == error_mark_node)
7605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7606 return error_expr;
7609 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7610 error_at (assoc.type_location,
7611 "%<_Generic%> association has function type");
7612 else if (!COMPLETE_TYPE_P (assoc.type))
7613 error_at (assoc.type_location,
7614 "%<_Generic%> association has incomplete type");
7616 if (variably_modified_type_p (assoc.type, NULL_TREE))
7617 error_at (assoc.type_location,
7618 "%<_Generic%> association has "
7619 "variable length type");
7622 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7624 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7625 return error_expr;
7628 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7629 if (assoc.expression.value == error_mark_node)
7631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7632 return error_expr;
7635 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7637 if (assoc.type == NULL_TREE)
7639 if (iter->type == NULL_TREE)
7641 error_at (assoc.type_location,
7642 "duplicate %<default%> case in %<_Generic%>");
7643 inform (iter->type_location, "original %<default%> is here");
7646 else if (iter->type != NULL_TREE)
7648 if (comptypes (assoc.type, iter->type))
7650 error_at (assoc.type_location,
7651 "%<_Generic%> specifies two compatible types");
7652 inform (iter->type_location, "compatible type is here");
7657 if (assoc.type == NULL_TREE)
7659 if (!match_found)
7661 matched_assoc = assoc;
7662 match_found = true;
7665 else if (comptypes (assoc.type, selector_type))
7667 if (!match_found || matched_assoc.type == NULL_TREE)
7669 matched_assoc = assoc;
7670 match_found = true;
7672 else
7674 error_at (assoc.type_location,
7675 "%<_Generic%> selector matches multiple associations");
7676 inform (matched_assoc.type_location,
7677 "other match is here");
7681 associations.safe_push (assoc);
7683 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7684 break;
7685 c_parser_consume_token (parser);
7688 if (!parens.require_close (parser))
7690 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7691 return error_expr;
7694 if (!match_found)
7696 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7697 "compatible with any association",
7698 selector_type);
7699 return error_expr;
7702 return matched_assoc.expression;
7705 /* Check the validity of a function pointer argument *EXPR (argument
7706 position POS) to __builtin_tgmath. Return the number of function
7707 arguments if possibly valid; return 0 having reported an error if
7708 not valid. */
7710 static unsigned int
7711 check_tgmath_function (c_expr *expr, unsigned int pos)
7713 tree type = TREE_TYPE (expr->value);
7714 if (!FUNCTION_POINTER_TYPE_P (type))
7716 error_at (expr->get_location (),
7717 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7718 pos);
7719 return 0;
7721 type = TREE_TYPE (type);
7722 if (!prototype_p (type))
7724 error_at (expr->get_location (),
7725 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7726 return 0;
7728 if (stdarg_p (type))
7730 error_at (expr->get_location (),
7731 "argument %u of %<__builtin_tgmath%> has variable arguments",
7732 pos);
7733 return 0;
7735 unsigned int nargs = 0;
7736 function_args_iterator iter;
7737 tree t;
7738 FOREACH_FUNCTION_ARGS (type, t, iter)
7740 if (t == void_type_node)
7741 break;
7742 nargs++;
7744 if (nargs == 0)
7746 error_at (expr->get_location (),
7747 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7748 return 0;
7750 return nargs;
7753 /* Ways in which a parameter or return value of a type-generic macro
7754 may vary between the different functions the macro may call. */
7755 enum tgmath_parm_kind
7757 tgmath_fixed, tgmath_real, tgmath_complex
7760 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7761 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7762 call c_parser_postfix_expression_after_paren_type on encountering them.
7764 postfix-expression:
7765 primary-expression
7766 postfix-expression [ expression ]
7767 postfix-expression ( argument-expression-list[opt] )
7768 postfix-expression . identifier
7769 postfix-expression -> identifier
7770 postfix-expression ++
7771 postfix-expression --
7772 ( type-name ) { initializer-list }
7773 ( type-name ) { initializer-list , }
7775 argument-expression-list:
7776 argument-expression
7777 argument-expression-list , argument-expression
7779 primary-expression:
7780 identifier
7781 constant
7782 string-literal
7783 ( expression )
7784 generic-selection
7786 GNU extensions:
7788 primary-expression:
7789 __func__
7790 (treated as a keyword in GNU C)
7791 __FUNCTION__
7792 __PRETTY_FUNCTION__
7793 ( compound-statement )
7794 __builtin_va_arg ( assignment-expression , type-name )
7795 __builtin_offsetof ( type-name , offsetof-member-designator )
7796 __builtin_choose_expr ( assignment-expression ,
7797 assignment-expression ,
7798 assignment-expression )
7799 __builtin_types_compatible_p ( type-name , type-name )
7800 __builtin_tgmath ( expr-list )
7801 __builtin_complex ( assignment-expression , assignment-expression )
7802 __builtin_shuffle ( assignment-expression , assignment-expression )
7803 __builtin_shuffle ( assignment-expression ,
7804 assignment-expression ,
7805 assignment-expression, )
7807 offsetof-member-designator:
7808 identifier
7809 offsetof-member-designator . identifier
7810 offsetof-member-designator [ expression ]
7812 Objective-C:
7814 primary-expression:
7815 [ objc-receiver objc-message-args ]
7816 @selector ( objc-selector-arg )
7817 @protocol ( identifier )
7818 @encode ( type-name )
7819 objc-string-literal
7820 Classname . identifier
7823 static struct c_expr
7824 c_parser_postfix_expression (c_parser *parser)
7826 struct c_expr expr, e1;
7827 struct c_type_name *t1, *t2;
7828 location_t loc = c_parser_peek_token (parser)->location;
7829 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7830 expr.original_code = ERROR_MARK;
7831 expr.original_type = NULL;
7832 switch (c_parser_peek_token (parser)->type)
7834 case CPP_NUMBER:
7835 expr.value = c_parser_peek_token (parser)->value;
7836 set_c_expr_source_range (&expr, tok_range);
7837 loc = c_parser_peek_token (parser)->location;
7838 c_parser_consume_token (parser);
7839 if (TREE_CODE (expr.value) == FIXED_CST
7840 && !targetm.fixed_point_supported_p ())
7842 error_at (loc, "fixed-point types not supported for this target");
7843 expr.set_error ();
7845 break;
7846 case CPP_CHAR:
7847 case CPP_CHAR16:
7848 case CPP_CHAR32:
7849 case CPP_WCHAR:
7850 expr.value = c_parser_peek_token (parser)->value;
7851 /* For the purpose of warning when a pointer is compared with
7852 a zero character constant. */
7853 expr.original_type = char_type_node;
7854 set_c_expr_source_range (&expr, tok_range);
7855 c_parser_consume_token (parser);
7856 break;
7857 case CPP_STRING:
7858 case CPP_STRING16:
7859 case CPP_STRING32:
7860 case CPP_WSTRING:
7861 case CPP_UTF8STRING:
7862 expr.value = c_parser_peek_token (parser)->value;
7863 set_c_expr_source_range (&expr, tok_range);
7864 expr.original_code = STRING_CST;
7865 c_parser_consume_token (parser);
7866 break;
7867 case CPP_OBJC_STRING:
7868 gcc_assert (c_dialect_objc ());
7869 expr.value
7870 = objc_build_string_object (c_parser_peek_token (parser)->value);
7871 set_c_expr_source_range (&expr, tok_range);
7872 c_parser_consume_token (parser);
7873 break;
7874 case CPP_NAME:
7875 switch (c_parser_peek_token (parser)->id_kind)
7877 case C_ID_ID:
7879 tree id = c_parser_peek_token (parser)->value;
7880 c_parser_consume_token (parser);
7881 expr.value = build_external_ref (loc, id,
7882 (c_parser_peek_token (parser)->type
7883 == CPP_OPEN_PAREN),
7884 &expr.original_type);
7885 set_c_expr_source_range (&expr, tok_range);
7886 break;
7888 case C_ID_CLASSNAME:
7890 /* Here we parse the Objective-C 2.0 Class.name dot
7891 syntax. */
7892 tree class_name = c_parser_peek_token (parser)->value;
7893 tree component;
7894 c_parser_consume_token (parser);
7895 gcc_assert (c_dialect_objc ());
7896 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7898 expr.set_error ();
7899 break;
7901 if (c_parser_next_token_is_not (parser, CPP_NAME))
7903 c_parser_error (parser, "expected identifier");
7904 expr.set_error ();
7905 break;
7907 c_token *component_tok = c_parser_peek_token (parser);
7908 component = component_tok->value;
7909 location_t end_loc = component_tok->get_finish ();
7910 c_parser_consume_token (parser);
7911 expr.value = objc_build_class_component_ref (class_name,
7912 component);
7913 set_c_expr_source_range (&expr, loc, end_loc);
7914 break;
7916 default:
7917 c_parser_error (parser, "expected expression");
7918 expr.set_error ();
7919 break;
7921 break;
7922 case CPP_OPEN_PAREN:
7923 /* A parenthesized expression, statement expression or compound
7924 literal. */
7925 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7927 /* A statement expression. */
7928 tree stmt;
7929 location_t brace_loc;
7930 c_parser_consume_token (parser);
7931 brace_loc = c_parser_peek_token (parser)->location;
7932 c_parser_consume_token (parser);
7933 /* If we've not yet started the current function's statement list,
7934 or we're in the parameter scope of an old-style function
7935 declaration, statement expressions are not allowed. */
7936 if (!building_stmt_list_p () || old_style_parameter_scope ())
7938 error_at (loc, "braced-group within expression allowed "
7939 "only inside a function");
7940 parser->error = true;
7941 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7943 expr.set_error ();
7944 break;
7946 stmt = c_begin_stmt_expr ();
7947 c_parser_compound_statement_nostart (parser);
7948 location_t close_loc = c_parser_peek_token (parser)->location;
7949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7950 "expected %<)%>");
7951 pedwarn (loc, OPT_Wpedantic,
7952 "ISO C forbids braced-groups within expressions");
7953 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7954 set_c_expr_source_range (&expr, loc, close_loc);
7955 mark_exp_read (expr.value);
7957 else
7959 /* A parenthesized expression. */
7960 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7961 c_parser_consume_token (parser);
7962 expr = c_parser_expression (parser);
7963 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7964 TREE_NO_WARNING (expr.value) = 1;
7965 if (expr.original_code != C_MAYBE_CONST_EXPR
7966 && expr.original_code != SIZEOF_EXPR)
7967 expr.original_code = ERROR_MARK;
7968 /* Don't change EXPR.ORIGINAL_TYPE. */
7969 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7970 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7971 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7972 "expected %<)%>", loc_open_paren);
7974 break;
7975 case CPP_KEYWORD:
7976 switch (c_parser_peek_token (parser)->keyword)
7978 case RID_FUNCTION_NAME:
7979 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7980 "%<__FUNCTION__%> predefined identifier");
7981 expr.value = fname_decl (loc,
7982 c_parser_peek_token (parser)->keyword,
7983 c_parser_peek_token (parser)->value);
7984 set_c_expr_source_range (&expr, loc, loc);
7985 c_parser_consume_token (parser);
7986 break;
7987 case RID_PRETTY_FUNCTION_NAME:
7988 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7989 "%<__PRETTY_FUNCTION__%> predefined identifier");
7990 expr.value = fname_decl (loc,
7991 c_parser_peek_token (parser)->keyword,
7992 c_parser_peek_token (parser)->value);
7993 set_c_expr_source_range (&expr, loc, loc);
7994 c_parser_consume_token (parser);
7995 break;
7996 case RID_C99_FUNCTION_NAME:
7997 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7998 "%<__func__%> predefined identifier");
7999 expr.value = fname_decl (loc,
8000 c_parser_peek_token (parser)->keyword,
8001 c_parser_peek_token (parser)->value);
8002 set_c_expr_source_range (&expr, loc, loc);
8003 c_parser_consume_token (parser);
8004 break;
8005 case RID_VA_ARG:
8007 location_t start_loc = loc;
8008 c_parser_consume_token (parser);
8009 matching_parens parens;
8010 if (!parens.require_open (parser))
8012 expr.set_error ();
8013 break;
8015 e1 = c_parser_expr_no_commas (parser, NULL);
8016 mark_exp_read (e1.value);
8017 e1.value = c_fully_fold (e1.value, false, NULL);
8018 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8020 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8021 expr.set_error ();
8022 break;
8024 loc = c_parser_peek_token (parser)->location;
8025 t1 = c_parser_type_name (parser);
8026 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8028 "expected %<)%>");
8029 if (t1 == NULL)
8031 expr.set_error ();
8033 else
8035 tree type_expr = NULL_TREE;
8036 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8037 groktypename (t1, &type_expr, NULL));
8038 if (type_expr)
8040 expr.value = build2 (C_MAYBE_CONST_EXPR,
8041 TREE_TYPE (expr.value), type_expr,
8042 expr.value);
8043 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8045 set_c_expr_source_range (&expr, start_loc, end_loc);
8048 break;
8049 case RID_OFFSETOF:
8051 c_parser_consume_token (parser);
8052 matching_parens parens;
8053 if (!parens.require_open (parser))
8055 expr.set_error ();
8056 break;
8058 t1 = c_parser_type_name (parser);
8059 if (t1 == NULL)
8060 parser->error = true;
8061 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8062 gcc_assert (parser->error);
8063 if (parser->error)
8065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8066 expr.set_error ();
8067 break;
8069 tree type = groktypename (t1, NULL, NULL);
8070 tree offsetof_ref;
8071 if (type == error_mark_node)
8072 offsetof_ref = error_mark_node;
8073 else
8075 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8076 SET_EXPR_LOCATION (offsetof_ref, loc);
8078 /* Parse the second argument to __builtin_offsetof. We
8079 must have one identifier, and beyond that we want to
8080 accept sub structure and sub array references. */
8081 if (c_parser_next_token_is (parser, CPP_NAME))
8083 c_token *comp_tok = c_parser_peek_token (parser);
8084 offsetof_ref = build_component_ref
8085 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8086 c_parser_consume_token (parser);
8087 while (c_parser_next_token_is (parser, CPP_DOT)
8088 || c_parser_next_token_is (parser,
8089 CPP_OPEN_SQUARE)
8090 || c_parser_next_token_is (parser,
8091 CPP_DEREF))
8093 if (c_parser_next_token_is (parser, CPP_DEREF))
8095 loc = c_parser_peek_token (parser)->location;
8096 offsetof_ref = build_array_ref (loc,
8097 offsetof_ref,
8098 integer_zero_node);
8099 goto do_dot;
8101 else if (c_parser_next_token_is (parser, CPP_DOT))
8103 do_dot:
8104 c_parser_consume_token (parser);
8105 if (c_parser_next_token_is_not (parser,
8106 CPP_NAME))
8108 c_parser_error (parser, "expected identifier");
8109 break;
8111 c_token *comp_tok = c_parser_peek_token (parser);
8112 offsetof_ref = build_component_ref
8113 (loc, offsetof_ref, comp_tok->value,
8114 comp_tok->location);
8115 c_parser_consume_token (parser);
8117 else
8119 struct c_expr ce;
8120 tree idx;
8121 loc = c_parser_peek_token (parser)->location;
8122 c_parser_consume_token (parser);
8123 ce = c_parser_expression (parser);
8124 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8125 idx = ce.value;
8126 idx = c_fully_fold (idx, false, NULL);
8127 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8128 "expected %<]%>");
8129 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8133 else
8134 c_parser_error (parser, "expected identifier");
8135 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8136 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8137 "expected %<)%>");
8138 expr.value = fold_offsetof (offsetof_ref);
8139 set_c_expr_source_range (&expr, loc, end_loc);
8141 break;
8142 case RID_CHOOSE_EXPR:
8144 vec<c_expr_t, va_gc> *cexpr_list;
8145 c_expr_t *e1_p, *e2_p, *e3_p;
8146 tree c;
8147 location_t close_paren_loc;
8149 c_parser_consume_token (parser);
8150 if (!c_parser_get_builtin_args (parser,
8151 "__builtin_choose_expr",
8152 &cexpr_list, true,
8153 &close_paren_loc))
8155 expr.set_error ();
8156 break;
8159 if (vec_safe_length (cexpr_list) != 3)
8161 error_at (loc, "wrong number of arguments to "
8162 "%<__builtin_choose_expr%>");
8163 expr.set_error ();
8164 break;
8167 e1_p = &(*cexpr_list)[0];
8168 e2_p = &(*cexpr_list)[1];
8169 e3_p = &(*cexpr_list)[2];
8171 c = e1_p->value;
8172 mark_exp_read (e2_p->value);
8173 mark_exp_read (e3_p->value);
8174 if (TREE_CODE (c) != INTEGER_CST
8175 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8176 error_at (loc,
8177 "first argument to %<__builtin_choose_expr%> not"
8178 " a constant");
8179 constant_expression_warning (c);
8180 expr = integer_zerop (c) ? *e3_p : *e2_p;
8181 set_c_expr_source_range (&expr, loc, close_paren_loc);
8182 break;
8184 case RID_TYPES_COMPATIBLE_P:
8186 c_parser_consume_token (parser);
8187 matching_parens parens;
8188 if (!parens.require_open (parser))
8190 expr.set_error ();
8191 break;
8193 t1 = c_parser_type_name (parser);
8194 if (t1 == NULL)
8196 expr.set_error ();
8197 break;
8199 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8201 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8202 expr.set_error ();
8203 break;
8205 t2 = c_parser_type_name (parser);
8206 if (t2 == NULL)
8208 expr.set_error ();
8209 break;
8211 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8212 parens.skip_until_found_close (parser);
8213 tree e1, e2;
8214 e1 = groktypename (t1, NULL, NULL);
8215 e2 = groktypename (t2, NULL, NULL);
8216 if (e1 == error_mark_node || e2 == error_mark_node)
8218 expr.set_error ();
8219 break;
8222 e1 = TYPE_MAIN_VARIANT (e1);
8223 e2 = TYPE_MAIN_VARIANT (e2);
8225 expr.value
8226 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8227 set_c_expr_source_range (&expr, loc, close_paren_loc);
8229 break;
8230 case RID_BUILTIN_TGMATH:
8232 vec<c_expr_t, va_gc> *cexpr_list;
8233 location_t close_paren_loc;
8235 c_parser_consume_token (parser);
8236 if (!c_parser_get_builtin_args (parser,
8237 "__builtin_tgmath",
8238 &cexpr_list, false,
8239 &close_paren_loc))
8241 expr.set_error ();
8242 break;
8245 if (vec_safe_length (cexpr_list) < 3)
8247 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8248 expr.set_error ();
8249 break;
8252 unsigned int i;
8253 c_expr_t *p;
8254 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8255 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8256 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8257 if (nargs == 0)
8259 expr.set_error ();
8260 break;
8262 if (vec_safe_length (cexpr_list) < nargs)
8264 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8265 expr.set_error ();
8266 break;
8268 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8269 if (num_functions < 2)
8271 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8272 expr.set_error ();
8273 break;
8276 /* The first NUM_FUNCTIONS expressions are the function
8277 pointers. The remaining NARGS expressions are the
8278 arguments that are to be passed to one of those
8279 functions, chosen following <tgmath.h> rules. */
8280 for (unsigned int j = 1; j < num_functions; j++)
8282 unsigned int this_nargs
8283 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8284 if (this_nargs == 0)
8286 expr.set_error ();
8287 goto out;
8289 if (this_nargs != nargs)
8291 error_at ((*cexpr_list)[j].get_location (),
8292 "argument %u of %<__builtin_tgmath%> has "
8293 "wrong number of arguments", j + 1);
8294 expr.set_error ();
8295 goto out;
8299 /* The functions all have the same number of arguments.
8300 Determine whether arguments and return types vary in
8301 ways permitted for <tgmath.h> functions. */
8302 /* The first entry in each of these vectors is for the
8303 return type, subsequent entries for parameter
8304 types. */
8305 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8306 auto_vec<tree> parm_first (nargs + 1);
8307 auto_vec<bool> parm_complex (nargs + 1);
8308 auto_vec<bool> parm_varies (nargs + 1);
8309 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8310 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8311 parm_first.quick_push (first_ret);
8312 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8313 parm_varies.quick_push (false);
8314 function_args_iterator iter;
8315 tree t;
8316 unsigned int argpos;
8317 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8319 if (t == void_type_node)
8320 break;
8321 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8322 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8323 parm_varies.quick_push (false);
8325 for (unsigned int j = 1; j < num_functions; j++)
8327 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8328 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8329 if (ret != parm_first[0])
8331 parm_varies[0] = true;
8332 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8333 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8335 error_at ((*cexpr_list)[0].get_location (),
8336 "invalid type-generic return type for "
8337 "argument %u of %<__builtin_tgmath%>",
8339 expr.set_error ();
8340 goto out;
8342 if (!SCALAR_FLOAT_TYPE_P (ret)
8343 && !COMPLEX_FLOAT_TYPE_P (ret))
8345 error_at ((*cexpr_list)[j].get_location (),
8346 "invalid type-generic return type for "
8347 "argument %u of %<__builtin_tgmath%>",
8348 j + 1);
8349 expr.set_error ();
8350 goto out;
8353 if (TREE_CODE (ret) == COMPLEX_TYPE)
8354 parm_complex[0] = true;
8355 argpos = 1;
8356 FOREACH_FUNCTION_ARGS (type, t, iter)
8358 if (t == void_type_node)
8359 break;
8360 t = TYPE_MAIN_VARIANT (t);
8361 if (t != parm_first[argpos])
8363 parm_varies[argpos] = true;
8364 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8365 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8367 error_at ((*cexpr_list)[0].get_location (),
8368 "invalid type-generic type for "
8369 "argument %u of argument %u of "
8370 "%<__builtin_tgmath%>", argpos, 1);
8371 expr.set_error ();
8372 goto out;
8374 if (!SCALAR_FLOAT_TYPE_P (t)
8375 && !COMPLEX_FLOAT_TYPE_P (t))
8377 error_at ((*cexpr_list)[j].get_location (),
8378 "invalid type-generic type for "
8379 "argument %u of argument %u of "
8380 "%<__builtin_tgmath%>", argpos, j + 1);
8381 expr.set_error ();
8382 goto out;
8385 if (TREE_CODE (t) == COMPLEX_TYPE)
8386 parm_complex[argpos] = true;
8387 argpos++;
8390 enum tgmath_parm_kind max_variation = tgmath_fixed;
8391 for (unsigned int j = 0; j <= nargs; j++)
8393 enum tgmath_parm_kind this_kind;
8394 if (parm_varies[j])
8396 if (parm_complex[j])
8397 max_variation = this_kind = tgmath_complex;
8398 else
8400 this_kind = tgmath_real;
8401 if (max_variation != tgmath_complex)
8402 max_variation = tgmath_real;
8405 else
8406 this_kind = tgmath_fixed;
8407 parm_kind.quick_push (this_kind);
8409 if (max_variation == tgmath_fixed)
8411 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8412 "all have the same type");
8413 expr.set_error ();
8414 break;
8417 /* Identify a parameter (not the return type) that varies,
8418 including with complex types if any variation includes
8419 complex types; there must be at least one such
8420 parameter. */
8421 unsigned int tgarg = 0;
8422 for (unsigned int j = 1; j <= nargs; j++)
8423 if (parm_kind[j] == max_variation)
8425 tgarg = j;
8426 break;
8428 if (tgarg == 0)
8430 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8431 "lack type-generic parameter");
8432 expr.set_error ();
8433 break;
8436 /* Determine the type of the relevant parameter for each
8437 function. */
8438 auto_vec<tree> tg_type (num_functions);
8439 for (unsigned int j = 0; j < num_functions; j++)
8441 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8442 argpos = 1;
8443 FOREACH_FUNCTION_ARGS (type, t, iter)
8445 if (argpos == tgarg)
8447 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8448 break;
8450 argpos++;
8454 /* Verify that the corresponding types are different for
8455 all the listed functions. Also determine whether all
8456 the types are complex, whether all the types are
8457 standard or binary, and whether all the types are
8458 decimal. */
8459 bool all_complex = true;
8460 bool all_binary = true;
8461 bool all_decimal = true;
8462 hash_set<tree> tg_types;
8463 FOR_EACH_VEC_ELT (tg_type, i, t)
8465 if (TREE_CODE (t) == COMPLEX_TYPE)
8466 all_decimal = false;
8467 else
8469 all_complex = false;
8470 if (DECIMAL_FLOAT_TYPE_P (t))
8471 all_binary = false;
8472 else
8473 all_decimal = false;
8475 if (tg_types.add (t))
8477 error_at ((*cexpr_list)[i].get_location (),
8478 "duplicate type-generic parameter type for "
8479 "function argument %u of %<__builtin_tgmath%>",
8480 i + 1);
8481 expr.set_error ();
8482 goto out;
8486 /* Verify that other parameters and the return type whose
8487 types vary have their types varying in the correct
8488 way. */
8489 for (unsigned int j = 0; j < num_functions; j++)
8491 tree exp_type = tg_type[j];
8492 tree exp_real_type = exp_type;
8493 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8494 exp_real_type = TREE_TYPE (exp_type);
8495 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8496 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8497 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8498 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8500 error_at ((*cexpr_list)[j].get_location (),
8501 "bad return type for function argument %u "
8502 "of %<__builtin_tgmath%>", j + 1);
8503 expr.set_error ();
8504 goto out;
8506 argpos = 1;
8507 FOREACH_FUNCTION_ARGS (type, t, iter)
8509 if (t == void_type_node)
8510 break;
8511 t = TYPE_MAIN_VARIANT (t);
8512 if ((parm_kind[argpos] == tgmath_complex
8513 && t != exp_type)
8514 || (parm_kind[argpos] == tgmath_real
8515 && t != exp_real_type))
8517 error_at ((*cexpr_list)[j].get_location (),
8518 "bad type for argument %u of "
8519 "function argument %u of "
8520 "%<__builtin_tgmath%>", argpos, j + 1);
8521 expr.set_error ();
8522 goto out;
8524 argpos++;
8528 /* The functions listed are a valid set of functions for a
8529 <tgmath.h> macro to select between. Identify the
8530 matching function, if any. First, the argument types
8531 must be combined following <tgmath.h> rules. Integer
8532 types are treated as _Decimal64 if any type-generic
8533 argument is decimal, or if the only alternatives for
8534 type-generic arguments are of decimal types, and are
8535 otherwise treated as double (or _Complex double for
8536 complex integer types, or _Float64 or _Complex _Float64
8537 if all the return types are the same _FloatN or
8538 _FloatNx type). After that adjustment, types are
8539 combined following the usual arithmetic conversions.
8540 If the function only accepts complex arguments, a
8541 complex type is produced. */
8542 bool arg_complex = all_complex;
8543 bool arg_binary = all_binary;
8544 bool arg_int_decimal = all_decimal;
8545 for (unsigned int j = 1; j <= nargs; j++)
8547 if (parm_kind[j] == tgmath_fixed)
8548 continue;
8549 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8550 tree type = TREE_TYPE (ce->value);
8551 if (!INTEGRAL_TYPE_P (type)
8552 && !SCALAR_FLOAT_TYPE_P (type)
8553 && TREE_CODE (type) != COMPLEX_TYPE)
8555 error_at (ce->get_location (),
8556 "invalid type of argument %u of type-generic "
8557 "function", j);
8558 expr.set_error ();
8559 goto out;
8561 if (DECIMAL_FLOAT_TYPE_P (type))
8563 arg_int_decimal = true;
8564 if (all_complex)
8566 error_at (ce->get_location (),
8567 "decimal floating-point argument %u to "
8568 "complex-only type-generic function", j);
8569 expr.set_error ();
8570 goto out;
8572 else if (all_binary)
8574 error_at (ce->get_location (),
8575 "decimal floating-point argument %u to "
8576 "binary-only type-generic function", j);
8577 expr.set_error ();
8578 goto out;
8580 else if (arg_complex)
8582 error_at (ce->get_location (),
8583 "both complex and decimal floating-point "
8584 "arguments to type-generic function");
8585 expr.set_error ();
8586 goto out;
8588 else if (arg_binary)
8590 error_at (ce->get_location (),
8591 "both binary and decimal floating-point "
8592 "arguments to type-generic function");
8593 expr.set_error ();
8594 goto out;
8597 else if (TREE_CODE (type) == COMPLEX_TYPE)
8599 arg_complex = true;
8600 if (COMPLEX_FLOAT_TYPE_P (type))
8601 arg_binary = true;
8602 if (all_decimal)
8604 error_at (ce->get_location (),
8605 "complex argument %u to "
8606 "decimal-only type-generic function", j);
8607 expr.set_error ();
8608 goto out;
8610 else if (arg_int_decimal)
8612 error_at (ce->get_location (),
8613 "both complex and decimal floating-point "
8614 "arguments to type-generic function");
8615 expr.set_error ();
8616 goto out;
8619 else if (SCALAR_FLOAT_TYPE_P (type))
8621 arg_binary = true;
8622 if (all_decimal)
8624 error_at (ce->get_location (),
8625 "binary argument %u to "
8626 "decimal-only type-generic function", j);
8627 expr.set_error ();
8628 goto out;
8630 else if (arg_int_decimal)
8632 error_at (ce->get_location (),
8633 "both binary and decimal floating-point "
8634 "arguments to type-generic function");
8635 expr.set_error ();
8636 goto out;
8640 /* For a macro rounding its result to a narrower type, map
8641 integer types to _Float64 not double if the return type
8642 is a _FloatN or _FloatNx type. */
8643 bool arg_int_float64 = false;
8644 if (parm_kind[0] == tgmath_fixed
8645 && SCALAR_FLOAT_TYPE_P (parm_first[0])
8646 && float64_type_node != NULL_TREE)
8647 for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
8648 if (parm_first[0] == FLOATN_TYPE_NODE (j))
8650 arg_int_float64 = true;
8651 break;
8653 tree arg_real = NULL_TREE;
8654 for (unsigned int j = 1; j <= nargs; j++)
8656 if (parm_kind[j] == tgmath_fixed)
8657 continue;
8658 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8659 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8660 if (TREE_CODE (type) == COMPLEX_TYPE)
8661 type = TREE_TYPE (type);
8662 if (INTEGRAL_TYPE_P (type))
8663 type = (arg_int_decimal
8664 ? dfloat64_type_node
8665 : arg_int_float64
8666 ? float64_type_node
8667 : double_type_node);
8668 if (arg_real == NULL_TREE)
8669 arg_real = type;
8670 else
8671 arg_real = common_type (arg_real, type);
8672 if (arg_real == error_mark_node)
8674 expr.set_error ();
8675 goto out;
8678 tree arg_type = (arg_complex
8679 ? build_complex_type (arg_real)
8680 : arg_real);
8682 /* Look for a function to call with type-generic parameter
8683 type ARG_TYPE. */
8684 c_expr_t *fn = NULL;
8685 for (unsigned int j = 0; j < num_functions; j++)
8687 if (tg_type[j] == arg_type)
8689 fn = &(*cexpr_list)[j];
8690 break;
8693 if (fn == NULL
8694 && parm_kind[0] == tgmath_fixed
8695 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8697 /* Presume this is a macro that rounds its result to a
8698 narrower type, and look for the first function with
8699 at least the range and precision of the argument
8700 type. */
8701 for (unsigned int j = 0; j < num_functions; j++)
8703 if (arg_complex
8704 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8705 continue;
8706 tree real_tg_type = (arg_complex
8707 ? TREE_TYPE (tg_type[j])
8708 : tg_type[j]);
8709 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8710 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8711 continue;
8712 scalar_float_mode arg_mode
8713 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8714 scalar_float_mode tg_mode
8715 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8716 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8717 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8718 if (arg_fmt->b == tg_fmt->b
8719 && arg_fmt->p <= tg_fmt->p
8720 && arg_fmt->emax <= tg_fmt->emax
8721 && (arg_fmt->emin - arg_fmt->p
8722 >= tg_fmt->emin - tg_fmt->p))
8724 fn = &(*cexpr_list)[j];
8725 break;
8729 if (fn == NULL)
8731 error_at (loc, "no matching function for type-generic call");
8732 expr.set_error ();
8733 break;
8736 /* Construct a call to FN. */
8737 vec<tree, va_gc> *args;
8738 vec_alloc (args, nargs);
8739 vec<tree, va_gc> *origtypes;
8740 vec_alloc (origtypes, nargs);
8741 auto_vec<location_t> arg_loc (nargs);
8742 for (unsigned int j = 0; j < nargs; j++)
8744 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8745 args->quick_push (ce->value);
8746 arg_loc.quick_push (ce->get_location ());
8747 origtypes->quick_push (ce->original_type);
8749 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8750 args, origtypes);
8751 set_c_expr_source_range (&expr, loc, close_paren_loc);
8752 break;
8754 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8756 vec<c_expr_t, va_gc> *cexpr_list;
8757 c_expr_t *e2_p;
8758 tree chain_value;
8759 location_t close_paren_loc;
8761 c_parser_consume_token (parser);
8762 if (!c_parser_get_builtin_args (parser,
8763 "__builtin_call_with_static_chain",
8764 &cexpr_list, false,
8765 &close_paren_loc))
8767 expr.set_error ();
8768 break;
8770 if (vec_safe_length (cexpr_list) != 2)
8772 error_at (loc, "wrong number of arguments to "
8773 "%<__builtin_call_with_static_chain%>");
8774 expr.set_error ();
8775 break;
8778 expr = (*cexpr_list)[0];
8779 e2_p = &(*cexpr_list)[1];
8780 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8781 chain_value = e2_p->value;
8782 mark_exp_read (chain_value);
8784 if (TREE_CODE (expr.value) != CALL_EXPR)
8785 error_at (loc, "first argument to "
8786 "%<__builtin_call_with_static_chain%> "
8787 "must be a call expression");
8788 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8789 error_at (loc, "second argument to "
8790 "%<__builtin_call_with_static_chain%> "
8791 "must be a pointer type");
8792 else
8793 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8794 set_c_expr_source_range (&expr, loc, close_paren_loc);
8795 break;
8797 case RID_BUILTIN_COMPLEX:
8799 vec<c_expr_t, va_gc> *cexpr_list;
8800 c_expr_t *e1_p, *e2_p;
8801 location_t close_paren_loc;
8803 c_parser_consume_token (parser);
8804 if (!c_parser_get_builtin_args (parser,
8805 "__builtin_complex",
8806 &cexpr_list, false,
8807 &close_paren_loc))
8809 expr.set_error ();
8810 break;
8813 if (vec_safe_length (cexpr_list) != 2)
8815 error_at (loc, "wrong number of arguments to "
8816 "%<__builtin_complex%>");
8817 expr.set_error ();
8818 break;
8821 e1_p = &(*cexpr_list)[0];
8822 e2_p = &(*cexpr_list)[1];
8824 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8825 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8826 e1_p->value = convert (TREE_TYPE (e1_p->value),
8827 TREE_OPERAND (e1_p->value, 0));
8828 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8829 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8830 e2_p->value = convert (TREE_TYPE (e2_p->value),
8831 TREE_OPERAND (e2_p->value, 0));
8832 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8833 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8834 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8835 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8837 error_at (loc, "%<__builtin_complex%> operand "
8838 "not of real binary floating-point type");
8839 expr.set_error ();
8840 break;
8842 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8843 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8845 error_at (loc,
8846 "%<__builtin_complex%> operands of different types");
8847 expr.set_error ();
8848 break;
8850 pedwarn_c90 (loc, OPT_Wpedantic,
8851 "ISO C90 does not support complex types");
8852 expr.value = build2_loc (loc, COMPLEX_EXPR,
8853 build_complex_type
8854 (TYPE_MAIN_VARIANT
8855 (TREE_TYPE (e1_p->value))),
8856 e1_p->value, e2_p->value);
8857 set_c_expr_source_range (&expr, loc, close_paren_loc);
8858 break;
8860 case RID_BUILTIN_SHUFFLE:
8862 vec<c_expr_t, va_gc> *cexpr_list;
8863 unsigned int i;
8864 c_expr_t *p;
8865 location_t close_paren_loc;
8867 c_parser_consume_token (parser);
8868 if (!c_parser_get_builtin_args (parser,
8869 "__builtin_shuffle",
8870 &cexpr_list, false,
8871 &close_paren_loc))
8873 expr.set_error ();
8874 break;
8877 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8878 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8880 if (vec_safe_length (cexpr_list) == 2)
8881 expr.value =
8882 c_build_vec_perm_expr
8883 (loc, (*cexpr_list)[0].value,
8884 NULL_TREE, (*cexpr_list)[1].value);
8886 else if (vec_safe_length (cexpr_list) == 3)
8887 expr.value =
8888 c_build_vec_perm_expr
8889 (loc, (*cexpr_list)[0].value,
8890 (*cexpr_list)[1].value,
8891 (*cexpr_list)[2].value);
8892 else
8894 error_at (loc, "wrong number of arguments to "
8895 "%<__builtin_shuffle%>");
8896 expr.set_error ();
8898 set_c_expr_source_range (&expr, loc, close_paren_loc);
8899 break;
8901 case RID_AT_SELECTOR:
8903 gcc_assert (c_dialect_objc ());
8904 c_parser_consume_token (parser);
8905 matching_parens parens;
8906 if (!parens.require_open (parser))
8908 expr.set_error ();
8909 break;
8911 tree sel = c_parser_objc_selector_arg (parser);
8912 location_t close_loc = c_parser_peek_token (parser)->location;
8913 parens.skip_until_found_close (parser);
8914 expr.value = objc_build_selector_expr (loc, sel);
8915 set_c_expr_source_range (&expr, loc, close_loc);
8917 break;
8918 case RID_AT_PROTOCOL:
8920 gcc_assert (c_dialect_objc ());
8921 c_parser_consume_token (parser);
8922 matching_parens parens;
8923 if (!parens.require_open (parser))
8925 expr.set_error ();
8926 break;
8928 if (c_parser_next_token_is_not (parser, CPP_NAME))
8930 c_parser_error (parser, "expected identifier");
8931 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8932 expr.set_error ();
8933 break;
8935 tree id = c_parser_peek_token (parser)->value;
8936 c_parser_consume_token (parser);
8937 location_t close_loc = c_parser_peek_token (parser)->location;
8938 parens.skip_until_found_close (parser);
8939 expr.value = objc_build_protocol_expr (id);
8940 set_c_expr_source_range (&expr, loc, close_loc);
8942 break;
8943 case RID_AT_ENCODE:
8945 /* Extension to support C-structures in the archiver. */
8946 gcc_assert (c_dialect_objc ());
8947 c_parser_consume_token (parser);
8948 matching_parens parens;
8949 if (!parens.require_open (parser))
8951 expr.set_error ();
8952 break;
8954 t1 = c_parser_type_name (parser);
8955 if (t1 == NULL)
8957 expr.set_error ();
8958 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8959 break;
8961 location_t close_loc = c_parser_peek_token (parser)->location;
8962 parens.skip_until_found_close (parser);
8963 tree type = groktypename (t1, NULL, NULL);
8964 expr.value = objc_build_encode_expr (type);
8965 set_c_expr_source_range (&expr, loc, close_loc);
8967 break;
8968 case RID_GENERIC:
8969 expr = c_parser_generic_selection (parser);
8970 break;
8971 default:
8972 c_parser_error (parser, "expected expression");
8973 expr.set_error ();
8974 break;
8976 break;
8977 case CPP_OPEN_SQUARE:
8978 if (c_dialect_objc ())
8980 tree receiver, args;
8981 c_parser_consume_token (parser);
8982 receiver = c_parser_objc_receiver (parser);
8983 args = c_parser_objc_message_args (parser);
8984 location_t close_loc = c_parser_peek_token (parser)->location;
8985 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8986 "expected %<]%>");
8987 expr.value = objc_build_message_expr (receiver, args);
8988 set_c_expr_source_range (&expr, loc, close_loc);
8989 break;
8991 /* Else fall through to report error. */
8992 /* FALLTHRU */
8993 default:
8994 c_parser_error (parser, "expected expression");
8995 expr.set_error ();
8996 break;
8998 out:
8999 return c_parser_postfix_expression_after_primary
9000 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9003 /* Parse a postfix expression after a parenthesized type name: the
9004 brace-enclosed initializer of a compound literal, possibly followed
9005 by some postfix operators. This is separate because it is not
9006 possible to tell until after the type name whether a cast
9007 expression has a cast or a compound literal, or whether the operand
9008 of sizeof is a parenthesized type name or starts with a compound
9009 literal. TYPE_LOC is the location where TYPE_NAME starts--the
9010 location of the first token after the parentheses around the type
9011 name. */
9013 static struct c_expr
9014 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9015 struct c_type_name *type_name,
9016 location_t type_loc)
9018 tree type;
9019 struct c_expr init;
9020 bool non_const;
9021 struct c_expr expr;
9022 location_t start_loc;
9023 tree type_expr = NULL_TREE;
9024 bool type_expr_const = true;
9025 check_compound_literal_type (type_loc, type_name);
9026 rich_location richloc (line_table, type_loc);
9027 start_init (NULL_TREE, NULL, 0, &richloc);
9028 type = groktypename (type_name, &type_expr, &type_expr_const);
9029 start_loc = c_parser_peek_token (parser)->location;
9030 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9032 error_at (type_loc, "compound literal has variable size");
9033 type = error_mark_node;
9035 init = c_parser_braced_init (parser, type, false, NULL);
9036 finish_init ();
9037 maybe_warn_string_init (type_loc, type, init);
9039 if (type != error_mark_node
9040 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9041 && current_function_decl)
9043 error ("compound literal qualified by address-space qualifier");
9044 type = error_mark_node;
9047 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9048 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9049 ? CONSTRUCTOR_NON_CONST (init.value)
9050 : init.original_code == C_MAYBE_CONST_EXPR);
9051 non_const |= !type_expr_const;
9052 unsigned int alignas_align = 0;
9053 if (type != error_mark_node
9054 && type_name->specs->align_log != -1)
9056 alignas_align = 1U << type_name->specs->align_log;
9057 if (alignas_align < min_align_of_type (type))
9059 error_at (type_name->specs->locations[cdw_alignas],
9060 "%<_Alignas%> specifiers cannot reduce "
9061 "alignment of compound literal");
9062 alignas_align = 0;
9065 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9066 alignas_align);
9067 set_c_expr_source_range (&expr, init.src_range);
9068 expr.original_code = ERROR_MARK;
9069 expr.original_type = NULL;
9070 if (type != error_mark_node
9071 && expr.value != error_mark_node
9072 && type_expr)
9074 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9076 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9077 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9079 else
9081 gcc_assert (!non_const);
9082 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9083 type_expr, expr.value);
9086 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9089 /* Callback function for sizeof_pointer_memaccess_warning to compare
9090 types. */
9092 static bool
9093 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9095 return comptypes (type1, type2) == 1;
9098 /* Parse a postfix expression after the initial primary or compound
9099 literal; that is, parse a series of postfix operators.
9101 EXPR_LOC is the location of the primary expression. */
9103 static struct c_expr
9104 c_parser_postfix_expression_after_primary (c_parser *parser,
9105 location_t expr_loc,
9106 struct c_expr expr)
9108 struct c_expr orig_expr;
9109 tree ident, idx;
9110 location_t sizeof_arg_loc[3], comp_loc;
9111 tree sizeof_arg[3];
9112 unsigned int literal_zero_mask;
9113 unsigned int i;
9114 vec<tree, va_gc> *exprlist;
9115 vec<tree, va_gc> *origtypes = NULL;
9116 vec<location_t> arg_loc = vNULL;
9117 location_t start;
9118 location_t finish;
9120 while (true)
9122 location_t op_loc = c_parser_peek_token (parser)->location;
9123 switch (c_parser_peek_token (parser)->type)
9125 case CPP_OPEN_SQUARE:
9126 /* Array reference. */
9127 c_parser_consume_token (parser);
9128 idx = c_parser_expression (parser).value;
9129 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9130 "expected %<]%>");
9131 start = expr.get_start ();
9132 finish = parser->tokens_buf[0].location;
9133 expr.value = build_array_ref (op_loc, expr.value, idx);
9134 set_c_expr_source_range (&expr, start, finish);
9135 expr.original_code = ERROR_MARK;
9136 expr.original_type = NULL;
9137 break;
9138 case CPP_OPEN_PAREN:
9139 /* Function call. */
9140 c_parser_consume_token (parser);
9141 for (i = 0; i < 3; i++)
9143 sizeof_arg[i] = NULL_TREE;
9144 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9146 literal_zero_mask = 0;
9147 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9148 exprlist = NULL;
9149 else
9150 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9151 sizeof_arg_loc, sizeof_arg,
9152 &arg_loc, &literal_zero_mask);
9153 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9154 "expected %<)%>");
9155 orig_expr = expr;
9156 mark_exp_read (expr.value);
9157 if (warn_sizeof_pointer_memaccess)
9158 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9159 expr.value, exprlist,
9160 sizeof_arg,
9161 sizeof_ptr_memacc_comptypes);
9162 if (TREE_CODE (expr.value) == FUNCTION_DECL
9163 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9164 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9165 && vec_safe_length (exprlist) == 3)
9167 tree arg0 = (*exprlist)[0];
9168 tree arg2 = (*exprlist)[2];
9169 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9172 start = expr.get_start ();
9173 finish = parser->tokens_buf[0].get_finish ();
9174 expr.value
9175 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9176 exprlist, origtypes);
9177 set_c_expr_source_range (&expr, start, finish);
9179 expr.original_code = ERROR_MARK;
9180 if (TREE_CODE (expr.value) == INTEGER_CST
9181 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9182 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9183 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9184 expr.original_code = C_MAYBE_CONST_EXPR;
9185 expr.original_type = NULL;
9186 if (exprlist)
9188 release_tree_vector (exprlist);
9189 release_tree_vector (origtypes);
9191 arg_loc.release ();
9192 break;
9193 case CPP_DOT:
9194 /* Structure element reference. */
9195 c_parser_consume_token (parser);
9196 expr = default_function_array_conversion (expr_loc, expr);
9197 if (c_parser_next_token_is (parser, CPP_NAME))
9199 c_token *comp_tok = c_parser_peek_token (parser);
9200 ident = comp_tok->value;
9201 comp_loc = comp_tok->location;
9203 else
9205 c_parser_error (parser, "expected identifier");
9206 expr.set_error ();
9207 expr.original_code = ERROR_MARK;
9208 expr.original_type = NULL;
9209 return expr;
9211 start = expr.get_start ();
9212 finish = c_parser_peek_token (parser)->get_finish ();
9213 c_parser_consume_token (parser);
9214 expr.value = build_component_ref (op_loc, expr.value, ident,
9215 comp_loc);
9216 set_c_expr_source_range (&expr, start, finish);
9217 expr.original_code = ERROR_MARK;
9218 if (TREE_CODE (expr.value) != COMPONENT_REF)
9219 expr.original_type = NULL;
9220 else
9222 /* Remember the original type of a bitfield. */
9223 tree field = TREE_OPERAND (expr.value, 1);
9224 if (TREE_CODE (field) != FIELD_DECL)
9225 expr.original_type = NULL;
9226 else
9227 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9229 break;
9230 case CPP_DEREF:
9231 /* Structure element reference. */
9232 c_parser_consume_token (parser);
9233 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9234 if (c_parser_next_token_is (parser, CPP_NAME))
9236 c_token *comp_tok = c_parser_peek_token (parser);
9237 ident = comp_tok->value;
9238 comp_loc = comp_tok->location;
9240 else
9242 c_parser_error (parser, "expected identifier");
9243 expr.set_error ();
9244 expr.original_code = ERROR_MARK;
9245 expr.original_type = NULL;
9246 return expr;
9248 start = expr.get_start ();
9249 finish = c_parser_peek_token (parser)->get_finish ();
9250 c_parser_consume_token (parser);
9251 expr.value = build_component_ref (op_loc,
9252 build_indirect_ref (op_loc,
9253 expr.value,
9254 RO_ARROW),
9255 ident, comp_loc);
9256 set_c_expr_source_range (&expr, start, finish);
9257 expr.original_code = ERROR_MARK;
9258 if (TREE_CODE (expr.value) != COMPONENT_REF)
9259 expr.original_type = NULL;
9260 else
9262 /* Remember the original type of a bitfield. */
9263 tree field = TREE_OPERAND (expr.value, 1);
9264 if (TREE_CODE (field) != FIELD_DECL)
9265 expr.original_type = NULL;
9266 else
9267 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9269 break;
9270 case CPP_PLUS_PLUS:
9271 /* Postincrement. */
9272 start = expr.get_start ();
9273 finish = c_parser_peek_token (parser)->get_finish ();
9274 c_parser_consume_token (parser);
9275 expr = default_function_array_read_conversion (expr_loc, expr);
9276 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9277 expr.value, false);
9278 set_c_expr_source_range (&expr, start, finish);
9279 expr.original_code = ERROR_MARK;
9280 expr.original_type = NULL;
9281 break;
9282 case CPP_MINUS_MINUS:
9283 /* Postdecrement. */
9284 start = expr.get_start ();
9285 finish = c_parser_peek_token (parser)->get_finish ();
9286 c_parser_consume_token (parser);
9287 expr = default_function_array_read_conversion (expr_loc, expr);
9288 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9289 expr.value, false);
9290 set_c_expr_source_range (&expr, start, finish);
9291 expr.original_code = ERROR_MARK;
9292 expr.original_type = NULL;
9293 break;
9294 default:
9295 return expr;
9300 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9302 expression:
9303 assignment-expression
9304 expression , assignment-expression
9307 static struct c_expr
9308 c_parser_expression (c_parser *parser)
9310 location_t tloc = c_parser_peek_token (parser)->location;
9311 struct c_expr expr;
9312 expr = c_parser_expr_no_commas (parser, NULL);
9313 if (c_parser_next_token_is (parser, CPP_COMMA))
9314 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9315 while (c_parser_next_token_is (parser, CPP_COMMA))
9317 struct c_expr next;
9318 tree lhsval;
9319 location_t loc = c_parser_peek_token (parser)->location;
9320 location_t expr_loc;
9321 c_parser_consume_token (parser);
9322 expr_loc = c_parser_peek_token (parser)->location;
9323 lhsval = expr.value;
9324 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9325 lhsval = TREE_OPERAND (lhsval, 1);
9326 if (DECL_P (lhsval) || handled_component_p (lhsval))
9327 mark_exp_read (lhsval);
9328 next = c_parser_expr_no_commas (parser, NULL);
9329 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9330 expr.value = build_compound_expr (loc, expr.value, next.value);
9331 expr.original_code = COMPOUND_EXPR;
9332 expr.original_type = next.original_type;
9334 return expr;
9337 /* Parse an expression and convert functions or arrays to pointers and
9338 lvalues to rvalues. */
9340 static struct c_expr
9341 c_parser_expression_conv (c_parser *parser)
9343 struct c_expr expr;
9344 location_t loc = c_parser_peek_token (parser)->location;
9345 expr = c_parser_expression (parser);
9346 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9347 return expr;
9350 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9351 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9353 static inline void
9354 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9355 unsigned int idx)
9357 if (idx >= HOST_BITS_PER_INT)
9358 return;
9360 c_token *tok = c_parser_peek_token (parser);
9361 switch (tok->type)
9363 case CPP_NUMBER:
9364 case CPP_CHAR:
9365 case CPP_WCHAR:
9366 case CPP_CHAR16:
9367 case CPP_CHAR32:
9368 /* If a parameter is literal zero alone, remember it
9369 for -Wmemset-transposed-args warning. */
9370 if (integer_zerop (tok->value)
9371 && !TREE_OVERFLOW (tok->value)
9372 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9373 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9374 *literal_zero_mask |= 1U << idx;
9375 default:
9376 break;
9380 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9381 functions and arrays to pointers and lvalues to rvalues. If
9382 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9383 locations of function arguments into this vector.
9385 nonempty-expr-list:
9386 assignment-expression
9387 nonempty-expr-list , assignment-expression
9390 static vec<tree, va_gc> *
9391 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9392 vec<tree, va_gc> **p_orig_types,
9393 location_t *sizeof_arg_loc, tree *sizeof_arg,
9394 vec<location_t> *locations,
9395 unsigned int *literal_zero_mask)
9397 vec<tree, va_gc> *ret;
9398 vec<tree, va_gc> *orig_types;
9399 struct c_expr expr;
9400 unsigned int idx = 0;
9402 ret = make_tree_vector ();
9403 if (p_orig_types == NULL)
9404 orig_types = NULL;
9405 else
9406 orig_types = make_tree_vector ();
9408 if (literal_zero_mask)
9409 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9410 expr = c_parser_expr_no_commas (parser, NULL);
9411 if (convert_p)
9412 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9413 if (fold_p)
9414 expr.value = c_fully_fold (expr.value, false, NULL);
9415 ret->quick_push (expr.value);
9416 if (orig_types)
9417 orig_types->quick_push (expr.original_type);
9418 if (locations)
9419 locations->safe_push (expr.get_location ());
9420 if (sizeof_arg != NULL
9421 && expr.original_code == SIZEOF_EXPR)
9423 sizeof_arg[0] = c_last_sizeof_arg;
9424 sizeof_arg_loc[0] = c_last_sizeof_loc;
9426 while (c_parser_next_token_is (parser, CPP_COMMA))
9428 c_parser_consume_token (parser);
9429 if (literal_zero_mask)
9430 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9431 expr = c_parser_expr_no_commas (parser, NULL);
9432 if (convert_p)
9433 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9434 true);
9435 if (fold_p)
9436 expr.value = c_fully_fold (expr.value, false, NULL);
9437 vec_safe_push (ret, expr.value);
9438 if (orig_types)
9439 vec_safe_push (orig_types, expr.original_type);
9440 if (locations)
9441 locations->safe_push (expr.get_location ());
9442 if (++idx < 3
9443 && sizeof_arg != NULL
9444 && expr.original_code == SIZEOF_EXPR)
9446 sizeof_arg[idx] = c_last_sizeof_arg;
9447 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9450 if (orig_types)
9451 *p_orig_types = orig_types;
9452 return ret;
9455 /* Parse Objective-C-specific constructs. */
9457 /* Parse an objc-class-definition.
9459 objc-class-definition:
9460 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9461 objc-class-instance-variables[opt] objc-methodprotolist @end
9462 @implementation identifier objc-superclass[opt]
9463 objc-class-instance-variables[opt]
9464 @interface identifier ( identifier ) objc-protocol-refs[opt]
9465 objc-methodprotolist @end
9466 @interface identifier ( ) objc-protocol-refs[opt]
9467 objc-methodprotolist @end
9468 @implementation identifier ( identifier )
9470 objc-superclass:
9471 : identifier
9473 "@interface identifier (" must start "@interface identifier (
9474 identifier ) ...": objc-methodprotolist in the first production may
9475 not start with a parenthesized identifier as a declarator of a data
9476 definition with no declaration specifiers if the objc-superclass,
9477 objc-protocol-refs and objc-class-instance-variables are omitted. */
9479 static void
9480 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9482 bool iface_p;
9483 tree id1;
9484 tree superclass;
9485 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9486 iface_p = true;
9487 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9488 iface_p = false;
9489 else
9490 gcc_unreachable ();
9492 c_parser_consume_token (parser);
9493 if (c_parser_next_token_is_not (parser, CPP_NAME))
9495 c_parser_error (parser, "expected identifier");
9496 return;
9498 id1 = c_parser_peek_token (parser)->value;
9499 c_parser_consume_token (parser);
9500 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9502 /* We have a category or class extension. */
9503 tree id2;
9504 tree proto = NULL_TREE;
9505 matching_parens parens;
9506 parens.consume_open (parser);
9507 if (c_parser_next_token_is_not (parser, CPP_NAME))
9509 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9511 /* We have a class extension. */
9512 id2 = NULL_TREE;
9514 else
9516 c_parser_error (parser, "expected identifier or %<)%>");
9517 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9518 return;
9521 else
9523 id2 = c_parser_peek_token (parser)->value;
9524 c_parser_consume_token (parser);
9526 parens.skip_until_found_close (parser);
9527 if (!iface_p)
9529 objc_start_category_implementation (id1, id2);
9530 return;
9532 if (c_parser_next_token_is (parser, CPP_LESS))
9533 proto = c_parser_objc_protocol_refs (parser);
9534 objc_start_category_interface (id1, id2, proto, attributes);
9535 c_parser_objc_methodprotolist (parser);
9536 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9537 objc_finish_interface ();
9538 return;
9540 if (c_parser_next_token_is (parser, CPP_COLON))
9542 c_parser_consume_token (parser);
9543 if (c_parser_next_token_is_not (parser, CPP_NAME))
9545 c_parser_error (parser, "expected identifier");
9546 return;
9548 superclass = c_parser_peek_token (parser)->value;
9549 c_parser_consume_token (parser);
9551 else
9552 superclass = NULL_TREE;
9553 if (iface_p)
9555 tree proto = NULL_TREE;
9556 if (c_parser_next_token_is (parser, CPP_LESS))
9557 proto = c_parser_objc_protocol_refs (parser);
9558 objc_start_class_interface (id1, superclass, proto, attributes);
9560 else
9561 objc_start_class_implementation (id1, superclass);
9562 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9563 c_parser_objc_class_instance_variables (parser);
9564 if (iface_p)
9566 objc_continue_interface ();
9567 c_parser_objc_methodprotolist (parser);
9568 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9569 objc_finish_interface ();
9571 else
9573 objc_continue_implementation ();
9574 return;
9578 /* Parse objc-class-instance-variables.
9580 objc-class-instance-variables:
9581 { objc-instance-variable-decl-list[opt] }
9583 objc-instance-variable-decl-list:
9584 objc-visibility-spec
9585 objc-instance-variable-decl ;
9587 objc-instance-variable-decl-list objc-visibility-spec
9588 objc-instance-variable-decl-list objc-instance-variable-decl ;
9589 objc-instance-variable-decl-list ;
9591 objc-visibility-spec:
9592 @private
9593 @protected
9594 @public
9596 objc-instance-variable-decl:
9597 struct-declaration
9600 static void
9601 c_parser_objc_class_instance_variables (c_parser *parser)
9603 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9604 c_parser_consume_token (parser);
9605 while (c_parser_next_token_is_not (parser, CPP_EOF))
9607 tree decls;
9608 /* Parse any stray semicolon. */
9609 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9611 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9612 "extra semicolon");
9613 c_parser_consume_token (parser);
9614 continue;
9616 /* Stop if at the end of the instance variables. */
9617 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9619 c_parser_consume_token (parser);
9620 break;
9622 /* Parse any objc-visibility-spec. */
9623 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9625 c_parser_consume_token (parser);
9626 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9627 continue;
9629 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9631 c_parser_consume_token (parser);
9632 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9633 continue;
9635 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9637 c_parser_consume_token (parser);
9638 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9639 continue;
9641 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9643 c_parser_consume_token (parser);
9644 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9645 continue;
9647 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9649 c_parser_pragma (parser, pragma_external, NULL);
9650 continue;
9653 /* Parse some comma-separated declarations. */
9654 decls = c_parser_struct_declaration (parser);
9655 if (decls == NULL)
9657 /* There is a syntax error. We want to skip the offending
9658 tokens up to the next ';' (included) or '}'
9659 (excluded). */
9661 /* First, skip manually a ')' or ']'. This is because they
9662 reduce the nesting level, so c_parser_skip_until_found()
9663 wouldn't be able to skip past them. */
9664 c_token *token = c_parser_peek_token (parser);
9665 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9666 c_parser_consume_token (parser);
9668 /* Then, do the standard skipping. */
9669 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9671 /* We hopefully recovered. Start normal parsing again. */
9672 parser->error = false;
9673 continue;
9675 else
9677 /* Comma-separated instance variables are chained together
9678 in reverse order; add them one by one. */
9679 tree ivar = nreverse (decls);
9680 for (; ivar; ivar = DECL_CHAIN (ivar))
9681 objc_add_instance_variable (copy_node (ivar));
9683 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9687 /* Parse an objc-class-declaration.
9689 objc-class-declaration:
9690 @class identifier-list ;
9693 static void
9694 c_parser_objc_class_declaration (c_parser *parser)
9696 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9697 c_parser_consume_token (parser);
9698 /* Any identifiers, including those declared as type names, are OK
9699 here. */
9700 while (true)
9702 tree id;
9703 if (c_parser_next_token_is_not (parser, CPP_NAME))
9705 c_parser_error (parser, "expected identifier");
9706 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9707 parser->error = false;
9708 return;
9710 id = c_parser_peek_token (parser)->value;
9711 objc_declare_class (id);
9712 c_parser_consume_token (parser);
9713 if (c_parser_next_token_is (parser, CPP_COMMA))
9714 c_parser_consume_token (parser);
9715 else
9716 break;
9718 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9721 /* Parse an objc-alias-declaration.
9723 objc-alias-declaration:
9724 @compatibility_alias identifier identifier ;
9727 static void
9728 c_parser_objc_alias_declaration (c_parser *parser)
9730 tree id1, id2;
9731 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9732 c_parser_consume_token (parser);
9733 if (c_parser_next_token_is_not (parser, CPP_NAME))
9735 c_parser_error (parser, "expected identifier");
9736 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9737 return;
9739 id1 = c_parser_peek_token (parser)->value;
9740 c_parser_consume_token (parser);
9741 if (c_parser_next_token_is_not (parser, CPP_NAME))
9743 c_parser_error (parser, "expected identifier");
9744 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9745 return;
9747 id2 = c_parser_peek_token (parser)->value;
9748 c_parser_consume_token (parser);
9749 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9750 objc_declare_alias (id1, id2);
9753 /* Parse an objc-protocol-definition.
9755 objc-protocol-definition:
9756 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9757 @protocol identifier-list ;
9759 "@protocol identifier ;" should be resolved as "@protocol
9760 identifier-list ;": objc-methodprotolist may not start with a
9761 semicolon in the first alternative if objc-protocol-refs are
9762 omitted. */
9764 static void
9765 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9767 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9769 c_parser_consume_token (parser);
9770 if (c_parser_next_token_is_not (parser, CPP_NAME))
9772 c_parser_error (parser, "expected identifier");
9773 return;
9775 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9776 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9778 /* Any identifiers, including those declared as type names, are
9779 OK here. */
9780 while (true)
9782 tree id;
9783 if (c_parser_next_token_is_not (parser, CPP_NAME))
9785 c_parser_error (parser, "expected identifier");
9786 break;
9788 id = c_parser_peek_token (parser)->value;
9789 objc_declare_protocol (id, attributes);
9790 c_parser_consume_token (parser);
9791 if (c_parser_next_token_is (parser, CPP_COMMA))
9792 c_parser_consume_token (parser);
9793 else
9794 break;
9796 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9798 else
9800 tree id = c_parser_peek_token (parser)->value;
9801 tree proto = NULL_TREE;
9802 c_parser_consume_token (parser);
9803 if (c_parser_next_token_is (parser, CPP_LESS))
9804 proto = c_parser_objc_protocol_refs (parser);
9805 parser->objc_pq_context = true;
9806 objc_start_protocol (id, proto, attributes);
9807 c_parser_objc_methodprotolist (parser);
9808 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9809 parser->objc_pq_context = false;
9810 objc_finish_interface ();
9814 /* Parse an objc-method-type.
9816 objc-method-type:
9820 Return true if it is a class method (+) and false if it is
9821 an instance method (-).
9823 static inline bool
9824 c_parser_objc_method_type (c_parser *parser)
9826 switch (c_parser_peek_token (parser)->type)
9828 case CPP_PLUS:
9829 c_parser_consume_token (parser);
9830 return true;
9831 case CPP_MINUS:
9832 c_parser_consume_token (parser);
9833 return false;
9834 default:
9835 gcc_unreachable ();
9839 /* Parse an objc-method-definition.
9841 objc-method-definition:
9842 objc-method-type objc-method-decl ;[opt] compound-statement
9845 static void
9846 c_parser_objc_method_definition (c_parser *parser)
9848 bool is_class_method = c_parser_objc_method_type (parser);
9849 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9850 parser->objc_pq_context = true;
9851 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9852 &expr);
9853 if (decl == error_mark_node)
9854 return; /* Bail here. */
9856 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9858 c_parser_consume_token (parser);
9859 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9860 "extra semicolon in method definition specified");
9863 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9865 c_parser_error (parser, "expected %<{%>");
9866 return;
9869 parser->objc_pq_context = false;
9870 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9872 add_stmt (c_parser_compound_statement (parser));
9873 objc_finish_method_definition (current_function_decl);
9875 else
9877 /* This code is executed when we find a method definition
9878 outside of an @implementation context (or invalid for other
9879 reasons). Parse the method (to keep going) but do not emit
9880 any code.
9882 c_parser_compound_statement (parser);
9886 /* Parse an objc-methodprotolist.
9888 objc-methodprotolist:
9889 empty
9890 objc-methodprotolist objc-methodproto
9891 objc-methodprotolist declaration
9892 objc-methodprotolist ;
9893 @optional
9894 @required
9896 The declaration is a data definition, which may be missing
9897 declaration specifiers under the same rules and diagnostics as
9898 other data definitions outside functions, and the stray semicolon
9899 is diagnosed the same way as a stray semicolon outside a
9900 function. */
9902 static void
9903 c_parser_objc_methodprotolist (c_parser *parser)
9905 while (true)
9907 /* The list is terminated by @end. */
9908 switch (c_parser_peek_token (parser)->type)
9910 case CPP_SEMICOLON:
9911 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9912 "ISO C does not allow extra %<;%> outside of a function");
9913 c_parser_consume_token (parser);
9914 break;
9915 case CPP_PLUS:
9916 case CPP_MINUS:
9917 c_parser_objc_methodproto (parser);
9918 break;
9919 case CPP_PRAGMA:
9920 c_parser_pragma (parser, pragma_external, NULL);
9921 break;
9922 case CPP_EOF:
9923 return;
9924 default:
9925 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9926 return;
9927 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9928 c_parser_objc_at_property_declaration (parser);
9929 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9931 objc_set_method_opt (true);
9932 c_parser_consume_token (parser);
9934 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9936 objc_set_method_opt (false);
9937 c_parser_consume_token (parser);
9939 else
9940 c_parser_declaration_or_fndef (parser, false, false, true,
9941 false, true, NULL, vNULL);
9942 break;
9947 /* Parse an objc-methodproto.
9949 objc-methodproto:
9950 objc-method-type objc-method-decl ;
9953 static void
9954 c_parser_objc_methodproto (c_parser *parser)
9956 bool is_class_method = c_parser_objc_method_type (parser);
9957 tree decl, attributes = NULL_TREE;
9959 /* Remember protocol qualifiers in prototypes. */
9960 parser->objc_pq_context = true;
9961 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9962 NULL);
9963 /* Forget protocol qualifiers now. */
9964 parser->objc_pq_context = false;
9966 /* Do not allow the presence of attributes to hide an erroneous
9967 method implementation in the interface section. */
9968 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9970 c_parser_error (parser, "expected %<;%>");
9971 return;
9974 if (decl != error_mark_node)
9975 objc_add_method_declaration (is_class_method, decl, attributes);
9977 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9980 /* If we are at a position that method attributes may be present, check that
9981 there are not any parsed already (a syntax error) and then collect any
9982 specified at the current location. Finally, if new attributes were present,
9983 check that the next token is legal ( ';' for decls and '{' for defs). */
9985 static bool
9986 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9988 bool bad = false;
9989 if (*attributes)
9991 c_parser_error (parser,
9992 "method attributes must be specified at the end only");
9993 *attributes = NULL_TREE;
9994 bad = true;
9997 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9998 *attributes = c_parser_attributes (parser);
10000 /* If there were no attributes here, just report any earlier error. */
10001 if (*attributes == NULL_TREE || bad)
10002 return bad;
10004 /* If the attributes are followed by a ; or {, then just report any earlier
10005 error. */
10006 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10007 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10008 return bad;
10010 /* We've got attributes, but not at the end. */
10011 c_parser_error (parser,
10012 "expected %<;%> or %<{%> after method attribute definition");
10013 return true;
10016 /* Parse an objc-method-decl.
10018 objc-method-decl:
10019 ( objc-type-name ) objc-selector
10020 objc-selector
10021 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10022 objc-keyword-selector objc-optparmlist
10023 attributes
10025 objc-keyword-selector:
10026 objc-keyword-decl
10027 objc-keyword-selector objc-keyword-decl
10029 objc-keyword-decl:
10030 objc-selector : ( objc-type-name ) identifier
10031 objc-selector : identifier
10032 : ( objc-type-name ) identifier
10033 : identifier
10035 objc-optparmlist:
10036 objc-optparms objc-optellipsis
10038 objc-optparms:
10039 empty
10040 objc-opt-parms , parameter-declaration
10042 objc-optellipsis:
10043 empty
10044 , ...
10047 static tree
10048 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10049 tree *attributes, tree *expr)
10051 tree type = NULL_TREE;
10052 tree sel;
10053 tree parms = NULL_TREE;
10054 bool ellipsis = false;
10055 bool attr_err = false;
10057 *attributes = NULL_TREE;
10058 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10060 matching_parens parens;
10061 parens.consume_open (parser);
10062 type = c_parser_objc_type_name (parser);
10063 parens.skip_until_found_close (parser);
10065 sel = c_parser_objc_selector (parser);
10066 /* If there is no selector, or a colon follows, we have an
10067 objc-keyword-selector. If there is a selector, and a colon does
10068 not follow, that selector ends the objc-method-decl. */
10069 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10071 tree tsel = sel;
10072 tree list = NULL_TREE;
10073 while (true)
10075 tree atype = NULL_TREE, id, keyworddecl;
10076 tree param_attr = NULL_TREE;
10077 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10078 break;
10079 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10081 c_parser_consume_token (parser);
10082 atype = c_parser_objc_type_name (parser);
10083 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10084 "expected %<)%>");
10086 /* New ObjC allows attributes on method parameters. */
10087 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10088 param_attr = c_parser_attributes (parser);
10089 if (c_parser_next_token_is_not (parser, CPP_NAME))
10091 c_parser_error (parser, "expected identifier");
10092 return error_mark_node;
10094 id = c_parser_peek_token (parser)->value;
10095 c_parser_consume_token (parser);
10096 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10097 list = chainon (list, keyworddecl);
10098 tsel = c_parser_objc_selector (parser);
10099 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10100 break;
10103 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10105 /* Parse the optional parameter list. Optional Objective-C
10106 method parameters follow the C syntax, and may include '...'
10107 to denote a variable number of arguments. */
10108 parms = make_node (TREE_LIST);
10109 while (c_parser_next_token_is (parser, CPP_COMMA))
10111 struct c_parm *parm;
10112 c_parser_consume_token (parser);
10113 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10115 ellipsis = true;
10116 c_parser_consume_token (parser);
10117 attr_err |= c_parser_objc_maybe_method_attributes
10118 (parser, attributes) ;
10119 break;
10121 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10122 if (parm == NULL)
10123 break;
10124 parms = chainon (parms,
10125 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10127 sel = list;
10129 else
10130 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10132 if (sel == NULL)
10134 c_parser_error (parser, "objective-c method declaration is expected");
10135 return error_mark_node;
10138 if (attr_err)
10139 return error_mark_node;
10141 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10144 /* Parse an objc-type-name.
10146 objc-type-name:
10147 objc-type-qualifiers[opt] type-name
10148 objc-type-qualifiers[opt]
10150 objc-type-qualifiers:
10151 objc-type-qualifier
10152 objc-type-qualifiers objc-type-qualifier
10154 objc-type-qualifier: one of
10155 in out inout bycopy byref oneway
10158 static tree
10159 c_parser_objc_type_name (c_parser *parser)
10161 tree quals = NULL_TREE;
10162 struct c_type_name *type_name = NULL;
10163 tree type = NULL_TREE;
10164 while (true)
10166 c_token *token = c_parser_peek_token (parser);
10167 if (token->type == CPP_KEYWORD
10168 && (token->keyword == RID_IN
10169 || token->keyword == RID_OUT
10170 || token->keyword == RID_INOUT
10171 || token->keyword == RID_BYCOPY
10172 || token->keyword == RID_BYREF
10173 || token->keyword == RID_ONEWAY))
10175 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10176 c_parser_consume_token (parser);
10178 else
10179 break;
10181 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10182 type_name = c_parser_type_name (parser);
10183 if (type_name)
10184 type = groktypename (type_name, NULL, NULL);
10186 /* If the type is unknown, and error has already been produced and
10187 we need to recover from the error. In that case, use NULL_TREE
10188 for the type, as if no type had been specified; this will use the
10189 default type ('id') which is good for error recovery. */
10190 if (type == error_mark_node)
10191 type = NULL_TREE;
10193 return build_tree_list (quals, type);
10196 /* Parse objc-protocol-refs.
10198 objc-protocol-refs:
10199 < identifier-list >
10202 static tree
10203 c_parser_objc_protocol_refs (c_parser *parser)
10205 tree list = NULL_TREE;
10206 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10207 c_parser_consume_token (parser);
10208 /* Any identifiers, including those declared as type names, are OK
10209 here. */
10210 while (true)
10212 tree id;
10213 if (c_parser_next_token_is_not (parser, CPP_NAME))
10215 c_parser_error (parser, "expected identifier");
10216 break;
10218 id = c_parser_peek_token (parser)->value;
10219 list = chainon (list, build_tree_list (NULL_TREE, id));
10220 c_parser_consume_token (parser);
10221 if (c_parser_next_token_is (parser, CPP_COMMA))
10222 c_parser_consume_token (parser);
10223 else
10224 break;
10226 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10227 return list;
10230 /* Parse an objc-try-catch-finally-statement.
10232 objc-try-catch-finally-statement:
10233 @try compound-statement objc-catch-list[opt]
10234 @try compound-statement objc-catch-list[opt] @finally compound-statement
10236 objc-catch-list:
10237 @catch ( objc-catch-parameter-declaration ) compound-statement
10238 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10240 objc-catch-parameter-declaration:
10241 parameter-declaration
10242 '...'
10244 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10246 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10247 for C++. Keep them in sync. */
10249 static void
10250 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10252 location_t location;
10253 tree stmt;
10255 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10256 c_parser_consume_token (parser);
10257 location = c_parser_peek_token (parser)->location;
10258 objc_maybe_warn_exceptions (location);
10259 stmt = c_parser_compound_statement (parser);
10260 objc_begin_try_stmt (location, stmt);
10262 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10264 struct c_parm *parm;
10265 tree parameter_declaration = error_mark_node;
10266 bool seen_open_paren = false;
10268 c_parser_consume_token (parser);
10269 matching_parens parens;
10270 if (!parens.require_open (parser))
10271 seen_open_paren = true;
10272 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10274 /* We have "@catch (...)" (where the '...' are literally
10275 what is in the code). Skip the '...'.
10276 parameter_declaration is set to NULL_TREE, and
10277 objc_being_catch_clauses() knows that that means
10278 '...'. */
10279 c_parser_consume_token (parser);
10280 parameter_declaration = NULL_TREE;
10282 else
10284 /* We have "@catch (NSException *exception)" or something
10285 like that. Parse the parameter declaration. */
10286 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10287 if (parm == NULL)
10288 parameter_declaration = error_mark_node;
10289 else
10290 parameter_declaration = grokparm (parm, NULL);
10292 if (seen_open_paren)
10293 parens.require_close (parser);
10294 else
10296 /* If there was no open parenthesis, we are recovering from
10297 an error, and we are trying to figure out what mistake
10298 the user has made. */
10300 /* If there is an immediate closing parenthesis, the user
10301 probably forgot the opening one (ie, they typed "@catch
10302 NSException *e)". Parse the closing parenthesis and keep
10303 going. */
10304 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10305 c_parser_consume_token (parser);
10307 /* If these is no immediate closing parenthesis, the user
10308 probably doesn't know that parenthesis are required at
10309 all (ie, they typed "@catch NSException *e"). So, just
10310 forget about the closing parenthesis and keep going. */
10312 objc_begin_catch_clause (parameter_declaration);
10313 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10314 c_parser_compound_statement_nostart (parser);
10315 objc_finish_catch_clause ();
10317 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10319 c_parser_consume_token (parser);
10320 location = c_parser_peek_token (parser)->location;
10321 stmt = c_parser_compound_statement (parser);
10322 objc_build_finally_clause (location, stmt);
10324 objc_finish_try_stmt ();
10327 /* Parse an objc-synchronized-statement.
10329 objc-synchronized-statement:
10330 @synchronized ( expression ) compound-statement
10333 static void
10334 c_parser_objc_synchronized_statement (c_parser *parser)
10336 location_t loc;
10337 tree expr, stmt;
10338 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10339 c_parser_consume_token (parser);
10340 loc = c_parser_peek_token (parser)->location;
10341 objc_maybe_warn_exceptions (loc);
10342 matching_parens parens;
10343 if (parens.require_open (parser))
10345 struct c_expr ce = c_parser_expression (parser);
10346 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10347 expr = ce.value;
10348 expr = c_fully_fold (expr, false, NULL);
10349 parens.skip_until_found_close (parser);
10351 else
10352 expr = error_mark_node;
10353 stmt = c_parser_compound_statement (parser);
10354 objc_build_synchronized (loc, expr, stmt);
10357 /* Parse an objc-selector; return NULL_TREE without an error if the
10358 next token is not an objc-selector.
10360 objc-selector:
10361 identifier
10362 one of
10363 enum struct union if else while do for switch case default
10364 break continue return goto asm sizeof typeof __alignof
10365 unsigned long const short volatile signed restrict _Complex
10366 in out inout bycopy byref oneway int char float double void _Bool
10367 _Atomic
10369 ??? Why this selection of keywords but not, for example, storage
10370 class specifiers? */
10372 static tree
10373 c_parser_objc_selector (c_parser *parser)
10375 c_token *token = c_parser_peek_token (parser);
10376 tree value = token->value;
10377 if (token->type == CPP_NAME)
10379 c_parser_consume_token (parser);
10380 return value;
10382 if (token->type != CPP_KEYWORD)
10383 return NULL_TREE;
10384 switch (token->keyword)
10386 case RID_ENUM:
10387 case RID_STRUCT:
10388 case RID_UNION:
10389 case RID_IF:
10390 case RID_ELSE:
10391 case RID_WHILE:
10392 case RID_DO:
10393 case RID_FOR:
10394 case RID_SWITCH:
10395 case RID_CASE:
10396 case RID_DEFAULT:
10397 case RID_BREAK:
10398 case RID_CONTINUE:
10399 case RID_RETURN:
10400 case RID_GOTO:
10401 case RID_ASM:
10402 case RID_SIZEOF:
10403 case RID_TYPEOF:
10404 case RID_ALIGNOF:
10405 case RID_UNSIGNED:
10406 case RID_LONG:
10407 case RID_CONST:
10408 case RID_SHORT:
10409 case RID_VOLATILE:
10410 case RID_SIGNED:
10411 case RID_RESTRICT:
10412 case RID_COMPLEX:
10413 case RID_IN:
10414 case RID_OUT:
10415 case RID_INOUT:
10416 case RID_BYCOPY:
10417 case RID_BYREF:
10418 case RID_ONEWAY:
10419 case RID_INT:
10420 case RID_CHAR:
10421 case RID_FLOAT:
10422 case RID_DOUBLE:
10423 CASE_RID_FLOATN_NX:
10424 case RID_VOID:
10425 case RID_BOOL:
10426 case RID_ATOMIC:
10427 case RID_AUTO_TYPE:
10428 case RID_INT_N_0:
10429 case RID_INT_N_1:
10430 case RID_INT_N_2:
10431 case RID_INT_N_3:
10432 c_parser_consume_token (parser);
10433 return value;
10434 default:
10435 return NULL_TREE;
10439 /* Parse an objc-selector-arg.
10441 objc-selector-arg:
10442 objc-selector
10443 objc-keywordname-list
10445 objc-keywordname-list:
10446 objc-keywordname
10447 objc-keywordname-list objc-keywordname
10449 objc-keywordname:
10450 objc-selector :
10454 static tree
10455 c_parser_objc_selector_arg (c_parser *parser)
10457 tree sel = c_parser_objc_selector (parser);
10458 tree list = NULL_TREE;
10459 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10460 return sel;
10461 while (true)
10463 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10464 return list;
10465 list = chainon (list, build_tree_list (sel, NULL_TREE));
10466 sel = c_parser_objc_selector (parser);
10467 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10468 break;
10470 return list;
10473 /* Parse an objc-receiver.
10475 objc-receiver:
10476 expression
10477 class-name
10478 type-name
10481 static tree
10482 c_parser_objc_receiver (c_parser *parser)
10484 location_t loc = c_parser_peek_token (parser)->location;
10486 if (c_parser_peek_token (parser)->type == CPP_NAME
10487 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10488 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10490 tree id = c_parser_peek_token (parser)->value;
10491 c_parser_consume_token (parser);
10492 return objc_get_class_reference (id);
10494 struct c_expr ce = c_parser_expression (parser);
10495 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10496 return c_fully_fold (ce.value, false, NULL);
10499 /* Parse objc-message-args.
10501 objc-message-args:
10502 objc-selector
10503 objc-keywordarg-list
10505 objc-keywordarg-list:
10506 objc-keywordarg
10507 objc-keywordarg-list objc-keywordarg
10509 objc-keywordarg:
10510 objc-selector : objc-keywordexpr
10511 : objc-keywordexpr
10514 static tree
10515 c_parser_objc_message_args (c_parser *parser)
10517 tree sel = c_parser_objc_selector (parser);
10518 tree list = NULL_TREE;
10519 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10520 return sel;
10521 while (true)
10523 tree keywordexpr;
10524 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10525 return error_mark_node;
10526 keywordexpr = c_parser_objc_keywordexpr (parser);
10527 list = chainon (list, build_tree_list (sel, keywordexpr));
10528 sel = c_parser_objc_selector (parser);
10529 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10530 break;
10532 return list;
10535 /* Parse an objc-keywordexpr.
10537 objc-keywordexpr:
10538 nonempty-expr-list
10541 static tree
10542 c_parser_objc_keywordexpr (c_parser *parser)
10544 tree ret;
10545 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10546 NULL, NULL, NULL, NULL);
10547 if (vec_safe_length (expr_list) == 1)
10549 /* Just return the expression, remove a level of
10550 indirection. */
10551 ret = (*expr_list)[0];
10553 else
10555 /* We have a comma expression, we will collapse later. */
10556 ret = build_tree_list_vec (expr_list);
10558 release_tree_vector (expr_list);
10559 return ret;
10562 /* A check, needed in several places, that ObjC interface, implementation or
10563 method definitions are not prefixed by incorrect items. */
10564 static bool
10565 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10566 struct c_declspecs *specs)
10568 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10569 || specs->typespec_kind != ctsk_none)
10571 c_parser_error (parser,
10572 "no type or storage class may be specified here,");
10573 c_parser_skip_to_end_of_block_or_statement (parser);
10574 return true;
10576 return false;
10579 /* Parse an Objective-C @property declaration. The syntax is:
10581 objc-property-declaration:
10582 '@property' objc-property-attributes[opt] struct-declaration ;
10584 objc-property-attributes:
10585 '(' objc-property-attribute-list ')'
10587 objc-property-attribute-list:
10588 objc-property-attribute
10589 objc-property-attribute-list, objc-property-attribute
10591 objc-property-attribute
10592 'getter' = identifier
10593 'setter' = identifier
10594 'readonly'
10595 'readwrite'
10596 'assign'
10597 'retain'
10598 'copy'
10599 'nonatomic'
10601 For example:
10602 @property NSString *name;
10603 @property (readonly) id object;
10604 @property (retain, nonatomic, getter=getTheName) id name;
10605 @property int a, b, c;
10607 PS: This function is identical to cp_parser_objc_at_propery_declaration
10608 for C++. Keep them in sync. */
10609 static void
10610 c_parser_objc_at_property_declaration (c_parser *parser)
10612 /* The following variables hold the attributes of the properties as
10613 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10614 seen. When we see an attribute, we set them to 'true' (if they
10615 are boolean properties) or to the identifier (if they have an
10616 argument, ie, for getter and setter). Note that here we only
10617 parse the list of attributes, check the syntax and accumulate the
10618 attributes that we find. objc_add_property_declaration() will
10619 then process the information. */
10620 bool property_assign = false;
10621 bool property_copy = false;
10622 tree property_getter_ident = NULL_TREE;
10623 bool property_nonatomic = false;
10624 bool property_readonly = false;
10625 bool property_readwrite = false;
10626 bool property_retain = false;
10627 tree property_setter_ident = NULL_TREE;
10629 /* 'properties' is the list of properties that we read. Usually a
10630 single one, but maybe more (eg, in "@property int a, b, c;" there
10631 are three). */
10632 tree properties;
10633 location_t loc;
10635 loc = c_parser_peek_token (parser)->location;
10636 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10638 c_parser_consume_token (parser); /* Eat '@property'. */
10640 /* Parse the optional attribute list... */
10641 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10643 matching_parens parens;
10645 /* Eat the '(' */
10646 parens.consume_open (parser);
10648 /* Property attribute keywords are valid now. */
10649 parser->objc_property_attr_context = true;
10651 while (true)
10653 bool syntax_error = false;
10654 c_token *token = c_parser_peek_token (parser);
10655 enum rid keyword;
10657 if (token->type != CPP_KEYWORD)
10659 if (token->type == CPP_CLOSE_PAREN)
10660 c_parser_error (parser, "expected identifier");
10661 else
10663 c_parser_consume_token (parser);
10664 c_parser_error (parser, "unknown property attribute");
10666 break;
10668 keyword = token->keyword;
10669 c_parser_consume_token (parser);
10670 switch (keyword)
10672 case RID_ASSIGN: property_assign = true; break;
10673 case RID_COPY: property_copy = true; break;
10674 case RID_NONATOMIC: property_nonatomic = true; break;
10675 case RID_READONLY: property_readonly = true; break;
10676 case RID_READWRITE: property_readwrite = true; break;
10677 case RID_RETAIN: property_retain = true; break;
10679 case RID_GETTER:
10680 case RID_SETTER:
10681 if (c_parser_next_token_is_not (parser, CPP_EQ))
10683 if (keyword == RID_GETTER)
10684 c_parser_error (parser,
10685 "missing %<=%> (after %<getter%> attribute)");
10686 else
10687 c_parser_error (parser,
10688 "missing %<=%> (after %<setter%> attribute)");
10689 syntax_error = true;
10690 break;
10692 c_parser_consume_token (parser); /* eat the = */
10693 if (c_parser_next_token_is_not (parser, CPP_NAME))
10695 c_parser_error (parser, "expected identifier");
10696 syntax_error = true;
10697 break;
10699 if (keyword == RID_SETTER)
10701 if (property_setter_ident != NULL_TREE)
10702 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10703 else
10704 property_setter_ident = c_parser_peek_token (parser)->value;
10705 c_parser_consume_token (parser);
10706 if (c_parser_next_token_is_not (parser, CPP_COLON))
10707 c_parser_error (parser, "setter name must terminate with %<:%>");
10708 else
10709 c_parser_consume_token (parser);
10711 else
10713 if (property_getter_ident != NULL_TREE)
10714 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10715 else
10716 property_getter_ident = c_parser_peek_token (parser)->value;
10717 c_parser_consume_token (parser);
10719 break;
10720 default:
10721 c_parser_error (parser, "unknown property attribute");
10722 syntax_error = true;
10723 break;
10726 if (syntax_error)
10727 break;
10729 if (c_parser_next_token_is (parser, CPP_COMMA))
10730 c_parser_consume_token (parser);
10731 else
10732 break;
10734 parser->objc_property_attr_context = false;
10735 parens.skip_until_found_close (parser);
10737 /* ... and the property declaration(s). */
10738 properties = c_parser_struct_declaration (parser);
10740 if (properties == error_mark_node)
10742 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10743 parser->error = false;
10744 return;
10747 if (properties == NULL_TREE)
10748 c_parser_error (parser, "expected identifier");
10749 else
10751 /* Comma-separated properties are chained together in
10752 reverse order; add them one by one. */
10753 properties = nreverse (properties);
10755 for (; properties; properties = TREE_CHAIN (properties))
10756 objc_add_property_declaration (loc, copy_node (properties),
10757 property_readonly, property_readwrite,
10758 property_assign, property_retain,
10759 property_copy, property_nonatomic,
10760 property_getter_ident, property_setter_ident);
10763 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10764 parser->error = false;
10767 /* Parse an Objective-C @synthesize declaration. The syntax is:
10769 objc-synthesize-declaration:
10770 @synthesize objc-synthesize-identifier-list ;
10772 objc-synthesize-identifier-list:
10773 objc-synthesize-identifier
10774 objc-synthesize-identifier-list, objc-synthesize-identifier
10776 objc-synthesize-identifier
10777 identifier
10778 identifier = identifier
10780 For example:
10781 @synthesize MyProperty;
10782 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10784 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10785 for C++. Keep them in sync.
10787 static void
10788 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10790 tree list = NULL_TREE;
10791 location_t loc;
10792 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10793 loc = c_parser_peek_token (parser)->location;
10795 c_parser_consume_token (parser);
10796 while (true)
10798 tree property, ivar;
10799 if (c_parser_next_token_is_not (parser, CPP_NAME))
10801 c_parser_error (parser, "expected identifier");
10802 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10803 /* Once we find the semicolon, we can resume normal parsing.
10804 We have to reset parser->error manually because
10805 c_parser_skip_until_found() won't reset it for us if the
10806 next token is precisely a semicolon. */
10807 parser->error = false;
10808 return;
10810 property = c_parser_peek_token (parser)->value;
10811 c_parser_consume_token (parser);
10812 if (c_parser_next_token_is (parser, CPP_EQ))
10814 c_parser_consume_token (parser);
10815 if (c_parser_next_token_is_not (parser, CPP_NAME))
10817 c_parser_error (parser, "expected identifier");
10818 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10819 parser->error = false;
10820 return;
10822 ivar = c_parser_peek_token (parser)->value;
10823 c_parser_consume_token (parser);
10825 else
10826 ivar = NULL_TREE;
10827 list = chainon (list, build_tree_list (ivar, property));
10828 if (c_parser_next_token_is (parser, CPP_COMMA))
10829 c_parser_consume_token (parser);
10830 else
10831 break;
10833 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10834 objc_add_synthesize_declaration (loc, list);
10837 /* Parse an Objective-C @dynamic declaration. The syntax is:
10839 objc-dynamic-declaration:
10840 @dynamic identifier-list ;
10842 For example:
10843 @dynamic MyProperty;
10844 @dynamic MyProperty, AnotherProperty;
10846 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10847 for C++. Keep them in sync.
10849 static void
10850 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10852 tree list = NULL_TREE;
10853 location_t loc;
10854 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10855 loc = c_parser_peek_token (parser)->location;
10857 c_parser_consume_token (parser);
10858 while (true)
10860 tree property;
10861 if (c_parser_next_token_is_not (parser, CPP_NAME))
10863 c_parser_error (parser, "expected identifier");
10864 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10865 parser->error = false;
10866 return;
10868 property = c_parser_peek_token (parser)->value;
10869 list = chainon (list, build_tree_list (NULL_TREE, property));
10870 c_parser_consume_token (parser);
10871 if (c_parser_next_token_is (parser, CPP_COMMA))
10872 c_parser_consume_token (parser);
10873 else
10874 break;
10876 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10877 objc_add_dynamic_declaration (loc, list);
10881 /* Parse a pragma GCC ivdep. */
10883 static bool
10884 c_parse_pragma_ivdep (c_parser *parser)
10886 c_parser_consume_pragma (parser);
10887 c_parser_skip_to_pragma_eol (parser);
10888 return true;
10891 /* Parse a pragma GCC unroll. */
10893 static unsigned short
10894 c_parser_pragma_unroll (c_parser *parser)
10896 unsigned short unroll;
10897 c_parser_consume_pragma (parser);
10898 location_t location = c_parser_peek_token (parser)->location;
10899 tree expr = c_parser_expr_no_commas (parser, NULL).value;
10900 mark_exp_read (expr);
10901 expr = c_fully_fold (expr, false, NULL);
10902 HOST_WIDE_INT lunroll = 0;
10903 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
10904 || TREE_CODE (expr) != INTEGER_CST
10905 || (lunroll = tree_to_shwi (expr)) < 0
10906 || lunroll >= USHRT_MAX)
10908 error_at (location, "%<#pragma GCC unroll%> requires an"
10909 " assignment-expression that evaluates to a non-negative"
10910 " integral constant less than %u", USHRT_MAX);
10911 unroll = 0;
10913 else
10915 unroll = (unsigned short)lunroll;
10916 if (unroll == 0)
10917 unroll = 1;
10920 c_parser_skip_to_pragma_eol (parser);
10921 return unroll;
10924 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10925 should be considered, statements. ALLOW_STMT is true if we're within
10926 the context of a function and such pragmas are to be allowed. Returns
10927 true if we actually parsed such a pragma. */
10929 static bool
10930 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10932 unsigned int id;
10933 const char *construct = NULL;
10935 id = c_parser_peek_token (parser)->pragma_kind;
10936 gcc_assert (id != PRAGMA_NONE);
10938 switch (id)
10940 case PRAGMA_OACC_DECLARE:
10941 c_parser_oacc_declare (parser);
10942 return false;
10944 case PRAGMA_OACC_ENTER_DATA:
10945 if (context != pragma_compound)
10947 construct = "acc enter data";
10948 in_compound:
10949 if (context == pragma_stmt)
10951 error_at (c_parser_peek_token (parser)->location,
10952 "%<#pragma %s%> may only be used in compound "
10953 "statements", construct);
10954 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10955 return false;
10957 goto bad_stmt;
10959 c_parser_oacc_enter_exit_data (parser, true);
10960 return false;
10962 case PRAGMA_OACC_EXIT_DATA:
10963 if (context != pragma_compound)
10965 construct = "acc exit data";
10966 goto in_compound;
10968 c_parser_oacc_enter_exit_data (parser, false);
10969 return false;
10971 case PRAGMA_OACC_ROUTINE:
10972 if (context != pragma_external)
10974 error_at (c_parser_peek_token (parser)->location,
10975 "%<#pragma acc routine%> must be at file scope");
10976 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10977 return false;
10979 c_parser_oacc_routine (parser, context);
10980 return false;
10982 case PRAGMA_OACC_UPDATE:
10983 if (context != pragma_compound)
10985 construct = "acc update";
10986 goto in_compound;
10988 c_parser_oacc_update (parser);
10989 return false;
10991 case PRAGMA_OMP_BARRIER:
10992 if (context != pragma_compound)
10994 construct = "omp barrier";
10995 goto in_compound;
10997 c_parser_omp_barrier (parser);
10998 return false;
11000 case PRAGMA_OMP_FLUSH:
11001 if (context != pragma_compound)
11003 construct = "omp flush";
11004 goto in_compound;
11006 c_parser_omp_flush (parser);
11007 return false;
11009 case PRAGMA_OMP_TASKWAIT:
11010 if (context != pragma_compound)
11012 construct = "omp taskwait";
11013 goto in_compound;
11015 c_parser_omp_taskwait (parser);
11016 return false;
11018 case PRAGMA_OMP_TASKYIELD:
11019 if (context != pragma_compound)
11021 construct = "omp taskyield";
11022 goto in_compound;
11024 c_parser_omp_taskyield (parser);
11025 return false;
11027 case PRAGMA_OMP_CANCEL:
11028 if (context != pragma_compound)
11030 construct = "omp cancel";
11031 goto in_compound;
11033 c_parser_omp_cancel (parser);
11034 return false;
11036 case PRAGMA_OMP_CANCELLATION_POINT:
11037 c_parser_omp_cancellation_point (parser, context);
11038 return false;
11040 case PRAGMA_OMP_THREADPRIVATE:
11041 c_parser_omp_threadprivate (parser);
11042 return false;
11044 case PRAGMA_OMP_TARGET:
11045 return c_parser_omp_target (parser, context, if_p);
11047 case PRAGMA_OMP_END_DECLARE_TARGET:
11048 c_parser_omp_end_declare_target (parser);
11049 return false;
11051 case PRAGMA_OMP_SECTION:
11052 error_at (c_parser_peek_token (parser)->location,
11053 "%<#pragma omp section%> may only be used in "
11054 "%<#pragma omp sections%> construct");
11055 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11056 return false;
11058 case PRAGMA_OMP_DECLARE:
11059 c_parser_omp_declare (parser, context);
11060 return false;
11062 case PRAGMA_OMP_ORDERED:
11063 return c_parser_omp_ordered (parser, context, if_p);
11065 case PRAGMA_IVDEP:
11067 const bool ivdep = c_parse_pragma_ivdep (parser);
11068 unsigned short unroll;
11069 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11070 unroll = c_parser_pragma_unroll (parser);
11071 else
11072 unroll = 0;
11073 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11074 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11075 && !c_parser_next_token_is_keyword (parser, RID_DO))
11077 c_parser_error (parser, "for, while or do statement expected");
11078 return false;
11080 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11081 c_parser_for_statement (parser, ivdep, unroll, if_p);
11082 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11083 c_parser_while_statement (parser, ivdep, unroll, if_p);
11084 else
11085 c_parser_do_statement (parser, ivdep, unroll);
11087 return false;
11089 case PRAGMA_UNROLL:
11091 unsigned short unroll = c_parser_pragma_unroll (parser);
11092 bool ivdep;
11093 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11094 ivdep = c_parse_pragma_ivdep (parser);
11095 else
11096 ivdep = false;
11097 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11098 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11099 && !c_parser_next_token_is_keyword (parser, RID_DO))
11101 c_parser_error (parser, "for, while or do statement expected");
11102 return false;
11104 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11105 c_parser_for_statement (parser, ivdep, unroll, if_p);
11106 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11107 c_parser_while_statement (parser, ivdep, unroll, if_p);
11108 else
11109 c_parser_do_statement (parser, ivdep, unroll);
11111 return false;
11113 case PRAGMA_GCC_PCH_PREPROCESS:
11114 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11115 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11116 return false;
11118 case PRAGMA_OACC_WAIT:
11119 if (context != pragma_compound)
11121 construct = "acc wait";
11122 goto in_compound;
11124 /* FALL THROUGH. */
11126 default:
11127 if (id < PRAGMA_FIRST_EXTERNAL)
11129 if (context != pragma_stmt && context != pragma_compound)
11131 bad_stmt:
11132 c_parser_error (parser, "expected declaration specifiers");
11133 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11134 return false;
11136 c_parser_omp_construct (parser, if_p);
11137 return true;
11139 break;
11142 c_parser_consume_pragma (parser);
11143 c_invoke_pragma_handler (id);
11145 /* Skip to EOL, but suppress any error message. Those will have been
11146 generated by the handler routine through calling error, as opposed
11147 to calling c_parser_error. */
11148 parser->error = true;
11149 c_parser_skip_to_pragma_eol (parser);
11151 return false;
11154 /* The interface the pragma parsers have to the lexer. */
11156 enum cpp_ttype
11157 pragma_lex (tree *value, location_t *loc)
11159 c_token *tok = c_parser_peek_token (the_parser);
11160 enum cpp_ttype ret = tok->type;
11162 *value = tok->value;
11163 if (loc)
11164 *loc = tok->location;
11166 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11167 ret = CPP_EOF;
11168 else
11170 if (ret == CPP_KEYWORD)
11171 ret = CPP_NAME;
11172 c_parser_consume_token (the_parser);
11175 return ret;
11178 static void
11179 c_parser_pragma_pch_preprocess (c_parser *parser)
11181 tree name = NULL;
11183 c_parser_consume_pragma (parser);
11184 if (c_parser_next_token_is (parser, CPP_STRING))
11186 name = c_parser_peek_token (parser)->value;
11187 c_parser_consume_token (parser);
11189 else
11190 c_parser_error (parser, "expected string literal");
11191 c_parser_skip_to_pragma_eol (parser);
11193 if (name)
11194 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11197 /* OpenACC and OpenMP parsing routines. */
11199 /* Returns name of the next clause.
11200 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11201 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11202 returned and the token is consumed. */
11204 static pragma_omp_clause
11205 c_parser_omp_clause_name (c_parser *parser)
11207 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11209 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11210 result = PRAGMA_OACC_CLAUSE_AUTO;
11211 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11212 result = PRAGMA_OMP_CLAUSE_IF;
11213 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11214 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11215 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11216 result = PRAGMA_OMP_CLAUSE_FOR;
11217 else if (c_parser_next_token_is (parser, CPP_NAME))
11219 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11221 switch (p[0])
11223 case 'a':
11224 if (!strcmp ("aligned", p))
11225 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11226 else if (!strcmp ("async", p))
11227 result = PRAGMA_OACC_CLAUSE_ASYNC;
11228 break;
11229 case 'c':
11230 if (!strcmp ("collapse", p))
11231 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11232 else if (!strcmp ("copy", p))
11233 result = PRAGMA_OACC_CLAUSE_COPY;
11234 else if (!strcmp ("copyin", p))
11235 result = PRAGMA_OMP_CLAUSE_COPYIN;
11236 else if (!strcmp ("copyout", p))
11237 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11238 else if (!strcmp ("copyprivate", p))
11239 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11240 else if (!strcmp ("create", p))
11241 result = PRAGMA_OACC_CLAUSE_CREATE;
11242 break;
11243 case 'd':
11244 if (!strcmp ("defaultmap", p))
11245 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11246 else if (!strcmp ("delete", p))
11247 result = PRAGMA_OACC_CLAUSE_DELETE;
11248 else if (!strcmp ("depend", p))
11249 result = PRAGMA_OMP_CLAUSE_DEPEND;
11250 else if (!strcmp ("device", p))
11251 result = PRAGMA_OMP_CLAUSE_DEVICE;
11252 else if (!strcmp ("deviceptr", p))
11253 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11254 else if (!strcmp ("device_resident", p))
11255 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11256 else if (!strcmp ("dist_schedule", p))
11257 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11258 break;
11259 case 'f':
11260 if (!strcmp ("final", p))
11261 result = PRAGMA_OMP_CLAUSE_FINAL;
11262 else if (!strcmp ("firstprivate", p))
11263 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11264 else if (!strcmp ("from", p))
11265 result = PRAGMA_OMP_CLAUSE_FROM;
11266 break;
11267 case 'g':
11268 if (!strcmp ("gang", p))
11269 result = PRAGMA_OACC_CLAUSE_GANG;
11270 else if (!strcmp ("grainsize", p))
11271 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11272 break;
11273 case 'h':
11274 if (!strcmp ("hint", p))
11275 result = PRAGMA_OMP_CLAUSE_HINT;
11276 else if (!strcmp ("host", p))
11277 result = PRAGMA_OACC_CLAUSE_HOST;
11278 break;
11279 case 'i':
11280 if (!strcmp ("inbranch", p))
11281 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11282 else if (!strcmp ("independent", p))
11283 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11284 else if (!strcmp ("is_device_ptr", p))
11285 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11286 break;
11287 case 'l':
11288 if (!strcmp ("lastprivate", p))
11289 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11290 else if (!strcmp ("linear", p))
11291 result = PRAGMA_OMP_CLAUSE_LINEAR;
11292 else if (!strcmp ("link", p))
11293 result = PRAGMA_OMP_CLAUSE_LINK;
11294 break;
11295 case 'm':
11296 if (!strcmp ("map", p))
11297 result = PRAGMA_OMP_CLAUSE_MAP;
11298 else if (!strcmp ("mergeable", p))
11299 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11300 break;
11301 case 'n':
11302 if (!strcmp ("nogroup", p))
11303 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11304 else if (!strcmp ("notinbranch", p))
11305 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11306 else if (!strcmp ("nowait", p))
11307 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11308 else if (!strcmp ("num_gangs", p))
11309 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11310 else if (!strcmp ("num_tasks", p))
11311 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11312 else if (!strcmp ("num_teams", p))
11313 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11314 else if (!strcmp ("num_threads", p))
11315 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11316 else if (!strcmp ("num_workers", p))
11317 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11318 break;
11319 case 'o':
11320 if (!strcmp ("ordered", p))
11321 result = PRAGMA_OMP_CLAUSE_ORDERED;
11322 break;
11323 case 'p':
11324 if (!strcmp ("parallel", p))
11325 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11326 else if (!strcmp ("present", p))
11327 result = PRAGMA_OACC_CLAUSE_PRESENT;
11328 else if (!strcmp ("present_or_copy", p)
11329 || !strcmp ("pcopy", p))
11330 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
11331 else if (!strcmp ("present_or_copyin", p)
11332 || !strcmp ("pcopyin", p))
11333 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
11334 else if (!strcmp ("present_or_copyout", p)
11335 || !strcmp ("pcopyout", p))
11336 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
11337 else if (!strcmp ("present_or_create", p)
11338 || !strcmp ("pcreate", p))
11339 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
11340 else if (!strcmp ("priority", p))
11341 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11342 else if (!strcmp ("private", p))
11343 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11344 else if (!strcmp ("proc_bind", p))
11345 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11346 break;
11347 case 'r':
11348 if (!strcmp ("reduction", p))
11349 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11350 break;
11351 case 's':
11352 if (!strcmp ("safelen", p))
11353 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11354 else if (!strcmp ("schedule", p))
11355 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11356 else if (!strcmp ("sections", p))
11357 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11358 else if (!strcmp ("seq", p))
11359 result = PRAGMA_OACC_CLAUSE_SEQ;
11360 else if (!strcmp ("shared", p))
11361 result = PRAGMA_OMP_CLAUSE_SHARED;
11362 else if (!strcmp ("simd", p))
11363 result = PRAGMA_OMP_CLAUSE_SIMD;
11364 else if (!strcmp ("simdlen", p))
11365 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11366 else if (!strcmp ("self", p))
11367 result = PRAGMA_OACC_CLAUSE_SELF;
11368 break;
11369 case 't':
11370 if (!strcmp ("taskgroup", p))
11371 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11372 else if (!strcmp ("thread_limit", p))
11373 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11374 else if (!strcmp ("threads", p))
11375 result = PRAGMA_OMP_CLAUSE_THREADS;
11376 else if (!strcmp ("tile", p))
11377 result = PRAGMA_OACC_CLAUSE_TILE;
11378 else if (!strcmp ("to", p))
11379 result = PRAGMA_OMP_CLAUSE_TO;
11380 break;
11381 case 'u':
11382 if (!strcmp ("uniform", p))
11383 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11384 else if (!strcmp ("untied", p))
11385 result = PRAGMA_OMP_CLAUSE_UNTIED;
11386 else if (!strcmp ("use_device", p))
11387 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11388 else if (!strcmp ("use_device_ptr", p))
11389 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11390 break;
11391 case 'v':
11392 if (!strcmp ("vector", p))
11393 result = PRAGMA_OACC_CLAUSE_VECTOR;
11394 else if (!strcmp ("vector_length", p))
11395 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11396 break;
11397 case 'w':
11398 if (!strcmp ("wait", p))
11399 result = PRAGMA_OACC_CLAUSE_WAIT;
11400 else if (!strcmp ("worker", p))
11401 result = PRAGMA_OACC_CLAUSE_WORKER;
11402 break;
11406 if (result != PRAGMA_OMP_CLAUSE_NONE)
11407 c_parser_consume_token (parser);
11409 return result;
11412 /* Validate that a clause of the given type does not already exist. */
11414 static void
11415 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11416 const char *name)
11418 tree c;
11420 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11421 if (OMP_CLAUSE_CODE (c) == code)
11423 location_t loc = OMP_CLAUSE_LOCATION (c);
11424 error_at (loc, "too many %qs clauses", name);
11425 break;
11429 /* OpenACC 2.0
11430 Parse wait clause or wait directive parameters. */
11432 static tree
11433 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11435 vec<tree, va_gc> *args;
11436 tree t, args_tree;
11438 matching_parens parens;
11439 if (!parens.require_open (parser))
11440 return list;
11442 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11444 if (args->length () == 0)
11446 c_parser_error (parser, "expected integer expression before ')'");
11447 release_tree_vector (args);
11448 return list;
11451 args_tree = build_tree_list_vec (args);
11453 for (t = args_tree; t; t = TREE_CHAIN (t))
11455 tree targ = TREE_VALUE (t);
11457 if (targ != error_mark_node)
11459 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11461 c_parser_error (parser, "expression must be integral");
11462 targ = error_mark_node;
11464 else
11466 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11468 OMP_CLAUSE_DECL (c) = targ;
11469 OMP_CLAUSE_CHAIN (c) = list;
11470 list = c;
11475 release_tree_vector (args);
11476 parens.require_close (parser);
11477 return list;
11480 /* OpenACC 2.0, OpenMP 2.5:
11481 variable-list:
11482 identifier
11483 variable-list , identifier
11485 If KIND is nonzero, create the appropriate node and install the
11486 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11487 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11489 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11490 return the list created. */
11492 static tree
11493 c_parser_omp_variable_list (c_parser *parser,
11494 location_t clause_loc,
11495 enum omp_clause_code kind, tree list)
11497 if (c_parser_next_token_is_not (parser, CPP_NAME)
11498 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11499 c_parser_error (parser, "expected identifier");
11501 while (c_parser_next_token_is (parser, CPP_NAME)
11502 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11504 tree t = lookup_name (c_parser_peek_token (parser)->value);
11506 if (t == NULL_TREE)
11508 undeclared_variable (c_parser_peek_token (parser)->location,
11509 c_parser_peek_token (parser)->value);
11510 t = error_mark_node;
11513 c_parser_consume_token (parser);
11515 if (t == error_mark_node)
11517 else if (kind != 0)
11519 switch (kind)
11521 case OMP_CLAUSE__CACHE_:
11522 /* The OpenACC cache directive explicitly only allows "array
11523 elements or subarrays". */
11524 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11526 c_parser_error (parser, "expected %<[%>");
11527 t = error_mark_node;
11528 break;
11530 /* FALLTHROUGH */
11531 case OMP_CLAUSE_MAP:
11532 case OMP_CLAUSE_FROM:
11533 case OMP_CLAUSE_TO:
11534 while (c_parser_next_token_is (parser, CPP_DOT))
11536 location_t op_loc = c_parser_peek_token (parser)->location;
11537 c_parser_consume_token (parser);
11538 if (!c_parser_next_token_is (parser, CPP_NAME))
11540 c_parser_error (parser, "expected identifier");
11541 t = error_mark_node;
11542 break;
11545 c_token *comp_tok = c_parser_peek_token (parser);
11546 tree ident = comp_tok->value;
11547 location_t comp_loc = comp_tok->location;
11548 c_parser_consume_token (parser);
11549 t = build_component_ref (op_loc, t, ident, comp_loc);
11551 /* FALLTHROUGH */
11552 case OMP_CLAUSE_DEPEND:
11553 case OMP_CLAUSE_REDUCTION:
11554 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11556 tree low_bound = NULL_TREE, length = NULL_TREE;
11558 c_parser_consume_token (parser);
11559 if (!c_parser_next_token_is (parser, CPP_COLON))
11561 location_t expr_loc
11562 = c_parser_peek_token (parser)->location;
11563 c_expr expr = c_parser_expression (parser);
11564 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11565 false, true);
11566 low_bound = expr.value;
11568 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11569 length = integer_one_node;
11570 else
11572 /* Look for `:'. */
11573 if (!c_parser_require (parser, CPP_COLON,
11574 "expected %<:%>"))
11576 t = error_mark_node;
11577 break;
11579 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11581 location_t expr_loc
11582 = c_parser_peek_token (parser)->location;
11583 c_expr expr = c_parser_expression (parser);
11584 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11585 false, true);
11586 length = expr.value;
11589 /* Look for the closing `]'. */
11590 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11591 "expected %<]%>"))
11593 t = error_mark_node;
11594 break;
11597 t = tree_cons (low_bound, length, t);
11599 break;
11600 default:
11601 break;
11604 if (t != error_mark_node)
11606 tree u = build_omp_clause (clause_loc, kind);
11607 OMP_CLAUSE_DECL (u) = t;
11608 OMP_CLAUSE_CHAIN (u) = list;
11609 list = u;
11612 else
11613 list = tree_cons (t, NULL_TREE, list);
11615 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11616 break;
11618 c_parser_consume_token (parser);
11621 return list;
11624 /* Similarly, but expect leading and trailing parenthesis. This is a very
11625 common case for OpenACC and OpenMP clauses. */
11627 static tree
11628 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11629 tree list)
11631 /* The clauses location. */
11632 location_t loc = c_parser_peek_token (parser)->location;
11634 matching_parens parens;
11635 if (parens.require_open (parser))
11637 list = c_parser_omp_variable_list (parser, loc, kind, list);
11638 parens.skip_until_found_close (parser);
11640 return list;
11643 /* OpenACC 2.0:
11644 copy ( variable-list )
11645 copyin ( variable-list )
11646 copyout ( variable-list )
11647 create ( variable-list )
11648 delete ( variable-list )
11649 present ( variable-list )
11650 present_or_copy ( variable-list )
11651 pcopy ( variable-list )
11652 present_or_copyin ( variable-list )
11653 pcopyin ( variable-list )
11654 present_or_copyout ( variable-list )
11655 pcopyout ( variable-list )
11656 present_or_create ( variable-list )
11657 pcreate ( variable-list ) */
11659 static tree
11660 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11661 tree list)
11663 enum gomp_map_kind kind;
11664 switch (c_kind)
11666 case PRAGMA_OACC_CLAUSE_COPY:
11667 kind = GOMP_MAP_FORCE_TOFROM;
11668 break;
11669 case PRAGMA_OACC_CLAUSE_COPYIN:
11670 kind = GOMP_MAP_FORCE_TO;
11671 break;
11672 case PRAGMA_OACC_CLAUSE_COPYOUT:
11673 kind = GOMP_MAP_FORCE_FROM;
11674 break;
11675 case PRAGMA_OACC_CLAUSE_CREATE:
11676 kind = GOMP_MAP_FORCE_ALLOC;
11677 break;
11678 case PRAGMA_OACC_CLAUSE_DELETE:
11679 kind = GOMP_MAP_DELETE;
11680 break;
11681 case PRAGMA_OACC_CLAUSE_DEVICE:
11682 kind = GOMP_MAP_FORCE_TO;
11683 break;
11684 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11685 kind = GOMP_MAP_DEVICE_RESIDENT;
11686 break;
11687 case PRAGMA_OACC_CLAUSE_HOST:
11688 case PRAGMA_OACC_CLAUSE_SELF:
11689 kind = GOMP_MAP_FORCE_FROM;
11690 break;
11691 case PRAGMA_OACC_CLAUSE_LINK:
11692 kind = GOMP_MAP_LINK;
11693 break;
11694 case PRAGMA_OACC_CLAUSE_PRESENT:
11695 kind = GOMP_MAP_FORCE_PRESENT;
11696 break;
11697 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11698 kind = GOMP_MAP_TOFROM;
11699 break;
11700 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11701 kind = GOMP_MAP_TO;
11702 break;
11703 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11704 kind = GOMP_MAP_FROM;
11705 break;
11706 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11707 kind = GOMP_MAP_ALLOC;
11708 break;
11709 default:
11710 gcc_unreachable ();
11712 tree nl, c;
11713 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11715 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11716 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11718 return nl;
11721 /* OpenACC 2.0:
11722 deviceptr ( variable-list ) */
11724 static tree
11725 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11727 location_t loc = c_parser_peek_token (parser)->location;
11728 tree vars, t;
11730 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11731 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11732 variable-list must only allow for pointer variables. */
11733 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11734 for (t = vars; t && t; t = TREE_CHAIN (t))
11736 tree v = TREE_PURPOSE (t);
11738 /* FIXME diagnostics: Ideally we should keep individual
11739 locations for all the variables in the var list to make the
11740 following errors more precise. Perhaps
11741 c_parser_omp_var_list_parens() should construct a list of
11742 locations to go along with the var list. */
11744 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11745 error_at (loc, "%qD is not a variable", v);
11746 else if (TREE_TYPE (v) == error_mark_node)
11748 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11749 error_at (loc, "%qD is not a pointer variable", v);
11751 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11752 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11753 OMP_CLAUSE_DECL (u) = v;
11754 OMP_CLAUSE_CHAIN (u) = list;
11755 list = u;
11758 return list;
11761 /* OpenACC 2.0, OpenMP 3.0:
11762 collapse ( constant-expression ) */
11764 static tree
11765 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11767 tree c, num = error_mark_node;
11768 HOST_WIDE_INT n;
11769 location_t loc;
11771 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11772 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11774 loc = c_parser_peek_token (parser)->location;
11775 matching_parens parens;
11776 if (parens.require_open (parser))
11778 num = c_parser_expr_no_commas (parser, NULL).value;
11779 parens.skip_until_found_close (parser);
11781 if (num == error_mark_node)
11782 return list;
11783 mark_exp_read (num);
11784 num = c_fully_fold (num, false, NULL);
11785 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11786 || !tree_fits_shwi_p (num)
11787 || (n = tree_to_shwi (num)) <= 0
11788 || (int) n != n)
11790 error_at (loc,
11791 "collapse argument needs positive constant integer expression");
11792 return list;
11794 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11795 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11796 OMP_CLAUSE_CHAIN (c) = list;
11797 return c;
11800 /* OpenMP 2.5:
11801 copyin ( variable-list ) */
11803 static tree
11804 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11806 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11809 /* OpenMP 2.5:
11810 copyprivate ( variable-list ) */
11812 static tree
11813 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11815 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11818 /* OpenMP 2.5:
11819 default ( none | shared )
11821 OpenACC:
11822 default ( none | present ) */
11824 static tree
11825 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11827 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11828 location_t loc = c_parser_peek_token (parser)->location;
11829 tree c;
11831 matching_parens parens;
11832 if (!parens.require_open (parser))
11833 return list;
11834 if (c_parser_next_token_is (parser, CPP_NAME))
11836 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11838 switch (p[0])
11840 case 'n':
11841 if (strcmp ("none", p) != 0)
11842 goto invalid_kind;
11843 kind = OMP_CLAUSE_DEFAULT_NONE;
11844 break;
11846 case 'p':
11847 if (strcmp ("present", p) != 0 || !is_oacc)
11848 goto invalid_kind;
11849 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11850 break;
11852 case 's':
11853 if (strcmp ("shared", p) != 0 || is_oacc)
11854 goto invalid_kind;
11855 kind = OMP_CLAUSE_DEFAULT_SHARED;
11856 break;
11858 default:
11859 goto invalid_kind;
11862 c_parser_consume_token (parser);
11864 else
11866 invalid_kind:
11867 if (is_oacc)
11868 c_parser_error (parser, "expected %<none%> or %<present%>");
11869 else
11870 c_parser_error (parser, "expected %<none%> or %<shared%>");
11872 parens.skip_until_found_close (parser);
11874 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11875 return list;
11877 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11878 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11879 OMP_CLAUSE_CHAIN (c) = list;
11880 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11882 return c;
11885 /* OpenMP 2.5:
11886 firstprivate ( variable-list ) */
11888 static tree
11889 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11891 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11894 /* OpenMP 3.1:
11895 final ( expression ) */
11897 static tree
11898 c_parser_omp_clause_final (c_parser *parser, tree list)
11900 location_t loc = c_parser_peek_token (parser)->location;
11901 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11903 tree t = c_parser_paren_condition (parser);
11904 tree c;
11906 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11908 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11909 OMP_CLAUSE_FINAL_EXPR (c) = t;
11910 OMP_CLAUSE_CHAIN (c) = list;
11911 list = c;
11913 else
11914 c_parser_error (parser, "expected %<(%>");
11916 return list;
11919 /* OpenACC, OpenMP 2.5:
11920 if ( expression )
11922 OpenMP 4.5:
11923 if ( directive-name-modifier : expression )
11925 directive-name-modifier:
11926 parallel | task | taskloop | target data | target | target update
11927 | target enter data | target exit data */
11929 static tree
11930 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11932 location_t location = c_parser_peek_token (parser)->location;
11933 enum tree_code if_modifier = ERROR_MARK;
11935 matching_parens parens;
11936 if (!parens.require_open (parser))
11937 return list;
11939 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11941 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11942 int n = 2;
11943 if (strcmp (p, "parallel") == 0)
11944 if_modifier = OMP_PARALLEL;
11945 else if (strcmp (p, "task") == 0)
11946 if_modifier = OMP_TASK;
11947 else if (strcmp (p, "taskloop") == 0)
11948 if_modifier = OMP_TASKLOOP;
11949 else if (strcmp (p, "target") == 0)
11951 if_modifier = OMP_TARGET;
11952 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11954 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11955 if (strcmp ("data", p) == 0)
11956 if_modifier = OMP_TARGET_DATA;
11957 else if (strcmp ("update", p) == 0)
11958 if_modifier = OMP_TARGET_UPDATE;
11959 else if (strcmp ("enter", p) == 0)
11960 if_modifier = OMP_TARGET_ENTER_DATA;
11961 else if (strcmp ("exit", p) == 0)
11962 if_modifier = OMP_TARGET_EXIT_DATA;
11963 if (if_modifier != OMP_TARGET)
11965 n = 3;
11966 c_parser_consume_token (parser);
11968 else
11970 location_t loc = c_parser_peek_2nd_token (parser)->location;
11971 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11972 "or %<exit%>");
11973 if_modifier = ERROR_MARK;
11975 if (if_modifier == OMP_TARGET_ENTER_DATA
11976 || if_modifier == OMP_TARGET_EXIT_DATA)
11978 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11980 p = IDENTIFIER_POINTER
11981 (c_parser_peek_2nd_token (parser)->value);
11982 if (strcmp ("data", p) == 0)
11983 n = 4;
11985 if (n == 4)
11986 c_parser_consume_token (parser);
11987 else
11989 location_t loc
11990 = c_parser_peek_2nd_token (parser)->location;
11991 error_at (loc, "expected %<data%>");
11992 if_modifier = ERROR_MARK;
11997 if (if_modifier != ERROR_MARK)
11999 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12001 c_parser_consume_token (parser);
12002 c_parser_consume_token (parser);
12004 else
12006 if (n > 2)
12008 location_t loc = c_parser_peek_2nd_token (parser)->location;
12009 error_at (loc, "expected %<:%>");
12011 if_modifier = ERROR_MARK;
12016 tree t = c_parser_condition (parser), c;
12017 parens.skip_until_found_close (parser);
12019 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12020 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12022 if (if_modifier != ERROR_MARK
12023 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12025 const char *p = NULL;
12026 switch (if_modifier)
12028 case OMP_PARALLEL: p = "parallel"; break;
12029 case OMP_TASK: p = "task"; break;
12030 case OMP_TASKLOOP: p = "taskloop"; break;
12031 case OMP_TARGET_DATA: p = "target data"; break;
12032 case OMP_TARGET: p = "target"; break;
12033 case OMP_TARGET_UPDATE: p = "target update"; break;
12034 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12035 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12036 default: gcc_unreachable ();
12038 error_at (location, "too many %<if%> clauses with %qs modifier",
12040 return list;
12042 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12044 if (!is_omp)
12045 error_at (location, "too many %<if%> clauses");
12046 else
12047 error_at (location, "too many %<if%> clauses without modifier");
12048 return list;
12050 else if (if_modifier == ERROR_MARK
12051 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12053 error_at (location, "if any %<if%> clause has modifier, then all "
12054 "%<if%> clauses have to use modifier");
12055 return list;
12059 c = build_omp_clause (location, OMP_CLAUSE_IF);
12060 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12061 OMP_CLAUSE_IF_EXPR (c) = t;
12062 OMP_CLAUSE_CHAIN (c) = list;
12063 return c;
12066 /* OpenMP 2.5:
12067 lastprivate ( variable-list ) */
12069 static tree
12070 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12072 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12075 /* OpenMP 3.1:
12076 mergeable */
12078 static tree
12079 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12081 tree c;
12083 /* FIXME: Should we allow duplicates? */
12084 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12086 c = build_omp_clause (c_parser_peek_token (parser)->location,
12087 OMP_CLAUSE_MERGEABLE);
12088 OMP_CLAUSE_CHAIN (c) = list;
12090 return c;
12093 /* OpenMP 2.5:
12094 nowait */
12096 static tree
12097 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12099 tree c;
12100 location_t loc = c_parser_peek_token (parser)->location;
12102 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12104 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12105 OMP_CLAUSE_CHAIN (c) = list;
12106 return c;
12109 /* OpenMP 2.5:
12110 num_threads ( expression ) */
12112 static tree
12113 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12115 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12116 matching_parens parens;
12117 if (parens.require_open (parser))
12119 location_t expr_loc = c_parser_peek_token (parser)->location;
12120 c_expr expr = c_parser_expression (parser);
12121 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12122 tree c, t = expr.value;
12123 t = c_fully_fold (t, false, NULL);
12125 parens.skip_until_found_close (parser);
12127 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12129 c_parser_error (parser, "expected integer expression");
12130 return list;
12133 /* Attempt to statically determine when the number isn't positive. */
12134 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12135 build_int_cst (TREE_TYPE (t), 0));
12136 protected_set_expr_location (c, expr_loc);
12137 if (c == boolean_true_node)
12139 warning_at (expr_loc, 0,
12140 "%<num_threads%> value must be positive");
12141 t = integer_one_node;
12144 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12146 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12147 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12148 OMP_CLAUSE_CHAIN (c) = list;
12149 list = c;
12152 return list;
12155 /* OpenMP 4.5:
12156 num_tasks ( expression ) */
12158 static tree
12159 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12161 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12162 matching_parens parens;
12163 if (parens.require_open (parser))
12165 location_t expr_loc = c_parser_peek_token (parser)->location;
12166 c_expr expr = c_parser_expression (parser);
12167 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12168 tree c, t = expr.value;
12169 t = c_fully_fold (t, false, NULL);
12171 parens.skip_until_found_close (parser);
12173 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12175 c_parser_error (parser, "expected integer expression");
12176 return list;
12179 /* Attempt to statically determine when the number isn't positive. */
12180 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12181 build_int_cst (TREE_TYPE (t), 0));
12182 if (CAN_HAVE_LOCATION_P (c))
12183 SET_EXPR_LOCATION (c, expr_loc);
12184 if (c == boolean_true_node)
12186 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12187 t = integer_one_node;
12190 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12192 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12193 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12194 OMP_CLAUSE_CHAIN (c) = list;
12195 list = c;
12198 return list;
12201 /* OpenMP 4.5:
12202 grainsize ( expression ) */
12204 static tree
12205 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12207 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12208 matching_parens parens;
12209 if (parens.require_open (parser))
12211 location_t expr_loc = c_parser_peek_token (parser)->location;
12212 c_expr expr = c_parser_expression (parser);
12213 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12214 tree c, t = expr.value;
12215 t = c_fully_fold (t, false, NULL);
12217 parens.skip_until_found_close (parser);
12219 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12221 c_parser_error (parser, "expected integer expression");
12222 return list;
12225 /* Attempt to statically determine when the number isn't positive. */
12226 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12227 build_int_cst (TREE_TYPE (t), 0));
12228 if (CAN_HAVE_LOCATION_P (c))
12229 SET_EXPR_LOCATION (c, expr_loc);
12230 if (c == boolean_true_node)
12232 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12233 t = integer_one_node;
12236 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12238 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12239 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12240 OMP_CLAUSE_CHAIN (c) = list;
12241 list = c;
12244 return list;
12247 /* OpenMP 4.5:
12248 priority ( expression ) */
12250 static tree
12251 c_parser_omp_clause_priority (c_parser *parser, tree list)
12253 location_t priority_loc = c_parser_peek_token (parser)->location;
12254 matching_parens parens;
12255 if (parens.require_open (parser))
12257 location_t expr_loc = c_parser_peek_token (parser)->location;
12258 c_expr expr = c_parser_expression (parser);
12259 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12260 tree c, t = expr.value;
12261 t = c_fully_fold (t, false, NULL);
12263 parens.skip_until_found_close (parser);
12265 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12267 c_parser_error (parser, "expected integer expression");
12268 return list;
12271 /* Attempt to statically determine when the number isn't
12272 non-negative. */
12273 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12274 build_int_cst (TREE_TYPE (t), 0));
12275 if (CAN_HAVE_LOCATION_P (c))
12276 SET_EXPR_LOCATION (c, expr_loc);
12277 if (c == boolean_true_node)
12279 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12280 t = integer_one_node;
12283 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12285 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12286 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12287 OMP_CLAUSE_CHAIN (c) = list;
12288 list = c;
12291 return list;
12294 /* OpenMP 4.5:
12295 hint ( expression ) */
12297 static tree
12298 c_parser_omp_clause_hint (c_parser *parser, tree list)
12300 location_t hint_loc = c_parser_peek_token (parser)->location;
12301 matching_parens parens;
12302 if (parens.require_open (parser))
12304 location_t expr_loc = c_parser_peek_token (parser)->location;
12305 c_expr expr = c_parser_expression (parser);
12306 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12307 tree c, t = expr.value;
12308 t = c_fully_fold (t, false, NULL);
12310 parens.skip_until_found_close (parser);
12312 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12314 c_parser_error (parser, "expected integer expression");
12315 return list;
12318 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12320 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12321 OMP_CLAUSE_HINT_EXPR (c) = t;
12322 OMP_CLAUSE_CHAIN (c) = list;
12323 list = c;
12326 return list;
12329 /* OpenMP 4.5:
12330 defaultmap ( tofrom : scalar ) */
12332 static tree
12333 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12335 location_t loc = c_parser_peek_token (parser)->location;
12336 tree c;
12337 const char *p;
12339 matching_parens parens;
12340 if (!parens.require_open (parser))
12341 return list;
12342 if (!c_parser_next_token_is (parser, CPP_NAME))
12344 c_parser_error (parser, "expected %<tofrom%>");
12345 goto out_err;
12347 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12348 if (strcmp (p, "tofrom") != 0)
12350 c_parser_error (parser, "expected %<tofrom%>");
12351 goto out_err;
12353 c_parser_consume_token (parser);
12354 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12355 goto out_err;
12356 if (!c_parser_next_token_is (parser, CPP_NAME))
12358 c_parser_error (parser, "expected %<scalar%>");
12359 goto out_err;
12361 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12362 if (strcmp (p, "scalar") != 0)
12364 c_parser_error (parser, "expected %<scalar%>");
12365 goto out_err;
12367 c_parser_consume_token (parser);
12368 parens.skip_until_found_close (parser);
12369 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12370 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12371 OMP_CLAUSE_CHAIN (c) = list;
12372 return c;
12374 out_err:
12375 parens.skip_until_found_close (parser);
12376 return list;
12379 /* OpenACC 2.0:
12380 use_device ( variable-list )
12382 OpenMP 4.5:
12383 use_device_ptr ( variable-list ) */
12385 static tree
12386 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12388 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12389 list);
12392 /* OpenMP 4.5:
12393 is_device_ptr ( variable-list ) */
12395 static tree
12396 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12398 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12401 /* OpenACC:
12402 num_gangs ( expression )
12403 num_workers ( expression )
12404 vector_length ( expression ) */
12406 static tree
12407 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12408 tree list)
12410 location_t loc = c_parser_peek_token (parser)->location;
12412 matching_parens parens;
12413 if (!parens.require_open (parser))
12414 return list;
12416 location_t expr_loc = c_parser_peek_token (parser)->location;
12417 c_expr expr = c_parser_expression (parser);
12418 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12419 tree c, t = expr.value;
12420 t = c_fully_fold (t, false, NULL);
12422 parens.skip_until_found_close (parser);
12424 if (t == error_mark_node)
12425 return list;
12426 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12428 error_at (expr_loc, "%qs expression must be integral",
12429 omp_clause_code_name[code]);
12430 return list;
12433 /* Attempt to statically determine when the number isn't positive. */
12434 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12435 build_int_cst (TREE_TYPE (t), 0));
12436 protected_set_expr_location (c, expr_loc);
12437 if (c == boolean_true_node)
12439 warning_at (expr_loc, 0,
12440 "%qs value must be positive",
12441 omp_clause_code_name[code]);
12442 t = integer_one_node;
12445 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12447 c = build_omp_clause (loc, code);
12448 OMP_CLAUSE_OPERAND (c, 0) = t;
12449 OMP_CLAUSE_CHAIN (c) = list;
12450 return c;
12453 /* OpenACC:
12455 gang [( gang-arg-list )]
12456 worker [( [num:] int-expr )]
12457 vector [( [length:] int-expr )]
12459 where gang-arg is one of:
12461 [num:] int-expr
12462 static: size-expr
12464 and size-expr may be:
12467 int-expr
12470 static tree
12471 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12472 const char *str, tree list)
12474 const char *id = "num";
12475 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12476 location_t loc = c_parser_peek_token (parser)->location;
12478 if (kind == OMP_CLAUSE_VECTOR)
12479 id = "length";
12481 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12483 c_parser_consume_token (parser);
12487 c_token *next = c_parser_peek_token (parser);
12488 int idx = 0;
12490 /* Gang static argument. */
12491 if (kind == OMP_CLAUSE_GANG
12492 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12494 c_parser_consume_token (parser);
12496 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12497 goto cleanup_error;
12499 idx = 1;
12500 if (ops[idx] != NULL_TREE)
12502 c_parser_error (parser, "too many %<static%> arguments");
12503 goto cleanup_error;
12506 /* Check for the '*' argument. */
12507 if (c_parser_next_token_is (parser, CPP_MULT)
12508 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12509 || c_parser_peek_2nd_token (parser)->type
12510 == CPP_CLOSE_PAREN))
12512 c_parser_consume_token (parser);
12513 ops[idx] = integer_minus_one_node;
12515 if (c_parser_next_token_is (parser, CPP_COMMA))
12517 c_parser_consume_token (parser);
12518 continue;
12520 else
12521 break;
12524 /* Worker num: argument and vector length: arguments. */
12525 else if (c_parser_next_token_is (parser, CPP_NAME)
12526 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12527 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12529 c_parser_consume_token (parser); /* id */
12530 c_parser_consume_token (parser); /* ':' */
12533 /* Now collect the actual argument. */
12534 if (ops[idx] != NULL_TREE)
12536 c_parser_error (parser, "unexpected argument");
12537 goto cleanup_error;
12540 location_t expr_loc = c_parser_peek_token (parser)->location;
12541 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12542 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12543 tree expr = cexpr.value;
12544 if (expr == error_mark_node)
12545 goto cleanup_error;
12547 expr = c_fully_fold (expr, false, NULL);
12549 /* Attempt to statically determine when the number isn't a
12550 positive integer. */
12552 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12554 c_parser_error (parser, "expected integer expression");
12555 return list;
12558 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12559 build_int_cst (TREE_TYPE (expr), 0));
12560 if (c == boolean_true_node)
12562 warning_at (loc, 0,
12563 "%qs value must be positive", str);
12564 expr = integer_one_node;
12567 ops[idx] = expr;
12569 if (kind == OMP_CLAUSE_GANG
12570 && c_parser_next_token_is (parser, CPP_COMMA))
12572 c_parser_consume_token (parser);
12573 continue;
12575 break;
12577 while (1);
12579 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12580 goto cleanup_error;
12583 check_no_duplicate_clause (list, kind, str);
12585 c = build_omp_clause (loc, kind);
12587 if (ops[1])
12588 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12590 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12591 OMP_CLAUSE_CHAIN (c) = list;
12593 return c;
12595 cleanup_error:
12596 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12597 return list;
12600 /* OpenACC:
12601 auto
12602 independent
12603 nohost
12604 seq */
12606 static tree
12607 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12608 tree list)
12610 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12612 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12613 OMP_CLAUSE_CHAIN (c) = list;
12615 return c;
12618 /* OpenACC:
12619 async [( int-expr )] */
12621 static tree
12622 c_parser_oacc_clause_async (c_parser *parser, tree list)
12624 tree c, t;
12625 location_t loc = c_parser_peek_token (parser)->location;
12627 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12629 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12631 c_parser_consume_token (parser);
12633 t = c_parser_expression (parser).value;
12634 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12635 c_parser_error (parser, "expected integer expression");
12636 else if (t == error_mark_node
12637 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12638 return list;
12640 else
12641 t = c_fully_fold (t, false, NULL);
12643 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12645 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12646 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12647 OMP_CLAUSE_CHAIN (c) = list;
12648 list = c;
12650 return list;
12653 /* OpenACC 2.0:
12654 tile ( size-expr-list ) */
12656 static tree
12657 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12659 tree c, expr = error_mark_node;
12660 location_t loc;
12661 tree tile = NULL_TREE;
12663 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12664 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12666 loc = c_parser_peek_token (parser)->location;
12667 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12668 return list;
12672 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12673 return list;
12675 if (c_parser_next_token_is (parser, CPP_MULT)
12676 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12677 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12679 c_parser_consume_token (parser);
12680 expr = integer_zero_node;
12682 else
12684 location_t expr_loc = c_parser_peek_token (parser)->location;
12685 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12686 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12687 expr = cexpr.value;
12689 if (expr == error_mark_node)
12691 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12692 "expected %<)%>");
12693 return list;
12696 expr = c_fully_fold (expr, false, NULL);
12698 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12699 || !tree_fits_shwi_p (expr)
12700 || tree_to_shwi (expr) <= 0)
12702 error_at (expr_loc, "%<tile%> argument needs positive"
12703 " integral constant");
12704 expr = integer_zero_node;
12708 tile = tree_cons (NULL_TREE, expr, tile);
12710 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12712 /* Consume the trailing ')'. */
12713 c_parser_consume_token (parser);
12715 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12716 tile = nreverse (tile);
12717 OMP_CLAUSE_TILE_LIST (c) = tile;
12718 OMP_CLAUSE_CHAIN (c) = list;
12719 return c;
12722 /* OpenACC:
12723 wait ( int-expr-list ) */
12725 static tree
12726 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12728 location_t clause_loc = c_parser_peek_token (parser)->location;
12730 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12731 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12733 return list;
12736 /* OpenMP 2.5:
12737 ordered
12739 OpenMP 4.5:
12740 ordered ( constant-expression ) */
12742 static tree
12743 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12745 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12747 tree c, num = NULL_TREE;
12748 HOST_WIDE_INT n;
12749 location_t loc = c_parser_peek_token (parser)->location;
12750 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12752 matching_parens parens;
12753 parens.consume_open (parser);
12754 num = c_parser_expr_no_commas (parser, NULL).value;
12755 parens.skip_until_found_close (parser);
12757 if (num == error_mark_node)
12758 return list;
12759 if (num)
12761 mark_exp_read (num);
12762 num = c_fully_fold (num, false, NULL);
12763 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12764 || !tree_fits_shwi_p (num)
12765 || (n = tree_to_shwi (num)) <= 0
12766 || (int) n != n)
12768 error_at (loc, "ordered argument needs positive "
12769 "constant integer expression");
12770 return list;
12773 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12774 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12775 OMP_CLAUSE_CHAIN (c) = list;
12776 return c;
12779 /* OpenMP 2.5:
12780 private ( variable-list ) */
12782 static tree
12783 c_parser_omp_clause_private (c_parser *parser, tree list)
12785 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12788 /* OpenMP 2.5:
12789 reduction ( reduction-operator : variable-list )
12791 reduction-operator:
12792 One of: + * - & ^ | && ||
12794 OpenMP 3.1:
12796 reduction-operator:
12797 One of: + * - & ^ | && || max min
12799 OpenMP 4.0:
12801 reduction-operator:
12802 One of: + * - & ^ | && ||
12803 identifier */
12805 static tree
12806 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12808 location_t clause_loc = c_parser_peek_token (parser)->location;
12809 matching_parens parens;
12810 if (parens.require_open (parser))
12812 enum tree_code code = ERROR_MARK;
12813 tree reduc_id = NULL_TREE;
12815 switch (c_parser_peek_token (parser)->type)
12817 case CPP_PLUS:
12818 code = PLUS_EXPR;
12819 break;
12820 case CPP_MULT:
12821 code = MULT_EXPR;
12822 break;
12823 case CPP_MINUS:
12824 code = MINUS_EXPR;
12825 break;
12826 case CPP_AND:
12827 code = BIT_AND_EXPR;
12828 break;
12829 case CPP_XOR:
12830 code = BIT_XOR_EXPR;
12831 break;
12832 case CPP_OR:
12833 code = BIT_IOR_EXPR;
12834 break;
12835 case CPP_AND_AND:
12836 code = TRUTH_ANDIF_EXPR;
12837 break;
12838 case CPP_OR_OR:
12839 code = TRUTH_ORIF_EXPR;
12840 break;
12841 case CPP_NAME:
12843 const char *p
12844 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12845 if (strcmp (p, "min") == 0)
12847 code = MIN_EXPR;
12848 break;
12850 if (strcmp (p, "max") == 0)
12852 code = MAX_EXPR;
12853 break;
12855 reduc_id = c_parser_peek_token (parser)->value;
12856 break;
12858 default:
12859 c_parser_error (parser,
12860 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12861 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12863 return list;
12865 c_parser_consume_token (parser);
12866 reduc_id = c_omp_reduction_id (code, reduc_id);
12867 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12869 tree nl, c;
12871 nl = c_parser_omp_variable_list (parser, clause_loc,
12872 OMP_CLAUSE_REDUCTION, list);
12873 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12875 tree d = OMP_CLAUSE_DECL (c), type;
12876 if (TREE_CODE (d) != TREE_LIST)
12877 type = TREE_TYPE (d);
12878 else
12880 int cnt = 0;
12881 tree t;
12882 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12883 cnt++;
12884 type = TREE_TYPE (t);
12885 while (cnt > 0)
12887 if (TREE_CODE (type) != POINTER_TYPE
12888 && TREE_CODE (type) != ARRAY_TYPE)
12889 break;
12890 type = TREE_TYPE (type);
12891 cnt--;
12894 while (TREE_CODE (type) == ARRAY_TYPE)
12895 type = TREE_TYPE (type);
12896 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12897 if (code == ERROR_MARK
12898 || !(INTEGRAL_TYPE_P (type)
12899 || TREE_CODE (type) == REAL_TYPE
12900 || TREE_CODE (type) == COMPLEX_TYPE))
12901 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12902 = c_omp_reduction_lookup (reduc_id,
12903 TYPE_MAIN_VARIANT (type));
12906 list = nl;
12908 parens.skip_until_found_close (parser);
12910 return list;
12913 /* OpenMP 2.5:
12914 schedule ( schedule-kind )
12915 schedule ( schedule-kind , expression )
12917 schedule-kind:
12918 static | dynamic | guided | runtime | auto
12920 OpenMP 4.5:
12921 schedule ( schedule-modifier : schedule-kind )
12922 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12924 schedule-modifier:
12925 simd
12926 monotonic
12927 nonmonotonic */
12929 static tree
12930 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12932 tree c, t;
12933 location_t loc = c_parser_peek_token (parser)->location;
12934 int modifiers = 0, nmodifiers = 0;
12936 matching_parens parens;
12937 if (!parens.require_open (parser))
12938 return list;
12940 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12942 while (c_parser_next_token_is (parser, CPP_NAME))
12944 tree kind = c_parser_peek_token (parser)->value;
12945 const char *p = IDENTIFIER_POINTER (kind);
12946 if (strcmp ("simd", p) == 0)
12947 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12948 else if (strcmp ("monotonic", p) == 0)
12949 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12950 else if (strcmp ("nonmonotonic", p) == 0)
12951 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12952 else
12953 break;
12954 c_parser_consume_token (parser);
12955 if (nmodifiers++ == 0
12956 && c_parser_next_token_is (parser, CPP_COMMA))
12957 c_parser_consume_token (parser);
12958 else
12960 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12961 break;
12965 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12966 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12967 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12968 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12970 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12971 "specified");
12972 modifiers = 0;
12975 if (c_parser_next_token_is (parser, CPP_NAME))
12977 tree kind = c_parser_peek_token (parser)->value;
12978 const char *p = IDENTIFIER_POINTER (kind);
12980 switch (p[0])
12982 case 'd':
12983 if (strcmp ("dynamic", p) != 0)
12984 goto invalid_kind;
12985 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12986 break;
12988 case 'g':
12989 if (strcmp ("guided", p) != 0)
12990 goto invalid_kind;
12991 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12992 break;
12994 case 'r':
12995 if (strcmp ("runtime", p) != 0)
12996 goto invalid_kind;
12997 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12998 break;
13000 default:
13001 goto invalid_kind;
13004 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
13005 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
13006 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
13007 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
13008 else
13009 goto invalid_kind;
13011 c_parser_consume_token (parser);
13012 if (c_parser_next_token_is (parser, CPP_COMMA))
13014 location_t here;
13015 c_parser_consume_token (parser);
13017 here = c_parser_peek_token (parser)->location;
13018 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13019 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13020 t = expr.value;
13021 t = c_fully_fold (t, false, NULL);
13023 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13024 error_at (here, "schedule %<runtime%> does not take "
13025 "a %<chunk_size%> parameter");
13026 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13027 error_at (here,
13028 "schedule %<auto%> does not take "
13029 "a %<chunk_size%> parameter");
13030 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13032 /* Attempt to statically determine when the number isn't
13033 positive. */
13034 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13035 build_int_cst (TREE_TYPE (t), 0));
13036 protected_set_expr_location (s, loc);
13037 if (s == boolean_true_node)
13039 warning_at (loc, 0,
13040 "chunk size value must be positive");
13041 t = integer_one_node;
13043 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13045 else
13046 c_parser_error (parser, "expected integer expression");
13048 parens.skip_until_found_close (parser);
13050 else
13051 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13052 "expected %<,%> or %<)%>");
13054 OMP_CLAUSE_SCHEDULE_KIND (c)
13055 = (enum omp_clause_schedule_kind)
13056 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13058 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13059 OMP_CLAUSE_CHAIN (c) = list;
13060 return c;
13062 invalid_kind:
13063 c_parser_error (parser, "invalid schedule kind");
13064 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13065 return list;
13068 /* OpenMP 2.5:
13069 shared ( variable-list ) */
13071 static tree
13072 c_parser_omp_clause_shared (c_parser *parser, tree list)
13074 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13077 /* OpenMP 3.0:
13078 untied */
13080 static tree
13081 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13083 tree c;
13085 /* FIXME: Should we allow duplicates? */
13086 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13088 c = build_omp_clause (c_parser_peek_token (parser)->location,
13089 OMP_CLAUSE_UNTIED);
13090 OMP_CLAUSE_CHAIN (c) = list;
13092 return c;
13095 /* OpenMP 4.0:
13096 inbranch
13097 notinbranch */
13099 static tree
13100 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13101 enum omp_clause_code code, tree list)
13103 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13105 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13106 OMP_CLAUSE_CHAIN (c) = list;
13108 return c;
13111 /* OpenMP 4.0:
13112 parallel
13114 sections
13115 taskgroup */
13117 static tree
13118 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13119 enum omp_clause_code code, tree list)
13121 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13122 OMP_CLAUSE_CHAIN (c) = list;
13124 return c;
13127 /* OpenMP 4.5:
13128 nogroup */
13130 static tree
13131 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13133 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13134 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13135 OMP_CLAUSE_NOGROUP);
13136 OMP_CLAUSE_CHAIN (c) = list;
13137 return c;
13140 /* OpenMP 4.5:
13141 simd
13142 threads */
13144 static tree
13145 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13146 enum omp_clause_code code, tree list)
13148 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13149 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13150 OMP_CLAUSE_CHAIN (c) = list;
13151 return c;
13154 /* OpenMP 4.0:
13155 num_teams ( expression ) */
13157 static tree
13158 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13160 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13161 matching_parens parens;
13162 if (parens.require_open (parser))
13164 location_t expr_loc = c_parser_peek_token (parser)->location;
13165 c_expr expr = c_parser_expression (parser);
13166 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13167 tree c, t = expr.value;
13168 t = c_fully_fold (t, false, NULL);
13170 parens.skip_until_found_close (parser);
13172 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13174 c_parser_error (parser, "expected integer expression");
13175 return list;
13178 /* Attempt to statically determine when the number isn't positive. */
13179 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13180 build_int_cst (TREE_TYPE (t), 0));
13181 protected_set_expr_location (c, expr_loc);
13182 if (c == boolean_true_node)
13184 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13185 t = integer_one_node;
13188 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13190 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13191 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13192 OMP_CLAUSE_CHAIN (c) = list;
13193 list = c;
13196 return list;
13199 /* OpenMP 4.0:
13200 thread_limit ( expression ) */
13202 static tree
13203 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13205 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13206 matching_parens parens;
13207 if (parens.require_open (parser))
13209 location_t expr_loc = c_parser_peek_token (parser)->location;
13210 c_expr expr = c_parser_expression (parser);
13211 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13212 tree c, t = expr.value;
13213 t = c_fully_fold (t, false, NULL);
13215 parens.skip_until_found_close (parser);
13217 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13219 c_parser_error (parser, "expected integer expression");
13220 return list;
13223 /* Attempt to statically determine when the number isn't positive. */
13224 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13225 build_int_cst (TREE_TYPE (t), 0));
13226 protected_set_expr_location (c, expr_loc);
13227 if (c == boolean_true_node)
13229 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13230 t = integer_one_node;
13233 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13234 "thread_limit");
13236 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13237 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13238 OMP_CLAUSE_CHAIN (c) = list;
13239 list = c;
13242 return list;
13245 /* OpenMP 4.0:
13246 aligned ( variable-list )
13247 aligned ( variable-list : constant-expression ) */
13249 static tree
13250 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13252 location_t clause_loc = c_parser_peek_token (parser)->location;
13253 tree nl, c;
13255 matching_parens parens;
13256 if (!parens.require_open (parser))
13257 return list;
13259 nl = c_parser_omp_variable_list (parser, clause_loc,
13260 OMP_CLAUSE_ALIGNED, list);
13262 if (c_parser_next_token_is (parser, CPP_COLON))
13264 c_parser_consume_token (parser);
13265 location_t expr_loc = c_parser_peek_token (parser)->location;
13266 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13267 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13268 tree alignment = expr.value;
13269 alignment = c_fully_fold (alignment, false, NULL);
13270 if (TREE_CODE (alignment) != INTEGER_CST
13271 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13272 || tree_int_cst_sgn (alignment) != 1)
13274 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13275 "be positive constant integer expression");
13276 alignment = NULL_TREE;
13279 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13280 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13283 parens.skip_until_found_close (parser);
13284 return nl;
13287 /* OpenMP 4.0:
13288 linear ( variable-list )
13289 linear ( variable-list : expression )
13291 OpenMP 4.5:
13292 linear ( modifier ( variable-list ) )
13293 linear ( modifier ( variable-list ) : expression ) */
13295 static tree
13296 c_parser_omp_clause_linear (c_parser *parser, tree list)
13298 location_t clause_loc = c_parser_peek_token (parser)->location;
13299 tree nl, c, step;
13300 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13302 matching_parens parens;
13303 if (!parens.require_open (parser))
13304 return list;
13306 if (c_parser_next_token_is (parser, CPP_NAME))
13308 c_token *tok = c_parser_peek_token (parser);
13309 const char *p = IDENTIFIER_POINTER (tok->value);
13310 if (strcmp ("val", p) == 0)
13311 kind = OMP_CLAUSE_LINEAR_VAL;
13312 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13313 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13314 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13316 c_parser_consume_token (parser);
13317 c_parser_consume_token (parser);
13321 nl = c_parser_omp_variable_list (parser, clause_loc,
13322 OMP_CLAUSE_LINEAR, list);
13324 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13325 parens.skip_until_found_close (parser);
13327 if (c_parser_next_token_is (parser, CPP_COLON))
13329 c_parser_consume_token (parser);
13330 location_t expr_loc = c_parser_peek_token (parser)->location;
13331 c_expr expr = c_parser_expression (parser);
13332 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13333 step = expr.value;
13334 step = c_fully_fold (step, false, NULL);
13335 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13337 error_at (clause_loc, "%<linear%> clause step expression must "
13338 "be integral");
13339 step = integer_one_node;
13343 else
13344 step = integer_one_node;
13346 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13348 OMP_CLAUSE_LINEAR_STEP (c) = step;
13349 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13352 parens.skip_until_found_close (parser);
13353 return nl;
13356 /* OpenMP 4.0:
13357 safelen ( constant-expression ) */
13359 static tree
13360 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13362 location_t clause_loc = c_parser_peek_token (parser)->location;
13363 tree c, t;
13365 matching_parens parens;
13366 if (!parens.require_open (parser))
13367 return list;
13369 location_t expr_loc = c_parser_peek_token (parser)->location;
13370 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13371 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13372 t = expr.value;
13373 t = c_fully_fold (t, false, NULL);
13374 if (TREE_CODE (t) != INTEGER_CST
13375 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13376 || tree_int_cst_sgn (t) != 1)
13378 error_at (clause_loc, "%<safelen%> clause expression must "
13379 "be positive constant integer expression");
13380 t = NULL_TREE;
13383 parens.skip_until_found_close (parser);
13384 if (t == NULL_TREE || t == error_mark_node)
13385 return list;
13387 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13389 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13390 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13391 OMP_CLAUSE_CHAIN (c) = list;
13392 return c;
13395 /* OpenMP 4.0:
13396 simdlen ( constant-expression ) */
13398 static tree
13399 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13401 location_t clause_loc = c_parser_peek_token (parser)->location;
13402 tree c, t;
13404 matching_parens parens;
13405 if (!parens.require_open (parser))
13406 return list;
13408 location_t expr_loc = c_parser_peek_token (parser)->location;
13409 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13410 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13411 t = expr.value;
13412 t = c_fully_fold (t, false, NULL);
13413 if (TREE_CODE (t) != INTEGER_CST
13414 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13415 || tree_int_cst_sgn (t) != 1)
13417 error_at (clause_loc, "%<simdlen%> clause expression must "
13418 "be positive constant integer expression");
13419 t = NULL_TREE;
13422 parens.skip_until_found_close (parser);
13423 if (t == NULL_TREE || t == error_mark_node)
13424 return list;
13426 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13428 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13429 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13430 OMP_CLAUSE_CHAIN (c) = list;
13431 return c;
13434 /* OpenMP 4.5:
13435 vec:
13436 identifier [+/- integer]
13437 vec , identifier [+/- integer]
13440 static tree
13441 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13442 tree list)
13444 tree vec = NULL;
13445 if (c_parser_next_token_is_not (parser, CPP_NAME)
13446 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13448 c_parser_error (parser, "expected identifier");
13449 return list;
13452 while (c_parser_next_token_is (parser, CPP_NAME)
13453 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13455 tree t = lookup_name (c_parser_peek_token (parser)->value);
13456 tree addend = NULL;
13458 if (t == NULL_TREE)
13460 undeclared_variable (c_parser_peek_token (parser)->location,
13461 c_parser_peek_token (parser)->value);
13462 t = error_mark_node;
13465 c_parser_consume_token (parser);
13467 bool neg = false;
13468 if (c_parser_next_token_is (parser, CPP_MINUS))
13469 neg = true;
13470 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13472 addend = integer_zero_node;
13473 neg = false;
13474 goto add_to_vector;
13476 c_parser_consume_token (parser);
13478 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13480 c_parser_error (parser, "expected integer");
13481 return list;
13484 addend = c_parser_peek_token (parser)->value;
13485 if (TREE_CODE (addend) != INTEGER_CST)
13487 c_parser_error (parser, "expected integer");
13488 return list;
13490 c_parser_consume_token (parser);
13492 add_to_vector:
13493 if (t != error_mark_node)
13495 vec = tree_cons (addend, t, vec);
13496 if (neg)
13497 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13500 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13501 break;
13503 c_parser_consume_token (parser);
13506 if (vec == NULL_TREE)
13507 return list;
13509 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13510 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13511 OMP_CLAUSE_DECL (u) = nreverse (vec);
13512 OMP_CLAUSE_CHAIN (u) = list;
13513 return u;
13516 /* OpenMP 4.0:
13517 depend ( depend-kind: variable-list )
13519 depend-kind:
13520 in | out | inout
13522 OpenMP 4.5:
13523 depend ( source )
13525 depend ( sink : vec ) */
13527 static tree
13528 c_parser_omp_clause_depend (c_parser *parser, tree list)
13530 location_t clause_loc = c_parser_peek_token (parser)->location;
13531 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13532 tree nl, c;
13534 matching_parens parens;
13535 if (!parens.require_open (parser))
13536 return list;
13538 if (c_parser_next_token_is (parser, CPP_NAME))
13540 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13541 if (strcmp ("in", p) == 0)
13542 kind = OMP_CLAUSE_DEPEND_IN;
13543 else if (strcmp ("inout", p) == 0)
13544 kind = OMP_CLAUSE_DEPEND_INOUT;
13545 else if (strcmp ("out", p) == 0)
13546 kind = OMP_CLAUSE_DEPEND_OUT;
13547 else if (strcmp ("source", p) == 0)
13548 kind = OMP_CLAUSE_DEPEND_SOURCE;
13549 else if (strcmp ("sink", p) == 0)
13550 kind = OMP_CLAUSE_DEPEND_SINK;
13551 else
13552 goto invalid_kind;
13554 else
13555 goto invalid_kind;
13557 c_parser_consume_token (parser);
13559 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13561 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13562 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13563 OMP_CLAUSE_DECL (c) = NULL_TREE;
13564 OMP_CLAUSE_CHAIN (c) = list;
13565 parens.skip_until_found_close (parser);
13566 return c;
13569 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13570 goto resync_fail;
13572 if (kind == OMP_CLAUSE_DEPEND_SINK)
13573 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13574 else
13576 nl = c_parser_omp_variable_list (parser, clause_loc,
13577 OMP_CLAUSE_DEPEND, list);
13579 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13580 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13583 parens.skip_until_found_close (parser);
13584 return nl;
13586 invalid_kind:
13587 c_parser_error (parser, "invalid depend kind");
13588 resync_fail:
13589 parens.skip_until_found_close (parser);
13590 return list;
13593 /* OpenMP 4.0:
13594 map ( map-kind: variable-list )
13595 map ( variable-list )
13597 map-kind:
13598 alloc | to | from | tofrom
13600 OpenMP 4.5:
13601 map-kind:
13602 alloc | to | from | tofrom | release | delete
13604 map ( always [,] map-kind: variable-list ) */
13606 static tree
13607 c_parser_omp_clause_map (c_parser *parser, tree list)
13609 location_t clause_loc = c_parser_peek_token (parser)->location;
13610 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13611 int always = 0;
13612 enum c_id_kind always_id_kind = C_ID_NONE;
13613 location_t always_loc = UNKNOWN_LOCATION;
13614 tree always_id = NULL_TREE;
13615 tree nl, c;
13617 matching_parens parens;
13618 if (!parens.require_open (parser))
13619 return list;
13621 if (c_parser_next_token_is (parser, CPP_NAME))
13623 c_token *tok = c_parser_peek_token (parser);
13624 const char *p = IDENTIFIER_POINTER (tok->value);
13625 always_id_kind = tok->id_kind;
13626 always_loc = tok->location;
13627 always_id = tok->value;
13628 if (strcmp ("always", p) == 0)
13630 c_token *sectok = c_parser_peek_2nd_token (parser);
13631 if (sectok->type == CPP_COMMA)
13633 c_parser_consume_token (parser);
13634 c_parser_consume_token (parser);
13635 always = 2;
13637 else if (sectok->type == CPP_NAME)
13639 p = IDENTIFIER_POINTER (sectok->value);
13640 if (strcmp ("alloc", p) == 0
13641 || strcmp ("to", p) == 0
13642 || strcmp ("from", p) == 0
13643 || strcmp ("tofrom", p) == 0
13644 || strcmp ("release", p) == 0
13645 || strcmp ("delete", p) == 0)
13647 c_parser_consume_token (parser);
13648 always = 1;
13654 if (c_parser_next_token_is (parser, CPP_NAME)
13655 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13657 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13658 if (strcmp ("alloc", p) == 0)
13659 kind = GOMP_MAP_ALLOC;
13660 else if (strcmp ("to", p) == 0)
13661 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13662 else if (strcmp ("from", p) == 0)
13663 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13664 else if (strcmp ("tofrom", p) == 0)
13665 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13666 else if (strcmp ("release", p) == 0)
13667 kind = GOMP_MAP_RELEASE;
13668 else if (strcmp ("delete", p) == 0)
13669 kind = GOMP_MAP_DELETE;
13670 else
13672 c_parser_error (parser, "invalid map kind");
13673 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13674 "expected %<)%>");
13675 return list;
13677 c_parser_consume_token (parser);
13678 c_parser_consume_token (parser);
13680 else if (always)
13682 if (always_id_kind != C_ID_ID)
13684 c_parser_error (parser, "expected identifier");
13685 parens.skip_until_found_close (parser);
13686 return list;
13689 tree t = lookup_name (always_id);
13690 if (t == NULL_TREE)
13692 undeclared_variable (always_loc, always_id);
13693 t = error_mark_node;
13695 if (t != error_mark_node)
13697 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13698 OMP_CLAUSE_DECL (u) = t;
13699 OMP_CLAUSE_CHAIN (u) = list;
13700 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13701 list = u;
13703 if (always == 1)
13705 parens.skip_until_found_close (parser);
13706 return list;
13710 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13712 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13713 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13715 parens.skip_until_found_close (parser);
13716 return nl;
13719 /* OpenMP 4.0:
13720 device ( expression ) */
13722 static tree
13723 c_parser_omp_clause_device (c_parser *parser, tree list)
13725 location_t clause_loc = c_parser_peek_token (parser)->location;
13726 matching_parens parens;
13727 if (parens.require_open (parser))
13729 location_t expr_loc = c_parser_peek_token (parser)->location;
13730 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13731 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13732 tree c, t = expr.value;
13733 t = c_fully_fold (t, false, NULL);
13735 parens.skip_until_found_close (parser);
13737 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13739 c_parser_error (parser, "expected integer expression");
13740 return list;
13743 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13745 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13746 OMP_CLAUSE_DEVICE_ID (c) = t;
13747 OMP_CLAUSE_CHAIN (c) = list;
13748 list = c;
13751 return list;
13754 /* OpenMP 4.0:
13755 dist_schedule ( static )
13756 dist_schedule ( static , expression ) */
13758 static tree
13759 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13761 tree c, t = NULL_TREE;
13762 location_t loc = c_parser_peek_token (parser)->location;
13764 matching_parens parens;
13765 if (!parens.require_open (parser))
13766 return list;
13768 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13770 c_parser_error (parser, "invalid dist_schedule kind");
13771 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13772 "expected %<)%>");
13773 return list;
13776 c_parser_consume_token (parser);
13777 if (c_parser_next_token_is (parser, CPP_COMMA))
13779 c_parser_consume_token (parser);
13781 location_t expr_loc = c_parser_peek_token (parser)->location;
13782 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13783 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13784 t = expr.value;
13785 t = c_fully_fold (t, false, NULL);
13786 parens.skip_until_found_close (parser);
13788 else
13789 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13790 "expected %<,%> or %<)%>");
13792 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13793 if (t == error_mark_node)
13794 return list;
13796 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13797 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13798 OMP_CLAUSE_CHAIN (c) = list;
13799 return c;
13802 /* OpenMP 4.0:
13803 proc_bind ( proc-bind-kind )
13805 proc-bind-kind:
13806 master | close | spread */
13808 static tree
13809 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13811 location_t clause_loc = c_parser_peek_token (parser)->location;
13812 enum omp_clause_proc_bind_kind kind;
13813 tree c;
13815 matching_parens parens;
13816 if (!parens.require_open (parser))
13817 return list;
13819 if (c_parser_next_token_is (parser, CPP_NAME))
13821 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13822 if (strcmp ("master", p) == 0)
13823 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13824 else if (strcmp ("close", p) == 0)
13825 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13826 else if (strcmp ("spread", p) == 0)
13827 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13828 else
13829 goto invalid_kind;
13831 else
13832 goto invalid_kind;
13834 c_parser_consume_token (parser);
13835 parens.skip_until_found_close (parser);
13836 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13837 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13838 OMP_CLAUSE_CHAIN (c) = list;
13839 return c;
13841 invalid_kind:
13842 c_parser_error (parser, "invalid proc_bind kind");
13843 parens.skip_until_found_close (parser);
13844 return list;
13847 /* OpenMP 4.0:
13848 to ( variable-list ) */
13850 static tree
13851 c_parser_omp_clause_to (c_parser *parser, tree list)
13853 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13856 /* OpenMP 4.0:
13857 from ( variable-list ) */
13859 static tree
13860 c_parser_omp_clause_from (c_parser *parser, tree list)
13862 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13865 /* OpenMP 4.0:
13866 uniform ( variable-list ) */
13868 static tree
13869 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13871 /* The clauses location. */
13872 location_t loc = c_parser_peek_token (parser)->location;
13874 matching_parens parens;
13875 if (parens.require_open (parser))
13877 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13878 list);
13879 parens.skip_until_found_close (parser);
13881 return list;
13884 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13885 is a bitmask in MASK. Return the list of clauses found. */
13887 static tree
13888 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13889 const char *where, bool finish_p = true)
13891 tree clauses = NULL;
13892 bool first = true;
13894 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13896 location_t here;
13897 pragma_omp_clause c_kind;
13898 const char *c_name;
13899 tree prev = clauses;
13901 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13902 c_parser_consume_token (parser);
13904 here = c_parser_peek_token (parser)->location;
13905 c_kind = c_parser_omp_clause_name (parser);
13907 switch (c_kind)
13909 case PRAGMA_OACC_CLAUSE_ASYNC:
13910 clauses = c_parser_oacc_clause_async (parser, clauses);
13911 c_name = "async";
13912 break;
13913 case PRAGMA_OACC_CLAUSE_AUTO:
13914 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13915 clauses);
13916 c_name = "auto";
13917 break;
13918 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13919 clauses = c_parser_omp_clause_collapse (parser, clauses);
13920 c_name = "collapse";
13921 break;
13922 case PRAGMA_OACC_CLAUSE_COPY:
13923 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13924 c_name = "copy";
13925 break;
13926 case PRAGMA_OACC_CLAUSE_COPYIN:
13927 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13928 c_name = "copyin";
13929 break;
13930 case PRAGMA_OACC_CLAUSE_COPYOUT:
13931 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13932 c_name = "copyout";
13933 break;
13934 case PRAGMA_OACC_CLAUSE_CREATE:
13935 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13936 c_name = "create";
13937 break;
13938 case PRAGMA_OACC_CLAUSE_DELETE:
13939 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13940 c_name = "delete";
13941 break;
13942 case PRAGMA_OMP_CLAUSE_DEFAULT:
13943 clauses = c_parser_omp_clause_default (parser, clauses, true);
13944 c_name = "default";
13945 break;
13946 case PRAGMA_OACC_CLAUSE_DEVICE:
13947 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13948 c_name = "device";
13949 break;
13950 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13951 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13952 c_name = "deviceptr";
13953 break;
13954 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13955 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13956 c_name = "device_resident";
13957 break;
13958 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13959 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13960 c_name = "firstprivate";
13961 break;
13962 case PRAGMA_OACC_CLAUSE_GANG:
13963 c_name = "gang";
13964 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13965 c_name, clauses);
13966 break;
13967 case PRAGMA_OACC_CLAUSE_HOST:
13968 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13969 c_name = "host";
13970 break;
13971 case PRAGMA_OACC_CLAUSE_IF:
13972 clauses = c_parser_omp_clause_if (parser, clauses, false);
13973 c_name = "if";
13974 break;
13975 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13976 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13977 clauses);
13978 c_name = "independent";
13979 break;
13980 case PRAGMA_OACC_CLAUSE_LINK:
13981 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13982 c_name = "link";
13983 break;
13984 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13985 clauses = c_parser_oacc_single_int_clause (parser,
13986 OMP_CLAUSE_NUM_GANGS,
13987 clauses);
13988 c_name = "num_gangs";
13989 break;
13990 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13991 clauses = c_parser_oacc_single_int_clause (parser,
13992 OMP_CLAUSE_NUM_WORKERS,
13993 clauses);
13994 c_name = "num_workers";
13995 break;
13996 case PRAGMA_OACC_CLAUSE_PRESENT:
13997 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13998 c_name = "present";
13999 break;
14000 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
14001 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14002 c_name = "present_or_copy";
14003 break;
14004 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
14005 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14006 c_name = "present_or_copyin";
14007 break;
14008 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
14009 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14010 c_name = "present_or_copyout";
14011 break;
14012 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
14013 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14014 c_name = "present_or_create";
14015 break;
14016 case PRAGMA_OACC_CLAUSE_PRIVATE:
14017 clauses = c_parser_omp_clause_private (parser, clauses);
14018 c_name = "private";
14019 break;
14020 case PRAGMA_OACC_CLAUSE_REDUCTION:
14021 clauses = c_parser_omp_clause_reduction (parser, clauses);
14022 c_name = "reduction";
14023 break;
14024 case PRAGMA_OACC_CLAUSE_SELF:
14025 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14026 c_name = "self";
14027 break;
14028 case PRAGMA_OACC_CLAUSE_SEQ:
14029 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14030 clauses);
14031 c_name = "seq";
14032 break;
14033 case PRAGMA_OACC_CLAUSE_TILE:
14034 clauses = c_parser_oacc_clause_tile (parser, clauses);
14035 c_name = "tile";
14036 break;
14037 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14038 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14039 c_name = "use_device";
14040 break;
14041 case PRAGMA_OACC_CLAUSE_VECTOR:
14042 c_name = "vector";
14043 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14044 c_name, clauses);
14045 break;
14046 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14047 clauses = c_parser_oacc_single_int_clause (parser,
14048 OMP_CLAUSE_VECTOR_LENGTH,
14049 clauses);
14050 c_name = "vector_length";
14051 break;
14052 case PRAGMA_OACC_CLAUSE_WAIT:
14053 clauses = c_parser_oacc_clause_wait (parser, clauses);
14054 c_name = "wait";
14055 break;
14056 case PRAGMA_OACC_CLAUSE_WORKER:
14057 c_name = "worker";
14058 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14059 c_name, clauses);
14060 break;
14061 default:
14062 c_parser_error (parser, "expected %<#pragma acc%> clause");
14063 goto saw_error;
14066 first = false;
14068 if (((mask >> c_kind) & 1) == 0)
14070 /* Remove the invalid clause(s) from the list to avoid
14071 confusing the rest of the compiler. */
14072 clauses = prev;
14073 error_at (here, "%qs is not valid for %qs", c_name, where);
14077 saw_error:
14078 c_parser_skip_to_pragma_eol (parser);
14080 if (finish_p)
14081 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14083 return clauses;
14086 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14087 is a bitmask in MASK. Return the list of clauses found. */
14089 static tree
14090 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14091 const char *where, bool finish_p = true)
14093 tree clauses = NULL;
14094 bool first = true;
14096 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14098 location_t here;
14099 pragma_omp_clause c_kind;
14100 const char *c_name;
14101 tree prev = clauses;
14103 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14104 c_parser_consume_token (parser);
14106 here = c_parser_peek_token (parser)->location;
14107 c_kind = c_parser_omp_clause_name (parser);
14109 switch (c_kind)
14111 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14112 clauses = c_parser_omp_clause_collapse (parser, clauses);
14113 c_name = "collapse";
14114 break;
14115 case PRAGMA_OMP_CLAUSE_COPYIN:
14116 clauses = c_parser_omp_clause_copyin (parser, clauses);
14117 c_name = "copyin";
14118 break;
14119 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14120 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14121 c_name = "copyprivate";
14122 break;
14123 case PRAGMA_OMP_CLAUSE_DEFAULT:
14124 clauses = c_parser_omp_clause_default (parser, clauses, false);
14125 c_name = "default";
14126 break;
14127 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14128 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14129 c_name = "firstprivate";
14130 break;
14131 case PRAGMA_OMP_CLAUSE_FINAL:
14132 clauses = c_parser_omp_clause_final (parser, clauses);
14133 c_name = "final";
14134 break;
14135 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14136 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14137 c_name = "grainsize";
14138 break;
14139 case PRAGMA_OMP_CLAUSE_HINT:
14140 clauses = c_parser_omp_clause_hint (parser, clauses);
14141 c_name = "hint";
14142 break;
14143 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14144 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14145 c_name = "defaultmap";
14146 break;
14147 case PRAGMA_OMP_CLAUSE_IF:
14148 clauses = c_parser_omp_clause_if (parser, clauses, true);
14149 c_name = "if";
14150 break;
14151 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14152 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14153 c_name = "lastprivate";
14154 break;
14155 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14156 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14157 c_name = "mergeable";
14158 break;
14159 case PRAGMA_OMP_CLAUSE_NOWAIT:
14160 clauses = c_parser_omp_clause_nowait (parser, clauses);
14161 c_name = "nowait";
14162 break;
14163 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14164 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14165 c_name = "num_tasks";
14166 break;
14167 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14168 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14169 c_name = "num_threads";
14170 break;
14171 case PRAGMA_OMP_CLAUSE_ORDERED:
14172 clauses = c_parser_omp_clause_ordered (parser, clauses);
14173 c_name = "ordered";
14174 break;
14175 case PRAGMA_OMP_CLAUSE_PRIORITY:
14176 clauses = c_parser_omp_clause_priority (parser, clauses);
14177 c_name = "priority";
14178 break;
14179 case PRAGMA_OMP_CLAUSE_PRIVATE:
14180 clauses = c_parser_omp_clause_private (parser, clauses);
14181 c_name = "private";
14182 break;
14183 case PRAGMA_OMP_CLAUSE_REDUCTION:
14184 clauses = c_parser_omp_clause_reduction (parser, clauses);
14185 c_name = "reduction";
14186 break;
14187 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14188 clauses = c_parser_omp_clause_schedule (parser, clauses);
14189 c_name = "schedule";
14190 break;
14191 case PRAGMA_OMP_CLAUSE_SHARED:
14192 clauses = c_parser_omp_clause_shared (parser, clauses);
14193 c_name = "shared";
14194 break;
14195 case PRAGMA_OMP_CLAUSE_UNTIED:
14196 clauses = c_parser_omp_clause_untied (parser, clauses);
14197 c_name = "untied";
14198 break;
14199 case PRAGMA_OMP_CLAUSE_INBRANCH:
14200 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14201 clauses);
14202 c_name = "inbranch";
14203 break;
14204 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14205 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14206 clauses);
14207 c_name = "notinbranch";
14208 break;
14209 case PRAGMA_OMP_CLAUSE_PARALLEL:
14210 clauses
14211 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14212 clauses);
14213 c_name = "parallel";
14214 if (!first)
14216 clause_not_first:
14217 error_at (here, "%qs must be the first clause of %qs",
14218 c_name, where);
14219 clauses = prev;
14221 break;
14222 case PRAGMA_OMP_CLAUSE_FOR:
14223 clauses
14224 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14225 clauses);
14226 c_name = "for";
14227 if (!first)
14228 goto clause_not_first;
14229 break;
14230 case PRAGMA_OMP_CLAUSE_SECTIONS:
14231 clauses
14232 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14233 clauses);
14234 c_name = "sections";
14235 if (!first)
14236 goto clause_not_first;
14237 break;
14238 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14239 clauses
14240 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14241 clauses);
14242 c_name = "taskgroup";
14243 if (!first)
14244 goto clause_not_first;
14245 break;
14246 case PRAGMA_OMP_CLAUSE_LINK:
14247 clauses
14248 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14249 c_name = "link";
14250 break;
14251 case PRAGMA_OMP_CLAUSE_TO:
14252 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14253 clauses
14254 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14255 clauses);
14256 else
14257 clauses = c_parser_omp_clause_to (parser, clauses);
14258 c_name = "to";
14259 break;
14260 case PRAGMA_OMP_CLAUSE_FROM:
14261 clauses = c_parser_omp_clause_from (parser, clauses);
14262 c_name = "from";
14263 break;
14264 case PRAGMA_OMP_CLAUSE_UNIFORM:
14265 clauses = c_parser_omp_clause_uniform (parser, clauses);
14266 c_name = "uniform";
14267 break;
14268 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14269 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14270 c_name = "num_teams";
14271 break;
14272 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14273 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14274 c_name = "thread_limit";
14275 break;
14276 case PRAGMA_OMP_CLAUSE_ALIGNED:
14277 clauses = c_parser_omp_clause_aligned (parser, clauses);
14278 c_name = "aligned";
14279 break;
14280 case PRAGMA_OMP_CLAUSE_LINEAR:
14281 clauses = c_parser_omp_clause_linear (parser, clauses);
14282 c_name = "linear";
14283 break;
14284 case PRAGMA_OMP_CLAUSE_DEPEND:
14285 clauses = c_parser_omp_clause_depend (parser, clauses);
14286 c_name = "depend";
14287 break;
14288 case PRAGMA_OMP_CLAUSE_MAP:
14289 clauses = c_parser_omp_clause_map (parser, clauses);
14290 c_name = "map";
14291 break;
14292 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14293 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14294 c_name = "use_device_ptr";
14295 break;
14296 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14297 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14298 c_name = "is_device_ptr";
14299 break;
14300 case PRAGMA_OMP_CLAUSE_DEVICE:
14301 clauses = c_parser_omp_clause_device (parser, clauses);
14302 c_name = "device";
14303 break;
14304 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14305 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14306 c_name = "dist_schedule";
14307 break;
14308 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14309 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14310 c_name = "proc_bind";
14311 break;
14312 case PRAGMA_OMP_CLAUSE_SAFELEN:
14313 clauses = c_parser_omp_clause_safelen (parser, clauses);
14314 c_name = "safelen";
14315 break;
14316 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14317 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14318 c_name = "simdlen";
14319 break;
14320 case PRAGMA_OMP_CLAUSE_NOGROUP:
14321 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14322 c_name = "nogroup";
14323 break;
14324 case PRAGMA_OMP_CLAUSE_THREADS:
14325 clauses
14326 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14327 clauses);
14328 c_name = "threads";
14329 break;
14330 case PRAGMA_OMP_CLAUSE_SIMD:
14331 clauses
14332 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14333 clauses);
14334 c_name = "simd";
14335 break;
14336 default:
14337 c_parser_error (parser, "expected %<#pragma omp%> clause");
14338 goto saw_error;
14341 first = false;
14343 if (((mask >> c_kind) & 1) == 0)
14345 /* Remove the invalid clause(s) from the list to avoid
14346 confusing the rest of the compiler. */
14347 clauses = prev;
14348 error_at (here, "%qs is not valid for %qs", c_name, where);
14352 saw_error:
14353 c_parser_skip_to_pragma_eol (parser);
14355 if (finish_p)
14357 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14358 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14359 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14362 return clauses;
14365 /* OpenACC 2.0, OpenMP 2.5:
14366 structured-block:
14367 statement
14369 In practice, we're also interested in adding the statement to an
14370 outer node. So it is convenient if we work around the fact that
14371 c_parser_statement calls add_stmt. */
14373 static tree
14374 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14376 tree stmt = push_stmt_list ();
14377 c_parser_statement (parser, if_p);
14378 return pop_stmt_list (stmt);
14381 /* OpenACC 2.0:
14382 # pragma acc cache (variable-list) new-line
14384 LOC is the location of the #pragma token.
14387 static tree
14388 c_parser_oacc_cache (location_t loc, c_parser *parser)
14390 tree stmt, clauses;
14392 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14393 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14395 c_parser_skip_to_pragma_eol (parser);
14397 stmt = make_node (OACC_CACHE);
14398 TREE_TYPE (stmt) = void_type_node;
14399 OACC_CACHE_CLAUSES (stmt) = clauses;
14400 SET_EXPR_LOCATION (stmt, loc);
14401 add_stmt (stmt);
14403 return stmt;
14406 /* OpenACC 2.0:
14407 # pragma acc data oacc-data-clause[optseq] new-line
14408 structured-block
14410 LOC is the location of the #pragma token.
14413 #define OACC_DATA_CLAUSE_MASK \
14414 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14426 static tree
14427 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14429 tree stmt, clauses, block;
14431 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14432 "#pragma acc data");
14434 block = c_begin_omp_parallel ();
14435 add_stmt (c_parser_omp_structured_block (parser, if_p));
14437 stmt = c_finish_oacc_data (loc, clauses, block);
14439 return stmt;
14442 /* OpenACC 2.0:
14443 # pragma acc declare oacc-data-clause[optseq] new-line
14446 #define OACC_DECLARE_CLAUSE_MASK \
14447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14460 static void
14461 c_parser_oacc_declare (c_parser *parser)
14463 location_t pragma_loc = c_parser_peek_token (parser)->location;
14464 tree clauses, stmt, t, decl;
14466 bool error = false;
14468 c_parser_consume_pragma (parser);
14470 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14471 "#pragma acc declare");
14472 if (!clauses)
14474 error_at (pragma_loc,
14475 "no valid clauses specified in %<#pragma acc declare%>");
14476 return;
14479 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14481 location_t loc = OMP_CLAUSE_LOCATION (t);
14482 decl = OMP_CLAUSE_DECL (t);
14483 if (!DECL_P (decl))
14485 error_at (loc, "array section in %<#pragma acc declare%>");
14486 error = true;
14487 continue;
14490 switch (OMP_CLAUSE_MAP_KIND (t))
14492 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14493 case GOMP_MAP_FORCE_ALLOC:
14494 case GOMP_MAP_FORCE_TO:
14495 case GOMP_MAP_FORCE_DEVICEPTR:
14496 case GOMP_MAP_DEVICE_RESIDENT:
14497 break;
14499 case GOMP_MAP_LINK:
14500 if (!global_bindings_p ()
14501 && (TREE_STATIC (decl)
14502 || !DECL_EXTERNAL (decl)))
14504 error_at (loc,
14505 "%qD must be a global variable in "
14506 "%<#pragma acc declare link%>",
14507 decl);
14508 error = true;
14509 continue;
14511 break;
14513 default:
14514 if (global_bindings_p ())
14516 error_at (loc, "invalid OpenACC clause at file scope");
14517 error = true;
14518 continue;
14520 if (DECL_EXTERNAL (decl))
14522 error_at (loc,
14523 "invalid use of %<extern%> variable %qD "
14524 "in %<#pragma acc declare%>", decl);
14525 error = true;
14526 continue;
14528 else if (TREE_PUBLIC (decl))
14530 error_at (loc,
14531 "invalid use of %<global%> variable %qD "
14532 "in %<#pragma acc declare%>", decl);
14533 error = true;
14534 continue;
14536 break;
14539 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14540 || lookup_attribute ("omp declare target link",
14541 DECL_ATTRIBUTES (decl)))
14543 error_at (loc, "variable %qD used more than once with "
14544 "%<#pragma acc declare%>", decl);
14545 error = true;
14546 continue;
14549 if (!error)
14551 tree id;
14553 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14554 id = get_identifier ("omp declare target link");
14555 else
14556 id = get_identifier ("omp declare target");
14558 DECL_ATTRIBUTES (decl)
14559 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14561 if (global_bindings_p ())
14563 symtab_node *node = symtab_node::get (decl);
14564 if (node != NULL)
14566 node->offloadable = 1;
14567 if (ENABLE_OFFLOADING)
14569 g->have_offload = true;
14570 if (is_a <varpool_node *> (node))
14571 vec_safe_push (offload_vars, decl);
14578 if (error || global_bindings_p ())
14579 return;
14581 stmt = make_node (OACC_DECLARE);
14582 TREE_TYPE (stmt) = void_type_node;
14583 OACC_DECLARE_CLAUSES (stmt) = clauses;
14584 SET_EXPR_LOCATION (stmt, pragma_loc);
14586 add_stmt (stmt);
14588 return;
14591 /* OpenACC 2.0:
14592 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14596 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14599 LOC is the location of the #pragma token.
14602 #define OACC_ENTER_DATA_CLAUSE_MASK \
14603 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14611 #define OACC_EXIT_DATA_CLAUSE_MASK \
14612 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14618 static void
14619 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14621 location_t loc = c_parser_peek_token (parser)->location;
14622 tree clauses, stmt;
14623 const char *p = "";
14625 c_parser_consume_pragma (parser);
14627 if (c_parser_next_token_is (parser, CPP_NAME))
14629 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14630 c_parser_consume_token (parser);
14633 if (strcmp (p, "data") != 0)
14635 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14636 enter ? "enter" : "exit");
14637 parser->error = true;
14638 c_parser_skip_to_pragma_eol (parser);
14639 return;
14642 if (enter)
14643 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14644 "#pragma acc enter data");
14645 else
14646 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14647 "#pragma acc exit data");
14649 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14651 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14652 enter ? "enter" : "exit");
14653 return;
14656 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14657 TREE_TYPE (stmt) = void_type_node;
14658 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14659 SET_EXPR_LOCATION (stmt, loc);
14660 add_stmt (stmt);
14664 /* OpenACC 2.0:
14665 # pragma acc host_data oacc-data-clause[optseq] new-line
14666 structured-block
14669 #define OACC_HOST_DATA_CLAUSE_MASK \
14670 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14672 static tree
14673 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14675 tree stmt, clauses, block;
14677 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14678 "#pragma acc host_data");
14680 block = c_begin_omp_parallel ();
14681 add_stmt (c_parser_omp_structured_block (parser, if_p));
14682 stmt = c_finish_oacc_host_data (loc, clauses, block);
14683 return stmt;
14687 /* OpenACC 2.0:
14689 # pragma acc loop oacc-loop-clause[optseq] new-line
14690 structured-block
14692 LOC is the location of the #pragma token.
14695 #define OACC_LOOP_CLAUSE_MASK \
14696 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14706 static tree
14707 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14708 omp_clause_mask mask, tree *cclauses, bool *if_p)
14710 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14712 strcat (p_name, " loop");
14713 mask |= OACC_LOOP_CLAUSE_MASK;
14715 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14716 cclauses == NULL);
14717 if (cclauses)
14719 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14720 if (*cclauses)
14721 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14722 if (clauses)
14723 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14726 tree block = c_begin_compound_stmt (true);
14727 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14728 if_p);
14729 block = c_end_compound_stmt (loc, block, true);
14730 add_stmt (block);
14732 return stmt;
14735 /* OpenACC 2.0:
14736 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14737 structured-block
14741 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14742 structured-block
14744 LOC is the location of the #pragma token.
14747 #define OACC_KERNELS_CLAUSE_MASK \
14748 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
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_VECTOR_LENGTH) \
14764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14766 #define OACC_PARALLEL_CLAUSE_MASK \
14767 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14788 static tree
14789 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14790 enum pragma_kind p_kind, char *p_name,
14791 bool *if_p)
14793 omp_clause_mask mask;
14794 enum tree_code code;
14795 switch (p_kind)
14797 case PRAGMA_OACC_KERNELS:
14798 strcat (p_name, " kernels");
14799 mask = OACC_KERNELS_CLAUSE_MASK;
14800 code = OACC_KERNELS;
14801 break;
14802 case PRAGMA_OACC_PARALLEL:
14803 strcat (p_name, " parallel");
14804 mask = OACC_PARALLEL_CLAUSE_MASK;
14805 code = OACC_PARALLEL;
14806 break;
14807 default:
14808 gcc_unreachable ();
14811 if (c_parser_next_token_is (parser, CPP_NAME))
14813 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14814 if (strcmp (p, "loop") == 0)
14816 c_parser_consume_token (parser);
14817 tree block = c_begin_omp_parallel ();
14818 tree clauses;
14819 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14820 return c_finish_omp_construct (loc, code, block, clauses);
14824 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14826 tree block = c_begin_omp_parallel ();
14827 add_stmt (c_parser_omp_structured_block (parser, if_p));
14829 return c_finish_omp_construct (loc, code, block, clauses);
14832 /* OpenACC 2.0:
14833 # pragma acc routine oacc-routine-clause[optseq] new-line
14834 function-definition
14836 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14839 #define OACC_ROUTINE_CLAUSE_MASK \
14840 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14845 /* Parse an OpenACC routine directive. For named directives, we apply
14846 immediately to the named function. For unnamed ones we then parse
14847 a declaration or definition, which must be for a function. */
14849 static void
14850 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14852 gcc_checking_assert (context == pragma_external);
14854 oacc_routine_data data;
14855 data.error_seen = false;
14856 data.fndecl_seen = false;
14857 data.clauses = NULL_TREE;
14858 data.loc = c_parser_peek_token (parser)->location;
14860 c_parser_consume_pragma (parser);
14862 /* Look for optional '( name )'. */
14863 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14865 c_parser_consume_token (parser); /* '(' */
14867 tree decl = NULL_TREE;
14868 c_token *name_token = c_parser_peek_token (parser);
14869 location_t name_loc = name_token->location;
14870 if (name_token->type == CPP_NAME
14871 && (name_token->id_kind == C_ID_ID
14872 || name_token->id_kind == C_ID_TYPENAME))
14874 decl = lookup_name (name_token->value);
14875 if (!decl)
14876 error_at (name_loc,
14877 "%qE has not been declared", name_token->value);
14878 c_parser_consume_token (parser);
14880 else
14881 c_parser_error (parser, "expected function name");
14883 if (!decl
14884 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14886 c_parser_skip_to_pragma_eol (parser, false);
14887 return;
14890 data.clauses
14891 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14892 "#pragma acc routine");
14894 if (TREE_CODE (decl) != FUNCTION_DECL)
14896 error_at (name_loc, "%qD does not refer to a function", decl);
14897 return;
14900 c_finish_oacc_routine (&data, decl, false);
14902 else /* No optional '( name )'. */
14904 data.clauses
14905 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14906 "#pragma acc routine");
14908 /* Emit a helpful diagnostic if there's another pragma following this
14909 one. Also don't allow a static assertion declaration, as in the
14910 following we'll just parse a *single* "declaration or function
14911 definition", and the static assertion counts an one. */
14912 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14913 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14915 error_at (data.loc,
14916 "%<#pragma acc routine%> not immediately followed by"
14917 " function declaration or definition");
14918 /* ..., and then just keep going. */
14919 return;
14922 /* We only have to consider the pragma_external case here. */
14923 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14924 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14926 int ext = disable_extension_diagnostics ();
14928 c_parser_consume_token (parser);
14929 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14930 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14931 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14932 NULL, vNULL, &data);
14933 restore_extension_diagnostics (ext);
14935 else
14936 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14937 NULL, vNULL, &data);
14941 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14942 IS_DEFN is true if we're applying it to the definition. */
14944 static void
14945 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14946 bool is_defn)
14948 /* Keep going if we're in error reporting mode. */
14949 if (data->error_seen
14950 || fndecl == error_mark_node)
14951 return;
14953 if (data->fndecl_seen)
14955 error_at (data->loc,
14956 "%<#pragma acc routine%> not immediately followed by"
14957 " a single function declaration or definition");
14958 data->error_seen = true;
14959 return;
14961 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14963 error_at (data->loc,
14964 "%<#pragma acc routine%> not immediately followed by"
14965 " function declaration or definition");
14966 data->error_seen = true;
14967 return;
14970 if (oacc_get_fn_attrib (fndecl))
14972 error_at (data->loc,
14973 "%<#pragma acc routine%> already applied to %qD", fndecl);
14974 data->error_seen = true;
14975 return;
14978 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14980 error_at (data->loc,
14981 TREE_USED (fndecl)
14982 ? G_("%<#pragma acc routine%> must be applied before use")
14983 : G_("%<#pragma acc routine%> must be applied before "
14984 "definition"));
14985 data->error_seen = true;
14986 return;
14989 /* Process the routine's dimension clauses. */
14990 tree dims = oacc_build_routine_dims (data->clauses);
14991 oacc_replace_fn_attrib (fndecl, dims);
14993 /* Add an "omp declare target" attribute. */
14994 DECL_ATTRIBUTES (fndecl)
14995 = tree_cons (get_identifier ("omp declare target"),
14996 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14998 /* Remember that we've used this "#pragma acc routine". */
14999 data->fndecl_seen = true;
15002 /* OpenACC 2.0:
15003 # pragma acc update oacc-update-clause[optseq] new-line
15006 #define OACC_UPDATE_CLAUSE_MASK \
15007 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
15009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
15010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
15012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15014 static void
15015 c_parser_oacc_update (c_parser *parser)
15017 location_t loc = c_parser_peek_token (parser)->location;
15019 c_parser_consume_pragma (parser);
15021 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15022 "#pragma acc update");
15023 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15025 error_at (loc,
15026 "%<#pragma acc update%> must contain at least one "
15027 "%<device%> or %<host%> or %<self%> clause");
15028 return;
15031 if (parser->error)
15032 return;
15034 tree stmt = make_node (OACC_UPDATE);
15035 TREE_TYPE (stmt) = void_type_node;
15036 OACC_UPDATE_CLAUSES (stmt) = clauses;
15037 SET_EXPR_LOCATION (stmt, loc);
15038 add_stmt (stmt);
15041 /* OpenACC 2.0:
15042 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15044 LOC is the location of the #pragma token.
15047 #define OACC_WAIT_CLAUSE_MASK \
15048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15050 static tree
15051 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15053 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15055 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15056 list = c_parser_oacc_wait_list (parser, loc, list);
15058 strcpy (p_name, " wait");
15059 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15060 stmt = c_finish_oacc_wait (loc, list, clauses);
15061 add_stmt (stmt);
15063 return stmt;
15066 /* OpenMP 2.5:
15067 # pragma omp atomic new-line
15068 expression-stmt
15070 expression-stmt:
15071 x binop= expr | x++ | ++x | x-- | --x
15072 binop:
15073 +, *, -, /, &, ^, |, <<, >>
15075 where x is an lvalue expression with scalar type.
15077 OpenMP 3.1:
15078 # pragma omp atomic new-line
15079 update-stmt
15081 # pragma omp atomic read new-line
15082 read-stmt
15084 # pragma omp atomic write new-line
15085 write-stmt
15087 # pragma omp atomic update new-line
15088 update-stmt
15090 # pragma omp atomic capture new-line
15091 capture-stmt
15093 # pragma omp atomic capture new-line
15094 capture-block
15096 read-stmt:
15097 v = x
15098 write-stmt:
15099 x = expr
15100 update-stmt:
15101 expression-stmt | x = x binop expr
15102 capture-stmt:
15103 v = expression-stmt
15104 capture-block:
15105 { v = x; update-stmt; } | { update-stmt; v = x; }
15107 OpenMP 4.0:
15108 update-stmt:
15109 expression-stmt | x = x binop expr | x = expr binop x
15110 capture-stmt:
15111 v = update-stmt
15112 capture-block:
15113 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15115 where x and v are lvalue expressions with scalar type.
15117 LOC is the location of the #pragma token. */
15119 static void
15120 c_parser_omp_atomic (location_t loc, c_parser *parser)
15122 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15123 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15124 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15125 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15126 struct c_expr expr;
15127 location_t eloc;
15128 bool structured_block = false;
15129 bool swapped = false;
15130 bool seq_cst = false;
15131 bool non_lvalue_p;
15133 if (c_parser_next_token_is (parser, CPP_NAME))
15135 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15136 if (!strcmp (p, "seq_cst"))
15138 seq_cst = true;
15139 c_parser_consume_token (parser);
15140 if (c_parser_next_token_is (parser, CPP_COMMA)
15141 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15142 c_parser_consume_token (parser);
15145 if (c_parser_next_token_is (parser, CPP_NAME))
15147 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15149 if (!strcmp (p, "read"))
15150 code = OMP_ATOMIC_READ;
15151 else if (!strcmp (p, "write"))
15152 code = NOP_EXPR;
15153 else if (!strcmp (p, "update"))
15154 code = OMP_ATOMIC;
15155 else if (!strcmp (p, "capture"))
15156 code = OMP_ATOMIC_CAPTURE_NEW;
15157 else
15158 p = NULL;
15159 if (p)
15160 c_parser_consume_token (parser);
15162 if (!seq_cst)
15164 if (c_parser_next_token_is (parser, CPP_COMMA)
15165 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15166 c_parser_consume_token (parser);
15168 if (c_parser_next_token_is (parser, CPP_NAME))
15170 const char *p
15171 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15172 if (!strcmp (p, "seq_cst"))
15174 seq_cst = true;
15175 c_parser_consume_token (parser);
15179 c_parser_skip_to_pragma_eol (parser);
15181 switch (code)
15183 case OMP_ATOMIC_READ:
15184 case NOP_EXPR: /* atomic write */
15185 v = c_parser_cast_expression (parser, NULL).value;
15186 non_lvalue_p = !lvalue_p (v);
15187 v = c_fully_fold (v, false, NULL, true);
15188 if (v == error_mark_node)
15189 goto saw_error;
15190 if (non_lvalue_p)
15191 v = non_lvalue (v);
15192 loc = c_parser_peek_token (parser)->location;
15193 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15194 goto saw_error;
15195 if (code == NOP_EXPR)
15197 lhs = c_parser_expression (parser).value;
15198 lhs = c_fully_fold (lhs, false, NULL);
15199 if (lhs == error_mark_node)
15200 goto saw_error;
15202 else
15204 lhs = c_parser_cast_expression (parser, NULL).value;
15205 non_lvalue_p = !lvalue_p (lhs);
15206 lhs = c_fully_fold (lhs, false, NULL, true);
15207 if (lhs == error_mark_node)
15208 goto saw_error;
15209 if (non_lvalue_p)
15210 lhs = non_lvalue (lhs);
15212 if (code == NOP_EXPR)
15214 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15215 opcode. */
15216 code = OMP_ATOMIC;
15217 rhs = lhs;
15218 lhs = v;
15219 v = NULL_TREE;
15221 goto done;
15222 case OMP_ATOMIC_CAPTURE_NEW:
15223 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15225 c_parser_consume_token (parser);
15226 structured_block = true;
15228 else
15230 v = c_parser_cast_expression (parser, NULL).value;
15231 non_lvalue_p = !lvalue_p (v);
15232 v = c_fully_fold (v, false, NULL, true);
15233 if (v == error_mark_node)
15234 goto saw_error;
15235 if (non_lvalue_p)
15236 v = non_lvalue (v);
15237 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15238 goto saw_error;
15240 break;
15241 default:
15242 break;
15245 /* For structured_block case we don't know yet whether
15246 old or new x should be captured. */
15247 restart:
15248 eloc = c_parser_peek_token (parser)->location;
15249 expr = c_parser_cast_expression (parser, NULL);
15250 lhs = expr.value;
15251 expr = default_function_array_conversion (eloc, expr);
15252 unfolded_lhs = expr.value;
15253 lhs = c_fully_fold (lhs, false, NULL, true);
15254 orig_lhs = lhs;
15255 switch (TREE_CODE (lhs))
15257 case ERROR_MARK:
15258 saw_error:
15259 c_parser_skip_to_end_of_block_or_statement (parser);
15260 if (structured_block)
15262 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15263 c_parser_consume_token (parser);
15264 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15266 c_parser_skip_to_end_of_block_or_statement (parser);
15267 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15268 c_parser_consume_token (parser);
15271 return;
15273 case POSTINCREMENT_EXPR:
15274 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15275 code = OMP_ATOMIC_CAPTURE_OLD;
15276 /* FALLTHROUGH */
15277 case PREINCREMENT_EXPR:
15278 lhs = TREE_OPERAND (lhs, 0);
15279 unfolded_lhs = NULL_TREE;
15280 opcode = PLUS_EXPR;
15281 rhs = integer_one_node;
15282 break;
15284 case POSTDECREMENT_EXPR:
15285 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15286 code = OMP_ATOMIC_CAPTURE_OLD;
15287 /* FALLTHROUGH */
15288 case PREDECREMENT_EXPR:
15289 lhs = TREE_OPERAND (lhs, 0);
15290 unfolded_lhs = NULL_TREE;
15291 opcode = MINUS_EXPR;
15292 rhs = integer_one_node;
15293 break;
15295 case COMPOUND_EXPR:
15296 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15297 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15298 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15299 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15300 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15301 (TREE_OPERAND (lhs, 1), 0), 0)))
15302 == BOOLEAN_TYPE)
15303 /* Undo effects of boolean_increment for post {in,de}crement. */
15304 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15305 /* FALLTHRU */
15306 case MODIFY_EXPR:
15307 if (TREE_CODE (lhs) == MODIFY_EXPR
15308 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15310 /* Undo effects of boolean_increment. */
15311 if (integer_onep (TREE_OPERAND (lhs, 1)))
15313 /* This is pre or post increment. */
15314 rhs = TREE_OPERAND (lhs, 1);
15315 lhs = TREE_OPERAND (lhs, 0);
15316 unfolded_lhs = NULL_TREE;
15317 opcode = NOP_EXPR;
15318 if (code == OMP_ATOMIC_CAPTURE_NEW
15319 && !structured_block
15320 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15321 code = OMP_ATOMIC_CAPTURE_OLD;
15322 break;
15324 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15325 && TREE_OPERAND (lhs, 0)
15326 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15328 /* This is pre or post decrement. */
15329 rhs = TREE_OPERAND (lhs, 1);
15330 lhs = TREE_OPERAND (lhs, 0);
15331 unfolded_lhs = NULL_TREE;
15332 opcode = NOP_EXPR;
15333 if (code == OMP_ATOMIC_CAPTURE_NEW
15334 && !structured_block
15335 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15336 code = OMP_ATOMIC_CAPTURE_OLD;
15337 break;
15340 /* FALLTHRU */
15341 default:
15342 if (!lvalue_p (unfolded_lhs))
15343 lhs = non_lvalue (lhs);
15344 switch (c_parser_peek_token (parser)->type)
15346 case CPP_MULT_EQ:
15347 opcode = MULT_EXPR;
15348 break;
15349 case CPP_DIV_EQ:
15350 opcode = TRUNC_DIV_EXPR;
15351 break;
15352 case CPP_PLUS_EQ:
15353 opcode = PLUS_EXPR;
15354 break;
15355 case CPP_MINUS_EQ:
15356 opcode = MINUS_EXPR;
15357 break;
15358 case CPP_LSHIFT_EQ:
15359 opcode = LSHIFT_EXPR;
15360 break;
15361 case CPP_RSHIFT_EQ:
15362 opcode = RSHIFT_EXPR;
15363 break;
15364 case CPP_AND_EQ:
15365 opcode = BIT_AND_EXPR;
15366 break;
15367 case CPP_OR_EQ:
15368 opcode = BIT_IOR_EXPR;
15369 break;
15370 case CPP_XOR_EQ:
15371 opcode = BIT_XOR_EXPR;
15372 break;
15373 case CPP_EQ:
15374 c_parser_consume_token (parser);
15375 eloc = c_parser_peek_token (parser)->location;
15376 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15377 rhs1 = expr.value;
15378 switch (TREE_CODE (rhs1))
15380 case MULT_EXPR:
15381 case TRUNC_DIV_EXPR:
15382 case RDIV_EXPR:
15383 case PLUS_EXPR:
15384 case MINUS_EXPR:
15385 case LSHIFT_EXPR:
15386 case RSHIFT_EXPR:
15387 case BIT_AND_EXPR:
15388 case BIT_IOR_EXPR:
15389 case BIT_XOR_EXPR:
15390 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15392 opcode = TREE_CODE (rhs1);
15393 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15394 true);
15395 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15396 true);
15397 goto stmt_done;
15399 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15401 opcode = TREE_CODE (rhs1);
15402 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15403 true);
15404 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15405 true);
15406 swapped = !commutative_tree_code (opcode);
15407 goto stmt_done;
15409 break;
15410 case ERROR_MARK:
15411 goto saw_error;
15412 default:
15413 break;
15415 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15417 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15419 code = OMP_ATOMIC_CAPTURE_OLD;
15420 v = lhs;
15421 lhs = NULL_TREE;
15422 expr = default_function_array_read_conversion (eloc, expr);
15423 unfolded_lhs1 = expr.value;
15424 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15425 rhs1 = NULL_TREE;
15426 c_parser_consume_token (parser);
15427 goto restart;
15429 if (structured_block)
15431 opcode = NOP_EXPR;
15432 expr = default_function_array_read_conversion (eloc, expr);
15433 rhs = c_fully_fold (expr.value, false, NULL, true);
15434 rhs1 = NULL_TREE;
15435 goto stmt_done;
15438 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15439 goto saw_error;
15440 default:
15441 c_parser_error (parser,
15442 "invalid operator for %<#pragma omp atomic%>");
15443 goto saw_error;
15446 /* Arrange to pass the location of the assignment operator to
15447 c_finish_omp_atomic. */
15448 loc = c_parser_peek_token (parser)->location;
15449 c_parser_consume_token (parser);
15450 eloc = c_parser_peek_token (parser)->location;
15451 expr = c_parser_expression (parser);
15452 expr = default_function_array_read_conversion (eloc, expr);
15453 rhs = expr.value;
15454 rhs = c_fully_fold (rhs, false, NULL, true);
15455 break;
15457 stmt_done:
15458 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15460 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15461 goto saw_error;
15462 v = c_parser_cast_expression (parser, NULL).value;
15463 non_lvalue_p = !lvalue_p (v);
15464 v = c_fully_fold (v, false, NULL, true);
15465 if (v == error_mark_node)
15466 goto saw_error;
15467 if (non_lvalue_p)
15468 v = non_lvalue (v);
15469 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15470 goto saw_error;
15471 eloc = c_parser_peek_token (parser)->location;
15472 expr = c_parser_cast_expression (parser, NULL);
15473 lhs1 = expr.value;
15474 expr = default_function_array_read_conversion (eloc, expr);
15475 unfolded_lhs1 = expr.value;
15476 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15477 if (lhs1 == error_mark_node)
15478 goto saw_error;
15479 if (!lvalue_p (unfolded_lhs1))
15480 lhs1 = non_lvalue (lhs1);
15482 if (structured_block)
15484 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15485 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15487 done:
15488 if (unfolded_lhs && unfolded_lhs1
15489 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15491 error ("%<#pragma omp atomic capture%> uses two different "
15492 "expressions for memory");
15493 stmt = error_mark_node;
15495 else
15496 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15497 swapped, seq_cst);
15498 if (stmt != error_mark_node)
15499 add_stmt (stmt);
15501 if (!structured_block)
15502 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15506 /* OpenMP 2.5:
15507 # pragma omp barrier new-line
15510 static void
15511 c_parser_omp_barrier (c_parser *parser)
15513 location_t loc = c_parser_peek_token (parser)->location;
15514 c_parser_consume_pragma (parser);
15515 c_parser_skip_to_pragma_eol (parser);
15517 c_finish_omp_barrier (loc);
15520 /* OpenMP 2.5:
15521 # pragma omp critical [(name)] new-line
15522 structured-block
15524 OpenMP 4.5:
15525 # pragma omp critical [(name) [hint(expression)]] new-line
15527 LOC is the location of the #pragma itself. */
15529 #define OMP_CRITICAL_CLAUSE_MASK \
15530 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15532 static tree
15533 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15535 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15537 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15539 c_parser_consume_token (parser);
15540 if (c_parser_next_token_is (parser, CPP_NAME))
15542 name = c_parser_peek_token (parser)->value;
15543 c_parser_consume_token (parser);
15544 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15546 else
15547 c_parser_error (parser, "expected identifier");
15549 clauses = c_parser_omp_all_clauses (parser,
15550 OMP_CRITICAL_CLAUSE_MASK,
15551 "#pragma omp critical");
15553 else
15555 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15556 c_parser_error (parser, "expected %<(%> or end of line");
15557 c_parser_skip_to_pragma_eol (parser);
15560 stmt = c_parser_omp_structured_block (parser, if_p);
15561 return c_finish_omp_critical (loc, stmt, name, clauses);
15564 /* OpenMP 2.5:
15565 # pragma omp flush flush-vars[opt] new-line
15567 flush-vars:
15568 ( variable-list ) */
15570 static void
15571 c_parser_omp_flush (c_parser *parser)
15573 location_t loc = c_parser_peek_token (parser)->location;
15574 c_parser_consume_pragma (parser);
15575 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15576 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15577 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15578 c_parser_error (parser, "expected %<(%> or end of line");
15579 c_parser_skip_to_pragma_eol (parser);
15581 c_finish_omp_flush (loc);
15584 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15585 The real trick here is to determine the loop control variable early
15586 so that we can push a new decl if necessary to make it private.
15587 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15588 respectively. */
15590 static tree
15591 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15592 tree clauses, tree *cclauses, bool *if_p)
15594 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15595 tree declv, condv, incrv, initv, ret = NULL_TREE;
15596 tree pre_body = NULL_TREE, this_pre_body;
15597 tree ordered_cl = NULL_TREE;
15598 bool fail = false, open_brace_parsed = false;
15599 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15600 location_t for_loc;
15601 bool tiling = false;
15602 vec<tree, va_gc> *for_block = make_tree_vector ();
15604 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15605 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15606 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15607 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15609 tiling = true;
15610 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15612 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15613 && OMP_CLAUSE_ORDERED_EXPR (cl))
15615 ordered_cl = cl;
15616 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15619 if (ordered && ordered < collapse)
15621 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15622 "%<ordered%> clause parameter is less than %<collapse%>");
15623 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15624 = build_int_cst (NULL_TREE, collapse);
15625 ordered = collapse;
15627 if (ordered)
15629 for (tree *pc = &clauses; *pc; )
15630 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15632 error_at (OMP_CLAUSE_LOCATION (*pc),
15633 "%<linear%> clause may not be specified together "
15634 "with %<ordered%> clause with a parameter");
15635 *pc = OMP_CLAUSE_CHAIN (*pc);
15637 else
15638 pc = &OMP_CLAUSE_CHAIN (*pc);
15641 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15642 count = ordered ? ordered : collapse;
15644 declv = make_tree_vec (count);
15645 initv = make_tree_vec (count);
15646 condv = make_tree_vec (count);
15647 incrv = make_tree_vec (count);
15649 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15651 c_parser_error (parser, "for statement expected");
15652 return NULL;
15654 for_loc = c_parser_peek_token (parser)->location;
15655 c_parser_consume_token (parser);
15657 for (i = 0; i < count; i++)
15659 int bracecount = 0;
15661 matching_parens parens;
15662 if (!parens.require_open (parser))
15663 goto pop_scopes;
15665 /* Parse the initialization declaration or expression. */
15666 if (c_parser_next_tokens_start_declaration (parser))
15668 if (i > 0)
15669 vec_safe_push (for_block, c_begin_compound_stmt (true));
15670 this_pre_body = push_stmt_list ();
15671 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15672 NULL, vNULL);
15673 if (this_pre_body)
15675 this_pre_body = pop_stmt_list (this_pre_body);
15676 if (pre_body)
15678 tree t = pre_body;
15679 pre_body = push_stmt_list ();
15680 add_stmt (t);
15681 add_stmt (this_pre_body);
15682 pre_body = pop_stmt_list (pre_body);
15684 else
15685 pre_body = this_pre_body;
15687 decl = check_for_loop_decls (for_loc, flag_isoc99);
15688 if (decl == NULL)
15689 goto error_init;
15690 if (DECL_INITIAL (decl) == error_mark_node)
15691 decl = error_mark_node;
15692 init = decl;
15694 else if (c_parser_next_token_is (parser, CPP_NAME)
15695 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15697 struct c_expr decl_exp;
15698 struct c_expr init_exp;
15699 location_t init_loc;
15701 decl_exp = c_parser_postfix_expression (parser);
15702 decl = decl_exp.value;
15704 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15706 init_loc = c_parser_peek_token (parser)->location;
15707 init_exp = c_parser_expr_no_commas (parser, NULL);
15708 init_exp = default_function_array_read_conversion (init_loc,
15709 init_exp);
15710 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15711 NOP_EXPR, init_loc, init_exp.value,
15712 init_exp.original_type);
15713 init = c_process_expr_stmt (init_loc, init);
15715 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15717 else
15719 error_init:
15720 c_parser_error (parser,
15721 "expected iteration declaration or initialization");
15722 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15723 "expected %<)%>");
15724 fail = true;
15725 goto parse_next;
15728 /* Parse the loop condition. */
15729 cond = NULL_TREE;
15730 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15732 location_t cond_loc = c_parser_peek_token (parser)->location;
15733 struct c_expr cond_expr
15734 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15736 cond = cond_expr.value;
15737 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15738 if (COMPARISON_CLASS_P (cond))
15740 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15741 op0 = c_fully_fold (op0, false, NULL);
15742 op1 = c_fully_fold (op1, false, NULL);
15743 TREE_OPERAND (cond, 0) = op0;
15744 TREE_OPERAND (cond, 1) = op1;
15746 switch (cond_expr.original_code)
15748 case GT_EXPR:
15749 case GE_EXPR:
15750 case LT_EXPR:
15751 case LE_EXPR:
15752 break;
15753 default:
15754 /* Can't be cond = error_mark_node, because we want to preserve
15755 the location until c_finish_omp_for. */
15756 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15757 break;
15759 protected_set_expr_location (cond, cond_loc);
15761 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15763 /* Parse the increment expression. */
15764 incr = NULL_TREE;
15765 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15767 location_t incr_loc = c_parser_peek_token (parser)->location;
15769 incr = c_process_expr_stmt (incr_loc,
15770 c_parser_expression (parser).value);
15772 parens.skip_until_found_close (parser);
15774 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15775 fail = true;
15776 else
15778 TREE_VEC_ELT (declv, i) = decl;
15779 TREE_VEC_ELT (initv, i) = init;
15780 TREE_VEC_ELT (condv, i) = cond;
15781 TREE_VEC_ELT (incrv, i) = incr;
15784 parse_next:
15785 if (i == count - 1)
15786 break;
15788 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15789 in between the collapsed for loops to be still considered perfectly
15790 nested. Hopefully the final version clarifies this.
15791 For now handle (multiple) {'s and empty statements. */
15794 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15796 c_parser_consume_token (parser);
15797 break;
15799 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15801 c_parser_consume_token (parser);
15802 bracecount++;
15804 else if (bracecount
15805 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15806 c_parser_consume_token (parser);
15807 else
15809 c_parser_error (parser, "not enough perfectly nested loops");
15810 if (bracecount)
15812 open_brace_parsed = true;
15813 bracecount--;
15815 fail = true;
15816 count = 0;
15817 break;
15820 while (1);
15822 nbraces += bracecount;
15825 if (nbraces)
15826 if_p = NULL;
15828 save_break = c_break_label;
15829 c_break_label = size_one_node;
15830 save_cont = c_cont_label;
15831 c_cont_label = NULL_TREE;
15832 body = push_stmt_list ();
15834 if (open_brace_parsed)
15836 location_t here = c_parser_peek_token (parser)->location;
15837 stmt = c_begin_compound_stmt (true);
15838 c_parser_compound_statement_nostart (parser);
15839 add_stmt (c_end_compound_stmt (here, stmt, true));
15841 else
15842 add_stmt (c_parser_c99_block_statement (parser, if_p));
15843 if (c_cont_label)
15845 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15846 SET_EXPR_LOCATION (t, loc);
15847 add_stmt (t);
15850 body = pop_stmt_list (body);
15851 c_break_label = save_break;
15852 c_cont_label = save_cont;
15854 while (nbraces)
15856 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15858 c_parser_consume_token (parser);
15859 nbraces--;
15861 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15862 c_parser_consume_token (parser);
15863 else
15865 c_parser_error (parser, "collapsed loops not perfectly nested");
15866 while (nbraces)
15868 location_t here = c_parser_peek_token (parser)->location;
15869 stmt = c_begin_compound_stmt (true);
15870 add_stmt (body);
15871 c_parser_compound_statement_nostart (parser);
15872 body = c_end_compound_stmt (here, stmt, true);
15873 nbraces--;
15875 goto pop_scopes;
15879 /* Only bother calling c_finish_omp_for if we haven't already generated
15880 an error from the initialization parsing. */
15881 if (!fail)
15883 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15884 incrv, body, pre_body);
15886 /* Check for iterators appearing in lb, b or incr expressions. */
15887 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15888 stmt = NULL_TREE;
15890 if (stmt)
15892 add_stmt (stmt);
15894 if (cclauses != NULL
15895 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15897 tree *c;
15898 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15899 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15900 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15901 c = &OMP_CLAUSE_CHAIN (*c);
15902 else
15904 for (i = 0; i < count; i++)
15905 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15906 break;
15907 if (i == count)
15908 c = &OMP_CLAUSE_CHAIN (*c);
15909 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15911 error_at (loc,
15912 "iteration variable %qD should not be firstprivate",
15913 OMP_CLAUSE_DECL (*c));
15914 *c = OMP_CLAUSE_CHAIN (*c);
15916 else
15918 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15919 tree l = *c;
15920 *c = OMP_CLAUSE_CHAIN (*c);
15921 if (code == OMP_SIMD)
15923 OMP_CLAUSE_CHAIN (l)
15924 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15925 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15927 else
15929 OMP_CLAUSE_CHAIN (l) = clauses;
15930 clauses = l;
15935 OMP_FOR_CLAUSES (stmt) = clauses;
15937 ret = stmt;
15939 pop_scopes:
15940 while (!for_block->is_empty ())
15942 /* FIXME diagnostics: LOC below should be the actual location of
15943 this particular for block. We need to build a list of
15944 locations to go along with FOR_BLOCK. */
15945 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15946 add_stmt (stmt);
15948 release_tree_vector (for_block);
15949 return ret;
15952 /* Helper function for OpenMP parsing, split clauses and call
15953 finish_omp_clauses on each of the set of clauses afterwards. */
15955 static void
15956 omp_split_clauses (location_t loc, enum tree_code code,
15957 omp_clause_mask mask, tree clauses, tree *cclauses)
15959 int i;
15960 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15961 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15962 if (cclauses[i])
15963 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15966 /* OpenMP 4.0:
15967 #pragma omp simd simd-clause[optseq] new-line
15968 for-loop
15970 LOC is the location of the #pragma token.
15973 #define OMP_SIMD_CLAUSE_MASK \
15974 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15983 static tree
15984 c_parser_omp_simd (location_t loc, c_parser *parser,
15985 char *p_name, omp_clause_mask mask, tree *cclauses,
15986 bool *if_p)
15988 tree block, clauses, ret;
15990 strcat (p_name, " simd");
15991 mask |= OMP_SIMD_CLAUSE_MASK;
15993 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15994 if (cclauses)
15996 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15997 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15998 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15999 OMP_CLAUSE_ORDERED);
16000 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
16002 error_at (OMP_CLAUSE_LOCATION (c),
16003 "%<ordered%> clause with parameter may not be specified "
16004 "on %qs construct", p_name);
16005 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
16009 block = c_begin_compound_stmt (true);
16010 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
16011 block = c_end_compound_stmt (loc, block, true);
16012 add_stmt (block);
16014 return ret;
16017 /* OpenMP 2.5:
16018 #pragma omp for for-clause[optseq] new-line
16019 for-loop
16021 OpenMP 4.0:
16022 #pragma omp for simd for-simd-clause[optseq] new-line
16023 for-loop
16025 LOC is the location of the #pragma token.
16028 #define OMP_FOR_CLAUSE_MASK \
16029 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
16035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
16036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16039 static tree
16040 c_parser_omp_for (location_t loc, c_parser *parser,
16041 char *p_name, omp_clause_mask mask, tree *cclauses,
16042 bool *if_p)
16044 tree block, clauses, ret;
16046 strcat (p_name, " for");
16047 mask |= OMP_FOR_CLAUSE_MASK;
16048 /* parallel for{, simd} disallows nowait clause, but for
16049 target {teams distribute ,}parallel for{, simd} it should be accepted. */
16050 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16051 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16052 /* Composite distribute parallel for{, simd} disallows ordered clause. */
16053 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16054 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16056 if (c_parser_next_token_is (parser, CPP_NAME))
16058 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16060 if (strcmp (p, "simd") == 0)
16062 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16063 if (cclauses == NULL)
16064 cclauses = cclauses_buf;
16066 c_parser_consume_token (parser);
16067 if (!flag_openmp) /* flag_openmp_simd */
16068 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16069 if_p);
16070 block = c_begin_compound_stmt (true);
16071 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16072 block = c_end_compound_stmt (loc, block, true);
16073 if (ret == NULL_TREE)
16074 return ret;
16075 ret = make_node (OMP_FOR);
16076 TREE_TYPE (ret) = void_type_node;
16077 OMP_FOR_BODY (ret) = block;
16078 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16079 SET_EXPR_LOCATION (ret, loc);
16080 add_stmt (ret);
16081 return ret;
16084 if (!flag_openmp) /* flag_openmp_simd */
16086 c_parser_skip_to_pragma_eol (parser, false);
16087 return NULL_TREE;
16090 /* Composite distribute parallel for disallows linear clause. */
16091 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16092 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16094 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16095 if (cclauses)
16097 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16098 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16101 block = c_begin_compound_stmt (true);
16102 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16103 block = c_end_compound_stmt (loc, block, true);
16104 add_stmt (block);
16106 return ret;
16109 /* OpenMP 2.5:
16110 # pragma omp master new-line
16111 structured-block
16113 LOC is the location of the #pragma token.
16116 static tree
16117 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16119 c_parser_skip_to_pragma_eol (parser);
16120 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16121 if_p));
16124 /* OpenMP 2.5:
16125 # pragma omp ordered new-line
16126 structured-block
16128 OpenMP 4.5:
16129 # pragma omp ordered ordered-clauses new-line
16130 structured-block
16132 # pragma omp ordered depend-clauses new-line */
16134 #define OMP_ORDERED_CLAUSE_MASK \
16135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16138 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16139 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16141 static bool
16142 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16143 bool *if_p)
16145 location_t loc = c_parser_peek_token (parser)->location;
16146 c_parser_consume_pragma (parser);
16148 if (context != pragma_stmt && context != pragma_compound)
16150 c_parser_error (parser, "expected declaration specifiers");
16151 c_parser_skip_to_pragma_eol (parser, false);
16152 return false;
16155 if (c_parser_next_token_is (parser, CPP_NAME))
16157 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16159 if (!strcmp ("depend", p))
16161 if (!flag_openmp) /* flag_openmp_simd */
16163 c_parser_skip_to_pragma_eol (parser, false);
16164 return false;
16166 if (context == pragma_stmt)
16168 error_at (loc,
16169 "%<#pragma omp ordered%> with %<depend%> clause may "
16170 "only be used in compound statements");
16171 c_parser_skip_to_pragma_eol (parser, false);
16172 return false;
16175 tree clauses
16176 = c_parser_omp_all_clauses (parser,
16177 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16178 "#pragma omp ordered");
16179 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16180 return false;
16184 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16185 "#pragma omp ordered");
16187 if (!flag_openmp /* flag_openmp_simd */
16188 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16189 return false;
16191 c_finish_omp_ordered (loc, clauses,
16192 c_parser_omp_structured_block (parser, if_p));
16193 return true;
16196 /* OpenMP 2.5:
16198 section-scope:
16199 { section-sequence }
16201 section-sequence:
16202 section-directive[opt] structured-block
16203 section-sequence section-directive structured-block
16205 SECTIONS_LOC is the location of the #pragma omp sections. */
16207 static tree
16208 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16210 tree stmt, substmt;
16211 bool error_suppress = false;
16212 location_t loc;
16214 loc = c_parser_peek_token (parser)->location;
16215 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16217 /* Avoid skipping until the end of the block. */
16218 parser->error = false;
16219 return NULL_TREE;
16222 stmt = push_stmt_list ();
16224 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16226 substmt = c_parser_omp_structured_block (parser, NULL);
16227 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16228 SET_EXPR_LOCATION (substmt, loc);
16229 add_stmt (substmt);
16232 while (1)
16234 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16235 break;
16236 if (c_parser_next_token_is (parser, CPP_EOF))
16237 break;
16239 loc = c_parser_peek_token (parser)->location;
16240 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16242 c_parser_consume_pragma (parser);
16243 c_parser_skip_to_pragma_eol (parser);
16244 error_suppress = false;
16246 else if (!error_suppress)
16248 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16249 error_suppress = true;
16252 substmt = c_parser_omp_structured_block (parser, NULL);
16253 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16254 SET_EXPR_LOCATION (substmt, loc);
16255 add_stmt (substmt);
16257 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16258 "expected %<#pragma omp section%> or %<}%>");
16260 substmt = pop_stmt_list (stmt);
16262 stmt = make_node (OMP_SECTIONS);
16263 SET_EXPR_LOCATION (stmt, sections_loc);
16264 TREE_TYPE (stmt) = void_type_node;
16265 OMP_SECTIONS_BODY (stmt) = substmt;
16267 return add_stmt (stmt);
16270 /* OpenMP 2.5:
16271 # pragma omp sections sections-clause[optseq] newline
16272 sections-scope
16274 LOC is the location of the #pragma token.
16277 #define OMP_SECTIONS_CLAUSE_MASK \
16278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16284 static tree
16285 c_parser_omp_sections (location_t loc, c_parser *parser,
16286 char *p_name, omp_clause_mask mask, tree *cclauses)
16288 tree block, clauses, ret;
16290 strcat (p_name, " sections");
16291 mask |= OMP_SECTIONS_CLAUSE_MASK;
16292 if (cclauses)
16293 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16295 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16296 if (cclauses)
16298 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16299 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16302 block = c_begin_compound_stmt (true);
16303 ret = c_parser_omp_sections_scope (loc, parser);
16304 if (ret)
16305 OMP_SECTIONS_CLAUSES (ret) = clauses;
16306 block = c_end_compound_stmt (loc, block, true);
16307 add_stmt (block);
16309 return ret;
16312 /* OpenMP 2.5:
16313 # pragma omp parallel parallel-clause[optseq] new-line
16314 structured-block
16315 # pragma omp parallel for parallel-for-clause[optseq] new-line
16316 structured-block
16317 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16318 structured-block
16320 OpenMP 4.0:
16321 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16322 structured-block
16324 LOC is the location of the #pragma token.
16327 #define OMP_PARALLEL_CLAUSE_MASK \
16328 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16338 static tree
16339 c_parser_omp_parallel (location_t loc, c_parser *parser,
16340 char *p_name, omp_clause_mask mask, tree *cclauses,
16341 bool *if_p)
16343 tree stmt, clauses, block;
16345 strcat (p_name, " parallel");
16346 mask |= OMP_PARALLEL_CLAUSE_MASK;
16347 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16348 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16349 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16350 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16352 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16354 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16355 if (cclauses == NULL)
16356 cclauses = cclauses_buf;
16358 c_parser_consume_token (parser);
16359 if (!flag_openmp) /* flag_openmp_simd */
16360 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16361 block = c_begin_omp_parallel ();
16362 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16363 stmt
16364 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16365 block);
16366 if (ret == NULL_TREE)
16367 return ret;
16368 OMP_PARALLEL_COMBINED (stmt) = 1;
16369 return stmt;
16371 /* When combined with distribute, parallel has to be followed by for.
16372 #pragma omp target parallel is allowed though. */
16373 else if (cclauses
16374 && (mask & (OMP_CLAUSE_MASK_1
16375 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16377 error_at (loc, "expected %<for%> after %qs", p_name);
16378 c_parser_skip_to_pragma_eol (parser);
16379 return NULL_TREE;
16381 else if (!flag_openmp) /* flag_openmp_simd */
16383 c_parser_skip_to_pragma_eol (parser, false);
16384 return NULL_TREE;
16386 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16388 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16389 if (strcmp (p, "sections") == 0)
16391 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16392 if (cclauses == NULL)
16393 cclauses = cclauses_buf;
16395 c_parser_consume_token (parser);
16396 block = c_begin_omp_parallel ();
16397 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16398 stmt = c_finish_omp_parallel (loc,
16399 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16400 block);
16401 OMP_PARALLEL_COMBINED (stmt) = 1;
16402 return stmt;
16406 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16407 if (cclauses)
16409 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16410 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16413 block = c_begin_omp_parallel ();
16414 c_parser_statement (parser, if_p);
16415 stmt = c_finish_omp_parallel (loc, clauses, block);
16417 return stmt;
16420 /* OpenMP 2.5:
16421 # pragma omp single single-clause[optseq] new-line
16422 structured-block
16424 LOC is the location of the #pragma.
16427 #define OMP_SINGLE_CLAUSE_MASK \
16428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16433 static tree
16434 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16436 tree stmt = make_node (OMP_SINGLE);
16437 SET_EXPR_LOCATION (stmt, loc);
16438 TREE_TYPE (stmt) = void_type_node;
16440 OMP_SINGLE_CLAUSES (stmt)
16441 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16442 "#pragma omp single");
16443 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16445 return add_stmt (stmt);
16448 /* OpenMP 3.0:
16449 # pragma omp task task-clause[optseq] new-line
16451 LOC is the location of the #pragma.
16454 #define OMP_TASK_CLAUSE_MASK \
16455 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16466 static tree
16467 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16469 tree clauses, block;
16471 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16472 "#pragma omp task");
16474 block = c_begin_omp_task ();
16475 c_parser_statement (parser, if_p);
16476 return c_finish_omp_task (loc, clauses, block);
16479 /* OpenMP 3.0:
16480 # pragma omp taskwait new-line
16483 static void
16484 c_parser_omp_taskwait (c_parser *parser)
16486 location_t loc = c_parser_peek_token (parser)->location;
16487 c_parser_consume_pragma (parser);
16488 c_parser_skip_to_pragma_eol (parser);
16490 c_finish_omp_taskwait (loc);
16493 /* OpenMP 3.1:
16494 # pragma omp taskyield new-line
16497 static void
16498 c_parser_omp_taskyield (c_parser *parser)
16500 location_t loc = c_parser_peek_token (parser)->location;
16501 c_parser_consume_pragma (parser);
16502 c_parser_skip_to_pragma_eol (parser);
16504 c_finish_omp_taskyield (loc);
16507 /* OpenMP 4.0:
16508 # pragma omp taskgroup new-line
16511 static tree
16512 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16514 location_t loc = c_parser_peek_token (parser)->location;
16515 c_parser_skip_to_pragma_eol (parser);
16516 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16517 if_p));
16520 /* OpenMP 4.0:
16521 # pragma omp cancel cancel-clause[optseq] new-line
16523 LOC is the location of the #pragma.
16526 #define OMP_CANCEL_CLAUSE_MASK \
16527 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16533 static void
16534 c_parser_omp_cancel (c_parser *parser)
16536 location_t loc = c_parser_peek_token (parser)->location;
16538 c_parser_consume_pragma (parser);
16539 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16540 "#pragma omp cancel");
16542 c_finish_omp_cancel (loc, clauses);
16545 /* OpenMP 4.0:
16546 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16548 LOC is the location of the #pragma.
16551 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16552 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16557 static void
16558 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16560 location_t loc = c_parser_peek_token (parser)->location;
16561 tree clauses;
16562 bool point_seen = false;
16564 c_parser_consume_pragma (parser);
16565 if (c_parser_next_token_is (parser, CPP_NAME))
16567 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16568 if (strcmp (p, "point") == 0)
16570 c_parser_consume_token (parser);
16571 point_seen = true;
16574 if (!point_seen)
16576 c_parser_error (parser, "expected %<point%>");
16577 c_parser_skip_to_pragma_eol (parser);
16578 return;
16581 if (context != pragma_compound)
16583 if (context == pragma_stmt)
16584 error_at (loc,
16585 "%<#pragma %s%> may only be used in compound statements",
16586 "omp cancellation point");
16587 else
16588 c_parser_error (parser, "expected declaration specifiers");
16589 c_parser_skip_to_pragma_eol (parser, false);
16590 return;
16593 clauses
16594 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16595 "#pragma omp cancellation point");
16597 c_finish_omp_cancellation_point (loc, clauses);
16600 /* OpenMP 4.0:
16601 #pragma omp distribute distribute-clause[optseq] new-line
16602 for-loop */
16604 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16605 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16611 static tree
16612 c_parser_omp_distribute (location_t loc, c_parser *parser,
16613 char *p_name, omp_clause_mask mask, tree *cclauses,
16614 bool *if_p)
16616 tree clauses, block, ret;
16618 strcat (p_name, " distribute");
16619 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16621 if (c_parser_next_token_is (parser, CPP_NAME))
16623 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16624 bool simd = false;
16625 bool parallel = false;
16627 if (strcmp (p, "simd") == 0)
16628 simd = true;
16629 else
16630 parallel = strcmp (p, "parallel") == 0;
16631 if (parallel || simd)
16633 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16634 if (cclauses == NULL)
16635 cclauses = cclauses_buf;
16636 c_parser_consume_token (parser);
16637 if (!flag_openmp) /* flag_openmp_simd */
16639 if (simd)
16640 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16641 if_p);
16642 else
16643 return c_parser_omp_parallel (loc, parser, p_name, mask,
16644 cclauses, if_p);
16646 block = c_begin_compound_stmt (true);
16647 if (simd)
16648 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16649 if_p);
16650 else
16651 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16652 if_p);
16653 block = c_end_compound_stmt (loc, block, true);
16654 if (ret == NULL)
16655 return ret;
16656 ret = make_node (OMP_DISTRIBUTE);
16657 TREE_TYPE (ret) = void_type_node;
16658 OMP_FOR_BODY (ret) = block;
16659 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16660 SET_EXPR_LOCATION (ret, loc);
16661 add_stmt (ret);
16662 return ret;
16665 if (!flag_openmp) /* flag_openmp_simd */
16667 c_parser_skip_to_pragma_eol (parser, false);
16668 return NULL_TREE;
16671 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16672 if (cclauses)
16674 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16675 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16678 block = c_begin_compound_stmt (true);
16679 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16680 if_p);
16681 block = c_end_compound_stmt (loc, block, true);
16682 add_stmt (block);
16684 return ret;
16687 /* OpenMP 4.0:
16688 # pragma omp teams teams-clause[optseq] new-line
16689 structured-block */
16691 #define OMP_TEAMS_CLAUSE_MASK \
16692 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16700 static tree
16701 c_parser_omp_teams (location_t loc, c_parser *parser,
16702 char *p_name, omp_clause_mask mask, tree *cclauses,
16703 bool *if_p)
16705 tree clauses, block, ret;
16707 strcat (p_name, " teams");
16708 mask |= OMP_TEAMS_CLAUSE_MASK;
16710 if (c_parser_next_token_is (parser, CPP_NAME))
16712 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16713 if (strcmp (p, "distribute") == 0)
16715 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16716 if (cclauses == NULL)
16717 cclauses = cclauses_buf;
16719 c_parser_consume_token (parser);
16720 if (!flag_openmp) /* flag_openmp_simd */
16721 return c_parser_omp_distribute (loc, parser, p_name, mask,
16722 cclauses, if_p);
16723 block = c_begin_compound_stmt (true);
16724 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16725 if_p);
16726 block = c_end_compound_stmt (loc, block, true);
16727 if (ret == NULL)
16728 return ret;
16729 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16730 ret = make_node (OMP_TEAMS);
16731 TREE_TYPE (ret) = void_type_node;
16732 OMP_TEAMS_CLAUSES (ret) = clauses;
16733 OMP_TEAMS_BODY (ret) = block;
16734 OMP_TEAMS_COMBINED (ret) = 1;
16735 return add_stmt (ret);
16738 if (!flag_openmp) /* flag_openmp_simd */
16740 c_parser_skip_to_pragma_eol (parser, false);
16741 return NULL_TREE;
16744 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16745 if (cclauses)
16747 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16748 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16751 tree stmt = make_node (OMP_TEAMS);
16752 TREE_TYPE (stmt) = void_type_node;
16753 OMP_TEAMS_CLAUSES (stmt) = clauses;
16754 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16756 return add_stmt (stmt);
16759 /* OpenMP 4.0:
16760 # pragma omp target data target-data-clause[optseq] new-line
16761 structured-block */
16763 #define OMP_TARGET_DATA_CLAUSE_MASK \
16764 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16769 static tree
16770 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16772 tree clauses
16773 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16774 "#pragma omp target data");
16775 int map_seen = 0;
16776 for (tree *pc = &clauses; *pc;)
16778 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16779 switch (OMP_CLAUSE_MAP_KIND (*pc))
16781 case GOMP_MAP_TO:
16782 case GOMP_MAP_ALWAYS_TO:
16783 case GOMP_MAP_FROM:
16784 case GOMP_MAP_ALWAYS_FROM:
16785 case GOMP_MAP_TOFROM:
16786 case GOMP_MAP_ALWAYS_TOFROM:
16787 case GOMP_MAP_ALLOC:
16788 map_seen = 3;
16789 break;
16790 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16791 case GOMP_MAP_ALWAYS_POINTER:
16792 break;
16793 default:
16794 map_seen |= 1;
16795 error_at (OMP_CLAUSE_LOCATION (*pc),
16796 "%<#pragma omp target data%> with map-type other "
16797 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16798 "on %<map%> clause");
16799 *pc = OMP_CLAUSE_CHAIN (*pc);
16800 continue;
16802 pc = &OMP_CLAUSE_CHAIN (*pc);
16805 if (map_seen != 3)
16807 if (map_seen == 0)
16808 error_at (loc,
16809 "%<#pragma omp target data%> must contain at least "
16810 "one %<map%> clause");
16811 return NULL_TREE;
16814 tree stmt = make_node (OMP_TARGET_DATA);
16815 TREE_TYPE (stmt) = void_type_node;
16816 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16817 keep_next_level ();
16818 tree block = c_begin_compound_stmt (true);
16819 add_stmt (c_parser_omp_structured_block (parser, if_p));
16820 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16822 SET_EXPR_LOCATION (stmt, loc);
16823 return add_stmt (stmt);
16826 /* OpenMP 4.0:
16827 # pragma omp target update target-update-clause[optseq] new-line */
16829 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16837 static bool
16838 c_parser_omp_target_update (location_t loc, c_parser *parser,
16839 enum pragma_context context)
16841 if (context == pragma_stmt)
16843 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16844 "omp target update");
16845 c_parser_skip_to_pragma_eol (parser, false);
16846 return false;
16849 tree clauses
16850 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16851 "#pragma omp target update");
16852 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16853 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16855 error_at (loc,
16856 "%<#pragma omp target update%> must contain at least one "
16857 "%<from%> or %<to%> clauses");
16858 return false;
16861 tree stmt = make_node (OMP_TARGET_UPDATE);
16862 TREE_TYPE (stmt) = void_type_node;
16863 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16864 SET_EXPR_LOCATION (stmt, loc);
16865 add_stmt (stmt);
16866 return false;
16869 /* OpenMP 4.5:
16870 # pragma omp target enter data target-data-clause[optseq] new-line */
16872 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16873 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16879 static tree
16880 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16881 enum pragma_context context)
16883 bool data_seen = false;
16884 if (c_parser_next_token_is (parser, CPP_NAME))
16886 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16887 if (strcmp (p, "data") == 0)
16889 c_parser_consume_token (parser);
16890 data_seen = true;
16893 if (!data_seen)
16895 c_parser_error (parser, "expected %<data%>");
16896 c_parser_skip_to_pragma_eol (parser);
16897 return NULL_TREE;
16900 if (context == pragma_stmt)
16902 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16903 "omp target enter data");
16904 c_parser_skip_to_pragma_eol (parser, false);
16905 return NULL_TREE;
16908 tree clauses
16909 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16910 "#pragma omp target enter data");
16911 int map_seen = 0;
16912 for (tree *pc = &clauses; *pc;)
16914 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16915 switch (OMP_CLAUSE_MAP_KIND (*pc))
16917 case GOMP_MAP_TO:
16918 case GOMP_MAP_ALWAYS_TO:
16919 case GOMP_MAP_ALLOC:
16920 map_seen = 3;
16921 break;
16922 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16923 case GOMP_MAP_ALWAYS_POINTER:
16924 break;
16925 default:
16926 map_seen |= 1;
16927 error_at (OMP_CLAUSE_LOCATION (*pc),
16928 "%<#pragma omp target enter data%> with map-type other "
16929 "than %<to%> or %<alloc%> on %<map%> clause");
16930 *pc = OMP_CLAUSE_CHAIN (*pc);
16931 continue;
16933 pc = &OMP_CLAUSE_CHAIN (*pc);
16936 if (map_seen != 3)
16938 if (map_seen == 0)
16939 error_at (loc,
16940 "%<#pragma omp target enter data%> must contain at least "
16941 "one %<map%> clause");
16942 return NULL_TREE;
16945 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16946 TREE_TYPE (stmt) = void_type_node;
16947 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16948 SET_EXPR_LOCATION (stmt, loc);
16949 add_stmt (stmt);
16950 return stmt;
16953 /* OpenMP 4.5:
16954 # pragma omp target exit data target-data-clause[optseq] new-line */
16956 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16963 static tree
16964 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16965 enum pragma_context context)
16967 bool data_seen = false;
16968 if (c_parser_next_token_is (parser, CPP_NAME))
16970 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16971 if (strcmp (p, "data") == 0)
16973 c_parser_consume_token (parser);
16974 data_seen = true;
16977 if (!data_seen)
16979 c_parser_error (parser, "expected %<data%>");
16980 c_parser_skip_to_pragma_eol (parser);
16981 return NULL_TREE;
16984 if (context == pragma_stmt)
16986 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16987 "omp target exit data");
16988 c_parser_skip_to_pragma_eol (parser, false);
16989 return NULL_TREE;
16992 tree clauses
16993 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16994 "#pragma omp target exit data");
16996 int map_seen = 0;
16997 for (tree *pc = &clauses; *pc;)
16999 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17000 switch (OMP_CLAUSE_MAP_KIND (*pc))
17002 case GOMP_MAP_FROM:
17003 case GOMP_MAP_ALWAYS_FROM:
17004 case GOMP_MAP_RELEASE:
17005 case GOMP_MAP_DELETE:
17006 map_seen = 3;
17007 break;
17008 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17009 case GOMP_MAP_ALWAYS_POINTER:
17010 break;
17011 default:
17012 map_seen |= 1;
17013 error_at (OMP_CLAUSE_LOCATION (*pc),
17014 "%<#pragma omp target exit data%> with map-type other "
17015 "than %<from%>, %<release%> or %<delete%> on %<map%>"
17016 " clause");
17017 *pc = OMP_CLAUSE_CHAIN (*pc);
17018 continue;
17020 pc = &OMP_CLAUSE_CHAIN (*pc);
17023 if (map_seen != 3)
17025 if (map_seen == 0)
17026 error_at (loc,
17027 "%<#pragma omp target exit data%> must contain at least one "
17028 "%<map%> clause");
17029 return NULL_TREE;
17032 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
17033 TREE_TYPE (stmt) = void_type_node;
17034 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
17035 SET_EXPR_LOCATION (stmt, loc);
17036 add_stmt (stmt);
17037 return stmt;
17040 /* OpenMP 4.0:
17041 # pragma omp target target-clause[optseq] new-line
17042 structured-block */
17044 #define OMP_TARGET_CLAUSE_MASK \
17045 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
17050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
17053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17055 static bool
17056 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17058 location_t loc = c_parser_peek_token (parser)->location;
17059 c_parser_consume_pragma (parser);
17060 tree *pc = NULL, stmt, block;
17062 if (context != pragma_stmt && context != pragma_compound)
17064 c_parser_error (parser, "expected declaration specifiers");
17065 c_parser_skip_to_pragma_eol (parser);
17066 return false;
17069 if (c_parser_next_token_is (parser, CPP_NAME))
17071 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17072 enum tree_code ccode = ERROR_MARK;
17074 if (strcmp (p, "teams") == 0)
17075 ccode = OMP_TEAMS;
17076 else if (strcmp (p, "parallel") == 0)
17077 ccode = OMP_PARALLEL;
17078 else if (strcmp (p, "simd") == 0)
17079 ccode = OMP_SIMD;
17080 if (ccode != ERROR_MARK)
17082 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17083 char p_name[sizeof ("#pragma omp target teams distribute "
17084 "parallel for simd")];
17086 c_parser_consume_token (parser);
17087 strcpy (p_name, "#pragma omp target");
17088 if (!flag_openmp) /* flag_openmp_simd */
17090 tree stmt;
17091 switch (ccode)
17093 case OMP_TEAMS:
17094 stmt = c_parser_omp_teams (loc, parser, p_name,
17095 OMP_TARGET_CLAUSE_MASK,
17096 cclauses, if_p);
17097 break;
17098 case OMP_PARALLEL:
17099 stmt = c_parser_omp_parallel (loc, parser, p_name,
17100 OMP_TARGET_CLAUSE_MASK,
17101 cclauses, if_p);
17102 break;
17103 case OMP_SIMD:
17104 stmt = c_parser_omp_simd (loc, parser, p_name,
17105 OMP_TARGET_CLAUSE_MASK,
17106 cclauses, if_p);
17107 break;
17108 default:
17109 gcc_unreachable ();
17111 return stmt != NULL_TREE;
17113 keep_next_level ();
17114 tree block = c_begin_compound_stmt (true), ret;
17115 switch (ccode)
17117 case OMP_TEAMS:
17118 ret = c_parser_omp_teams (loc, parser, p_name,
17119 OMP_TARGET_CLAUSE_MASK, cclauses,
17120 if_p);
17121 break;
17122 case OMP_PARALLEL:
17123 ret = c_parser_omp_parallel (loc, parser, p_name,
17124 OMP_TARGET_CLAUSE_MASK, cclauses,
17125 if_p);
17126 break;
17127 case OMP_SIMD:
17128 ret = c_parser_omp_simd (loc, parser, p_name,
17129 OMP_TARGET_CLAUSE_MASK, cclauses,
17130 if_p);
17131 break;
17132 default:
17133 gcc_unreachable ();
17135 block = c_end_compound_stmt (loc, block, true);
17136 if (ret == NULL_TREE)
17137 return false;
17138 if (ccode == OMP_TEAMS)
17140 /* For combined target teams, ensure the num_teams and
17141 thread_limit clause expressions are evaluated on the host,
17142 before entering the target construct. */
17143 tree c;
17144 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17145 c; c = OMP_CLAUSE_CHAIN (c))
17146 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17147 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17148 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17150 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17151 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17152 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17153 expr, NULL_TREE, NULL_TREE);
17154 add_stmt (expr);
17155 OMP_CLAUSE_OPERAND (c, 0) = expr;
17156 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17157 OMP_CLAUSE_FIRSTPRIVATE);
17158 OMP_CLAUSE_DECL (tc) = tmp;
17159 OMP_CLAUSE_CHAIN (tc)
17160 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17161 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17164 tree stmt = make_node (OMP_TARGET);
17165 TREE_TYPE (stmt) = void_type_node;
17166 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17167 OMP_TARGET_BODY (stmt) = block;
17168 OMP_TARGET_COMBINED (stmt) = 1;
17169 add_stmt (stmt);
17170 pc = &OMP_TARGET_CLAUSES (stmt);
17171 goto check_clauses;
17173 else if (!flag_openmp) /* flag_openmp_simd */
17175 c_parser_skip_to_pragma_eol (parser, false);
17176 return false;
17178 else if (strcmp (p, "data") == 0)
17180 c_parser_consume_token (parser);
17181 c_parser_omp_target_data (loc, parser, if_p);
17182 return true;
17184 else if (strcmp (p, "enter") == 0)
17186 c_parser_consume_token (parser);
17187 c_parser_omp_target_enter_data (loc, parser, context);
17188 return false;
17190 else if (strcmp (p, "exit") == 0)
17192 c_parser_consume_token (parser);
17193 c_parser_omp_target_exit_data (loc, parser, context);
17194 return false;
17196 else if (strcmp (p, "update") == 0)
17198 c_parser_consume_token (parser);
17199 return c_parser_omp_target_update (loc, parser, context);
17202 if (!flag_openmp) /* flag_openmp_simd */
17204 c_parser_skip_to_pragma_eol (parser, false);
17205 return false;
17208 stmt = make_node (OMP_TARGET);
17209 TREE_TYPE (stmt) = void_type_node;
17211 OMP_TARGET_CLAUSES (stmt)
17212 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17213 "#pragma omp target");
17214 pc = &OMP_TARGET_CLAUSES (stmt);
17215 keep_next_level ();
17216 block = c_begin_compound_stmt (true);
17217 add_stmt (c_parser_omp_structured_block (parser, if_p));
17218 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17220 SET_EXPR_LOCATION (stmt, loc);
17221 add_stmt (stmt);
17223 check_clauses:
17224 while (*pc)
17226 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17227 switch (OMP_CLAUSE_MAP_KIND (*pc))
17229 case GOMP_MAP_TO:
17230 case GOMP_MAP_ALWAYS_TO:
17231 case GOMP_MAP_FROM:
17232 case GOMP_MAP_ALWAYS_FROM:
17233 case GOMP_MAP_TOFROM:
17234 case GOMP_MAP_ALWAYS_TOFROM:
17235 case GOMP_MAP_ALLOC:
17236 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17237 case GOMP_MAP_ALWAYS_POINTER:
17238 break;
17239 default:
17240 error_at (OMP_CLAUSE_LOCATION (*pc),
17241 "%<#pragma omp target%> with map-type other "
17242 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17243 "on %<map%> clause");
17244 *pc = OMP_CLAUSE_CHAIN (*pc);
17245 continue;
17247 pc = &OMP_CLAUSE_CHAIN (*pc);
17249 return true;
17252 /* OpenMP 4.0:
17253 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17255 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17263 static void
17264 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17266 auto_vec<c_token> clauses;
17267 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17269 c_token *token = c_parser_peek_token (parser);
17270 if (token->type == CPP_EOF)
17272 c_parser_skip_to_pragma_eol (parser);
17273 return;
17275 clauses.safe_push (*token);
17276 c_parser_consume_token (parser);
17278 clauses.safe_push (*c_parser_peek_token (parser));
17279 c_parser_skip_to_pragma_eol (parser);
17281 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17283 if (c_parser_peek_token (parser)->pragma_kind
17284 != PRAGMA_OMP_DECLARE
17285 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17286 || strcmp (IDENTIFIER_POINTER
17287 (c_parser_peek_2nd_token (parser)->value),
17288 "simd") != 0)
17290 c_parser_error (parser,
17291 "%<#pragma omp declare simd%> must be followed by "
17292 "function declaration or definition or another "
17293 "%<#pragma omp declare simd%>");
17294 return;
17296 c_parser_consume_pragma (parser);
17297 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17299 c_token *token = c_parser_peek_token (parser);
17300 if (token->type == CPP_EOF)
17302 c_parser_skip_to_pragma_eol (parser);
17303 return;
17305 clauses.safe_push (*token);
17306 c_parser_consume_token (parser);
17308 clauses.safe_push (*c_parser_peek_token (parser));
17309 c_parser_skip_to_pragma_eol (parser);
17312 /* Make sure nothing tries to read past the end of the tokens. */
17313 c_token eof_token;
17314 memset (&eof_token, 0, sizeof (eof_token));
17315 eof_token.type = CPP_EOF;
17316 clauses.safe_push (eof_token);
17317 clauses.safe_push (eof_token);
17319 switch (context)
17321 case pragma_external:
17322 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17323 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17325 int ext = disable_extension_diagnostics ();
17327 c_parser_consume_token (parser);
17328 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17329 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17330 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17331 NULL, clauses);
17332 restore_extension_diagnostics (ext);
17334 else
17335 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17336 NULL, clauses);
17337 break;
17338 case pragma_struct:
17339 case pragma_param:
17340 case pragma_stmt:
17341 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17342 "function declaration or definition");
17343 break;
17344 case pragma_compound:
17345 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17346 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17348 int ext = disable_extension_diagnostics ();
17350 c_parser_consume_token (parser);
17351 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17352 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17353 if (c_parser_next_tokens_start_declaration (parser))
17355 c_parser_declaration_or_fndef (parser, true, true, true, true,
17356 true, NULL, clauses);
17357 restore_extension_diagnostics (ext);
17358 break;
17360 restore_extension_diagnostics (ext);
17362 else if (c_parser_next_tokens_start_declaration (parser))
17364 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17365 NULL, clauses);
17366 break;
17368 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17369 "function declaration or definition");
17370 break;
17371 default:
17372 gcc_unreachable ();
17376 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17377 and put that into "omp declare simd" attribute. */
17379 static void
17380 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17381 vec<c_token> clauses)
17383 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17384 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17385 has already processed the tokens. */
17386 if (clauses.exists () && clauses[0].type == CPP_EOF)
17387 return;
17388 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17390 error ("%<#pragma omp declare simd%> not immediately followed by "
17391 "a function declaration or definition");
17392 clauses[0].type = CPP_EOF;
17393 return;
17395 if (clauses.exists () && clauses[0].type != CPP_NAME)
17397 error_at (DECL_SOURCE_LOCATION (fndecl),
17398 "%<#pragma omp declare simd%> not immediately followed by "
17399 "a single function declaration or definition");
17400 clauses[0].type = CPP_EOF;
17401 return;
17404 if (parms == NULL_TREE)
17405 parms = DECL_ARGUMENTS (fndecl);
17407 unsigned int tokens_avail = parser->tokens_avail;
17408 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17411 parser->tokens = clauses.address ();
17412 parser->tokens_avail = clauses.length ();
17414 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17415 while (parser->tokens_avail > 3)
17417 c_token *token = c_parser_peek_token (parser);
17418 gcc_assert (token->type == CPP_NAME
17419 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17420 c_parser_consume_token (parser);
17421 parser->in_pragma = true;
17423 tree c = NULL_TREE;
17424 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17425 "#pragma omp declare simd");
17426 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17427 if (c != NULL_TREE)
17428 c = tree_cons (NULL_TREE, c, NULL_TREE);
17429 c = build_tree_list (get_identifier ("omp declare simd"), c);
17430 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17431 DECL_ATTRIBUTES (fndecl) = c;
17434 parser->tokens = &parser->tokens_buf[0];
17435 parser->tokens_avail = tokens_avail;
17436 if (clauses.exists ())
17437 clauses[0].type = CPP_PRAGMA;
17441 /* OpenMP 4.0:
17442 # pragma omp declare target new-line
17443 declarations and definitions
17444 # pragma omp end declare target new-line
17446 OpenMP 4.5:
17447 # pragma omp declare target ( extended-list ) new-line
17449 # pragma omp declare target declare-target-clauses[seq] new-line */
17451 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17452 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17455 static void
17456 c_parser_omp_declare_target (c_parser *parser)
17458 location_t loc = c_parser_peek_token (parser)->location;
17459 tree clauses = NULL_TREE;
17460 if (c_parser_next_token_is (parser, CPP_NAME))
17461 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17462 "#pragma omp declare target");
17463 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17465 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17466 clauses);
17467 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17468 c_parser_skip_to_pragma_eol (parser);
17470 else
17472 c_parser_skip_to_pragma_eol (parser);
17473 current_omp_declare_target_attribute++;
17474 return;
17476 if (current_omp_declare_target_attribute)
17477 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17478 "%<#pragma omp declare target%> without clauses and "
17479 "%<#pragma omp end declare target%>");
17480 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17482 tree t = OMP_CLAUSE_DECL (c), id;
17483 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17484 tree at2 = lookup_attribute ("omp declare target link",
17485 DECL_ATTRIBUTES (t));
17486 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17488 id = get_identifier ("omp declare target link");
17489 std::swap (at1, at2);
17491 else
17492 id = get_identifier ("omp declare target");
17493 if (at2)
17495 error_at (OMP_CLAUSE_LOCATION (c),
17496 "%qD specified both in declare target %<link%> and %<to%>"
17497 " clauses", t);
17498 continue;
17500 if (!at1)
17502 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17503 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17504 continue;
17506 symtab_node *node = symtab_node::get (t);
17507 if (node != NULL)
17509 node->offloadable = 1;
17510 if (ENABLE_OFFLOADING)
17512 g->have_offload = true;
17513 if (is_a <varpool_node *> (node))
17514 vec_safe_push (offload_vars, t);
17521 static void
17522 c_parser_omp_end_declare_target (c_parser *parser)
17524 location_t loc = c_parser_peek_token (parser)->location;
17525 c_parser_consume_pragma (parser);
17526 if (c_parser_next_token_is (parser, CPP_NAME)
17527 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17528 "declare") == 0)
17530 c_parser_consume_token (parser);
17531 if (c_parser_next_token_is (parser, CPP_NAME)
17532 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17533 "target") == 0)
17534 c_parser_consume_token (parser);
17535 else
17537 c_parser_error (parser, "expected %<target%>");
17538 c_parser_skip_to_pragma_eol (parser);
17539 return;
17542 else
17544 c_parser_error (parser, "expected %<declare%>");
17545 c_parser_skip_to_pragma_eol (parser);
17546 return;
17548 c_parser_skip_to_pragma_eol (parser);
17549 if (!current_omp_declare_target_attribute)
17550 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17551 "%<#pragma omp declare target%>");
17552 else
17553 current_omp_declare_target_attribute--;
17557 /* OpenMP 4.0
17558 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17559 initializer-clause[opt] new-line
17561 initializer-clause:
17562 initializer (omp_priv = initializer)
17563 initializer (function-name (argument-list)) */
17565 static void
17566 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17568 unsigned int tokens_avail = 0, i;
17569 vec<tree> types = vNULL;
17570 vec<c_token> clauses = vNULL;
17571 enum tree_code reduc_code = ERROR_MARK;
17572 tree reduc_id = NULL_TREE;
17573 tree type;
17574 location_t rloc = c_parser_peek_token (parser)->location;
17576 if (context == pragma_struct || context == pragma_param)
17578 error ("%<#pragma omp declare reduction%> not at file or block scope");
17579 goto fail;
17582 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17583 goto fail;
17585 switch (c_parser_peek_token (parser)->type)
17587 case CPP_PLUS:
17588 reduc_code = PLUS_EXPR;
17589 break;
17590 case CPP_MULT:
17591 reduc_code = MULT_EXPR;
17592 break;
17593 case CPP_MINUS:
17594 reduc_code = MINUS_EXPR;
17595 break;
17596 case CPP_AND:
17597 reduc_code = BIT_AND_EXPR;
17598 break;
17599 case CPP_XOR:
17600 reduc_code = BIT_XOR_EXPR;
17601 break;
17602 case CPP_OR:
17603 reduc_code = BIT_IOR_EXPR;
17604 break;
17605 case CPP_AND_AND:
17606 reduc_code = TRUTH_ANDIF_EXPR;
17607 break;
17608 case CPP_OR_OR:
17609 reduc_code = TRUTH_ORIF_EXPR;
17610 break;
17611 case CPP_NAME:
17612 const char *p;
17613 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17614 if (strcmp (p, "min") == 0)
17616 reduc_code = MIN_EXPR;
17617 break;
17619 if (strcmp (p, "max") == 0)
17621 reduc_code = MAX_EXPR;
17622 break;
17624 reduc_id = c_parser_peek_token (parser)->value;
17625 break;
17626 default:
17627 c_parser_error (parser,
17628 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17629 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17630 goto fail;
17633 tree orig_reduc_id, reduc_decl;
17634 orig_reduc_id = reduc_id;
17635 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17636 reduc_decl = c_omp_reduction_decl (reduc_id);
17637 c_parser_consume_token (parser);
17639 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17640 goto fail;
17642 while (true)
17644 location_t loc = c_parser_peek_token (parser)->location;
17645 struct c_type_name *ctype = c_parser_type_name (parser);
17646 if (ctype != NULL)
17648 type = groktypename (ctype, NULL, NULL);
17649 if (type == error_mark_node)
17651 else if ((INTEGRAL_TYPE_P (type)
17652 || TREE_CODE (type) == REAL_TYPE
17653 || TREE_CODE (type) == COMPLEX_TYPE)
17654 && orig_reduc_id == NULL_TREE)
17655 error_at (loc, "predeclared arithmetic type in "
17656 "%<#pragma omp declare reduction%>");
17657 else if (TREE_CODE (type) == FUNCTION_TYPE
17658 || TREE_CODE (type) == ARRAY_TYPE)
17659 error_at (loc, "function or array type in "
17660 "%<#pragma omp declare reduction%>");
17661 else if (TYPE_ATOMIC (type))
17662 error_at (loc, "%<_Atomic%> qualified type in "
17663 "%<#pragma omp declare reduction%>");
17664 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17665 error_at (loc, "const, volatile or restrict qualified type in "
17666 "%<#pragma omp declare reduction%>");
17667 else
17669 tree t;
17670 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17671 if (comptypes (TREE_PURPOSE (t), type))
17673 error_at (loc, "redeclaration of %qs "
17674 "%<#pragma omp declare reduction%> for "
17675 "type %qT",
17676 IDENTIFIER_POINTER (reduc_id)
17677 + sizeof ("omp declare reduction ") - 1,
17678 type);
17679 location_t ploc
17680 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17681 0));
17682 error_at (ploc, "previous %<#pragma omp declare "
17683 "reduction%>");
17684 break;
17686 if (t == NULL_TREE)
17687 types.safe_push (type);
17689 if (c_parser_next_token_is (parser, CPP_COMMA))
17690 c_parser_consume_token (parser);
17691 else
17692 break;
17694 else
17695 break;
17698 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17699 || types.is_empty ())
17701 fail:
17702 clauses.release ();
17703 types.release ();
17704 while (true)
17706 c_token *token = c_parser_peek_token (parser);
17707 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17708 break;
17709 c_parser_consume_token (parser);
17711 c_parser_skip_to_pragma_eol (parser);
17712 return;
17715 if (types.length () > 1)
17717 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17719 c_token *token = c_parser_peek_token (parser);
17720 if (token->type == CPP_EOF)
17721 goto fail;
17722 clauses.safe_push (*token);
17723 c_parser_consume_token (parser);
17725 clauses.safe_push (*c_parser_peek_token (parser));
17726 c_parser_skip_to_pragma_eol (parser);
17728 /* Make sure nothing tries to read past the end of the tokens. */
17729 c_token eof_token;
17730 memset (&eof_token, 0, sizeof (eof_token));
17731 eof_token.type = CPP_EOF;
17732 clauses.safe_push (eof_token);
17733 clauses.safe_push (eof_token);
17736 int errs = errorcount;
17737 FOR_EACH_VEC_ELT (types, i, type)
17739 tokens_avail = parser->tokens_avail;
17740 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17741 if (!clauses.is_empty ())
17743 parser->tokens = clauses.address ();
17744 parser->tokens_avail = clauses.length ();
17745 parser->in_pragma = true;
17748 bool nested = current_function_decl != NULL_TREE;
17749 if (nested)
17750 c_push_function_context ();
17751 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17752 reduc_id, default_function_type);
17753 current_function_decl = fndecl;
17754 allocate_struct_function (fndecl, true);
17755 push_scope ();
17756 tree stmt = push_stmt_list ();
17757 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17758 warn about these. */
17759 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17760 get_identifier ("omp_out"), type);
17761 DECL_ARTIFICIAL (omp_out) = 1;
17762 DECL_CONTEXT (omp_out) = fndecl;
17763 pushdecl (omp_out);
17764 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17765 get_identifier ("omp_in"), type);
17766 DECL_ARTIFICIAL (omp_in) = 1;
17767 DECL_CONTEXT (omp_in) = fndecl;
17768 pushdecl (omp_in);
17769 struct c_expr combiner = c_parser_expression (parser);
17770 struct c_expr initializer;
17771 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17772 bool bad = false;
17773 initializer.set_error ();
17774 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17775 bad = true;
17776 else if (c_parser_next_token_is (parser, CPP_NAME)
17777 && strcmp (IDENTIFIER_POINTER
17778 (c_parser_peek_token (parser)->value),
17779 "initializer") == 0)
17781 c_parser_consume_token (parser);
17782 pop_scope ();
17783 push_scope ();
17784 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17785 get_identifier ("omp_priv"), type);
17786 DECL_ARTIFICIAL (omp_priv) = 1;
17787 DECL_INITIAL (omp_priv) = error_mark_node;
17788 DECL_CONTEXT (omp_priv) = fndecl;
17789 pushdecl (omp_priv);
17790 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17791 get_identifier ("omp_orig"), type);
17792 DECL_ARTIFICIAL (omp_orig) = 1;
17793 DECL_CONTEXT (omp_orig) = fndecl;
17794 pushdecl (omp_orig);
17795 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17796 bad = true;
17797 else if (!c_parser_next_token_is (parser, CPP_NAME))
17799 c_parser_error (parser, "expected %<omp_priv%> or "
17800 "function-name");
17801 bad = true;
17803 else if (strcmp (IDENTIFIER_POINTER
17804 (c_parser_peek_token (parser)->value),
17805 "omp_priv") != 0)
17807 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17808 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17810 c_parser_error (parser, "expected function-name %<(%>");
17811 bad = true;
17813 else
17814 initializer = c_parser_postfix_expression (parser);
17815 if (initializer.value
17816 && TREE_CODE (initializer.value) == CALL_EXPR)
17818 int j;
17819 tree c = initializer.value;
17820 for (j = 0; j < call_expr_nargs (c); j++)
17822 tree a = CALL_EXPR_ARG (c, j);
17823 STRIP_NOPS (a);
17824 if (TREE_CODE (a) == ADDR_EXPR
17825 && TREE_OPERAND (a, 0) == omp_priv)
17826 break;
17828 if (j == call_expr_nargs (c))
17829 error ("one of the initializer call arguments should be "
17830 "%<&omp_priv%>");
17833 else
17835 c_parser_consume_token (parser);
17836 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17837 bad = true;
17838 else
17840 tree st = push_stmt_list ();
17841 location_t loc = c_parser_peek_token (parser)->location;
17842 rich_location richloc (line_table, loc);
17843 start_init (omp_priv, NULL_TREE, 0, &richloc);
17844 struct c_expr init = c_parser_initializer (parser);
17845 finish_init ();
17846 finish_decl (omp_priv, loc, init.value,
17847 init.original_type, NULL_TREE);
17848 pop_stmt_list (st);
17851 if (!bad
17852 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17853 bad = true;
17856 if (!bad)
17858 c_parser_skip_to_pragma_eol (parser);
17860 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17861 DECL_INITIAL (reduc_decl));
17862 DECL_INITIAL (reduc_decl) = t;
17863 DECL_SOURCE_LOCATION (omp_out) = rloc;
17864 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17865 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17866 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17867 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17868 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17869 if (omp_priv)
17871 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17872 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17873 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17874 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17875 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17876 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17877 walk_tree (&DECL_INITIAL (omp_priv),
17878 c_check_omp_declare_reduction_r,
17879 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17883 pop_stmt_list (stmt);
17884 pop_scope ();
17885 if (cfun->language != NULL)
17887 ggc_free (cfun->language);
17888 cfun->language = NULL;
17890 set_cfun (NULL);
17891 current_function_decl = NULL_TREE;
17892 if (nested)
17893 c_pop_function_context ();
17895 if (!clauses.is_empty ())
17897 parser->tokens = &parser->tokens_buf[0];
17898 parser->tokens_avail = tokens_avail;
17900 if (bad)
17901 goto fail;
17902 if (errs != errorcount)
17903 break;
17906 clauses.release ();
17907 types.release ();
17911 /* OpenMP 4.0
17912 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17913 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17914 initializer-clause[opt] new-line
17915 #pragma omp declare target new-line */
17917 static void
17918 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17920 c_parser_consume_pragma (parser);
17921 if (c_parser_next_token_is (parser, CPP_NAME))
17923 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17924 if (strcmp (p, "simd") == 0)
17926 /* c_parser_consume_token (parser); done in
17927 c_parser_omp_declare_simd. */
17928 c_parser_omp_declare_simd (parser, context);
17929 return;
17931 if (strcmp (p, "reduction") == 0)
17933 c_parser_consume_token (parser);
17934 c_parser_omp_declare_reduction (parser, context);
17935 return;
17937 if (!flag_openmp) /* flag_openmp_simd */
17939 c_parser_skip_to_pragma_eol (parser, false);
17940 return;
17942 if (strcmp (p, "target") == 0)
17944 c_parser_consume_token (parser);
17945 c_parser_omp_declare_target (parser);
17946 return;
17950 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17951 "or %<target%>");
17952 c_parser_skip_to_pragma_eol (parser);
17955 /* OpenMP 4.5:
17956 #pragma omp taskloop taskloop-clause[optseq] new-line
17957 for-loop
17959 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17960 for-loop */
17962 #define OMP_TASKLOOP_CLAUSE_MASK \
17963 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17978 static tree
17979 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17980 char *p_name, omp_clause_mask mask, tree *cclauses,
17981 bool *if_p)
17983 tree clauses, block, ret;
17985 strcat (p_name, " taskloop");
17986 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17988 if (c_parser_next_token_is (parser, CPP_NAME))
17990 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17992 if (strcmp (p, "simd") == 0)
17994 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17995 if (cclauses == NULL)
17996 cclauses = cclauses_buf;
17997 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17998 c_parser_consume_token (parser);
17999 if (!flag_openmp) /* flag_openmp_simd */
18000 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
18001 if_p);
18002 block = c_begin_compound_stmt (true);
18003 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
18004 block = c_end_compound_stmt (loc, block, true);
18005 if (ret == NULL)
18006 return ret;
18007 ret = make_node (OMP_TASKLOOP);
18008 TREE_TYPE (ret) = void_type_node;
18009 OMP_FOR_BODY (ret) = block;
18010 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18011 SET_EXPR_LOCATION (ret, loc);
18012 add_stmt (ret);
18013 return ret;
18016 if (!flag_openmp) /* flag_openmp_simd */
18018 c_parser_skip_to_pragma_eol (parser, false);
18019 return NULL_TREE;
18022 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18023 if (cclauses)
18025 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
18026 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18029 block = c_begin_compound_stmt (true);
18030 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
18031 block = c_end_compound_stmt (loc, block, true);
18032 add_stmt (block);
18034 return ret;
18037 /* Main entry point to parsing most OpenMP pragmas. */
18039 static void
18040 c_parser_omp_construct (c_parser *parser, bool *if_p)
18042 enum pragma_kind p_kind;
18043 location_t loc;
18044 tree stmt;
18045 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18046 omp_clause_mask mask (0);
18048 loc = c_parser_peek_token (parser)->location;
18049 p_kind = c_parser_peek_token (parser)->pragma_kind;
18050 c_parser_consume_pragma (parser);
18052 switch (p_kind)
18054 case PRAGMA_OACC_ATOMIC:
18055 c_parser_omp_atomic (loc, parser);
18056 return;
18057 case PRAGMA_OACC_CACHE:
18058 strcpy (p_name, "#pragma acc");
18059 stmt = c_parser_oacc_cache (loc, parser);
18060 break;
18061 case PRAGMA_OACC_DATA:
18062 stmt = c_parser_oacc_data (loc, parser, if_p);
18063 break;
18064 case PRAGMA_OACC_HOST_DATA:
18065 stmt = c_parser_oacc_host_data (loc, parser, if_p);
18066 break;
18067 case PRAGMA_OACC_KERNELS:
18068 case PRAGMA_OACC_PARALLEL:
18069 strcpy (p_name, "#pragma acc");
18070 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18071 if_p);
18072 break;
18073 case PRAGMA_OACC_LOOP:
18074 strcpy (p_name, "#pragma acc");
18075 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18076 break;
18077 case PRAGMA_OACC_WAIT:
18078 strcpy (p_name, "#pragma wait");
18079 stmt = c_parser_oacc_wait (loc, parser, p_name);
18080 break;
18081 case PRAGMA_OMP_ATOMIC:
18082 c_parser_omp_atomic (loc, parser);
18083 return;
18084 case PRAGMA_OMP_CRITICAL:
18085 stmt = c_parser_omp_critical (loc, parser, if_p);
18086 break;
18087 case PRAGMA_OMP_DISTRIBUTE:
18088 strcpy (p_name, "#pragma omp");
18089 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18090 break;
18091 case PRAGMA_OMP_FOR:
18092 strcpy (p_name, "#pragma omp");
18093 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18094 break;
18095 case PRAGMA_OMP_MASTER:
18096 stmt = c_parser_omp_master (loc, parser, if_p);
18097 break;
18098 case PRAGMA_OMP_PARALLEL:
18099 strcpy (p_name, "#pragma omp");
18100 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18101 break;
18102 case PRAGMA_OMP_SECTIONS:
18103 strcpy (p_name, "#pragma omp");
18104 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18105 break;
18106 case PRAGMA_OMP_SIMD:
18107 strcpy (p_name, "#pragma omp");
18108 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18109 break;
18110 case PRAGMA_OMP_SINGLE:
18111 stmt = c_parser_omp_single (loc, parser, if_p);
18112 break;
18113 case PRAGMA_OMP_TASK:
18114 stmt = c_parser_omp_task (loc, parser, if_p);
18115 break;
18116 case PRAGMA_OMP_TASKGROUP:
18117 stmt = c_parser_omp_taskgroup (parser, if_p);
18118 break;
18119 case PRAGMA_OMP_TASKLOOP:
18120 strcpy (p_name, "#pragma omp");
18121 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18122 break;
18123 case PRAGMA_OMP_TEAMS:
18124 strcpy (p_name, "#pragma omp");
18125 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18126 break;
18127 default:
18128 gcc_unreachable ();
18131 if (stmt)
18132 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18136 /* OpenMP 2.5:
18137 # pragma omp threadprivate (variable-list) */
18139 static void
18140 c_parser_omp_threadprivate (c_parser *parser)
18142 tree vars, t;
18143 location_t loc;
18145 c_parser_consume_pragma (parser);
18146 loc = c_parser_peek_token (parser)->location;
18147 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18149 /* Mark every variable in VARS to be assigned thread local storage. */
18150 for (t = vars; t; t = TREE_CHAIN (t))
18152 tree v = TREE_PURPOSE (t);
18154 /* FIXME diagnostics: Ideally we should keep individual
18155 locations for all the variables in the var list to make the
18156 following errors more precise. Perhaps
18157 c_parser_omp_var_list_parens() should construct a list of
18158 locations to go along with the var list. */
18160 /* If V had already been marked threadprivate, it doesn't matter
18161 whether it had been used prior to this point. */
18162 if (!VAR_P (v))
18163 error_at (loc, "%qD is not a variable", v);
18164 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18165 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18166 else if (! is_global_var (v))
18167 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18168 else if (TREE_TYPE (v) == error_mark_node)
18170 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18171 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18172 else
18174 if (! DECL_THREAD_LOCAL_P (v))
18176 set_decl_tls_model (v, decl_default_tls_model (v));
18177 /* If rtl has been already set for this var, call
18178 make_decl_rtl once again, so that encode_section_info
18179 has a chance to look at the new decl flags. */
18180 if (DECL_RTL_SET_P (v))
18181 make_decl_rtl (v);
18183 C_DECL_THREADPRIVATE_P (v) = 1;
18187 c_parser_skip_to_pragma_eol (parser);
18190 /* Parse a transaction attribute (GCC Extension).
18192 transaction-attribute:
18193 attributes
18194 [ [ any-word ] ]
18196 The transactional memory language description is written for C++,
18197 and uses the C++0x attribute syntax. For compatibility, allow the
18198 bracket style for transactions in C as well. */
18200 static tree
18201 c_parser_transaction_attributes (c_parser *parser)
18203 tree attr_name, attr = NULL;
18205 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18206 return c_parser_attributes (parser);
18208 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18209 return NULL_TREE;
18210 c_parser_consume_token (parser);
18211 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18212 goto error1;
18214 attr_name = c_parser_attribute_any_word (parser);
18215 if (attr_name)
18217 c_parser_consume_token (parser);
18218 attr = build_tree_list (attr_name, NULL_TREE);
18220 else
18221 c_parser_error (parser, "expected identifier");
18223 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18224 error1:
18225 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18226 return attr;
18229 /* Parse a __transaction_atomic or __transaction_relaxed statement
18230 (GCC Extension).
18232 transaction-statement:
18233 __transaction_atomic transaction-attribute[opt] compound-statement
18234 __transaction_relaxed compound-statement
18236 Note that the only valid attribute is: "outer".
18239 static tree
18240 c_parser_transaction (c_parser *parser, enum rid keyword)
18242 unsigned int old_in = parser->in_transaction;
18243 unsigned int this_in = 1, new_in;
18244 location_t loc = c_parser_peek_token (parser)->location;
18245 tree stmt, attrs;
18247 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18248 || keyword == RID_TRANSACTION_RELAXED)
18249 && c_parser_next_token_is_keyword (parser, keyword));
18250 c_parser_consume_token (parser);
18252 if (keyword == RID_TRANSACTION_RELAXED)
18253 this_in |= TM_STMT_ATTR_RELAXED;
18254 else
18256 attrs = c_parser_transaction_attributes (parser);
18257 if (attrs)
18258 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18261 /* Keep track if we're in the lexical scope of an outer transaction. */
18262 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18264 parser->in_transaction = new_in;
18265 stmt = c_parser_compound_statement (parser);
18266 parser->in_transaction = old_in;
18268 if (flag_tm)
18269 stmt = c_finish_transaction (loc, stmt, this_in);
18270 else
18271 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18272 "%<__transaction_atomic%> without transactional memory support enabled"
18273 : "%<__transaction_relaxed %> "
18274 "without transactional memory support enabled"));
18276 return stmt;
18279 /* Parse a __transaction_atomic or __transaction_relaxed expression
18280 (GCC Extension).
18282 transaction-expression:
18283 __transaction_atomic ( expression )
18284 __transaction_relaxed ( expression )
18287 static struct c_expr
18288 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18290 struct c_expr ret;
18291 unsigned int old_in = parser->in_transaction;
18292 unsigned int this_in = 1;
18293 location_t loc = c_parser_peek_token (parser)->location;
18294 tree attrs;
18296 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18297 || keyword == RID_TRANSACTION_RELAXED)
18298 && c_parser_next_token_is_keyword (parser, keyword));
18299 c_parser_consume_token (parser);
18301 if (keyword == RID_TRANSACTION_RELAXED)
18302 this_in |= TM_STMT_ATTR_RELAXED;
18303 else
18305 attrs = c_parser_transaction_attributes (parser);
18306 if (attrs)
18307 this_in |= parse_tm_stmt_attr (attrs, 0);
18310 parser->in_transaction = this_in;
18311 matching_parens parens;
18312 if (parens.require_open (parser))
18314 tree expr = c_parser_expression (parser).value;
18315 ret.original_type = TREE_TYPE (expr);
18316 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18317 if (this_in & TM_STMT_ATTR_RELAXED)
18318 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18319 SET_EXPR_LOCATION (ret.value, loc);
18320 ret.original_code = TRANSACTION_EXPR;
18321 if (!parens.require_close (parser))
18323 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18324 goto error;
18327 else
18329 error:
18330 ret.set_error ();
18331 ret.original_code = ERROR_MARK;
18332 ret.original_type = NULL;
18334 parser->in_transaction = old_in;
18336 if (!flag_tm)
18337 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18338 "%<__transaction_atomic%> without transactional memory support enabled"
18339 : "%<__transaction_relaxed %> "
18340 "without transactional memory support enabled"));
18342 set_c_expr_source_range (&ret, loc, loc);
18344 return ret;
18347 /* Parse a __transaction_cancel statement (GCC Extension).
18349 transaction-cancel-statement:
18350 __transaction_cancel transaction-attribute[opt] ;
18352 Note that the only valid attribute is "outer".
18355 static tree
18356 c_parser_transaction_cancel (c_parser *parser)
18358 location_t loc = c_parser_peek_token (parser)->location;
18359 tree attrs;
18360 bool is_outer = false;
18362 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18363 c_parser_consume_token (parser);
18365 attrs = c_parser_transaction_attributes (parser);
18366 if (attrs)
18367 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18369 if (!flag_tm)
18371 error_at (loc, "%<__transaction_cancel%> without "
18372 "transactional memory support enabled");
18373 goto ret_error;
18375 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18377 error_at (loc, "%<__transaction_cancel%> within a "
18378 "%<__transaction_relaxed%>");
18379 goto ret_error;
18381 else if (is_outer)
18383 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18384 && !is_tm_may_cancel_outer (current_function_decl))
18386 error_at (loc, "outer %<__transaction_cancel%> not "
18387 "within outer %<__transaction_atomic%>");
18388 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18389 goto ret_error;
18392 else if (parser->in_transaction == 0)
18394 error_at (loc, "%<__transaction_cancel%> not within "
18395 "%<__transaction_atomic%>");
18396 goto ret_error;
18399 return add_stmt (build_tm_abort_call (loc, is_outer));
18401 ret_error:
18402 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18405 /* Parse a single source file. */
18407 void
18408 c_parse_file (void)
18410 /* Use local storage to begin. If the first token is a pragma, parse it.
18411 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18412 which will cause garbage collection. */
18413 c_parser tparser;
18415 memset (&tparser, 0, sizeof tparser);
18416 tparser.tokens = &tparser.tokens_buf[0];
18417 the_parser = &tparser;
18419 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18420 c_parser_pragma_pch_preprocess (&tparser);
18422 the_parser = ggc_alloc<c_parser> ();
18423 *the_parser = tparser;
18424 if (tparser.tokens == &tparser.tokens_buf[0])
18425 the_parser->tokens = &the_parser->tokens_buf[0];
18427 /* Initialize EH, if we've been told to do so. */
18428 if (flag_exceptions)
18429 using_eh_for_cleanups ();
18431 c_parser_translation_unit (the_parser);
18432 the_parser = NULL;
18435 /* Parse the body of a function declaration marked with "__RTL".
18437 The RTL parser works on the level of characters read from a
18438 FILE *, whereas c_parser works at the level of tokens.
18439 Square this circle by consuming all of the tokens up to and
18440 including the closing brace, recording the start/end of the RTL
18441 fragment, and reopening the file and re-reading the relevant
18442 lines within the RTL parser.
18444 This requires the opening and closing braces of the C function
18445 to be on separate lines from the RTL they wrap.
18447 Take ownership of START_WITH_PASS, if non-NULL. */
18449 void
18450 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18452 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18454 free (start_with_pass);
18455 return;
18458 location_t start_loc = c_parser_peek_token (parser)->location;
18460 /* Consume all tokens, up to the closing brace, handling
18461 matching pairs of braces in the rtl dump. */
18462 int num_open_braces = 1;
18463 while (1)
18465 switch (c_parser_peek_token (parser)->type)
18467 case CPP_OPEN_BRACE:
18468 num_open_braces++;
18469 break;
18470 case CPP_CLOSE_BRACE:
18471 if (--num_open_braces == 0)
18472 goto found_closing_brace;
18473 break;
18474 case CPP_EOF:
18475 error_at (start_loc, "no closing brace");
18476 free (start_with_pass);
18477 return;
18478 default:
18479 break;
18481 c_parser_consume_token (parser);
18484 found_closing_brace:
18485 /* At the closing brace; record its location. */
18486 location_t end_loc = c_parser_peek_token (parser)->location;
18488 /* Consume the closing brace. */
18489 c_parser_consume_token (parser);
18491 /* Invoke the RTL parser. */
18492 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18494 free (start_with_pass);
18495 return;
18498 /* If a pass name was provided for START_WITH_PASS, run the backend
18499 accordingly now, on the cfun created above, transferring
18500 ownership of START_WITH_PASS. */
18501 if (start_with_pass)
18502 run_rtl_passes (start_with_pass);
18505 #include "gt-c-c-parser.h"