Allow _Alignas in compound literals (C11 DR#444).
[official-gcc.git] / gcc / c / c-parser.c
blobd39854805048166d34d4f3293b7db345e0b31ffe
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
72 /* We need to walk over decls with incomplete struct/union/enum types
73 after parsing the whole translation unit.
74 In finish_decl(), if the decl is static, has incomplete
75 struct/union/enum type, it is appeneded to incomplete_record_decls.
76 In c_parser_translation_unit(), we iterate over incomplete_record_decls
77 and report error if any of the decls are still incomplete. */
79 vec<tree> incomplete_record_decls;
81 void
82 set_c_expr_source_range (c_expr *expr,
83 location_t start, location_t finish)
85 expr->src_range.m_start = start;
86 expr->src_range.m_finish = finish;
87 if (expr->value)
88 set_source_range (expr->value, start, finish);
91 void
92 set_c_expr_source_range (c_expr *expr,
93 source_range src_range)
95 expr->src_range = src_range;
96 if (expr->value)
97 set_source_range (expr->value, src_range);
101 /* Initialization routine for this file. */
103 void
104 c_parse_init (void)
106 /* The only initialization required is of the reserved word
107 identifiers. */
108 unsigned int i;
109 tree id;
110 int mask = 0;
112 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
113 the c_token structure. */
114 gcc_assert (RID_MAX <= 255);
116 mask |= D_CXXONLY;
117 if (!flag_isoc99)
118 mask |= D_C99;
119 if (flag_no_asm)
121 mask |= D_ASM | D_EXT;
122 if (!flag_isoc99)
123 mask |= D_EXT89;
125 if (!c_dialect_objc ())
126 mask |= D_OBJC | D_CXX_OBJC;
128 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
129 for (i = 0; i < num_c_common_reswords; i++)
131 /* If a keyword is disabled, do not enter it into the table
132 and so create a canonical spelling that isn't a keyword. */
133 if (c_common_reswords[i].disable & mask)
135 if (warn_cxx_compat
136 && (c_common_reswords[i].disable & D_CXXWARN))
138 id = get_identifier (c_common_reswords[i].word);
139 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
140 C_IS_RESERVED_WORD (id) = 1;
142 continue;
145 id = get_identifier (c_common_reswords[i].word);
146 C_SET_RID_CODE (id, c_common_reswords[i].rid);
147 C_IS_RESERVED_WORD (id) = 1;
148 ridpointers [(int) c_common_reswords[i].rid] = id;
151 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 /* We always create the symbols but they aren't always supported. */
154 char name[50];
155 sprintf (name, "__int%d", int_n_data[i].bitsize);
156 id = get_identifier (name);
157 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
158 C_IS_RESERVED_WORD (id) = 1;
162 /* A parser structure recording information about the state and
163 context of parsing. Includes lexer information with up to two
164 tokens of look-ahead; more are not needed for C. */
165 struct GTY(()) c_parser {
166 /* The look-ahead tokens. */
167 c_token * GTY((skip)) tokens;
168 /* Buffer for look-ahead tokens. */
169 c_token tokens_buf[4];
170 /* How many look-ahead tokens are available (0 - 4, or
171 more if parsing from pre-lexed tokens). */
172 unsigned int tokens_avail;
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
185 /* Objective-C specific parser/lexer information. */
187 /* True if we are in a context where the Objective-C "PQ" keywords
188 are considered keywords. */
189 BOOL_BITFIELD objc_pq_context : 1;
190 /* True if we are parsing a (potential) Objective-C foreach
191 statement. This is set to true after we parsed 'for (' and while
192 we wait for 'in' or ';' to decide if it's a standard C for loop or an
193 Objective-C foreach loop. */
194 BOOL_BITFIELD objc_could_be_foreach_context : 1;
195 /* The following flag is needed to contextualize Objective-C lexical
196 analysis. In some cases (e.g., 'int NSObject;'), it is
197 undesirable to bind an identifier to an Objective-C class, even
198 if a class with that name exists. */
199 BOOL_BITFIELD objc_need_raw_identifier : 1;
200 /* Nonzero if we're processing a __transaction statement. The value
201 is 1 | TM_STMT_ATTR_*. */
202 unsigned int in_transaction : 4;
203 /* True if we are in a context where the Objective-C "Property attribute"
204 keywords are valid. */
205 BOOL_BITFIELD objc_property_attr_context : 1;
207 /* Location of the last consumed token. */
208 location_t last_token_location;
211 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
213 c_token *
214 c_parser_tokens_buf (c_parser *parser, unsigned n)
216 return &parser->tokens_buf[n];
219 /* Return the error state of PARSER. */
221 bool
222 c_parser_error (c_parser *parser)
224 return parser->error;
227 /* Set the error state of PARSER to ERR. */
229 void
230 c_parser_set_error (c_parser *parser, bool err)
232 parser->error = err;
236 /* The actual parser and external interface. ??? Does this need to be
237 garbage-collected? */
239 static GTY (()) c_parser *the_parser;
241 /* Read in and lex a single token, storing it in *TOKEN. */
243 static void
244 c_lex_one_token (c_parser *parser, c_token *token)
246 timevar_push (TV_LEX);
248 token->type = c_lex_with_flags (&token->value, &token->location,
249 &token->flags,
250 (parser->lex_untranslated_string
251 ? C_LEX_STRING_NO_TRANSLATE : 0));
252 token->id_kind = C_ID_NONE;
253 token->keyword = RID_MAX;
254 token->pragma_kind = PRAGMA_NONE;
256 switch (token->type)
258 case CPP_NAME:
260 tree decl;
262 bool objc_force_identifier = parser->objc_need_raw_identifier;
263 if (c_dialect_objc ())
264 parser->objc_need_raw_identifier = false;
266 if (C_IS_RESERVED_WORD (token->value))
268 enum rid rid_code = C_RID_CODE (token->value);
270 if (rid_code == RID_CXX_COMPAT_WARN)
272 warning_at (token->location,
273 OPT_Wc___compat,
274 "identifier %qE conflicts with C++ keyword",
275 token->value);
277 else if (rid_code >= RID_FIRST_ADDR_SPACE
278 && rid_code <= RID_LAST_ADDR_SPACE)
280 addr_space_t as;
281 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
282 targetm.addr_space.diagnose_usage (as, token->location);
283 token->id_kind = C_ID_ADDRSPACE;
284 token->keyword = rid_code;
285 break;
287 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
289 /* We found an Objective-C "pq" keyword (in, out,
290 inout, bycopy, byref, oneway). They need special
291 care because the interpretation depends on the
292 context. */
293 if (parser->objc_pq_context)
295 token->type = CPP_KEYWORD;
296 token->keyword = rid_code;
297 break;
299 else if (parser->objc_could_be_foreach_context
300 && rid_code == RID_IN)
302 /* We are in Objective-C, inside a (potential)
303 foreach context (which means after having
304 parsed 'for (', but before having parsed ';'),
305 and we found 'in'. We consider it the keyword
306 which terminates the declaration at the
307 beginning of a foreach-statement. Note that
308 this means you can't use 'in' for anything else
309 in that context; in particular, in Objective-C
310 you can't use 'in' as the name of the running
311 variable in a C for loop. We could potentially
312 try to add code here to disambiguate, but it
313 seems a reasonable limitation. */
314 token->type = CPP_KEYWORD;
315 token->keyword = rid_code;
316 break;
318 /* Else, "pq" keywords outside of the "pq" context are
319 not keywords, and we fall through to the code for
320 normal tokens. */
322 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
324 /* We found an Objective-C "property attribute"
325 keyword (getter, setter, readonly, etc). These are
326 only valid in the property context. */
327 if (parser->objc_property_attr_context)
329 token->type = CPP_KEYWORD;
330 token->keyword = rid_code;
331 break;
333 /* Else they are not special keywords.
336 else if (c_dialect_objc ()
337 && (OBJC_IS_AT_KEYWORD (rid_code)
338 || OBJC_IS_CXX_KEYWORD (rid_code)))
340 /* We found one of the Objective-C "@" keywords (defs,
341 selector, synchronized, etc) or one of the
342 Objective-C "cxx" keywords (class, private,
343 protected, public, try, catch, throw) without a
344 preceding '@' sign. Do nothing and fall through to
345 the code for normal tokens (in C++ we would still
346 consider the CXX ones keywords, but not in C). */
349 else
351 token->type = CPP_KEYWORD;
352 token->keyword = rid_code;
353 break;
357 decl = lookup_name (token->value);
358 if (decl)
360 if (TREE_CODE (decl) == TYPE_DECL)
362 token->id_kind = C_ID_TYPENAME;
363 break;
366 else if (c_dialect_objc ())
368 tree objc_interface_decl = objc_is_class_name (token->value);
369 /* Objective-C class names are in the same namespace as
370 variables and typedefs, and hence are shadowed by local
371 declarations. */
372 if (objc_interface_decl
373 && (!objc_force_identifier || global_bindings_p ()))
375 token->value = objc_interface_decl;
376 token->id_kind = C_ID_CLASSNAME;
377 break;
380 token->id_kind = C_ID_ID;
382 break;
383 case CPP_AT_NAME:
384 /* This only happens in Objective-C; it must be a keyword. */
385 token->type = CPP_KEYWORD;
386 switch (C_RID_CODE (token->value))
388 /* Replace 'class' with '@class', 'private' with '@private',
389 etc. This prevents confusion with the C++ keyword
390 'class', and makes the tokens consistent with other
391 Objective-C 'AT' keywords. For example '@class' is
392 reported as RID_AT_CLASS which is consistent with
393 '@synchronized', which is reported as
394 RID_AT_SYNCHRONIZED.
396 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
397 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
398 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
399 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
400 case RID_THROW: token->keyword = RID_AT_THROW; break;
401 case RID_TRY: token->keyword = RID_AT_TRY; break;
402 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
403 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
404 default: token->keyword = C_RID_CODE (token->value);
406 break;
407 case CPP_COLON:
408 case CPP_COMMA:
409 case CPP_CLOSE_PAREN:
410 case CPP_SEMICOLON:
411 /* These tokens may affect the interpretation of any identifiers
412 following, if doing Objective-C. */
413 if (c_dialect_objc ())
414 parser->objc_need_raw_identifier = false;
415 break;
416 case CPP_PRAGMA:
417 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
418 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
419 token->value = NULL;
420 break;
421 default:
422 break;
424 timevar_pop (TV_LEX);
427 /* Return a pointer to the next token from PARSER, reading it in if
428 necessary. */
430 c_token *
431 c_parser_peek_token (c_parser *parser)
433 if (parser->tokens_avail == 0)
435 c_lex_one_token (parser, &parser->tokens[0]);
436 parser->tokens_avail = 1;
438 return &parser->tokens[0];
441 /* Return a pointer to the next-but-one token from PARSER, reading it
442 in if necessary. The next token is already read in. */
444 c_token *
445 c_parser_peek_2nd_token (c_parser *parser)
447 if (parser->tokens_avail >= 2)
448 return &parser->tokens[1];
449 gcc_assert (parser->tokens_avail == 1);
450 gcc_assert (parser->tokens[0].type != CPP_EOF);
451 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
452 c_lex_one_token (parser, &parser->tokens[1]);
453 parser->tokens_avail = 2;
454 return &parser->tokens[1];
457 /* Return a pointer to the Nth token from PARSER, reading it
458 in if necessary. The N-1th token is already read in. */
460 c_token *
461 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
463 /* N is 1-based, not zero-based. */
464 gcc_assert (n > 0);
466 if (parser->tokens_avail >= n)
467 return &parser->tokens[n - 1];
468 gcc_assert (parser->tokens_avail == n - 1);
469 c_lex_one_token (parser, &parser->tokens[n - 1]);
470 parser->tokens_avail = n;
471 return &parser->tokens[n - 1];
474 bool
475 c_keyword_starts_typename (enum rid keyword)
477 switch (keyword)
479 case RID_UNSIGNED:
480 case RID_LONG:
481 case RID_SHORT:
482 case RID_SIGNED:
483 case RID_COMPLEX:
484 case RID_INT:
485 case RID_CHAR:
486 case RID_FLOAT:
487 case RID_DOUBLE:
488 case RID_VOID:
489 case RID_DFLOAT32:
490 case RID_DFLOAT64:
491 case RID_DFLOAT128:
492 CASE_RID_FLOATN_NX:
493 case RID_BOOL:
494 case RID_ENUM:
495 case RID_STRUCT:
496 case RID_UNION:
497 case RID_TYPEOF:
498 case RID_CONST:
499 case RID_ATOMIC:
500 case RID_VOLATILE:
501 case RID_RESTRICT:
502 case RID_ATTRIBUTE:
503 case RID_FRACT:
504 case RID_ACCUM:
505 case RID_SAT:
506 case RID_AUTO_TYPE:
507 case RID_ALIGNAS:
508 return true;
509 default:
510 if (keyword >= RID_FIRST_INT_N
511 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
512 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
513 return true;
514 return false;
518 /* Return true if TOKEN can start a type name,
519 false otherwise. */
520 bool
521 c_token_starts_typename (c_token *token)
523 switch (token->type)
525 case CPP_NAME:
526 switch (token->id_kind)
528 case C_ID_ID:
529 return false;
530 case C_ID_ADDRSPACE:
531 return true;
532 case C_ID_TYPENAME:
533 return true;
534 case C_ID_CLASSNAME:
535 gcc_assert (c_dialect_objc ());
536 return true;
537 default:
538 gcc_unreachable ();
540 case CPP_KEYWORD:
541 return c_keyword_starts_typename (token->keyword);
542 case CPP_LESS:
543 if (c_dialect_objc ())
544 return true;
545 return false;
546 default:
547 return false;
551 /* Return true if the next token from PARSER can start a type name,
552 false otherwise. LA specifies how to do lookahead in order to
553 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
555 static inline bool
556 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
558 c_token *token = c_parser_peek_token (parser);
559 if (c_token_starts_typename (token))
560 return true;
562 /* Try a bit harder to detect an unknown typename. */
563 if (la != cla_prefer_id
564 && token->type == CPP_NAME
565 && token->id_kind == C_ID_ID
567 /* Do not try too hard when we could have "object in array". */
568 && !parser->objc_could_be_foreach_context
570 && (la == cla_prefer_type
571 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
572 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
574 /* Only unknown identifiers. */
575 && !lookup_name (token->value))
576 return true;
578 return false;
581 /* Return true if TOKEN is a type qualifier, false otherwise. */
582 static bool
583 c_token_is_qualifier (c_token *token)
585 switch (token->type)
587 case CPP_NAME:
588 switch (token->id_kind)
590 case C_ID_ADDRSPACE:
591 return true;
592 default:
593 return false;
595 case CPP_KEYWORD:
596 switch (token->keyword)
598 case RID_CONST:
599 case RID_VOLATILE:
600 case RID_RESTRICT:
601 case RID_ATTRIBUTE:
602 case RID_ATOMIC:
603 return true;
604 default:
605 return false;
607 case CPP_LESS:
608 return false;
609 default:
610 gcc_unreachable ();
614 /* Return true if the next token from PARSER is a type qualifier,
615 false otherwise. */
616 static inline bool
617 c_parser_next_token_is_qualifier (c_parser *parser)
619 c_token *token = c_parser_peek_token (parser);
620 return c_token_is_qualifier (token);
623 /* Return true if TOKEN can start declaration specifiers, false
624 otherwise. */
625 static bool
626 c_token_starts_declspecs (c_token *token)
628 switch (token->type)
630 case CPP_NAME:
631 switch (token->id_kind)
633 case C_ID_ID:
634 return false;
635 case C_ID_ADDRSPACE:
636 return true;
637 case C_ID_TYPENAME:
638 return true;
639 case C_ID_CLASSNAME:
640 gcc_assert (c_dialect_objc ());
641 return true;
642 default:
643 gcc_unreachable ();
645 case CPP_KEYWORD:
646 switch (token->keyword)
648 case RID_STATIC:
649 case RID_EXTERN:
650 case RID_REGISTER:
651 case RID_TYPEDEF:
652 case RID_INLINE:
653 case RID_NORETURN:
654 case RID_AUTO:
655 case RID_THREAD:
656 case RID_UNSIGNED:
657 case RID_LONG:
658 case RID_SHORT:
659 case RID_SIGNED:
660 case RID_COMPLEX:
661 case RID_INT:
662 case RID_CHAR:
663 case RID_FLOAT:
664 case RID_DOUBLE:
665 case RID_VOID:
666 case RID_DFLOAT32:
667 case RID_DFLOAT64:
668 case RID_DFLOAT128:
669 CASE_RID_FLOATN_NX:
670 case RID_BOOL:
671 case RID_ENUM:
672 case RID_STRUCT:
673 case RID_UNION:
674 case RID_TYPEOF:
675 case RID_CONST:
676 case RID_VOLATILE:
677 case RID_RESTRICT:
678 case RID_ATTRIBUTE:
679 case RID_FRACT:
680 case RID_ACCUM:
681 case RID_SAT:
682 case RID_ALIGNAS:
683 case RID_ATOMIC:
684 case RID_AUTO_TYPE:
685 return true;
686 default:
687 if (token->keyword >= RID_FIRST_INT_N
688 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
689 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
690 return true;
691 return false;
693 case CPP_LESS:
694 if (c_dialect_objc ())
695 return true;
696 return false;
697 default:
698 return false;
703 /* Return true if TOKEN can start declaration specifiers or a static
704 assertion, false otherwise. */
705 static bool
706 c_token_starts_declaration (c_token *token)
708 if (c_token_starts_declspecs (token)
709 || token->keyword == RID_STATIC_ASSERT)
710 return true;
711 else
712 return false;
715 /* Return true if the next token from PARSER can start declaration
716 specifiers, false otherwise. */
717 bool
718 c_parser_next_token_starts_declspecs (c_parser *parser)
720 c_token *token = c_parser_peek_token (parser);
722 /* In Objective-C, a classname normally starts a declspecs unless it
723 is immediately followed by a dot. In that case, it is the
724 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
725 setter/getter on the class. c_token_starts_declspecs() can't
726 differentiate between the two cases because it only checks the
727 current token, so we have a special check here. */
728 if (c_dialect_objc ()
729 && token->type == CPP_NAME
730 && token->id_kind == C_ID_CLASSNAME
731 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
732 return false;
734 return c_token_starts_declspecs (token);
737 /* Return true if the next tokens from PARSER can start declaration
738 specifiers or a static assertion, false otherwise. */
739 bool
740 c_parser_next_tokens_start_declaration (c_parser *parser)
742 c_token *token = c_parser_peek_token (parser);
744 /* Same as above. */
745 if (c_dialect_objc ()
746 && token->type == CPP_NAME
747 && token->id_kind == C_ID_CLASSNAME
748 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
749 return false;
751 /* Labels do not start declarations. */
752 if (token->type == CPP_NAME
753 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
754 return false;
756 if (c_token_starts_declaration (token))
757 return true;
759 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
760 return true;
762 return false;
765 /* Consume the next token from PARSER. */
767 void
768 c_parser_consume_token (c_parser *parser)
770 gcc_assert (parser->tokens_avail >= 1);
771 gcc_assert (parser->tokens[0].type != CPP_EOF);
772 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
773 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
774 parser->last_token_location = parser->tokens[0].location;
775 if (parser->tokens != &parser->tokens_buf[0])
776 parser->tokens++;
777 else if (parser->tokens_avail == 2)
778 parser->tokens[0] = parser->tokens[1];
779 parser->tokens_avail--;
782 /* Expect the current token to be a #pragma. Consume it and remember
783 that we've begun parsing a pragma. */
785 static void
786 c_parser_consume_pragma (c_parser *parser)
788 gcc_assert (!parser->in_pragma);
789 gcc_assert (parser->tokens_avail >= 1);
790 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
791 if (parser->tokens != &parser->tokens_buf[0])
792 parser->tokens++;
793 else if (parser->tokens_avail == 2)
794 parser->tokens[0] = parser->tokens[1];
795 parser->tokens_avail--;
796 parser->in_pragma = true;
799 /* Update the global input_location from TOKEN. */
800 static inline void
801 c_parser_set_source_position_from_token (c_token *token)
803 if (token->type != CPP_EOF)
805 input_location = token->location;
809 /* Helper function for c_parser_error.
810 Having peeked a token of kind TOK1_KIND that might signify
811 a conflict marker, peek successor tokens to determine
812 if we actually do have a conflict marker.
813 Specifically, we consider a run of 7 '<', '=' or '>' characters
814 at the start of a line as a conflict marker.
815 These come through the lexer as three pairs and a single,
816 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
817 If it returns true, *OUT_LOC is written to with the location/range
818 of the marker. */
820 static bool
821 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
822 location_t *out_loc)
824 c_token *token2 = c_parser_peek_2nd_token (parser);
825 if (token2->type != tok1_kind)
826 return false;
827 c_token *token3 = c_parser_peek_nth_token (parser, 3);
828 if (token3->type != tok1_kind)
829 return false;
830 c_token *token4 = c_parser_peek_nth_token (parser, 4);
831 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
832 return false;
834 /* It must be at the start of the line. */
835 location_t start_loc = c_parser_peek_token (parser)->location;
836 if (LOCATION_COLUMN (start_loc) != 1)
837 return false;
839 /* We have a conflict marker. Construct a location of the form:
840 <<<<<<<
841 ^~~~~~~
842 with start == caret, finishing at the end of the marker. */
843 location_t finish_loc = get_finish (token4->location);
844 *out_loc = make_location (start_loc, start_loc, finish_loc);
846 return true;
849 /* Issue a diagnostic of the form
850 FILE:LINE: MESSAGE before TOKEN
851 where TOKEN is the next token in the input stream of PARSER.
852 MESSAGE (specified by the caller) is usually of the form "expected
853 OTHER-TOKEN".
855 Use RICHLOC as the location of the diagnostic.
857 Do not issue a diagnostic if still recovering from an error.
859 Return true iff an error was actually emitted.
861 ??? This is taken from the C++ parser, but building up messages in
862 this way is not i18n-friendly and some other approach should be
863 used. */
865 static bool
866 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
867 rich_location *richloc)
869 c_token *token = c_parser_peek_token (parser);
870 if (parser->error)
871 return false;
872 parser->error = true;
873 if (!gmsgid)
874 return false;
876 /* If this is actually a conflict marker, report it as such. */
877 if (token->type == CPP_LSHIFT
878 || token->type == CPP_RSHIFT
879 || token->type == CPP_EQ_EQ)
881 location_t loc;
882 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
884 error_at (loc, "version control conflict marker in file");
885 return true;
889 c_parse_error (gmsgid,
890 /* Because c_parse_error does not understand
891 CPP_KEYWORD, keywords are treated like
892 identifiers. */
893 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
894 /* ??? The C parser does not save the cpp flags of a
895 token, we need to pass 0 here and we will not get
896 the source spelling of some tokens but rather the
897 canonical spelling. */
898 token->value, /*flags=*/0, richloc);
899 return true;
902 /* As c_parser_error_richloc, but issue the message at the
903 location of PARSER's next token, or at input_location
904 if the next token is EOF. */
906 bool
907 c_parser_error (c_parser *parser, const char *gmsgid)
909 c_token *token = c_parser_peek_token (parser);
910 c_parser_set_source_position_from_token (token);
911 rich_location richloc (line_table, input_location);
912 return c_parser_error_richloc (parser, gmsgid, &richloc);
915 /* Some tokens naturally come in pairs e.g.'(' and ')'.
916 This class is for tracking such a matching pair of symbols.
917 In particular, it tracks the location of the first token,
918 so that if the second token is missing, we can highlight the
919 location of the first token when notifying the user about the
920 problem. */
922 template <typename traits_t>
923 class token_pair
925 public:
926 /* token_pair's ctor. */
927 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
929 /* If the next token is the opening symbol for this pair, consume it and
930 return true.
931 Otherwise, issue an error and return false.
932 In either case, record the location of the opening token. */
934 bool require_open (c_parser *parser)
936 c_token *token = c_parser_peek_token (parser);
937 if (token)
938 m_open_loc = token->location;
940 return c_parser_require (parser, traits_t::open_token_type,
941 traits_t::open_gmsgid);
944 /* Consume the next token from PARSER, recording its location as
945 that of the opening token within the pair. */
947 void consume_open (c_parser *parser)
949 c_token *token = c_parser_peek_token (parser);
950 gcc_assert (token->type == traits_t::open_token_type);
951 m_open_loc = token->location;
952 c_parser_consume_token (parser);
955 /* If the next token is the closing symbol for this pair, consume it
956 and return true.
957 Otherwise, issue an error, highlighting the location of the
958 corresponding opening token, and return false. */
960 bool require_close (c_parser *parser) const
962 return c_parser_require (parser, traits_t::close_token_type,
963 traits_t::close_gmsgid, m_open_loc);
966 /* Like token_pair::require_close, except that tokens will be skipped
967 until the desired token is found. An error message is still produced
968 if the next token is not as expected. */
970 void skip_until_found_close (c_parser *parser) const
972 c_parser_skip_until_found (parser, traits_t::close_token_type,
973 traits_t::close_gmsgid, m_open_loc);
976 private:
977 location_t m_open_loc;
980 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
982 struct matching_paren_traits
984 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
985 static const char * const open_gmsgid;
986 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
987 static const char * const close_gmsgid;
990 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
991 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
993 /* "matching_parens" is a token_pair<T> class for tracking matching
994 pairs of parentheses. */
996 typedef token_pair<matching_paren_traits> matching_parens;
998 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1000 struct matching_brace_traits
1002 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1003 static const char * const open_gmsgid;
1004 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1005 static const char * const close_gmsgid;
1008 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1009 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1011 /* "matching_braces" is a token_pair<T> class for tracking matching
1012 pairs of braces. */
1014 typedef token_pair<matching_brace_traits> matching_braces;
1016 /* Get a description of the matching symbol to TYPE e.g. "(" for
1017 CPP_CLOSE_PAREN. */
1019 static const char *
1020 get_matching_symbol (enum cpp_ttype type)
1022 switch (type)
1024 default:
1025 gcc_unreachable ();
1026 return "";
1027 case CPP_CLOSE_PAREN:
1028 return "(";
1029 case CPP_CLOSE_BRACE:
1030 return "{";
1034 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1035 issue the error MSGID. If MSGID is NULL then a message has already
1036 been produced and no message will be produced this time. Returns
1037 true if found, false otherwise.
1039 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1040 within any error as the location of an "opening" token matching
1041 the close token TYPE (e.g. the location of the '(' when TYPE is
1042 CPP_CLOSE_PAREN).
1044 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1045 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1046 attempt to generate a fix-it hint for the problem.
1047 Otherwise msgid describes multiple token types (e.g.
1048 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1049 generate a fix-it hint. */
1051 bool
1052 c_parser_require (c_parser *parser,
1053 enum cpp_ttype type,
1054 const char *msgid,
1055 location_t matching_location,
1056 bool type_is_unique)
1058 if (c_parser_next_token_is (parser, type))
1060 c_parser_consume_token (parser);
1061 return true;
1063 else
1065 location_t next_token_loc = c_parser_peek_token (parser)->location;
1066 gcc_rich_location richloc (next_token_loc);
1068 /* Potentially supply a fix-it hint, suggesting to add the
1069 missing token immediately after the *previous* token.
1070 This may move the primary location within richloc. */
1071 if (!parser->error && type_is_unique)
1072 maybe_suggest_missing_token_insertion (&richloc, type,
1073 parser->last_token_location);
1075 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1076 Attempt to consolidate diagnostics by printing it as a
1077 secondary range within the main diagnostic. */
1078 bool added_matching_location = false;
1079 if (matching_location != UNKNOWN_LOCATION)
1080 added_matching_location
1081 = richloc.add_location_if_nearby (matching_location);
1083 if (c_parser_error_richloc (parser, msgid, &richloc))
1084 /* If we weren't able to consolidate matching_location, then
1085 print it as a secondary diagnostic. */
1086 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1087 inform (matching_location, "to match this %qs",
1088 get_matching_symbol (type));
1090 return false;
1094 /* If the next token is the indicated keyword, consume it. Otherwise,
1095 issue the error MSGID. Returns true if found, false otherwise. */
1097 static bool
1098 c_parser_require_keyword (c_parser *parser,
1099 enum rid keyword,
1100 const char *msgid)
1102 if (c_parser_next_token_is_keyword (parser, keyword))
1104 c_parser_consume_token (parser);
1105 return true;
1107 else
1109 c_parser_error (parser, msgid);
1110 return false;
1114 /* Like c_parser_require, except that tokens will be skipped until the
1115 desired token is found. An error message is still produced if the
1116 next token is not as expected. If MSGID is NULL then a message has
1117 already been produced and no message will be produced this
1118 time.
1120 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1121 within any error as the location of an "opening" token matching
1122 the close token TYPE (e.g. the location of the '(' when TYPE is
1123 CPP_CLOSE_PAREN). */
1125 void
1126 c_parser_skip_until_found (c_parser *parser,
1127 enum cpp_ttype type,
1128 const char *msgid,
1129 location_t matching_location)
1131 unsigned nesting_depth = 0;
1133 if (c_parser_require (parser, type, msgid, matching_location))
1134 return;
1136 /* Skip tokens until the desired token is found. */
1137 while (true)
1139 /* Peek at the next token. */
1140 c_token *token = c_parser_peek_token (parser);
1141 /* If we've reached the token we want, consume it and stop. */
1142 if (token->type == type && !nesting_depth)
1144 c_parser_consume_token (parser);
1145 break;
1148 /* If we've run out of tokens, stop. */
1149 if (token->type == CPP_EOF)
1150 return;
1151 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1152 return;
1153 if (token->type == CPP_OPEN_BRACE
1154 || token->type == CPP_OPEN_PAREN
1155 || token->type == CPP_OPEN_SQUARE)
1156 ++nesting_depth;
1157 else if (token->type == CPP_CLOSE_BRACE
1158 || token->type == CPP_CLOSE_PAREN
1159 || token->type == CPP_CLOSE_SQUARE)
1161 if (nesting_depth-- == 0)
1162 break;
1164 /* Consume this token. */
1165 c_parser_consume_token (parser);
1167 parser->error = false;
1170 /* Skip tokens until the end of a parameter is found, but do not
1171 consume the comma, semicolon or closing delimiter. */
1173 static void
1174 c_parser_skip_to_end_of_parameter (c_parser *parser)
1176 unsigned nesting_depth = 0;
1178 while (true)
1180 c_token *token = c_parser_peek_token (parser);
1181 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1182 && !nesting_depth)
1183 break;
1184 /* If we've run out of tokens, stop. */
1185 if (token->type == CPP_EOF)
1186 return;
1187 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1188 return;
1189 if (token->type == CPP_OPEN_BRACE
1190 || token->type == CPP_OPEN_PAREN
1191 || token->type == CPP_OPEN_SQUARE)
1192 ++nesting_depth;
1193 else if (token->type == CPP_CLOSE_BRACE
1194 || token->type == CPP_CLOSE_PAREN
1195 || token->type == CPP_CLOSE_SQUARE)
1197 if (nesting_depth-- == 0)
1198 break;
1200 /* Consume this token. */
1201 c_parser_consume_token (parser);
1203 parser->error = false;
1206 /* Expect to be at the end of the pragma directive and consume an
1207 end of line marker. */
1209 static void
1210 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1212 gcc_assert (parser->in_pragma);
1213 parser->in_pragma = false;
1215 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1216 c_parser_error (parser, "expected end of line");
1218 cpp_ttype token_type;
1221 c_token *token = c_parser_peek_token (parser);
1222 token_type = token->type;
1223 if (token_type == CPP_EOF)
1224 break;
1225 c_parser_consume_token (parser);
1227 while (token_type != CPP_PRAGMA_EOL);
1229 parser->error = false;
1232 /* Skip tokens until we have consumed an entire block, or until we
1233 have consumed a non-nested ';'. */
1235 static void
1236 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1238 unsigned nesting_depth = 0;
1239 bool save_error = parser->error;
1241 while (true)
1243 c_token *token;
1245 /* Peek at the next token. */
1246 token = c_parser_peek_token (parser);
1248 switch (token->type)
1250 case CPP_EOF:
1251 return;
1253 case CPP_PRAGMA_EOL:
1254 if (parser->in_pragma)
1255 return;
1256 break;
1258 case CPP_SEMICOLON:
1259 /* If the next token is a ';', we have reached the
1260 end of the statement. */
1261 if (!nesting_depth)
1263 /* Consume the ';'. */
1264 c_parser_consume_token (parser);
1265 goto finished;
1267 break;
1269 case CPP_CLOSE_BRACE:
1270 /* If the next token is a non-nested '}', then we have
1271 reached the end of the current block. */
1272 if (nesting_depth == 0 || --nesting_depth == 0)
1274 c_parser_consume_token (parser);
1275 goto finished;
1277 break;
1279 case CPP_OPEN_BRACE:
1280 /* If it the next token is a '{', then we are entering a new
1281 block. Consume the entire block. */
1282 ++nesting_depth;
1283 break;
1285 case CPP_PRAGMA:
1286 /* If we see a pragma, consume the whole thing at once. We
1287 have some safeguards against consuming pragmas willy-nilly.
1288 Normally, we'd expect to be here with parser->error set,
1289 which disables these safeguards. But it's possible to get
1290 here for secondary error recovery, after parser->error has
1291 been cleared. */
1292 c_parser_consume_pragma (parser);
1293 c_parser_skip_to_pragma_eol (parser);
1294 parser->error = save_error;
1295 continue;
1297 default:
1298 break;
1301 c_parser_consume_token (parser);
1304 finished:
1305 parser->error = false;
1308 /* CPP's options (initialized by c-opts.c). */
1309 extern cpp_options *cpp_opts;
1311 /* Save the warning flags which are controlled by __extension__. */
1313 static inline int
1314 disable_extension_diagnostics (void)
1316 int ret = (pedantic
1317 | (warn_pointer_arith << 1)
1318 | (warn_traditional << 2)
1319 | (flag_iso << 3)
1320 | (warn_long_long << 4)
1321 | (warn_cxx_compat << 5)
1322 | (warn_overlength_strings << 6)
1323 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1324 play tricks to properly restore it. */
1325 | ((warn_c90_c99_compat == 1) << 7)
1326 | ((warn_c90_c99_compat == -1) << 8)
1327 /* Similarly for warn_c99_c11_compat. */
1328 | ((warn_c99_c11_compat == 1) << 9)
1329 | ((warn_c99_c11_compat == -1) << 10)
1331 cpp_opts->cpp_pedantic = pedantic = 0;
1332 warn_pointer_arith = 0;
1333 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1334 flag_iso = 0;
1335 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1336 warn_cxx_compat = 0;
1337 warn_overlength_strings = 0;
1338 warn_c90_c99_compat = 0;
1339 warn_c99_c11_compat = 0;
1340 return ret;
1343 /* Restore the warning flags which are controlled by __extension__.
1344 FLAGS is the return value from disable_extension_diagnostics. */
1346 static inline void
1347 restore_extension_diagnostics (int flags)
1349 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1350 warn_pointer_arith = (flags >> 1) & 1;
1351 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1352 flag_iso = (flags >> 3) & 1;
1353 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1354 warn_cxx_compat = (flags >> 5) & 1;
1355 warn_overlength_strings = (flags >> 6) & 1;
1356 /* See above for why is this needed. */
1357 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1358 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1361 /* Helper data structure for parsing #pragma acc routine. */
1362 struct oacc_routine_data {
1363 bool error_seen; /* Set if error has been reported. */
1364 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1365 tree clauses;
1366 location_t loc;
1369 static void c_parser_external_declaration (c_parser *);
1370 static void c_parser_asm_definition (c_parser *);
1371 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1372 bool, bool, tree *, vec<c_token>,
1373 struct oacc_routine_data * = NULL,
1374 bool * = NULL);
1375 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1376 static void c_parser_static_assert_declaration (c_parser *);
1377 static struct c_typespec c_parser_enum_specifier (c_parser *);
1378 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1379 static tree c_parser_struct_declaration (c_parser *);
1380 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1381 static tree c_parser_alignas_specifier (c_parser *);
1382 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1383 c_dtr_syn, bool *);
1384 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1385 bool,
1386 struct c_declarator *);
1387 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1388 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1389 tree);
1390 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1391 static tree c_parser_simple_asm_expr (c_parser *);
1392 static tree c_parser_attributes (c_parser *);
1393 static struct c_expr c_parser_initializer (c_parser *);
1394 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1395 struct obstack *);
1396 static void c_parser_initelt (c_parser *, struct obstack *);
1397 static void c_parser_initval (c_parser *, struct c_expr *,
1398 struct obstack *);
1399 static tree c_parser_compound_statement (c_parser *);
1400 static void c_parser_compound_statement_nostart (c_parser *);
1401 static void c_parser_label (c_parser *);
1402 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1403 static void c_parser_statement_after_labels (c_parser *, bool *,
1404 vec<tree> * = NULL);
1405 static tree c_parser_c99_block_statement (c_parser *, bool *,
1406 location_t * = NULL);
1407 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1408 static void c_parser_switch_statement (c_parser *, bool *);
1409 static void c_parser_while_statement (c_parser *, bool, bool *);
1410 static void c_parser_do_statement (c_parser *, bool);
1411 static void c_parser_for_statement (c_parser *, bool, bool *);
1412 static tree c_parser_asm_statement (c_parser *);
1413 static tree c_parser_asm_operands (c_parser *);
1414 static tree c_parser_asm_goto_operands (c_parser *);
1415 static tree c_parser_asm_clobbers (c_parser *);
1416 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1417 tree = NULL_TREE);
1418 static struct c_expr c_parser_conditional_expression (c_parser *,
1419 struct c_expr *, tree);
1420 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1421 tree);
1422 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1423 static struct c_expr c_parser_unary_expression (c_parser *);
1424 static struct c_expr c_parser_sizeof_expression (c_parser *);
1425 static struct c_expr c_parser_alignof_expression (c_parser *);
1426 static struct c_expr c_parser_postfix_expression (c_parser *);
1427 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1428 struct c_type_name *,
1429 location_t);
1430 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1431 location_t loc,
1432 struct c_expr);
1433 static tree c_parser_transaction (c_parser *, enum rid);
1434 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1435 static tree c_parser_transaction_cancel (c_parser *);
1436 static struct c_expr c_parser_expression (c_parser *);
1437 static struct c_expr c_parser_expression_conv (c_parser *);
1438 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1439 vec<tree, va_gc> **, location_t *,
1440 tree *, vec<location_t> *,
1441 unsigned int * = NULL);
1442 static void c_parser_oacc_declare (c_parser *);
1443 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1444 static void c_parser_oacc_update (c_parser *);
1445 static void c_parser_omp_construct (c_parser *, bool *);
1446 static void c_parser_omp_threadprivate (c_parser *);
1447 static void c_parser_omp_barrier (c_parser *);
1448 static void c_parser_omp_flush (c_parser *);
1449 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1450 tree, tree *, bool *);
1451 static void c_parser_omp_taskwait (c_parser *);
1452 static void c_parser_omp_taskyield (c_parser *);
1453 static void c_parser_omp_cancel (c_parser *);
1455 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1456 pragma_stmt, pragma_compound };
1457 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1458 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1459 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1460 static void c_parser_omp_end_declare_target (c_parser *);
1461 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1462 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1463 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1465 /* These Objective-C parser functions are only ever called when
1466 compiling Objective-C. */
1467 static void c_parser_objc_class_definition (c_parser *, tree);
1468 static void c_parser_objc_class_instance_variables (c_parser *);
1469 static void c_parser_objc_class_declaration (c_parser *);
1470 static void c_parser_objc_alias_declaration (c_parser *);
1471 static void c_parser_objc_protocol_definition (c_parser *, tree);
1472 static bool c_parser_objc_method_type (c_parser *);
1473 static void c_parser_objc_method_definition (c_parser *);
1474 static void c_parser_objc_methodprotolist (c_parser *);
1475 static void c_parser_objc_methodproto (c_parser *);
1476 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1477 static tree c_parser_objc_type_name (c_parser *);
1478 static tree c_parser_objc_protocol_refs (c_parser *);
1479 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1480 static void c_parser_objc_synchronized_statement (c_parser *);
1481 static tree c_parser_objc_selector (c_parser *);
1482 static tree c_parser_objc_selector_arg (c_parser *);
1483 static tree c_parser_objc_receiver (c_parser *);
1484 static tree c_parser_objc_message_args (c_parser *);
1485 static tree c_parser_objc_keywordexpr (c_parser *);
1486 static void c_parser_objc_at_property_declaration (c_parser *);
1487 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1488 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1489 static bool c_parser_objc_diagnose_bad_element_prefix
1490 (c_parser *, struct c_declspecs *);
1492 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1494 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1496 translation-unit:
1497 external-declarations
1499 external-declarations:
1500 external-declaration
1501 external-declarations external-declaration
1503 GNU extensions:
1505 translation-unit:
1506 empty
1509 static void
1510 c_parser_translation_unit (c_parser *parser)
1512 if (c_parser_next_token_is (parser, CPP_EOF))
1514 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1515 "ISO C forbids an empty translation unit");
1517 else
1519 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1520 mark_valid_location_for_stdc_pragma (false);
1523 ggc_collect ();
1524 c_parser_external_declaration (parser);
1525 obstack_free (&parser_obstack, obstack_position);
1527 while (c_parser_next_token_is_not (parser, CPP_EOF));
1530 unsigned int i;
1531 tree decl;
1532 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1533 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1534 error ("storage size of %q+D isn%'t known", decl);
1537 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1539 external-declaration:
1540 function-definition
1541 declaration
1543 GNU extensions:
1545 external-declaration:
1546 asm-definition
1548 __extension__ external-declaration
1550 Objective-C:
1552 external-declaration:
1553 objc-class-definition
1554 objc-class-declaration
1555 objc-alias-declaration
1556 objc-protocol-definition
1557 objc-method-definition
1558 @end
1561 static void
1562 c_parser_external_declaration (c_parser *parser)
1564 int ext;
1565 switch (c_parser_peek_token (parser)->type)
1567 case CPP_KEYWORD:
1568 switch (c_parser_peek_token (parser)->keyword)
1570 case RID_EXTENSION:
1571 ext = disable_extension_diagnostics ();
1572 c_parser_consume_token (parser);
1573 c_parser_external_declaration (parser);
1574 restore_extension_diagnostics (ext);
1575 break;
1576 case RID_ASM:
1577 c_parser_asm_definition (parser);
1578 break;
1579 case RID_AT_INTERFACE:
1580 case RID_AT_IMPLEMENTATION:
1581 gcc_assert (c_dialect_objc ());
1582 c_parser_objc_class_definition (parser, NULL_TREE);
1583 break;
1584 case RID_AT_CLASS:
1585 gcc_assert (c_dialect_objc ());
1586 c_parser_objc_class_declaration (parser);
1587 break;
1588 case RID_AT_ALIAS:
1589 gcc_assert (c_dialect_objc ());
1590 c_parser_objc_alias_declaration (parser);
1591 break;
1592 case RID_AT_PROTOCOL:
1593 gcc_assert (c_dialect_objc ());
1594 c_parser_objc_protocol_definition (parser, NULL_TREE);
1595 break;
1596 case RID_AT_PROPERTY:
1597 gcc_assert (c_dialect_objc ());
1598 c_parser_objc_at_property_declaration (parser);
1599 break;
1600 case RID_AT_SYNTHESIZE:
1601 gcc_assert (c_dialect_objc ());
1602 c_parser_objc_at_synthesize_declaration (parser);
1603 break;
1604 case RID_AT_DYNAMIC:
1605 gcc_assert (c_dialect_objc ());
1606 c_parser_objc_at_dynamic_declaration (parser);
1607 break;
1608 case RID_AT_END:
1609 gcc_assert (c_dialect_objc ());
1610 c_parser_consume_token (parser);
1611 objc_finish_implementation ();
1612 break;
1613 default:
1614 goto decl_or_fndef;
1616 break;
1617 case CPP_SEMICOLON:
1618 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1619 "ISO C does not allow extra %<;%> outside of a function");
1620 c_parser_consume_token (parser);
1621 break;
1622 case CPP_PRAGMA:
1623 mark_valid_location_for_stdc_pragma (true);
1624 c_parser_pragma (parser, pragma_external, NULL);
1625 mark_valid_location_for_stdc_pragma (false);
1626 break;
1627 case CPP_PLUS:
1628 case CPP_MINUS:
1629 if (c_dialect_objc ())
1631 c_parser_objc_method_definition (parser);
1632 break;
1634 /* Else fall through, and yield a syntax error trying to parse
1635 as a declaration or function definition. */
1636 /* FALLTHRU */
1637 default:
1638 decl_or_fndef:
1639 /* A declaration or a function definition (or, in Objective-C,
1640 an @interface or @protocol with prefix attributes). We can
1641 only tell which after parsing the declaration specifiers, if
1642 any, and the first declarator. */
1643 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1644 NULL, vNULL);
1645 break;
1649 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1650 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1652 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1653 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1654 is accepted; otherwise (old-style parameter declarations) only other
1655 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1656 assertion is accepted; otherwise (old-style parameter declarations)
1657 it is not. If NESTED is true, we are inside a function or parsing
1658 old-style parameter declarations; any functions encountered are
1659 nested functions and declaration specifiers are required; otherwise
1660 we are at top level and functions are normal functions and
1661 declaration specifiers may be optional. If EMPTY_OK is true, empty
1662 declarations are OK (subject to all other constraints); otherwise
1663 (old-style parameter declarations) they are diagnosed. If
1664 START_ATTR_OK is true, the declaration specifiers may start with
1665 attributes; otherwise they may not.
1666 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1667 declaration when parsing an Objective-C foreach statement.
1668 FALLTHRU_ATTR_P is used to signal whether this function parsed
1669 "__attribute__((fallthrough));".
1671 declaration:
1672 declaration-specifiers init-declarator-list[opt] ;
1673 static_assert-declaration
1675 function-definition:
1676 declaration-specifiers[opt] declarator declaration-list[opt]
1677 compound-statement
1679 declaration-list:
1680 declaration
1681 declaration-list declaration
1683 init-declarator-list:
1684 init-declarator
1685 init-declarator-list , init-declarator
1687 init-declarator:
1688 declarator simple-asm-expr[opt] attributes[opt]
1689 declarator simple-asm-expr[opt] attributes[opt] = initializer
1691 GNU extensions:
1693 nested-function-definition:
1694 declaration-specifiers declarator declaration-list[opt]
1695 compound-statement
1697 attribute ;
1699 Objective-C:
1700 attributes objc-class-definition
1701 attributes objc-category-definition
1702 attributes objc-protocol-definition
1704 The simple-asm-expr and attributes are GNU extensions.
1706 This function does not handle __extension__; that is handled in its
1707 callers. ??? Following the old parser, __extension__ may start
1708 external declarations, declarations in functions and declarations
1709 at the start of "for" loops, but not old-style parameter
1710 declarations.
1712 C99 requires declaration specifiers in a function definition; the
1713 absence is diagnosed through the diagnosis of implicit int. In GNU
1714 C we also allow but diagnose declarations without declaration
1715 specifiers, but only at top level (elsewhere they conflict with
1716 other syntax).
1718 In Objective-C, declarations of the looping variable in a foreach
1719 statement are exceptionally terminated by 'in' (for example, 'for
1720 (NSObject *object in array) { ... }').
1722 OpenMP:
1724 declaration:
1725 threadprivate-directive
1727 GIMPLE:
1729 gimple-function-definition:
1730 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1731 declaration-list[opt] compound-statement
1733 rtl-function-definition:
1734 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1735 declaration-list[opt] compound-statement */
1737 static void
1738 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1739 bool static_assert_ok, bool empty_ok,
1740 bool nested, bool start_attr_ok,
1741 tree *objc_foreach_object_declaration,
1742 vec<c_token> omp_declare_simd_clauses,
1743 struct oacc_routine_data *oacc_routine_data,
1744 bool *fallthru_attr_p)
1746 struct c_declspecs *specs;
1747 tree prefix_attrs;
1748 tree all_prefix_attrs;
1749 bool diagnosed_no_specs = false;
1750 location_t here = c_parser_peek_token (parser)->location;
1752 if (static_assert_ok
1753 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1755 c_parser_static_assert_declaration (parser);
1756 return;
1758 specs = build_null_declspecs ();
1760 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1761 if (c_parser_peek_token (parser)->type == CPP_NAME
1762 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1763 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1764 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1765 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1767 tree name = c_parser_peek_token (parser)->value;
1769 /* Issue a warning about NAME being an unknown type name, perhaps
1770 with some kind of hint.
1771 If the user forgot a "struct" etc, suggest inserting
1772 it. Otherwise, attempt to look for misspellings. */
1773 gcc_rich_location richloc (here);
1774 if (tag_exists_p (RECORD_TYPE, name))
1776 /* This is not C++ with its implicit typedef. */
1777 richloc.add_fixit_insert_before ("struct ");
1778 error_at (&richloc,
1779 "unknown type name %qE;"
1780 " use %<struct%> keyword to refer to the type",
1781 name);
1783 else if (tag_exists_p (UNION_TYPE, name))
1785 richloc.add_fixit_insert_before ("union ");
1786 error_at (&richloc,
1787 "unknown type name %qE;"
1788 " use %<union%> keyword to refer to the type",
1789 name);
1791 else if (tag_exists_p (ENUMERAL_TYPE, name))
1793 richloc.add_fixit_insert_before ("enum ");
1794 error_at (&richloc,
1795 "unknown type name %qE;"
1796 " use %<enum%> keyword to refer to the type",
1797 name);
1799 else
1801 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1802 here);
1803 if (hint)
1805 richloc.add_fixit_replace (hint.suggestion ());
1806 error_at (&richloc,
1807 "unknown type name %qE; did you mean %qs?",
1808 name, hint.suggestion ());
1810 else
1811 error_at (here, "unknown type name %qE", name);
1814 /* Parse declspecs normally to get a correct pointer type, but avoid
1815 a further "fails to be a type name" error. Refuse nested functions
1816 since it is not how the user likely wants us to recover. */
1817 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1818 c_parser_peek_token (parser)->keyword = RID_VOID;
1819 c_parser_peek_token (parser)->value = error_mark_node;
1820 fndef_ok = !nested;
1823 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1824 true, true, cla_nonabstract_decl);
1825 if (parser->error)
1827 c_parser_skip_to_end_of_block_or_statement (parser);
1828 return;
1830 if (nested && !specs->declspecs_seen_p)
1832 c_parser_error (parser, "expected declaration specifiers");
1833 c_parser_skip_to_end_of_block_or_statement (parser);
1834 return;
1837 finish_declspecs (specs);
1838 bool auto_type_p = specs->typespec_word == cts_auto_type;
1839 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1841 if (auto_type_p)
1842 error_at (here, "%<__auto_type%> in empty declaration");
1843 else if (specs->typespec_kind == ctsk_none
1844 && attribute_fallthrough_p (specs->attrs))
1846 if (fallthru_attr_p != NULL)
1847 *fallthru_attr_p = true;
1848 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1849 void_type_node, 0);
1850 add_stmt (fn);
1852 else if (empty_ok)
1853 shadow_tag (specs);
1854 else
1856 shadow_tag_warned (specs, 1);
1857 pedwarn (here, 0, "empty declaration");
1859 c_parser_consume_token (parser);
1860 if (oacc_routine_data)
1861 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1862 return;
1865 /* Provide better error recovery. Note that a type name here is usually
1866 better diagnosed as a redeclaration. */
1867 if (empty_ok
1868 && specs->typespec_kind == ctsk_tagdef
1869 && c_parser_next_token_starts_declspecs (parser)
1870 && !c_parser_next_token_is (parser, CPP_NAME))
1872 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1873 parser->error = false;
1874 shadow_tag_warned (specs, 1);
1875 return;
1877 else if (c_dialect_objc () && !auto_type_p)
1879 /* Prefix attributes are an error on method decls. */
1880 switch (c_parser_peek_token (parser)->type)
1882 case CPP_PLUS:
1883 case CPP_MINUS:
1884 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1885 return;
1886 if (specs->attrs)
1888 warning_at (c_parser_peek_token (parser)->location,
1889 OPT_Wattributes,
1890 "prefix attributes are ignored for methods");
1891 specs->attrs = NULL_TREE;
1893 if (fndef_ok)
1894 c_parser_objc_method_definition (parser);
1895 else
1896 c_parser_objc_methodproto (parser);
1897 return;
1898 break;
1899 default:
1900 break;
1902 /* This is where we parse 'attributes @interface ...',
1903 'attributes @implementation ...', 'attributes @protocol ...'
1904 (where attributes could be, for example, __attribute__
1905 ((deprecated)).
1907 switch (c_parser_peek_token (parser)->keyword)
1909 case RID_AT_INTERFACE:
1911 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1912 return;
1913 c_parser_objc_class_definition (parser, specs->attrs);
1914 return;
1916 break;
1917 case RID_AT_IMPLEMENTATION:
1919 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1920 return;
1921 if (specs->attrs)
1923 warning_at (c_parser_peek_token (parser)->location,
1924 OPT_Wattributes,
1925 "prefix attributes are ignored for implementations");
1926 specs->attrs = NULL_TREE;
1928 c_parser_objc_class_definition (parser, NULL_TREE);
1929 return;
1931 break;
1932 case RID_AT_PROTOCOL:
1934 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1935 return;
1936 c_parser_objc_protocol_definition (parser, specs->attrs);
1937 return;
1939 break;
1940 case RID_AT_ALIAS:
1941 case RID_AT_CLASS:
1942 case RID_AT_END:
1943 case RID_AT_PROPERTY:
1944 if (specs->attrs)
1946 c_parser_error (parser, "unexpected attribute");
1947 specs->attrs = NULL;
1949 break;
1950 default:
1951 break;
1954 else if (attribute_fallthrough_p (specs->attrs))
1955 warning_at (here, OPT_Wattributes,
1956 "%<fallthrough%> attribute not followed by %<;%>");
1958 pending_xref_error ();
1959 prefix_attrs = specs->attrs;
1960 all_prefix_attrs = prefix_attrs;
1961 specs->attrs = NULL_TREE;
1962 while (true)
1964 struct c_declarator *declarator;
1965 bool dummy = false;
1966 timevar_id_t tv;
1967 tree fnbody = NULL_TREE;
1968 /* Declaring either one or more declarators (in which case we
1969 should diagnose if there were no declaration specifiers) or a
1970 function definition (in which case the diagnostic for
1971 implicit int suffices). */
1972 declarator = c_parser_declarator (parser,
1973 specs->typespec_kind != ctsk_none,
1974 C_DTR_NORMAL, &dummy);
1975 if (declarator == NULL)
1977 if (omp_declare_simd_clauses.exists ())
1978 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1979 omp_declare_simd_clauses);
1980 if (oacc_routine_data)
1981 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1982 c_parser_skip_to_end_of_block_or_statement (parser);
1983 return;
1985 if (auto_type_p && declarator->kind != cdk_id)
1987 error_at (here,
1988 "%<__auto_type%> requires a plain identifier"
1989 " as declarator");
1990 c_parser_skip_to_end_of_block_or_statement (parser);
1991 return;
1993 if (c_parser_next_token_is (parser, CPP_EQ)
1994 || c_parser_next_token_is (parser, CPP_COMMA)
1995 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1996 || c_parser_next_token_is_keyword (parser, RID_ASM)
1997 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1998 || c_parser_next_token_is_keyword (parser, RID_IN))
2000 tree asm_name = NULL_TREE;
2001 tree postfix_attrs = NULL_TREE;
2002 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2004 diagnosed_no_specs = true;
2005 pedwarn (here, 0, "data definition has no type or storage class");
2007 /* Having seen a data definition, there cannot now be a
2008 function definition. */
2009 fndef_ok = false;
2010 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2011 asm_name = c_parser_simple_asm_expr (parser);
2012 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2014 postfix_attrs = c_parser_attributes (parser);
2015 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2017 /* This means there is an attribute specifier after
2018 the declarator in a function definition. Provide
2019 some more information for the user. */
2020 error_at (here, "attributes should be specified before the "
2021 "declarator in a function definition");
2022 c_parser_skip_to_end_of_block_or_statement (parser);
2023 return;
2026 if (c_parser_next_token_is (parser, CPP_EQ))
2028 tree d;
2029 struct c_expr init;
2030 location_t init_loc;
2031 c_parser_consume_token (parser);
2032 if (auto_type_p)
2034 init_loc = c_parser_peek_token (parser)->location;
2035 rich_location richloc (line_table, init_loc);
2036 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2037 /* A parameter is initialized, which is invalid. Don't
2038 attempt to instrument the initializer. */
2039 int flag_sanitize_save = flag_sanitize;
2040 if (nested && !empty_ok)
2041 flag_sanitize = 0;
2042 init = c_parser_expr_no_commas (parser, NULL);
2043 flag_sanitize = flag_sanitize_save;
2044 if (TREE_CODE (init.value) == COMPONENT_REF
2045 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2046 error_at (here,
2047 "%<__auto_type%> used with a bit-field"
2048 " initializer");
2049 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2050 tree init_type = TREE_TYPE (init.value);
2051 /* As with typeof, remove all qualifiers from atomic types. */
2052 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2053 init_type
2054 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2055 bool vm_type = variably_modified_type_p (init_type,
2056 NULL_TREE);
2057 if (vm_type)
2058 init.value = save_expr (init.value);
2059 finish_init ();
2060 specs->typespec_kind = ctsk_typeof;
2061 specs->locations[cdw_typedef] = init_loc;
2062 specs->typedef_p = true;
2063 specs->type = init_type;
2064 if (vm_type)
2066 bool maybe_const = true;
2067 tree type_expr = c_fully_fold (init.value, false,
2068 &maybe_const);
2069 specs->expr_const_operands &= maybe_const;
2070 if (specs->expr)
2071 specs->expr = build2 (COMPOUND_EXPR,
2072 TREE_TYPE (type_expr),
2073 specs->expr, type_expr);
2074 else
2075 specs->expr = type_expr;
2077 d = start_decl (declarator, specs, true,
2078 chainon (postfix_attrs, all_prefix_attrs));
2079 if (!d)
2080 d = error_mark_node;
2081 if (omp_declare_simd_clauses.exists ())
2082 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2083 omp_declare_simd_clauses);
2085 else
2087 /* The declaration of the variable is in effect while
2088 its initializer is parsed. */
2089 d = start_decl (declarator, specs, true,
2090 chainon (postfix_attrs, all_prefix_attrs));
2091 if (!d)
2092 d = error_mark_node;
2093 if (omp_declare_simd_clauses.exists ())
2094 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2095 omp_declare_simd_clauses);
2096 init_loc = c_parser_peek_token (parser)->location;
2097 rich_location richloc (line_table, init_loc);
2098 start_init (d, asm_name, global_bindings_p (), &richloc);
2099 /* A parameter is initialized, which is invalid. Don't
2100 attempt to instrument the initializer. */
2101 int flag_sanitize_save = flag_sanitize;
2102 if (TREE_CODE (d) == PARM_DECL)
2103 flag_sanitize = 0;
2104 init = c_parser_initializer (parser);
2105 flag_sanitize = flag_sanitize_save;
2106 finish_init ();
2108 if (oacc_routine_data)
2109 c_finish_oacc_routine (oacc_routine_data, d, false);
2110 if (d != error_mark_node)
2112 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2113 finish_decl (d, init_loc, init.value,
2114 init.original_type, asm_name);
2117 else
2119 if (auto_type_p)
2121 error_at (here,
2122 "%<__auto_type%> requires an initialized "
2123 "data declaration");
2124 c_parser_skip_to_end_of_block_or_statement (parser);
2125 return;
2127 tree d = start_decl (declarator, specs, false,
2128 chainon (postfix_attrs,
2129 all_prefix_attrs));
2130 if (d && TREE_CODE (d) == FUNCTION_DECL)
2131 if (declarator->kind == cdk_function)
2132 if (DECL_ARGUMENTS (d) == NULL_TREE)
2133 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2134 if (omp_declare_simd_clauses.exists ())
2136 tree parms = NULL_TREE;
2137 if (d && TREE_CODE (d) == FUNCTION_DECL)
2139 struct c_declarator *ce = declarator;
2140 while (ce != NULL)
2141 if (ce->kind == cdk_function)
2143 parms = ce->u.arg_info->parms;
2144 break;
2146 else
2147 ce = ce->declarator;
2149 if (parms)
2150 temp_store_parm_decls (d, parms);
2151 c_finish_omp_declare_simd (parser, d, parms,
2152 omp_declare_simd_clauses);
2153 if (parms)
2154 temp_pop_parm_decls ();
2156 if (oacc_routine_data)
2157 c_finish_oacc_routine (oacc_routine_data, d, false);
2158 if (d)
2159 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2160 NULL_TREE, asm_name);
2162 if (c_parser_next_token_is_keyword (parser, RID_IN))
2164 if (d)
2165 *objc_foreach_object_declaration = d;
2166 else
2167 *objc_foreach_object_declaration = error_mark_node;
2170 if (c_parser_next_token_is (parser, CPP_COMMA))
2172 if (auto_type_p)
2174 error_at (here,
2175 "%<__auto_type%> may only be used with"
2176 " a single declarator");
2177 c_parser_skip_to_end_of_block_or_statement (parser);
2178 return;
2180 c_parser_consume_token (parser);
2181 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2182 all_prefix_attrs = chainon (c_parser_attributes (parser),
2183 prefix_attrs);
2184 else
2185 all_prefix_attrs = prefix_attrs;
2186 continue;
2188 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2190 c_parser_consume_token (parser);
2191 return;
2193 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2195 /* This can only happen in Objective-C: we found the
2196 'in' that terminates the declaration inside an
2197 Objective-C foreach statement. Do not consume the
2198 token, so that the caller can use it to determine
2199 that this indeed is a foreach context. */
2200 return;
2202 else
2204 c_parser_error (parser, "expected %<,%> or %<;%>");
2205 c_parser_skip_to_end_of_block_or_statement (parser);
2206 return;
2209 else if (auto_type_p)
2211 error_at (here,
2212 "%<__auto_type%> requires an initialized data declaration");
2213 c_parser_skip_to_end_of_block_or_statement (parser);
2214 return;
2216 else if (!fndef_ok)
2218 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2219 "%<asm%> or %<__attribute__%>");
2220 c_parser_skip_to_end_of_block_or_statement (parser);
2221 return;
2223 /* Function definition (nested or otherwise). */
2224 if (nested)
2226 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2227 c_push_function_context ();
2229 if (!start_function (specs, declarator, all_prefix_attrs))
2231 /* At this point we've consumed:
2232 declaration-specifiers declarator
2233 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2234 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2235 but the
2236 declaration-specifiers declarator
2237 aren't grokkable as a function definition, so we have
2238 an error. */
2239 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2240 if (c_parser_next_token_starts_declspecs (parser))
2242 /* If we have
2243 declaration-specifiers declarator decl-specs
2244 then assume we have a missing semicolon, which would
2245 give us:
2246 declaration-specifiers declarator decl-specs
2249 <~~~~~~~~~ declaration ~~~~~~~~~~>
2250 Use c_parser_require to get an error with a fix-it hint. */
2251 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2252 parser->error = false;
2254 else
2256 /* This can appear in many cases looking nothing like a
2257 function definition, so we don't give a more specific
2258 error suggesting there was one. */
2259 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2260 "or %<__attribute__%>");
2262 if (nested)
2263 c_pop_function_context ();
2264 break;
2267 if (DECL_DECLARED_INLINE_P (current_function_decl))
2268 tv = TV_PARSE_INLINE;
2269 else
2270 tv = TV_PARSE_FUNC;
2271 auto_timevar at (g_timer, tv);
2273 /* Parse old-style parameter declarations. ??? Attributes are
2274 not allowed to start declaration specifiers here because of a
2275 syntax conflict between a function declaration with attribute
2276 suffix and a function definition with an attribute prefix on
2277 first old-style parameter declaration. Following the old
2278 parser, they are not accepted on subsequent old-style
2279 parameter declarations either. However, there is no
2280 ambiguity after the first declaration, nor indeed on the
2281 first as long as we don't allow postfix attributes after a
2282 declarator with a nonempty identifier list in a definition;
2283 and postfix attributes have never been accepted here in
2284 function definitions either. */
2285 while (c_parser_next_token_is_not (parser, CPP_EOF)
2286 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2287 c_parser_declaration_or_fndef (parser, false, false, false,
2288 true, false, NULL, vNULL);
2289 store_parm_decls ();
2290 if (omp_declare_simd_clauses.exists ())
2291 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2292 omp_declare_simd_clauses);
2293 if (oacc_routine_data)
2294 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2295 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2296 = c_parser_peek_token (parser)->location;
2298 /* If the definition was marked with __GIMPLE then parse the
2299 function body as GIMPLE. */
2300 if (specs->gimple_p)
2302 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2303 bool saved = in_late_binary_op;
2304 in_late_binary_op = true;
2305 c_parser_parse_gimple_body (parser);
2306 in_late_binary_op = saved;
2308 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2309 consuming the function body. */
2310 else if (specs->rtl_p)
2312 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2314 /* Normally, store_parm_decls sets next_is_function_body,
2315 anticipating a function body. We need a push_scope/pop_scope
2316 pair to flush out this state, or subsequent function parsing
2317 will go wrong. */
2318 push_scope ();
2319 pop_scope ();
2321 finish_function ();
2322 return;
2324 else
2325 fnbody = c_parser_compound_statement (parser);
2326 tree fndecl = current_function_decl;
2327 if (nested)
2329 tree decl = current_function_decl;
2330 /* Mark nested functions as needing static-chain initially.
2331 lower_nested_functions will recompute it but the
2332 DECL_STATIC_CHAIN flag is also used before that happens,
2333 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2334 DECL_STATIC_CHAIN (decl) = 1;
2335 add_stmt (fnbody);
2336 finish_function ();
2337 c_pop_function_context ();
2338 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2340 else
2342 if (fnbody)
2343 add_stmt (fnbody);
2344 finish_function ();
2346 /* Get rid of the empty stmt list for GIMPLE. */
2347 if (specs->gimple_p)
2348 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2350 break;
2354 /* Parse an asm-definition (asm() outside a function body). This is a
2355 GNU extension.
2357 asm-definition:
2358 simple-asm-expr ;
2361 static void
2362 c_parser_asm_definition (c_parser *parser)
2364 tree asm_str = c_parser_simple_asm_expr (parser);
2365 if (asm_str)
2366 symtab->finalize_toplevel_asm (asm_str);
2367 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2370 /* Parse a static assertion (C11 6.7.10).
2372 static_assert-declaration:
2373 static_assert-declaration-no-semi ;
2376 static void
2377 c_parser_static_assert_declaration (c_parser *parser)
2379 c_parser_static_assert_declaration_no_semi (parser);
2380 if (parser->error
2381 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2382 c_parser_skip_to_end_of_block_or_statement (parser);
2385 /* Parse a static assertion (C11 6.7.10), without the trailing
2386 semicolon.
2388 static_assert-declaration-no-semi:
2389 _Static_assert ( constant-expression , string-literal )
2392 static void
2393 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2395 location_t assert_loc, value_loc;
2396 tree value;
2397 tree string;
2399 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2400 assert_loc = c_parser_peek_token (parser)->location;
2401 if (flag_isoc99)
2402 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2403 "ISO C99 does not support %<_Static_assert%>");
2404 else
2405 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2406 "ISO C90 does not support %<_Static_assert%>");
2407 c_parser_consume_token (parser);
2408 matching_parens parens;
2409 if (!parens.require_open (parser))
2410 return;
2411 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2412 value = c_parser_expr_no_commas (parser, NULL).value;
2413 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2414 parser->lex_untranslated_string = true;
2415 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2417 parser->lex_untranslated_string = false;
2418 return;
2420 switch (c_parser_peek_token (parser)->type)
2422 case CPP_STRING:
2423 case CPP_STRING16:
2424 case CPP_STRING32:
2425 case CPP_WSTRING:
2426 case CPP_UTF8STRING:
2427 string = c_parser_peek_token (parser)->value;
2428 c_parser_consume_token (parser);
2429 parser->lex_untranslated_string = false;
2430 break;
2431 default:
2432 c_parser_error (parser, "expected string literal");
2433 parser->lex_untranslated_string = false;
2434 return;
2436 parens.require_close (parser);
2438 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2440 error_at (value_loc, "expression in static assertion is not an integer");
2441 return;
2443 if (TREE_CODE (value) != INTEGER_CST)
2445 value = c_fully_fold (value, false, NULL);
2446 /* Strip no-op conversions. */
2447 STRIP_TYPE_NOPS (value);
2448 if (TREE_CODE (value) == INTEGER_CST)
2449 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2450 "is not an integer constant expression");
2452 if (TREE_CODE (value) != INTEGER_CST)
2454 error_at (value_loc, "expression in static assertion is not constant");
2455 return;
2457 constant_expression_warning (value);
2458 if (integer_zerop (value))
2459 error_at (assert_loc, "static assertion failed: %E", string);
2462 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2463 6.7, C11 6.7), adding them to SPECS (which may already include some).
2464 Storage class specifiers are accepted iff SCSPEC_OK; type
2465 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2466 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2467 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2469 declaration-specifiers:
2470 storage-class-specifier declaration-specifiers[opt]
2471 type-specifier declaration-specifiers[opt]
2472 type-qualifier declaration-specifiers[opt]
2473 function-specifier declaration-specifiers[opt]
2474 alignment-specifier declaration-specifiers[opt]
2476 Function specifiers (inline) are from C99, and are currently
2477 handled as storage class specifiers, as is __thread. Alignment
2478 specifiers are from C11.
2480 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2481 storage-class-specifier:
2482 typedef
2483 extern
2484 static
2485 auto
2486 register
2487 _Thread_local
2489 (_Thread_local is new in C11.)
2491 C99 6.7.4, C11 6.7.4:
2492 function-specifier:
2493 inline
2494 _Noreturn
2496 (_Noreturn is new in C11.)
2498 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2499 type-specifier:
2500 void
2501 char
2502 short
2504 long
2505 float
2506 double
2507 signed
2508 unsigned
2509 _Bool
2510 _Complex
2511 [_Imaginary removed in C99 TC2]
2512 struct-or-union-specifier
2513 enum-specifier
2514 typedef-name
2515 atomic-type-specifier
2517 (_Bool and _Complex are new in C99.)
2518 (atomic-type-specifier is new in C11.)
2520 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2522 type-qualifier:
2523 const
2524 restrict
2525 volatile
2526 address-space-qualifier
2527 _Atomic
2529 (restrict is new in C99.)
2530 (_Atomic is new in C11.)
2532 GNU extensions:
2534 declaration-specifiers:
2535 attributes declaration-specifiers[opt]
2537 type-qualifier:
2538 address-space
2540 address-space:
2541 identifier recognized by the target
2543 storage-class-specifier:
2544 __thread
2546 type-specifier:
2547 typeof-specifier
2548 __auto_type
2549 __intN
2550 _Decimal32
2551 _Decimal64
2552 _Decimal128
2553 _Fract
2554 _Accum
2555 _Sat
2557 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2558 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2560 atomic-type-specifier
2561 _Atomic ( type-name )
2563 Objective-C:
2565 type-specifier:
2566 class-name objc-protocol-refs[opt]
2567 typedef-name objc-protocol-refs
2568 objc-protocol-refs
2571 void
2572 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2573 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2574 bool alignspec_ok, bool auto_type_ok,
2575 enum c_lookahead_kind la)
2577 bool attrs_ok = start_attr_ok;
2578 bool seen_type = specs->typespec_kind != ctsk_none;
2580 if (!typespec_ok)
2581 gcc_assert (la == cla_prefer_id);
2583 while (c_parser_next_token_is (parser, CPP_NAME)
2584 || c_parser_next_token_is (parser, CPP_KEYWORD)
2585 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2587 struct c_typespec t;
2588 tree attrs;
2589 tree align;
2590 location_t loc = c_parser_peek_token (parser)->location;
2592 /* If we cannot accept a type, exit if the next token must start
2593 one. Also, if we already have seen a tagged definition,
2594 a typename would be an error anyway and likely the user
2595 has simply forgotten a semicolon, so we exit. */
2596 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2597 && c_parser_next_tokens_start_typename (parser, la)
2598 && !c_parser_next_token_is_qualifier (parser)
2599 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2600 break;
2602 if (c_parser_next_token_is (parser, CPP_NAME))
2604 c_token *name_token = c_parser_peek_token (parser);
2605 tree value = name_token->value;
2606 c_id_kind kind = name_token->id_kind;
2608 if (kind == C_ID_ADDRSPACE)
2610 addr_space_t as
2611 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2612 declspecs_add_addrspace (name_token->location, specs, as);
2613 c_parser_consume_token (parser);
2614 attrs_ok = true;
2615 continue;
2618 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2620 /* If we cannot accept a type, and the next token must start one,
2621 exit. Do the same if we already have seen a tagged definition,
2622 since it would be an error anyway and likely the user has simply
2623 forgotten a semicolon. */
2624 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2625 break;
2627 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2628 a C_ID_CLASSNAME. */
2629 c_parser_consume_token (parser);
2630 seen_type = true;
2631 attrs_ok = true;
2632 if (kind == C_ID_ID)
2634 error_at (loc, "unknown type name %qE", value);
2635 t.kind = ctsk_typedef;
2636 t.spec = error_mark_node;
2638 else if (kind == C_ID_TYPENAME
2639 && (!c_dialect_objc ()
2640 || c_parser_next_token_is_not (parser, CPP_LESS)))
2642 t.kind = ctsk_typedef;
2643 /* For a typedef name, record the meaning, not the name.
2644 In case of 'foo foo, bar;'. */
2645 t.spec = lookup_name (value);
2647 else
2649 tree proto = NULL_TREE;
2650 gcc_assert (c_dialect_objc ());
2651 t.kind = ctsk_objc;
2652 if (c_parser_next_token_is (parser, CPP_LESS))
2653 proto = c_parser_objc_protocol_refs (parser);
2654 t.spec = objc_get_protocol_qualified_type (value, proto);
2656 t.expr = NULL_TREE;
2657 t.expr_const_operands = true;
2658 declspecs_add_type (name_token->location, specs, t);
2659 continue;
2661 if (c_parser_next_token_is (parser, CPP_LESS))
2663 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2664 nisse@lysator.liu.se. */
2665 tree proto;
2666 gcc_assert (c_dialect_objc ());
2667 if (!typespec_ok || seen_type)
2668 break;
2669 proto = c_parser_objc_protocol_refs (parser);
2670 t.kind = ctsk_objc;
2671 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2672 t.expr = NULL_TREE;
2673 t.expr_const_operands = true;
2674 declspecs_add_type (loc, specs, t);
2675 continue;
2677 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2678 switch (c_parser_peek_token (parser)->keyword)
2680 case RID_STATIC:
2681 case RID_EXTERN:
2682 case RID_REGISTER:
2683 case RID_TYPEDEF:
2684 case RID_INLINE:
2685 case RID_NORETURN:
2686 case RID_AUTO:
2687 case RID_THREAD:
2688 if (!scspec_ok)
2689 goto out;
2690 attrs_ok = true;
2691 /* TODO: Distinguish between function specifiers (inline, noreturn)
2692 and storage class specifiers, either here or in
2693 declspecs_add_scspec. */
2694 declspecs_add_scspec (loc, specs,
2695 c_parser_peek_token (parser)->value);
2696 c_parser_consume_token (parser);
2697 break;
2698 case RID_AUTO_TYPE:
2699 if (!auto_type_ok)
2700 goto out;
2701 /* Fall through. */
2702 case RID_UNSIGNED:
2703 case RID_LONG:
2704 case RID_SHORT:
2705 case RID_SIGNED:
2706 case RID_COMPLEX:
2707 case RID_INT:
2708 case RID_CHAR:
2709 case RID_FLOAT:
2710 case RID_DOUBLE:
2711 case RID_VOID:
2712 case RID_DFLOAT32:
2713 case RID_DFLOAT64:
2714 case RID_DFLOAT128:
2715 CASE_RID_FLOATN_NX:
2716 case RID_BOOL:
2717 case RID_FRACT:
2718 case RID_ACCUM:
2719 case RID_SAT:
2720 case RID_INT_N_0:
2721 case RID_INT_N_1:
2722 case RID_INT_N_2:
2723 case RID_INT_N_3:
2724 if (!typespec_ok)
2725 goto out;
2726 attrs_ok = true;
2727 seen_type = true;
2728 if (c_dialect_objc ())
2729 parser->objc_need_raw_identifier = true;
2730 t.kind = ctsk_resword;
2731 t.spec = c_parser_peek_token (parser)->value;
2732 t.expr = NULL_TREE;
2733 t.expr_const_operands = true;
2734 declspecs_add_type (loc, specs, t);
2735 c_parser_consume_token (parser);
2736 break;
2737 case RID_ENUM:
2738 if (!typespec_ok)
2739 goto out;
2740 attrs_ok = true;
2741 seen_type = true;
2742 t = c_parser_enum_specifier (parser);
2743 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2744 declspecs_add_type (loc, specs, t);
2745 break;
2746 case RID_STRUCT:
2747 case RID_UNION:
2748 if (!typespec_ok)
2749 goto out;
2750 attrs_ok = true;
2751 seen_type = true;
2752 t = c_parser_struct_or_union_specifier (parser);
2753 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2754 declspecs_add_type (loc, specs, t);
2755 break;
2756 case RID_TYPEOF:
2757 /* ??? The old parser rejected typeof after other type
2758 specifiers, but is a syntax error the best way of
2759 handling this? */
2760 if (!typespec_ok || seen_type)
2761 goto out;
2762 attrs_ok = true;
2763 seen_type = true;
2764 t = c_parser_typeof_specifier (parser);
2765 declspecs_add_type (loc, specs, t);
2766 break;
2767 case RID_ATOMIC:
2768 /* C parser handling of Objective-C constructs needs
2769 checking for correct lvalue-to-rvalue conversions, and
2770 the code in build_modify_expr handling various
2771 Objective-C cases, and that in build_unary_op handling
2772 Objective-C cases for increment / decrement, also needs
2773 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2774 and objc_types_are_equivalent may also need updates. */
2775 if (c_dialect_objc ())
2776 sorry ("%<_Atomic%> in Objective-C");
2777 if (flag_isoc99)
2778 pedwarn_c99 (loc, OPT_Wpedantic,
2779 "ISO C99 does not support the %<_Atomic%> qualifier");
2780 else
2781 pedwarn_c99 (loc, OPT_Wpedantic,
2782 "ISO C90 does not support the %<_Atomic%> qualifier");
2783 attrs_ok = true;
2784 tree value;
2785 value = c_parser_peek_token (parser)->value;
2786 c_parser_consume_token (parser);
2787 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2789 /* _Atomic ( type-name ). */
2790 seen_type = true;
2791 c_parser_consume_token (parser);
2792 struct c_type_name *type = c_parser_type_name (parser);
2793 t.kind = ctsk_typeof;
2794 t.spec = error_mark_node;
2795 t.expr = NULL_TREE;
2796 t.expr_const_operands = true;
2797 if (type != NULL)
2798 t.spec = groktypename (type, &t.expr,
2799 &t.expr_const_operands);
2800 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2801 "expected %<)%>");
2802 if (t.spec != error_mark_node)
2804 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2805 error_at (loc, "%<_Atomic%>-qualified array type");
2806 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2807 error_at (loc, "%<_Atomic%>-qualified function type");
2808 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2809 error_at (loc, "%<_Atomic%> applied to a qualified type");
2810 else
2811 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2813 declspecs_add_type (loc, specs, t);
2815 else
2816 declspecs_add_qual (loc, specs, value);
2817 break;
2818 case RID_CONST:
2819 case RID_VOLATILE:
2820 case RID_RESTRICT:
2821 attrs_ok = true;
2822 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2823 c_parser_consume_token (parser);
2824 break;
2825 case RID_ATTRIBUTE:
2826 if (!attrs_ok)
2827 goto out;
2828 attrs = c_parser_attributes (parser);
2829 declspecs_add_attrs (loc, specs, attrs);
2830 break;
2831 case RID_ALIGNAS:
2832 if (!alignspec_ok)
2833 goto out;
2834 align = c_parser_alignas_specifier (parser);
2835 declspecs_add_alignas (loc, specs, align);
2836 break;
2837 case RID_GIMPLE:
2838 if (! flag_gimple)
2839 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2840 c_parser_consume_token (parser);
2841 specs->gimple_p = true;
2842 specs->locations[cdw_gimple] = loc;
2843 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2844 break;
2845 case RID_RTL:
2846 c_parser_consume_token (parser);
2847 specs->rtl_p = true;
2848 specs->locations[cdw_rtl] = loc;
2849 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2850 break;
2851 default:
2852 goto out;
2855 out: ;
2858 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2860 enum-specifier:
2861 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2862 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2863 enum attributes[opt] identifier
2865 The form with trailing comma is new in C99. The forms with
2866 attributes are GNU extensions. In GNU C, we accept any expression
2867 without commas in the syntax (assignment expressions, not just
2868 conditional expressions); assignment expressions will be diagnosed
2869 as non-constant.
2871 enumerator-list:
2872 enumerator
2873 enumerator-list , enumerator
2875 enumerator:
2876 enumeration-constant
2877 enumeration-constant = constant-expression
2879 GNU Extensions:
2881 enumerator:
2882 enumeration-constant attributes[opt]
2883 enumeration-constant attributes[opt] = constant-expression
2887 static struct c_typespec
2888 c_parser_enum_specifier (c_parser *parser)
2890 struct c_typespec ret;
2891 tree attrs;
2892 tree ident = NULL_TREE;
2893 location_t enum_loc;
2894 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2895 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2896 c_parser_consume_token (parser);
2897 attrs = c_parser_attributes (parser);
2898 enum_loc = c_parser_peek_token (parser)->location;
2899 /* Set the location in case we create a decl now. */
2900 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2901 if (c_parser_next_token_is (parser, CPP_NAME))
2903 ident = c_parser_peek_token (parser)->value;
2904 ident_loc = c_parser_peek_token (parser)->location;
2905 enum_loc = ident_loc;
2906 c_parser_consume_token (parser);
2908 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2910 /* Parse an enum definition. */
2911 struct c_enum_contents the_enum;
2912 tree type;
2913 tree postfix_attrs;
2914 /* We chain the enumerators in reverse order, then put them in
2915 forward order at the end. */
2916 tree values;
2917 timevar_push (TV_PARSE_ENUM);
2918 type = start_enum (enum_loc, &the_enum, ident);
2919 values = NULL_TREE;
2920 c_parser_consume_token (parser);
2921 while (true)
2923 tree enum_id;
2924 tree enum_value;
2925 tree enum_decl;
2926 bool seen_comma;
2927 c_token *token;
2928 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2929 location_t decl_loc, value_loc;
2930 if (c_parser_next_token_is_not (parser, CPP_NAME))
2932 /* Give a nicer error for "enum {}". */
2933 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2934 && !parser->error)
2936 error_at (c_parser_peek_token (parser)->location,
2937 "empty enum is invalid");
2938 parser->error = true;
2940 else
2941 c_parser_error (parser, "expected identifier");
2942 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2943 values = error_mark_node;
2944 break;
2946 token = c_parser_peek_token (parser);
2947 enum_id = token->value;
2948 /* Set the location in case we create a decl now. */
2949 c_parser_set_source_position_from_token (token);
2950 decl_loc = value_loc = token->location;
2951 c_parser_consume_token (parser);
2952 /* Parse any specified attributes. */
2953 tree enum_attrs = c_parser_attributes (parser);
2954 if (c_parser_next_token_is (parser, CPP_EQ))
2956 c_parser_consume_token (parser);
2957 value_loc = c_parser_peek_token (parser)->location;
2958 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2960 else
2961 enum_value = NULL_TREE;
2962 enum_decl = build_enumerator (decl_loc, value_loc,
2963 &the_enum, enum_id, enum_value);
2964 if (enum_attrs)
2965 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2966 TREE_CHAIN (enum_decl) = values;
2967 values = enum_decl;
2968 seen_comma = false;
2969 if (c_parser_next_token_is (parser, CPP_COMMA))
2971 comma_loc = c_parser_peek_token (parser)->location;
2972 seen_comma = true;
2973 c_parser_consume_token (parser);
2975 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2977 if (seen_comma)
2978 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2979 "comma at end of enumerator list");
2980 c_parser_consume_token (parser);
2981 break;
2983 if (!seen_comma)
2985 c_parser_error (parser, "expected %<,%> or %<}%>");
2986 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2987 values = error_mark_node;
2988 break;
2991 postfix_attrs = c_parser_attributes (parser);
2992 ret.spec = finish_enum (type, nreverse (values),
2993 chainon (attrs, postfix_attrs));
2994 ret.kind = ctsk_tagdef;
2995 ret.expr = NULL_TREE;
2996 ret.expr_const_operands = true;
2997 timevar_pop (TV_PARSE_ENUM);
2998 return ret;
3000 else if (!ident)
3002 c_parser_error (parser, "expected %<{%>");
3003 ret.spec = error_mark_node;
3004 ret.kind = ctsk_tagref;
3005 ret.expr = NULL_TREE;
3006 ret.expr_const_operands = true;
3007 return ret;
3009 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3010 /* In ISO C, enumerated types can be referred to only if already
3011 defined. */
3012 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3014 gcc_assert (ident);
3015 pedwarn (enum_loc, OPT_Wpedantic,
3016 "ISO C forbids forward references to %<enum%> types");
3018 return ret;
3021 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3023 struct-or-union-specifier:
3024 struct-or-union attributes[opt] identifier[opt]
3025 { struct-contents } attributes[opt]
3026 struct-or-union attributes[opt] identifier
3028 struct-contents:
3029 struct-declaration-list
3031 struct-declaration-list:
3032 struct-declaration ;
3033 struct-declaration-list struct-declaration ;
3035 GNU extensions:
3037 struct-contents:
3038 empty
3039 struct-declaration
3040 struct-declaration-list struct-declaration
3042 struct-declaration-list:
3043 struct-declaration-list ;
3046 (Note that in the syntax here, unlike that in ISO C, the semicolons
3047 are included here rather than in struct-declaration, in order to
3048 describe the syntax with extra semicolons and missing semicolon at
3049 end.)
3051 Objective-C:
3053 struct-declaration-list:
3054 @defs ( class-name )
3056 (Note this does not include a trailing semicolon, but can be
3057 followed by further declarations, and gets a pedwarn-if-pedantic
3058 when followed by a semicolon.) */
3060 static struct c_typespec
3061 c_parser_struct_or_union_specifier (c_parser *parser)
3063 struct c_typespec ret;
3064 tree attrs;
3065 tree ident = NULL_TREE;
3066 location_t struct_loc;
3067 location_t ident_loc = UNKNOWN_LOCATION;
3068 enum tree_code code;
3069 switch (c_parser_peek_token (parser)->keyword)
3071 case RID_STRUCT:
3072 code = RECORD_TYPE;
3073 break;
3074 case RID_UNION:
3075 code = UNION_TYPE;
3076 break;
3077 default:
3078 gcc_unreachable ();
3080 struct_loc = c_parser_peek_token (parser)->location;
3081 c_parser_consume_token (parser);
3082 attrs = c_parser_attributes (parser);
3084 /* Set the location in case we create a decl now. */
3085 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3087 if (c_parser_next_token_is (parser, CPP_NAME))
3089 ident = c_parser_peek_token (parser)->value;
3090 ident_loc = c_parser_peek_token (parser)->location;
3091 struct_loc = ident_loc;
3092 c_parser_consume_token (parser);
3094 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3096 /* Parse a struct or union definition. Start the scope of the
3097 tag before parsing components. */
3098 struct c_struct_parse_info *struct_info;
3099 tree type = start_struct (struct_loc, code, ident, &struct_info);
3100 tree postfix_attrs;
3101 /* We chain the components in reverse order, then put them in
3102 forward order at the end. Each struct-declaration may
3103 declare multiple components (comma-separated), so we must use
3104 chainon to join them, although when parsing each
3105 struct-declaration we can use TREE_CHAIN directly.
3107 The theory behind all this is that there will be more
3108 semicolon separated fields than comma separated fields, and
3109 so we'll be minimizing the number of node traversals required
3110 by chainon. */
3111 tree contents;
3112 timevar_push (TV_PARSE_STRUCT);
3113 contents = NULL_TREE;
3114 c_parser_consume_token (parser);
3115 /* Handle the Objective-C @defs construct,
3116 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3117 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3119 tree name;
3120 gcc_assert (c_dialect_objc ());
3121 c_parser_consume_token (parser);
3122 matching_parens parens;
3123 if (!parens.require_open (parser))
3124 goto end_at_defs;
3125 if (c_parser_next_token_is (parser, CPP_NAME)
3126 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3128 name = c_parser_peek_token (parser)->value;
3129 c_parser_consume_token (parser);
3131 else
3133 c_parser_error (parser, "expected class name");
3134 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3135 goto end_at_defs;
3137 parens.skip_until_found_close (parser);
3138 contents = nreverse (objc_get_class_ivars (name));
3140 end_at_defs:
3141 /* Parse the struct-declarations and semicolons. Problems with
3142 semicolons are diagnosed here; empty structures are diagnosed
3143 elsewhere. */
3144 while (true)
3146 tree decls;
3147 /* Parse any stray semicolon. */
3148 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3150 location_t semicolon_loc
3151 = c_parser_peek_token (parser)->location;
3152 gcc_rich_location richloc (semicolon_loc);
3153 richloc.add_fixit_remove ();
3154 pedwarn (&richloc, OPT_Wpedantic,
3155 "extra semicolon in struct or union specified");
3156 c_parser_consume_token (parser);
3157 continue;
3159 /* Stop if at the end of the struct or union contents. */
3160 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3162 c_parser_consume_token (parser);
3163 break;
3165 /* Accept #pragmas at struct scope. */
3166 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3168 c_parser_pragma (parser, pragma_struct, NULL);
3169 continue;
3171 /* Parse some comma-separated declarations, but not the
3172 trailing semicolon if any. */
3173 decls = c_parser_struct_declaration (parser);
3174 contents = chainon (decls, contents);
3175 /* If no semicolon follows, either we have a parse error or
3176 are at the end of the struct or union and should
3177 pedwarn. */
3178 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3179 c_parser_consume_token (parser);
3180 else
3182 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3183 pedwarn (c_parser_peek_token (parser)->location, 0,
3184 "no semicolon at end of struct or union");
3185 else if (parser->error
3186 || !c_parser_next_token_starts_declspecs (parser))
3188 c_parser_error (parser, "expected %<;%>");
3189 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3190 break;
3193 /* If we come here, we have already emitted an error
3194 for an expected `;', identifier or `(', and we also
3195 recovered already. Go on with the next field. */
3198 postfix_attrs = c_parser_attributes (parser);
3199 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3200 chainon (attrs, postfix_attrs), struct_info);
3201 ret.kind = ctsk_tagdef;
3202 ret.expr = NULL_TREE;
3203 ret.expr_const_operands = true;
3204 timevar_pop (TV_PARSE_STRUCT);
3205 return ret;
3207 else if (!ident)
3209 c_parser_error (parser, "expected %<{%>");
3210 ret.spec = error_mark_node;
3211 ret.kind = ctsk_tagref;
3212 ret.expr = NULL_TREE;
3213 ret.expr_const_operands = true;
3214 return ret;
3216 ret = parser_xref_tag (ident_loc, code, ident);
3217 return ret;
3220 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3221 *without* the trailing semicolon.
3223 struct-declaration:
3224 specifier-qualifier-list struct-declarator-list
3225 static_assert-declaration-no-semi
3227 specifier-qualifier-list:
3228 type-specifier specifier-qualifier-list[opt]
3229 type-qualifier specifier-qualifier-list[opt]
3230 alignment-specifier specifier-qualifier-list[opt]
3231 attributes specifier-qualifier-list[opt]
3233 struct-declarator-list:
3234 struct-declarator
3235 struct-declarator-list , attributes[opt] struct-declarator
3237 struct-declarator:
3238 declarator attributes[opt]
3239 declarator[opt] : constant-expression attributes[opt]
3241 GNU extensions:
3243 struct-declaration:
3244 __extension__ struct-declaration
3245 specifier-qualifier-list
3247 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3248 of attributes where shown is a GNU extension. In GNU C, we accept
3249 any expression without commas in the syntax (assignment
3250 expressions, not just conditional expressions); assignment
3251 expressions will be diagnosed as non-constant. */
3253 static tree
3254 c_parser_struct_declaration (c_parser *parser)
3256 struct c_declspecs *specs;
3257 tree prefix_attrs;
3258 tree all_prefix_attrs;
3259 tree decls;
3260 location_t decl_loc;
3261 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3263 int ext;
3264 tree decl;
3265 ext = disable_extension_diagnostics ();
3266 c_parser_consume_token (parser);
3267 decl = c_parser_struct_declaration (parser);
3268 restore_extension_diagnostics (ext);
3269 return decl;
3271 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3273 c_parser_static_assert_declaration_no_semi (parser);
3274 return NULL_TREE;
3276 specs = build_null_declspecs ();
3277 decl_loc = c_parser_peek_token (parser)->location;
3278 /* Strictly by the standard, we shouldn't allow _Alignas here,
3279 but it appears to have been intended to allow it there, so
3280 we're keeping it as it is until WG14 reaches a conclusion
3281 of N1731.
3282 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3283 c_parser_declspecs (parser, specs, false, true, true,
3284 true, false, cla_nonabstract_decl);
3285 if (parser->error)
3286 return NULL_TREE;
3287 if (!specs->declspecs_seen_p)
3289 c_parser_error (parser, "expected specifier-qualifier-list");
3290 return NULL_TREE;
3292 finish_declspecs (specs);
3293 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3294 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3296 tree ret;
3297 if (specs->typespec_kind == ctsk_none)
3299 pedwarn (decl_loc, OPT_Wpedantic,
3300 "ISO C forbids member declarations with no members");
3301 shadow_tag_warned (specs, pedantic);
3302 ret = NULL_TREE;
3304 else
3306 /* Support for unnamed structs or unions as members of
3307 structs or unions (which is [a] useful and [b] supports
3308 MS P-SDK). */
3309 tree attrs = NULL;
3311 ret = grokfield (c_parser_peek_token (parser)->location,
3312 build_id_declarator (NULL_TREE), specs,
3313 NULL_TREE, &attrs);
3314 if (ret)
3315 decl_attributes (&ret, attrs, 0);
3317 return ret;
3320 /* Provide better error recovery. Note that a type name here is valid,
3321 and will be treated as a field name. */
3322 if (specs->typespec_kind == ctsk_tagdef
3323 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3324 && c_parser_next_token_starts_declspecs (parser)
3325 && !c_parser_next_token_is (parser, CPP_NAME))
3327 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3328 parser->error = false;
3329 return NULL_TREE;
3332 pending_xref_error ();
3333 prefix_attrs = specs->attrs;
3334 all_prefix_attrs = prefix_attrs;
3335 specs->attrs = NULL_TREE;
3336 decls = NULL_TREE;
3337 while (true)
3339 /* Declaring one or more declarators or un-named bit-fields. */
3340 struct c_declarator *declarator;
3341 bool dummy = false;
3342 if (c_parser_next_token_is (parser, CPP_COLON))
3343 declarator = build_id_declarator (NULL_TREE);
3344 else
3345 declarator = c_parser_declarator (parser,
3346 specs->typespec_kind != ctsk_none,
3347 C_DTR_NORMAL, &dummy);
3348 if (declarator == NULL)
3350 c_parser_skip_to_end_of_block_or_statement (parser);
3351 break;
3353 if (c_parser_next_token_is (parser, CPP_COLON)
3354 || c_parser_next_token_is (parser, CPP_COMMA)
3355 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3356 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3357 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3359 tree postfix_attrs = NULL_TREE;
3360 tree width = NULL_TREE;
3361 tree d;
3362 if (c_parser_next_token_is (parser, CPP_COLON))
3364 c_parser_consume_token (parser);
3365 width = c_parser_expr_no_commas (parser, NULL).value;
3367 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3368 postfix_attrs = c_parser_attributes (parser);
3369 d = grokfield (c_parser_peek_token (parser)->location,
3370 declarator, specs, width, &all_prefix_attrs);
3371 decl_attributes (&d, chainon (postfix_attrs,
3372 all_prefix_attrs), 0);
3373 DECL_CHAIN (d) = decls;
3374 decls = d;
3375 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3376 all_prefix_attrs = chainon (c_parser_attributes (parser),
3377 prefix_attrs);
3378 else
3379 all_prefix_attrs = prefix_attrs;
3380 if (c_parser_next_token_is (parser, CPP_COMMA))
3381 c_parser_consume_token (parser);
3382 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3383 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3385 /* Semicolon consumed in caller. */
3386 break;
3388 else
3390 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3391 break;
3394 else
3396 c_parser_error (parser,
3397 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3398 "%<__attribute__%>");
3399 break;
3402 return decls;
3405 /* Parse a typeof specifier (a GNU extension).
3407 typeof-specifier:
3408 typeof ( expression )
3409 typeof ( type-name )
3412 static struct c_typespec
3413 c_parser_typeof_specifier (c_parser *parser)
3415 struct c_typespec ret;
3416 ret.kind = ctsk_typeof;
3417 ret.spec = error_mark_node;
3418 ret.expr = NULL_TREE;
3419 ret.expr_const_operands = true;
3420 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3421 c_parser_consume_token (parser);
3422 c_inhibit_evaluation_warnings++;
3423 in_typeof++;
3424 matching_parens parens;
3425 if (!parens.require_open (parser))
3427 c_inhibit_evaluation_warnings--;
3428 in_typeof--;
3429 return ret;
3431 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3433 struct c_type_name *type = c_parser_type_name (parser);
3434 c_inhibit_evaluation_warnings--;
3435 in_typeof--;
3436 if (type != NULL)
3438 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3439 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3442 else
3444 bool was_vm;
3445 location_t here = c_parser_peek_token (parser)->location;
3446 struct c_expr expr = c_parser_expression (parser);
3447 c_inhibit_evaluation_warnings--;
3448 in_typeof--;
3449 if (TREE_CODE (expr.value) == COMPONENT_REF
3450 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3451 error_at (here, "%<typeof%> applied to a bit-field");
3452 mark_exp_read (expr.value);
3453 ret.spec = TREE_TYPE (expr.value);
3454 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3455 /* This is returned with the type so that when the type is
3456 evaluated, this can be evaluated. */
3457 if (was_vm)
3458 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3459 pop_maybe_used (was_vm);
3460 /* For use in macros such as those in <stdatomic.h>, remove all
3461 qualifiers from atomic types. (const can be an issue for more macros
3462 using typeof than just the <stdatomic.h> ones.) */
3463 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3464 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3466 parens.skip_until_found_close (parser);
3467 return ret;
3470 /* Parse an alignment-specifier.
3472 C11 6.7.5:
3474 alignment-specifier:
3475 _Alignas ( type-name )
3476 _Alignas ( constant-expression )
3479 static tree
3480 c_parser_alignas_specifier (c_parser * parser)
3482 tree ret = error_mark_node;
3483 location_t loc = c_parser_peek_token (parser)->location;
3484 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3485 c_parser_consume_token (parser);
3486 if (flag_isoc99)
3487 pedwarn_c99 (loc, OPT_Wpedantic,
3488 "ISO C99 does not support %<_Alignas%>");
3489 else
3490 pedwarn_c99 (loc, OPT_Wpedantic,
3491 "ISO C90 does not support %<_Alignas%>");
3492 matching_parens parens;
3493 if (!parens.require_open (parser))
3494 return ret;
3495 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3497 struct c_type_name *type = c_parser_type_name (parser);
3498 if (type != NULL)
3499 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3500 false, true, 1);
3502 else
3503 ret = c_parser_expr_no_commas (parser, NULL).value;
3504 parens.skip_until_found_close (parser);
3505 return ret;
3508 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3509 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3510 a typedef name may be redeclared; otherwise it may not. KIND
3511 indicates which kind of declarator is wanted. Returns a valid
3512 declarator except in the case of a syntax error in which case NULL is
3513 returned. *SEEN_ID is set to true if an identifier being declared is
3514 seen; this is used to diagnose bad forms of abstract array declarators
3515 and to determine whether an identifier list is syntactically permitted.
3517 declarator:
3518 pointer[opt] direct-declarator
3520 direct-declarator:
3521 identifier
3522 ( attributes[opt] declarator )
3523 direct-declarator array-declarator
3524 direct-declarator ( parameter-type-list )
3525 direct-declarator ( identifier-list[opt] )
3527 pointer:
3528 * type-qualifier-list[opt]
3529 * type-qualifier-list[opt] pointer
3531 type-qualifier-list:
3532 type-qualifier
3533 attributes
3534 type-qualifier-list type-qualifier
3535 type-qualifier-list attributes
3537 array-declarator:
3538 [ type-qualifier-list[opt] assignment-expression[opt] ]
3539 [ static type-qualifier-list[opt] assignment-expression ]
3540 [ type-qualifier-list static assignment-expression ]
3541 [ type-qualifier-list[opt] * ]
3543 parameter-type-list:
3544 parameter-list
3545 parameter-list , ...
3547 parameter-list:
3548 parameter-declaration
3549 parameter-list , parameter-declaration
3551 parameter-declaration:
3552 declaration-specifiers declarator attributes[opt]
3553 declaration-specifiers abstract-declarator[opt] attributes[opt]
3555 identifier-list:
3556 identifier
3557 identifier-list , identifier
3559 abstract-declarator:
3560 pointer
3561 pointer[opt] direct-abstract-declarator
3563 direct-abstract-declarator:
3564 ( attributes[opt] abstract-declarator )
3565 direct-abstract-declarator[opt] array-declarator
3566 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3568 GNU extensions:
3570 direct-declarator:
3571 direct-declarator ( parameter-forward-declarations
3572 parameter-type-list[opt] )
3574 direct-abstract-declarator:
3575 direct-abstract-declarator[opt] ( parameter-forward-declarations
3576 parameter-type-list[opt] )
3578 parameter-forward-declarations:
3579 parameter-list ;
3580 parameter-forward-declarations parameter-list ;
3582 The uses of attributes shown above are GNU extensions.
3584 Some forms of array declarator are not included in C99 in the
3585 syntax for abstract declarators; these are disallowed elsewhere.
3586 This may be a defect (DR#289).
3588 This function also accepts an omitted abstract declarator as being
3589 an abstract declarator, although not part of the formal syntax. */
3591 struct c_declarator *
3592 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3593 bool *seen_id)
3595 /* Parse any initial pointer part. */
3596 if (c_parser_next_token_is (parser, CPP_MULT))
3598 struct c_declspecs *quals_attrs = build_null_declspecs ();
3599 struct c_declarator *inner;
3600 c_parser_consume_token (parser);
3601 c_parser_declspecs (parser, quals_attrs, false, false, true,
3602 false, false, cla_prefer_id);
3603 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3604 if (inner == NULL)
3605 return NULL;
3606 else
3607 return make_pointer_declarator (quals_attrs, inner);
3609 /* Now we have a direct declarator, direct abstract declarator or
3610 nothing (which counts as a direct abstract declarator here). */
3611 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3614 /* Parse a direct declarator or direct abstract declarator; arguments
3615 as c_parser_declarator. */
3617 static struct c_declarator *
3618 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3619 bool *seen_id)
3621 /* The direct declarator must start with an identifier (possibly
3622 omitted) or a parenthesized declarator (possibly abstract). In
3623 an ordinary declarator, initial parentheses must start a
3624 parenthesized declarator. In an abstract declarator or parameter
3625 declarator, they could start a parenthesized declarator or a
3626 parameter list. To tell which, the open parenthesis and any
3627 following attributes must be read. If a declaration specifier
3628 follows, then it is a parameter list; if the specifier is a
3629 typedef name, there might be an ambiguity about redeclaring it,
3630 which is resolved in the direction of treating it as a typedef
3631 name. If a close parenthesis follows, it is also an empty
3632 parameter list, as the syntax does not permit empty abstract
3633 declarators. Otherwise, it is a parenthesized declarator (in
3634 which case the analysis may be repeated inside it, recursively).
3636 ??? There is an ambiguity in a parameter declaration "int
3637 (__attribute__((foo)) x)", where x is not a typedef name: it
3638 could be an abstract declarator for a function, or declare x with
3639 parentheses. The proper resolution of this ambiguity needs
3640 documenting. At present we follow an accident of the old
3641 parser's implementation, whereby the first parameter must have
3642 some declaration specifiers other than just attributes. Thus as
3643 a parameter declaration it is treated as a parenthesized
3644 parameter named x, and as an abstract declarator it is
3645 rejected.
3647 ??? Also following the old parser, attributes inside an empty
3648 parameter list are ignored, making it a list not yielding a
3649 prototype, rather than giving an error or making it have one
3650 parameter with implicit type int.
3652 ??? Also following the old parser, typedef names may be
3653 redeclared in declarators, but not Objective-C class names. */
3655 if (kind != C_DTR_ABSTRACT
3656 && c_parser_next_token_is (parser, CPP_NAME)
3657 && ((type_seen_p
3658 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3659 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3660 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3662 struct c_declarator *inner
3663 = build_id_declarator (c_parser_peek_token (parser)->value);
3664 *seen_id = true;
3665 inner->id_loc = c_parser_peek_token (parser)->location;
3666 c_parser_consume_token (parser);
3667 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3670 if (kind != C_DTR_NORMAL
3671 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3673 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3674 inner->id_loc = c_parser_peek_token (parser)->location;
3675 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3678 /* Either we are at the end of an abstract declarator, or we have
3679 parentheses. */
3681 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3683 tree attrs;
3684 struct c_declarator *inner;
3685 c_parser_consume_token (parser);
3686 attrs = c_parser_attributes (parser);
3687 if (kind != C_DTR_NORMAL
3688 && (c_parser_next_token_starts_declspecs (parser)
3689 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3691 struct c_arg_info *args
3692 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3693 attrs);
3694 if (args == NULL)
3695 return NULL;
3696 else
3698 inner
3699 = build_function_declarator (args,
3700 build_id_declarator (NULL_TREE));
3701 return c_parser_direct_declarator_inner (parser, *seen_id,
3702 inner);
3705 /* A parenthesized declarator. */
3706 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3707 if (inner != NULL && attrs != NULL)
3708 inner = build_attrs_declarator (attrs, inner);
3709 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3711 c_parser_consume_token (parser);
3712 if (inner == NULL)
3713 return NULL;
3714 else
3715 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3717 else
3719 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3720 "expected %<)%>");
3721 return NULL;
3724 else
3726 if (kind == C_DTR_NORMAL)
3728 c_parser_error (parser, "expected identifier or %<(%>");
3729 return NULL;
3731 else
3732 return build_id_declarator (NULL_TREE);
3736 /* Parse part of a direct declarator or direct abstract declarator,
3737 given that some (in INNER) has already been parsed; ID_PRESENT is
3738 true if an identifier is present, false for an abstract
3739 declarator. */
3741 static struct c_declarator *
3742 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3743 struct c_declarator *inner)
3745 /* Parse a sequence of array declarators and parameter lists. */
3746 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3748 location_t brace_loc = c_parser_peek_token (parser)->location;
3749 struct c_declarator *declarator;
3750 struct c_declspecs *quals_attrs = build_null_declspecs ();
3751 bool static_seen;
3752 bool star_seen;
3753 struct c_expr dimen;
3754 dimen.value = NULL_TREE;
3755 dimen.original_code = ERROR_MARK;
3756 dimen.original_type = NULL_TREE;
3757 c_parser_consume_token (parser);
3758 c_parser_declspecs (parser, quals_attrs, false, false, true,
3759 false, false, cla_prefer_id);
3760 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3761 if (static_seen)
3762 c_parser_consume_token (parser);
3763 if (static_seen && !quals_attrs->declspecs_seen_p)
3764 c_parser_declspecs (parser, quals_attrs, false, false, true,
3765 false, false, cla_prefer_id);
3766 if (!quals_attrs->declspecs_seen_p)
3767 quals_attrs = NULL;
3768 /* If "static" is present, there must be an array dimension.
3769 Otherwise, there may be a dimension, "*", or no
3770 dimension. */
3771 if (static_seen)
3773 star_seen = false;
3774 dimen = c_parser_expr_no_commas (parser, NULL);
3776 else
3778 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3780 dimen.value = NULL_TREE;
3781 star_seen = false;
3783 else if (c_parser_next_token_is (parser, CPP_MULT))
3785 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3787 dimen.value = NULL_TREE;
3788 star_seen = true;
3789 c_parser_consume_token (parser);
3791 else
3793 star_seen = false;
3794 dimen = c_parser_expr_no_commas (parser, NULL);
3797 else
3799 star_seen = false;
3800 dimen = c_parser_expr_no_commas (parser, NULL);
3803 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3804 c_parser_consume_token (parser);
3805 else
3807 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3808 "expected %<]%>");
3809 return NULL;
3811 if (dimen.value)
3812 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3813 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3814 static_seen, star_seen);
3815 if (declarator == NULL)
3816 return NULL;
3817 inner = set_array_declarator_inner (declarator, inner);
3818 return c_parser_direct_declarator_inner (parser, id_present, inner);
3820 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3822 tree attrs;
3823 struct c_arg_info *args;
3824 c_parser_consume_token (parser);
3825 attrs = c_parser_attributes (parser);
3826 args = c_parser_parms_declarator (parser, id_present, attrs);
3827 if (args == NULL)
3828 return NULL;
3829 else
3831 inner = build_function_declarator (args, inner);
3832 return c_parser_direct_declarator_inner (parser, id_present, inner);
3835 return inner;
3838 /* Parse a parameter list or identifier list, including the closing
3839 parenthesis but not the opening one. ATTRS are the attributes at
3840 the start of the list. ID_LIST_OK is true if an identifier list is
3841 acceptable; such a list must not have attributes at the start. */
3843 static struct c_arg_info *
3844 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3846 push_scope ();
3847 declare_parm_level ();
3848 /* If the list starts with an identifier, it is an identifier list.
3849 Otherwise, it is either a prototype list or an empty list. */
3850 if (id_list_ok
3851 && !attrs
3852 && c_parser_next_token_is (parser, CPP_NAME)
3853 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3855 /* Look ahead to detect typos in type names. */
3856 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3857 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3858 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3859 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3860 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3862 tree list = NULL_TREE, *nextp = &list;
3863 while (c_parser_next_token_is (parser, CPP_NAME)
3864 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3866 *nextp = build_tree_list (NULL_TREE,
3867 c_parser_peek_token (parser)->value);
3868 nextp = & TREE_CHAIN (*nextp);
3869 c_parser_consume_token (parser);
3870 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3871 break;
3872 c_parser_consume_token (parser);
3873 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3875 c_parser_error (parser, "expected identifier");
3876 break;
3879 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3881 struct c_arg_info *ret = build_arg_info ();
3882 ret->types = list;
3883 c_parser_consume_token (parser);
3884 pop_scope ();
3885 return ret;
3887 else
3889 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3890 "expected %<)%>");
3891 pop_scope ();
3892 return NULL;
3895 else
3897 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3898 NULL);
3899 pop_scope ();
3900 return ret;
3904 /* Parse a parameter list (possibly empty), including the closing
3905 parenthesis but not the opening one. ATTRS are the attributes at
3906 the start of the list. EXPR is NULL or an expression that needs to
3907 be evaluated for the side effects of array size expressions in the
3908 parameters. */
3910 static struct c_arg_info *
3911 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3913 bool bad_parm = false;
3915 /* ??? Following the old parser, forward parameter declarations may
3916 use abstract declarators, and if no real parameter declarations
3917 follow the forward declarations then this is not diagnosed. Also
3918 note as above that attributes are ignored as the only contents of
3919 the parentheses, or as the only contents after forward
3920 declarations. */
3921 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3923 struct c_arg_info *ret = build_arg_info ();
3924 c_parser_consume_token (parser);
3925 return ret;
3927 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3929 struct c_arg_info *ret = build_arg_info ();
3931 if (flag_allow_parameterless_variadic_functions)
3933 /* F (...) is allowed. */
3934 ret->types = NULL_TREE;
3936 else
3938 /* Suppress -Wold-style-definition for this case. */
3939 ret->types = error_mark_node;
3940 error_at (c_parser_peek_token (parser)->location,
3941 "ISO C requires a named argument before %<...%>");
3943 c_parser_consume_token (parser);
3944 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3946 c_parser_consume_token (parser);
3947 return ret;
3949 else
3951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3952 "expected %<)%>");
3953 return NULL;
3956 /* Nonempty list of parameters, either terminated with semicolon
3957 (forward declarations; recurse) or with close parenthesis (normal
3958 function) or with ", ... )" (variadic function). */
3959 while (true)
3961 /* Parse a parameter. */
3962 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3963 attrs = NULL_TREE;
3964 if (parm == NULL)
3965 bad_parm = true;
3966 else
3967 push_parm_decl (parm, &expr);
3968 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3970 tree new_attrs;
3971 c_parser_consume_token (parser);
3972 mark_forward_parm_decls ();
3973 new_attrs = c_parser_attributes (parser);
3974 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3976 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3978 c_parser_consume_token (parser);
3979 if (bad_parm)
3980 return NULL;
3981 else
3982 return get_parm_info (false, expr);
3984 if (!c_parser_require (parser, CPP_COMMA,
3985 "expected %<;%>, %<,%> or %<)%>",
3986 UNKNOWN_LOCATION, false))
3988 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3989 return NULL;
3991 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3993 c_parser_consume_token (parser);
3994 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3996 c_parser_consume_token (parser);
3997 if (bad_parm)
3998 return NULL;
3999 else
4000 return get_parm_info (true, expr);
4002 else
4004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4005 "expected %<)%>");
4006 return NULL;
4012 /* Parse a parameter declaration. ATTRS are the attributes at the
4013 start of the declaration if it is the first parameter. */
4015 static struct c_parm *
4016 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4018 struct c_declspecs *specs;
4019 struct c_declarator *declarator;
4020 tree prefix_attrs;
4021 tree postfix_attrs = NULL_TREE;
4022 bool dummy = false;
4024 /* Accept #pragmas between parameter declarations. */
4025 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4026 c_parser_pragma (parser, pragma_param, NULL);
4028 if (!c_parser_next_token_starts_declspecs (parser))
4030 c_token *token = c_parser_peek_token (parser);
4031 if (parser->error)
4032 return NULL;
4033 c_parser_set_source_position_from_token (token);
4034 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4036 name_hint hint = lookup_name_fuzzy (token->value,
4037 FUZZY_LOOKUP_TYPENAME,
4038 token->location);
4039 if (hint)
4041 gcc_rich_location richloc (token->location);
4042 richloc.add_fixit_replace (hint.suggestion ());
4043 error_at (&richloc,
4044 "unknown type name %qE; did you mean %qs?",
4045 token->value, hint.suggestion ());
4047 else
4048 error_at (token->location, "unknown type name %qE", token->value);
4049 parser->error = true;
4051 /* ??? In some Objective-C cases '...' isn't applicable so there
4052 should be a different message. */
4053 else
4054 c_parser_error (parser,
4055 "expected declaration specifiers or %<...%>");
4056 c_parser_skip_to_end_of_parameter (parser);
4057 return NULL;
4060 location_t start_loc = c_parser_peek_token (parser)->location;
4062 specs = build_null_declspecs ();
4063 if (attrs)
4065 declspecs_add_attrs (input_location, specs, attrs);
4066 attrs = NULL_TREE;
4068 c_parser_declspecs (parser, specs, true, true, true, true, false,
4069 cla_nonabstract_decl);
4070 finish_declspecs (specs);
4071 pending_xref_error ();
4072 prefix_attrs = specs->attrs;
4073 specs->attrs = NULL_TREE;
4074 declarator = c_parser_declarator (parser,
4075 specs->typespec_kind != ctsk_none,
4076 C_DTR_PARM, &dummy);
4077 if (declarator == NULL)
4079 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4080 return NULL;
4082 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4083 postfix_attrs = c_parser_attributes (parser);
4085 /* Generate a location for the parameter, ranging from the start of the
4086 initial token to the end of the final token.
4088 If we have a identifier, then use it for the caret location, e.g.
4090 extern int callee (int one, int (*two)(int, int), float three);
4091 ~~~~~~^~~~~~~~~~~~~~
4093 otherwise, reuse the start location for the caret location e.g.:
4095 extern int callee (int one, int (*)(int, int), float three);
4096 ^~~~~~~~~~~~~~~~~
4098 location_t end_loc = parser->last_token_location;
4100 /* Find any cdk_id declarator; determine if we have an identifier. */
4101 c_declarator *id_declarator = declarator;
4102 while (id_declarator && id_declarator->kind != cdk_id)
4103 id_declarator = id_declarator->declarator;
4104 location_t caret_loc = (id_declarator->u.id
4105 ? id_declarator->id_loc
4106 : start_loc);
4107 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4109 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4110 declarator, param_loc);
4113 /* Parse a string literal in an asm expression. It should not be
4114 translated, and wide string literals are an error although
4115 permitted by the syntax. This is a GNU extension.
4117 asm-string-literal:
4118 string-literal
4120 ??? At present, following the old parser, the caller needs to have
4121 set lex_untranslated_string to 1. It would be better to follow the
4122 C++ parser rather than using this kludge. */
4124 static tree
4125 c_parser_asm_string_literal (c_parser *parser)
4127 tree str;
4128 int save_flag = warn_overlength_strings;
4129 warn_overlength_strings = 0;
4130 if (c_parser_next_token_is (parser, CPP_STRING))
4132 str = c_parser_peek_token (parser)->value;
4133 c_parser_consume_token (parser);
4135 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4137 error_at (c_parser_peek_token (parser)->location,
4138 "wide string literal in %<asm%>");
4139 str = build_string (1, "");
4140 c_parser_consume_token (parser);
4142 else
4144 c_parser_error (parser, "expected string literal");
4145 str = NULL_TREE;
4147 warn_overlength_strings = save_flag;
4148 return str;
4151 /* Parse a simple asm expression. This is used in restricted
4152 contexts, where a full expression with inputs and outputs does not
4153 make sense. This is a GNU extension.
4155 simple-asm-expr:
4156 asm ( asm-string-literal )
4159 static tree
4160 c_parser_simple_asm_expr (c_parser *parser)
4162 tree str;
4163 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4164 /* ??? Follow the C++ parser rather than using the
4165 lex_untranslated_string kludge. */
4166 parser->lex_untranslated_string = true;
4167 c_parser_consume_token (parser);
4168 matching_parens parens;
4169 if (!parens.require_open (parser))
4171 parser->lex_untranslated_string = false;
4172 return NULL_TREE;
4174 str = c_parser_asm_string_literal (parser);
4175 parser->lex_untranslated_string = false;
4176 if (!parens.require_close (parser))
4178 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4179 return NULL_TREE;
4181 return str;
4184 static tree
4185 c_parser_attribute_any_word (c_parser *parser)
4187 tree attr_name = NULL_TREE;
4189 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4191 /* ??? See comment above about what keywords are accepted here. */
4192 bool ok;
4193 switch (c_parser_peek_token (parser)->keyword)
4195 case RID_STATIC:
4196 case RID_UNSIGNED:
4197 case RID_LONG:
4198 case RID_CONST:
4199 case RID_EXTERN:
4200 case RID_REGISTER:
4201 case RID_TYPEDEF:
4202 case RID_SHORT:
4203 case RID_INLINE:
4204 case RID_NORETURN:
4205 case RID_VOLATILE:
4206 case RID_SIGNED:
4207 case RID_AUTO:
4208 case RID_RESTRICT:
4209 case RID_COMPLEX:
4210 case RID_THREAD:
4211 case RID_INT:
4212 case RID_CHAR:
4213 case RID_FLOAT:
4214 case RID_DOUBLE:
4215 case RID_VOID:
4216 case RID_DFLOAT32:
4217 case RID_DFLOAT64:
4218 case RID_DFLOAT128:
4219 CASE_RID_FLOATN_NX:
4220 case RID_BOOL:
4221 case RID_FRACT:
4222 case RID_ACCUM:
4223 case RID_SAT:
4224 case RID_TRANSACTION_ATOMIC:
4225 case RID_TRANSACTION_CANCEL:
4226 case RID_ATOMIC:
4227 case RID_AUTO_TYPE:
4228 case RID_INT_N_0:
4229 case RID_INT_N_1:
4230 case RID_INT_N_2:
4231 case RID_INT_N_3:
4232 ok = true;
4233 break;
4234 default:
4235 ok = false;
4236 break;
4238 if (!ok)
4239 return NULL_TREE;
4241 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4242 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4244 else if (c_parser_next_token_is (parser, CPP_NAME))
4245 attr_name = c_parser_peek_token (parser)->value;
4247 return attr_name;
4250 /* Parse (possibly empty) attributes. This is a GNU extension.
4252 attributes:
4253 empty
4254 attributes attribute
4256 attribute:
4257 __attribute__ ( ( attribute-list ) )
4259 attribute-list:
4260 attrib
4261 attribute_list , attrib
4263 attrib:
4264 empty
4265 any-word
4266 any-word ( identifier )
4267 any-word ( identifier , nonempty-expr-list )
4268 any-word ( expr-list )
4270 where the "identifier" must not be declared as a type, and
4271 "any-word" may be any identifier (including one declared as a
4272 type), a reserved word storage class specifier, type specifier or
4273 type qualifier. ??? This still leaves out most reserved keywords
4274 (following the old parser), shouldn't we include them, and why not
4275 allow identifiers declared as types to start the arguments? */
4277 static tree
4278 c_parser_attributes (c_parser *parser)
4280 tree attrs = NULL_TREE;
4281 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4283 /* ??? Follow the C++ parser rather than using the
4284 lex_untranslated_string kludge. */
4285 parser->lex_untranslated_string = true;
4286 /* Consume the `__attribute__' keyword. */
4287 c_parser_consume_token (parser);
4288 /* Look for the two `(' tokens. */
4289 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4291 parser->lex_untranslated_string = false;
4292 return attrs;
4294 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4296 parser->lex_untranslated_string = false;
4297 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4298 return attrs;
4300 /* Parse the attribute list. */
4301 while (c_parser_next_token_is (parser, CPP_COMMA)
4302 || c_parser_next_token_is (parser, CPP_NAME)
4303 || c_parser_next_token_is (parser, CPP_KEYWORD))
4305 tree attr, attr_name, attr_args;
4306 vec<tree, va_gc> *expr_list;
4307 if (c_parser_next_token_is (parser, CPP_COMMA))
4309 c_parser_consume_token (parser);
4310 continue;
4313 attr_name = c_parser_attribute_any_word (parser);
4314 if (attr_name == NULL)
4315 break;
4316 attr_name = canonicalize_attr_name (attr_name);
4317 c_parser_consume_token (parser);
4318 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4320 attr = build_tree_list (attr_name, NULL_TREE);
4321 /* Add this attribute to the list. */
4322 attrs = chainon (attrs, attr);
4323 /* If the next token isn't a comma, we're done. */
4324 if (!c_parser_next_token_is (parser, CPP_COMMA))
4325 break;
4326 continue;
4328 c_parser_consume_token (parser);
4329 /* Parse the attribute contents. If they start with an
4330 identifier which is followed by a comma or close
4331 parenthesis, then the arguments start with that
4332 identifier; otherwise they are an expression list.
4333 In objective-c the identifier may be a classname. */
4334 if (c_parser_next_token_is (parser, CPP_NAME)
4335 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4336 || (c_dialect_objc ()
4337 && c_parser_peek_token (parser)->id_kind
4338 == C_ID_CLASSNAME))
4339 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4340 || (c_parser_peek_2nd_token (parser)->type
4341 == CPP_CLOSE_PAREN))
4342 && (attribute_takes_identifier_p (attr_name)
4343 || (c_dialect_objc ()
4344 && c_parser_peek_token (parser)->id_kind
4345 == C_ID_CLASSNAME)))
4347 tree arg1 = c_parser_peek_token (parser)->value;
4348 c_parser_consume_token (parser);
4349 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4350 attr_args = build_tree_list (NULL_TREE, arg1);
4351 else
4353 tree tree_list;
4354 c_parser_consume_token (parser);
4355 expr_list = c_parser_expr_list (parser, false, true,
4356 NULL, NULL, NULL, NULL);
4357 tree_list = build_tree_list_vec (expr_list);
4358 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4359 release_tree_vector (expr_list);
4362 else
4364 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4365 attr_args = NULL_TREE;
4366 else
4368 expr_list = c_parser_expr_list (parser, false, true,
4369 NULL, NULL, NULL, NULL);
4370 attr_args = build_tree_list_vec (expr_list);
4371 release_tree_vector (expr_list);
4375 attr = build_tree_list (attr_name, attr_args);
4376 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4377 c_parser_consume_token (parser);
4378 else
4380 parser->lex_untranslated_string = false;
4381 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4382 "expected %<)%>");
4383 return attrs;
4385 /* Add this attribute to the list. */
4386 attrs = chainon (attrs, attr);
4387 /* If the next token isn't a comma, we're done. */
4388 if (!c_parser_next_token_is (parser, CPP_COMMA))
4389 break;
4391 /* Look for the two `)' tokens. */
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 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4402 c_parser_consume_token (parser);
4403 else
4405 parser->lex_untranslated_string = false;
4406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4407 "expected %<)%>");
4408 return attrs;
4410 parser->lex_untranslated_string = false;
4413 return attrs;
4416 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4417 says whether alignment specifiers are OK (only in cases that might
4418 be the type name of a compound literal).
4420 type-name:
4421 specifier-qualifier-list abstract-declarator[opt]
4424 struct c_type_name *
4425 c_parser_type_name (c_parser *parser, bool alignas_ok)
4427 struct c_declspecs *specs = build_null_declspecs ();
4428 struct c_declarator *declarator;
4429 struct c_type_name *ret;
4430 bool dummy = false;
4431 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4432 cla_prefer_type);
4433 if (!specs->declspecs_seen_p)
4435 c_parser_error (parser, "expected specifier-qualifier-list");
4436 return NULL;
4438 if (specs->type != error_mark_node)
4440 pending_xref_error ();
4441 finish_declspecs (specs);
4443 declarator = c_parser_declarator (parser,
4444 specs->typespec_kind != ctsk_none,
4445 C_DTR_ABSTRACT, &dummy);
4446 if (declarator == NULL)
4447 return NULL;
4448 ret = XOBNEW (&parser_obstack, struct c_type_name);
4449 ret->specs = specs;
4450 ret->declarator = declarator;
4451 return ret;
4454 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4456 initializer:
4457 assignment-expression
4458 { initializer-list }
4459 { initializer-list , }
4461 initializer-list:
4462 designation[opt] initializer
4463 initializer-list , designation[opt] initializer
4465 designation:
4466 designator-list =
4468 designator-list:
4469 designator
4470 designator-list designator
4472 designator:
4473 array-designator
4474 . identifier
4476 array-designator:
4477 [ constant-expression ]
4479 GNU extensions:
4481 initializer:
4484 designation:
4485 array-designator
4486 identifier :
4488 array-designator:
4489 [ constant-expression ... constant-expression ]
4491 Any expression without commas is accepted in the syntax for the
4492 constant-expressions, with non-constant expressions rejected later.
4494 This function is only used for top-level initializers; for nested
4495 ones, see c_parser_initval. */
4497 static struct c_expr
4498 c_parser_initializer (c_parser *parser)
4500 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4501 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4502 else
4504 struct c_expr ret;
4505 location_t loc = c_parser_peek_token (parser)->location;
4506 ret = c_parser_expr_no_commas (parser, NULL);
4507 if (TREE_CODE (ret.value) != STRING_CST
4508 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4509 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4510 return ret;
4514 /* The location of the last comma within the current initializer list,
4515 or UNKNOWN_LOCATION if not within one. */
4517 location_t last_init_list_comma;
4519 /* Parse a braced initializer list. TYPE is the type specified for a
4520 compound literal, and NULL_TREE for other initializers and for
4521 nested braced lists. NESTED_P is true for nested braced lists,
4522 false for the list of a compound literal or the list that is the
4523 top-level initializer in a declaration. */
4525 static struct c_expr
4526 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4527 struct obstack *outer_obstack)
4529 struct c_expr ret;
4530 struct obstack braced_init_obstack;
4531 location_t brace_loc = c_parser_peek_token (parser)->location;
4532 gcc_obstack_init (&braced_init_obstack);
4533 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4534 matching_braces braces;
4535 braces.consume_open (parser);
4536 if (nested_p)
4538 finish_implicit_inits (brace_loc, outer_obstack);
4539 push_init_level (brace_loc, 0, &braced_init_obstack);
4541 else
4542 really_start_incremental_init (type);
4543 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4545 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4547 else
4549 /* Parse a non-empty initializer list, possibly with a trailing
4550 comma. */
4551 while (true)
4553 c_parser_initelt (parser, &braced_init_obstack);
4554 if (parser->error)
4555 break;
4556 if (c_parser_next_token_is (parser, CPP_COMMA))
4558 last_init_list_comma = c_parser_peek_token (parser)->location;
4559 c_parser_consume_token (parser);
4561 else
4562 break;
4563 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4564 break;
4567 c_token *next_tok = c_parser_peek_token (parser);
4568 if (next_tok->type != CPP_CLOSE_BRACE)
4570 ret.value = error_mark_node;
4571 ret.original_code = ERROR_MARK;
4572 ret.original_type = NULL;
4573 braces.skip_until_found_close (parser);
4574 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4575 obstack_free (&braced_init_obstack, NULL);
4576 return ret;
4578 location_t close_loc = next_tok->location;
4579 c_parser_consume_token (parser);
4580 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4581 obstack_free (&braced_init_obstack, NULL);
4582 set_c_expr_source_range (&ret, brace_loc, close_loc);
4583 return ret;
4586 /* Parse a nested initializer, including designators. */
4588 static void
4589 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4591 /* Parse any designator or designator list. A single array
4592 designator may have the subsequent "=" omitted in GNU C, but a
4593 longer list or a structure member designator may not. */
4594 if (c_parser_next_token_is (parser, CPP_NAME)
4595 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4597 /* Old-style structure member designator. */
4598 set_init_label (c_parser_peek_token (parser)->location,
4599 c_parser_peek_token (parser)->value,
4600 c_parser_peek_token (parser)->location,
4601 braced_init_obstack);
4602 /* Use the colon as the error location. */
4603 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4604 "obsolete use of designated initializer with %<:%>");
4605 c_parser_consume_token (parser);
4606 c_parser_consume_token (parser);
4608 else
4610 /* des_seen is 0 if there have been no designators, 1 if there
4611 has been a single array designator and 2 otherwise. */
4612 int des_seen = 0;
4613 /* Location of a designator. */
4614 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4615 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4616 || c_parser_next_token_is (parser, CPP_DOT))
4618 int des_prev = des_seen;
4619 if (!des_seen)
4620 des_loc = c_parser_peek_token (parser)->location;
4621 if (des_seen < 2)
4622 des_seen++;
4623 if (c_parser_next_token_is (parser, CPP_DOT))
4625 des_seen = 2;
4626 c_parser_consume_token (parser);
4627 if (c_parser_next_token_is (parser, CPP_NAME))
4629 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4630 c_parser_peek_token (parser)->location,
4631 braced_init_obstack);
4632 c_parser_consume_token (parser);
4634 else
4636 struct c_expr init;
4637 init.value = error_mark_node;
4638 init.original_code = ERROR_MARK;
4639 init.original_type = NULL;
4640 c_parser_error (parser, "expected identifier");
4641 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4642 process_init_element (input_location, init, false,
4643 braced_init_obstack);
4644 return;
4647 else
4649 tree first, second;
4650 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4651 location_t array_index_loc = UNKNOWN_LOCATION;
4652 /* ??? Following the old parser, [ objc-receiver
4653 objc-message-args ] is accepted as an initializer,
4654 being distinguished from a designator by what follows
4655 the first assignment expression inside the square
4656 brackets, but after a first array designator a
4657 subsequent square bracket is for Objective-C taken to
4658 start an expression, using the obsolete form of
4659 designated initializer without '=', rather than
4660 possibly being a second level of designation: in LALR
4661 terms, the '[' is shifted rather than reducing
4662 designator to designator-list. */
4663 if (des_prev == 1 && c_dialect_objc ())
4665 des_seen = des_prev;
4666 break;
4668 if (des_prev == 0 && c_dialect_objc ())
4670 /* This might be an array designator or an
4671 Objective-C message expression. If the former,
4672 continue parsing here; if the latter, parse the
4673 remainder of the initializer given the starting
4674 primary-expression. ??? It might make sense to
4675 distinguish when des_prev == 1 as well; see
4676 previous comment. */
4677 tree rec, args;
4678 struct c_expr mexpr;
4679 c_parser_consume_token (parser);
4680 if (c_parser_peek_token (parser)->type == CPP_NAME
4681 && ((c_parser_peek_token (parser)->id_kind
4682 == C_ID_TYPENAME)
4683 || (c_parser_peek_token (parser)->id_kind
4684 == C_ID_CLASSNAME)))
4686 /* Type name receiver. */
4687 tree id = c_parser_peek_token (parser)->value;
4688 c_parser_consume_token (parser);
4689 rec = objc_get_class_reference (id);
4690 goto parse_message_args;
4692 first = c_parser_expr_no_commas (parser, NULL).value;
4693 mark_exp_read (first);
4694 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4695 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4696 goto array_desig_after_first;
4697 /* Expression receiver. So far only one part
4698 without commas has been parsed; there might be
4699 more of the expression. */
4700 rec = first;
4701 while (c_parser_next_token_is (parser, CPP_COMMA))
4703 struct c_expr next;
4704 location_t comma_loc, exp_loc;
4705 comma_loc = c_parser_peek_token (parser)->location;
4706 c_parser_consume_token (parser);
4707 exp_loc = c_parser_peek_token (parser)->location;
4708 next = c_parser_expr_no_commas (parser, NULL);
4709 next = convert_lvalue_to_rvalue (exp_loc, next,
4710 true, true);
4711 rec = build_compound_expr (comma_loc, rec, next.value);
4713 parse_message_args:
4714 /* Now parse the objc-message-args. */
4715 args = c_parser_objc_message_args (parser);
4716 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4717 "expected %<]%>");
4718 mexpr.value
4719 = objc_build_message_expr (rec, args);
4720 mexpr.original_code = ERROR_MARK;
4721 mexpr.original_type = NULL;
4722 /* Now parse and process the remainder of the
4723 initializer, starting with this message
4724 expression as a primary-expression. */
4725 c_parser_initval (parser, &mexpr, braced_init_obstack);
4726 return;
4728 c_parser_consume_token (parser);
4729 array_index_loc = c_parser_peek_token (parser)->location;
4730 first = c_parser_expr_no_commas (parser, NULL).value;
4731 mark_exp_read (first);
4732 array_desig_after_first:
4733 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4735 ellipsis_loc = c_parser_peek_token (parser)->location;
4736 c_parser_consume_token (parser);
4737 second = c_parser_expr_no_commas (parser, NULL).value;
4738 mark_exp_read (second);
4740 else
4741 second = NULL_TREE;
4742 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4744 c_parser_consume_token (parser);
4745 set_init_index (array_index_loc, first, second,
4746 braced_init_obstack);
4747 if (second)
4748 pedwarn (ellipsis_loc, OPT_Wpedantic,
4749 "ISO C forbids specifying range of elements to initialize");
4751 else
4752 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4753 "expected %<]%>");
4756 if (des_seen >= 1)
4758 if (c_parser_next_token_is (parser, CPP_EQ))
4760 pedwarn_c90 (des_loc, OPT_Wpedantic,
4761 "ISO C90 forbids specifying subobject "
4762 "to initialize");
4763 c_parser_consume_token (parser);
4765 else
4767 if (des_seen == 1)
4768 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4769 "obsolete use of designated initializer without %<=%>");
4770 else
4772 struct c_expr init;
4773 init.value = error_mark_node;
4774 init.original_code = ERROR_MARK;
4775 init.original_type = NULL;
4776 c_parser_error (parser, "expected %<=%>");
4777 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4778 process_init_element (input_location, init, false,
4779 braced_init_obstack);
4780 return;
4785 c_parser_initval (parser, NULL, braced_init_obstack);
4788 /* Parse a nested initializer; as c_parser_initializer but parses
4789 initializers within braced lists, after any designators have been
4790 applied. If AFTER is not NULL then it is an Objective-C message
4791 expression which is the primary-expression starting the
4792 initializer. */
4794 static void
4795 c_parser_initval (c_parser *parser, struct c_expr *after,
4796 struct obstack * braced_init_obstack)
4798 struct c_expr init;
4799 gcc_assert (!after || c_dialect_objc ());
4800 location_t loc = c_parser_peek_token (parser)->location;
4802 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4803 init = c_parser_braced_init (parser, NULL_TREE, true,
4804 braced_init_obstack);
4805 else
4807 init = c_parser_expr_no_commas (parser, after);
4808 if (init.value != NULL_TREE
4809 && TREE_CODE (init.value) != STRING_CST
4810 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4811 init = convert_lvalue_to_rvalue (loc, init, true, true);
4813 process_init_element (loc, init, false, braced_init_obstack);
4816 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4817 C99 6.8.2, C11 6.8.2).
4819 compound-statement:
4820 { block-item-list[opt] }
4821 { label-declarations block-item-list }
4823 block-item-list:
4824 block-item
4825 block-item-list block-item
4827 block-item:
4828 nested-declaration
4829 statement
4831 nested-declaration:
4832 declaration
4834 GNU extensions:
4836 compound-statement:
4837 { label-declarations block-item-list }
4839 nested-declaration:
4840 __extension__ nested-declaration
4841 nested-function-definition
4843 label-declarations:
4844 label-declaration
4845 label-declarations label-declaration
4847 label-declaration:
4848 __label__ identifier-list ;
4850 Allowing the mixing of declarations and code is new in C99. The
4851 GNU syntax also permits (not shown above) labels at the end of
4852 compound statements, which yield an error. We don't allow labels
4853 on declarations; this might seem like a natural extension, but
4854 there would be a conflict between attributes on the label and
4855 prefix attributes on the declaration. ??? The syntax follows the
4856 old parser in requiring something after label declarations.
4857 Although they are erroneous if the labels declared aren't defined,
4858 is it useful for the syntax to be this way?
4860 OpenACC:
4862 block-item:
4863 openacc-directive
4865 openacc-directive:
4866 update-directive
4868 OpenMP:
4870 block-item:
4871 openmp-directive
4873 openmp-directive:
4874 barrier-directive
4875 flush-directive
4876 taskwait-directive
4877 taskyield-directive
4878 cancel-directive
4879 cancellation-point-directive */
4881 static tree
4882 c_parser_compound_statement (c_parser *parser)
4884 tree stmt;
4885 location_t brace_loc;
4886 brace_loc = c_parser_peek_token (parser)->location;
4887 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4889 /* Ensure a scope is entered and left anyway to avoid confusion
4890 if we have just prepared to enter a function body. */
4891 stmt = c_begin_compound_stmt (true);
4892 c_end_compound_stmt (brace_loc, stmt, true);
4893 return error_mark_node;
4895 stmt = c_begin_compound_stmt (true);
4896 c_parser_compound_statement_nostart (parser);
4898 return c_end_compound_stmt (brace_loc, stmt, true);
4901 /* Parse a compound statement except for the opening brace. This is
4902 used for parsing both compound statements and statement expressions
4903 (which follow different paths to handling the opening). */
4905 static void
4906 c_parser_compound_statement_nostart (c_parser *parser)
4908 bool last_stmt = false;
4909 bool last_label = false;
4910 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4911 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4912 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4914 c_parser_consume_token (parser);
4915 return;
4917 mark_valid_location_for_stdc_pragma (true);
4918 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4920 /* Read zero or more forward-declarations for labels that nested
4921 functions can jump to. */
4922 mark_valid_location_for_stdc_pragma (false);
4923 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4925 label_loc = c_parser_peek_token (parser)->location;
4926 c_parser_consume_token (parser);
4927 /* Any identifiers, including those declared as type names,
4928 are OK here. */
4929 while (true)
4931 tree label;
4932 if (c_parser_next_token_is_not (parser, CPP_NAME))
4934 c_parser_error (parser, "expected identifier");
4935 break;
4937 label
4938 = declare_label (c_parser_peek_token (parser)->value);
4939 C_DECLARED_LABEL_FLAG (label) = 1;
4940 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4941 c_parser_consume_token (parser);
4942 if (c_parser_next_token_is (parser, CPP_COMMA))
4943 c_parser_consume_token (parser);
4944 else
4945 break;
4947 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4949 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4951 /* We must now have at least one statement, label or declaration. */
4952 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4954 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4955 c_parser_error (parser, "expected declaration or statement");
4956 c_parser_consume_token (parser);
4957 return;
4959 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4961 location_t loc = c_parser_peek_token (parser)->location;
4962 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4963 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4964 || (c_parser_next_token_is (parser, CPP_NAME)
4965 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4967 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4968 label_loc = c_parser_peek_2nd_token (parser)->location;
4969 else
4970 label_loc = c_parser_peek_token (parser)->location;
4971 last_label = true;
4972 last_stmt = false;
4973 mark_valid_location_for_stdc_pragma (false);
4974 c_parser_label (parser);
4976 else if (!last_label
4977 && c_parser_next_tokens_start_declaration (parser))
4979 last_label = false;
4980 mark_valid_location_for_stdc_pragma (false);
4981 bool fallthru_attr_p = false;
4982 c_parser_declaration_or_fndef (parser, true, true, true, true,
4983 true, NULL, vNULL, NULL,
4984 &fallthru_attr_p);
4985 if (last_stmt && !fallthru_attr_p)
4986 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4987 "ISO C90 forbids mixed declarations and code");
4988 last_stmt = fallthru_attr_p;
4990 else if (!last_label
4991 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4993 /* __extension__ can start a declaration, but is also an
4994 unary operator that can start an expression. Consume all
4995 but the last of a possible series of __extension__ to
4996 determine which. */
4997 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4998 && (c_parser_peek_2nd_token (parser)->keyword
4999 == RID_EXTENSION))
5000 c_parser_consume_token (parser);
5001 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5003 int ext;
5004 ext = disable_extension_diagnostics ();
5005 c_parser_consume_token (parser);
5006 last_label = false;
5007 mark_valid_location_for_stdc_pragma (false);
5008 c_parser_declaration_or_fndef (parser, true, true, true, true,
5009 true, NULL, vNULL);
5010 /* Following the old parser, __extension__ does not
5011 disable this diagnostic. */
5012 restore_extension_diagnostics (ext);
5013 if (last_stmt)
5014 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5015 "ISO C90 forbids mixed declarations and code");
5016 last_stmt = false;
5018 else
5019 goto statement;
5021 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5023 /* External pragmas, and some omp pragmas, are not associated
5024 with regular c code, and so are not to be considered statements
5025 syntactically. This ensures that the user doesn't put them
5026 places that would turn into syntax errors if the directive
5027 were ignored. */
5028 if (c_parser_pragma (parser,
5029 last_label ? pragma_stmt : pragma_compound,
5030 NULL))
5031 last_label = false, last_stmt = true;
5033 else if (c_parser_next_token_is (parser, CPP_EOF))
5035 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5036 c_parser_error (parser, "expected declaration or statement");
5037 return;
5039 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5041 if (parser->in_if_block)
5043 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5044 error_at (loc, "expected %<}%> before %<else%>");
5045 return;
5047 else
5049 error_at (loc, "%<else%> without a previous %<if%>");
5050 c_parser_consume_token (parser);
5051 continue;
5054 else
5056 statement:
5057 last_label = false;
5058 last_stmt = true;
5059 mark_valid_location_for_stdc_pragma (false);
5060 c_parser_statement_after_labels (parser, NULL);
5063 parser->error = false;
5065 if (last_label)
5066 error_at (label_loc, "label at end of compound statement");
5067 c_parser_consume_token (parser);
5068 /* Restore the value we started with. */
5069 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5072 /* Parse all consecutive labels. */
5074 static void
5075 c_parser_all_labels (c_parser *parser)
5077 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5078 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5079 || (c_parser_next_token_is (parser, CPP_NAME)
5080 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5081 c_parser_label (parser);
5084 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5086 label:
5087 identifier : attributes[opt]
5088 case constant-expression :
5089 default :
5091 GNU extensions:
5093 label:
5094 case constant-expression ... constant-expression :
5096 The use of attributes on labels is a GNU extension. The syntax in
5097 GNU C accepts any expressions without commas, non-constant
5098 expressions being rejected later. */
5100 static void
5101 c_parser_label (c_parser *parser)
5103 location_t loc1 = c_parser_peek_token (parser)->location;
5104 tree label = NULL_TREE;
5106 /* Remember whether this case or a user-defined label is allowed to fall
5107 through to. */
5108 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5110 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5112 tree exp1, exp2;
5113 c_parser_consume_token (parser);
5114 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5115 if (c_parser_next_token_is (parser, CPP_COLON))
5117 c_parser_consume_token (parser);
5118 label = do_case (loc1, exp1, NULL_TREE);
5120 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5122 c_parser_consume_token (parser);
5123 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5124 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5125 label = do_case (loc1, exp1, exp2);
5127 else
5128 c_parser_error (parser, "expected %<:%> or %<...%>");
5130 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5132 c_parser_consume_token (parser);
5133 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5134 label = do_case (loc1, NULL_TREE, NULL_TREE);
5136 else
5138 tree name = c_parser_peek_token (parser)->value;
5139 tree tlab;
5140 tree attrs;
5141 location_t loc2 = c_parser_peek_token (parser)->location;
5142 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5143 c_parser_consume_token (parser);
5144 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5145 c_parser_consume_token (parser);
5146 attrs = c_parser_attributes (parser);
5147 tlab = define_label (loc2, name);
5148 if (tlab)
5150 decl_attributes (&tlab, attrs, 0);
5151 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5154 if (label)
5156 if (TREE_CODE (label) == LABEL_EXPR)
5157 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5158 else
5159 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5161 /* Allow '__attribute__((fallthrough));'. */
5162 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5164 location_t loc = c_parser_peek_token (parser)->location;
5165 tree attrs = c_parser_attributes (parser);
5166 if (attribute_fallthrough_p (attrs))
5168 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5170 tree fn = build_call_expr_internal_loc (loc,
5171 IFN_FALLTHROUGH,
5172 void_type_node, 0);
5173 add_stmt (fn);
5175 else
5176 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5177 "not followed by %<;%>");
5179 else if (attrs != NULL_TREE)
5180 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5181 " can be applied to a null statement");
5183 if (c_parser_next_tokens_start_declaration (parser))
5185 error_at (c_parser_peek_token (parser)->location,
5186 "a label can only be part of a statement and "
5187 "a declaration is not a statement");
5188 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5189 /*static_assert_ok*/ true,
5190 /*empty_ok*/ true, /*nested*/ true,
5191 /*start_attr_ok*/ true, NULL,
5192 vNULL);
5197 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5199 statement:
5200 labeled-statement
5201 compound-statement
5202 expression-statement
5203 selection-statement
5204 iteration-statement
5205 jump-statement
5207 labeled-statement:
5208 label statement
5210 expression-statement:
5211 expression[opt] ;
5213 selection-statement:
5214 if-statement
5215 switch-statement
5217 iteration-statement:
5218 while-statement
5219 do-statement
5220 for-statement
5222 jump-statement:
5223 goto identifier ;
5224 continue ;
5225 break ;
5226 return expression[opt] ;
5228 GNU extensions:
5230 statement:
5231 asm-statement
5233 jump-statement:
5234 goto * expression ;
5236 expression-statement:
5237 attributes ;
5239 Objective-C:
5241 statement:
5242 objc-throw-statement
5243 objc-try-catch-statement
5244 objc-synchronized-statement
5246 objc-throw-statement:
5247 @throw expression ;
5248 @throw ;
5250 OpenACC:
5252 statement:
5253 openacc-construct
5255 openacc-construct:
5256 parallel-construct
5257 kernels-construct
5258 data-construct
5259 loop-construct
5261 parallel-construct:
5262 parallel-directive structured-block
5264 kernels-construct:
5265 kernels-directive structured-block
5267 data-construct:
5268 data-directive structured-block
5270 loop-construct:
5271 loop-directive structured-block
5273 OpenMP:
5275 statement:
5276 openmp-construct
5278 openmp-construct:
5279 parallel-construct
5280 for-construct
5281 simd-construct
5282 for-simd-construct
5283 sections-construct
5284 single-construct
5285 parallel-for-construct
5286 parallel-for-simd-construct
5287 parallel-sections-construct
5288 master-construct
5289 critical-construct
5290 atomic-construct
5291 ordered-construct
5293 parallel-construct:
5294 parallel-directive structured-block
5296 for-construct:
5297 for-directive iteration-statement
5299 simd-construct:
5300 simd-directive iteration-statements
5302 for-simd-construct:
5303 for-simd-directive iteration-statements
5305 sections-construct:
5306 sections-directive section-scope
5308 single-construct:
5309 single-directive structured-block
5311 parallel-for-construct:
5312 parallel-for-directive iteration-statement
5314 parallel-for-simd-construct:
5315 parallel-for-simd-directive iteration-statement
5317 parallel-sections-construct:
5318 parallel-sections-directive section-scope
5320 master-construct:
5321 master-directive structured-block
5323 critical-construct:
5324 critical-directive structured-block
5326 atomic-construct:
5327 atomic-directive expression-statement
5329 ordered-construct:
5330 ordered-directive structured-block
5332 Transactional Memory:
5334 statement:
5335 transaction-statement
5336 transaction-cancel-statement
5338 IF_P is used to track whether there's a (possibly labeled) if statement
5339 which is not enclosed in braces and has an else clause. This is used to
5340 implement -Wparentheses. */
5342 static void
5343 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5345 c_parser_all_labels (parser);
5346 if (loc_after_labels)
5347 *loc_after_labels = c_parser_peek_token (parser)->location;
5348 c_parser_statement_after_labels (parser, if_p, NULL);
5351 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5352 of if-else-if conditions.
5354 IF_P is used to track whether there's a (possibly labeled) if statement
5355 which is not enclosed in braces and has an else clause. This is used to
5356 implement -Wparentheses. */
5358 static void
5359 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5360 vec<tree> *chain)
5362 location_t loc = c_parser_peek_token (parser)->location;
5363 tree stmt = NULL_TREE;
5364 bool in_if_block = parser->in_if_block;
5365 parser->in_if_block = false;
5366 if (if_p != NULL)
5367 *if_p = false;
5368 switch (c_parser_peek_token (parser)->type)
5370 case CPP_OPEN_BRACE:
5371 add_stmt (c_parser_compound_statement (parser));
5372 break;
5373 case CPP_KEYWORD:
5374 switch (c_parser_peek_token (parser)->keyword)
5376 case RID_IF:
5377 c_parser_if_statement (parser, if_p, chain);
5378 break;
5379 case RID_SWITCH:
5380 c_parser_switch_statement (parser, if_p);
5381 break;
5382 case RID_WHILE:
5383 c_parser_while_statement (parser, false, if_p);
5384 break;
5385 case RID_DO:
5386 c_parser_do_statement (parser, false);
5387 break;
5388 case RID_FOR:
5389 c_parser_for_statement (parser, false, if_p);
5390 break;
5391 case RID_GOTO:
5392 c_parser_consume_token (parser);
5393 if (c_parser_next_token_is (parser, CPP_NAME))
5395 stmt = c_finish_goto_label (loc,
5396 c_parser_peek_token (parser)->value);
5397 c_parser_consume_token (parser);
5399 else if (c_parser_next_token_is (parser, CPP_MULT))
5401 struct c_expr val;
5403 c_parser_consume_token (parser);
5404 val = c_parser_expression (parser);
5405 val = convert_lvalue_to_rvalue (loc, val, false, true);
5406 stmt = c_finish_goto_ptr (loc, val.value);
5408 else
5409 c_parser_error (parser, "expected identifier or %<*%>");
5410 goto expect_semicolon;
5411 case RID_CONTINUE:
5412 c_parser_consume_token (parser);
5413 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5414 goto expect_semicolon;
5415 case RID_BREAK:
5416 c_parser_consume_token (parser);
5417 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5418 goto expect_semicolon;
5419 case RID_RETURN:
5420 c_parser_consume_token (parser);
5421 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5423 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5424 c_parser_consume_token (parser);
5426 else
5428 location_t xloc = c_parser_peek_token (parser)->location;
5429 struct c_expr expr = c_parser_expression_conv (parser);
5430 mark_exp_read (expr.value);
5431 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5432 expr.value, expr.original_type);
5433 goto expect_semicolon;
5435 break;
5436 case RID_ASM:
5437 stmt = c_parser_asm_statement (parser);
5438 break;
5439 case RID_TRANSACTION_ATOMIC:
5440 case RID_TRANSACTION_RELAXED:
5441 stmt = c_parser_transaction (parser,
5442 c_parser_peek_token (parser)->keyword);
5443 break;
5444 case RID_TRANSACTION_CANCEL:
5445 stmt = c_parser_transaction_cancel (parser);
5446 goto expect_semicolon;
5447 case RID_AT_THROW:
5448 gcc_assert (c_dialect_objc ());
5449 c_parser_consume_token (parser);
5450 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5452 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5453 c_parser_consume_token (parser);
5455 else
5457 struct c_expr expr = c_parser_expression (parser);
5458 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5459 expr.value = c_fully_fold (expr.value, false, NULL);
5460 stmt = objc_build_throw_stmt (loc, expr.value);
5461 goto expect_semicolon;
5463 break;
5464 case RID_AT_TRY:
5465 gcc_assert (c_dialect_objc ());
5466 c_parser_objc_try_catch_finally_statement (parser);
5467 break;
5468 case RID_AT_SYNCHRONIZED:
5469 gcc_assert (c_dialect_objc ());
5470 c_parser_objc_synchronized_statement (parser);
5471 break;
5472 case RID_ATTRIBUTE:
5474 /* Allow '__attribute__((fallthrough));'. */
5475 tree attrs = c_parser_attributes (parser);
5476 if (attribute_fallthrough_p (attrs))
5478 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5480 tree fn = build_call_expr_internal_loc (loc,
5481 IFN_FALLTHROUGH,
5482 void_type_node, 0);
5483 add_stmt (fn);
5484 /* Eat the ';'. */
5485 c_parser_consume_token (parser);
5487 else
5488 warning_at (loc, OPT_Wattributes,
5489 "%<fallthrough%> attribute not followed "
5490 "by %<;%>");
5492 else if (attrs != NULL_TREE)
5493 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5494 " can be applied to a null statement");
5495 break;
5497 default:
5498 goto expr_stmt;
5500 break;
5501 case CPP_SEMICOLON:
5502 c_parser_consume_token (parser);
5503 break;
5504 case CPP_CLOSE_PAREN:
5505 case CPP_CLOSE_SQUARE:
5506 /* Avoid infinite loop in error recovery:
5507 c_parser_skip_until_found stops at a closing nesting
5508 delimiter without consuming it, but here we need to consume
5509 it to proceed further. */
5510 c_parser_error (parser, "expected statement");
5511 c_parser_consume_token (parser);
5512 break;
5513 case CPP_PRAGMA:
5514 c_parser_pragma (parser, pragma_stmt, if_p);
5515 break;
5516 default:
5517 expr_stmt:
5518 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5519 expect_semicolon:
5520 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5521 break;
5523 /* Two cases cannot and do not have line numbers associated: If stmt
5524 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5525 cannot hold line numbers. But that's OK because the statement
5526 will either be changed to a MODIFY_EXPR during gimplification of
5527 the statement expr, or discarded. If stmt was compound, but
5528 without new variables, we will have skipped the creation of a
5529 BIND and will have a bare STATEMENT_LIST. But that's OK because
5530 (recursively) all of the component statements should already have
5531 line numbers assigned. ??? Can we discard no-op statements
5532 earlier? */
5533 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5534 protected_set_expr_location (stmt, loc);
5536 parser->in_if_block = in_if_block;
5539 /* Parse the condition from an if, do, while or for statements. */
5541 static tree
5542 c_parser_condition (c_parser *parser)
5544 location_t loc = c_parser_peek_token (parser)->location;
5545 tree cond;
5546 cond = c_parser_expression_conv (parser).value;
5547 cond = c_objc_common_truthvalue_conversion (loc, cond);
5548 cond = c_fully_fold (cond, false, NULL);
5549 if (warn_sequence_point)
5550 verify_sequence_points (cond);
5551 return cond;
5554 /* Parse a parenthesized condition from an if, do or while statement.
5556 condition:
5557 ( expression )
5559 static tree
5560 c_parser_paren_condition (c_parser *parser)
5562 tree cond;
5563 matching_parens parens;
5564 if (!parens.require_open (parser))
5565 return error_mark_node;
5566 cond = c_parser_condition (parser);
5567 parens.skip_until_found_close (parser);
5568 return cond;
5571 /* Parse a statement which is a block in C99.
5573 IF_P is used to track whether there's a (possibly labeled) if statement
5574 which is not enclosed in braces and has an else clause. This is used to
5575 implement -Wparentheses. */
5577 static tree
5578 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5579 location_t *loc_after_labels)
5581 tree block = c_begin_compound_stmt (flag_isoc99);
5582 location_t loc = c_parser_peek_token (parser)->location;
5583 c_parser_statement (parser, if_p, loc_after_labels);
5584 return c_end_compound_stmt (loc, block, flag_isoc99);
5587 /* Parse the body of an if statement. This is just parsing a
5588 statement but (a) it is a block in C99, (b) we track whether the
5589 body is an if statement for the sake of -Wparentheses warnings, (c)
5590 we handle an empty body specially for the sake of -Wempty-body
5591 warnings, and (d) we call parser_compound_statement directly
5592 because c_parser_statement_after_labels resets
5593 parser->in_if_block.
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_if_body (c_parser *parser, bool *if_p,
5601 const token_indent_info &if_tinfo)
5603 tree block = c_begin_compound_stmt (flag_isoc99);
5604 location_t body_loc = c_parser_peek_token (parser)->location;
5605 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5606 token_indent_info body_tinfo
5607 = get_token_indent_info (c_parser_peek_token (parser));
5609 c_parser_all_labels (parser);
5610 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5612 location_t loc = c_parser_peek_token (parser)->location;
5613 add_stmt (build_empty_stmt (loc));
5614 c_parser_consume_token (parser);
5615 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5616 warning_at (loc, OPT_Wempty_body,
5617 "suggest braces around empty body in an %<if%> statement");
5619 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5620 add_stmt (c_parser_compound_statement (parser));
5621 else
5623 body_loc_after_labels = c_parser_peek_token (parser)->location;
5624 c_parser_statement_after_labels (parser, if_p);
5627 token_indent_info next_tinfo
5628 = get_token_indent_info (c_parser_peek_token (parser));
5629 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5630 if (body_loc_after_labels != UNKNOWN_LOCATION
5631 && next_tinfo.type != CPP_SEMICOLON)
5632 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5633 if_tinfo.location, RID_IF);
5635 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5638 /* Parse the else body of an if statement. This is just parsing a
5639 statement but (a) it is a block in C99, (b) we handle an empty body
5640 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5641 of if-else-if conditions. */
5643 static tree
5644 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5645 vec<tree> *chain)
5647 location_t body_loc = c_parser_peek_token (parser)->location;
5648 tree block = c_begin_compound_stmt (flag_isoc99);
5649 token_indent_info body_tinfo
5650 = get_token_indent_info (c_parser_peek_token (parser));
5651 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5653 c_parser_all_labels (parser);
5654 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5656 location_t loc = c_parser_peek_token (parser)->location;
5657 warning_at (loc,
5658 OPT_Wempty_body,
5659 "suggest braces around empty body in an %<else%> statement");
5660 add_stmt (build_empty_stmt (loc));
5661 c_parser_consume_token (parser);
5663 else
5665 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5666 body_loc_after_labels = c_parser_peek_token (parser)->location;
5667 c_parser_statement_after_labels (parser, NULL, chain);
5670 token_indent_info next_tinfo
5671 = get_token_indent_info (c_parser_peek_token (parser));
5672 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5673 if (body_loc_after_labels != UNKNOWN_LOCATION
5674 && next_tinfo.type != CPP_SEMICOLON)
5675 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5676 else_tinfo.location, RID_ELSE);
5678 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5681 /* We might need to reclassify any previously-lexed identifier, e.g.
5682 when we've left a for loop with an if-statement without else in the
5683 body - we might have used a wrong scope for the token. See PR67784. */
5685 static void
5686 c_parser_maybe_reclassify_token (c_parser *parser)
5688 if (c_parser_next_token_is (parser, CPP_NAME))
5690 c_token *token = c_parser_peek_token (parser);
5692 if (token->id_kind != C_ID_CLASSNAME)
5694 tree decl = lookup_name (token->value);
5696 token->id_kind = C_ID_ID;
5697 if (decl)
5699 if (TREE_CODE (decl) == TYPE_DECL)
5700 token->id_kind = C_ID_TYPENAME;
5702 else if (c_dialect_objc ())
5704 tree objc_interface_decl = objc_is_class_name (token->value);
5705 /* Objective-C class names are in the same namespace as
5706 variables and typedefs, and hence are shadowed by local
5707 declarations. */
5708 if (objc_interface_decl)
5710 token->value = objc_interface_decl;
5711 token->id_kind = C_ID_CLASSNAME;
5718 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5720 if-statement:
5721 if ( expression ) statement
5722 if ( expression ) statement else statement
5724 CHAIN is a vector of if-else-if conditions.
5725 IF_P is used to track whether there's a (possibly labeled) if statement
5726 which is not enclosed in braces and has an else clause. This is used to
5727 implement -Wparentheses. */
5729 static void
5730 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5732 tree block;
5733 location_t loc;
5734 tree cond;
5735 bool nested_if = false;
5736 tree first_body, second_body;
5737 bool in_if_block;
5739 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5740 token_indent_info if_tinfo
5741 = get_token_indent_info (c_parser_peek_token (parser));
5742 c_parser_consume_token (parser);
5743 block = c_begin_compound_stmt (flag_isoc99);
5744 loc = c_parser_peek_token (parser)->location;
5745 cond = c_parser_paren_condition (parser);
5746 in_if_block = parser->in_if_block;
5747 parser->in_if_block = true;
5748 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5749 parser->in_if_block = in_if_block;
5751 if (warn_duplicated_cond)
5752 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5754 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5756 token_indent_info else_tinfo
5757 = get_token_indent_info (c_parser_peek_token (parser));
5758 c_parser_consume_token (parser);
5759 if (warn_duplicated_cond)
5761 if (c_parser_next_token_is_keyword (parser, RID_IF)
5762 && chain == NULL)
5764 /* We've got "if (COND) else if (COND2)". Start the
5765 condition chain and add COND as the first element. */
5766 chain = new vec<tree> ();
5767 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5768 chain->safe_push (cond);
5770 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5772 /* This is if-else without subsequent if. Zap the condition
5773 chain; we would have already warned at this point. */
5774 delete chain;
5775 chain = NULL;
5778 second_body = c_parser_else_body (parser, else_tinfo, chain);
5779 /* Set IF_P to true to indicate that this if statement has an
5780 else clause. This may trigger the Wparentheses warning
5781 below when we get back up to the parent if statement. */
5782 if (if_p != NULL)
5783 *if_p = true;
5785 else
5787 second_body = NULL_TREE;
5789 /* Diagnose an ambiguous else if if-then-else is nested inside
5790 if-then. */
5791 if (nested_if)
5792 warning_at (loc, OPT_Wdangling_else,
5793 "suggest explicit braces to avoid ambiguous %<else%>");
5795 if (warn_duplicated_cond)
5797 /* This if statement does not have an else clause. We don't
5798 need the condition chain anymore. */
5799 delete chain;
5800 chain = NULL;
5803 c_finish_if_stmt (loc, cond, first_body, second_body);
5804 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5806 c_parser_maybe_reclassify_token (parser);
5809 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5811 switch-statement:
5812 switch (expression) statement
5815 static void
5816 c_parser_switch_statement (c_parser *parser, bool *if_p)
5818 struct c_expr ce;
5819 tree block, expr, body, save_break;
5820 location_t switch_loc = c_parser_peek_token (parser)->location;
5821 location_t switch_cond_loc;
5822 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5823 c_parser_consume_token (parser);
5824 block = c_begin_compound_stmt (flag_isoc99);
5825 bool explicit_cast_p = false;
5826 matching_parens parens;
5827 if (parens.require_open (parser))
5829 switch_cond_loc = c_parser_peek_token (parser)->location;
5830 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5831 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5832 explicit_cast_p = true;
5833 ce = c_parser_expression (parser);
5834 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5835 expr = ce.value;
5836 /* ??? expr has no valid location? */
5837 parens.skip_until_found_close (parser);
5839 else
5841 switch_cond_loc = UNKNOWN_LOCATION;
5842 expr = error_mark_node;
5843 ce.original_type = error_mark_node;
5845 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5846 save_break = c_break_label;
5847 c_break_label = NULL_TREE;
5848 location_t loc_after_labels;
5849 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5850 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5851 location_t next_loc = c_parser_peek_token (parser)->location;
5852 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5853 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5854 RID_SWITCH);
5855 if (c_break_label)
5857 location_t here = c_parser_peek_token (parser)->location;
5858 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5859 SET_EXPR_LOCATION (t, here);
5860 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5861 append_to_statement_list_force (t, &body);
5863 c_finish_case (body, ce.original_type);
5864 c_break_label = save_break;
5865 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5866 c_parser_maybe_reclassify_token (parser);
5869 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5871 while-statement:
5872 while (expression) statement
5874 IF_P is used to track whether there's a (possibly labeled) if statement
5875 which is not enclosed in braces and has an else clause. This is used to
5876 implement -Wparentheses. */
5878 static void
5879 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5881 tree block, cond, body, save_break, save_cont;
5882 location_t loc;
5883 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5884 token_indent_info while_tinfo
5885 = get_token_indent_info (c_parser_peek_token (parser));
5886 c_parser_consume_token (parser);
5887 block = c_begin_compound_stmt (flag_isoc99);
5888 loc = c_parser_peek_token (parser)->location;
5889 cond = c_parser_paren_condition (parser);
5890 if (ivdep && cond != error_mark_node)
5891 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5892 build_int_cst (integer_type_node,
5893 annot_expr_ivdep_kind),
5894 integer_zero_node);
5895 save_break = c_break_label;
5896 c_break_label = NULL_TREE;
5897 save_cont = c_cont_label;
5898 c_cont_label = NULL_TREE;
5900 token_indent_info body_tinfo
5901 = get_token_indent_info (c_parser_peek_token (parser));
5903 location_t loc_after_labels;
5904 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5905 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5906 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5907 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5908 c_parser_maybe_reclassify_token (parser);
5910 token_indent_info next_tinfo
5911 = get_token_indent_info (c_parser_peek_token (parser));
5912 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5914 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5915 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5916 while_tinfo.location, RID_WHILE);
5918 c_break_label = save_break;
5919 c_cont_label = save_cont;
5922 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5924 do-statement:
5925 do statement while ( expression ) ;
5928 static void
5929 c_parser_do_statement (c_parser *parser, bool ivdep)
5931 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5932 location_t loc;
5933 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5934 c_parser_consume_token (parser);
5935 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5936 warning_at (c_parser_peek_token (parser)->location,
5937 OPT_Wempty_body,
5938 "suggest braces around empty body in %<do%> statement");
5939 block = c_begin_compound_stmt (flag_isoc99);
5940 loc = c_parser_peek_token (parser)->location;
5941 save_break = c_break_label;
5942 c_break_label = NULL_TREE;
5943 save_cont = c_cont_label;
5944 c_cont_label = NULL_TREE;
5945 body = c_parser_c99_block_statement (parser, NULL);
5946 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5947 new_break = c_break_label;
5948 c_break_label = save_break;
5949 new_cont = c_cont_label;
5950 c_cont_label = save_cont;
5951 cond = c_parser_paren_condition (parser);
5952 if (ivdep && cond != error_mark_node)
5953 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5954 build_int_cst (integer_type_node,
5955 annot_expr_ivdep_kind),
5956 integer_zero_node);
5957 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5958 c_parser_skip_to_end_of_block_or_statement (parser);
5959 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5960 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5963 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5965 for-statement:
5966 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5967 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5969 The form with a declaration is new in C99.
5971 ??? In accordance with the old parser, the declaration may be a
5972 nested function, which is then rejected in check_for_loop_decls,
5973 but does it make any sense for this to be included in the grammar?
5974 Note in particular that the nested function does not include a
5975 trailing ';', whereas the "declaration" production includes one.
5976 Also, can we reject bad declarations earlier and cheaper than
5977 check_for_loop_decls?
5979 In Objective-C, there are two additional variants:
5981 foreach-statement:
5982 for ( expression in expresssion ) statement
5983 for ( declaration in expression ) statement
5985 This is inconsistent with C, because the second variant is allowed
5986 even if c99 is not enabled.
5988 The rest of the comment documents these Objective-C foreach-statement.
5990 Here is the canonical example of the first variant:
5991 for (object in array) { do something with object }
5992 we call the first expression ("object") the "object_expression" and
5993 the second expression ("array") the "collection_expression".
5994 object_expression must be an lvalue of type "id" (a generic Objective-C
5995 object) because the loop works by assigning to object_expression the
5996 various objects from the collection_expression. collection_expression
5997 must evaluate to something of type "id" which responds to the method
5998 countByEnumeratingWithState:objects:count:.
6000 The canonical example of the second variant is:
6001 for (id object in array) { do something with object }
6002 which is completely equivalent to
6004 id object;
6005 for (object in array) { do something with object }
6007 Note that initizializing 'object' in some way (eg, "for ((object =
6008 xxx) in array) { do something with object }") is possibly
6009 technically valid, but completely pointless as 'object' will be
6010 assigned to something else as soon as the loop starts. We should
6011 most likely reject it (TODO).
6013 The beginning of the Objective-C foreach-statement looks exactly
6014 like the beginning of the for-statement, and we can tell it is a
6015 foreach-statement only because the initial declaration or
6016 expression is terminated by 'in' instead of ';'.
6018 IF_P is used to track whether there's a (possibly labeled) if statement
6019 which is not enclosed in braces and has an else clause. This is used to
6020 implement -Wparentheses. */
6022 static void
6023 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
6025 tree block, cond, incr, save_break, save_cont, body;
6026 /* The following are only used when parsing an ObjC foreach statement. */
6027 tree object_expression;
6028 /* Silence the bogus uninitialized warning. */
6029 tree collection_expression = NULL;
6030 location_t loc = c_parser_peek_token (parser)->location;
6031 location_t for_loc = c_parser_peek_token (parser)->location;
6032 bool is_foreach_statement = false;
6033 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6034 token_indent_info for_tinfo
6035 = get_token_indent_info (c_parser_peek_token (parser));
6036 c_parser_consume_token (parser);
6037 /* Open a compound statement in Objective-C as well, just in case this is
6038 as foreach expression. */
6039 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6040 cond = error_mark_node;
6041 incr = error_mark_node;
6042 matching_parens parens;
6043 if (parens.require_open (parser))
6045 /* Parse the initialization declaration or expression. */
6046 object_expression = error_mark_node;
6047 parser->objc_could_be_foreach_context = c_dialect_objc ();
6048 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6050 parser->objc_could_be_foreach_context = false;
6051 c_parser_consume_token (parser);
6052 c_finish_expr_stmt (loc, NULL_TREE);
6054 else if (c_parser_next_tokens_start_declaration (parser))
6056 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6057 &object_expression, vNULL);
6058 parser->objc_could_be_foreach_context = false;
6060 if (c_parser_next_token_is_keyword (parser, RID_IN))
6062 c_parser_consume_token (parser);
6063 is_foreach_statement = true;
6064 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6065 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6067 else
6068 check_for_loop_decls (for_loc, flag_isoc99);
6070 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6072 /* __extension__ can start a declaration, but is also an
6073 unary operator that can start an expression. Consume all
6074 but the last of a possible series of __extension__ to
6075 determine which. */
6076 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6077 && (c_parser_peek_2nd_token (parser)->keyword
6078 == RID_EXTENSION))
6079 c_parser_consume_token (parser);
6080 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6082 int ext;
6083 ext = disable_extension_diagnostics ();
6084 c_parser_consume_token (parser);
6085 c_parser_declaration_or_fndef (parser, true, true, true, true,
6086 true, &object_expression, vNULL);
6087 parser->objc_could_be_foreach_context = false;
6089 restore_extension_diagnostics (ext);
6090 if (c_parser_next_token_is_keyword (parser, RID_IN))
6092 c_parser_consume_token (parser);
6093 is_foreach_statement = true;
6094 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6095 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6097 else
6098 check_for_loop_decls (for_loc, flag_isoc99);
6100 else
6101 goto init_expr;
6103 else
6105 init_expr:
6107 struct c_expr ce;
6108 tree init_expression;
6109 ce = c_parser_expression (parser);
6110 init_expression = ce.value;
6111 parser->objc_could_be_foreach_context = false;
6112 if (c_parser_next_token_is_keyword (parser, RID_IN))
6114 c_parser_consume_token (parser);
6115 is_foreach_statement = true;
6116 if (! lvalue_p (init_expression))
6117 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6118 object_expression = c_fully_fold (init_expression, false, NULL);
6120 else
6122 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6123 init_expression = ce.value;
6124 c_finish_expr_stmt (loc, init_expression);
6125 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6129 /* Parse the loop condition. In the case of a foreach
6130 statement, there is no loop condition. */
6131 gcc_assert (!parser->objc_could_be_foreach_context);
6132 if (!is_foreach_statement)
6134 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6136 if (ivdep)
6138 c_parser_error (parser, "missing loop condition in loop with "
6139 "%<GCC ivdep%> pragma");
6140 cond = error_mark_node;
6142 else
6144 c_parser_consume_token (parser);
6145 cond = NULL_TREE;
6148 else
6150 cond = c_parser_condition (parser);
6151 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6152 "expected %<;%>");
6154 if (ivdep && cond != error_mark_node)
6155 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6156 build_int_cst (integer_type_node,
6157 annot_expr_ivdep_kind),
6158 integer_zero_node);
6160 /* Parse the increment expression (the third expression in a
6161 for-statement). In the case of a foreach-statement, this is
6162 the expression that follows the 'in'. */
6163 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6165 if (is_foreach_statement)
6167 c_parser_error (parser, "missing collection in fast enumeration");
6168 collection_expression = error_mark_node;
6170 else
6171 incr = c_process_expr_stmt (loc, NULL_TREE);
6173 else
6175 if (is_foreach_statement)
6176 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6177 false, NULL);
6178 else
6180 struct c_expr ce = c_parser_expression (parser);
6181 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6182 incr = c_process_expr_stmt (loc, ce.value);
6185 parens.skip_until_found_close (parser);
6187 save_break = c_break_label;
6188 c_break_label = NULL_TREE;
6189 save_cont = c_cont_label;
6190 c_cont_label = NULL_TREE;
6192 token_indent_info body_tinfo
6193 = get_token_indent_info (c_parser_peek_token (parser));
6195 location_t loc_after_labels;
6196 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6197 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6199 if (is_foreach_statement)
6200 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6201 else
6202 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6203 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6204 c_parser_maybe_reclassify_token (parser);
6206 token_indent_info next_tinfo
6207 = get_token_indent_info (c_parser_peek_token (parser));
6208 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6210 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6211 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6212 for_tinfo.location, RID_FOR);
6214 c_break_label = save_break;
6215 c_cont_label = save_cont;
6218 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6219 statement with inputs, outputs, clobbers, and volatile tag
6220 allowed.
6222 asm-statement:
6223 asm type-qualifier[opt] ( asm-argument ) ;
6224 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6226 asm-argument:
6227 asm-string-literal
6228 asm-string-literal : asm-operands[opt]
6229 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6230 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6232 asm-goto-argument:
6233 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6234 : asm-goto-operands
6236 Qualifiers other than volatile are accepted in the syntax but
6237 warned for. */
6239 static tree
6240 c_parser_asm_statement (c_parser *parser)
6242 tree quals, str, outputs, inputs, clobbers, labels, ret;
6243 bool simple, is_goto;
6244 location_t asm_loc = c_parser_peek_token (parser)->location;
6245 int section, nsections;
6247 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6248 c_parser_consume_token (parser);
6249 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6251 quals = c_parser_peek_token (parser)->value;
6252 c_parser_consume_token (parser);
6254 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6255 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6257 warning_at (c_parser_peek_token (parser)->location,
6259 "%E qualifier ignored on asm",
6260 c_parser_peek_token (parser)->value);
6261 quals = NULL_TREE;
6262 c_parser_consume_token (parser);
6264 else
6265 quals = NULL_TREE;
6267 is_goto = false;
6268 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6270 c_parser_consume_token (parser);
6271 is_goto = true;
6274 /* ??? Follow the C++ parser rather than using the
6275 lex_untranslated_string kludge. */
6276 parser->lex_untranslated_string = true;
6277 ret = NULL;
6279 matching_parens parens;
6280 if (!parens.require_open (parser))
6281 goto error;
6283 str = c_parser_asm_string_literal (parser);
6284 if (str == NULL_TREE)
6285 goto error_close_paren;
6287 simple = true;
6288 outputs = NULL_TREE;
6289 inputs = NULL_TREE;
6290 clobbers = NULL_TREE;
6291 labels = NULL_TREE;
6293 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6294 goto done_asm;
6296 /* Parse each colon-delimited section of operands. */
6297 nsections = 3 + is_goto;
6298 for (section = 0; section < nsections; ++section)
6300 if (!c_parser_require (parser, CPP_COLON,
6301 is_goto
6302 ? G_("expected %<:%>")
6303 : G_("expected %<:%> or %<)%>"),
6304 UNKNOWN_LOCATION, is_goto))
6305 goto error_close_paren;
6307 /* Once past any colon, we're no longer a simple asm. */
6308 simple = false;
6310 if ((!c_parser_next_token_is (parser, CPP_COLON)
6311 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6312 || section == 3)
6313 switch (section)
6315 case 0:
6316 /* For asm goto, we don't allow output operands, but reserve
6317 the slot for a future extension that does allow them. */
6318 if (!is_goto)
6319 outputs = c_parser_asm_operands (parser);
6320 break;
6321 case 1:
6322 inputs = c_parser_asm_operands (parser);
6323 break;
6324 case 2:
6325 clobbers = c_parser_asm_clobbers (parser);
6326 break;
6327 case 3:
6328 labels = c_parser_asm_goto_operands (parser);
6329 break;
6330 default:
6331 gcc_unreachable ();
6334 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6335 goto done_asm;
6338 done_asm:
6339 if (!parens.require_close (parser))
6341 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6342 goto error;
6345 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6346 c_parser_skip_to_end_of_block_or_statement (parser);
6348 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6349 clobbers, labels, simple));
6351 error:
6352 parser->lex_untranslated_string = false;
6353 return ret;
6355 error_close_paren:
6356 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6357 goto error;
6360 /* Parse asm operands, a GNU extension.
6362 asm-operands:
6363 asm-operand
6364 asm-operands , asm-operand
6366 asm-operand:
6367 asm-string-literal ( expression )
6368 [ identifier ] asm-string-literal ( expression )
6371 static tree
6372 c_parser_asm_operands (c_parser *parser)
6374 tree list = NULL_TREE;
6375 while (true)
6377 tree name, str;
6378 struct c_expr expr;
6379 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6381 c_parser_consume_token (parser);
6382 if (c_parser_next_token_is (parser, CPP_NAME))
6384 tree id = c_parser_peek_token (parser)->value;
6385 c_parser_consume_token (parser);
6386 name = build_string (IDENTIFIER_LENGTH (id),
6387 IDENTIFIER_POINTER (id));
6389 else
6391 c_parser_error (parser, "expected identifier");
6392 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6393 return NULL_TREE;
6395 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6396 "expected %<]%>");
6398 else
6399 name = NULL_TREE;
6400 str = c_parser_asm_string_literal (parser);
6401 if (str == NULL_TREE)
6402 return NULL_TREE;
6403 parser->lex_untranslated_string = false;
6404 matching_parens parens;
6405 if (!parens.require_open (parser))
6407 parser->lex_untranslated_string = true;
6408 return NULL_TREE;
6410 expr = c_parser_expression (parser);
6411 mark_exp_read (expr.value);
6412 parser->lex_untranslated_string = true;
6413 if (!parens.require_close (parser))
6415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6416 return NULL_TREE;
6418 list = chainon (list, build_tree_list (build_tree_list (name, str),
6419 expr.value));
6420 if (c_parser_next_token_is (parser, CPP_COMMA))
6421 c_parser_consume_token (parser);
6422 else
6423 break;
6425 return list;
6428 /* Parse asm clobbers, a GNU extension.
6430 asm-clobbers:
6431 asm-string-literal
6432 asm-clobbers , asm-string-literal
6435 static tree
6436 c_parser_asm_clobbers (c_parser *parser)
6438 tree list = NULL_TREE;
6439 while (true)
6441 tree str = c_parser_asm_string_literal (parser);
6442 if (str)
6443 list = tree_cons (NULL_TREE, str, list);
6444 else
6445 return NULL_TREE;
6446 if (c_parser_next_token_is (parser, CPP_COMMA))
6447 c_parser_consume_token (parser);
6448 else
6449 break;
6451 return list;
6454 /* Parse asm goto labels, a GNU extension.
6456 asm-goto-operands:
6457 identifier
6458 asm-goto-operands , identifier
6461 static tree
6462 c_parser_asm_goto_operands (c_parser *parser)
6464 tree list = NULL_TREE;
6465 while (true)
6467 tree name, label;
6469 if (c_parser_next_token_is (parser, CPP_NAME))
6471 c_token *tok = c_parser_peek_token (parser);
6472 name = tok->value;
6473 label = lookup_label_for_goto (tok->location, name);
6474 c_parser_consume_token (parser);
6475 TREE_USED (label) = 1;
6477 else
6479 c_parser_error (parser, "expected identifier");
6480 return NULL_TREE;
6483 name = build_string (IDENTIFIER_LENGTH (name),
6484 IDENTIFIER_POINTER (name));
6485 list = tree_cons (name, label, list);
6486 if (c_parser_next_token_is (parser, CPP_COMMA))
6487 c_parser_consume_token (parser);
6488 else
6489 return nreverse (list);
6493 /* Parse an expression other than a compound expression; that is, an
6494 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6495 AFTER is not NULL then it is an Objective-C message expression which
6496 is the primary-expression starting the expression as an initializer.
6498 assignment-expression:
6499 conditional-expression
6500 unary-expression assignment-operator assignment-expression
6502 assignment-operator: one of
6503 = *= /= %= += -= <<= >>= &= ^= |=
6505 In GNU C we accept any conditional expression on the LHS and
6506 diagnose the invalid lvalue rather than producing a syntax
6507 error. */
6509 static struct c_expr
6510 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6511 tree omp_atomic_lhs)
6513 struct c_expr lhs, rhs, ret;
6514 enum tree_code code;
6515 location_t op_location, exp_location;
6516 gcc_assert (!after || c_dialect_objc ());
6517 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6518 op_location = c_parser_peek_token (parser)->location;
6519 switch (c_parser_peek_token (parser)->type)
6521 case CPP_EQ:
6522 code = NOP_EXPR;
6523 break;
6524 case CPP_MULT_EQ:
6525 code = MULT_EXPR;
6526 break;
6527 case CPP_DIV_EQ:
6528 code = TRUNC_DIV_EXPR;
6529 break;
6530 case CPP_MOD_EQ:
6531 code = TRUNC_MOD_EXPR;
6532 break;
6533 case CPP_PLUS_EQ:
6534 code = PLUS_EXPR;
6535 break;
6536 case CPP_MINUS_EQ:
6537 code = MINUS_EXPR;
6538 break;
6539 case CPP_LSHIFT_EQ:
6540 code = LSHIFT_EXPR;
6541 break;
6542 case CPP_RSHIFT_EQ:
6543 code = RSHIFT_EXPR;
6544 break;
6545 case CPP_AND_EQ:
6546 code = BIT_AND_EXPR;
6547 break;
6548 case CPP_XOR_EQ:
6549 code = BIT_XOR_EXPR;
6550 break;
6551 case CPP_OR_EQ:
6552 code = BIT_IOR_EXPR;
6553 break;
6554 default:
6555 return lhs;
6557 c_parser_consume_token (parser);
6558 exp_location = c_parser_peek_token (parser)->location;
6559 rhs = c_parser_expr_no_commas (parser, NULL);
6560 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6562 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6563 code, exp_location, rhs.value,
6564 rhs.original_type);
6565 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6566 if (code == NOP_EXPR)
6567 ret.original_code = MODIFY_EXPR;
6568 else
6570 TREE_NO_WARNING (ret.value) = 1;
6571 ret.original_code = ERROR_MARK;
6573 ret.original_type = NULL;
6574 return ret;
6577 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6578 AFTER is not NULL then it is an Objective-C message expression which is
6579 the primary-expression starting the expression as an initializer.
6581 conditional-expression:
6582 logical-OR-expression
6583 logical-OR-expression ? expression : conditional-expression
6585 GNU extensions:
6587 conditional-expression:
6588 logical-OR-expression ? : conditional-expression
6591 static struct c_expr
6592 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6593 tree omp_atomic_lhs)
6595 struct c_expr cond, exp1, exp2, ret;
6596 location_t start, cond_loc, colon_loc;
6598 gcc_assert (!after || c_dialect_objc ());
6600 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6602 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6603 return cond;
6604 if (cond.value != error_mark_node)
6605 start = cond.get_start ();
6606 else
6607 start = UNKNOWN_LOCATION;
6608 cond_loc = c_parser_peek_token (parser)->location;
6609 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6610 c_parser_consume_token (parser);
6611 if (c_parser_next_token_is (parser, CPP_COLON))
6613 tree eptype = NULL_TREE;
6615 location_t middle_loc = c_parser_peek_token (parser)->location;
6616 pedwarn (middle_loc, OPT_Wpedantic,
6617 "ISO C forbids omitting the middle term of a ?: expression");
6618 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6620 eptype = TREE_TYPE (cond.value);
6621 cond.value = TREE_OPERAND (cond.value, 0);
6623 tree e = cond.value;
6624 while (TREE_CODE (e) == COMPOUND_EXPR)
6625 e = TREE_OPERAND (e, 1);
6626 warn_for_omitted_condop (middle_loc, e);
6627 /* Make sure first operand is calculated only once. */
6628 exp1.value = save_expr (default_conversion (cond.value));
6629 if (eptype)
6630 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6631 exp1.original_type = NULL;
6632 exp1.src_range = cond.src_range;
6633 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6634 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6636 else
6638 cond.value
6639 = c_objc_common_truthvalue_conversion
6640 (cond_loc, default_conversion (cond.value));
6641 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6642 exp1 = c_parser_expression_conv (parser);
6643 mark_exp_read (exp1.value);
6644 c_inhibit_evaluation_warnings +=
6645 ((cond.value == truthvalue_true_node)
6646 - (cond.value == truthvalue_false_node));
6649 colon_loc = c_parser_peek_token (parser)->location;
6650 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6652 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6653 ret.value = error_mark_node;
6654 ret.original_code = ERROR_MARK;
6655 ret.original_type = NULL;
6656 return ret;
6659 location_t exp2_loc = c_parser_peek_token (parser)->location;
6660 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6661 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6663 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6664 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6665 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6666 ret.value = build_conditional_expr (colon_loc, cond.value,
6667 cond.original_code == C_MAYBE_CONST_EXPR,
6668 exp1.value, exp1.original_type, loc1,
6669 exp2.value, exp2.original_type, loc2);
6670 ret.original_code = ERROR_MARK;
6671 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6672 ret.original_type = NULL;
6673 else
6675 tree t1, t2;
6677 /* If both sides are enum type, the default conversion will have
6678 made the type of the result be an integer type. We want to
6679 remember the enum types we started with. */
6680 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6681 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6682 ret.original_type = ((t1 != error_mark_node
6683 && t2 != error_mark_node
6684 && (TYPE_MAIN_VARIANT (t1)
6685 == TYPE_MAIN_VARIANT (t2)))
6686 ? t1
6687 : NULL);
6689 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6690 return ret;
6693 /* Parse a binary expression; that is, a logical-OR-expression (C90
6694 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6695 NULL then it is an Objective-C message expression which is the
6696 primary-expression starting the expression as an initializer.
6698 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6699 when it should be the unfolded lhs. In a valid OpenMP source,
6700 one of the operands of the toplevel binary expression must be equal
6701 to it. In that case, just return a build2 created binary operation
6702 rather than result of parser_build_binary_op.
6704 multiplicative-expression:
6705 cast-expression
6706 multiplicative-expression * cast-expression
6707 multiplicative-expression / cast-expression
6708 multiplicative-expression % cast-expression
6710 additive-expression:
6711 multiplicative-expression
6712 additive-expression + multiplicative-expression
6713 additive-expression - multiplicative-expression
6715 shift-expression:
6716 additive-expression
6717 shift-expression << additive-expression
6718 shift-expression >> additive-expression
6720 relational-expression:
6721 shift-expression
6722 relational-expression < shift-expression
6723 relational-expression > shift-expression
6724 relational-expression <= shift-expression
6725 relational-expression >= shift-expression
6727 equality-expression:
6728 relational-expression
6729 equality-expression == relational-expression
6730 equality-expression != relational-expression
6732 AND-expression:
6733 equality-expression
6734 AND-expression & equality-expression
6736 exclusive-OR-expression:
6737 AND-expression
6738 exclusive-OR-expression ^ AND-expression
6740 inclusive-OR-expression:
6741 exclusive-OR-expression
6742 inclusive-OR-expression | exclusive-OR-expression
6744 logical-AND-expression:
6745 inclusive-OR-expression
6746 logical-AND-expression && inclusive-OR-expression
6748 logical-OR-expression:
6749 logical-AND-expression
6750 logical-OR-expression || logical-AND-expression
6753 static struct c_expr
6754 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6755 tree omp_atomic_lhs)
6757 /* A binary expression is parsed using operator-precedence parsing,
6758 with the operands being cast expressions. All the binary
6759 operators are left-associative. Thus a binary expression is of
6760 form:
6762 E0 op1 E1 op2 E2 ...
6764 which we represent on a stack. On the stack, the precedence
6765 levels are strictly increasing. When a new operator is
6766 encountered of higher precedence than that at the top of the
6767 stack, it is pushed; its LHS is the top expression, and its RHS
6768 is everything parsed until it is popped. When a new operator is
6769 encountered with precedence less than or equal to that at the top
6770 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6771 by the result of the operation until the operator at the top of
6772 the stack has lower precedence than the new operator or there is
6773 only one element on the stack; then the top expression is the LHS
6774 of the new operator. In the case of logical AND and OR
6775 expressions, we also need to adjust c_inhibit_evaluation_warnings
6776 as appropriate when the operators are pushed and popped. */
6778 struct {
6779 /* The expression at this stack level. */
6780 struct c_expr expr;
6781 /* The precedence of the operator on its left, PREC_NONE at the
6782 bottom of the stack. */
6783 enum c_parser_prec prec;
6784 /* The operation on its left. */
6785 enum tree_code op;
6786 /* The source location of this operation. */
6787 location_t loc;
6788 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6789 tree sizeof_arg;
6790 } stack[NUM_PRECS];
6791 int sp;
6792 /* Location of the binary operator. */
6793 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6794 #define POP \
6795 do { \
6796 switch (stack[sp].op) \
6798 case TRUTH_ANDIF_EXPR: \
6799 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6800 == truthvalue_false_node); \
6801 break; \
6802 case TRUTH_ORIF_EXPR: \
6803 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6804 == truthvalue_true_node); \
6805 break; \
6806 case TRUNC_DIV_EXPR: \
6807 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6808 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6810 tree type0 = stack[sp - 1].sizeof_arg; \
6811 tree type1 = stack[sp].sizeof_arg; \
6812 tree first_arg = type0; \
6813 if (!TYPE_P (type0)) \
6814 type0 = TREE_TYPE (type0); \
6815 if (!TYPE_P (type1)) \
6816 type1 = TREE_TYPE (type1); \
6817 if (POINTER_TYPE_P (type0) \
6818 && comptypes (TREE_TYPE (type0), type1) \
6819 && !(TREE_CODE (first_arg) == PARM_DECL \
6820 && C_ARRAY_PARAMETER (first_arg) \
6821 && warn_sizeof_array_argument)) \
6822 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6823 "division %<sizeof (%T) / sizeof (%T)%> does " \
6824 "not compute the number of array elements", \
6825 type0, type1)) \
6826 if (DECL_P (first_arg)) \
6827 inform (DECL_SOURCE_LOCATION (first_arg), \
6828 "first %<sizeof%> operand was declared here"); \
6830 break; \
6831 default: \
6832 break; \
6834 stack[sp - 1].expr \
6835 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6836 stack[sp - 1].expr, true, true); \
6837 stack[sp].expr \
6838 = convert_lvalue_to_rvalue (stack[sp].loc, \
6839 stack[sp].expr, true, true); \
6840 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6841 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6842 && ((1 << stack[sp].prec) \
6843 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6844 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6845 && stack[sp].op != TRUNC_MOD_EXPR \
6846 && stack[0].expr.value != error_mark_node \
6847 && stack[1].expr.value != error_mark_node \
6848 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6849 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6850 stack[0].expr.value \
6851 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6852 stack[0].expr.value, stack[1].expr.value); \
6853 else \
6854 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6855 stack[sp].op, \
6856 stack[sp - 1].expr, \
6857 stack[sp].expr); \
6858 sp--; \
6859 } while (0)
6860 gcc_assert (!after || c_dialect_objc ());
6861 stack[0].loc = c_parser_peek_token (parser)->location;
6862 stack[0].expr = c_parser_cast_expression (parser, after);
6863 stack[0].prec = PREC_NONE;
6864 stack[0].sizeof_arg = c_last_sizeof_arg;
6865 sp = 0;
6866 while (true)
6868 enum c_parser_prec oprec;
6869 enum tree_code ocode;
6870 source_range src_range;
6871 if (parser->error)
6872 goto out;
6873 switch (c_parser_peek_token (parser)->type)
6875 case CPP_MULT:
6876 oprec = PREC_MULT;
6877 ocode = MULT_EXPR;
6878 break;
6879 case CPP_DIV:
6880 oprec = PREC_MULT;
6881 ocode = TRUNC_DIV_EXPR;
6882 break;
6883 case CPP_MOD:
6884 oprec = PREC_MULT;
6885 ocode = TRUNC_MOD_EXPR;
6886 break;
6887 case CPP_PLUS:
6888 oprec = PREC_ADD;
6889 ocode = PLUS_EXPR;
6890 break;
6891 case CPP_MINUS:
6892 oprec = PREC_ADD;
6893 ocode = MINUS_EXPR;
6894 break;
6895 case CPP_LSHIFT:
6896 oprec = PREC_SHIFT;
6897 ocode = LSHIFT_EXPR;
6898 break;
6899 case CPP_RSHIFT:
6900 oprec = PREC_SHIFT;
6901 ocode = RSHIFT_EXPR;
6902 break;
6903 case CPP_LESS:
6904 oprec = PREC_REL;
6905 ocode = LT_EXPR;
6906 break;
6907 case CPP_GREATER:
6908 oprec = PREC_REL;
6909 ocode = GT_EXPR;
6910 break;
6911 case CPP_LESS_EQ:
6912 oprec = PREC_REL;
6913 ocode = LE_EXPR;
6914 break;
6915 case CPP_GREATER_EQ:
6916 oprec = PREC_REL;
6917 ocode = GE_EXPR;
6918 break;
6919 case CPP_EQ_EQ:
6920 oprec = PREC_EQ;
6921 ocode = EQ_EXPR;
6922 break;
6923 case CPP_NOT_EQ:
6924 oprec = PREC_EQ;
6925 ocode = NE_EXPR;
6926 break;
6927 case CPP_AND:
6928 oprec = PREC_BITAND;
6929 ocode = BIT_AND_EXPR;
6930 break;
6931 case CPP_XOR:
6932 oprec = PREC_BITXOR;
6933 ocode = BIT_XOR_EXPR;
6934 break;
6935 case CPP_OR:
6936 oprec = PREC_BITOR;
6937 ocode = BIT_IOR_EXPR;
6938 break;
6939 case CPP_AND_AND:
6940 oprec = PREC_LOGAND;
6941 ocode = TRUTH_ANDIF_EXPR;
6942 break;
6943 case CPP_OR_OR:
6944 oprec = PREC_LOGOR;
6945 ocode = TRUTH_ORIF_EXPR;
6946 break;
6947 default:
6948 /* Not a binary operator, so end of the binary
6949 expression. */
6950 goto out;
6952 binary_loc = c_parser_peek_token (parser)->location;
6953 while (oprec <= stack[sp].prec)
6954 POP;
6955 c_parser_consume_token (parser);
6956 switch (ocode)
6958 case TRUTH_ANDIF_EXPR:
6959 src_range = stack[sp].expr.src_range;
6960 stack[sp].expr
6961 = convert_lvalue_to_rvalue (stack[sp].loc,
6962 stack[sp].expr, true, true);
6963 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6964 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6965 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6966 == truthvalue_false_node);
6967 set_c_expr_source_range (&stack[sp].expr, src_range);
6968 break;
6969 case TRUTH_ORIF_EXPR:
6970 src_range = stack[sp].expr.src_range;
6971 stack[sp].expr
6972 = convert_lvalue_to_rvalue (stack[sp].loc,
6973 stack[sp].expr, true, true);
6974 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6975 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6976 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6977 == truthvalue_true_node);
6978 set_c_expr_source_range (&stack[sp].expr, src_range);
6979 break;
6980 default:
6981 break;
6983 sp++;
6984 stack[sp].loc = binary_loc;
6985 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6986 stack[sp].prec = oprec;
6987 stack[sp].op = ocode;
6988 stack[sp].sizeof_arg = c_last_sizeof_arg;
6990 out:
6991 while (sp > 0)
6992 POP;
6993 return stack[0].expr;
6994 #undef POP
6997 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
6998 is not NULL then it is an Objective-C message expression which is the
6999 primary-expression starting the expression as an initializer.
7001 cast-expression:
7002 unary-expression
7003 ( type-name ) unary-expression
7006 static struct c_expr
7007 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7009 location_t cast_loc = c_parser_peek_token (parser)->location;
7010 gcc_assert (!after || c_dialect_objc ());
7011 if (after)
7012 return c_parser_postfix_expression_after_primary (parser,
7013 cast_loc, *after);
7014 /* If the expression begins with a parenthesized type name, it may
7015 be either a cast or a compound literal; we need to see whether
7016 the next character is '{' to tell the difference. If not, it is
7017 an unary expression. Full detection of unknown typenames here
7018 would require a 3-token lookahead. */
7019 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7020 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7022 struct c_type_name *type_name;
7023 struct c_expr ret;
7024 struct c_expr expr;
7025 matching_parens parens;
7026 parens.consume_open (parser);
7027 type_name = c_parser_type_name (parser, true);
7028 parens.skip_until_found_close (parser);
7029 if (type_name == NULL)
7031 ret.value = error_mark_node;
7032 ret.original_code = ERROR_MARK;
7033 ret.original_type = NULL;
7034 return ret;
7037 /* Save casted types in the function's used types hash table. */
7038 used_types_insert (type_name->specs->type);
7040 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7041 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7042 cast_loc);
7043 if (type_name->specs->alignas_p)
7044 error_at (type_name->specs->locations[cdw_alignas],
7045 "alignment specified for type name in cast");
7047 location_t expr_loc = c_parser_peek_token (parser)->location;
7048 expr = c_parser_cast_expression (parser, NULL);
7049 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7051 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7052 if (ret.value && expr.value)
7053 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7054 ret.original_code = ERROR_MARK;
7055 ret.original_type = NULL;
7056 return ret;
7058 else
7059 return c_parser_unary_expression (parser);
7062 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7064 unary-expression:
7065 postfix-expression
7066 ++ unary-expression
7067 -- unary-expression
7068 unary-operator cast-expression
7069 sizeof unary-expression
7070 sizeof ( type-name )
7072 unary-operator: one of
7073 & * + - ~ !
7075 GNU extensions:
7077 unary-expression:
7078 __alignof__ unary-expression
7079 __alignof__ ( type-name )
7080 && identifier
7082 (C11 permits _Alignof with type names only.)
7084 unary-operator: one of
7085 __extension__ __real__ __imag__
7087 Transactional Memory:
7089 unary-expression:
7090 transaction-expression
7092 In addition, the GNU syntax treats ++ and -- as unary operators, so
7093 they may be applied to cast expressions with errors for non-lvalues
7094 given later. */
7096 static struct c_expr
7097 c_parser_unary_expression (c_parser *parser)
7099 int ext;
7100 struct c_expr ret, op;
7101 location_t op_loc = c_parser_peek_token (parser)->location;
7102 location_t exp_loc;
7103 location_t finish;
7104 ret.original_code = ERROR_MARK;
7105 ret.original_type = NULL;
7106 switch (c_parser_peek_token (parser)->type)
7108 case CPP_PLUS_PLUS:
7109 c_parser_consume_token (parser);
7110 exp_loc = c_parser_peek_token (parser)->location;
7111 op = c_parser_cast_expression (parser, NULL);
7113 op = default_function_array_read_conversion (exp_loc, op);
7114 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7115 case CPP_MINUS_MINUS:
7116 c_parser_consume_token (parser);
7117 exp_loc = c_parser_peek_token (parser)->location;
7118 op = c_parser_cast_expression (parser, NULL);
7120 op = default_function_array_read_conversion (exp_loc, op);
7121 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7122 case CPP_AND:
7123 c_parser_consume_token (parser);
7124 op = c_parser_cast_expression (parser, NULL);
7125 mark_exp_read (op.value);
7126 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7127 case CPP_MULT:
7129 c_parser_consume_token (parser);
7130 exp_loc = c_parser_peek_token (parser)->location;
7131 op = c_parser_cast_expression (parser, NULL);
7132 finish = op.get_finish ();
7133 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7134 location_t combined_loc = make_location (op_loc, op_loc, finish);
7135 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7136 ret.src_range.m_start = op_loc;
7137 ret.src_range.m_finish = finish;
7138 return ret;
7140 case CPP_PLUS:
7141 if (!c_dialect_objc () && !in_system_header_at (input_location))
7142 warning_at (op_loc,
7143 OPT_Wtraditional,
7144 "traditional C rejects the unary plus operator");
7145 c_parser_consume_token (parser);
7146 exp_loc = c_parser_peek_token (parser)->location;
7147 op = c_parser_cast_expression (parser, NULL);
7148 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7149 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7150 case CPP_MINUS:
7151 c_parser_consume_token (parser);
7152 exp_loc = c_parser_peek_token (parser)->location;
7153 op = c_parser_cast_expression (parser, NULL);
7154 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7155 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7156 case CPP_COMPL:
7157 c_parser_consume_token (parser);
7158 exp_loc = c_parser_peek_token (parser)->location;
7159 op = c_parser_cast_expression (parser, NULL);
7160 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7161 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7162 case CPP_NOT:
7163 c_parser_consume_token (parser);
7164 exp_loc = c_parser_peek_token (parser)->location;
7165 op = c_parser_cast_expression (parser, NULL);
7166 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7167 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7168 case CPP_AND_AND:
7169 /* Refer to the address of a label as a pointer. */
7170 c_parser_consume_token (parser);
7171 if (c_parser_next_token_is (parser, CPP_NAME))
7173 ret.value = finish_label_address_expr
7174 (c_parser_peek_token (parser)->value, op_loc);
7175 set_c_expr_source_range (&ret, op_loc,
7176 c_parser_peek_token (parser)->get_finish ());
7177 c_parser_consume_token (parser);
7179 else
7181 c_parser_error (parser, "expected identifier");
7182 ret.set_error ();
7184 return ret;
7185 case CPP_KEYWORD:
7186 switch (c_parser_peek_token (parser)->keyword)
7188 case RID_SIZEOF:
7189 return c_parser_sizeof_expression (parser);
7190 case RID_ALIGNOF:
7191 return c_parser_alignof_expression (parser);
7192 case RID_EXTENSION:
7193 c_parser_consume_token (parser);
7194 ext = disable_extension_diagnostics ();
7195 ret = c_parser_cast_expression (parser, NULL);
7196 restore_extension_diagnostics (ext);
7197 return ret;
7198 case RID_REALPART:
7199 c_parser_consume_token (parser);
7200 exp_loc = c_parser_peek_token (parser)->location;
7201 op = c_parser_cast_expression (parser, NULL);
7202 op = default_function_array_conversion (exp_loc, op);
7203 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7204 case RID_IMAGPART:
7205 c_parser_consume_token (parser);
7206 exp_loc = c_parser_peek_token (parser)->location;
7207 op = c_parser_cast_expression (parser, NULL);
7208 op = default_function_array_conversion (exp_loc, op);
7209 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7210 case RID_TRANSACTION_ATOMIC:
7211 case RID_TRANSACTION_RELAXED:
7212 return c_parser_transaction_expression (parser,
7213 c_parser_peek_token (parser)->keyword);
7214 default:
7215 return c_parser_postfix_expression (parser);
7217 default:
7218 return c_parser_postfix_expression (parser);
7222 /* Parse a sizeof expression. */
7224 static struct c_expr
7225 c_parser_sizeof_expression (c_parser *parser)
7227 struct c_expr expr;
7228 struct c_expr result;
7229 location_t expr_loc;
7230 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7232 location_t start;
7233 location_t finish = UNKNOWN_LOCATION;
7235 start = c_parser_peek_token (parser)->location;
7237 c_parser_consume_token (parser);
7238 c_inhibit_evaluation_warnings++;
7239 in_sizeof++;
7240 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7241 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7243 /* Either sizeof ( type-name ) or sizeof unary-expression
7244 starting with a compound literal. */
7245 struct c_type_name *type_name;
7246 matching_parens parens;
7247 parens.consume_open (parser);
7248 expr_loc = c_parser_peek_token (parser)->location;
7249 type_name = c_parser_type_name (parser, true);
7250 parens.skip_until_found_close (parser);
7251 finish = parser->tokens_buf[0].location;
7252 if (type_name == NULL)
7254 struct c_expr ret;
7255 c_inhibit_evaluation_warnings--;
7256 in_sizeof--;
7257 ret.value = error_mark_node;
7258 ret.original_code = ERROR_MARK;
7259 ret.original_type = NULL;
7260 return ret;
7262 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7264 expr = c_parser_postfix_expression_after_paren_type (parser,
7265 type_name,
7266 expr_loc);
7267 finish = expr.get_finish ();
7268 goto sizeof_expr;
7270 /* sizeof ( type-name ). */
7271 if (type_name->specs->alignas_p)
7272 error_at (type_name->specs->locations[cdw_alignas],
7273 "alignment specified for type name in %<sizeof%>");
7274 c_inhibit_evaluation_warnings--;
7275 in_sizeof--;
7276 result = c_expr_sizeof_type (expr_loc, type_name);
7278 else
7280 expr_loc = c_parser_peek_token (parser)->location;
7281 expr = c_parser_unary_expression (parser);
7282 finish = expr.get_finish ();
7283 sizeof_expr:
7284 c_inhibit_evaluation_warnings--;
7285 in_sizeof--;
7286 mark_exp_read (expr.value);
7287 if (TREE_CODE (expr.value) == COMPONENT_REF
7288 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7289 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7290 result = c_expr_sizeof_expr (expr_loc, expr);
7292 if (finish != UNKNOWN_LOCATION)
7293 set_c_expr_source_range (&result, start, finish);
7294 return result;
7297 /* Parse an alignof expression. */
7299 static struct c_expr
7300 c_parser_alignof_expression (c_parser *parser)
7302 struct c_expr expr;
7303 location_t start_loc = c_parser_peek_token (parser)->location;
7304 location_t end_loc;
7305 tree alignof_spelling = c_parser_peek_token (parser)->value;
7306 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7307 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7308 "_Alignof") == 0;
7309 /* A diagnostic is not required for the use of this identifier in
7310 the implementation namespace; only diagnose it for the C11
7311 spelling because of existing code using the other spellings. */
7312 if (is_c11_alignof)
7314 if (flag_isoc99)
7315 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7316 alignof_spelling);
7317 else
7318 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7319 alignof_spelling);
7321 c_parser_consume_token (parser);
7322 c_inhibit_evaluation_warnings++;
7323 in_alignof++;
7324 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7325 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7327 /* Either __alignof__ ( type-name ) or __alignof__
7328 unary-expression starting with a compound literal. */
7329 location_t loc;
7330 struct c_type_name *type_name;
7331 struct c_expr ret;
7332 matching_parens parens;
7333 parens.consume_open (parser);
7334 loc = c_parser_peek_token (parser)->location;
7335 type_name = c_parser_type_name (parser, true);
7336 end_loc = c_parser_peek_token (parser)->location;
7337 parens.skip_until_found_close (parser);
7338 if (type_name == NULL)
7340 struct c_expr ret;
7341 c_inhibit_evaluation_warnings--;
7342 in_alignof--;
7343 ret.value = error_mark_node;
7344 ret.original_code = ERROR_MARK;
7345 ret.original_type = NULL;
7346 return ret;
7348 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7350 expr = c_parser_postfix_expression_after_paren_type (parser,
7351 type_name,
7352 loc);
7353 goto alignof_expr;
7355 /* alignof ( type-name ). */
7356 if (type_name->specs->alignas_p)
7357 error_at (type_name->specs->locations[cdw_alignas],
7358 "alignment specified for type name in %qE",
7359 alignof_spelling);
7360 c_inhibit_evaluation_warnings--;
7361 in_alignof--;
7362 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7363 NULL, NULL),
7364 false, is_c11_alignof, 1);
7365 ret.original_code = ERROR_MARK;
7366 ret.original_type = NULL;
7367 set_c_expr_source_range (&ret, start_loc, end_loc);
7368 return ret;
7370 else
7372 struct c_expr ret;
7373 expr = c_parser_unary_expression (parser);
7374 end_loc = expr.src_range.m_finish;
7375 alignof_expr:
7376 mark_exp_read (expr.value);
7377 c_inhibit_evaluation_warnings--;
7378 in_alignof--;
7379 if (is_c11_alignof)
7380 pedwarn (start_loc,
7381 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7382 alignof_spelling);
7383 ret.value = c_alignof_expr (start_loc, expr.value);
7384 ret.original_code = ERROR_MARK;
7385 ret.original_type = NULL;
7386 set_c_expr_source_range (&ret, start_loc, end_loc);
7387 return ret;
7391 /* Helper function to read arguments of builtins which are interfaces
7392 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7393 others. The name of the builtin is passed using BNAME parameter.
7394 Function returns true if there were no errors while parsing and
7395 stores the arguments in CEXPR_LIST. If it returns true,
7396 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7397 parenthesis. */
7398 static bool
7399 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7400 vec<c_expr_t, va_gc> **ret_cexpr_list,
7401 bool choose_expr_p,
7402 location_t *out_close_paren_loc)
7404 location_t loc = c_parser_peek_token (parser)->location;
7405 vec<c_expr_t, va_gc> *cexpr_list;
7406 c_expr_t expr;
7407 bool saved_force_folding_builtin_constant_p;
7409 *ret_cexpr_list = NULL;
7410 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7412 error_at (loc, "cannot take address of %qs", bname);
7413 return false;
7416 c_parser_consume_token (parser);
7418 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7420 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7421 c_parser_consume_token (parser);
7422 return true;
7425 saved_force_folding_builtin_constant_p
7426 = force_folding_builtin_constant_p;
7427 force_folding_builtin_constant_p |= choose_expr_p;
7428 expr = c_parser_expr_no_commas (parser, NULL);
7429 force_folding_builtin_constant_p
7430 = saved_force_folding_builtin_constant_p;
7431 vec_alloc (cexpr_list, 1);
7432 vec_safe_push (cexpr_list, expr);
7433 while (c_parser_next_token_is (parser, CPP_COMMA))
7435 c_parser_consume_token (parser);
7436 expr = c_parser_expr_no_commas (parser, NULL);
7437 vec_safe_push (cexpr_list, expr);
7440 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7441 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7442 return false;
7444 *ret_cexpr_list = cexpr_list;
7445 return true;
7448 /* This represents a single generic-association. */
7450 struct c_generic_association
7452 /* The location of the starting token of the type. */
7453 location_t type_location;
7454 /* The association's type, or NULL_TREE for 'default'. */
7455 tree type;
7456 /* The association's expression. */
7457 struct c_expr expression;
7460 /* Parse a generic-selection. (C11 6.5.1.1).
7462 generic-selection:
7463 _Generic ( assignment-expression , generic-assoc-list )
7465 generic-assoc-list:
7466 generic-association
7467 generic-assoc-list , generic-association
7469 generic-association:
7470 type-name : assignment-expression
7471 default : assignment-expression
7474 static struct c_expr
7475 c_parser_generic_selection (c_parser *parser)
7477 struct c_expr selector, error_expr;
7478 tree selector_type;
7479 struct c_generic_association matched_assoc;
7480 bool match_found = false;
7481 location_t generic_loc, selector_loc;
7483 error_expr.original_code = ERROR_MARK;
7484 error_expr.original_type = NULL;
7485 error_expr.set_error ();
7486 matched_assoc.type_location = UNKNOWN_LOCATION;
7487 matched_assoc.type = NULL_TREE;
7488 matched_assoc.expression = error_expr;
7490 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7491 generic_loc = c_parser_peek_token (parser)->location;
7492 c_parser_consume_token (parser);
7493 if (flag_isoc99)
7494 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7495 "ISO C99 does not support %<_Generic%>");
7496 else
7497 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7498 "ISO C90 does not support %<_Generic%>");
7500 matching_parens parens;
7501 if (!parens.require_open (parser))
7502 return error_expr;
7504 c_inhibit_evaluation_warnings++;
7505 selector_loc = c_parser_peek_token (parser)->location;
7506 selector = c_parser_expr_no_commas (parser, NULL);
7507 selector = default_function_array_conversion (selector_loc, selector);
7508 c_inhibit_evaluation_warnings--;
7510 if (selector.value == error_mark_node)
7512 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7513 return selector;
7515 selector_type = TREE_TYPE (selector.value);
7516 /* In ISO C terms, rvalues (including the controlling expression of
7517 _Generic) do not have qualified types. */
7518 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7519 selector_type = TYPE_MAIN_VARIANT (selector_type);
7520 /* In ISO C terms, _Noreturn is not part of the type of expressions
7521 such as &abort, but in GCC it is represented internally as a type
7522 qualifier. */
7523 if (FUNCTION_POINTER_TYPE_P (selector_type)
7524 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7525 selector_type
7526 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7528 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7531 return error_expr;
7534 auto_vec<c_generic_association> associations;
7535 while (1)
7537 struct c_generic_association assoc, *iter;
7538 unsigned int ix;
7539 c_token *token = c_parser_peek_token (parser);
7541 assoc.type_location = token->location;
7542 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7544 c_parser_consume_token (parser);
7545 assoc.type = NULL_TREE;
7547 else
7549 struct c_type_name *type_name;
7551 type_name = c_parser_type_name (parser);
7552 if (type_name == NULL)
7554 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7555 return error_expr;
7557 assoc.type = groktypename (type_name, NULL, NULL);
7558 if (assoc.type == error_mark_node)
7560 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7561 return error_expr;
7564 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7565 error_at (assoc.type_location,
7566 "%<_Generic%> association has function type");
7567 else if (!COMPLETE_TYPE_P (assoc.type))
7568 error_at (assoc.type_location,
7569 "%<_Generic%> association has incomplete type");
7571 if (variably_modified_type_p (assoc.type, NULL_TREE))
7572 error_at (assoc.type_location,
7573 "%<_Generic%> association has "
7574 "variable length type");
7577 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7579 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7580 return error_expr;
7583 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7584 if (assoc.expression.value == error_mark_node)
7586 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7587 return error_expr;
7590 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7592 if (assoc.type == NULL_TREE)
7594 if (iter->type == NULL_TREE)
7596 error_at (assoc.type_location,
7597 "duplicate %<default%> case in %<_Generic%>");
7598 inform (iter->type_location, "original %<default%> is here");
7601 else if (iter->type != NULL_TREE)
7603 if (comptypes (assoc.type, iter->type))
7605 error_at (assoc.type_location,
7606 "%<_Generic%> specifies two compatible types");
7607 inform (iter->type_location, "compatible type is here");
7612 if (assoc.type == NULL_TREE)
7614 if (!match_found)
7616 matched_assoc = assoc;
7617 match_found = true;
7620 else if (comptypes (assoc.type, selector_type))
7622 if (!match_found || matched_assoc.type == NULL_TREE)
7624 matched_assoc = assoc;
7625 match_found = true;
7627 else
7629 error_at (assoc.type_location,
7630 "%<_Generic%> selector matches multiple associations");
7631 inform (matched_assoc.type_location,
7632 "other match is here");
7636 associations.safe_push (assoc);
7638 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7639 break;
7640 c_parser_consume_token (parser);
7643 if (!parens.require_close (parser))
7645 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7646 return error_expr;
7649 if (!match_found)
7651 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7652 "compatible with any association",
7653 selector_type);
7654 return error_expr;
7657 return matched_assoc.expression;
7660 /* Check the validity of a function pointer argument *EXPR (argument
7661 position POS) to __builtin_tgmath. Return the number of function
7662 arguments if possibly valid; return 0 having reported an error if
7663 not valid. */
7665 static unsigned int
7666 check_tgmath_function (c_expr *expr, unsigned int pos)
7668 tree type = TREE_TYPE (expr->value);
7669 if (!FUNCTION_POINTER_TYPE_P (type))
7671 error_at (expr->get_location (),
7672 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7673 pos);
7674 return 0;
7676 type = TREE_TYPE (type);
7677 if (!prototype_p (type))
7679 error_at (expr->get_location (),
7680 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7681 return 0;
7683 if (stdarg_p (type))
7685 error_at (expr->get_location (),
7686 "argument %u of %<__builtin_tgmath%> has variable arguments",
7687 pos);
7688 return 0;
7690 unsigned int nargs = 0;
7691 function_args_iterator iter;
7692 tree t;
7693 FOREACH_FUNCTION_ARGS (type, t, iter)
7695 if (t == void_type_node)
7696 break;
7697 nargs++;
7699 if (nargs == 0)
7701 error_at (expr->get_location (),
7702 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7703 return 0;
7705 return nargs;
7708 /* Ways in which a parameter or return value of a type-generic macro
7709 may vary between the different functions the macro may call. */
7710 enum tgmath_parm_kind
7712 tgmath_fixed, tgmath_real, tgmath_complex
7715 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7716 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7717 call c_parser_postfix_expression_after_paren_type on encountering them.
7719 postfix-expression:
7720 primary-expression
7721 postfix-expression [ expression ]
7722 postfix-expression ( argument-expression-list[opt] )
7723 postfix-expression . identifier
7724 postfix-expression -> identifier
7725 postfix-expression ++
7726 postfix-expression --
7727 ( type-name ) { initializer-list }
7728 ( type-name ) { initializer-list , }
7730 argument-expression-list:
7731 argument-expression
7732 argument-expression-list , argument-expression
7734 primary-expression:
7735 identifier
7736 constant
7737 string-literal
7738 ( expression )
7739 generic-selection
7741 GNU extensions:
7743 primary-expression:
7744 __func__
7745 (treated as a keyword in GNU C)
7746 __FUNCTION__
7747 __PRETTY_FUNCTION__
7748 ( compound-statement )
7749 __builtin_va_arg ( assignment-expression , type-name )
7750 __builtin_offsetof ( type-name , offsetof-member-designator )
7751 __builtin_choose_expr ( assignment-expression ,
7752 assignment-expression ,
7753 assignment-expression )
7754 __builtin_types_compatible_p ( type-name , type-name )
7755 __builtin_tgmath ( expr-list )
7756 __builtin_complex ( assignment-expression , assignment-expression )
7757 __builtin_shuffle ( assignment-expression , assignment-expression )
7758 __builtin_shuffle ( assignment-expression ,
7759 assignment-expression ,
7760 assignment-expression, )
7762 offsetof-member-designator:
7763 identifier
7764 offsetof-member-designator . identifier
7765 offsetof-member-designator [ expression ]
7767 Objective-C:
7769 primary-expression:
7770 [ objc-receiver objc-message-args ]
7771 @selector ( objc-selector-arg )
7772 @protocol ( identifier )
7773 @encode ( type-name )
7774 objc-string-literal
7775 Classname . identifier
7778 static struct c_expr
7779 c_parser_postfix_expression (c_parser *parser)
7781 struct c_expr expr, e1;
7782 struct c_type_name *t1, *t2;
7783 location_t loc = c_parser_peek_token (parser)->location;
7784 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7785 expr.original_code = ERROR_MARK;
7786 expr.original_type = NULL;
7787 switch (c_parser_peek_token (parser)->type)
7789 case CPP_NUMBER:
7790 expr.value = c_parser_peek_token (parser)->value;
7791 set_c_expr_source_range (&expr, tok_range);
7792 loc = c_parser_peek_token (parser)->location;
7793 c_parser_consume_token (parser);
7794 if (TREE_CODE (expr.value) == FIXED_CST
7795 && !targetm.fixed_point_supported_p ())
7797 error_at (loc, "fixed-point types not supported for this target");
7798 expr.value = error_mark_node;
7800 break;
7801 case CPP_CHAR:
7802 case CPP_CHAR16:
7803 case CPP_CHAR32:
7804 case CPP_WCHAR:
7805 expr.value = c_parser_peek_token (parser)->value;
7806 /* For the purpose of warning when a pointer is compared with
7807 a zero character constant. */
7808 expr.original_type = char_type_node;
7809 set_c_expr_source_range (&expr, tok_range);
7810 c_parser_consume_token (parser);
7811 break;
7812 case CPP_STRING:
7813 case CPP_STRING16:
7814 case CPP_STRING32:
7815 case CPP_WSTRING:
7816 case CPP_UTF8STRING:
7817 expr.value = c_parser_peek_token (parser)->value;
7818 set_c_expr_source_range (&expr, tok_range);
7819 expr.original_code = STRING_CST;
7820 c_parser_consume_token (parser);
7821 break;
7822 case CPP_OBJC_STRING:
7823 gcc_assert (c_dialect_objc ());
7824 expr.value
7825 = objc_build_string_object (c_parser_peek_token (parser)->value);
7826 set_c_expr_source_range (&expr, tok_range);
7827 c_parser_consume_token (parser);
7828 break;
7829 case CPP_NAME:
7830 switch (c_parser_peek_token (parser)->id_kind)
7832 case C_ID_ID:
7834 tree id = c_parser_peek_token (parser)->value;
7835 c_parser_consume_token (parser);
7836 expr.value = build_external_ref (loc, id,
7837 (c_parser_peek_token (parser)->type
7838 == CPP_OPEN_PAREN),
7839 &expr.original_type);
7840 set_c_expr_source_range (&expr, tok_range);
7841 break;
7843 case C_ID_CLASSNAME:
7845 /* Here we parse the Objective-C 2.0 Class.name dot
7846 syntax. */
7847 tree class_name = c_parser_peek_token (parser)->value;
7848 tree component;
7849 c_parser_consume_token (parser);
7850 gcc_assert (c_dialect_objc ());
7851 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7853 expr.set_error ();
7854 break;
7856 if (c_parser_next_token_is_not (parser, CPP_NAME))
7858 c_parser_error (parser, "expected identifier");
7859 expr.set_error ();
7860 break;
7862 c_token *component_tok = c_parser_peek_token (parser);
7863 component = component_tok->value;
7864 location_t end_loc = component_tok->get_finish ();
7865 c_parser_consume_token (parser);
7866 expr.value = objc_build_class_component_ref (class_name,
7867 component);
7868 set_c_expr_source_range (&expr, loc, end_loc);
7869 break;
7871 default:
7872 c_parser_error (parser, "expected expression");
7873 expr.set_error ();
7874 break;
7876 break;
7877 case CPP_OPEN_PAREN:
7878 /* A parenthesized expression, statement expression or compound
7879 literal. */
7880 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7882 /* A statement expression. */
7883 tree stmt;
7884 location_t brace_loc;
7885 c_parser_consume_token (parser);
7886 brace_loc = c_parser_peek_token (parser)->location;
7887 c_parser_consume_token (parser);
7888 if (!building_stmt_list_p ())
7890 error_at (loc, "braced-group within expression allowed "
7891 "only inside a function");
7892 parser->error = true;
7893 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7894 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7895 expr.set_error ();
7896 break;
7898 stmt = c_begin_stmt_expr ();
7899 c_parser_compound_statement_nostart (parser);
7900 location_t close_loc = c_parser_peek_token (parser)->location;
7901 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7902 "expected %<)%>");
7903 pedwarn (loc, OPT_Wpedantic,
7904 "ISO C forbids braced-groups within expressions");
7905 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7906 set_c_expr_source_range (&expr, loc, close_loc);
7907 mark_exp_read (expr.value);
7909 else
7911 /* A parenthesized expression. */
7912 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7913 c_parser_consume_token (parser);
7914 expr = c_parser_expression (parser);
7915 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7916 TREE_NO_WARNING (expr.value) = 1;
7917 if (expr.original_code != C_MAYBE_CONST_EXPR
7918 && expr.original_code != SIZEOF_EXPR)
7919 expr.original_code = ERROR_MARK;
7920 /* Don't change EXPR.ORIGINAL_TYPE. */
7921 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7922 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7924 "expected %<)%>", loc_open_paren);
7926 break;
7927 case CPP_KEYWORD:
7928 switch (c_parser_peek_token (parser)->keyword)
7930 case RID_FUNCTION_NAME:
7931 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7932 "%<__FUNCTION__%> predefined identifier");
7933 expr.value = fname_decl (loc,
7934 c_parser_peek_token (parser)->keyword,
7935 c_parser_peek_token (parser)->value);
7936 set_c_expr_source_range (&expr, loc, loc);
7937 c_parser_consume_token (parser);
7938 break;
7939 case RID_PRETTY_FUNCTION_NAME:
7940 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7941 "%<__PRETTY_FUNCTION__%> predefined identifier");
7942 expr.value = fname_decl (loc,
7943 c_parser_peek_token (parser)->keyword,
7944 c_parser_peek_token (parser)->value);
7945 set_c_expr_source_range (&expr, loc, loc);
7946 c_parser_consume_token (parser);
7947 break;
7948 case RID_C99_FUNCTION_NAME:
7949 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7950 "%<__func__%> predefined identifier");
7951 expr.value = fname_decl (loc,
7952 c_parser_peek_token (parser)->keyword,
7953 c_parser_peek_token (parser)->value);
7954 set_c_expr_source_range (&expr, loc, loc);
7955 c_parser_consume_token (parser);
7956 break;
7957 case RID_VA_ARG:
7959 location_t start_loc = loc;
7960 c_parser_consume_token (parser);
7961 matching_parens parens;
7962 if (!parens.require_open (parser))
7964 expr.set_error ();
7965 break;
7967 e1 = c_parser_expr_no_commas (parser, NULL);
7968 mark_exp_read (e1.value);
7969 e1.value = c_fully_fold (e1.value, false, NULL);
7970 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7972 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7973 expr.set_error ();
7974 break;
7976 loc = c_parser_peek_token (parser)->location;
7977 t1 = c_parser_type_name (parser);
7978 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7979 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7980 "expected %<)%>");
7981 if (t1 == NULL)
7983 expr.set_error ();
7985 else
7987 tree type_expr = NULL_TREE;
7988 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7989 groktypename (t1, &type_expr, NULL));
7990 if (type_expr)
7992 expr.value = build2 (C_MAYBE_CONST_EXPR,
7993 TREE_TYPE (expr.value), type_expr,
7994 expr.value);
7995 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7997 set_c_expr_source_range (&expr, start_loc, end_loc);
8000 break;
8001 case RID_OFFSETOF:
8003 c_parser_consume_token (parser);
8004 matching_parens parens;
8005 if (!parens.require_open (parser))
8007 expr.set_error ();
8008 break;
8010 t1 = c_parser_type_name (parser);
8011 if (t1 == NULL)
8012 parser->error = true;
8013 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8014 gcc_assert (parser->error);
8015 if (parser->error)
8017 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8018 expr.set_error ();
8019 break;
8021 tree type = groktypename (t1, NULL, NULL);
8022 tree offsetof_ref;
8023 if (type == error_mark_node)
8024 offsetof_ref = error_mark_node;
8025 else
8027 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8028 SET_EXPR_LOCATION (offsetof_ref, loc);
8030 /* Parse the second argument to __builtin_offsetof. We
8031 must have one identifier, and beyond that we want to
8032 accept sub structure and sub array references. */
8033 if (c_parser_next_token_is (parser, CPP_NAME))
8035 c_token *comp_tok = c_parser_peek_token (parser);
8036 offsetof_ref = build_component_ref
8037 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8038 c_parser_consume_token (parser);
8039 while (c_parser_next_token_is (parser, CPP_DOT)
8040 || c_parser_next_token_is (parser,
8041 CPP_OPEN_SQUARE)
8042 || c_parser_next_token_is (parser,
8043 CPP_DEREF))
8045 if (c_parser_next_token_is (parser, CPP_DEREF))
8047 loc = c_parser_peek_token (parser)->location;
8048 offsetof_ref = build_array_ref (loc,
8049 offsetof_ref,
8050 integer_zero_node);
8051 goto do_dot;
8053 else if (c_parser_next_token_is (parser, CPP_DOT))
8055 do_dot:
8056 c_parser_consume_token (parser);
8057 if (c_parser_next_token_is_not (parser,
8058 CPP_NAME))
8060 c_parser_error (parser, "expected identifier");
8061 break;
8063 c_token *comp_tok = c_parser_peek_token (parser);
8064 offsetof_ref = build_component_ref
8065 (loc, offsetof_ref, comp_tok->value,
8066 comp_tok->location);
8067 c_parser_consume_token (parser);
8069 else
8071 struct c_expr ce;
8072 tree idx;
8073 loc = c_parser_peek_token (parser)->location;
8074 c_parser_consume_token (parser);
8075 ce = c_parser_expression (parser);
8076 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8077 idx = ce.value;
8078 idx = c_fully_fold (idx, false, NULL);
8079 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8080 "expected %<]%>");
8081 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8085 else
8086 c_parser_error (parser, "expected identifier");
8087 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8088 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8089 "expected %<)%>");
8090 expr.value = fold_offsetof (offsetof_ref);
8091 set_c_expr_source_range (&expr, loc, end_loc);
8093 break;
8094 case RID_CHOOSE_EXPR:
8096 vec<c_expr_t, va_gc> *cexpr_list;
8097 c_expr_t *e1_p, *e2_p, *e3_p;
8098 tree c;
8099 location_t close_paren_loc;
8101 c_parser_consume_token (parser);
8102 if (!c_parser_get_builtin_args (parser,
8103 "__builtin_choose_expr",
8104 &cexpr_list, true,
8105 &close_paren_loc))
8107 expr.set_error ();
8108 break;
8111 if (vec_safe_length (cexpr_list) != 3)
8113 error_at (loc, "wrong number of arguments to "
8114 "%<__builtin_choose_expr%>");
8115 expr.set_error ();
8116 break;
8119 e1_p = &(*cexpr_list)[0];
8120 e2_p = &(*cexpr_list)[1];
8121 e3_p = &(*cexpr_list)[2];
8123 c = e1_p->value;
8124 mark_exp_read (e2_p->value);
8125 mark_exp_read (e3_p->value);
8126 if (TREE_CODE (c) != INTEGER_CST
8127 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8128 error_at (loc,
8129 "first argument to %<__builtin_choose_expr%> not"
8130 " a constant");
8131 constant_expression_warning (c);
8132 expr = integer_zerop (c) ? *e3_p : *e2_p;
8133 set_c_expr_source_range (&expr, loc, close_paren_loc);
8134 break;
8136 case RID_TYPES_COMPATIBLE_P:
8138 c_parser_consume_token (parser);
8139 matching_parens parens;
8140 if (!parens.require_open (parser))
8142 expr.set_error ();
8143 break;
8145 t1 = c_parser_type_name (parser);
8146 if (t1 == NULL)
8148 expr.set_error ();
8149 break;
8151 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8153 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8154 expr.set_error ();
8155 break;
8157 t2 = c_parser_type_name (parser);
8158 if (t2 == NULL)
8160 expr.set_error ();
8161 break;
8163 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8164 parens.skip_until_found_close (parser);
8165 tree e1, e2;
8166 e1 = groktypename (t1, NULL, NULL);
8167 e2 = groktypename (t2, NULL, NULL);
8168 if (e1 == error_mark_node || e2 == error_mark_node)
8170 expr.set_error ();
8171 break;
8174 e1 = TYPE_MAIN_VARIANT (e1);
8175 e2 = TYPE_MAIN_VARIANT (e2);
8177 expr.value
8178 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8179 set_c_expr_source_range (&expr, loc, close_paren_loc);
8181 break;
8182 case RID_BUILTIN_TGMATH:
8184 vec<c_expr_t, va_gc> *cexpr_list;
8185 location_t close_paren_loc;
8187 c_parser_consume_token (parser);
8188 if (!c_parser_get_builtin_args (parser,
8189 "__builtin_tgmath",
8190 &cexpr_list, false,
8191 &close_paren_loc))
8193 expr.set_error ();
8194 break;
8197 if (vec_safe_length (cexpr_list) < 3)
8199 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8200 expr.set_error ();
8201 break;
8204 unsigned int i;
8205 c_expr_t *p;
8206 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8207 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8208 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8209 if (nargs == 0)
8211 expr.set_error ();
8212 break;
8214 if (vec_safe_length (cexpr_list) < nargs)
8216 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8217 expr.set_error ();
8218 break;
8220 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8221 if (num_functions < 2)
8223 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8224 expr.set_error ();
8225 break;
8228 /* The first NUM_FUNCTIONS expressions are the function
8229 pointers. The remaining NARGS expressions are the
8230 arguments that are to be passed to one of those
8231 functions, chosen following <tgmath.h> rules. */
8232 for (unsigned int j = 1; j < num_functions; j++)
8234 unsigned int this_nargs
8235 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8236 if (this_nargs == 0)
8238 expr.set_error ();
8239 goto out;
8241 if (this_nargs != nargs)
8243 error_at ((*cexpr_list)[j].get_location (),
8244 "argument %u of %<__builtin_tgmath%> has "
8245 "wrong number of arguments", j + 1);
8246 expr.set_error ();
8247 goto out;
8251 /* The functions all have the same number of arguments.
8252 Determine whether arguments and return types vary in
8253 ways permitted for <tgmath.h> functions. */
8254 /* The first entry in each of these vectors is for the
8255 return type, subsequent entries for parameter
8256 types. */
8257 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8258 auto_vec<tree> parm_first (nargs + 1);
8259 auto_vec<bool> parm_complex (nargs + 1);
8260 auto_vec<bool> parm_varies (nargs + 1);
8261 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8262 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8263 parm_first.quick_push (first_ret);
8264 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8265 parm_varies.quick_push (false);
8266 function_args_iterator iter;
8267 tree t;
8268 unsigned int argpos;
8269 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8271 if (t == void_type_node)
8272 break;
8273 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8274 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8275 parm_varies.quick_push (false);
8277 for (unsigned int j = 1; j < num_functions; j++)
8279 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8280 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8281 if (ret != parm_first[0])
8283 parm_varies[0] = true;
8284 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8285 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8287 error_at ((*cexpr_list)[0].get_location (),
8288 "invalid type-generic return type for "
8289 "argument %u of %<__builtin_tgmath%>",
8291 expr.set_error ();
8292 goto out;
8294 if (!SCALAR_FLOAT_TYPE_P (ret)
8295 && !COMPLEX_FLOAT_TYPE_P (ret))
8297 error_at ((*cexpr_list)[j].get_location (),
8298 "invalid type-generic return type for "
8299 "argument %u of %<__builtin_tgmath%>",
8300 j + 1);
8301 expr.set_error ();
8302 goto out;
8305 if (TREE_CODE (ret) == COMPLEX_TYPE)
8306 parm_complex[0] = true;
8307 argpos = 1;
8308 FOREACH_FUNCTION_ARGS (type, t, iter)
8310 if (t == void_type_node)
8311 break;
8312 t = TYPE_MAIN_VARIANT (t);
8313 if (t != parm_first[argpos])
8315 parm_varies[argpos] = true;
8316 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8317 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8319 error_at ((*cexpr_list)[0].get_location (),
8320 "invalid type-generic type for "
8321 "argument %u of argument %u of "
8322 "%<__builtin_tgmath%>", argpos, 1);
8323 expr.set_error ();
8324 goto out;
8326 if (!SCALAR_FLOAT_TYPE_P (t)
8327 && !COMPLEX_FLOAT_TYPE_P (t))
8329 error_at ((*cexpr_list)[j].get_location (),
8330 "invalid type-generic type for "
8331 "argument %u of argument %u of "
8332 "%<__builtin_tgmath%>", argpos, j + 1);
8333 expr.set_error ();
8334 goto out;
8337 if (TREE_CODE (t) == COMPLEX_TYPE)
8338 parm_complex[argpos] = true;
8339 argpos++;
8342 enum tgmath_parm_kind max_variation = tgmath_fixed;
8343 for (unsigned int j = 0; j <= nargs; j++)
8345 enum tgmath_parm_kind this_kind;
8346 if (parm_varies[j])
8348 if (parm_complex[j])
8349 max_variation = this_kind = tgmath_complex;
8350 else
8352 this_kind = tgmath_real;
8353 if (max_variation != tgmath_complex)
8354 max_variation = tgmath_real;
8357 else
8358 this_kind = tgmath_fixed;
8359 parm_kind.quick_push (this_kind);
8361 if (max_variation == tgmath_fixed)
8363 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8364 "all have the same type");
8365 expr.set_error ();
8366 break;
8369 /* Identify a parameter (not the return type) that varies,
8370 including with complex types if any variation includes
8371 complex types; there must be at least one such
8372 parameter. */
8373 unsigned int tgarg = 0;
8374 for (unsigned int j = 1; j <= nargs; j++)
8375 if (parm_kind[j] == max_variation)
8377 tgarg = j;
8378 break;
8380 if (tgarg == 0)
8382 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8383 "lack type-generic parameter");
8384 expr.set_error ();
8385 break;
8388 /* Determine the type of the relevant parameter for each
8389 function. */
8390 auto_vec<tree> tg_type (num_functions);
8391 for (unsigned int j = 0; j < num_functions; j++)
8393 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8394 argpos = 1;
8395 FOREACH_FUNCTION_ARGS (type, t, iter)
8397 if (argpos == tgarg)
8399 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8400 break;
8402 argpos++;
8406 /* Verify that the corresponding types are different for
8407 all the listed functions. Also determine whether all
8408 the types are complex, whether all the types are
8409 standard or binary, and whether all the types are
8410 decimal. */
8411 bool all_complex = true;
8412 bool all_binary = true;
8413 bool all_decimal = true;
8414 hash_set<tree> tg_types;
8415 FOR_EACH_VEC_ELT (tg_type, i, t)
8417 if (TREE_CODE (t) == COMPLEX_TYPE)
8418 all_decimal = false;
8419 else
8421 all_complex = false;
8422 if (DECIMAL_FLOAT_TYPE_P (t))
8423 all_binary = false;
8424 else
8425 all_decimal = false;
8427 if (tg_types.add (t))
8429 error_at ((*cexpr_list)[i].get_location (),
8430 "duplicate type-generic parameter type for "
8431 "function argument %u of %<__builtin_tgmath%>",
8432 i + 1);
8433 expr.set_error ();
8434 goto out;
8438 /* Verify that other parameters and the return type whose
8439 types vary have their types varying in the correct
8440 way. */
8441 for (unsigned int j = 0; j < num_functions; j++)
8443 tree exp_type = tg_type[j];
8444 tree exp_real_type = exp_type;
8445 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8446 exp_real_type = TREE_TYPE (exp_type);
8447 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8448 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8449 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8450 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8452 error_at ((*cexpr_list)[j].get_location (),
8453 "bad return type for function argument %u "
8454 "of %<__builtin_tgmath%>", j + 1);
8455 expr.set_error ();
8456 goto out;
8458 argpos = 1;
8459 FOREACH_FUNCTION_ARGS (type, t, iter)
8461 if (t == void_type_node)
8462 break;
8463 t = TYPE_MAIN_VARIANT (t);
8464 if ((parm_kind[argpos] == tgmath_complex
8465 && t != exp_type)
8466 || (parm_kind[argpos] == tgmath_real
8467 && t != exp_real_type))
8469 error_at ((*cexpr_list)[j].get_location (),
8470 "bad type for argument %u of "
8471 "function argument %u of "
8472 "%<__builtin_tgmath%>", argpos, j + 1);
8473 expr.set_error ();
8474 goto out;
8476 argpos++;
8480 /* The functions listed are a valid set of functions for a
8481 <tgmath.h> macro to select between. Identify the
8482 matching function, if any. First, the argument types
8483 must be combined following <tgmath.h> rules. Integer
8484 types are treated as _Decimal64 if any type-generic
8485 argument is decimal, or if the only alternatives for
8486 type-generic arguments are of decimal types, and are
8487 otherwise treated as double (or _Complex double for
8488 complex integer types). After that adjustment, types
8489 are combined following the usual arithmetic
8490 conversions. If the function only accepts complex
8491 arguments, a complex type is produced. */
8492 bool arg_complex = all_complex;
8493 bool arg_binary = all_binary;
8494 bool arg_int_decimal = all_decimal;
8495 for (unsigned int j = 1; j <= nargs; j++)
8497 if (parm_kind[j] == tgmath_fixed)
8498 continue;
8499 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8500 tree type = TREE_TYPE (ce->value);
8501 if (!INTEGRAL_TYPE_P (type)
8502 && !SCALAR_FLOAT_TYPE_P (type)
8503 && TREE_CODE (type) != COMPLEX_TYPE)
8505 error_at (ce->get_location (),
8506 "invalid type of argument %u of type-generic "
8507 "function", j);
8508 expr.set_error ();
8509 goto out;
8511 if (DECIMAL_FLOAT_TYPE_P (type))
8513 arg_int_decimal = true;
8514 if (all_complex)
8516 error_at (ce->get_location (),
8517 "decimal floating-point argument %u to "
8518 "complex-only type-generic function", j);
8519 expr.set_error ();
8520 goto out;
8522 else if (all_binary)
8524 error_at (ce->get_location (),
8525 "decimal floating-point argument %u to "
8526 "binary-only type-generic function", j);
8527 expr.set_error ();
8528 goto out;
8530 else if (arg_complex)
8532 error_at (ce->get_location (),
8533 "both complex and decimal floating-point "
8534 "arguments to type-generic function");
8535 expr.set_error ();
8536 goto out;
8538 else if (arg_binary)
8540 error_at (ce->get_location (),
8541 "both binary and decimal floating-point "
8542 "arguments to type-generic function");
8543 expr.set_error ();
8544 goto out;
8547 else if (TREE_CODE (type) == COMPLEX_TYPE)
8549 arg_complex = true;
8550 if (COMPLEX_FLOAT_TYPE_P (type))
8551 arg_binary = true;
8552 if (all_decimal)
8554 error_at (ce->get_location (),
8555 "complex argument %u to "
8556 "decimal-only type-generic function", j);
8557 expr.set_error ();
8558 goto out;
8560 else if (arg_int_decimal)
8562 error_at (ce->get_location (),
8563 "both complex and decimal floating-point "
8564 "arguments to type-generic function");
8565 expr.set_error ();
8566 goto out;
8569 else if (SCALAR_FLOAT_TYPE_P (type))
8571 arg_binary = true;
8572 if (all_decimal)
8574 error_at (ce->get_location (),
8575 "binary argument %u to "
8576 "decimal-only type-generic function", j);
8577 expr.set_error ();
8578 goto out;
8580 else if (arg_int_decimal)
8582 error_at (ce->get_location (),
8583 "both binary and decimal floating-point "
8584 "arguments to type-generic function");
8585 expr.set_error ();
8586 goto out;
8590 tree arg_real = NULL_TREE;
8591 for (unsigned int j = 1; j <= nargs; j++)
8593 if (parm_kind[j] == tgmath_fixed)
8594 continue;
8595 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8596 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8597 if (TREE_CODE (type) == COMPLEX_TYPE)
8598 type = TREE_TYPE (type);
8599 if (INTEGRAL_TYPE_P (type))
8600 type = (arg_int_decimal
8601 ? dfloat64_type_node
8602 : double_type_node);
8603 if (arg_real == NULL_TREE)
8604 arg_real = type;
8605 else
8606 arg_real = common_type (arg_real, type);
8607 if (arg_real == error_mark_node)
8609 expr.set_error ();
8610 goto out;
8613 tree arg_type = (arg_complex
8614 ? build_complex_type (arg_real)
8615 : arg_real);
8617 /* Look for a function to call with type-generic parameter
8618 type ARG_TYPE. */
8619 c_expr_t *fn = NULL;
8620 for (unsigned int j = 0; j < num_functions; j++)
8622 if (tg_type[j] == arg_type)
8624 fn = &(*cexpr_list)[j];
8625 break;
8628 if (fn == NULL
8629 && parm_kind[0] == tgmath_fixed
8630 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8632 /* Presume this is a macro that rounds its result to a
8633 narrower type, and look for the first function with
8634 at least the range and precision of the argument
8635 type. */
8636 for (unsigned int j = 0; j < num_functions; j++)
8638 if (arg_complex
8639 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8640 continue;
8641 tree real_tg_type = (arg_complex
8642 ? TREE_TYPE (tg_type[j])
8643 : tg_type[j]);
8644 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8645 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8646 continue;
8647 scalar_float_mode arg_mode
8648 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8649 scalar_float_mode tg_mode
8650 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8651 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8652 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8653 if (arg_fmt->b == tg_fmt->b
8654 && arg_fmt->p <= tg_fmt->p
8655 && arg_fmt->emax <= tg_fmt->emax
8656 && (arg_fmt->emin - arg_fmt->p
8657 >= tg_fmt->emin - tg_fmt->p))
8659 fn = &(*cexpr_list)[j];
8660 break;
8664 if (fn == NULL)
8666 error_at (loc, "no matching function for type-generic call");
8667 expr.set_error ();
8668 break;
8671 /* Construct a call to FN. */
8672 vec<tree, va_gc> *args;
8673 vec_alloc (args, nargs);
8674 vec<tree, va_gc> *origtypes;
8675 vec_alloc (origtypes, nargs);
8676 auto_vec<location_t> arg_loc (nargs);
8677 for (unsigned int j = 0; j < nargs; j++)
8679 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8680 args->quick_push (ce->value);
8681 arg_loc.quick_push (ce->get_location ());
8682 origtypes->quick_push (ce->original_type);
8684 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8685 args, origtypes);
8686 set_c_expr_source_range (&expr, loc, close_paren_loc);
8687 break;
8689 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8691 vec<c_expr_t, va_gc> *cexpr_list;
8692 c_expr_t *e2_p;
8693 tree chain_value;
8694 location_t close_paren_loc;
8696 c_parser_consume_token (parser);
8697 if (!c_parser_get_builtin_args (parser,
8698 "__builtin_call_with_static_chain",
8699 &cexpr_list, false,
8700 &close_paren_loc))
8702 expr.set_error ();
8703 break;
8705 if (vec_safe_length (cexpr_list) != 2)
8707 error_at (loc, "wrong number of arguments to "
8708 "%<__builtin_call_with_static_chain%>");
8709 expr.set_error ();
8710 break;
8713 expr = (*cexpr_list)[0];
8714 e2_p = &(*cexpr_list)[1];
8715 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8716 chain_value = e2_p->value;
8717 mark_exp_read (chain_value);
8719 if (TREE_CODE (expr.value) != CALL_EXPR)
8720 error_at (loc, "first argument to "
8721 "%<__builtin_call_with_static_chain%> "
8722 "must be a call expression");
8723 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8724 error_at (loc, "second argument to "
8725 "%<__builtin_call_with_static_chain%> "
8726 "must be a pointer type");
8727 else
8728 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8729 set_c_expr_source_range (&expr, loc, close_paren_loc);
8730 break;
8732 case RID_BUILTIN_COMPLEX:
8734 vec<c_expr_t, va_gc> *cexpr_list;
8735 c_expr_t *e1_p, *e2_p;
8736 location_t close_paren_loc;
8738 c_parser_consume_token (parser);
8739 if (!c_parser_get_builtin_args (parser,
8740 "__builtin_complex",
8741 &cexpr_list, false,
8742 &close_paren_loc))
8744 expr.set_error ();
8745 break;
8748 if (vec_safe_length (cexpr_list) != 2)
8750 error_at (loc, "wrong number of arguments to "
8751 "%<__builtin_complex%>");
8752 expr.set_error ();
8753 break;
8756 e1_p = &(*cexpr_list)[0];
8757 e2_p = &(*cexpr_list)[1];
8759 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8760 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8761 e1_p->value = convert (TREE_TYPE (e1_p->value),
8762 TREE_OPERAND (e1_p->value, 0));
8763 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8764 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8765 e2_p->value = convert (TREE_TYPE (e2_p->value),
8766 TREE_OPERAND (e2_p->value, 0));
8767 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8768 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8769 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8770 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8772 error_at (loc, "%<__builtin_complex%> operand "
8773 "not of real binary floating-point type");
8774 expr.set_error ();
8775 break;
8777 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8778 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8780 error_at (loc,
8781 "%<__builtin_complex%> operands of different types");
8782 expr.set_error ();
8783 break;
8785 pedwarn_c90 (loc, OPT_Wpedantic,
8786 "ISO C90 does not support complex types");
8787 expr.value = build2_loc (loc, COMPLEX_EXPR,
8788 build_complex_type
8789 (TYPE_MAIN_VARIANT
8790 (TREE_TYPE (e1_p->value))),
8791 e1_p->value, e2_p->value);
8792 set_c_expr_source_range (&expr, loc, close_paren_loc);
8793 break;
8795 case RID_BUILTIN_SHUFFLE:
8797 vec<c_expr_t, va_gc> *cexpr_list;
8798 unsigned int i;
8799 c_expr_t *p;
8800 location_t close_paren_loc;
8802 c_parser_consume_token (parser);
8803 if (!c_parser_get_builtin_args (parser,
8804 "__builtin_shuffle",
8805 &cexpr_list, false,
8806 &close_paren_loc))
8808 expr.set_error ();
8809 break;
8812 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8813 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8815 if (vec_safe_length (cexpr_list) == 2)
8816 expr.value =
8817 c_build_vec_perm_expr
8818 (loc, (*cexpr_list)[0].value,
8819 NULL_TREE, (*cexpr_list)[1].value);
8821 else if (vec_safe_length (cexpr_list) == 3)
8822 expr.value =
8823 c_build_vec_perm_expr
8824 (loc, (*cexpr_list)[0].value,
8825 (*cexpr_list)[1].value,
8826 (*cexpr_list)[2].value);
8827 else
8829 error_at (loc, "wrong number of arguments to "
8830 "%<__builtin_shuffle%>");
8831 expr.set_error ();
8833 set_c_expr_source_range (&expr, loc, close_paren_loc);
8834 break;
8836 case RID_AT_SELECTOR:
8838 gcc_assert (c_dialect_objc ());
8839 c_parser_consume_token (parser);
8840 matching_parens parens;
8841 if (!parens.require_open (parser))
8843 expr.set_error ();
8844 break;
8846 tree sel = c_parser_objc_selector_arg (parser);
8847 location_t close_loc = c_parser_peek_token (parser)->location;
8848 parens.skip_until_found_close (parser);
8849 expr.value = objc_build_selector_expr (loc, sel);
8850 set_c_expr_source_range (&expr, loc, close_loc);
8852 break;
8853 case RID_AT_PROTOCOL:
8855 gcc_assert (c_dialect_objc ());
8856 c_parser_consume_token (parser);
8857 matching_parens parens;
8858 if (!parens.require_open (parser))
8860 expr.set_error ();
8861 break;
8863 if (c_parser_next_token_is_not (parser, CPP_NAME))
8865 c_parser_error (parser, "expected identifier");
8866 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8867 expr.set_error ();
8868 break;
8870 tree id = c_parser_peek_token (parser)->value;
8871 c_parser_consume_token (parser);
8872 location_t close_loc = c_parser_peek_token (parser)->location;
8873 parens.skip_until_found_close (parser);
8874 expr.value = objc_build_protocol_expr (id);
8875 set_c_expr_source_range (&expr, loc, close_loc);
8877 break;
8878 case RID_AT_ENCODE:
8880 /* Extension to support C-structures in the archiver. */
8881 gcc_assert (c_dialect_objc ());
8882 c_parser_consume_token (parser);
8883 matching_parens parens;
8884 if (!parens.require_open (parser))
8886 expr.set_error ();
8887 break;
8889 t1 = c_parser_type_name (parser);
8890 if (t1 == NULL)
8892 expr.set_error ();
8893 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8894 break;
8896 location_t close_loc = c_parser_peek_token (parser)->location;
8897 parens.skip_until_found_close (parser);
8898 tree type = groktypename (t1, NULL, NULL);
8899 expr.value = objc_build_encode_expr (type);
8900 set_c_expr_source_range (&expr, loc, close_loc);
8902 break;
8903 case RID_GENERIC:
8904 expr = c_parser_generic_selection (parser);
8905 break;
8906 default:
8907 c_parser_error (parser, "expected expression");
8908 expr.set_error ();
8909 break;
8911 break;
8912 case CPP_OPEN_SQUARE:
8913 if (c_dialect_objc ())
8915 tree receiver, args;
8916 c_parser_consume_token (parser);
8917 receiver = c_parser_objc_receiver (parser);
8918 args = c_parser_objc_message_args (parser);
8919 location_t close_loc = c_parser_peek_token (parser)->location;
8920 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8921 "expected %<]%>");
8922 expr.value = objc_build_message_expr (receiver, args);
8923 set_c_expr_source_range (&expr, loc, close_loc);
8924 break;
8926 /* Else fall through to report error. */
8927 /* FALLTHRU */
8928 default:
8929 c_parser_error (parser, "expected expression");
8930 expr.set_error ();
8931 break;
8933 out:
8934 return c_parser_postfix_expression_after_primary
8935 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8938 /* Parse a postfix expression after a parenthesized type name: the
8939 brace-enclosed initializer of a compound literal, possibly followed
8940 by some postfix operators. This is separate because it is not
8941 possible to tell until after the type name whether a cast
8942 expression has a cast or a compound literal, or whether the operand
8943 of sizeof is a parenthesized type name or starts with a compound
8944 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8945 location of the first token after the parentheses around the type
8946 name. */
8948 static struct c_expr
8949 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8950 struct c_type_name *type_name,
8951 location_t type_loc)
8953 tree type;
8954 struct c_expr init;
8955 bool non_const;
8956 struct c_expr expr;
8957 location_t start_loc;
8958 tree type_expr = NULL_TREE;
8959 bool type_expr_const = true;
8960 check_compound_literal_type (type_loc, type_name);
8961 rich_location richloc (line_table, type_loc);
8962 start_init (NULL_TREE, NULL, 0, &richloc);
8963 type = groktypename (type_name, &type_expr, &type_expr_const);
8964 start_loc = c_parser_peek_token (parser)->location;
8965 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8967 error_at (type_loc, "compound literal has variable size");
8968 type = error_mark_node;
8970 init = c_parser_braced_init (parser, type, false, NULL);
8971 finish_init ();
8972 maybe_warn_string_init (type_loc, type, init);
8974 if (type != error_mark_node
8975 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8976 && current_function_decl)
8978 error ("compound literal qualified by address-space qualifier");
8979 type = error_mark_node;
8982 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8983 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8984 ? CONSTRUCTOR_NON_CONST (init.value)
8985 : init.original_code == C_MAYBE_CONST_EXPR);
8986 non_const |= !type_expr_const;
8987 unsigned int alignas_align = 0;
8988 if (type != error_mark_node
8989 && type_name->specs->align_log != -1)
8991 alignas_align = 1U << type_name->specs->align_log;
8992 if (alignas_align < min_align_of_type (type))
8994 error_at (type_name->specs->locations[cdw_alignas],
8995 "%<_Alignas%> specifiers cannot reduce "
8996 "alignment of compound literal");
8997 alignas_align = 0;
9000 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9001 alignas_align);
9002 set_c_expr_source_range (&expr, init.src_range);
9003 expr.original_code = ERROR_MARK;
9004 expr.original_type = NULL;
9005 if (type != error_mark_node
9006 && expr.value != error_mark_node
9007 && type_expr)
9009 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9011 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9012 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9014 else
9016 gcc_assert (!non_const);
9017 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9018 type_expr, expr.value);
9021 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9024 /* Callback function for sizeof_pointer_memaccess_warning to compare
9025 types. */
9027 static bool
9028 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9030 return comptypes (type1, type2) == 1;
9033 /* Parse a postfix expression after the initial primary or compound
9034 literal; that is, parse a series of postfix operators.
9036 EXPR_LOC is the location of the primary expression. */
9038 static struct c_expr
9039 c_parser_postfix_expression_after_primary (c_parser *parser,
9040 location_t expr_loc,
9041 struct c_expr expr)
9043 struct c_expr orig_expr;
9044 tree ident, idx;
9045 location_t sizeof_arg_loc[3], comp_loc;
9046 tree sizeof_arg[3];
9047 unsigned int literal_zero_mask;
9048 unsigned int i;
9049 vec<tree, va_gc> *exprlist;
9050 vec<tree, va_gc> *origtypes = NULL;
9051 vec<location_t> arg_loc = vNULL;
9052 location_t start;
9053 location_t finish;
9055 while (true)
9057 location_t op_loc = c_parser_peek_token (parser)->location;
9058 switch (c_parser_peek_token (parser)->type)
9060 case CPP_OPEN_SQUARE:
9061 /* Array reference. */
9062 c_parser_consume_token (parser);
9063 idx = c_parser_expression (parser).value;
9064 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9065 "expected %<]%>");
9066 start = expr.get_start ();
9067 finish = parser->tokens_buf[0].location;
9068 expr.value = build_array_ref (op_loc, expr.value, idx);
9069 set_c_expr_source_range (&expr, start, finish);
9070 expr.original_code = ERROR_MARK;
9071 expr.original_type = NULL;
9072 break;
9073 case CPP_OPEN_PAREN:
9074 /* Function call. */
9075 c_parser_consume_token (parser);
9076 for (i = 0; i < 3; i++)
9078 sizeof_arg[i] = NULL_TREE;
9079 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9081 literal_zero_mask = 0;
9082 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9083 exprlist = NULL;
9084 else
9085 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9086 sizeof_arg_loc, sizeof_arg,
9087 &arg_loc, &literal_zero_mask);
9088 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9089 "expected %<)%>");
9090 orig_expr = expr;
9091 mark_exp_read (expr.value);
9092 if (warn_sizeof_pointer_memaccess)
9093 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9094 expr.value, exprlist,
9095 sizeof_arg,
9096 sizeof_ptr_memacc_comptypes);
9097 if (TREE_CODE (expr.value) == FUNCTION_DECL
9098 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9099 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9100 && vec_safe_length (exprlist) == 3)
9102 tree arg0 = (*exprlist)[0];
9103 tree arg2 = (*exprlist)[2];
9104 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9107 start = expr.get_start ();
9108 finish = parser->tokens_buf[0].get_finish ();
9109 expr.value
9110 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9111 exprlist, origtypes);
9112 set_c_expr_source_range (&expr, start, finish);
9114 expr.original_code = ERROR_MARK;
9115 if (TREE_CODE (expr.value) == INTEGER_CST
9116 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9117 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9118 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9119 expr.original_code = C_MAYBE_CONST_EXPR;
9120 expr.original_type = NULL;
9121 if (exprlist)
9123 release_tree_vector (exprlist);
9124 release_tree_vector (origtypes);
9126 arg_loc.release ();
9127 break;
9128 case CPP_DOT:
9129 /* Structure element reference. */
9130 c_parser_consume_token (parser);
9131 expr = default_function_array_conversion (expr_loc, expr);
9132 if (c_parser_next_token_is (parser, CPP_NAME))
9134 c_token *comp_tok = c_parser_peek_token (parser);
9135 ident = comp_tok->value;
9136 comp_loc = comp_tok->location;
9138 else
9140 c_parser_error (parser, "expected identifier");
9141 expr.set_error ();
9142 expr.original_code = ERROR_MARK;
9143 expr.original_type = NULL;
9144 return expr;
9146 start = expr.get_start ();
9147 finish = c_parser_peek_token (parser)->get_finish ();
9148 c_parser_consume_token (parser);
9149 expr.value = build_component_ref (op_loc, expr.value, ident,
9150 comp_loc);
9151 set_c_expr_source_range (&expr, start, finish);
9152 expr.original_code = ERROR_MARK;
9153 if (TREE_CODE (expr.value) != COMPONENT_REF)
9154 expr.original_type = NULL;
9155 else
9157 /* Remember the original type of a bitfield. */
9158 tree field = TREE_OPERAND (expr.value, 1);
9159 if (TREE_CODE (field) != FIELD_DECL)
9160 expr.original_type = NULL;
9161 else
9162 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9164 break;
9165 case CPP_DEREF:
9166 /* Structure element reference. */
9167 c_parser_consume_token (parser);
9168 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9169 if (c_parser_next_token_is (parser, CPP_NAME))
9171 c_token *comp_tok = c_parser_peek_token (parser);
9172 ident = comp_tok->value;
9173 comp_loc = comp_tok->location;
9175 else
9177 c_parser_error (parser, "expected identifier");
9178 expr.set_error ();
9179 expr.original_code = ERROR_MARK;
9180 expr.original_type = NULL;
9181 return expr;
9183 start = expr.get_start ();
9184 finish = c_parser_peek_token (parser)->get_finish ();
9185 c_parser_consume_token (parser);
9186 expr.value = build_component_ref (op_loc,
9187 build_indirect_ref (op_loc,
9188 expr.value,
9189 RO_ARROW),
9190 ident, comp_loc);
9191 set_c_expr_source_range (&expr, start, finish);
9192 expr.original_code = ERROR_MARK;
9193 if (TREE_CODE (expr.value) != COMPONENT_REF)
9194 expr.original_type = NULL;
9195 else
9197 /* Remember the original type of a bitfield. */
9198 tree field = TREE_OPERAND (expr.value, 1);
9199 if (TREE_CODE (field) != FIELD_DECL)
9200 expr.original_type = NULL;
9201 else
9202 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9204 break;
9205 case CPP_PLUS_PLUS:
9206 /* Postincrement. */
9207 start = expr.get_start ();
9208 finish = c_parser_peek_token (parser)->get_finish ();
9209 c_parser_consume_token (parser);
9210 expr = default_function_array_read_conversion (expr_loc, expr);
9211 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9212 expr.value, false);
9213 set_c_expr_source_range (&expr, start, finish);
9214 expr.original_code = ERROR_MARK;
9215 expr.original_type = NULL;
9216 break;
9217 case CPP_MINUS_MINUS:
9218 /* Postdecrement. */
9219 start = expr.get_start ();
9220 finish = c_parser_peek_token (parser)->get_finish ();
9221 c_parser_consume_token (parser);
9222 expr = default_function_array_read_conversion (expr_loc, expr);
9223 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9224 expr.value, false);
9225 set_c_expr_source_range (&expr, start, finish);
9226 expr.original_code = ERROR_MARK;
9227 expr.original_type = NULL;
9228 break;
9229 default:
9230 return expr;
9235 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9237 expression:
9238 assignment-expression
9239 expression , assignment-expression
9242 static struct c_expr
9243 c_parser_expression (c_parser *parser)
9245 location_t tloc = c_parser_peek_token (parser)->location;
9246 struct c_expr expr;
9247 expr = c_parser_expr_no_commas (parser, NULL);
9248 if (c_parser_next_token_is (parser, CPP_COMMA))
9249 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9250 while (c_parser_next_token_is (parser, CPP_COMMA))
9252 struct c_expr next;
9253 tree lhsval;
9254 location_t loc = c_parser_peek_token (parser)->location;
9255 location_t expr_loc;
9256 c_parser_consume_token (parser);
9257 expr_loc = c_parser_peek_token (parser)->location;
9258 lhsval = expr.value;
9259 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9260 lhsval = TREE_OPERAND (lhsval, 1);
9261 if (DECL_P (lhsval) || handled_component_p (lhsval))
9262 mark_exp_read (lhsval);
9263 next = c_parser_expr_no_commas (parser, NULL);
9264 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9265 expr.value = build_compound_expr (loc, expr.value, next.value);
9266 expr.original_code = COMPOUND_EXPR;
9267 expr.original_type = next.original_type;
9269 return expr;
9272 /* Parse an expression and convert functions or arrays to pointers and
9273 lvalues to rvalues. */
9275 static struct c_expr
9276 c_parser_expression_conv (c_parser *parser)
9278 struct c_expr expr;
9279 location_t loc = c_parser_peek_token (parser)->location;
9280 expr = c_parser_expression (parser);
9281 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9282 return expr;
9285 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9286 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9288 static inline void
9289 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9290 unsigned int idx)
9292 if (idx >= HOST_BITS_PER_INT)
9293 return;
9295 c_token *tok = c_parser_peek_token (parser);
9296 switch (tok->type)
9298 case CPP_NUMBER:
9299 case CPP_CHAR:
9300 case CPP_WCHAR:
9301 case CPP_CHAR16:
9302 case CPP_CHAR32:
9303 /* If a parameter is literal zero alone, remember it
9304 for -Wmemset-transposed-args warning. */
9305 if (integer_zerop (tok->value)
9306 && !TREE_OVERFLOW (tok->value)
9307 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9308 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9309 *literal_zero_mask |= 1U << idx;
9310 default:
9311 break;
9315 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9316 functions and arrays to pointers and lvalues to rvalues. If
9317 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9318 locations of function arguments into this vector.
9320 nonempty-expr-list:
9321 assignment-expression
9322 nonempty-expr-list , assignment-expression
9325 static vec<tree, va_gc> *
9326 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9327 vec<tree, va_gc> **p_orig_types,
9328 location_t *sizeof_arg_loc, tree *sizeof_arg,
9329 vec<location_t> *locations,
9330 unsigned int *literal_zero_mask)
9332 vec<tree, va_gc> *ret;
9333 vec<tree, va_gc> *orig_types;
9334 struct c_expr expr;
9335 unsigned int idx = 0;
9337 ret = make_tree_vector ();
9338 if (p_orig_types == NULL)
9339 orig_types = NULL;
9340 else
9341 orig_types = make_tree_vector ();
9343 if (literal_zero_mask)
9344 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9345 expr = c_parser_expr_no_commas (parser, NULL);
9346 if (convert_p)
9347 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9348 if (fold_p)
9349 expr.value = c_fully_fold (expr.value, false, NULL);
9350 ret->quick_push (expr.value);
9351 if (orig_types)
9352 orig_types->quick_push (expr.original_type);
9353 if (locations)
9354 locations->safe_push (expr.get_location ());
9355 if (sizeof_arg != NULL
9356 && expr.original_code == SIZEOF_EXPR)
9358 sizeof_arg[0] = c_last_sizeof_arg;
9359 sizeof_arg_loc[0] = c_last_sizeof_loc;
9361 while (c_parser_next_token_is (parser, CPP_COMMA))
9363 c_parser_consume_token (parser);
9364 if (literal_zero_mask)
9365 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9366 expr = c_parser_expr_no_commas (parser, NULL);
9367 if (convert_p)
9368 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9369 true);
9370 if (fold_p)
9371 expr.value = c_fully_fold (expr.value, false, NULL);
9372 vec_safe_push (ret, expr.value);
9373 if (orig_types)
9374 vec_safe_push (orig_types, expr.original_type);
9375 if (locations)
9376 locations->safe_push (expr.get_location ());
9377 if (++idx < 3
9378 && sizeof_arg != NULL
9379 && expr.original_code == SIZEOF_EXPR)
9381 sizeof_arg[idx] = c_last_sizeof_arg;
9382 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9385 if (orig_types)
9386 *p_orig_types = orig_types;
9387 return ret;
9390 /* Parse Objective-C-specific constructs. */
9392 /* Parse an objc-class-definition.
9394 objc-class-definition:
9395 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9396 objc-class-instance-variables[opt] objc-methodprotolist @end
9397 @implementation identifier objc-superclass[opt]
9398 objc-class-instance-variables[opt]
9399 @interface identifier ( identifier ) objc-protocol-refs[opt]
9400 objc-methodprotolist @end
9401 @interface identifier ( ) objc-protocol-refs[opt]
9402 objc-methodprotolist @end
9403 @implementation identifier ( identifier )
9405 objc-superclass:
9406 : identifier
9408 "@interface identifier (" must start "@interface identifier (
9409 identifier ) ...": objc-methodprotolist in the first production may
9410 not start with a parenthesized identifier as a declarator of a data
9411 definition with no declaration specifiers if the objc-superclass,
9412 objc-protocol-refs and objc-class-instance-variables are omitted. */
9414 static void
9415 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9417 bool iface_p;
9418 tree id1;
9419 tree superclass;
9420 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9421 iface_p = true;
9422 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9423 iface_p = false;
9424 else
9425 gcc_unreachable ();
9427 c_parser_consume_token (parser);
9428 if (c_parser_next_token_is_not (parser, CPP_NAME))
9430 c_parser_error (parser, "expected identifier");
9431 return;
9433 id1 = c_parser_peek_token (parser)->value;
9434 c_parser_consume_token (parser);
9435 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9437 /* We have a category or class extension. */
9438 tree id2;
9439 tree proto = NULL_TREE;
9440 matching_parens parens;
9441 parens.consume_open (parser);
9442 if (c_parser_next_token_is_not (parser, CPP_NAME))
9444 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9446 /* We have a class extension. */
9447 id2 = NULL_TREE;
9449 else
9451 c_parser_error (parser, "expected identifier or %<)%>");
9452 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9453 return;
9456 else
9458 id2 = c_parser_peek_token (parser)->value;
9459 c_parser_consume_token (parser);
9461 parens.skip_until_found_close (parser);
9462 if (!iface_p)
9464 objc_start_category_implementation (id1, id2);
9465 return;
9467 if (c_parser_next_token_is (parser, CPP_LESS))
9468 proto = c_parser_objc_protocol_refs (parser);
9469 objc_start_category_interface (id1, id2, proto, attributes);
9470 c_parser_objc_methodprotolist (parser);
9471 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9472 objc_finish_interface ();
9473 return;
9475 if (c_parser_next_token_is (parser, CPP_COLON))
9477 c_parser_consume_token (parser);
9478 if (c_parser_next_token_is_not (parser, CPP_NAME))
9480 c_parser_error (parser, "expected identifier");
9481 return;
9483 superclass = c_parser_peek_token (parser)->value;
9484 c_parser_consume_token (parser);
9486 else
9487 superclass = NULL_TREE;
9488 if (iface_p)
9490 tree proto = NULL_TREE;
9491 if (c_parser_next_token_is (parser, CPP_LESS))
9492 proto = c_parser_objc_protocol_refs (parser);
9493 objc_start_class_interface (id1, superclass, proto, attributes);
9495 else
9496 objc_start_class_implementation (id1, superclass);
9497 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9498 c_parser_objc_class_instance_variables (parser);
9499 if (iface_p)
9501 objc_continue_interface ();
9502 c_parser_objc_methodprotolist (parser);
9503 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9504 objc_finish_interface ();
9506 else
9508 objc_continue_implementation ();
9509 return;
9513 /* Parse objc-class-instance-variables.
9515 objc-class-instance-variables:
9516 { objc-instance-variable-decl-list[opt] }
9518 objc-instance-variable-decl-list:
9519 objc-visibility-spec
9520 objc-instance-variable-decl ;
9522 objc-instance-variable-decl-list objc-visibility-spec
9523 objc-instance-variable-decl-list objc-instance-variable-decl ;
9524 objc-instance-variable-decl-list ;
9526 objc-visibility-spec:
9527 @private
9528 @protected
9529 @public
9531 objc-instance-variable-decl:
9532 struct-declaration
9535 static void
9536 c_parser_objc_class_instance_variables (c_parser *parser)
9538 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9539 c_parser_consume_token (parser);
9540 while (c_parser_next_token_is_not (parser, CPP_EOF))
9542 tree decls;
9543 /* Parse any stray semicolon. */
9544 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9546 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9547 "extra semicolon");
9548 c_parser_consume_token (parser);
9549 continue;
9551 /* Stop if at the end of the instance variables. */
9552 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9554 c_parser_consume_token (parser);
9555 break;
9557 /* Parse any objc-visibility-spec. */
9558 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9560 c_parser_consume_token (parser);
9561 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9562 continue;
9564 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9566 c_parser_consume_token (parser);
9567 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9568 continue;
9570 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9572 c_parser_consume_token (parser);
9573 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9574 continue;
9576 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9578 c_parser_consume_token (parser);
9579 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9580 continue;
9582 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9584 c_parser_pragma (parser, pragma_external, NULL);
9585 continue;
9588 /* Parse some comma-separated declarations. */
9589 decls = c_parser_struct_declaration (parser);
9590 if (decls == NULL)
9592 /* There is a syntax error. We want to skip the offending
9593 tokens up to the next ';' (included) or '}'
9594 (excluded). */
9596 /* First, skip manually a ')' or ']'. This is because they
9597 reduce the nesting level, so c_parser_skip_until_found()
9598 wouldn't be able to skip past them. */
9599 c_token *token = c_parser_peek_token (parser);
9600 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9601 c_parser_consume_token (parser);
9603 /* Then, do the standard skipping. */
9604 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9606 /* We hopefully recovered. Start normal parsing again. */
9607 parser->error = false;
9608 continue;
9610 else
9612 /* Comma-separated instance variables are chained together
9613 in reverse order; add them one by one. */
9614 tree ivar = nreverse (decls);
9615 for (; ivar; ivar = DECL_CHAIN (ivar))
9616 objc_add_instance_variable (copy_node (ivar));
9618 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9622 /* Parse an objc-class-declaration.
9624 objc-class-declaration:
9625 @class identifier-list ;
9628 static void
9629 c_parser_objc_class_declaration (c_parser *parser)
9631 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9632 c_parser_consume_token (parser);
9633 /* Any identifiers, including those declared as type names, are OK
9634 here. */
9635 while (true)
9637 tree id;
9638 if (c_parser_next_token_is_not (parser, CPP_NAME))
9640 c_parser_error (parser, "expected identifier");
9641 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9642 parser->error = false;
9643 return;
9645 id = c_parser_peek_token (parser)->value;
9646 objc_declare_class (id);
9647 c_parser_consume_token (parser);
9648 if (c_parser_next_token_is (parser, CPP_COMMA))
9649 c_parser_consume_token (parser);
9650 else
9651 break;
9653 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9656 /* Parse an objc-alias-declaration.
9658 objc-alias-declaration:
9659 @compatibility_alias identifier identifier ;
9662 static void
9663 c_parser_objc_alias_declaration (c_parser *parser)
9665 tree id1, id2;
9666 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9667 c_parser_consume_token (parser);
9668 if (c_parser_next_token_is_not (parser, CPP_NAME))
9670 c_parser_error (parser, "expected identifier");
9671 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9672 return;
9674 id1 = c_parser_peek_token (parser)->value;
9675 c_parser_consume_token (parser);
9676 if (c_parser_next_token_is_not (parser, CPP_NAME))
9678 c_parser_error (parser, "expected identifier");
9679 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9680 return;
9682 id2 = c_parser_peek_token (parser)->value;
9683 c_parser_consume_token (parser);
9684 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9685 objc_declare_alias (id1, id2);
9688 /* Parse an objc-protocol-definition.
9690 objc-protocol-definition:
9691 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9692 @protocol identifier-list ;
9694 "@protocol identifier ;" should be resolved as "@protocol
9695 identifier-list ;": objc-methodprotolist may not start with a
9696 semicolon in the first alternative if objc-protocol-refs are
9697 omitted. */
9699 static void
9700 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9702 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9704 c_parser_consume_token (parser);
9705 if (c_parser_next_token_is_not (parser, CPP_NAME))
9707 c_parser_error (parser, "expected identifier");
9708 return;
9710 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9711 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9713 /* Any identifiers, including those declared as type names, are
9714 OK here. */
9715 while (true)
9717 tree id;
9718 if (c_parser_next_token_is_not (parser, CPP_NAME))
9720 c_parser_error (parser, "expected identifier");
9721 break;
9723 id = c_parser_peek_token (parser)->value;
9724 objc_declare_protocol (id, attributes);
9725 c_parser_consume_token (parser);
9726 if (c_parser_next_token_is (parser, CPP_COMMA))
9727 c_parser_consume_token (parser);
9728 else
9729 break;
9731 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9733 else
9735 tree id = c_parser_peek_token (parser)->value;
9736 tree proto = NULL_TREE;
9737 c_parser_consume_token (parser);
9738 if (c_parser_next_token_is (parser, CPP_LESS))
9739 proto = c_parser_objc_protocol_refs (parser);
9740 parser->objc_pq_context = true;
9741 objc_start_protocol (id, proto, attributes);
9742 c_parser_objc_methodprotolist (parser);
9743 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9744 parser->objc_pq_context = false;
9745 objc_finish_interface ();
9749 /* Parse an objc-method-type.
9751 objc-method-type:
9755 Return true if it is a class method (+) and false if it is
9756 an instance method (-).
9758 static inline bool
9759 c_parser_objc_method_type (c_parser *parser)
9761 switch (c_parser_peek_token (parser)->type)
9763 case CPP_PLUS:
9764 c_parser_consume_token (parser);
9765 return true;
9766 case CPP_MINUS:
9767 c_parser_consume_token (parser);
9768 return false;
9769 default:
9770 gcc_unreachable ();
9774 /* Parse an objc-method-definition.
9776 objc-method-definition:
9777 objc-method-type objc-method-decl ;[opt] compound-statement
9780 static void
9781 c_parser_objc_method_definition (c_parser *parser)
9783 bool is_class_method = c_parser_objc_method_type (parser);
9784 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9785 parser->objc_pq_context = true;
9786 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9787 &expr);
9788 if (decl == error_mark_node)
9789 return; /* Bail here. */
9791 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9793 c_parser_consume_token (parser);
9794 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9795 "extra semicolon in method definition specified");
9798 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9800 c_parser_error (parser, "expected %<{%>");
9801 return;
9804 parser->objc_pq_context = false;
9805 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9807 add_stmt (c_parser_compound_statement (parser));
9808 objc_finish_method_definition (current_function_decl);
9810 else
9812 /* This code is executed when we find a method definition
9813 outside of an @implementation context (or invalid for other
9814 reasons). Parse the method (to keep going) but do not emit
9815 any code.
9817 c_parser_compound_statement (parser);
9821 /* Parse an objc-methodprotolist.
9823 objc-methodprotolist:
9824 empty
9825 objc-methodprotolist objc-methodproto
9826 objc-methodprotolist declaration
9827 objc-methodprotolist ;
9828 @optional
9829 @required
9831 The declaration is a data definition, which may be missing
9832 declaration specifiers under the same rules and diagnostics as
9833 other data definitions outside functions, and the stray semicolon
9834 is diagnosed the same way as a stray semicolon outside a
9835 function. */
9837 static void
9838 c_parser_objc_methodprotolist (c_parser *parser)
9840 while (true)
9842 /* The list is terminated by @end. */
9843 switch (c_parser_peek_token (parser)->type)
9845 case CPP_SEMICOLON:
9846 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9847 "ISO C does not allow extra %<;%> outside of a function");
9848 c_parser_consume_token (parser);
9849 break;
9850 case CPP_PLUS:
9851 case CPP_MINUS:
9852 c_parser_objc_methodproto (parser);
9853 break;
9854 case CPP_PRAGMA:
9855 c_parser_pragma (parser, pragma_external, NULL);
9856 break;
9857 case CPP_EOF:
9858 return;
9859 default:
9860 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9861 return;
9862 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9863 c_parser_objc_at_property_declaration (parser);
9864 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9866 objc_set_method_opt (true);
9867 c_parser_consume_token (parser);
9869 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9871 objc_set_method_opt (false);
9872 c_parser_consume_token (parser);
9874 else
9875 c_parser_declaration_or_fndef (parser, false, false, true,
9876 false, true, NULL, vNULL);
9877 break;
9882 /* Parse an objc-methodproto.
9884 objc-methodproto:
9885 objc-method-type objc-method-decl ;
9888 static void
9889 c_parser_objc_methodproto (c_parser *parser)
9891 bool is_class_method = c_parser_objc_method_type (parser);
9892 tree decl, attributes = NULL_TREE;
9894 /* Remember protocol qualifiers in prototypes. */
9895 parser->objc_pq_context = true;
9896 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9897 NULL);
9898 /* Forget protocol qualifiers now. */
9899 parser->objc_pq_context = false;
9901 /* Do not allow the presence of attributes to hide an erroneous
9902 method implementation in the interface section. */
9903 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9905 c_parser_error (parser, "expected %<;%>");
9906 return;
9909 if (decl != error_mark_node)
9910 objc_add_method_declaration (is_class_method, decl, attributes);
9912 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9915 /* If we are at a position that method attributes may be present, check that
9916 there are not any parsed already (a syntax error) and then collect any
9917 specified at the current location. Finally, if new attributes were present,
9918 check that the next token is legal ( ';' for decls and '{' for defs). */
9920 static bool
9921 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9923 bool bad = false;
9924 if (*attributes)
9926 c_parser_error (parser,
9927 "method attributes must be specified at the end only");
9928 *attributes = NULL_TREE;
9929 bad = true;
9932 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9933 *attributes = c_parser_attributes (parser);
9935 /* If there were no attributes here, just report any earlier error. */
9936 if (*attributes == NULL_TREE || bad)
9937 return bad;
9939 /* If the attributes are followed by a ; or {, then just report any earlier
9940 error. */
9941 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9942 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9943 return bad;
9945 /* We've got attributes, but not at the end. */
9946 c_parser_error (parser,
9947 "expected %<;%> or %<{%> after method attribute definition");
9948 return true;
9951 /* Parse an objc-method-decl.
9953 objc-method-decl:
9954 ( objc-type-name ) objc-selector
9955 objc-selector
9956 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9957 objc-keyword-selector objc-optparmlist
9958 attributes
9960 objc-keyword-selector:
9961 objc-keyword-decl
9962 objc-keyword-selector objc-keyword-decl
9964 objc-keyword-decl:
9965 objc-selector : ( objc-type-name ) identifier
9966 objc-selector : identifier
9967 : ( objc-type-name ) identifier
9968 : identifier
9970 objc-optparmlist:
9971 objc-optparms objc-optellipsis
9973 objc-optparms:
9974 empty
9975 objc-opt-parms , parameter-declaration
9977 objc-optellipsis:
9978 empty
9979 , ...
9982 static tree
9983 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9984 tree *attributes, tree *expr)
9986 tree type = NULL_TREE;
9987 tree sel;
9988 tree parms = NULL_TREE;
9989 bool ellipsis = false;
9990 bool attr_err = false;
9992 *attributes = NULL_TREE;
9993 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9995 matching_parens parens;
9996 parens.consume_open (parser);
9997 type = c_parser_objc_type_name (parser);
9998 parens.skip_until_found_close (parser);
10000 sel = c_parser_objc_selector (parser);
10001 /* If there is no selector, or a colon follows, we have an
10002 objc-keyword-selector. If there is a selector, and a colon does
10003 not follow, that selector ends the objc-method-decl. */
10004 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10006 tree tsel = sel;
10007 tree list = NULL_TREE;
10008 while (true)
10010 tree atype = NULL_TREE, id, keyworddecl;
10011 tree param_attr = NULL_TREE;
10012 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10013 break;
10014 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10016 c_parser_consume_token (parser);
10017 atype = c_parser_objc_type_name (parser);
10018 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10019 "expected %<)%>");
10021 /* New ObjC allows attributes on method parameters. */
10022 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10023 param_attr = c_parser_attributes (parser);
10024 if (c_parser_next_token_is_not (parser, CPP_NAME))
10026 c_parser_error (parser, "expected identifier");
10027 return error_mark_node;
10029 id = c_parser_peek_token (parser)->value;
10030 c_parser_consume_token (parser);
10031 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10032 list = chainon (list, keyworddecl);
10033 tsel = c_parser_objc_selector (parser);
10034 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10035 break;
10038 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10040 /* Parse the optional parameter list. Optional Objective-C
10041 method parameters follow the C syntax, and may include '...'
10042 to denote a variable number of arguments. */
10043 parms = make_node (TREE_LIST);
10044 while (c_parser_next_token_is (parser, CPP_COMMA))
10046 struct c_parm *parm;
10047 c_parser_consume_token (parser);
10048 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10050 ellipsis = true;
10051 c_parser_consume_token (parser);
10052 attr_err |= c_parser_objc_maybe_method_attributes
10053 (parser, attributes) ;
10054 break;
10056 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10057 if (parm == NULL)
10058 break;
10059 parms = chainon (parms,
10060 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10062 sel = list;
10064 else
10065 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10067 if (sel == NULL)
10069 c_parser_error (parser, "objective-c method declaration is expected");
10070 return error_mark_node;
10073 if (attr_err)
10074 return error_mark_node;
10076 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10079 /* Parse an objc-type-name.
10081 objc-type-name:
10082 objc-type-qualifiers[opt] type-name
10083 objc-type-qualifiers[opt]
10085 objc-type-qualifiers:
10086 objc-type-qualifier
10087 objc-type-qualifiers objc-type-qualifier
10089 objc-type-qualifier: one of
10090 in out inout bycopy byref oneway
10093 static tree
10094 c_parser_objc_type_name (c_parser *parser)
10096 tree quals = NULL_TREE;
10097 struct c_type_name *type_name = NULL;
10098 tree type = NULL_TREE;
10099 while (true)
10101 c_token *token = c_parser_peek_token (parser);
10102 if (token->type == CPP_KEYWORD
10103 && (token->keyword == RID_IN
10104 || token->keyword == RID_OUT
10105 || token->keyword == RID_INOUT
10106 || token->keyword == RID_BYCOPY
10107 || token->keyword == RID_BYREF
10108 || token->keyword == RID_ONEWAY))
10110 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10111 c_parser_consume_token (parser);
10113 else
10114 break;
10116 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10117 type_name = c_parser_type_name (parser);
10118 if (type_name)
10119 type = groktypename (type_name, NULL, NULL);
10121 /* If the type is unknown, and error has already been produced and
10122 we need to recover from the error. In that case, use NULL_TREE
10123 for the type, as if no type had been specified; this will use the
10124 default type ('id') which is good for error recovery. */
10125 if (type == error_mark_node)
10126 type = NULL_TREE;
10128 return build_tree_list (quals, type);
10131 /* Parse objc-protocol-refs.
10133 objc-protocol-refs:
10134 < identifier-list >
10137 static tree
10138 c_parser_objc_protocol_refs (c_parser *parser)
10140 tree list = NULL_TREE;
10141 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10142 c_parser_consume_token (parser);
10143 /* Any identifiers, including those declared as type names, are OK
10144 here. */
10145 while (true)
10147 tree id;
10148 if (c_parser_next_token_is_not (parser, CPP_NAME))
10150 c_parser_error (parser, "expected identifier");
10151 break;
10153 id = c_parser_peek_token (parser)->value;
10154 list = chainon (list, build_tree_list (NULL_TREE, id));
10155 c_parser_consume_token (parser);
10156 if (c_parser_next_token_is (parser, CPP_COMMA))
10157 c_parser_consume_token (parser);
10158 else
10159 break;
10161 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10162 return list;
10165 /* Parse an objc-try-catch-finally-statement.
10167 objc-try-catch-finally-statement:
10168 @try compound-statement objc-catch-list[opt]
10169 @try compound-statement objc-catch-list[opt] @finally compound-statement
10171 objc-catch-list:
10172 @catch ( objc-catch-parameter-declaration ) compound-statement
10173 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10175 objc-catch-parameter-declaration:
10176 parameter-declaration
10177 '...'
10179 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10181 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10182 for C++. Keep them in sync. */
10184 static void
10185 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10187 location_t location;
10188 tree stmt;
10190 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10191 c_parser_consume_token (parser);
10192 location = c_parser_peek_token (parser)->location;
10193 objc_maybe_warn_exceptions (location);
10194 stmt = c_parser_compound_statement (parser);
10195 objc_begin_try_stmt (location, stmt);
10197 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10199 struct c_parm *parm;
10200 tree parameter_declaration = error_mark_node;
10201 bool seen_open_paren = false;
10203 c_parser_consume_token (parser);
10204 matching_parens parens;
10205 if (!parens.require_open (parser))
10206 seen_open_paren = true;
10207 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10209 /* We have "@catch (...)" (where the '...' are literally
10210 what is in the code). Skip the '...'.
10211 parameter_declaration is set to NULL_TREE, and
10212 objc_being_catch_clauses() knows that that means
10213 '...'. */
10214 c_parser_consume_token (parser);
10215 parameter_declaration = NULL_TREE;
10217 else
10219 /* We have "@catch (NSException *exception)" or something
10220 like that. Parse the parameter declaration. */
10221 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10222 if (parm == NULL)
10223 parameter_declaration = error_mark_node;
10224 else
10225 parameter_declaration = grokparm (parm, NULL);
10227 if (seen_open_paren)
10228 parens.require_close (parser);
10229 else
10231 /* If there was no open parenthesis, we are recovering from
10232 an error, and we are trying to figure out what mistake
10233 the user has made. */
10235 /* If there is an immediate closing parenthesis, the user
10236 probably forgot the opening one (ie, they typed "@catch
10237 NSException *e)". Parse the closing parenthesis and keep
10238 going. */
10239 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10240 c_parser_consume_token (parser);
10242 /* If these is no immediate closing parenthesis, the user
10243 probably doesn't know that parenthesis are required at
10244 all (ie, they typed "@catch NSException *e"). So, just
10245 forget about the closing parenthesis and keep going. */
10247 objc_begin_catch_clause (parameter_declaration);
10248 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10249 c_parser_compound_statement_nostart (parser);
10250 objc_finish_catch_clause ();
10252 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10254 c_parser_consume_token (parser);
10255 location = c_parser_peek_token (parser)->location;
10256 stmt = c_parser_compound_statement (parser);
10257 objc_build_finally_clause (location, stmt);
10259 objc_finish_try_stmt ();
10262 /* Parse an objc-synchronized-statement.
10264 objc-synchronized-statement:
10265 @synchronized ( expression ) compound-statement
10268 static void
10269 c_parser_objc_synchronized_statement (c_parser *parser)
10271 location_t loc;
10272 tree expr, stmt;
10273 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10274 c_parser_consume_token (parser);
10275 loc = c_parser_peek_token (parser)->location;
10276 objc_maybe_warn_exceptions (loc);
10277 matching_parens parens;
10278 if (parens.require_open (parser))
10280 struct c_expr ce = c_parser_expression (parser);
10281 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10282 expr = ce.value;
10283 expr = c_fully_fold (expr, false, NULL);
10284 parens.skip_until_found_close (parser);
10286 else
10287 expr = error_mark_node;
10288 stmt = c_parser_compound_statement (parser);
10289 objc_build_synchronized (loc, expr, stmt);
10292 /* Parse an objc-selector; return NULL_TREE without an error if the
10293 next token is not an objc-selector.
10295 objc-selector:
10296 identifier
10297 one of
10298 enum struct union if else while do for switch case default
10299 break continue return goto asm sizeof typeof __alignof
10300 unsigned long const short volatile signed restrict _Complex
10301 in out inout bycopy byref oneway int char float double void _Bool
10302 _Atomic
10304 ??? Why this selection of keywords but not, for example, storage
10305 class specifiers? */
10307 static tree
10308 c_parser_objc_selector (c_parser *parser)
10310 c_token *token = c_parser_peek_token (parser);
10311 tree value = token->value;
10312 if (token->type == CPP_NAME)
10314 c_parser_consume_token (parser);
10315 return value;
10317 if (token->type != CPP_KEYWORD)
10318 return NULL_TREE;
10319 switch (token->keyword)
10321 case RID_ENUM:
10322 case RID_STRUCT:
10323 case RID_UNION:
10324 case RID_IF:
10325 case RID_ELSE:
10326 case RID_WHILE:
10327 case RID_DO:
10328 case RID_FOR:
10329 case RID_SWITCH:
10330 case RID_CASE:
10331 case RID_DEFAULT:
10332 case RID_BREAK:
10333 case RID_CONTINUE:
10334 case RID_RETURN:
10335 case RID_GOTO:
10336 case RID_ASM:
10337 case RID_SIZEOF:
10338 case RID_TYPEOF:
10339 case RID_ALIGNOF:
10340 case RID_UNSIGNED:
10341 case RID_LONG:
10342 case RID_CONST:
10343 case RID_SHORT:
10344 case RID_VOLATILE:
10345 case RID_SIGNED:
10346 case RID_RESTRICT:
10347 case RID_COMPLEX:
10348 case RID_IN:
10349 case RID_OUT:
10350 case RID_INOUT:
10351 case RID_BYCOPY:
10352 case RID_BYREF:
10353 case RID_ONEWAY:
10354 case RID_INT:
10355 case RID_CHAR:
10356 case RID_FLOAT:
10357 case RID_DOUBLE:
10358 CASE_RID_FLOATN_NX:
10359 case RID_VOID:
10360 case RID_BOOL:
10361 case RID_ATOMIC:
10362 case RID_AUTO_TYPE:
10363 case RID_INT_N_0:
10364 case RID_INT_N_1:
10365 case RID_INT_N_2:
10366 case RID_INT_N_3:
10367 c_parser_consume_token (parser);
10368 return value;
10369 default:
10370 return NULL_TREE;
10374 /* Parse an objc-selector-arg.
10376 objc-selector-arg:
10377 objc-selector
10378 objc-keywordname-list
10380 objc-keywordname-list:
10381 objc-keywordname
10382 objc-keywordname-list objc-keywordname
10384 objc-keywordname:
10385 objc-selector :
10389 static tree
10390 c_parser_objc_selector_arg (c_parser *parser)
10392 tree sel = c_parser_objc_selector (parser);
10393 tree list = NULL_TREE;
10394 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10395 return sel;
10396 while (true)
10398 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10399 return list;
10400 list = chainon (list, build_tree_list (sel, NULL_TREE));
10401 sel = c_parser_objc_selector (parser);
10402 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10403 break;
10405 return list;
10408 /* Parse an objc-receiver.
10410 objc-receiver:
10411 expression
10412 class-name
10413 type-name
10416 static tree
10417 c_parser_objc_receiver (c_parser *parser)
10419 location_t loc = c_parser_peek_token (parser)->location;
10421 if (c_parser_peek_token (parser)->type == CPP_NAME
10422 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10423 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10425 tree id = c_parser_peek_token (parser)->value;
10426 c_parser_consume_token (parser);
10427 return objc_get_class_reference (id);
10429 struct c_expr ce = c_parser_expression (parser);
10430 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10431 return c_fully_fold (ce.value, false, NULL);
10434 /* Parse objc-message-args.
10436 objc-message-args:
10437 objc-selector
10438 objc-keywordarg-list
10440 objc-keywordarg-list:
10441 objc-keywordarg
10442 objc-keywordarg-list objc-keywordarg
10444 objc-keywordarg:
10445 objc-selector : objc-keywordexpr
10446 : objc-keywordexpr
10449 static tree
10450 c_parser_objc_message_args (c_parser *parser)
10452 tree sel = c_parser_objc_selector (parser);
10453 tree list = NULL_TREE;
10454 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10455 return sel;
10456 while (true)
10458 tree keywordexpr;
10459 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10460 return error_mark_node;
10461 keywordexpr = c_parser_objc_keywordexpr (parser);
10462 list = chainon (list, build_tree_list (sel, keywordexpr));
10463 sel = c_parser_objc_selector (parser);
10464 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10465 break;
10467 return list;
10470 /* Parse an objc-keywordexpr.
10472 objc-keywordexpr:
10473 nonempty-expr-list
10476 static tree
10477 c_parser_objc_keywordexpr (c_parser *parser)
10479 tree ret;
10480 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10481 NULL, NULL, NULL, NULL);
10482 if (vec_safe_length (expr_list) == 1)
10484 /* Just return the expression, remove a level of
10485 indirection. */
10486 ret = (*expr_list)[0];
10488 else
10490 /* We have a comma expression, we will collapse later. */
10491 ret = build_tree_list_vec (expr_list);
10493 release_tree_vector (expr_list);
10494 return ret;
10497 /* A check, needed in several places, that ObjC interface, implementation or
10498 method definitions are not prefixed by incorrect items. */
10499 static bool
10500 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10501 struct c_declspecs *specs)
10503 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10504 || specs->typespec_kind != ctsk_none)
10506 c_parser_error (parser,
10507 "no type or storage class may be specified here,");
10508 c_parser_skip_to_end_of_block_or_statement (parser);
10509 return true;
10511 return false;
10514 /* Parse an Objective-C @property declaration. The syntax is:
10516 objc-property-declaration:
10517 '@property' objc-property-attributes[opt] struct-declaration ;
10519 objc-property-attributes:
10520 '(' objc-property-attribute-list ')'
10522 objc-property-attribute-list:
10523 objc-property-attribute
10524 objc-property-attribute-list, objc-property-attribute
10526 objc-property-attribute
10527 'getter' = identifier
10528 'setter' = identifier
10529 'readonly'
10530 'readwrite'
10531 'assign'
10532 'retain'
10533 'copy'
10534 'nonatomic'
10536 For example:
10537 @property NSString *name;
10538 @property (readonly) id object;
10539 @property (retain, nonatomic, getter=getTheName) id name;
10540 @property int a, b, c;
10542 PS: This function is identical to cp_parser_objc_at_propery_declaration
10543 for C++. Keep them in sync. */
10544 static void
10545 c_parser_objc_at_property_declaration (c_parser *parser)
10547 /* The following variables hold the attributes of the properties as
10548 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10549 seen. When we see an attribute, we set them to 'true' (if they
10550 are boolean properties) or to the identifier (if they have an
10551 argument, ie, for getter and setter). Note that here we only
10552 parse the list of attributes, check the syntax and accumulate the
10553 attributes that we find. objc_add_property_declaration() will
10554 then process the information. */
10555 bool property_assign = false;
10556 bool property_copy = false;
10557 tree property_getter_ident = NULL_TREE;
10558 bool property_nonatomic = false;
10559 bool property_readonly = false;
10560 bool property_readwrite = false;
10561 bool property_retain = false;
10562 tree property_setter_ident = NULL_TREE;
10564 /* 'properties' is the list of properties that we read. Usually a
10565 single one, but maybe more (eg, in "@property int a, b, c;" there
10566 are three). */
10567 tree properties;
10568 location_t loc;
10570 loc = c_parser_peek_token (parser)->location;
10571 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10573 c_parser_consume_token (parser); /* Eat '@property'. */
10575 /* Parse the optional attribute list... */
10576 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10578 matching_parens parens;
10580 /* Eat the '(' */
10581 parens.consume_open (parser);
10583 /* Property attribute keywords are valid now. */
10584 parser->objc_property_attr_context = true;
10586 while (true)
10588 bool syntax_error = false;
10589 c_token *token = c_parser_peek_token (parser);
10590 enum rid keyword;
10592 if (token->type != CPP_KEYWORD)
10594 if (token->type == CPP_CLOSE_PAREN)
10595 c_parser_error (parser, "expected identifier");
10596 else
10598 c_parser_consume_token (parser);
10599 c_parser_error (parser, "unknown property attribute");
10601 break;
10603 keyword = token->keyword;
10604 c_parser_consume_token (parser);
10605 switch (keyword)
10607 case RID_ASSIGN: property_assign = true; break;
10608 case RID_COPY: property_copy = true; break;
10609 case RID_NONATOMIC: property_nonatomic = true; break;
10610 case RID_READONLY: property_readonly = true; break;
10611 case RID_READWRITE: property_readwrite = true; break;
10612 case RID_RETAIN: property_retain = true; break;
10614 case RID_GETTER:
10615 case RID_SETTER:
10616 if (c_parser_next_token_is_not (parser, CPP_EQ))
10618 if (keyword == RID_GETTER)
10619 c_parser_error (parser,
10620 "missing %<=%> (after %<getter%> attribute)");
10621 else
10622 c_parser_error (parser,
10623 "missing %<=%> (after %<setter%> attribute)");
10624 syntax_error = true;
10625 break;
10627 c_parser_consume_token (parser); /* eat the = */
10628 if (c_parser_next_token_is_not (parser, CPP_NAME))
10630 c_parser_error (parser, "expected identifier");
10631 syntax_error = true;
10632 break;
10634 if (keyword == RID_SETTER)
10636 if (property_setter_ident != NULL_TREE)
10637 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10638 else
10639 property_setter_ident = c_parser_peek_token (parser)->value;
10640 c_parser_consume_token (parser);
10641 if (c_parser_next_token_is_not (parser, CPP_COLON))
10642 c_parser_error (parser, "setter name must terminate with %<:%>");
10643 else
10644 c_parser_consume_token (parser);
10646 else
10648 if (property_getter_ident != NULL_TREE)
10649 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10650 else
10651 property_getter_ident = c_parser_peek_token (parser)->value;
10652 c_parser_consume_token (parser);
10654 break;
10655 default:
10656 c_parser_error (parser, "unknown property attribute");
10657 syntax_error = true;
10658 break;
10661 if (syntax_error)
10662 break;
10664 if (c_parser_next_token_is (parser, CPP_COMMA))
10665 c_parser_consume_token (parser);
10666 else
10667 break;
10669 parser->objc_property_attr_context = false;
10670 parens.skip_until_found_close (parser);
10672 /* ... and the property declaration(s). */
10673 properties = c_parser_struct_declaration (parser);
10675 if (properties == error_mark_node)
10677 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10678 parser->error = false;
10679 return;
10682 if (properties == NULL_TREE)
10683 c_parser_error (parser, "expected identifier");
10684 else
10686 /* Comma-separated properties are chained together in
10687 reverse order; add them one by one. */
10688 properties = nreverse (properties);
10690 for (; properties; properties = TREE_CHAIN (properties))
10691 objc_add_property_declaration (loc, copy_node (properties),
10692 property_readonly, property_readwrite,
10693 property_assign, property_retain,
10694 property_copy, property_nonatomic,
10695 property_getter_ident, property_setter_ident);
10698 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10699 parser->error = false;
10702 /* Parse an Objective-C @synthesize declaration. The syntax is:
10704 objc-synthesize-declaration:
10705 @synthesize objc-synthesize-identifier-list ;
10707 objc-synthesize-identifier-list:
10708 objc-synthesize-identifier
10709 objc-synthesize-identifier-list, objc-synthesize-identifier
10711 objc-synthesize-identifier
10712 identifier
10713 identifier = identifier
10715 For example:
10716 @synthesize MyProperty;
10717 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10719 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10720 for C++. Keep them in sync.
10722 static void
10723 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10725 tree list = NULL_TREE;
10726 location_t loc;
10727 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10728 loc = c_parser_peek_token (parser)->location;
10730 c_parser_consume_token (parser);
10731 while (true)
10733 tree property, ivar;
10734 if (c_parser_next_token_is_not (parser, CPP_NAME))
10736 c_parser_error (parser, "expected identifier");
10737 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10738 /* Once we find the semicolon, we can resume normal parsing.
10739 We have to reset parser->error manually because
10740 c_parser_skip_until_found() won't reset it for us if the
10741 next token is precisely a semicolon. */
10742 parser->error = false;
10743 return;
10745 property = c_parser_peek_token (parser)->value;
10746 c_parser_consume_token (parser);
10747 if (c_parser_next_token_is (parser, CPP_EQ))
10749 c_parser_consume_token (parser);
10750 if (c_parser_next_token_is_not (parser, CPP_NAME))
10752 c_parser_error (parser, "expected identifier");
10753 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10754 parser->error = false;
10755 return;
10757 ivar = c_parser_peek_token (parser)->value;
10758 c_parser_consume_token (parser);
10760 else
10761 ivar = NULL_TREE;
10762 list = chainon (list, build_tree_list (ivar, property));
10763 if (c_parser_next_token_is (parser, CPP_COMMA))
10764 c_parser_consume_token (parser);
10765 else
10766 break;
10768 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10769 objc_add_synthesize_declaration (loc, list);
10772 /* Parse an Objective-C @dynamic declaration. The syntax is:
10774 objc-dynamic-declaration:
10775 @dynamic identifier-list ;
10777 For example:
10778 @dynamic MyProperty;
10779 @dynamic MyProperty, AnotherProperty;
10781 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10782 for C++. Keep them in sync.
10784 static void
10785 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10787 tree list = NULL_TREE;
10788 location_t loc;
10789 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10790 loc = c_parser_peek_token (parser)->location;
10792 c_parser_consume_token (parser);
10793 while (true)
10795 tree property;
10796 if (c_parser_next_token_is_not (parser, CPP_NAME))
10798 c_parser_error (parser, "expected identifier");
10799 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10800 parser->error = false;
10801 return;
10803 property = c_parser_peek_token (parser)->value;
10804 list = chainon (list, build_tree_list (NULL_TREE, property));
10805 c_parser_consume_token (parser);
10806 if (c_parser_next_token_is (parser, CPP_COMMA))
10807 c_parser_consume_token (parser);
10808 else
10809 break;
10811 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10812 objc_add_dynamic_declaration (loc, list);
10816 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10817 should be considered, statements. ALLOW_STMT is true if we're within
10818 the context of a function and such pragmas are to be allowed. Returns
10819 true if we actually parsed such a pragma. */
10821 static bool
10822 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10824 unsigned int id;
10825 const char *construct = NULL;
10827 id = c_parser_peek_token (parser)->pragma_kind;
10828 gcc_assert (id != PRAGMA_NONE);
10830 switch (id)
10832 case PRAGMA_OACC_DECLARE:
10833 c_parser_oacc_declare (parser);
10834 return false;
10836 case PRAGMA_OACC_ENTER_DATA:
10837 if (context != pragma_compound)
10839 construct = "acc enter data";
10840 in_compound:
10841 if (context == pragma_stmt)
10843 error_at (c_parser_peek_token (parser)->location,
10844 "%<#pragma %s%> may only be used in compound "
10845 "statements", construct);
10846 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10847 return false;
10849 goto bad_stmt;
10851 c_parser_oacc_enter_exit_data (parser, true);
10852 return false;
10854 case PRAGMA_OACC_EXIT_DATA:
10855 if (context != pragma_compound)
10857 construct = "acc exit data";
10858 goto in_compound;
10860 c_parser_oacc_enter_exit_data (parser, false);
10861 return false;
10863 case PRAGMA_OACC_ROUTINE:
10864 if (context != pragma_external)
10866 error_at (c_parser_peek_token (parser)->location,
10867 "%<#pragma acc routine%> must be at file scope");
10868 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10869 return false;
10871 c_parser_oacc_routine (parser, context);
10872 return false;
10874 case PRAGMA_OACC_UPDATE:
10875 if (context != pragma_compound)
10877 construct = "acc update";
10878 goto in_compound;
10880 c_parser_oacc_update (parser);
10881 return false;
10883 case PRAGMA_OMP_BARRIER:
10884 if (context != pragma_compound)
10886 construct = "omp barrier";
10887 goto in_compound;
10889 c_parser_omp_barrier (parser);
10890 return false;
10892 case PRAGMA_OMP_FLUSH:
10893 if (context != pragma_compound)
10895 construct = "omp flush";
10896 goto in_compound;
10898 c_parser_omp_flush (parser);
10899 return false;
10901 case PRAGMA_OMP_TASKWAIT:
10902 if (context != pragma_compound)
10904 construct = "omp taskwait";
10905 goto in_compound;
10907 c_parser_omp_taskwait (parser);
10908 return false;
10910 case PRAGMA_OMP_TASKYIELD:
10911 if (context != pragma_compound)
10913 construct = "omp taskyield";
10914 goto in_compound;
10916 c_parser_omp_taskyield (parser);
10917 return false;
10919 case PRAGMA_OMP_CANCEL:
10920 if (context != pragma_compound)
10922 construct = "omp cancel";
10923 goto in_compound;
10925 c_parser_omp_cancel (parser);
10926 return false;
10928 case PRAGMA_OMP_CANCELLATION_POINT:
10929 c_parser_omp_cancellation_point (parser, context);
10930 return false;
10932 case PRAGMA_OMP_THREADPRIVATE:
10933 c_parser_omp_threadprivate (parser);
10934 return false;
10936 case PRAGMA_OMP_TARGET:
10937 return c_parser_omp_target (parser, context, if_p);
10939 case PRAGMA_OMP_END_DECLARE_TARGET:
10940 c_parser_omp_end_declare_target (parser);
10941 return false;
10943 case PRAGMA_OMP_SECTION:
10944 error_at (c_parser_peek_token (parser)->location,
10945 "%<#pragma omp section%> may only be used in "
10946 "%<#pragma omp sections%> construct");
10947 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10948 return false;
10950 case PRAGMA_OMP_DECLARE:
10951 c_parser_omp_declare (parser, context);
10952 return false;
10954 case PRAGMA_OMP_ORDERED:
10955 return c_parser_omp_ordered (parser, context, if_p);
10957 case PRAGMA_IVDEP:
10958 c_parser_consume_pragma (parser);
10959 c_parser_skip_to_pragma_eol (parser);
10960 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10961 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10962 && !c_parser_next_token_is_keyword (parser, RID_DO))
10964 c_parser_error (parser, "for, while or do statement expected");
10965 return false;
10967 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10968 c_parser_for_statement (parser, true, if_p);
10969 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10970 c_parser_while_statement (parser, true, if_p);
10971 else
10972 c_parser_do_statement (parser, true);
10973 return false;
10975 case PRAGMA_GCC_PCH_PREPROCESS:
10976 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10977 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10978 return false;
10980 case PRAGMA_OACC_WAIT:
10981 if (context != pragma_compound)
10983 construct = "acc wait";
10984 goto in_compound;
10986 /* FALL THROUGH. */
10988 default:
10989 if (id < PRAGMA_FIRST_EXTERNAL)
10991 if (context != pragma_stmt && context != pragma_compound)
10993 bad_stmt:
10994 c_parser_error (parser, "expected declaration specifiers");
10995 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10996 return false;
10998 c_parser_omp_construct (parser, if_p);
10999 return true;
11001 break;
11004 c_parser_consume_pragma (parser);
11005 c_invoke_pragma_handler (id);
11007 /* Skip to EOL, but suppress any error message. Those will have been
11008 generated by the handler routine through calling error, as opposed
11009 to calling c_parser_error. */
11010 parser->error = true;
11011 c_parser_skip_to_pragma_eol (parser);
11013 return false;
11016 /* The interface the pragma parsers have to the lexer. */
11018 enum cpp_ttype
11019 pragma_lex (tree *value, location_t *loc)
11021 c_token *tok = c_parser_peek_token (the_parser);
11022 enum cpp_ttype ret = tok->type;
11024 *value = tok->value;
11025 if (loc)
11026 *loc = tok->location;
11028 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11029 ret = CPP_EOF;
11030 else
11032 if (ret == CPP_KEYWORD)
11033 ret = CPP_NAME;
11034 c_parser_consume_token (the_parser);
11037 return ret;
11040 static void
11041 c_parser_pragma_pch_preprocess (c_parser *parser)
11043 tree name = NULL;
11045 c_parser_consume_pragma (parser);
11046 if (c_parser_next_token_is (parser, CPP_STRING))
11048 name = c_parser_peek_token (parser)->value;
11049 c_parser_consume_token (parser);
11051 else
11052 c_parser_error (parser, "expected string literal");
11053 c_parser_skip_to_pragma_eol (parser);
11055 if (name)
11056 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11059 /* OpenACC and OpenMP parsing routines. */
11061 /* Returns name of the next clause.
11062 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11063 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11064 returned and the token is consumed. */
11066 static pragma_omp_clause
11067 c_parser_omp_clause_name (c_parser *parser)
11069 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11071 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11072 result = PRAGMA_OACC_CLAUSE_AUTO;
11073 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11074 result = PRAGMA_OMP_CLAUSE_IF;
11075 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11076 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11077 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11078 result = PRAGMA_OMP_CLAUSE_FOR;
11079 else if (c_parser_next_token_is (parser, CPP_NAME))
11081 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11083 switch (p[0])
11085 case 'a':
11086 if (!strcmp ("aligned", p))
11087 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11088 else if (!strcmp ("async", p))
11089 result = PRAGMA_OACC_CLAUSE_ASYNC;
11090 break;
11091 case 'c':
11092 if (!strcmp ("collapse", p))
11093 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11094 else if (!strcmp ("copy", p))
11095 result = PRAGMA_OACC_CLAUSE_COPY;
11096 else if (!strcmp ("copyin", p))
11097 result = PRAGMA_OMP_CLAUSE_COPYIN;
11098 else if (!strcmp ("copyout", p))
11099 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11100 else if (!strcmp ("copyprivate", p))
11101 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11102 else if (!strcmp ("create", p))
11103 result = PRAGMA_OACC_CLAUSE_CREATE;
11104 break;
11105 case 'd':
11106 if (!strcmp ("defaultmap", p))
11107 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11108 else if (!strcmp ("delete", p))
11109 result = PRAGMA_OACC_CLAUSE_DELETE;
11110 else if (!strcmp ("depend", p))
11111 result = PRAGMA_OMP_CLAUSE_DEPEND;
11112 else if (!strcmp ("device", p))
11113 result = PRAGMA_OMP_CLAUSE_DEVICE;
11114 else if (!strcmp ("deviceptr", p))
11115 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11116 else if (!strcmp ("device_resident", p))
11117 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11118 else if (!strcmp ("dist_schedule", p))
11119 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11120 break;
11121 case 'f':
11122 if (!strcmp ("final", p))
11123 result = PRAGMA_OMP_CLAUSE_FINAL;
11124 else if (!strcmp ("firstprivate", p))
11125 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11126 else if (!strcmp ("from", p))
11127 result = PRAGMA_OMP_CLAUSE_FROM;
11128 break;
11129 case 'g':
11130 if (!strcmp ("gang", p))
11131 result = PRAGMA_OACC_CLAUSE_GANG;
11132 else if (!strcmp ("grainsize", p))
11133 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11134 break;
11135 case 'h':
11136 if (!strcmp ("hint", p))
11137 result = PRAGMA_OMP_CLAUSE_HINT;
11138 else if (!strcmp ("host", p))
11139 result = PRAGMA_OACC_CLAUSE_HOST;
11140 break;
11141 case 'i':
11142 if (!strcmp ("inbranch", p))
11143 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11144 else if (!strcmp ("independent", p))
11145 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11146 else if (!strcmp ("is_device_ptr", p))
11147 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11148 break;
11149 case 'l':
11150 if (!strcmp ("lastprivate", p))
11151 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11152 else if (!strcmp ("linear", p))
11153 result = PRAGMA_OMP_CLAUSE_LINEAR;
11154 else if (!strcmp ("link", p))
11155 result = PRAGMA_OMP_CLAUSE_LINK;
11156 break;
11157 case 'm':
11158 if (!strcmp ("map", p))
11159 result = PRAGMA_OMP_CLAUSE_MAP;
11160 else if (!strcmp ("mergeable", p))
11161 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11162 break;
11163 case 'n':
11164 if (!strcmp ("nogroup", p))
11165 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11166 else if (!strcmp ("notinbranch", p))
11167 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11168 else if (!strcmp ("nowait", p))
11169 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11170 else if (!strcmp ("num_gangs", p))
11171 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11172 else if (!strcmp ("num_tasks", p))
11173 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11174 else if (!strcmp ("num_teams", p))
11175 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11176 else if (!strcmp ("num_threads", p))
11177 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11178 else if (!strcmp ("num_workers", p))
11179 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11180 break;
11181 case 'o':
11182 if (!strcmp ("ordered", p))
11183 result = PRAGMA_OMP_CLAUSE_ORDERED;
11184 break;
11185 case 'p':
11186 if (!strcmp ("parallel", p))
11187 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11188 else if (!strcmp ("present", p))
11189 result = PRAGMA_OACC_CLAUSE_PRESENT;
11190 else if (!strcmp ("present_or_copy", p)
11191 || !strcmp ("pcopy", p))
11192 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
11193 else if (!strcmp ("present_or_copyin", p)
11194 || !strcmp ("pcopyin", p))
11195 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
11196 else if (!strcmp ("present_or_copyout", p)
11197 || !strcmp ("pcopyout", p))
11198 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
11199 else if (!strcmp ("present_or_create", p)
11200 || !strcmp ("pcreate", p))
11201 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
11202 else if (!strcmp ("priority", p))
11203 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11204 else if (!strcmp ("private", p))
11205 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11206 else if (!strcmp ("proc_bind", p))
11207 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11208 break;
11209 case 'r':
11210 if (!strcmp ("reduction", p))
11211 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11212 break;
11213 case 's':
11214 if (!strcmp ("safelen", p))
11215 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11216 else if (!strcmp ("schedule", p))
11217 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11218 else if (!strcmp ("sections", p))
11219 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11220 else if (!strcmp ("seq", p))
11221 result = PRAGMA_OACC_CLAUSE_SEQ;
11222 else if (!strcmp ("shared", p))
11223 result = PRAGMA_OMP_CLAUSE_SHARED;
11224 else if (!strcmp ("simd", p))
11225 result = PRAGMA_OMP_CLAUSE_SIMD;
11226 else if (!strcmp ("simdlen", p))
11227 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11228 else if (!strcmp ("self", p))
11229 result = PRAGMA_OACC_CLAUSE_SELF;
11230 break;
11231 case 't':
11232 if (!strcmp ("taskgroup", p))
11233 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11234 else if (!strcmp ("thread_limit", p))
11235 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11236 else if (!strcmp ("threads", p))
11237 result = PRAGMA_OMP_CLAUSE_THREADS;
11238 else if (!strcmp ("tile", p))
11239 result = PRAGMA_OACC_CLAUSE_TILE;
11240 else if (!strcmp ("to", p))
11241 result = PRAGMA_OMP_CLAUSE_TO;
11242 break;
11243 case 'u':
11244 if (!strcmp ("uniform", p))
11245 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11246 else if (!strcmp ("untied", p))
11247 result = PRAGMA_OMP_CLAUSE_UNTIED;
11248 else if (!strcmp ("use_device", p))
11249 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11250 else if (!strcmp ("use_device_ptr", p))
11251 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11252 break;
11253 case 'v':
11254 if (!strcmp ("vector", p))
11255 result = PRAGMA_OACC_CLAUSE_VECTOR;
11256 else if (!strcmp ("vector_length", p))
11257 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11258 break;
11259 case 'w':
11260 if (!strcmp ("wait", p))
11261 result = PRAGMA_OACC_CLAUSE_WAIT;
11262 else if (!strcmp ("worker", p))
11263 result = PRAGMA_OACC_CLAUSE_WORKER;
11264 break;
11268 if (result != PRAGMA_OMP_CLAUSE_NONE)
11269 c_parser_consume_token (parser);
11271 return result;
11274 /* Validate that a clause of the given type does not already exist. */
11276 static void
11277 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11278 const char *name)
11280 tree c;
11282 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11283 if (OMP_CLAUSE_CODE (c) == code)
11285 location_t loc = OMP_CLAUSE_LOCATION (c);
11286 error_at (loc, "too many %qs clauses", name);
11287 break;
11291 /* OpenACC 2.0
11292 Parse wait clause or wait directive parameters. */
11294 static tree
11295 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11297 vec<tree, va_gc> *args;
11298 tree t, args_tree;
11300 matching_parens parens;
11301 if (!parens.require_open (parser))
11302 return list;
11304 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11306 if (args->length () == 0)
11308 c_parser_error (parser, "expected integer expression before ')'");
11309 release_tree_vector (args);
11310 return list;
11313 args_tree = build_tree_list_vec (args);
11315 for (t = args_tree; t; t = TREE_CHAIN (t))
11317 tree targ = TREE_VALUE (t);
11319 if (targ != error_mark_node)
11321 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11323 c_parser_error (parser, "expression must be integral");
11324 targ = error_mark_node;
11326 else
11328 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11330 OMP_CLAUSE_DECL (c) = targ;
11331 OMP_CLAUSE_CHAIN (c) = list;
11332 list = c;
11337 release_tree_vector (args);
11338 parens.require_close (parser);
11339 return list;
11342 /* OpenACC 2.0, OpenMP 2.5:
11343 variable-list:
11344 identifier
11345 variable-list , identifier
11347 If KIND is nonzero, create the appropriate node and install the
11348 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11349 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11351 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11352 return the list created. */
11354 static tree
11355 c_parser_omp_variable_list (c_parser *parser,
11356 location_t clause_loc,
11357 enum omp_clause_code kind, tree list)
11359 if (c_parser_next_token_is_not (parser, CPP_NAME)
11360 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11361 c_parser_error (parser, "expected identifier");
11363 while (c_parser_next_token_is (parser, CPP_NAME)
11364 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11366 tree t = lookup_name (c_parser_peek_token (parser)->value);
11368 if (t == NULL_TREE)
11370 undeclared_variable (c_parser_peek_token (parser)->location,
11371 c_parser_peek_token (parser)->value);
11372 t = error_mark_node;
11375 c_parser_consume_token (parser);
11377 if (t == error_mark_node)
11379 else if (kind != 0)
11381 switch (kind)
11383 case OMP_CLAUSE__CACHE_:
11384 /* The OpenACC cache directive explicitly only allows "array
11385 elements or subarrays". */
11386 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11388 c_parser_error (parser, "expected %<[%>");
11389 t = error_mark_node;
11390 break;
11392 /* FALLTHROUGH */
11393 case OMP_CLAUSE_MAP:
11394 case OMP_CLAUSE_FROM:
11395 case OMP_CLAUSE_TO:
11396 while (c_parser_next_token_is (parser, CPP_DOT))
11398 location_t op_loc = c_parser_peek_token (parser)->location;
11399 c_parser_consume_token (parser);
11400 if (!c_parser_next_token_is (parser, CPP_NAME))
11402 c_parser_error (parser, "expected identifier");
11403 t = error_mark_node;
11404 break;
11407 c_token *comp_tok = c_parser_peek_token (parser);
11408 tree ident = comp_tok->value;
11409 location_t comp_loc = comp_tok->location;
11410 c_parser_consume_token (parser);
11411 t = build_component_ref (op_loc, t, ident, comp_loc);
11413 /* FALLTHROUGH */
11414 case OMP_CLAUSE_DEPEND:
11415 case OMP_CLAUSE_REDUCTION:
11416 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11418 tree low_bound = NULL_TREE, length = NULL_TREE;
11420 c_parser_consume_token (parser);
11421 if (!c_parser_next_token_is (parser, CPP_COLON))
11423 location_t expr_loc
11424 = c_parser_peek_token (parser)->location;
11425 c_expr expr = c_parser_expression (parser);
11426 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11427 false, true);
11428 low_bound = expr.value;
11430 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11431 length = integer_one_node;
11432 else
11434 /* Look for `:'. */
11435 if (!c_parser_require (parser, CPP_COLON,
11436 "expected %<:%>"))
11438 t = error_mark_node;
11439 break;
11441 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11443 location_t expr_loc
11444 = c_parser_peek_token (parser)->location;
11445 c_expr expr = c_parser_expression (parser);
11446 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11447 false, true);
11448 length = expr.value;
11451 /* Look for the closing `]'. */
11452 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11453 "expected %<]%>"))
11455 t = error_mark_node;
11456 break;
11459 t = tree_cons (low_bound, length, t);
11461 break;
11462 default:
11463 break;
11466 if (t != error_mark_node)
11468 tree u = build_omp_clause (clause_loc, kind);
11469 OMP_CLAUSE_DECL (u) = t;
11470 OMP_CLAUSE_CHAIN (u) = list;
11471 list = u;
11474 else
11475 list = tree_cons (t, NULL_TREE, list);
11477 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11478 break;
11480 c_parser_consume_token (parser);
11483 return list;
11486 /* Similarly, but expect leading and trailing parenthesis. This is a very
11487 common case for OpenACC and OpenMP clauses. */
11489 static tree
11490 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11491 tree list)
11493 /* The clauses location. */
11494 location_t loc = c_parser_peek_token (parser)->location;
11496 matching_parens parens;
11497 if (parens.require_open (parser))
11499 list = c_parser_omp_variable_list (parser, loc, kind, list);
11500 parens.skip_until_found_close (parser);
11502 return list;
11505 /* OpenACC 2.0:
11506 copy ( variable-list )
11507 copyin ( variable-list )
11508 copyout ( variable-list )
11509 create ( variable-list )
11510 delete ( variable-list )
11511 present ( variable-list )
11512 present_or_copy ( variable-list )
11513 pcopy ( variable-list )
11514 present_or_copyin ( variable-list )
11515 pcopyin ( variable-list )
11516 present_or_copyout ( variable-list )
11517 pcopyout ( variable-list )
11518 present_or_create ( variable-list )
11519 pcreate ( variable-list ) */
11521 static tree
11522 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11523 tree list)
11525 enum gomp_map_kind kind;
11526 switch (c_kind)
11528 case PRAGMA_OACC_CLAUSE_COPY:
11529 kind = GOMP_MAP_FORCE_TOFROM;
11530 break;
11531 case PRAGMA_OACC_CLAUSE_COPYIN:
11532 kind = GOMP_MAP_FORCE_TO;
11533 break;
11534 case PRAGMA_OACC_CLAUSE_COPYOUT:
11535 kind = GOMP_MAP_FORCE_FROM;
11536 break;
11537 case PRAGMA_OACC_CLAUSE_CREATE:
11538 kind = GOMP_MAP_FORCE_ALLOC;
11539 break;
11540 case PRAGMA_OACC_CLAUSE_DELETE:
11541 kind = GOMP_MAP_DELETE;
11542 break;
11543 case PRAGMA_OACC_CLAUSE_DEVICE:
11544 kind = GOMP_MAP_FORCE_TO;
11545 break;
11546 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11547 kind = GOMP_MAP_DEVICE_RESIDENT;
11548 break;
11549 case PRAGMA_OACC_CLAUSE_HOST:
11550 case PRAGMA_OACC_CLAUSE_SELF:
11551 kind = GOMP_MAP_FORCE_FROM;
11552 break;
11553 case PRAGMA_OACC_CLAUSE_LINK:
11554 kind = GOMP_MAP_LINK;
11555 break;
11556 case PRAGMA_OACC_CLAUSE_PRESENT:
11557 kind = GOMP_MAP_FORCE_PRESENT;
11558 break;
11559 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11560 kind = GOMP_MAP_TOFROM;
11561 break;
11562 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11563 kind = GOMP_MAP_TO;
11564 break;
11565 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11566 kind = GOMP_MAP_FROM;
11567 break;
11568 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11569 kind = GOMP_MAP_ALLOC;
11570 break;
11571 default:
11572 gcc_unreachable ();
11574 tree nl, c;
11575 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11577 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11578 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11580 return nl;
11583 /* OpenACC 2.0:
11584 deviceptr ( variable-list ) */
11586 static tree
11587 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11589 location_t loc = c_parser_peek_token (parser)->location;
11590 tree vars, t;
11592 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11593 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11594 variable-list must only allow for pointer variables. */
11595 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11596 for (t = vars; t && t; t = TREE_CHAIN (t))
11598 tree v = TREE_PURPOSE (t);
11600 /* FIXME diagnostics: Ideally we should keep individual
11601 locations for all the variables in the var list to make the
11602 following errors more precise. Perhaps
11603 c_parser_omp_var_list_parens() should construct a list of
11604 locations to go along with the var list. */
11606 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11607 error_at (loc, "%qD is not a variable", v);
11608 else if (TREE_TYPE (v) == error_mark_node)
11610 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11611 error_at (loc, "%qD is not a pointer variable", v);
11613 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11614 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11615 OMP_CLAUSE_DECL (u) = v;
11616 OMP_CLAUSE_CHAIN (u) = list;
11617 list = u;
11620 return list;
11623 /* OpenACC 2.0, OpenMP 3.0:
11624 collapse ( constant-expression ) */
11626 static tree
11627 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11629 tree c, num = error_mark_node;
11630 HOST_WIDE_INT n;
11631 location_t loc;
11633 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11634 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11636 loc = c_parser_peek_token (parser)->location;
11637 matching_parens parens;
11638 if (parens.require_open (parser))
11640 num = c_parser_expr_no_commas (parser, NULL).value;
11641 parens.skip_until_found_close (parser);
11643 if (num == error_mark_node)
11644 return list;
11645 mark_exp_read (num);
11646 num = c_fully_fold (num, false, NULL);
11647 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11648 || !tree_fits_shwi_p (num)
11649 || (n = tree_to_shwi (num)) <= 0
11650 || (int) n != n)
11652 error_at (loc,
11653 "collapse argument needs positive constant integer expression");
11654 return list;
11656 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11657 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11658 OMP_CLAUSE_CHAIN (c) = list;
11659 return c;
11662 /* OpenMP 2.5:
11663 copyin ( variable-list ) */
11665 static tree
11666 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11668 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11671 /* OpenMP 2.5:
11672 copyprivate ( variable-list ) */
11674 static tree
11675 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11677 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11680 /* OpenMP 2.5:
11681 default ( none | shared )
11683 OpenACC:
11684 default ( none | present ) */
11686 static tree
11687 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11689 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11690 location_t loc = c_parser_peek_token (parser)->location;
11691 tree c;
11693 matching_parens parens;
11694 if (!parens.require_open (parser))
11695 return list;
11696 if (c_parser_next_token_is (parser, CPP_NAME))
11698 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11700 switch (p[0])
11702 case 'n':
11703 if (strcmp ("none", p) != 0)
11704 goto invalid_kind;
11705 kind = OMP_CLAUSE_DEFAULT_NONE;
11706 break;
11708 case 'p':
11709 if (strcmp ("present", p) != 0 || !is_oacc)
11710 goto invalid_kind;
11711 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11712 break;
11714 case 's':
11715 if (strcmp ("shared", p) != 0 || is_oacc)
11716 goto invalid_kind;
11717 kind = OMP_CLAUSE_DEFAULT_SHARED;
11718 break;
11720 default:
11721 goto invalid_kind;
11724 c_parser_consume_token (parser);
11726 else
11728 invalid_kind:
11729 if (is_oacc)
11730 c_parser_error (parser, "expected %<none%> or %<present%>");
11731 else
11732 c_parser_error (parser, "expected %<none%> or %<shared%>");
11734 parens.skip_until_found_close (parser);
11736 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11737 return list;
11739 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11740 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11741 OMP_CLAUSE_CHAIN (c) = list;
11742 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11744 return c;
11747 /* OpenMP 2.5:
11748 firstprivate ( variable-list ) */
11750 static tree
11751 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11753 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11756 /* OpenMP 3.1:
11757 final ( expression ) */
11759 static tree
11760 c_parser_omp_clause_final (c_parser *parser, tree list)
11762 location_t loc = c_parser_peek_token (parser)->location;
11763 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11765 tree t = c_parser_paren_condition (parser);
11766 tree c;
11768 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11770 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11771 OMP_CLAUSE_FINAL_EXPR (c) = t;
11772 OMP_CLAUSE_CHAIN (c) = list;
11773 list = c;
11775 else
11776 c_parser_error (parser, "expected %<(%>");
11778 return list;
11781 /* OpenACC, OpenMP 2.5:
11782 if ( expression )
11784 OpenMP 4.5:
11785 if ( directive-name-modifier : expression )
11787 directive-name-modifier:
11788 parallel | task | taskloop | target data | target | target update
11789 | target enter data | target exit data */
11791 static tree
11792 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11794 location_t location = c_parser_peek_token (parser)->location;
11795 enum tree_code if_modifier = ERROR_MARK;
11797 matching_parens parens;
11798 if (!parens.require_open (parser))
11799 return list;
11801 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11803 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11804 int n = 2;
11805 if (strcmp (p, "parallel") == 0)
11806 if_modifier = OMP_PARALLEL;
11807 else if (strcmp (p, "task") == 0)
11808 if_modifier = OMP_TASK;
11809 else if (strcmp (p, "taskloop") == 0)
11810 if_modifier = OMP_TASKLOOP;
11811 else if (strcmp (p, "target") == 0)
11813 if_modifier = OMP_TARGET;
11814 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11816 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11817 if (strcmp ("data", p) == 0)
11818 if_modifier = OMP_TARGET_DATA;
11819 else if (strcmp ("update", p) == 0)
11820 if_modifier = OMP_TARGET_UPDATE;
11821 else if (strcmp ("enter", p) == 0)
11822 if_modifier = OMP_TARGET_ENTER_DATA;
11823 else if (strcmp ("exit", p) == 0)
11824 if_modifier = OMP_TARGET_EXIT_DATA;
11825 if (if_modifier != OMP_TARGET)
11827 n = 3;
11828 c_parser_consume_token (parser);
11830 else
11832 location_t loc = c_parser_peek_2nd_token (parser)->location;
11833 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11834 "or %<exit%>");
11835 if_modifier = ERROR_MARK;
11837 if (if_modifier == OMP_TARGET_ENTER_DATA
11838 || if_modifier == OMP_TARGET_EXIT_DATA)
11840 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11842 p = IDENTIFIER_POINTER
11843 (c_parser_peek_2nd_token (parser)->value);
11844 if (strcmp ("data", p) == 0)
11845 n = 4;
11847 if (n == 4)
11848 c_parser_consume_token (parser);
11849 else
11851 location_t loc
11852 = c_parser_peek_2nd_token (parser)->location;
11853 error_at (loc, "expected %<data%>");
11854 if_modifier = ERROR_MARK;
11859 if (if_modifier != ERROR_MARK)
11861 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11863 c_parser_consume_token (parser);
11864 c_parser_consume_token (parser);
11866 else
11868 if (n > 2)
11870 location_t loc = c_parser_peek_2nd_token (parser)->location;
11871 error_at (loc, "expected %<:%>");
11873 if_modifier = ERROR_MARK;
11878 tree t = c_parser_condition (parser), c;
11879 parens.skip_until_found_close (parser);
11881 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11882 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11884 if (if_modifier != ERROR_MARK
11885 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11887 const char *p = NULL;
11888 switch (if_modifier)
11890 case OMP_PARALLEL: p = "parallel"; break;
11891 case OMP_TASK: p = "task"; break;
11892 case OMP_TASKLOOP: p = "taskloop"; break;
11893 case OMP_TARGET_DATA: p = "target data"; break;
11894 case OMP_TARGET: p = "target"; break;
11895 case OMP_TARGET_UPDATE: p = "target update"; break;
11896 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11897 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11898 default: gcc_unreachable ();
11900 error_at (location, "too many %<if%> clauses with %qs modifier",
11902 return list;
11904 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11906 if (!is_omp)
11907 error_at (location, "too many %<if%> clauses");
11908 else
11909 error_at (location, "too many %<if%> clauses without modifier");
11910 return list;
11912 else if (if_modifier == ERROR_MARK
11913 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11915 error_at (location, "if any %<if%> clause has modifier, then all "
11916 "%<if%> clauses have to use modifier");
11917 return list;
11921 c = build_omp_clause (location, OMP_CLAUSE_IF);
11922 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11923 OMP_CLAUSE_IF_EXPR (c) = t;
11924 OMP_CLAUSE_CHAIN (c) = list;
11925 return c;
11928 /* OpenMP 2.5:
11929 lastprivate ( variable-list ) */
11931 static tree
11932 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11934 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11937 /* OpenMP 3.1:
11938 mergeable */
11940 static tree
11941 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11943 tree c;
11945 /* FIXME: Should we allow duplicates? */
11946 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11948 c = build_omp_clause (c_parser_peek_token (parser)->location,
11949 OMP_CLAUSE_MERGEABLE);
11950 OMP_CLAUSE_CHAIN (c) = list;
11952 return c;
11955 /* OpenMP 2.5:
11956 nowait */
11958 static tree
11959 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11961 tree c;
11962 location_t loc = c_parser_peek_token (parser)->location;
11964 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11966 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11967 OMP_CLAUSE_CHAIN (c) = list;
11968 return c;
11971 /* OpenMP 2.5:
11972 num_threads ( expression ) */
11974 static tree
11975 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11977 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11978 matching_parens parens;
11979 if (parens.require_open (parser))
11981 location_t expr_loc = c_parser_peek_token (parser)->location;
11982 c_expr expr = c_parser_expression (parser);
11983 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11984 tree c, t = expr.value;
11985 t = c_fully_fold (t, false, NULL);
11987 parens.skip_until_found_close (parser);
11989 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11991 c_parser_error (parser, "expected integer expression");
11992 return list;
11995 /* Attempt to statically determine when the number isn't positive. */
11996 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11997 build_int_cst (TREE_TYPE (t), 0));
11998 protected_set_expr_location (c, expr_loc);
11999 if (c == boolean_true_node)
12001 warning_at (expr_loc, 0,
12002 "%<num_threads%> value must be positive");
12003 t = integer_one_node;
12006 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12008 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12009 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12010 OMP_CLAUSE_CHAIN (c) = list;
12011 list = c;
12014 return list;
12017 /* OpenMP 4.5:
12018 num_tasks ( expression ) */
12020 static tree
12021 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12023 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12024 matching_parens parens;
12025 if (parens.require_open (parser))
12027 location_t expr_loc = c_parser_peek_token (parser)->location;
12028 c_expr expr = c_parser_expression (parser);
12029 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12030 tree c, t = expr.value;
12031 t = c_fully_fold (t, false, NULL);
12033 parens.skip_until_found_close (parser);
12035 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12037 c_parser_error (parser, "expected integer expression");
12038 return list;
12041 /* Attempt to statically determine when the number isn't positive. */
12042 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12043 build_int_cst (TREE_TYPE (t), 0));
12044 if (CAN_HAVE_LOCATION_P (c))
12045 SET_EXPR_LOCATION (c, expr_loc);
12046 if (c == boolean_true_node)
12048 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12049 t = integer_one_node;
12052 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12054 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12055 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12056 OMP_CLAUSE_CHAIN (c) = list;
12057 list = c;
12060 return list;
12063 /* OpenMP 4.5:
12064 grainsize ( expression ) */
12066 static tree
12067 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12069 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12070 matching_parens parens;
12071 if (parens.require_open (parser))
12073 location_t expr_loc = c_parser_peek_token (parser)->location;
12074 c_expr expr = c_parser_expression (parser);
12075 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12076 tree c, t = expr.value;
12077 t = c_fully_fold (t, false, NULL);
12079 parens.skip_until_found_close (parser);
12081 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12083 c_parser_error (parser, "expected integer expression");
12084 return list;
12087 /* Attempt to statically determine when the number isn't positive. */
12088 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12089 build_int_cst (TREE_TYPE (t), 0));
12090 if (CAN_HAVE_LOCATION_P (c))
12091 SET_EXPR_LOCATION (c, expr_loc);
12092 if (c == boolean_true_node)
12094 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12095 t = integer_one_node;
12098 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12100 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12101 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12102 OMP_CLAUSE_CHAIN (c) = list;
12103 list = c;
12106 return list;
12109 /* OpenMP 4.5:
12110 priority ( expression ) */
12112 static tree
12113 c_parser_omp_clause_priority (c_parser *parser, tree list)
12115 location_t priority_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
12134 non-negative. */
12135 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12136 build_int_cst (TREE_TYPE (t), 0));
12137 if (CAN_HAVE_LOCATION_P (c))
12138 SET_EXPR_LOCATION (c, expr_loc);
12139 if (c == boolean_true_node)
12141 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12142 t = integer_one_node;
12145 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12147 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12148 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12149 OMP_CLAUSE_CHAIN (c) = list;
12150 list = c;
12153 return list;
12156 /* OpenMP 4.5:
12157 hint ( expression ) */
12159 static tree
12160 c_parser_omp_clause_hint (c_parser *parser, tree list)
12162 location_t hint_loc = c_parser_peek_token (parser)->location;
12163 matching_parens parens;
12164 if (parens.require_open (parser))
12166 location_t expr_loc = c_parser_peek_token (parser)->location;
12167 c_expr expr = c_parser_expression (parser);
12168 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12169 tree c, t = expr.value;
12170 t = c_fully_fold (t, false, NULL);
12172 parens.skip_until_found_close (parser);
12174 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12176 c_parser_error (parser, "expected integer expression");
12177 return list;
12180 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12182 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12183 OMP_CLAUSE_HINT_EXPR (c) = t;
12184 OMP_CLAUSE_CHAIN (c) = list;
12185 list = c;
12188 return list;
12191 /* OpenMP 4.5:
12192 defaultmap ( tofrom : scalar ) */
12194 static tree
12195 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12197 location_t loc = c_parser_peek_token (parser)->location;
12198 tree c;
12199 const char *p;
12201 matching_parens parens;
12202 if (!parens.require_open (parser))
12203 return list;
12204 if (!c_parser_next_token_is (parser, CPP_NAME))
12206 c_parser_error (parser, "expected %<tofrom%>");
12207 goto out_err;
12209 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12210 if (strcmp (p, "tofrom") != 0)
12212 c_parser_error (parser, "expected %<tofrom%>");
12213 goto out_err;
12215 c_parser_consume_token (parser);
12216 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12217 goto out_err;
12218 if (!c_parser_next_token_is (parser, CPP_NAME))
12220 c_parser_error (parser, "expected %<scalar%>");
12221 goto out_err;
12223 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12224 if (strcmp (p, "scalar") != 0)
12226 c_parser_error (parser, "expected %<scalar%>");
12227 goto out_err;
12229 c_parser_consume_token (parser);
12230 parens.skip_until_found_close (parser);
12231 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12232 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12233 OMP_CLAUSE_CHAIN (c) = list;
12234 return c;
12236 out_err:
12237 parens.skip_until_found_close (parser);
12238 return list;
12241 /* OpenACC 2.0:
12242 use_device ( variable-list )
12244 OpenMP 4.5:
12245 use_device_ptr ( variable-list ) */
12247 static tree
12248 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12250 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12251 list);
12254 /* OpenMP 4.5:
12255 is_device_ptr ( variable-list ) */
12257 static tree
12258 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12260 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12263 /* OpenACC:
12264 num_gangs ( expression )
12265 num_workers ( expression )
12266 vector_length ( expression ) */
12268 static tree
12269 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12270 tree list)
12272 location_t loc = c_parser_peek_token (parser)->location;
12274 matching_parens parens;
12275 if (!parens.require_open (parser))
12276 return list;
12278 location_t expr_loc = c_parser_peek_token (parser)->location;
12279 c_expr expr = c_parser_expression (parser);
12280 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12281 tree c, t = expr.value;
12282 t = c_fully_fold (t, false, NULL);
12284 parens.skip_until_found_close (parser);
12286 if (t == error_mark_node)
12287 return list;
12288 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12290 error_at (expr_loc, "%qs expression must be integral",
12291 omp_clause_code_name[code]);
12292 return list;
12295 /* Attempt to statically determine when the number isn't positive. */
12296 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12297 build_int_cst (TREE_TYPE (t), 0));
12298 protected_set_expr_location (c, expr_loc);
12299 if (c == boolean_true_node)
12301 warning_at (expr_loc, 0,
12302 "%qs value must be positive",
12303 omp_clause_code_name[code]);
12304 t = integer_one_node;
12307 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12309 c = build_omp_clause (loc, code);
12310 OMP_CLAUSE_OPERAND (c, 0) = t;
12311 OMP_CLAUSE_CHAIN (c) = list;
12312 return c;
12315 /* OpenACC:
12317 gang [( gang-arg-list )]
12318 worker [( [num:] int-expr )]
12319 vector [( [length:] int-expr )]
12321 where gang-arg is one of:
12323 [num:] int-expr
12324 static: size-expr
12326 and size-expr may be:
12329 int-expr
12332 static tree
12333 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12334 const char *str, tree list)
12336 const char *id = "num";
12337 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12338 location_t loc = c_parser_peek_token (parser)->location;
12340 if (kind == OMP_CLAUSE_VECTOR)
12341 id = "length";
12343 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12345 c_parser_consume_token (parser);
12349 c_token *next = c_parser_peek_token (parser);
12350 int idx = 0;
12352 /* Gang static argument. */
12353 if (kind == OMP_CLAUSE_GANG
12354 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12356 c_parser_consume_token (parser);
12358 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12359 goto cleanup_error;
12361 idx = 1;
12362 if (ops[idx] != NULL_TREE)
12364 c_parser_error (parser, "too many %<static%> arguments");
12365 goto cleanup_error;
12368 /* Check for the '*' argument. */
12369 if (c_parser_next_token_is (parser, CPP_MULT)
12370 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12371 || c_parser_peek_2nd_token (parser)->type
12372 == CPP_CLOSE_PAREN))
12374 c_parser_consume_token (parser);
12375 ops[idx] = integer_minus_one_node;
12377 if (c_parser_next_token_is (parser, CPP_COMMA))
12379 c_parser_consume_token (parser);
12380 continue;
12382 else
12383 break;
12386 /* Worker num: argument and vector length: arguments. */
12387 else if (c_parser_next_token_is (parser, CPP_NAME)
12388 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12389 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12391 c_parser_consume_token (parser); /* id */
12392 c_parser_consume_token (parser); /* ':' */
12395 /* Now collect the actual argument. */
12396 if (ops[idx] != NULL_TREE)
12398 c_parser_error (parser, "unexpected argument");
12399 goto cleanup_error;
12402 location_t expr_loc = c_parser_peek_token (parser)->location;
12403 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12404 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12405 tree expr = cexpr.value;
12406 if (expr == error_mark_node)
12407 goto cleanup_error;
12409 expr = c_fully_fold (expr, false, NULL);
12411 /* Attempt to statically determine when the number isn't a
12412 positive integer. */
12414 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12416 c_parser_error (parser, "expected integer expression");
12417 return list;
12420 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12421 build_int_cst (TREE_TYPE (expr), 0));
12422 if (c == boolean_true_node)
12424 warning_at (loc, 0,
12425 "%qs value must be positive", str);
12426 expr = integer_one_node;
12429 ops[idx] = expr;
12431 if (kind == OMP_CLAUSE_GANG
12432 && c_parser_next_token_is (parser, CPP_COMMA))
12434 c_parser_consume_token (parser);
12435 continue;
12437 break;
12439 while (1);
12441 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12442 goto cleanup_error;
12445 check_no_duplicate_clause (list, kind, str);
12447 c = build_omp_clause (loc, kind);
12449 if (ops[1])
12450 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12452 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12453 OMP_CLAUSE_CHAIN (c) = list;
12455 return c;
12457 cleanup_error:
12458 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12459 return list;
12462 /* OpenACC:
12463 auto
12464 independent
12465 nohost
12466 seq */
12468 static tree
12469 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12470 tree list)
12472 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12474 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12475 OMP_CLAUSE_CHAIN (c) = list;
12477 return c;
12480 /* OpenACC:
12481 async [( int-expr )] */
12483 static tree
12484 c_parser_oacc_clause_async (c_parser *parser, tree list)
12486 tree c, t;
12487 location_t loc = c_parser_peek_token (parser)->location;
12489 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12491 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12493 c_parser_consume_token (parser);
12495 t = c_parser_expression (parser).value;
12496 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12497 c_parser_error (parser, "expected integer expression");
12498 else if (t == error_mark_node
12499 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12500 return list;
12502 else
12503 t = c_fully_fold (t, false, NULL);
12505 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12507 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12508 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12509 OMP_CLAUSE_CHAIN (c) = list;
12510 list = c;
12512 return list;
12515 /* OpenACC 2.0:
12516 tile ( size-expr-list ) */
12518 static tree
12519 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12521 tree c, expr = error_mark_node;
12522 location_t loc;
12523 tree tile = NULL_TREE;
12525 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12526 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12528 loc = c_parser_peek_token (parser)->location;
12529 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12530 return list;
12534 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12535 return list;
12537 if (c_parser_next_token_is (parser, CPP_MULT)
12538 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12539 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12541 c_parser_consume_token (parser);
12542 expr = integer_zero_node;
12544 else
12546 location_t expr_loc = c_parser_peek_token (parser)->location;
12547 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12548 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12549 expr = cexpr.value;
12551 if (expr == error_mark_node)
12553 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12554 "expected %<)%>");
12555 return list;
12558 expr = c_fully_fold (expr, false, NULL);
12560 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12561 || !tree_fits_shwi_p (expr)
12562 || tree_to_shwi (expr) <= 0)
12564 error_at (expr_loc, "%<tile%> argument needs positive"
12565 " integral constant");
12566 expr = integer_zero_node;
12570 tile = tree_cons (NULL_TREE, expr, tile);
12572 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12574 /* Consume the trailing ')'. */
12575 c_parser_consume_token (parser);
12577 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12578 tile = nreverse (tile);
12579 OMP_CLAUSE_TILE_LIST (c) = tile;
12580 OMP_CLAUSE_CHAIN (c) = list;
12581 return c;
12584 /* OpenACC:
12585 wait ( int-expr-list ) */
12587 static tree
12588 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12590 location_t clause_loc = c_parser_peek_token (parser)->location;
12592 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12593 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12595 return list;
12598 /* OpenMP 2.5:
12599 ordered
12601 OpenMP 4.5:
12602 ordered ( constant-expression ) */
12604 static tree
12605 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12607 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12609 tree c, num = NULL_TREE;
12610 HOST_WIDE_INT n;
12611 location_t loc = c_parser_peek_token (parser)->location;
12612 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12614 matching_parens parens;
12615 parens.consume_open (parser);
12616 num = c_parser_expr_no_commas (parser, NULL).value;
12617 parens.skip_until_found_close (parser);
12619 if (num == error_mark_node)
12620 return list;
12621 if (num)
12623 mark_exp_read (num);
12624 num = c_fully_fold (num, false, NULL);
12625 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12626 || !tree_fits_shwi_p (num)
12627 || (n = tree_to_shwi (num)) <= 0
12628 || (int) n != n)
12630 error_at (loc, "ordered argument needs positive "
12631 "constant integer expression");
12632 return list;
12635 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12636 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12637 OMP_CLAUSE_CHAIN (c) = list;
12638 return c;
12641 /* OpenMP 2.5:
12642 private ( variable-list ) */
12644 static tree
12645 c_parser_omp_clause_private (c_parser *parser, tree list)
12647 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12650 /* OpenMP 2.5:
12651 reduction ( reduction-operator : variable-list )
12653 reduction-operator:
12654 One of: + * - & ^ | && ||
12656 OpenMP 3.1:
12658 reduction-operator:
12659 One of: + * - & ^ | && || max min
12661 OpenMP 4.0:
12663 reduction-operator:
12664 One of: + * - & ^ | && ||
12665 identifier */
12667 static tree
12668 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12670 location_t clause_loc = c_parser_peek_token (parser)->location;
12671 matching_parens parens;
12672 if (parens.require_open (parser))
12674 enum tree_code code = ERROR_MARK;
12675 tree reduc_id = NULL_TREE;
12677 switch (c_parser_peek_token (parser)->type)
12679 case CPP_PLUS:
12680 code = PLUS_EXPR;
12681 break;
12682 case CPP_MULT:
12683 code = MULT_EXPR;
12684 break;
12685 case CPP_MINUS:
12686 code = MINUS_EXPR;
12687 break;
12688 case CPP_AND:
12689 code = BIT_AND_EXPR;
12690 break;
12691 case CPP_XOR:
12692 code = BIT_XOR_EXPR;
12693 break;
12694 case CPP_OR:
12695 code = BIT_IOR_EXPR;
12696 break;
12697 case CPP_AND_AND:
12698 code = TRUTH_ANDIF_EXPR;
12699 break;
12700 case CPP_OR_OR:
12701 code = TRUTH_ORIF_EXPR;
12702 break;
12703 case CPP_NAME:
12705 const char *p
12706 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12707 if (strcmp (p, "min") == 0)
12709 code = MIN_EXPR;
12710 break;
12712 if (strcmp (p, "max") == 0)
12714 code = MAX_EXPR;
12715 break;
12717 reduc_id = c_parser_peek_token (parser)->value;
12718 break;
12720 default:
12721 c_parser_error (parser,
12722 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12723 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12724 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12725 return list;
12727 c_parser_consume_token (parser);
12728 reduc_id = c_omp_reduction_id (code, reduc_id);
12729 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12731 tree nl, c;
12733 nl = c_parser_omp_variable_list (parser, clause_loc,
12734 OMP_CLAUSE_REDUCTION, list);
12735 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12737 tree d = OMP_CLAUSE_DECL (c), type;
12738 if (TREE_CODE (d) != TREE_LIST)
12739 type = TREE_TYPE (d);
12740 else
12742 int cnt = 0;
12743 tree t;
12744 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12745 cnt++;
12746 type = TREE_TYPE (t);
12747 while (cnt > 0)
12749 if (TREE_CODE (type) != POINTER_TYPE
12750 && TREE_CODE (type) != ARRAY_TYPE)
12751 break;
12752 type = TREE_TYPE (type);
12753 cnt--;
12756 while (TREE_CODE (type) == ARRAY_TYPE)
12757 type = TREE_TYPE (type);
12758 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12759 if (code == ERROR_MARK
12760 || !(INTEGRAL_TYPE_P (type)
12761 || TREE_CODE (type) == REAL_TYPE
12762 || TREE_CODE (type) == COMPLEX_TYPE))
12763 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12764 = c_omp_reduction_lookup (reduc_id,
12765 TYPE_MAIN_VARIANT (type));
12768 list = nl;
12770 parens.skip_until_found_close (parser);
12772 return list;
12775 /* OpenMP 2.5:
12776 schedule ( schedule-kind )
12777 schedule ( schedule-kind , expression )
12779 schedule-kind:
12780 static | dynamic | guided | runtime | auto
12782 OpenMP 4.5:
12783 schedule ( schedule-modifier : schedule-kind )
12784 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12786 schedule-modifier:
12787 simd
12788 monotonic
12789 nonmonotonic */
12791 static tree
12792 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12794 tree c, t;
12795 location_t loc = c_parser_peek_token (parser)->location;
12796 int modifiers = 0, nmodifiers = 0;
12798 matching_parens parens;
12799 if (!parens.require_open (parser))
12800 return list;
12802 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12804 while (c_parser_next_token_is (parser, CPP_NAME))
12806 tree kind = c_parser_peek_token (parser)->value;
12807 const char *p = IDENTIFIER_POINTER (kind);
12808 if (strcmp ("simd", p) == 0)
12809 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12810 else if (strcmp ("monotonic", p) == 0)
12811 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12812 else if (strcmp ("nonmonotonic", p) == 0)
12813 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12814 else
12815 break;
12816 c_parser_consume_token (parser);
12817 if (nmodifiers++ == 0
12818 && c_parser_next_token_is (parser, CPP_COMMA))
12819 c_parser_consume_token (parser);
12820 else
12822 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12823 break;
12827 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12828 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12829 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12830 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12832 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12833 "specified");
12834 modifiers = 0;
12837 if (c_parser_next_token_is (parser, CPP_NAME))
12839 tree kind = c_parser_peek_token (parser)->value;
12840 const char *p = IDENTIFIER_POINTER (kind);
12842 switch (p[0])
12844 case 'd':
12845 if (strcmp ("dynamic", p) != 0)
12846 goto invalid_kind;
12847 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12848 break;
12850 case 'g':
12851 if (strcmp ("guided", p) != 0)
12852 goto invalid_kind;
12853 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12854 break;
12856 case 'r':
12857 if (strcmp ("runtime", p) != 0)
12858 goto invalid_kind;
12859 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12860 break;
12862 default:
12863 goto invalid_kind;
12866 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12867 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12868 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12869 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12870 else
12871 goto invalid_kind;
12873 c_parser_consume_token (parser);
12874 if (c_parser_next_token_is (parser, CPP_COMMA))
12876 location_t here;
12877 c_parser_consume_token (parser);
12879 here = c_parser_peek_token (parser)->location;
12880 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12881 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12882 t = expr.value;
12883 t = c_fully_fold (t, false, NULL);
12885 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12886 error_at (here, "schedule %<runtime%> does not take "
12887 "a %<chunk_size%> parameter");
12888 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12889 error_at (here,
12890 "schedule %<auto%> does not take "
12891 "a %<chunk_size%> parameter");
12892 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12894 /* Attempt to statically determine when the number isn't
12895 positive. */
12896 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12897 build_int_cst (TREE_TYPE (t), 0));
12898 protected_set_expr_location (s, loc);
12899 if (s == boolean_true_node)
12901 warning_at (loc, 0,
12902 "chunk size value must be positive");
12903 t = integer_one_node;
12905 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12907 else
12908 c_parser_error (parser, "expected integer expression");
12910 parens.skip_until_found_close (parser);
12912 else
12913 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12914 "expected %<,%> or %<)%>");
12916 OMP_CLAUSE_SCHEDULE_KIND (c)
12917 = (enum omp_clause_schedule_kind)
12918 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12920 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12921 OMP_CLAUSE_CHAIN (c) = list;
12922 return c;
12924 invalid_kind:
12925 c_parser_error (parser, "invalid schedule kind");
12926 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12927 return list;
12930 /* OpenMP 2.5:
12931 shared ( variable-list ) */
12933 static tree
12934 c_parser_omp_clause_shared (c_parser *parser, tree list)
12936 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12939 /* OpenMP 3.0:
12940 untied */
12942 static tree
12943 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12945 tree c;
12947 /* FIXME: Should we allow duplicates? */
12948 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12950 c = build_omp_clause (c_parser_peek_token (parser)->location,
12951 OMP_CLAUSE_UNTIED);
12952 OMP_CLAUSE_CHAIN (c) = list;
12954 return c;
12957 /* OpenMP 4.0:
12958 inbranch
12959 notinbranch */
12961 static tree
12962 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12963 enum omp_clause_code code, tree list)
12965 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12967 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12968 OMP_CLAUSE_CHAIN (c) = list;
12970 return c;
12973 /* OpenMP 4.0:
12974 parallel
12976 sections
12977 taskgroup */
12979 static tree
12980 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12981 enum omp_clause_code code, tree list)
12983 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12984 OMP_CLAUSE_CHAIN (c) = list;
12986 return c;
12989 /* OpenMP 4.5:
12990 nogroup */
12992 static tree
12993 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12995 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12996 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12997 OMP_CLAUSE_NOGROUP);
12998 OMP_CLAUSE_CHAIN (c) = list;
12999 return c;
13002 /* OpenMP 4.5:
13003 simd
13004 threads */
13006 static tree
13007 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13008 enum omp_clause_code code, tree list)
13010 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13011 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13012 OMP_CLAUSE_CHAIN (c) = list;
13013 return c;
13016 /* OpenMP 4.0:
13017 num_teams ( expression ) */
13019 static tree
13020 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13022 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13023 matching_parens parens;
13024 if (parens.require_open (parser))
13026 location_t expr_loc = c_parser_peek_token (parser)->location;
13027 c_expr expr = c_parser_expression (parser);
13028 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13029 tree c, t = expr.value;
13030 t = c_fully_fold (t, false, NULL);
13032 parens.skip_until_found_close (parser);
13034 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13036 c_parser_error (parser, "expected integer expression");
13037 return list;
13040 /* Attempt to statically determine when the number isn't positive. */
13041 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13042 build_int_cst (TREE_TYPE (t), 0));
13043 protected_set_expr_location (c, expr_loc);
13044 if (c == boolean_true_node)
13046 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13047 t = integer_one_node;
13050 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13052 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13053 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13054 OMP_CLAUSE_CHAIN (c) = list;
13055 list = c;
13058 return list;
13061 /* OpenMP 4.0:
13062 thread_limit ( expression ) */
13064 static tree
13065 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13067 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13068 matching_parens parens;
13069 if (parens.require_open (parser))
13071 location_t expr_loc = c_parser_peek_token (parser)->location;
13072 c_expr expr = c_parser_expression (parser);
13073 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13074 tree c, t = expr.value;
13075 t = c_fully_fold (t, false, NULL);
13077 parens.skip_until_found_close (parser);
13079 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13081 c_parser_error (parser, "expected integer expression");
13082 return list;
13085 /* Attempt to statically determine when the number isn't positive. */
13086 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13087 build_int_cst (TREE_TYPE (t), 0));
13088 protected_set_expr_location (c, expr_loc);
13089 if (c == boolean_true_node)
13091 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13092 t = integer_one_node;
13095 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13096 "thread_limit");
13098 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13099 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13100 OMP_CLAUSE_CHAIN (c) = list;
13101 list = c;
13104 return list;
13107 /* OpenMP 4.0:
13108 aligned ( variable-list )
13109 aligned ( variable-list : constant-expression ) */
13111 static tree
13112 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13114 location_t clause_loc = c_parser_peek_token (parser)->location;
13115 tree nl, c;
13117 matching_parens parens;
13118 if (!parens.require_open (parser))
13119 return list;
13121 nl = c_parser_omp_variable_list (parser, clause_loc,
13122 OMP_CLAUSE_ALIGNED, list);
13124 if (c_parser_next_token_is (parser, CPP_COLON))
13126 c_parser_consume_token (parser);
13127 location_t expr_loc = c_parser_peek_token (parser)->location;
13128 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13129 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13130 tree alignment = expr.value;
13131 alignment = c_fully_fold (alignment, false, NULL);
13132 if (TREE_CODE (alignment) != INTEGER_CST
13133 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13134 || tree_int_cst_sgn (alignment) != 1)
13136 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13137 "be positive constant integer expression");
13138 alignment = NULL_TREE;
13141 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13142 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13145 parens.skip_until_found_close (parser);
13146 return nl;
13149 /* OpenMP 4.0:
13150 linear ( variable-list )
13151 linear ( variable-list : expression )
13153 OpenMP 4.5:
13154 linear ( modifier ( variable-list ) )
13155 linear ( modifier ( variable-list ) : expression ) */
13157 static tree
13158 c_parser_omp_clause_linear (c_parser *parser, tree list)
13160 location_t clause_loc = c_parser_peek_token (parser)->location;
13161 tree nl, c, step;
13162 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13164 matching_parens parens;
13165 if (!parens.require_open (parser))
13166 return list;
13168 if (c_parser_next_token_is (parser, CPP_NAME))
13170 c_token *tok = c_parser_peek_token (parser);
13171 const char *p = IDENTIFIER_POINTER (tok->value);
13172 if (strcmp ("val", p) == 0)
13173 kind = OMP_CLAUSE_LINEAR_VAL;
13174 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13175 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13176 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13178 c_parser_consume_token (parser);
13179 c_parser_consume_token (parser);
13183 nl = c_parser_omp_variable_list (parser, clause_loc,
13184 OMP_CLAUSE_LINEAR, list);
13186 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13187 parens.skip_until_found_close (parser);
13189 if (c_parser_next_token_is (parser, CPP_COLON))
13191 c_parser_consume_token (parser);
13192 location_t expr_loc = c_parser_peek_token (parser)->location;
13193 c_expr expr = c_parser_expression (parser);
13194 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13195 step = expr.value;
13196 step = c_fully_fold (step, false, NULL);
13197 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13199 error_at (clause_loc, "%<linear%> clause step expression must "
13200 "be integral");
13201 step = integer_one_node;
13205 else
13206 step = integer_one_node;
13208 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13210 OMP_CLAUSE_LINEAR_STEP (c) = step;
13211 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13214 parens.skip_until_found_close (parser);
13215 return nl;
13218 /* OpenMP 4.0:
13219 safelen ( constant-expression ) */
13221 static tree
13222 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13224 location_t clause_loc = c_parser_peek_token (parser)->location;
13225 tree c, t;
13227 matching_parens parens;
13228 if (!parens.require_open (parser))
13229 return list;
13231 location_t expr_loc = c_parser_peek_token (parser)->location;
13232 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13233 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13234 t = expr.value;
13235 t = c_fully_fold (t, false, NULL);
13236 if (TREE_CODE (t) != INTEGER_CST
13237 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13238 || tree_int_cst_sgn (t) != 1)
13240 error_at (clause_loc, "%<safelen%> clause expression must "
13241 "be positive constant integer expression");
13242 t = NULL_TREE;
13245 parens.skip_until_found_close (parser);
13246 if (t == NULL_TREE || t == error_mark_node)
13247 return list;
13249 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13251 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13252 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13253 OMP_CLAUSE_CHAIN (c) = list;
13254 return c;
13257 /* OpenMP 4.0:
13258 simdlen ( constant-expression ) */
13260 static tree
13261 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13263 location_t clause_loc = c_parser_peek_token (parser)->location;
13264 tree c, t;
13266 matching_parens parens;
13267 if (!parens.require_open (parser))
13268 return list;
13270 location_t expr_loc = c_parser_peek_token (parser)->location;
13271 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13272 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13273 t = expr.value;
13274 t = c_fully_fold (t, false, NULL);
13275 if (TREE_CODE (t) != INTEGER_CST
13276 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13277 || tree_int_cst_sgn (t) != 1)
13279 error_at (clause_loc, "%<simdlen%> clause expression must "
13280 "be positive constant integer expression");
13281 t = NULL_TREE;
13284 parens.skip_until_found_close (parser);
13285 if (t == NULL_TREE || t == error_mark_node)
13286 return list;
13288 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13290 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13291 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13292 OMP_CLAUSE_CHAIN (c) = list;
13293 return c;
13296 /* OpenMP 4.5:
13297 vec:
13298 identifier [+/- integer]
13299 vec , identifier [+/- integer]
13302 static tree
13303 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13304 tree list)
13306 tree vec = NULL;
13307 if (c_parser_next_token_is_not (parser, CPP_NAME)
13308 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13310 c_parser_error (parser, "expected identifier");
13311 return list;
13314 while (c_parser_next_token_is (parser, CPP_NAME)
13315 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13317 tree t = lookup_name (c_parser_peek_token (parser)->value);
13318 tree addend = NULL;
13320 if (t == NULL_TREE)
13322 undeclared_variable (c_parser_peek_token (parser)->location,
13323 c_parser_peek_token (parser)->value);
13324 t = error_mark_node;
13327 c_parser_consume_token (parser);
13329 bool neg = false;
13330 if (c_parser_next_token_is (parser, CPP_MINUS))
13331 neg = true;
13332 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13334 addend = integer_zero_node;
13335 neg = false;
13336 goto add_to_vector;
13338 c_parser_consume_token (parser);
13340 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13342 c_parser_error (parser, "expected integer");
13343 return list;
13346 addend = c_parser_peek_token (parser)->value;
13347 if (TREE_CODE (addend) != INTEGER_CST)
13349 c_parser_error (parser, "expected integer");
13350 return list;
13352 c_parser_consume_token (parser);
13354 add_to_vector:
13355 if (t != error_mark_node)
13357 vec = tree_cons (addend, t, vec);
13358 if (neg)
13359 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13362 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13363 break;
13365 c_parser_consume_token (parser);
13368 if (vec == NULL_TREE)
13369 return list;
13371 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13372 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13373 OMP_CLAUSE_DECL (u) = nreverse (vec);
13374 OMP_CLAUSE_CHAIN (u) = list;
13375 return u;
13378 /* OpenMP 4.0:
13379 depend ( depend-kind: variable-list )
13381 depend-kind:
13382 in | out | inout
13384 OpenMP 4.5:
13385 depend ( source )
13387 depend ( sink : vec ) */
13389 static tree
13390 c_parser_omp_clause_depend (c_parser *parser, tree list)
13392 location_t clause_loc = c_parser_peek_token (parser)->location;
13393 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13394 tree nl, c;
13396 matching_parens parens;
13397 if (!parens.require_open (parser))
13398 return list;
13400 if (c_parser_next_token_is (parser, CPP_NAME))
13402 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13403 if (strcmp ("in", p) == 0)
13404 kind = OMP_CLAUSE_DEPEND_IN;
13405 else if (strcmp ("inout", p) == 0)
13406 kind = OMP_CLAUSE_DEPEND_INOUT;
13407 else if (strcmp ("out", p) == 0)
13408 kind = OMP_CLAUSE_DEPEND_OUT;
13409 else if (strcmp ("source", p) == 0)
13410 kind = OMP_CLAUSE_DEPEND_SOURCE;
13411 else if (strcmp ("sink", p) == 0)
13412 kind = OMP_CLAUSE_DEPEND_SINK;
13413 else
13414 goto invalid_kind;
13416 else
13417 goto invalid_kind;
13419 c_parser_consume_token (parser);
13421 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13423 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13424 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13425 OMP_CLAUSE_DECL (c) = NULL_TREE;
13426 OMP_CLAUSE_CHAIN (c) = list;
13427 parens.skip_until_found_close (parser);
13428 return c;
13431 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13432 goto resync_fail;
13434 if (kind == OMP_CLAUSE_DEPEND_SINK)
13435 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13436 else
13438 nl = c_parser_omp_variable_list (parser, clause_loc,
13439 OMP_CLAUSE_DEPEND, list);
13441 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13442 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13445 parens.skip_until_found_close (parser);
13446 return nl;
13448 invalid_kind:
13449 c_parser_error (parser, "invalid depend kind");
13450 resync_fail:
13451 parens.skip_until_found_close (parser);
13452 return list;
13455 /* OpenMP 4.0:
13456 map ( map-kind: variable-list )
13457 map ( variable-list )
13459 map-kind:
13460 alloc | to | from | tofrom
13462 OpenMP 4.5:
13463 map-kind:
13464 alloc | to | from | tofrom | release | delete
13466 map ( always [,] map-kind: variable-list ) */
13468 static tree
13469 c_parser_omp_clause_map (c_parser *parser, tree list)
13471 location_t clause_loc = c_parser_peek_token (parser)->location;
13472 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13473 int always = 0;
13474 enum c_id_kind always_id_kind = C_ID_NONE;
13475 location_t always_loc = UNKNOWN_LOCATION;
13476 tree always_id = NULL_TREE;
13477 tree nl, c;
13479 matching_parens parens;
13480 if (!parens.require_open (parser))
13481 return list;
13483 if (c_parser_next_token_is (parser, CPP_NAME))
13485 c_token *tok = c_parser_peek_token (parser);
13486 const char *p = IDENTIFIER_POINTER (tok->value);
13487 always_id_kind = tok->id_kind;
13488 always_loc = tok->location;
13489 always_id = tok->value;
13490 if (strcmp ("always", p) == 0)
13492 c_token *sectok = c_parser_peek_2nd_token (parser);
13493 if (sectok->type == CPP_COMMA)
13495 c_parser_consume_token (parser);
13496 c_parser_consume_token (parser);
13497 always = 2;
13499 else if (sectok->type == CPP_NAME)
13501 p = IDENTIFIER_POINTER (sectok->value);
13502 if (strcmp ("alloc", p) == 0
13503 || strcmp ("to", p) == 0
13504 || strcmp ("from", p) == 0
13505 || strcmp ("tofrom", p) == 0
13506 || strcmp ("release", p) == 0
13507 || strcmp ("delete", p) == 0)
13509 c_parser_consume_token (parser);
13510 always = 1;
13516 if (c_parser_next_token_is (parser, CPP_NAME)
13517 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13519 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13520 if (strcmp ("alloc", p) == 0)
13521 kind = GOMP_MAP_ALLOC;
13522 else if (strcmp ("to", p) == 0)
13523 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13524 else if (strcmp ("from", p) == 0)
13525 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13526 else if (strcmp ("tofrom", p) == 0)
13527 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13528 else if (strcmp ("release", p) == 0)
13529 kind = GOMP_MAP_RELEASE;
13530 else if (strcmp ("delete", p) == 0)
13531 kind = GOMP_MAP_DELETE;
13532 else
13534 c_parser_error (parser, "invalid map kind");
13535 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13536 "expected %<)%>");
13537 return list;
13539 c_parser_consume_token (parser);
13540 c_parser_consume_token (parser);
13542 else if (always)
13544 if (always_id_kind != C_ID_ID)
13546 c_parser_error (parser, "expected identifier");
13547 parens.skip_until_found_close (parser);
13548 return list;
13551 tree t = lookup_name (always_id);
13552 if (t == NULL_TREE)
13554 undeclared_variable (always_loc, always_id);
13555 t = error_mark_node;
13557 if (t != error_mark_node)
13559 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13560 OMP_CLAUSE_DECL (u) = t;
13561 OMP_CLAUSE_CHAIN (u) = list;
13562 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13563 list = u;
13565 if (always == 1)
13567 parens.skip_until_found_close (parser);
13568 return list;
13572 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13574 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13575 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13577 parens.skip_until_found_close (parser);
13578 return nl;
13581 /* OpenMP 4.0:
13582 device ( expression ) */
13584 static tree
13585 c_parser_omp_clause_device (c_parser *parser, tree list)
13587 location_t clause_loc = c_parser_peek_token (parser)->location;
13588 matching_parens parens;
13589 if (parens.require_open (parser))
13591 location_t expr_loc = c_parser_peek_token (parser)->location;
13592 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13593 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13594 tree c, t = expr.value;
13595 t = c_fully_fold (t, false, NULL);
13597 parens.skip_until_found_close (parser);
13599 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13601 c_parser_error (parser, "expected integer expression");
13602 return list;
13605 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13607 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13608 OMP_CLAUSE_DEVICE_ID (c) = t;
13609 OMP_CLAUSE_CHAIN (c) = list;
13610 list = c;
13613 return list;
13616 /* OpenMP 4.0:
13617 dist_schedule ( static )
13618 dist_schedule ( static , expression ) */
13620 static tree
13621 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13623 tree c, t = NULL_TREE;
13624 location_t loc = c_parser_peek_token (parser)->location;
13626 matching_parens parens;
13627 if (!parens.require_open (parser))
13628 return list;
13630 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13632 c_parser_error (parser, "invalid dist_schedule kind");
13633 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13634 "expected %<)%>");
13635 return list;
13638 c_parser_consume_token (parser);
13639 if (c_parser_next_token_is (parser, CPP_COMMA))
13641 c_parser_consume_token (parser);
13643 location_t expr_loc = c_parser_peek_token (parser)->location;
13644 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13645 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13646 t = expr.value;
13647 t = c_fully_fold (t, false, NULL);
13648 parens.skip_until_found_close (parser);
13650 else
13651 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13652 "expected %<,%> or %<)%>");
13654 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13655 if (t == error_mark_node)
13656 return list;
13658 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13659 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13660 OMP_CLAUSE_CHAIN (c) = list;
13661 return c;
13664 /* OpenMP 4.0:
13665 proc_bind ( proc-bind-kind )
13667 proc-bind-kind:
13668 master | close | spread */
13670 static tree
13671 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13673 location_t clause_loc = c_parser_peek_token (parser)->location;
13674 enum omp_clause_proc_bind_kind kind;
13675 tree c;
13677 matching_parens parens;
13678 if (!parens.require_open (parser))
13679 return list;
13681 if (c_parser_next_token_is (parser, CPP_NAME))
13683 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13684 if (strcmp ("master", p) == 0)
13685 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13686 else if (strcmp ("close", p) == 0)
13687 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13688 else if (strcmp ("spread", p) == 0)
13689 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13690 else
13691 goto invalid_kind;
13693 else
13694 goto invalid_kind;
13696 c_parser_consume_token (parser);
13697 parens.skip_until_found_close (parser);
13698 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13699 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13700 OMP_CLAUSE_CHAIN (c) = list;
13701 return c;
13703 invalid_kind:
13704 c_parser_error (parser, "invalid proc_bind kind");
13705 parens.skip_until_found_close (parser);
13706 return list;
13709 /* OpenMP 4.0:
13710 to ( variable-list ) */
13712 static tree
13713 c_parser_omp_clause_to (c_parser *parser, tree list)
13715 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13718 /* OpenMP 4.0:
13719 from ( variable-list ) */
13721 static tree
13722 c_parser_omp_clause_from (c_parser *parser, tree list)
13724 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13727 /* OpenMP 4.0:
13728 uniform ( variable-list ) */
13730 static tree
13731 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13733 /* The clauses location. */
13734 location_t loc = c_parser_peek_token (parser)->location;
13736 matching_parens parens;
13737 if (parens.require_open (parser))
13739 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13740 list);
13741 parens.skip_until_found_close (parser);
13743 return list;
13746 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13747 is a bitmask in MASK. Return the list of clauses found. */
13749 static tree
13750 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13751 const char *where, bool finish_p = true)
13753 tree clauses = NULL;
13754 bool first = true;
13756 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13758 location_t here;
13759 pragma_omp_clause c_kind;
13760 const char *c_name;
13761 tree prev = clauses;
13763 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13764 c_parser_consume_token (parser);
13766 here = c_parser_peek_token (parser)->location;
13767 c_kind = c_parser_omp_clause_name (parser);
13769 switch (c_kind)
13771 case PRAGMA_OACC_CLAUSE_ASYNC:
13772 clauses = c_parser_oacc_clause_async (parser, clauses);
13773 c_name = "async";
13774 break;
13775 case PRAGMA_OACC_CLAUSE_AUTO:
13776 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13777 clauses);
13778 c_name = "auto";
13779 break;
13780 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13781 clauses = c_parser_omp_clause_collapse (parser, clauses);
13782 c_name = "collapse";
13783 break;
13784 case PRAGMA_OACC_CLAUSE_COPY:
13785 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13786 c_name = "copy";
13787 break;
13788 case PRAGMA_OACC_CLAUSE_COPYIN:
13789 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13790 c_name = "copyin";
13791 break;
13792 case PRAGMA_OACC_CLAUSE_COPYOUT:
13793 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13794 c_name = "copyout";
13795 break;
13796 case PRAGMA_OACC_CLAUSE_CREATE:
13797 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13798 c_name = "create";
13799 break;
13800 case PRAGMA_OACC_CLAUSE_DELETE:
13801 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13802 c_name = "delete";
13803 break;
13804 case PRAGMA_OMP_CLAUSE_DEFAULT:
13805 clauses = c_parser_omp_clause_default (parser, clauses, true);
13806 c_name = "default";
13807 break;
13808 case PRAGMA_OACC_CLAUSE_DEVICE:
13809 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13810 c_name = "device";
13811 break;
13812 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13813 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13814 c_name = "deviceptr";
13815 break;
13816 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13817 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13818 c_name = "device_resident";
13819 break;
13820 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13821 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13822 c_name = "firstprivate";
13823 break;
13824 case PRAGMA_OACC_CLAUSE_GANG:
13825 c_name = "gang";
13826 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13827 c_name, clauses);
13828 break;
13829 case PRAGMA_OACC_CLAUSE_HOST:
13830 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13831 c_name = "host";
13832 break;
13833 case PRAGMA_OACC_CLAUSE_IF:
13834 clauses = c_parser_omp_clause_if (parser, clauses, false);
13835 c_name = "if";
13836 break;
13837 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13838 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13839 clauses);
13840 c_name = "independent";
13841 break;
13842 case PRAGMA_OACC_CLAUSE_LINK:
13843 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13844 c_name = "link";
13845 break;
13846 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13847 clauses = c_parser_oacc_single_int_clause (parser,
13848 OMP_CLAUSE_NUM_GANGS,
13849 clauses);
13850 c_name = "num_gangs";
13851 break;
13852 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13853 clauses = c_parser_oacc_single_int_clause (parser,
13854 OMP_CLAUSE_NUM_WORKERS,
13855 clauses);
13856 c_name = "num_workers";
13857 break;
13858 case PRAGMA_OACC_CLAUSE_PRESENT:
13859 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13860 c_name = "present";
13861 break;
13862 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13863 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13864 c_name = "present_or_copy";
13865 break;
13866 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13867 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13868 c_name = "present_or_copyin";
13869 break;
13870 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13871 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13872 c_name = "present_or_copyout";
13873 break;
13874 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13875 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13876 c_name = "present_or_create";
13877 break;
13878 case PRAGMA_OACC_CLAUSE_PRIVATE:
13879 clauses = c_parser_omp_clause_private (parser, clauses);
13880 c_name = "private";
13881 break;
13882 case PRAGMA_OACC_CLAUSE_REDUCTION:
13883 clauses = c_parser_omp_clause_reduction (parser, clauses);
13884 c_name = "reduction";
13885 break;
13886 case PRAGMA_OACC_CLAUSE_SELF:
13887 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13888 c_name = "self";
13889 break;
13890 case PRAGMA_OACC_CLAUSE_SEQ:
13891 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13892 clauses);
13893 c_name = "seq";
13894 break;
13895 case PRAGMA_OACC_CLAUSE_TILE:
13896 clauses = c_parser_oacc_clause_tile (parser, clauses);
13897 c_name = "tile";
13898 break;
13899 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13900 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13901 c_name = "use_device";
13902 break;
13903 case PRAGMA_OACC_CLAUSE_VECTOR:
13904 c_name = "vector";
13905 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13906 c_name, clauses);
13907 break;
13908 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13909 clauses = c_parser_oacc_single_int_clause (parser,
13910 OMP_CLAUSE_VECTOR_LENGTH,
13911 clauses);
13912 c_name = "vector_length";
13913 break;
13914 case PRAGMA_OACC_CLAUSE_WAIT:
13915 clauses = c_parser_oacc_clause_wait (parser, clauses);
13916 c_name = "wait";
13917 break;
13918 case PRAGMA_OACC_CLAUSE_WORKER:
13919 c_name = "worker";
13920 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13921 c_name, clauses);
13922 break;
13923 default:
13924 c_parser_error (parser, "expected %<#pragma acc%> clause");
13925 goto saw_error;
13928 first = false;
13930 if (((mask >> c_kind) & 1) == 0)
13932 /* Remove the invalid clause(s) from the list to avoid
13933 confusing the rest of the compiler. */
13934 clauses = prev;
13935 error_at (here, "%qs is not valid for %qs", c_name, where);
13939 saw_error:
13940 c_parser_skip_to_pragma_eol (parser);
13942 if (finish_p)
13943 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13945 return clauses;
13948 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13949 is a bitmask in MASK. Return the list of clauses found. */
13951 static tree
13952 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13953 const char *where, bool finish_p = true)
13955 tree clauses = NULL;
13956 bool first = true;
13958 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13960 location_t here;
13961 pragma_omp_clause c_kind;
13962 const char *c_name;
13963 tree prev = clauses;
13965 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13966 c_parser_consume_token (parser);
13968 here = c_parser_peek_token (parser)->location;
13969 c_kind = c_parser_omp_clause_name (parser);
13971 switch (c_kind)
13973 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13974 clauses = c_parser_omp_clause_collapse (parser, clauses);
13975 c_name = "collapse";
13976 break;
13977 case PRAGMA_OMP_CLAUSE_COPYIN:
13978 clauses = c_parser_omp_clause_copyin (parser, clauses);
13979 c_name = "copyin";
13980 break;
13981 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13982 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13983 c_name = "copyprivate";
13984 break;
13985 case PRAGMA_OMP_CLAUSE_DEFAULT:
13986 clauses = c_parser_omp_clause_default (parser, clauses, false);
13987 c_name = "default";
13988 break;
13989 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13990 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13991 c_name = "firstprivate";
13992 break;
13993 case PRAGMA_OMP_CLAUSE_FINAL:
13994 clauses = c_parser_omp_clause_final (parser, clauses);
13995 c_name = "final";
13996 break;
13997 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13998 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13999 c_name = "grainsize";
14000 break;
14001 case PRAGMA_OMP_CLAUSE_HINT:
14002 clauses = c_parser_omp_clause_hint (parser, clauses);
14003 c_name = "hint";
14004 break;
14005 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14006 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14007 c_name = "defaultmap";
14008 break;
14009 case PRAGMA_OMP_CLAUSE_IF:
14010 clauses = c_parser_omp_clause_if (parser, clauses, true);
14011 c_name = "if";
14012 break;
14013 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14014 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14015 c_name = "lastprivate";
14016 break;
14017 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14018 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14019 c_name = "mergeable";
14020 break;
14021 case PRAGMA_OMP_CLAUSE_NOWAIT:
14022 clauses = c_parser_omp_clause_nowait (parser, clauses);
14023 c_name = "nowait";
14024 break;
14025 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14026 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14027 c_name = "num_tasks";
14028 break;
14029 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14030 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14031 c_name = "num_threads";
14032 break;
14033 case PRAGMA_OMP_CLAUSE_ORDERED:
14034 clauses = c_parser_omp_clause_ordered (parser, clauses);
14035 c_name = "ordered";
14036 break;
14037 case PRAGMA_OMP_CLAUSE_PRIORITY:
14038 clauses = c_parser_omp_clause_priority (parser, clauses);
14039 c_name = "priority";
14040 break;
14041 case PRAGMA_OMP_CLAUSE_PRIVATE:
14042 clauses = c_parser_omp_clause_private (parser, clauses);
14043 c_name = "private";
14044 break;
14045 case PRAGMA_OMP_CLAUSE_REDUCTION:
14046 clauses = c_parser_omp_clause_reduction (parser, clauses);
14047 c_name = "reduction";
14048 break;
14049 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14050 clauses = c_parser_omp_clause_schedule (parser, clauses);
14051 c_name = "schedule";
14052 break;
14053 case PRAGMA_OMP_CLAUSE_SHARED:
14054 clauses = c_parser_omp_clause_shared (parser, clauses);
14055 c_name = "shared";
14056 break;
14057 case PRAGMA_OMP_CLAUSE_UNTIED:
14058 clauses = c_parser_omp_clause_untied (parser, clauses);
14059 c_name = "untied";
14060 break;
14061 case PRAGMA_OMP_CLAUSE_INBRANCH:
14062 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14063 clauses);
14064 c_name = "inbranch";
14065 break;
14066 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14067 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14068 clauses);
14069 c_name = "notinbranch";
14070 break;
14071 case PRAGMA_OMP_CLAUSE_PARALLEL:
14072 clauses
14073 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14074 clauses);
14075 c_name = "parallel";
14076 if (!first)
14078 clause_not_first:
14079 error_at (here, "%qs must be the first clause of %qs",
14080 c_name, where);
14081 clauses = prev;
14083 break;
14084 case PRAGMA_OMP_CLAUSE_FOR:
14085 clauses
14086 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14087 clauses);
14088 c_name = "for";
14089 if (!first)
14090 goto clause_not_first;
14091 break;
14092 case PRAGMA_OMP_CLAUSE_SECTIONS:
14093 clauses
14094 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14095 clauses);
14096 c_name = "sections";
14097 if (!first)
14098 goto clause_not_first;
14099 break;
14100 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14101 clauses
14102 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14103 clauses);
14104 c_name = "taskgroup";
14105 if (!first)
14106 goto clause_not_first;
14107 break;
14108 case PRAGMA_OMP_CLAUSE_LINK:
14109 clauses
14110 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14111 c_name = "link";
14112 break;
14113 case PRAGMA_OMP_CLAUSE_TO:
14114 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14115 clauses
14116 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14117 clauses);
14118 else
14119 clauses = c_parser_omp_clause_to (parser, clauses);
14120 c_name = "to";
14121 break;
14122 case PRAGMA_OMP_CLAUSE_FROM:
14123 clauses = c_parser_omp_clause_from (parser, clauses);
14124 c_name = "from";
14125 break;
14126 case PRAGMA_OMP_CLAUSE_UNIFORM:
14127 clauses = c_parser_omp_clause_uniform (parser, clauses);
14128 c_name = "uniform";
14129 break;
14130 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14131 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14132 c_name = "num_teams";
14133 break;
14134 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14135 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14136 c_name = "thread_limit";
14137 break;
14138 case PRAGMA_OMP_CLAUSE_ALIGNED:
14139 clauses = c_parser_omp_clause_aligned (parser, clauses);
14140 c_name = "aligned";
14141 break;
14142 case PRAGMA_OMP_CLAUSE_LINEAR:
14143 clauses = c_parser_omp_clause_linear (parser, clauses);
14144 c_name = "linear";
14145 break;
14146 case PRAGMA_OMP_CLAUSE_DEPEND:
14147 clauses = c_parser_omp_clause_depend (parser, clauses);
14148 c_name = "depend";
14149 break;
14150 case PRAGMA_OMP_CLAUSE_MAP:
14151 clauses = c_parser_omp_clause_map (parser, clauses);
14152 c_name = "map";
14153 break;
14154 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14155 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14156 c_name = "use_device_ptr";
14157 break;
14158 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14159 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14160 c_name = "is_device_ptr";
14161 break;
14162 case PRAGMA_OMP_CLAUSE_DEVICE:
14163 clauses = c_parser_omp_clause_device (parser, clauses);
14164 c_name = "device";
14165 break;
14166 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14167 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14168 c_name = "dist_schedule";
14169 break;
14170 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14171 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14172 c_name = "proc_bind";
14173 break;
14174 case PRAGMA_OMP_CLAUSE_SAFELEN:
14175 clauses = c_parser_omp_clause_safelen (parser, clauses);
14176 c_name = "safelen";
14177 break;
14178 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14179 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14180 c_name = "simdlen";
14181 break;
14182 case PRAGMA_OMP_CLAUSE_NOGROUP:
14183 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14184 c_name = "nogroup";
14185 break;
14186 case PRAGMA_OMP_CLAUSE_THREADS:
14187 clauses
14188 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14189 clauses);
14190 c_name = "threads";
14191 break;
14192 case PRAGMA_OMP_CLAUSE_SIMD:
14193 clauses
14194 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14195 clauses);
14196 c_name = "simd";
14197 break;
14198 default:
14199 c_parser_error (parser, "expected %<#pragma omp%> clause");
14200 goto saw_error;
14203 first = false;
14205 if (((mask >> c_kind) & 1) == 0)
14207 /* Remove the invalid clause(s) from the list to avoid
14208 confusing the rest of the compiler. */
14209 clauses = prev;
14210 error_at (here, "%qs is not valid for %qs", c_name, where);
14214 saw_error:
14215 c_parser_skip_to_pragma_eol (parser);
14217 if (finish_p)
14219 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14220 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14221 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14224 return clauses;
14227 /* OpenACC 2.0, OpenMP 2.5:
14228 structured-block:
14229 statement
14231 In practice, we're also interested in adding the statement to an
14232 outer node. So it is convenient if we work around the fact that
14233 c_parser_statement calls add_stmt. */
14235 static tree
14236 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14238 tree stmt = push_stmt_list ();
14239 c_parser_statement (parser, if_p);
14240 return pop_stmt_list (stmt);
14243 /* OpenACC 2.0:
14244 # pragma acc cache (variable-list) new-line
14246 LOC is the location of the #pragma token.
14249 static tree
14250 c_parser_oacc_cache (location_t loc, c_parser *parser)
14252 tree stmt, clauses;
14254 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14255 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14257 c_parser_skip_to_pragma_eol (parser);
14259 stmt = make_node (OACC_CACHE);
14260 TREE_TYPE (stmt) = void_type_node;
14261 OACC_CACHE_CLAUSES (stmt) = clauses;
14262 SET_EXPR_LOCATION (stmt, loc);
14263 add_stmt (stmt);
14265 return stmt;
14268 /* OpenACC 2.0:
14269 # pragma acc data oacc-data-clause[optseq] new-line
14270 structured-block
14272 LOC is the location of the #pragma token.
14275 #define OACC_DATA_CLAUSE_MASK \
14276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14288 static tree
14289 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14291 tree stmt, clauses, block;
14293 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14294 "#pragma acc data");
14296 block = c_begin_omp_parallel ();
14297 add_stmt (c_parser_omp_structured_block (parser, if_p));
14299 stmt = c_finish_oacc_data (loc, clauses, block);
14301 return stmt;
14304 /* OpenACC 2.0:
14305 # pragma acc declare oacc-data-clause[optseq] new-line
14308 #define OACC_DECLARE_CLAUSE_MASK \
14309 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14322 static void
14323 c_parser_oacc_declare (c_parser *parser)
14325 location_t pragma_loc = c_parser_peek_token (parser)->location;
14326 tree clauses, stmt, t, decl;
14328 bool error = false;
14330 c_parser_consume_pragma (parser);
14332 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14333 "#pragma acc declare");
14334 if (!clauses)
14336 error_at (pragma_loc,
14337 "no valid clauses specified in %<#pragma acc declare%>");
14338 return;
14341 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14343 location_t loc = OMP_CLAUSE_LOCATION (t);
14344 decl = OMP_CLAUSE_DECL (t);
14345 if (!DECL_P (decl))
14347 error_at (loc, "array section in %<#pragma acc declare%>");
14348 error = true;
14349 continue;
14352 switch (OMP_CLAUSE_MAP_KIND (t))
14354 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14355 case GOMP_MAP_FORCE_ALLOC:
14356 case GOMP_MAP_FORCE_TO:
14357 case GOMP_MAP_FORCE_DEVICEPTR:
14358 case GOMP_MAP_DEVICE_RESIDENT:
14359 break;
14361 case GOMP_MAP_LINK:
14362 if (!global_bindings_p ()
14363 && (TREE_STATIC (decl)
14364 || !DECL_EXTERNAL (decl)))
14366 error_at (loc,
14367 "%qD must be a global variable in "
14368 "%<#pragma acc declare link%>",
14369 decl);
14370 error = true;
14371 continue;
14373 break;
14375 default:
14376 if (global_bindings_p ())
14378 error_at (loc, "invalid OpenACC clause at file scope");
14379 error = true;
14380 continue;
14382 if (DECL_EXTERNAL (decl))
14384 error_at (loc,
14385 "invalid use of %<extern%> variable %qD "
14386 "in %<#pragma acc declare%>", decl);
14387 error = true;
14388 continue;
14390 else if (TREE_PUBLIC (decl))
14392 error_at (loc,
14393 "invalid use of %<global%> variable %qD "
14394 "in %<#pragma acc declare%>", decl);
14395 error = true;
14396 continue;
14398 break;
14401 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14402 || lookup_attribute ("omp declare target link",
14403 DECL_ATTRIBUTES (decl)))
14405 error_at (loc, "variable %qD used more than once with "
14406 "%<#pragma acc declare%>", decl);
14407 error = true;
14408 continue;
14411 if (!error)
14413 tree id;
14415 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14416 id = get_identifier ("omp declare target link");
14417 else
14418 id = get_identifier ("omp declare target");
14420 DECL_ATTRIBUTES (decl)
14421 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14423 if (global_bindings_p ())
14425 symtab_node *node = symtab_node::get (decl);
14426 if (node != NULL)
14428 node->offloadable = 1;
14429 if (ENABLE_OFFLOADING)
14431 g->have_offload = true;
14432 if (is_a <varpool_node *> (node))
14433 vec_safe_push (offload_vars, decl);
14440 if (error || global_bindings_p ())
14441 return;
14443 stmt = make_node (OACC_DECLARE);
14444 TREE_TYPE (stmt) = void_type_node;
14445 OACC_DECLARE_CLAUSES (stmt) = clauses;
14446 SET_EXPR_LOCATION (stmt, pragma_loc);
14448 add_stmt (stmt);
14450 return;
14453 /* OpenACC 2.0:
14454 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14458 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14461 LOC is the location of the #pragma token.
14464 #define OACC_ENTER_DATA_CLAUSE_MASK \
14465 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14473 #define OACC_EXIT_DATA_CLAUSE_MASK \
14474 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14480 static void
14481 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14483 location_t loc = c_parser_peek_token (parser)->location;
14484 tree clauses, stmt;
14485 const char *p = "";
14487 c_parser_consume_pragma (parser);
14489 if (c_parser_next_token_is (parser, CPP_NAME))
14491 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14492 c_parser_consume_token (parser);
14495 if (strcmp (p, "data") != 0)
14497 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14498 enter ? "enter" : "exit");
14499 parser->error = true;
14500 c_parser_skip_to_pragma_eol (parser);
14501 return;
14504 if (enter)
14505 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14506 "#pragma acc enter data");
14507 else
14508 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14509 "#pragma acc exit data");
14511 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14513 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14514 enter ? "enter" : "exit");
14515 return;
14518 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14519 TREE_TYPE (stmt) = void_type_node;
14520 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14521 SET_EXPR_LOCATION (stmt, loc);
14522 add_stmt (stmt);
14526 /* OpenACC 2.0:
14527 # pragma acc host_data oacc-data-clause[optseq] new-line
14528 structured-block
14531 #define OACC_HOST_DATA_CLAUSE_MASK \
14532 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14534 static tree
14535 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14537 tree stmt, clauses, block;
14539 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14540 "#pragma acc host_data");
14542 block = c_begin_omp_parallel ();
14543 add_stmt (c_parser_omp_structured_block (parser, if_p));
14544 stmt = c_finish_oacc_host_data (loc, clauses, block);
14545 return stmt;
14549 /* OpenACC 2.0:
14551 # pragma acc loop oacc-loop-clause[optseq] new-line
14552 structured-block
14554 LOC is the location of the #pragma token.
14557 #define OACC_LOOP_CLAUSE_MASK \
14558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14568 static tree
14569 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14570 omp_clause_mask mask, tree *cclauses, bool *if_p)
14572 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14574 strcat (p_name, " loop");
14575 mask |= OACC_LOOP_CLAUSE_MASK;
14577 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14578 cclauses == NULL);
14579 if (cclauses)
14581 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14582 if (*cclauses)
14583 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14584 if (clauses)
14585 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14588 tree block = c_begin_compound_stmt (true);
14589 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14590 if_p);
14591 block = c_end_compound_stmt (loc, block, true);
14592 add_stmt (block);
14594 return stmt;
14597 /* OpenACC 2.0:
14598 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14599 structured-block
14603 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14604 structured-block
14606 LOC is the location of the #pragma token.
14609 #define OACC_KERNELS_CLAUSE_MASK \
14610 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14628 #define OACC_PARALLEL_CLAUSE_MASK \
14629 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14650 static tree
14651 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14652 enum pragma_kind p_kind, char *p_name,
14653 bool *if_p)
14655 omp_clause_mask mask;
14656 enum tree_code code;
14657 switch (p_kind)
14659 case PRAGMA_OACC_KERNELS:
14660 strcat (p_name, " kernels");
14661 mask = OACC_KERNELS_CLAUSE_MASK;
14662 code = OACC_KERNELS;
14663 break;
14664 case PRAGMA_OACC_PARALLEL:
14665 strcat (p_name, " parallel");
14666 mask = OACC_PARALLEL_CLAUSE_MASK;
14667 code = OACC_PARALLEL;
14668 break;
14669 default:
14670 gcc_unreachable ();
14673 if (c_parser_next_token_is (parser, CPP_NAME))
14675 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14676 if (strcmp (p, "loop") == 0)
14678 c_parser_consume_token (parser);
14679 tree block = c_begin_omp_parallel ();
14680 tree clauses;
14681 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14682 return c_finish_omp_construct (loc, code, block, clauses);
14686 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14688 tree block = c_begin_omp_parallel ();
14689 add_stmt (c_parser_omp_structured_block (parser, if_p));
14691 return c_finish_omp_construct (loc, code, block, clauses);
14694 /* OpenACC 2.0:
14695 # pragma acc routine oacc-routine-clause[optseq] new-line
14696 function-definition
14698 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14701 #define OACC_ROUTINE_CLAUSE_MASK \
14702 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14707 /* Parse an OpenACC routine directive. For named directives, we apply
14708 immediately to the named function. For unnamed ones we then parse
14709 a declaration or definition, which must be for a function. */
14711 static void
14712 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14714 gcc_checking_assert (context == pragma_external);
14716 oacc_routine_data data;
14717 data.error_seen = false;
14718 data.fndecl_seen = false;
14719 data.clauses = NULL_TREE;
14720 data.loc = c_parser_peek_token (parser)->location;
14722 c_parser_consume_pragma (parser);
14724 /* Look for optional '( name )'. */
14725 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14727 c_parser_consume_token (parser); /* '(' */
14729 tree decl = NULL_TREE;
14730 c_token *name_token = c_parser_peek_token (parser);
14731 location_t name_loc = name_token->location;
14732 if (name_token->type == CPP_NAME
14733 && (name_token->id_kind == C_ID_ID
14734 || name_token->id_kind == C_ID_TYPENAME))
14736 decl = lookup_name (name_token->value);
14737 if (!decl)
14738 error_at (name_loc,
14739 "%qE has not been declared", name_token->value);
14740 c_parser_consume_token (parser);
14742 else
14743 c_parser_error (parser, "expected function name");
14745 if (!decl
14746 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14748 c_parser_skip_to_pragma_eol (parser, false);
14749 return;
14752 data.clauses
14753 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14754 "#pragma acc routine");
14756 if (TREE_CODE (decl) != FUNCTION_DECL)
14758 error_at (name_loc, "%qD does not refer to a function", decl);
14759 return;
14762 c_finish_oacc_routine (&data, decl, false);
14764 else /* No optional '( name )'. */
14766 data.clauses
14767 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14768 "#pragma acc routine");
14770 /* Emit a helpful diagnostic if there's another pragma following this
14771 one. Also don't allow a static assertion declaration, as in the
14772 following we'll just parse a *single* "declaration or function
14773 definition", and the static assertion counts an one. */
14774 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14775 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14777 error_at (data.loc,
14778 "%<#pragma acc routine%> not immediately followed by"
14779 " function declaration or definition");
14780 /* ..., and then just keep going. */
14781 return;
14784 /* We only have to consider the pragma_external case here. */
14785 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14786 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14788 int ext = disable_extension_diagnostics ();
14790 c_parser_consume_token (parser);
14791 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14792 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14793 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14794 NULL, vNULL, &data);
14795 restore_extension_diagnostics (ext);
14797 else
14798 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14799 NULL, vNULL, &data);
14803 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14804 IS_DEFN is true if we're applying it to the definition. */
14806 static void
14807 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14808 bool is_defn)
14810 /* Keep going if we're in error reporting mode. */
14811 if (data->error_seen
14812 || fndecl == error_mark_node)
14813 return;
14815 if (data->fndecl_seen)
14817 error_at (data->loc,
14818 "%<#pragma acc routine%> not immediately followed by"
14819 " a single function declaration or definition");
14820 data->error_seen = true;
14821 return;
14823 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14825 error_at (data->loc,
14826 "%<#pragma acc routine%> not immediately followed by"
14827 " function declaration or definition");
14828 data->error_seen = true;
14829 return;
14832 if (oacc_get_fn_attrib (fndecl))
14834 error_at (data->loc,
14835 "%<#pragma acc routine%> already applied to %qD", fndecl);
14836 data->error_seen = true;
14837 return;
14840 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14842 error_at (data->loc,
14843 TREE_USED (fndecl)
14844 ? G_("%<#pragma acc routine%> must be applied before use")
14845 : G_("%<#pragma acc routine%> must be applied before "
14846 "definition"));
14847 data->error_seen = true;
14848 return;
14851 /* Process the routine's dimension clauses. */
14852 tree dims = oacc_build_routine_dims (data->clauses);
14853 oacc_replace_fn_attrib (fndecl, dims);
14855 /* Add an "omp declare target" attribute. */
14856 DECL_ATTRIBUTES (fndecl)
14857 = tree_cons (get_identifier ("omp declare target"),
14858 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14860 /* Remember that we've used this "#pragma acc routine". */
14861 data->fndecl_seen = true;
14864 /* OpenACC 2.0:
14865 # pragma acc update oacc-update-clause[optseq] new-line
14868 #define OACC_UPDATE_CLAUSE_MASK \
14869 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14876 static void
14877 c_parser_oacc_update (c_parser *parser)
14879 location_t loc = c_parser_peek_token (parser)->location;
14881 c_parser_consume_pragma (parser);
14883 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14884 "#pragma acc update");
14885 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14887 error_at (loc,
14888 "%<#pragma acc update%> must contain at least one "
14889 "%<device%> or %<host%> or %<self%> clause");
14890 return;
14893 if (parser->error)
14894 return;
14896 tree stmt = make_node (OACC_UPDATE);
14897 TREE_TYPE (stmt) = void_type_node;
14898 OACC_UPDATE_CLAUSES (stmt) = clauses;
14899 SET_EXPR_LOCATION (stmt, loc);
14900 add_stmt (stmt);
14903 /* OpenACC 2.0:
14904 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14906 LOC is the location of the #pragma token.
14909 #define OACC_WAIT_CLAUSE_MASK \
14910 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14912 static tree
14913 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14915 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14917 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14918 list = c_parser_oacc_wait_list (parser, loc, list);
14920 strcpy (p_name, " wait");
14921 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14922 stmt = c_finish_oacc_wait (loc, list, clauses);
14923 add_stmt (stmt);
14925 return stmt;
14928 /* OpenMP 2.5:
14929 # pragma omp atomic new-line
14930 expression-stmt
14932 expression-stmt:
14933 x binop= expr | x++ | ++x | x-- | --x
14934 binop:
14935 +, *, -, /, &, ^, |, <<, >>
14937 where x is an lvalue expression with scalar type.
14939 OpenMP 3.1:
14940 # pragma omp atomic new-line
14941 update-stmt
14943 # pragma omp atomic read new-line
14944 read-stmt
14946 # pragma omp atomic write new-line
14947 write-stmt
14949 # pragma omp atomic update new-line
14950 update-stmt
14952 # pragma omp atomic capture new-line
14953 capture-stmt
14955 # pragma omp atomic capture new-line
14956 capture-block
14958 read-stmt:
14959 v = x
14960 write-stmt:
14961 x = expr
14962 update-stmt:
14963 expression-stmt | x = x binop expr
14964 capture-stmt:
14965 v = expression-stmt
14966 capture-block:
14967 { v = x; update-stmt; } | { update-stmt; v = x; }
14969 OpenMP 4.0:
14970 update-stmt:
14971 expression-stmt | x = x binop expr | x = expr binop x
14972 capture-stmt:
14973 v = update-stmt
14974 capture-block:
14975 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14977 where x and v are lvalue expressions with scalar type.
14979 LOC is the location of the #pragma token. */
14981 static void
14982 c_parser_omp_atomic (location_t loc, c_parser *parser)
14984 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14985 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14986 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14987 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14988 struct c_expr expr;
14989 location_t eloc;
14990 bool structured_block = false;
14991 bool swapped = false;
14992 bool seq_cst = false;
14993 bool non_lvalue_p;
14995 if (c_parser_next_token_is (parser, CPP_NAME))
14997 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14998 if (!strcmp (p, "seq_cst"))
15000 seq_cst = true;
15001 c_parser_consume_token (parser);
15002 if (c_parser_next_token_is (parser, CPP_COMMA)
15003 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15004 c_parser_consume_token (parser);
15007 if (c_parser_next_token_is (parser, CPP_NAME))
15009 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15011 if (!strcmp (p, "read"))
15012 code = OMP_ATOMIC_READ;
15013 else if (!strcmp (p, "write"))
15014 code = NOP_EXPR;
15015 else if (!strcmp (p, "update"))
15016 code = OMP_ATOMIC;
15017 else if (!strcmp (p, "capture"))
15018 code = OMP_ATOMIC_CAPTURE_NEW;
15019 else
15020 p = NULL;
15021 if (p)
15022 c_parser_consume_token (parser);
15024 if (!seq_cst)
15026 if (c_parser_next_token_is (parser, CPP_COMMA)
15027 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15028 c_parser_consume_token (parser);
15030 if (c_parser_next_token_is (parser, CPP_NAME))
15032 const char *p
15033 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15034 if (!strcmp (p, "seq_cst"))
15036 seq_cst = true;
15037 c_parser_consume_token (parser);
15041 c_parser_skip_to_pragma_eol (parser);
15043 switch (code)
15045 case OMP_ATOMIC_READ:
15046 case NOP_EXPR: /* atomic write */
15047 v = c_parser_cast_expression (parser, NULL).value;
15048 non_lvalue_p = !lvalue_p (v);
15049 v = c_fully_fold (v, false, NULL, true);
15050 if (v == error_mark_node)
15051 goto saw_error;
15052 if (non_lvalue_p)
15053 v = non_lvalue (v);
15054 loc = c_parser_peek_token (parser)->location;
15055 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15056 goto saw_error;
15057 if (code == NOP_EXPR)
15059 lhs = c_parser_expression (parser).value;
15060 lhs = c_fully_fold (lhs, false, NULL);
15061 if (lhs == error_mark_node)
15062 goto saw_error;
15064 else
15066 lhs = c_parser_cast_expression (parser, NULL).value;
15067 non_lvalue_p = !lvalue_p (lhs);
15068 lhs = c_fully_fold (lhs, false, NULL, true);
15069 if (lhs == error_mark_node)
15070 goto saw_error;
15071 if (non_lvalue_p)
15072 lhs = non_lvalue (lhs);
15074 if (code == NOP_EXPR)
15076 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15077 opcode. */
15078 code = OMP_ATOMIC;
15079 rhs = lhs;
15080 lhs = v;
15081 v = NULL_TREE;
15083 goto done;
15084 case OMP_ATOMIC_CAPTURE_NEW:
15085 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15087 c_parser_consume_token (parser);
15088 structured_block = true;
15090 else
15092 v = c_parser_cast_expression (parser, NULL).value;
15093 non_lvalue_p = !lvalue_p (v);
15094 v = c_fully_fold (v, false, NULL, true);
15095 if (v == error_mark_node)
15096 goto saw_error;
15097 if (non_lvalue_p)
15098 v = non_lvalue (v);
15099 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15100 goto saw_error;
15102 break;
15103 default:
15104 break;
15107 /* For structured_block case we don't know yet whether
15108 old or new x should be captured. */
15109 restart:
15110 eloc = c_parser_peek_token (parser)->location;
15111 expr = c_parser_cast_expression (parser, NULL);
15112 lhs = expr.value;
15113 expr = default_function_array_conversion (eloc, expr);
15114 unfolded_lhs = expr.value;
15115 lhs = c_fully_fold (lhs, false, NULL, true);
15116 orig_lhs = lhs;
15117 switch (TREE_CODE (lhs))
15119 case ERROR_MARK:
15120 saw_error:
15121 c_parser_skip_to_end_of_block_or_statement (parser);
15122 if (structured_block)
15124 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15125 c_parser_consume_token (parser);
15126 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15128 c_parser_skip_to_end_of_block_or_statement (parser);
15129 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15130 c_parser_consume_token (parser);
15133 return;
15135 case POSTINCREMENT_EXPR:
15136 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15137 code = OMP_ATOMIC_CAPTURE_OLD;
15138 /* FALLTHROUGH */
15139 case PREINCREMENT_EXPR:
15140 lhs = TREE_OPERAND (lhs, 0);
15141 unfolded_lhs = NULL_TREE;
15142 opcode = PLUS_EXPR;
15143 rhs = integer_one_node;
15144 break;
15146 case POSTDECREMENT_EXPR:
15147 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15148 code = OMP_ATOMIC_CAPTURE_OLD;
15149 /* FALLTHROUGH */
15150 case PREDECREMENT_EXPR:
15151 lhs = TREE_OPERAND (lhs, 0);
15152 unfolded_lhs = NULL_TREE;
15153 opcode = MINUS_EXPR;
15154 rhs = integer_one_node;
15155 break;
15157 case COMPOUND_EXPR:
15158 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15159 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15160 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15161 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15162 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15163 (TREE_OPERAND (lhs, 1), 0), 0)))
15164 == BOOLEAN_TYPE)
15165 /* Undo effects of boolean_increment for post {in,de}crement. */
15166 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15167 /* FALLTHRU */
15168 case MODIFY_EXPR:
15169 if (TREE_CODE (lhs) == MODIFY_EXPR
15170 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15172 /* Undo effects of boolean_increment. */
15173 if (integer_onep (TREE_OPERAND (lhs, 1)))
15175 /* This is pre or post increment. */
15176 rhs = TREE_OPERAND (lhs, 1);
15177 lhs = TREE_OPERAND (lhs, 0);
15178 unfolded_lhs = NULL_TREE;
15179 opcode = NOP_EXPR;
15180 if (code == OMP_ATOMIC_CAPTURE_NEW
15181 && !structured_block
15182 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15183 code = OMP_ATOMIC_CAPTURE_OLD;
15184 break;
15186 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15187 && TREE_OPERAND (lhs, 0)
15188 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15190 /* This is pre or post decrement. */
15191 rhs = TREE_OPERAND (lhs, 1);
15192 lhs = TREE_OPERAND (lhs, 0);
15193 unfolded_lhs = NULL_TREE;
15194 opcode = NOP_EXPR;
15195 if (code == OMP_ATOMIC_CAPTURE_NEW
15196 && !structured_block
15197 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15198 code = OMP_ATOMIC_CAPTURE_OLD;
15199 break;
15202 /* FALLTHRU */
15203 default:
15204 if (!lvalue_p (unfolded_lhs))
15205 lhs = non_lvalue (lhs);
15206 switch (c_parser_peek_token (parser)->type)
15208 case CPP_MULT_EQ:
15209 opcode = MULT_EXPR;
15210 break;
15211 case CPP_DIV_EQ:
15212 opcode = TRUNC_DIV_EXPR;
15213 break;
15214 case CPP_PLUS_EQ:
15215 opcode = PLUS_EXPR;
15216 break;
15217 case CPP_MINUS_EQ:
15218 opcode = MINUS_EXPR;
15219 break;
15220 case CPP_LSHIFT_EQ:
15221 opcode = LSHIFT_EXPR;
15222 break;
15223 case CPP_RSHIFT_EQ:
15224 opcode = RSHIFT_EXPR;
15225 break;
15226 case CPP_AND_EQ:
15227 opcode = BIT_AND_EXPR;
15228 break;
15229 case CPP_OR_EQ:
15230 opcode = BIT_IOR_EXPR;
15231 break;
15232 case CPP_XOR_EQ:
15233 opcode = BIT_XOR_EXPR;
15234 break;
15235 case CPP_EQ:
15236 c_parser_consume_token (parser);
15237 eloc = c_parser_peek_token (parser)->location;
15238 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15239 rhs1 = expr.value;
15240 switch (TREE_CODE (rhs1))
15242 case MULT_EXPR:
15243 case TRUNC_DIV_EXPR:
15244 case RDIV_EXPR:
15245 case PLUS_EXPR:
15246 case MINUS_EXPR:
15247 case LSHIFT_EXPR:
15248 case RSHIFT_EXPR:
15249 case BIT_AND_EXPR:
15250 case BIT_IOR_EXPR:
15251 case BIT_XOR_EXPR:
15252 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15254 opcode = TREE_CODE (rhs1);
15255 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15256 true);
15257 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15258 true);
15259 goto stmt_done;
15261 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15263 opcode = TREE_CODE (rhs1);
15264 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15265 true);
15266 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15267 true);
15268 swapped = !commutative_tree_code (opcode);
15269 goto stmt_done;
15271 break;
15272 case ERROR_MARK:
15273 goto saw_error;
15274 default:
15275 break;
15277 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15279 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15281 code = OMP_ATOMIC_CAPTURE_OLD;
15282 v = lhs;
15283 lhs = NULL_TREE;
15284 expr = default_function_array_read_conversion (eloc, expr);
15285 unfolded_lhs1 = expr.value;
15286 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15287 rhs1 = NULL_TREE;
15288 c_parser_consume_token (parser);
15289 goto restart;
15291 if (structured_block)
15293 opcode = NOP_EXPR;
15294 expr = default_function_array_read_conversion (eloc, expr);
15295 rhs = c_fully_fold (expr.value, false, NULL, true);
15296 rhs1 = NULL_TREE;
15297 goto stmt_done;
15300 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15301 goto saw_error;
15302 default:
15303 c_parser_error (parser,
15304 "invalid operator for %<#pragma omp atomic%>");
15305 goto saw_error;
15308 /* Arrange to pass the location of the assignment operator to
15309 c_finish_omp_atomic. */
15310 loc = c_parser_peek_token (parser)->location;
15311 c_parser_consume_token (parser);
15312 eloc = c_parser_peek_token (parser)->location;
15313 expr = c_parser_expression (parser);
15314 expr = default_function_array_read_conversion (eloc, expr);
15315 rhs = expr.value;
15316 rhs = c_fully_fold (rhs, false, NULL, true);
15317 break;
15319 stmt_done:
15320 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15322 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15323 goto saw_error;
15324 v = c_parser_cast_expression (parser, NULL).value;
15325 non_lvalue_p = !lvalue_p (v);
15326 v = c_fully_fold (v, false, NULL, true);
15327 if (v == error_mark_node)
15328 goto saw_error;
15329 if (non_lvalue_p)
15330 v = non_lvalue (v);
15331 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15332 goto saw_error;
15333 eloc = c_parser_peek_token (parser)->location;
15334 expr = c_parser_cast_expression (parser, NULL);
15335 lhs1 = expr.value;
15336 expr = default_function_array_read_conversion (eloc, expr);
15337 unfolded_lhs1 = expr.value;
15338 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15339 if (lhs1 == error_mark_node)
15340 goto saw_error;
15341 if (!lvalue_p (unfolded_lhs1))
15342 lhs1 = non_lvalue (lhs1);
15344 if (structured_block)
15346 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15347 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15349 done:
15350 if (unfolded_lhs && unfolded_lhs1
15351 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15353 error ("%<#pragma omp atomic capture%> uses two different "
15354 "expressions for memory");
15355 stmt = error_mark_node;
15357 else
15358 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15359 swapped, seq_cst);
15360 if (stmt != error_mark_node)
15361 add_stmt (stmt);
15363 if (!structured_block)
15364 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15368 /* OpenMP 2.5:
15369 # pragma omp barrier new-line
15372 static void
15373 c_parser_omp_barrier (c_parser *parser)
15375 location_t loc = c_parser_peek_token (parser)->location;
15376 c_parser_consume_pragma (parser);
15377 c_parser_skip_to_pragma_eol (parser);
15379 c_finish_omp_barrier (loc);
15382 /* OpenMP 2.5:
15383 # pragma omp critical [(name)] new-line
15384 structured-block
15386 OpenMP 4.5:
15387 # pragma omp critical [(name) [hint(expression)]] new-line
15389 LOC is the location of the #pragma itself. */
15391 #define OMP_CRITICAL_CLAUSE_MASK \
15392 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15394 static tree
15395 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15397 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15399 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15401 c_parser_consume_token (parser);
15402 if (c_parser_next_token_is (parser, CPP_NAME))
15404 name = c_parser_peek_token (parser)->value;
15405 c_parser_consume_token (parser);
15406 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15408 else
15409 c_parser_error (parser, "expected identifier");
15411 clauses = c_parser_omp_all_clauses (parser,
15412 OMP_CRITICAL_CLAUSE_MASK,
15413 "#pragma omp critical");
15415 else
15417 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15418 c_parser_error (parser, "expected %<(%> or end of line");
15419 c_parser_skip_to_pragma_eol (parser);
15422 stmt = c_parser_omp_structured_block (parser, if_p);
15423 return c_finish_omp_critical (loc, stmt, name, clauses);
15426 /* OpenMP 2.5:
15427 # pragma omp flush flush-vars[opt] new-line
15429 flush-vars:
15430 ( variable-list ) */
15432 static void
15433 c_parser_omp_flush (c_parser *parser)
15435 location_t loc = c_parser_peek_token (parser)->location;
15436 c_parser_consume_pragma (parser);
15437 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15438 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15439 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15440 c_parser_error (parser, "expected %<(%> or end of line");
15441 c_parser_skip_to_pragma_eol (parser);
15443 c_finish_omp_flush (loc);
15446 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15447 The real trick here is to determine the loop control variable early
15448 so that we can push a new decl if necessary to make it private.
15449 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15450 respectively. */
15452 static tree
15453 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15454 tree clauses, tree *cclauses, bool *if_p)
15456 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15457 tree declv, condv, incrv, initv, ret = NULL_TREE;
15458 tree pre_body = NULL_TREE, this_pre_body;
15459 tree ordered_cl = NULL_TREE;
15460 bool fail = false, open_brace_parsed = false;
15461 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15462 location_t for_loc;
15463 bool tiling = false;
15464 vec<tree, va_gc> *for_block = make_tree_vector ();
15466 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15467 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15468 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15469 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15471 tiling = true;
15472 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15474 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15475 && OMP_CLAUSE_ORDERED_EXPR (cl))
15477 ordered_cl = cl;
15478 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15481 if (ordered && ordered < collapse)
15483 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15484 "%<ordered%> clause parameter is less than %<collapse%>");
15485 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15486 = build_int_cst (NULL_TREE, collapse);
15487 ordered = collapse;
15489 if (ordered)
15491 for (tree *pc = &clauses; *pc; )
15492 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15494 error_at (OMP_CLAUSE_LOCATION (*pc),
15495 "%<linear%> clause may not be specified together "
15496 "with %<ordered%> clause with a parameter");
15497 *pc = OMP_CLAUSE_CHAIN (*pc);
15499 else
15500 pc = &OMP_CLAUSE_CHAIN (*pc);
15503 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15504 count = ordered ? ordered : collapse;
15506 declv = make_tree_vec (count);
15507 initv = make_tree_vec (count);
15508 condv = make_tree_vec (count);
15509 incrv = make_tree_vec (count);
15511 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15513 c_parser_error (parser, "for statement expected");
15514 return NULL;
15516 for_loc = c_parser_peek_token (parser)->location;
15517 c_parser_consume_token (parser);
15519 for (i = 0; i < count; i++)
15521 int bracecount = 0;
15523 matching_parens parens;
15524 if (!parens.require_open (parser))
15525 goto pop_scopes;
15527 /* Parse the initialization declaration or expression. */
15528 if (c_parser_next_tokens_start_declaration (parser))
15530 if (i > 0)
15531 vec_safe_push (for_block, c_begin_compound_stmt (true));
15532 this_pre_body = push_stmt_list ();
15533 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15534 NULL, vNULL);
15535 if (this_pre_body)
15537 this_pre_body = pop_stmt_list (this_pre_body);
15538 if (pre_body)
15540 tree t = pre_body;
15541 pre_body = push_stmt_list ();
15542 add_stmt (t);
15543 add_stmt (this_pre_body);
15544 pre_body = pop_stmt_list (pre_body);
15546 else
15547 pre_body = this_pre_body;
15549 decl = check_for_loop_decls (for_loc, flag_isoc99);
15550 if (decl == NULL)
15551 goto error_init;
15552 if (DECL_INITIAL (decl) == error_mark_node)
15553 decl = error_mark_node;
15554 init = decl;
15556 else if (c_parser_next_token_is (parser, CPP_NAME)
15557 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15559 struct c_expr decl_exp;
15560 struct c_expr init_exp;
15561 location_t init_loc;
15563 decl_exp = c_parser_postfix_expression (parser);
15564 decl = decl_exp.value;
15566 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15568 init_loc = c_parser_peek_token (parser)->location;
15569 init_exp = c_parser_expr_no_commas (parser, NULL);
15570 init_exp = default_function_array_read_conversion (init_loc,
15571 init_exp);
15572 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15573 NOP_EXPR, init_loc, init_exp.value,
15574 init_exp.original_type);
15575 init = c_process_expr_stmt (init_loc, init);
15577 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15579 else
15581 error_init:
15582 c_parser_error (parser,
15583 "expected iteration declaration or initialization");
15584 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15585 "expected %<)%>");
15586 fail = true;
15587 goto parse_next;
15590 /* Parse the loop condition. */
15591 cond = NULL_TREE;
15592 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15594 location_t cond_loc = c_parser_peek_token (parser)->location;
15595 struct c_expr cond_expr
15596 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15598 cond = cond_expr.value;
15599 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15600 if (COMPARISON_CLASS_P (cond))
15602 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15603 op0 = c_fully_fold (op0, false, NULL);
15604 op1 = c_fully_fold (op1, false, NULL);
15605 TREE_OPERAND (cond, 0) = op0;
15606 TREE_OPERAND (cond, 1) = op1;
15608 switch (cond_expr.original_code)
15610 case GT_EXPR:
15611 case GE_EXPR:
15612 case LT_EXPR:
15613 case LE_EXPR:
15614 break;
15615 default:
15616 /* Can't be cond = error_mark_node, because we want to preserve
15617 the location until c_finish_omp_for. */
15618 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15619 break;
15621 protected_set_expr_location (cond, cond_loc);
15623 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15625 /* Parse the increment expression. */
15626 incr = NULL_TREE;
15627 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15629 location_t incr_loc = c_parser_peek_token (parser)->location;
15631 incr = c_process_expr_stmt (incr_loc,
15632 c_parser_expression (parser).value);
15634 parens.skip_until_found_close (parser);
15636 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15637 fail = true;
15638 else
15640 TREE_VEC_ELT (declv, i) = decl;
15641 TREE_VEC_ELT (initv, i) = init;
15642 TREE_VEC_ELT (condv, i) = cond;
15643 TREE_VEC_ELT (incrv, i) = incr;
15646 parse_next:
15647 if (i == count - 1)
15648 break;
15650 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15651 in between the collapsed for loops to be still considered perfectly
15652 nested. Hopefully the final version clarifies this.
15653 For now handle (multiple) {'s and empty statements. */
15656 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15658 c_parser_consume_token (parser);
15659 break;
15661 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15663 c_parser_consume_token (parser);
15664 bracecount++;
15666 else if (bracecount
15667 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15668 c_parser_consume_token (parser);
15669 else
15671 c_parser_error (parser, "not enough perfectly nested loops");
15672 if (bracecount)
15674 open_brace_parsed = true;
15675 bracecount--;
15677 fail = true;
15678 count = 0;
15679 break;
15682 while (1);
15684 nbraces += bracecount;
15687 if (nbraces)
15688 if_p = NULL;
15690 save_break = c_break_label;
15691 c_break_label = size_one_node;
15692 save_cont = c_cont_label;
15693 c_cont_label = NULL_TREE;
15694 body = push_stmt_list ();
15696 if (open_brace_parsed)
15698 location_t here = c_parser_peek_token (parser)->location;
15699 stmt = c_begin_compound_stmt (true);
15700 c_parser_compound_statement_nostart (parser);
15701 add_stmt (c_end_compound_stmt (here, stmt, true));
15703 else
15704 add_stmt (c_parser_c99_block_statement (parser, if_p));
15705 if (c_cont_label)
15707 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15708 SET_EXPR_LOCATION (t, loc);
15709 add_stmt (t);
15712 body = pop_stmt_list (body);
15713 c_break_label = save_break;
15714 c_cont_label = save_cont;
15716 while (nbraces)
15718 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15720 c_parser_consume_token (parser);
15721 nbraces--;
15723 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15724 c_parser_consume_token (parser);
15725 else
15727 c_parser_error (parser, "collapsed loops not perfectly nested");
15728 while (nbraces)
15730 location_t here = c_parser_peek_token (parser)->location;
15731 stmt = c_begin_compound_stmt (true);
15732 add_stmt (body);
15733 c_parser_compound_statement_nostart (parser);
15734 body = c_end_compound_stmt (here, stmt, true);
15735 nbraces--;
15737 goto pop_scopes;
15741 /* Only bother calling c_finish_omp_for if we haven't already generated
15742 an error from the initialization parsing. */
15743 if (!fail)
15745 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15746 incrv, body, pre_body);
15748 /* Check for iterators appearing in lb, b or incr expressions. */
15749 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15750 stmt = NULL_TREE;
15752 if (stmt)
15754 add_stmt (stmt);
15756 if (cclauses != NULL
15757 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15759 tree *c;
15760 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15761 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15762 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15763 c = &OMP_CLAUSE_CHAIN (*c);
15764 else
15766 for (i = 0; i < count; i++)
15767 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15768 break;
15769 if (i == count)
15770 c = &OMP_CLAUSE_CHAIN (*c);
15771 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15773 error_at (loc,
15774 "iteration variable %qD should not be firstprivate",
15775 OMP_CLAUSE_DECL (*c));
15776 *c = OMP_CLAUSE_CHAIN (*c);
15778 else
15780 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15781 tree l = *c;
15782 *c = OMP_CLAUSE_CHAIN (*c);
15783 if (code == OMP_SIMD)
15785 OMP_CLAUSE_CHAIN (l)
15786 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15787 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15789 else
15791 OMP_CLAUSE_CHAIN (l) = clauses;
15792 clauses = l;
15797 OMP_FOR_CLAUSES (stmt) = clauses;
15799 ret = stmt;
15801 pop_scopes:
15802 while (!for_block->is_empty ())
15804 /* FIXME diagnostics: LOC below should be the actual location of
15805 this particular for block. We need to build a list of
15806 locations to go along with FOR_BLOCK. */
15807 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15808 add_stmt (stmt);
15810 release_tree_vector (for_block);
15811 return ret;
15814 /* Helper function for OpenMP parsing, split clauses and call
15815 finish_omp_clauses on each of the set of clauses afterwards. */
15817 static void
15818 omp_split_clauses (location_t loc, enum tree_code code,
15819 omp_clause_mask mask, tree clauses, tree *cclauses)
15821 int i;
15822 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15823 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15824 if (cclauses[i])
15825 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15828 /* OpenMP 4.0:
15829 #pragma omp simd simd-clause[optseq] new-line
15830 for-loop
15832 LOC is the location of the #pragma token.
15835 #define OMP_SIMD_CLAUSE_MASK \
15836 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15845 static tree
15846 c_parser_omp_simd (location_t loc, c_parser *parser,
15847 char *p_name, omp_clause_mask mask, tree *cclauses,
15848 bool *if_p)
15850 tree block, clauses, ret;
15852 strcat (p_name, " simd");
15853 mask |= OMP_SIMD_CLAUSE_MASK;
15855 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15856 if (cclauses)
15858 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15859 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15860 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15861 OMP_CLAUSE_ORDERED);
15862 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15864 error_at (OMP_CLAUSE_LOCATION (c),
15865 "%<ordered%> clause with parameter may not be specified "
15866 "on %qs construct", p_name);
15867 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15871 block = c_begin_compound_stmt (true);
15872 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15873 block = c_end_compound_stmt (loc, block, true);
15874 add_stmt (block);
15876 return ret;
15879 /* OpenMP 2.5:
15880 #pragma omp for for-clause[optseq] new-line
15881 for-loop
15883 OpenMP 4.0:
15884 #pragma omp for simd for-simd-clause[optseq] new-line
15885 for-loop
15887 LOC is the location of the #pragma token.
15890 #define OMP_FOR_CLAUSE_MASK \
15891 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15901 static tree
15902 c_parser_omp_for (location_t loc, c_parser *parser,
15903 char *p_name, omp_clause_mask mask, tree *cclauses,
15904 bool *if_p)
15906 tree block, clauses, ret;
15908 strcat (p_name, " for");
15909 mask |= OMP_FOR_CLAUSE_MASK;
15910 /* parallel for{, simd} disallows nowait clause, but for
15911 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15912 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15913 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15914 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15915 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15916 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15918 if (c_parser_next_token_is (parser, CPP_NAME))
15920 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15922 if (strcmp (p, "simd") == 0)
15924 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15925 if (cclauses == NULL)
15926 cclauses = cclauses_buf;
15928 c_parser_consume_token (parser);
15929 if (!flag_openmp) /* flag_openmp_simd */
15930 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15931 if_p);
15932 block = c_begin_compound_stmt (true);
15933 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15934 block = c_end_compound_stmt (loc, block, true);
15935 if (ret == NULL_TREE)
15936 return ret;
15937 ret = make_node (OMP_FOR);
15938 TREE_TYPE (ret) = void_type_node;
15939 OMP_FOR_BODY (ret) = block;
15940 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15941 SET_EXPR_LOCATION (ret, loc);
15942 add_stmt (ret);
15943 return ret;
15946 if (!flag_openmp) /* flag_openmp_simd */
15948 c_parser_skip_to_pragma_eol (parser, false);
15949 return NULL_TREE;
15952 /* Composite distribute parallel for disallows linear clause. */
15953 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15954 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15956 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15957 if (cclauses)
15959 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15960 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15963 block = c_begin_compound_stmt (true);
15964 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15965 block = c_end_compound_stmt (loc, block, true);
15966 add_stmt (block);
15968 return ret;
15971 /* OpenMP 2.5:
15972 # pragma omp master new-line
15973 structured-block
15975 LOC is the location of the #pragma token.
15978 static tree
15979 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15981 c_parser_skip_to_pragma_eol (parser);
15982 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15983 if_p));
15986 /* OpenMP 2.5:
15987 # pragma omp ordered new-line
15988 structured-block
15990 OpenMP 4.5:
15991 # pragma omp ordered ordered-clauses new-line
15992 structured-block
15994 # pragma omp ordered depend-clauses new-line */
15996 #define OMP_ORDERED_CLAUSE_MASK \
15997 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16000 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16001 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16003 static bool
16004 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16005 bool *if_p)
16007 location_t loc = c_parser_peek_token (parser)->location;
16008 c_parser_consume_pragma (parser);
16010 if (context != pragma_stmt && context != pragma_compound)
16012 c_parser_error (parser, "expected declaration specifiers");
16013 c_parser_skip_to_pragma_eol (parser, false);
16014 return false;
16017 if (c_parser_next_token_is (parser, CPP_NAME))
16019 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16021 if (!strcmp ("depend", p))
16023 if (!flag_openmp) /* flag_openmp_simd */
16025 c_parser_skip_to_pragma_eol (parser, false);
16026 return false;
16028 if (context == pragma_stmt)
16030 error_at (loc,
16031 "%<#pragma omp ordered%> with %<depend%> clause may "
16032 "only be used in compound statements");
16033 c_parser_skip_to_pragma_eol (parser, false);
16034 return false;
16037 tree clauses
16038 = c_parser_omp_all_clauses (parser,
16039 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16040 "#pragma omp ordered");
16041 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16042 return false;
16046 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16047 "#pragma omp ordered");
16049 if (!flag_openmp /* flag_openmp_simd */
16050 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16051 return false;
16053 c_finish_omp_ordered (loc, clauses,
16054 c_parser_omp_structured_block (parser, if_p));
16055 return true;
16058 /* OpenMP 2.5:
16060 section-scope:
16061 { section-sequence }
16063 section-sequence:
16064 section-directive[opt] structured-block
16065 section-sequence section-directive structured-block
16067 SECTIONS_LOC is the location of the #pragma omp sections. */
16069 static tree
16070 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16072 tree stmt, substmt;
16073 bool error_suppress = false;
16074 location_t loc;
16076 loc = c_parser_peek_token (parser)->location;
16077 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16079 /* Avoid skipping until the end of the block. */
16080 parser->error = false;
16081 return NULL_TREE;
16084 stmt = push_stmt_list ();
16086 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16088 substmt = c_parser_omp_structured_block (parser, NULL);
16089 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16090 SET_EXPR_LOCATION (substmt, loc);
16091 add_stmt (substmt);
16094 while (1)
16096 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16097 break;
16098 if (c_parser_next_token_is (parser, CPP_EOF))
16099 break;
16101 loc = c_parser_peek_token (parser)->location;
16102 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16104 c_parser_consume_pragma (parser);
16105 c_parser_skip_to_pragma_eol (parser);
16106 error_suppress = false;
16108 else if (!error_suppress)
16110 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16111 error_suppress = true;
16114 substmt = c_parser_omp_structured_block (parser, NULL);
16115 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16116 SET_EXPR_LOCATION (substmt, loc);
16117 add_stmt (substmt);
16119 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16120 "expected %<#pragma omp section%> or %<}%>");
16122 substmt = pop_stmt_list (stmt);
16124 stmt = make_node (OMP_SECTIONS);
16125 SET_EXPR_LOCATION (stmt, sections_loc);
16126 TREE_TYPE (stmt) = void_type_node;
16127 OMP_SECTIONS_BODY (stmt) = substmt;
16129 return add_stmt (stmt);
16132 /* OpenMP 2.5:
16133 # pragma omp sections sections-clause[optseq] newline
16134 sections-scope
16136 LOC is the location of the #pragma token.
16139 #define OMP_SECTIONS_CLAUSE_MASK \
16140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16146 static tree
16147 c_parser_omp_sections (location_t loc, c_parser *parser,
16148 char *p_name, omp_clause_mask mask, tree *cclauses)
16150 tree block, clauses, ret;
16152 strcat (p_name, " sections");
16153 mask |= OMP_SECTIONS_CLAUSE_MASK;
16154 if (cclauses)
16155 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16157 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16158 if (cclauses)
16160 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16161 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16164 block = c_begin_compound_stmt (true);
16165 ret = c_parser_omp_sections_scope (loc, parser);
16166 if (ret)
16167 OMP_SECTIONS_CLAUSES (ret) = clauses;
16168 block = c_end_compound_stmt (loc, block, true);
16169 add_stmt (block);
16171 return ret;
16174 /* OpenMP 2.5:
16175 # pragma omp parallel parallel-clause[optseq] new-line
16176 structured-block
16177 # pragma omp parallel for parallel-for-clause[optseq] new-line
16178 structured-block
16179 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16180 structured-block
16182 OpenMP 4.0:
16183 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16184 structured-block
16186 LOC is the location of the #pragma token.
16189 #define OMP_PARALLEL_CLAUSE_MASK \
16190 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16200 static tree
16201 c_parser_omp_parallel (location_t loc, c_parser *parser,
16202 char *p_name, omp_clause_mask mask, tree *cclauses,
16203 bool *if_p)
16205 tree stmt, clauses, block;
16207 strcat (p_name, " parallel");
16208 mask |= OMP_PARALLEL_CLAUSE_MASK;
16209 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16210 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16211 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16212 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16214 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16216 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16217 if (cclauses == NULL)
16218 cclauses = cclauses_buf;
16220 c_parser_consume_token (parser);
16221 if (!flag_openmp) /* flag_openmp_simd */
16222 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16223 block = c_begin_omp_parallel ();
16224 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16225 stmt
16226 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16227 block);
16228 if (ret == NULL_TREE)
16229 return ret;
16230 OMP_PARALLEL_COMBINED (stmt) = 1;
16231 return stmt;
16233 /* When combined with distribute, parallel has to be followed by for.
16234 #pragma omp target parallel is allowed though. */
16235 else if (cclauses
16236 && (mask & (OMP_CLAUSE_MASK_1
16237 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16239 error_at (loc, "expected %<for%> after %qs", p_name);
16240 c_parser_skip_to_pragma_eol (parser);
16241 return NULL_TREE;
16243 else if (!flag_openmp) /* flag_openmp_simd */
16245 c_parser_skip_to_pragma_eol (parser, false);
16246 return NULL_TREE;
16248 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16250 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16251 if (strcmp (p, "sections") == 0)
16253 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16254 if (cclauses == NULL)
16255 cclauses = cclauses_buf;
16257 c_parser_consume_token (parser);
16258 block = c_begin_omp_parallel ();
16259 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16260 stmt = c_finish_omp_parallel (loc,
16261 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16262 block);
16263 OMP_PARALLEL_COMBINED (stmt) = 1;
16264 return stmt;
16268 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16269 if (cclauses)
16271 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16272 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16275 block = c_begin_omp_parallel ();
16276 c_parser_statement (parser, if_p);
16277 stmt = c_finish_omp_parallel (loc, clauses, block);
16279 return stmt;
16282 /* OpenMP 2.5:
16283 # pragma omp single single-clause[optseq] new-line
16284 structured-block
16286 LOC is the location of the #pragma.
16289 #define OMP_SINGLE_CLAUSE_MASK \
16290 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16295 static tree
16296 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16298 tree stmt = make_node (OMP_SINGLE);
16299 SET_EXPR_LOCATION (stmt, loc);
16300 TREE_TYPE (stmt) = void_type_node;
16302 OMP_SINGLE_CLAUSES (stmt)
16303 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16304 "#pragma omp single");
16305 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16307 return add_stmt (stmt);
16310 /* OpenMP 3.0:
16311 # pragma omp task task-clause[optseq] new-line
16313 LOC is the location of the #pragma.
16316 #define OMP_TASK_CLAUSE_MASK \
16317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16328 static tree
16329 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16331 tree clauses, block;
16333 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16334 "#pragma omp task");
16336 block = c_begin_omp_task ();
16337 c_parser_statement (parser, if_p);
16338 return c_finish_omp_task (loc, clauses, block);
16341 /* OpenMP 3.0:
16342 # pragma omp taskwait new-line
16345 static void
16346 c_parser_omp_taskwait (c_parser *parser)
16348 location_t loc = c_parser_peek_token (parser)->location;
16349 c_parser_consume_pragma (parser);
16350 c_parser_skip_to_pragma_eol (parser);
16352 c_finish_omp_taskwait (loc);
16355 /* OpenMP 3.1:
16356 # pragma omp taskyield new-line
16359 static void
16360 c_parser_omp_taskyield (c_parser *parser)
16362 location_t loc = c_parser_peek_token (parser)->location;
16363 c_parser_consume_pragma (parser);
16364 c_parser_skip_to_pragma_eol (parser);
16366 c_finish_omp_taskyield (loc);
16369 /* OpenMP 4.0:
16370 # pragma omp taskgroup new-line
16373 static tree
16374 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16376 location_t loc = c_parser_peek_token (parser)->location;
16377 c_parser_skip_to_pragma_eol (parser);
16378 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16379 if_p));
16382 /* OpenMP 4.0:
16383 # pragma omp cancel cancel-clause[optseq] new-line
16385 LOC is the location of the #pragma.
16388 #define OMP_CANCEL_CLAUSE_MASK \
16389 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16395 static void
16396 c_parser_omp_cancel (c_parser *parser)
16398 location_t loc = c_parser_peek_token (parser)->location;
16400 c_parser_consume_pragma (parser);
16401 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16402 "#pragma omp cancel");
16404 c_finish_omp_cancel (loc, clauses);
16407 /* OpenMP 4.0:
16408 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16410 LOC is the location of the #pragma.
16413 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16414 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16419 static void
16420 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16422 location_t loc = c_parser_peek_token (parser)->location;
16423 tree clauses;
16424 bool point_seen = false;
16426 c_parser_consume_pragma (parser);
16427 if (c_parser_next_token_is (parser, CPP_NAME))
16429 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16430 if (strcmp (p, "point") == 0)
16432 c_parser_consume_token (parser);
16433 point_seen = true;
16436 if (!point_seen)
16438 c_parser_error (parser, "expected %<point%>");
16439 c_parser_skip_to_pragma_eol (parser);
16440 return;
16443 if (context != pragma_compound)
16445 if (context == pragma_stmt)
16446 error_at (loc,
16447 "%<#pragma %s%> may only be used in compound statements",
16448 "omp cancellation point");
16449 else
16450 c_parser_error (parser, "expected declaration specifiers");
16451 c_parser_skip_to_pragma_eol (parser, false);
16452 return;
16455 clauses
16456 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16457 "#pragma omp cancellation point");
16459 c_finish_omp_cancellation_point (loc, clauses);
16462 /* OpenMP 4.0:
16463 #pragma omp distribute distribute-clause[optseq] new-line
16464 for-loop */
16466 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16473 static tree
16474 c_parser_omp_distribute (location_t loc, c_parser *parser,
16475 char *p_name, omp_clause_mask mask, tree *cclauses,
16476 bool *if_p)
16478 tree clauses, block, ret;
16480 strcat (p_name, " distribute");
16481 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16483 if (c_parser_next_token_is (parser, CPP_NAME))
16485 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16486 bool simd = false;
16487 bool parallel = false;
16489 if (strcmp (p, "simd") == 0)
16490 simd = true;
16491 else
16492 parallel = strcmp (p, "parallel") == 0;
16493 if (parallel || simd)
16495 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16496 if (cclauses == NULL)
16497 cclauses = cclauses_buf;
16498 c_parser_consume_token (parser);
16499 if (!flag_openmp) /* flag_openmp_simd */
16501 if (simd)
16502 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16503 if_p);
16504 else
16505 return c_parser_omp_parallel (loc, parser, p_name, mask,
16506 cclauses, if_p);
16508 block = c_begin_compound_stmt (true);
16509 if (simd)
16510 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16511 if_p);
16512 else
16513 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16514 if_p);
16515 block = c_end_compound_stmt (loc, block, true);
16516 if (ret == NULL)
16517 return ret;
16518 ret = make_node (OMP_DISTRIBUTE);
16519 TREE_TYPE (ret) = void_type_node;
16520 OMP_FOR_BODY (ret) = block;
16521 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16522 SET_EXPR_LOCATION (ret, loc);
16523 add_stmt (ret);
16524 return ret;
16527 if (!flag_openmp) /* flag_openmp_simd */
16529 c_parser_skip_to_pragma_eol (parser, false);
16530 return NULL_TREE;
16533 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16534 if (cclauses)
16536 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16537 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16540 block = c_begin_compound_stmt (true);
16541 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16542 if_p);
16543 block = c_end_compound_stmt (loc, block, true);
16544 add_stmt (block);
16546 return ret;
16549 /* OpenMP 4.0:
16550 # pragma omp teams teams-clause[optseq] new-line
16551 structured-block */
16553 #define OMP_TEAMS_CLAUSE_MASK \
16554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16562 static tree
16563 c_parser_omp_teams (location_t loc, c_parser *parser,
16564 char *p_name, omp_clause_mask mask, tree *cclauses,
16565 bool *if_p)
16567 tree clauses, block, ret;
16569 strcat (p_name, " teams");
16570 mask |= OMP_TEAMS_CLAUSE_MASK;
16572 if (c_parser_next_token_is (parser, CPP_NAME))
16574 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16575 if (strcmp (p, "distribute") == 0)
16577 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16578 if (cclauses == NULL)
16579 cclauses = cclauses_buf;
16581 c_parser_consume_token (parser);
16582 if (!flag_openmp) /* flag_openmp_simd */
16583 return c_parser_omp_distribute (loc, parser, p_name, mask,
16584 cclauses, if_p);
16585 block = c_begin_compound_stmt (true);
16586 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16587 if_p);
16588 block = c_end_compound_stmt (loc, block, true);
16589 if (ret == NULL)
16590 return ret;
16591 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16592 ret = make_node (OMP_TEAMS);
16593 TREE_TYPE (ret) = void_type_node;
16594 OMP_TEAMS_CLAUSES (ret) = clauses;
16595 OMP_TEAMS_BODY (ret) = block;
16596 OMP_TEAMS_COMBINED (ret) = 1;
16597 return add_stmt (ret);
16600 if (!flag_openmp) /* flag_openmp_simd */
16602 c_parser_skip_to_pragma_eol (parser, false);
16603 return NULL_TREE;
16606 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16607 if (cclauses)
16609 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16610 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16613 tree stmt = make_node (OMP_TEAMS);
16614 TREE_TYPE (stmt) = void_type_node;
16615 OMP_TEAMS_CLAUSES (stmt) = clauses;
16616 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16618 return add_stmt (stmt);
16621 /* OpenMP 4.0:
16622 # pragma omp target data target-data-clause[optseq] new-line
16623 structured-block */
16625 #define OMP_TARGET_DATA_CLAUSE_MASK \
16626 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16631 static tree
16632 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16634 tree clauses
16635 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16636 "#pragma omp target data");
16637 int map_seen = 0;
16638 for (tree *pc = &clauses; *pc;)
16640 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16641 switch (OMP_CLAUSE_MAP_KIND (*pc))
16643 case GOMP_MAP_TO:
16644 case GOMP_MAP_ALWAYS_TO:
16645 case GOMP_MAP_FROM:
16646 case GOMP_MAP_ALWAYS_FROM:
16647 case GOMP_MAP_TOFROM:
16648 case GOMP_MAP_ALWAYS_TOFROM:
16649 case GOMP_MAP_ALLOC:
16650 map_seen = 3;
16651 break;
16652 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16653 case GOMP_MAP_ALWAYS_POINTER:
16654 break;
16655 default:
16656 map_seen |= 1;
16657 error_at (OMP_CLAUSE_LOCATION (*pc),
16658 "%<#pragma omp target data%> with map-type other "
16659 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16660 "on %<map%> clause");
16661 *pc = OMP_CLAUSE_CHAIN (*pc);
16662 continue;
16664 pc = &OMP_CLAUSE_CHAIN (*pc);
16667 if (map_seen != 3)
16669 if (map_seen == 0)
16670 error_at (loc,
16671 "%<#pragma omp target data%> must contain at least "
16672 "one %<map%> clause");
16673 return NULL_TREE;
16676 tree stmt = make_node (OMP_TARGET_DATA);
16677 TREE_TYPE (stmt) = void_type_node;
16678 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16679 keep_next_level ();
16680 tree block = c_begin_compound_stmt (true);
16681 add_stmt (c_parser_omp_structured_block (parser, if_p));
16682 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16684 SET_EXPR_LOCATION (stmt, loc);
16685 return add_stmt (stmt);
16688 /* OpenMP 4.0:
16689 # pragma omp target update target-update-clause[optseq] new-line */
16691 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16692 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16699 static bool
16700 c_parser_omp_target_update (location_t loc, c_parser *parser,
16701 enum pragma_context context)
16703 if (context == pragma_stmt)
16705 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16706 "omp target update");
16707 c_parser_skip_to_pragma_eol (parser, false);
16708 return false;
16711 tree clauses
16712 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16713 "#pragma omp target update");
16714 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16715 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16717 error_at (loc,
16718 "%<#pragma omp target update%> must contain at least one "
16719 "%<from%> or %<to%> clauses");
16720 return false;
16723 tree stmt = make_node (OMP_TARGET_UPDATE);
16724 TREE_TYPE (stmt) = void_type_node;
16725 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16726 SET_EXPR_LOCATION (stmt, loc);
16727 add_stmt (stmt);
16728 return false;
16731 /* OpenMP 4.5:
16732 # pragma omp target enter data target-data-clause[optseq] new-line */
16734 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16735 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16741 static tree
16742 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16743 enum pragma_context context)
16745 bool data_seen = false;
16746 if (c_parser_next_token_is (parser, CPP_NAME))
16748 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16749 if (strcmp (p, "data") == 0)
16751 c_parser_consume_token (parser);
16752 data_seen = true;
16755 if (!data_seen)
16757 c_parser_error (parser, "expected %<data%>");
16758 c_parser_skip_to_pragma_eol (parser);
16759 return NULL_TREE;
16762 if (context == pragma_stmt)
16764 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16765 "omp target enter data");
16766 c_parser_skip_to_pragma_eol (parser, false);
16767 return NULL_TREE;
16770 tree clauses
16771 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16772 "#pragma omp target enter data");
16773 int map_seen = 0;
16774 for (tree *pc = &clauses; *pc;)
16776 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16777 switch (OMP_CLAUSE_MAP_KIND (*pc))
16779 case GOMP_MAP_TO:
16780 case GOMP_MAP_ALWAYS_TO:
16781 case GOMP_MAP_ALLOC:
16782 map_seen = 3;
16783 break;
16784 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16785 case GOMP_MAP_ALWAYS_POINTER:
16786 break;
16787 default:
16788 map_seen |= 1;
16789 error_at (OMP_CLAUSE_LOCATION (*pc),
16790 "%<#pragma omp target enter data%> with map-type other "
16791 "than %<to%> or %<alloc%> on %<map%> clause");
16792 *pc = OMP_CLAUSE_CHAIN (*pc);
16793 continue;
16795 pc = &OMP_CLAUSE_CHAIN (*pc);
16798 if (map_seen != 3)
16800 if (map_seen == 0)
16801 error_at (loc,
16802 "%<#pragma omp target enter data%> must contain at least "
16803 "one %<map%> clause");
16804 return NULL_TREE;
16807 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16808 TREE_TYPE (stmt) = void_type_node;
16809 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16810 SET_EXPR_LOCATION (stmt, loc);
16811 add_stmt (stmt);
16812 return stmt;
16815 /* OpenMP 4.5:
16816 # pragma omp target exit data target-data-clause[optseq] new-line */
16818 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16825 static tree
16826 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16827 enum pragma_context context)
16829 bool data_seen = false;
16830 if (c_parser_next_token_is (parser, CPP_NAME))
16832 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16833 if (strcmp (p, "data") == 0)
16835 c_parser_consume_token (parser);
16836 data_seen = true;
16839 if (!data_seen)
16841 c_parser_error (parser, "expected %<data%>");
16842 c_parser_skip_to_pragma_eol (parser);
16843 return NULL_TREE;
16846 if (context == pragma_stmt)
16848 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16849 "omp target exit data");
16850 c_parser_skip_to_pragma_eol (parser, false);
16851 return NULL_TREE;
16854 tree clauses
16855 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16856 "#pragma omp target exit data");
16858 int map_seen = 0;
16859 for (tree *pc = &clauses; *pc;)
16861 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16862 switch (OMP_CLAUSE_MAP_KIND (*pc))
16864 case GOMP_MAP_FROM:
16865 case GOMP_MAP_ALWAYS_FROM:
16866 case GOMP_MAP_RELEASE:
16867 case GOMP_MAP_DELETE:
16868 map_seen = 3;
16869 break;
16870 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16871 case GOMP_MAP_ALWAYS_POINTER:
16872 break;
16873 default:
16874 map_seen |= 1;
16875 error_at (OMP_CLAUSE_LOCATION (*pc),
16876 "%<#pragma omp target exit data%> with map-type other "
16877 "than %<from%>, %<release%> or %<delete%> on %<map%>"
16878 " clause");
16879 *pc = OMP_CLAUSE_CHAIN (*pc);
16880 continue;
16882 pc = &OMP_CLAUSE_CHAIN (*pc);
16885 if (map_seen != 3)
16887 if (map_seen == 0)
16888 error_at (loc,
16889 "%<#pragma omp target exit data%> must contain at least one "
16890 "%<map%> clause");
16891 return NULL_TREE;
16894 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16895 TREE_TYPE (stmt) = void_type_node;
16896 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16897 SET_EXPR_LOCATION (stmt, loc);
16898 add_stmt (stmt);
16899 return stmt;
16902 /* OpenMP 4.0:
16903 # pragma omp target target-clause[optseq] new-line
16904 structured-block */
16906 #define OMP_TARGET_CLAUSE_MASK \
16907 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16917 static bool
16918 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16920 location_t loc = c_parser_peek_token (parser)->location;
16921 c_parser_consume_pragma (parser);
16922 tree *pc = NULL, stmt, block;
16924 if (context != pragma_stmt && context != pragma_compound)
16926 c_parser_error (parser, "expected declaration specifiers");
16927 c_parser_skip_to_pragma_eol (parser);
16928 return false;
16931 if (c_parser_next_token_is (parser, CPP_NAME))
16933 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16934 enum tree_code ccode = ERROR_MARK;
16936 if (strcmp (p, "teams") == 0)
16937 ccode = OMP_TEAMS;
16938 else if (strcmp (p, "parallel") == 0)
16939 ccode = OMP_PARALLEL;
16940 else if (strcmp (p, "simd") == 0)
16941 ccode = OMP_SIMD;
16942 if (ccode != ERROR_MARK)
16944 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16945 char p_name[sizeof ("#pragma omp target teams distribute "
16946 "parallel for simd")];
16948 c_parser_consume_token (parser);
16949 strcpy (p_name, "#pragma omp target");
16950 if (!flag_openmp) /* flag_openmp_simd */
16952 tree stmt;
16953 switch (ccode)
16955 case OMP_TEAMS:
16956 stmt = c_parser_omp_teams (loc, parser, p_name,
16957 OMP_TARGET_CLAUSE_MASK,
16958 cclauses, if_p);
16959 break;
16960 case OMP_PARALLEL:
16961 stmt = c_parser_omp_parallel (loc, parser, p_name,
16962 OMP_TARGET_CLAUSE_MASK,
16963 cclauses, if_p);
16964 break;
16965 case OMP_SIMD:
16966 stmt = c_parser_omp_simd (loc, parser, p_name,
16967 OMP_TARGET_CLAUSE_MASK,
16968 cclauses, if_p);
16969 break;
16970 default:
16971 gcc_unreachable ();
16973 return stmt != NULL_TREE;
16975 keep_next_level ();
16976 tree block = c_begin_compound_stmt (true), ret;
16977 switch (ccode)
16979 case OMP_TEAMS:
16980 ret = c_parser_omp_teams (loc, parser, p_name,
16981 OMP_TARGET_CLAUSE_MASK, cclauses,
16982 if_p);
16983 break;
16984 case OMP_PARALLEL:
16985 ret = c_parser_omp_parallel (loc, parser, p_name,
16986 OMP_TARGET_CLAUSE_MASK, cclauses,
16987 if_p);
16988 break;
16989 case OMP_SIMD:
16990 ret = c_parser_omp_simd (loc, parser, p_name,
16991 OMP_TARGET_CLAUSE_MASK, cclauses,
16992 if_p);
16993 break;
16994 default:
16995 gcc_unreachable ();
16997 block = c_end_compound_stmt (loc, block, true);
16998 if (ret == NULL_TREE)
16999 return false;
17000 if (ccode == OMP_TEAMS)
17002 /* For combined target teams, ensure the num_teams and
17003 thread_limit clause expressions are evaluated on the host,
17004 before entering the target construct. */
17005 tree c;
17006 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17007 c; c = OMP_CLAUSE_CHAIN (c))
17008 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17009 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17010 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17012 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17013 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17014 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17015 expr, NULL_TREE, NULL_TREE);
17016 add_stmt (expr);
17017 OMP_CLAUSE_OPERAND (c, 0) = expr;
17018 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17019 OMP_CLAUSE_FIRSTPRIVATE);
17020 OMP_CLAUSE_DECL (tc) = tmp;
17021 OMP_CLAUSE_CHAIN (tc)
17022 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17023 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17026 tree stmt = make_node (OMP_TARGET);
17027 TREE_TYPE (stmt) = void_type_node;
17028 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17029 OMP_TARGET_BODY (stmt) = block;
17030 OMP_TARGET_COMBINED (stmt) = 1;
17031 add_stmt (stmt);
17032 pc = &OMP_TARGET_CLAUSES (stmt);
17033 goto check_clauses;
17035 else if (!flag_openmp) /* flag_openmp_simd */
17037 c_parser_skip_to_pragma_eol (parser, false);
17038 return false;
17040 else if (strcmp (p, "data") == 0)
17042 c_parser_consume_token (parser);
17043 c_parser_omp_target_data (loc, parser, if_p);
17044 return true;
17046 else if (strcmp (p, "enter") == 0)
17048 c_parser_consume_token (parser);
17049 c_parser_omp_target_enter_data (loc, parser, context);
17050 return false;
17052 else if (strcmp (p, "exit") == 0)
17054 c_parser_consume_token (parser);
17055 c_parser_omp_target_exit_data (loc, parser, context);
17056 return false;
17058 else if (strcmp (p, "update") == 0)
17060 c_parser_consume_token (parser);
17061 return c_parser_omp_target_update (loc, parser, context);
17064 if (!flag_openmp) /* flag_openmp_simd */
17066 c_parser_skip_to_pragma_eol (parser, false);
17067 return false;
17070 stmt = make_node (OMP_TARGET);
17071 TREE_TYPE (stmt) = void_type_node;
17073 OMP_TARGET_CLAUSES (stmt)
17074 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17075 "#pragma omp target");
17076 pc = &OMP_TARGET_CLAUSES (stmt);
17077 keep_next_level ();
17078 block = c_begin_compound_stmt (true);
17079 add_stmt (c_parser_omp_structured_block (parser, if_p));
17080 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17082 SET_EXPR_LOCATION (stmt, loc);
17083 add_stmt (stmt);
17085 check_clauses:
17086 while (*pc)
17088 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17089 switch (OMP_CLAUSE_MAP_KIND (*pc))
17091 case GOMP_MAP_TO:
17092 case GOMP_MAP_ALWAYS_TO:
17093 case GOMP_MAP_FROM:
17094 case GOMP_MAP_ALWAYS_FROM:
17095 case GOMP_MAP_TOFROM:
17096 case GOMP_MAP_ALWAYS_TOFROM:
17097 case GOMP_MAP_ALLOC:
17098 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17099 case GOMP_MAP_ALWAYS_POINTER:
17100 break;
17101 default:
17102 error_at (OMP_CLAUSE_LOCATION (*pc),
17103 "%<#pragma omp target%> with map-type other "
17104 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17105 "on %<map%> clause");
17106 *pc = OMP_CLAUSE_CHAIN (*pc);
17107 continue;
17109 pc = &OMP_CLAUSE_CHAIN (*pc);
17111 return true;
17114 /* OpenMP 4.0:
17115 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17117 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17118 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17125 static void
17126 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17128 auto_vec<c_token> clauses;
17129 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17131 c_token *token = c_parser_peek_token (parser);
17132 if (token->type == CPP_EOF)
17134 c_parser_skip_to_pragma_eol (parser);
17135 return;
17137 clauses.safe_push (*token);
17138 c_parser_consume_token (parser);
17140 clauses.safe_push (*c_parser_peek_token (parser));
17141 c_parser_skip_to_pragma_eol (parser);
17143 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17145 if (c_parser_peek_token (parser)->pragma_kind
17146 != PRAGMA_OMP_DECLARE
17147 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17148 || strcmp (IDENTIFIER_POINTER
17149 (c_parser_peek_2nd_token (parser)->value),
17150 "simd") != 0)
17152 c_parser_error (parser,
17153 "%<#pragma omp declare simd%> must be followed by "
17154 "function declaration or definition or another "
17155 "%<#pragma omp declare simd%>");
17156 return;
17158 c_parser_consume_pragma (parser);
17159 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17161 c_token *token = c_parser_peek_token (parser);
17162 if (token->type == CPP_EOF)
17164 c_parser_skip_to_pragma_eol (parser);
17165 return;
17167 clauses.safe_push (*token);
17168 c_parser_consume_token (parser);
17170 clauses.safe_push (*c_parser_peek_token (parser));
17171 c_parser_skip_to_pragma_eol (parser);
17174 /* Make sure nothing tries to read past the end of the tokens. */
17175 c_token eof_token;
17176 memset (&eof_token, 0, sizeof (eof_token));
17177 eof_token.type = CPP_EOF;
17178 clauses.safe_push (eof_token);
17179 clauses.safe_push (eof_token);
17181 switch (context)
17183 case pragma_external:
17184 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17185 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17187 int ext = disable_extension_diagnostics ();
17189 c_parser_consume_token (parser);
17190 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17191 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17192 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17193 NULL, clauses);
17194 restore_extension_diagnostics (ext);
17196 else
17197 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17198 NULL, clauses);
17199 break;
17200 case pragma_struct:
17201 case pragma_param:
17202 case pragma_stmt:
17203 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17204 "function declaration or definition");
17205 break;
17206 case pragma_compound:
17207 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17208 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17210 int ext = disable_extension_diagnostics ();
17212 c_parser_consume_token (parser);
17213 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17214 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17215 if (c_parser_next_tokens_start_declaration (parser))
17217 c_parser_declaration_or_fndef (parser, true, true, true, true,
17218 true, NULL, clauses);
17219 restore_extension_diagnostics (ext);
17220 break;
17222 restore_extension_diagnostics (ext);
17224 else if (c_parser_next_tokens_start_declaration (parser))
17226 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17227 NULL, clauses);
17228 break;
17230 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17231 "function declaration or definition");
17232 break;
17233 default:
17234 gcc_unreachable ();
17238 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17239 and put that into "omp declare simd" attribute. */
17241 static void
17242 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17243 vec<c_token> clauses)
17245 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17246 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17247 has already processed the tokens. */
17248 if (clauses.exists () && clauses[0].type == CPP_EOF)
17249 return;
17250 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17252 error ("%<#pragma omp declare simd%> not immediately followed by "
17253 "a function declaration or definition");
17254 clauses[0].type = CPP_EOF;
17255 return;
17257 if (clauses.exists () && clauses[0].type != CPP_NAME)
17259 error_at (DECL_SOURCE_LOCATION (fndecl),
17260 "%<#pragma omp declare simd%> not immediately followed by "
17261 "a single function declaration or definition");
17262 clauses[0].type = CPP_EOF;
17263 return;
17266 if (parms == NULL_TREE)
17267 parms = DECL_ARGUMENTS (fndecl);
17269 unsigned int tokens_avail = parser->tokens_avail;
17270 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17273 parser->tokens = clauses.address ();
17274 parser->tokens_avail = clauses.length ();
17276 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17277 while (parser->tokens_avail > 3)
17279 c_token *token = c_parser_peek_token (parser);
17280 gcc_assert (token->type == CPP_NAME
17281 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17282 c_parser_consume_token (parser);
17283 parser->in_pragma = true;
17285 tree c = NULL_TREE;
17286 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17287 "#pragma omp declare simd");
17288 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17289 if (c != NULL_TREE)
17290 c = tree_cons (NULL_TREE, c, NULL_TREE);
17291 c = build_tree_list (get_identifier ("omp declare simd"), c);
17292 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17293 DECL_ATTRIBUTES (fndecl) = c;
17296 parser->tokens = &parser->tokens_buf[0];
17297 parser->tokens_avail = tokens_avail;
17298 if (clauses.exists ())
17299 clauses[0].type = CPP_PRAGMA;
17303 /* OpenMP 4.0:
17304 # pragma omp declare target new-line
17305 declarations and definitions
17306 # pragma omp end declare target new-line
17308 OpenMP 4.5:
17309 # pragma omp declare target ( extended-list ) new-line
17311 # pragma omp declare target declare-target-clauses[seq] new-line */
17313 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17317 static void
17318 c_parser_omp_declare_target (c_parser *parser)
17320 location_t loc = c_parser_peek_token (parser)->location;
17321 tree clauses = NULL_TREE;
17322 if (c_parser_next_token_is (parser, CPP_NAME))
17323 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17324 "#pragma omp declare target");
17325 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17327 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17328 clauses);
17329 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17330 c_parser_skip_to_pragma_eol (parser);
17332 else
17334 c_parser_skip_to_pragma_eol (parser);
17335 current_omp_declare_target_attribute++;
17336 return;
17338 if (current_omp_declare_target_attribute)
17339 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17340 "%<#pragma omp declare target%> without clauses and "
17341 "%<#pragma omp end declare target%>");
17342 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17344 tree t = OMP_CLAUSE_DECL (c), id;
17345 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17346 tree at2 = lookup_attribute ("omp declare target link",
17347 DECL_ATTRIBUTES (t));
17348 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17350 id = get_identifier ("omp declare target link");
17351 std::swap (at1, at2);
17353 else
17354 id = get_identifier ("omp declare target");
17355 if (at2)
17357 error_at (OMP_CLAUSE_LOCATION (c),
17358 "%qD specified both in declare target %<link%> and %<to%>"
17359 " clauses", t);
17360 continue;
17362 if (!at1)
17364 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17365 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17366 continue;
17368 symtab_node *node = symtab_node::get (t);
17369 if (node != NULL)
17371 node->offloadable = 1;
17372 if (ENABLE_OFFLOADING)
17374 g->have_offload = true;
17375 if (is_a <varpool_node *> (node))
17376 vec_safe_push (offload_vars, t);
17383 static void
17384 c_parser_omp_end_declare_target (c_parser *parser)
17386 location_t loc = c_parser_peek_token (parser)->location;
17387 c_parser_consume_pragma (parser);
17388 if (c_parser_next_token_is (parser, CPP_NAME)
17389 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17390 "declare") == 0)
17392 c_parser_consume_token (parser);
17393 if (c_parser_next_token_is (parser, CPP_NAME)
17394 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17395 "target") == 0)
17396 c_parser_consume_token (parser);
17397 else
17399 c_parser_error (parser, "expected %<target%>");
17400 c_parser_skip_to_pragma_eol (parser);
17401 return;
17404 else
17406 c_parser_error (parser, "expected %<declare%>");
17407 c_parser_skip_to_pragma_eol (parser);
17408 return;
17410 c_parser_skip_to_pragma_eol (parser);
17411 if (!current_omp_declare_target_attribute)
17412 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17413 "%<#pragma omp declare target%>");
17414 else
17415 current_omp_declare_target_attribute--;
17419 /* OpenMP 4.0
17420 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17421 initializer-clause[opt] new-line
17423 initializer-clause:
17424 initializer (omp_priv = initializer)
17425 initializer (function-name (argument-list)) */
17427 static void
17428 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17430 unsigned int tokens_avail = 0, i;
17431 vec<tree> types = vNULL;
17432 vec<c_token> clauses = vNULL;
17433 enum tree_code reduc_code = ERROR_MARK;
17434 tree reduc_id = NULL_TREE;
17435 tree type;
17436 location_t rloc = c_parser_peek_token (parser)->location;
17438 if (context == pragma_struct || context == pragma_param)
17440 error ("%<#pragma omp declare reduction%> not at file or block scope");
17441 goto fail;
17444 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17445 goto fail;
17447 switch (c_parser_peek_token (parser)->type)
17449 case CPP_PLUS:
17450 reduc_code = PLUS_EXPR;
17451 break;
17452 case CPP_MULT:
17453 reduc_code = MULT_EXPR;
17454 break;
17455 case CPP_MINUS:
17456 reduc_code = MINUS_EXPR;
17457 break;
17458 case CPP_AND:
17459 reduc_code = BIT_AND_EXPR;
17460 break;
17461 case CPP_XOR:
17462 reduc_code = BIT_XOR_EXPR;
17463 break;
17464 case CPP_OR:
17465 reduc_code = BIT_IOR_EXPR;
17466 break;
17467 case CPP_AND_AND:
17468 reduc_code = TRUTH_ANDIF_EXPR;
17469 break;
17470 case CPP_OR_OR:
17471 reduc_code = TRUTH_ORIF_EXPR;
17472 break;
17473 case CPP_NAME:
17474 const char *p;
17475 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17476 if (strcmp (p, "min") == 0)
17478 reduc_code = MIN_EXPR;
17479 break;
17481 if (strcmp (p, "max") == 0)
17483 reduc_code = MAX_EXPR;
17484 break;
17486 reduc_id = c_parser_peek_token (parser)->value;
17487 break;
17488 default:
17489 c_parser_error (parser,
17490 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17491 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17492 goto fail;
17495 tree orig_reduc_id, reduc_decl;
17496 orig_reduc_id = reduc_id;
17497 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17498 reduc_decl = c_omp_reduction_decl (reduc_id);
17499 c_parser_consume_token (parser);
17501 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17502 goto fail;
17504 while (true)
17506 location_t loc = c_parser_peek_token (parser)->location;
17507 struct c_type_name *ctype = c_parser_type_name (parser);
17508 if (ctype != NULL)
17510 type = groktypename (ctype, NULL, NULL);
17511 if (type == error_mark_node)
17513 else if ((INTEGRAL_TYPE_P (type)
17514 || TREE_CODE (type) == REAL_TYPE
17515 || TREE_CODE (type) == COMPLEX_TYPE)
17516 && orig_reduc_id == NULL_TREE)
17517 error_at (loc, "predeclared arithmetic type in "
17518 "%<#pragma omp declare reduction%>");
17519 else if (TREE_CODE (type) == FUNCTION_TYPE
17520 || TREE_CODE (type) == ARRAY_TYPE)
17521 error_at (loc, "function or array type in "
17522 "%<#pragma omp declare reduction%>");
17523 else if (TYPE_ATOMIC (type))
17524 error_at (loc, "%<_Atomic%> qualified type in "
17525 "%<#pragma omp declare reduction%>");
17526 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17527 error_at (loc, "const, volatile or restrict qualified type in "
17528 "%<#pragma omp declare reduction%>");
17529 else
17531 tree t;
17532 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17533 if (comptypes (TREE_PURPOSE (t), type))
17535 error_at (loc, "redeclaration of %qs "
17536 "%<#pragma omp declare reduction%> for "
17537 "type %qT",
17538 IDENTIFIER_POINTER (reduc_id)
17539 + sizeof ("omp declare reduction ") - 1,
17540 type);
17541 location_t ploc
17542 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17543 0));
17544 error_at (ploc, "previous %<#pragma omp declare "
17545 "reduction%>");
17546 break;
17548 if (t == NULL_TREE)
17549 types.safe_push (type);
17551 if (c_parser_next_token_is (parser, CPP_COMMA))
17552 c_parser_consume_token (parser);
17553 else
17554 break;
17556 else
17557 break;
17560 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17561 || types.is_empty ())
17563 fail:
17564 clauses.release ();
17565 types.release ();
17566 while (true)
17568 c_token *token = c_parser_peek_token (parser);
17569 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17570 break;
17571 c_parser_consume_token (parser);
17573 c_parser_skip_to_pragma_eol (parser);
17574 return;
17577 if (types.length () > 1)
17579 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17581 c_token *token = c_parser_peek_token (parser);
17582 if (token->type == CPP_EOF)
17583 goto fail;
17584 clauses.safe_push (*token);
17585 c_parser_consume_token (parser);
17587 clauses.safe_push (*c_parser_peek_token (parser));
17588 c_parser_skip_to_pragma_eol (parser);
17590 /* Make sure nothing tries to read past the end of the tokens. */
17591 c_token eof_token;
17592 memset (&eof_token, 0, sizeof (eof_token));
17593 eof_token.type = CPP_EOF;
17594 clauses.safe_push (eof_token);
17595 clauses.safe_push (eof_token);
17598 int errs = errorcount;
17599 FOR_EACH_VEC_ELT (types, i, type)
17601 tokens_avail = parser->tokens_avail;
17602 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17603 if (!clauses.is_empty ())
17605 parser->tokens = clauses.address ();
17606 parser->tokens_avail = clauses.length ();
17607 parser->in_pragma = true;
17610 bool nested = current_function_decl != NULL_TREE;
17611 if (nested)
17612 c_push_function_context ();
17613 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17614 reduc_id, default_function_type);
17615 current_function_decl = fndecl;
17616 allocate_struct_function (fndecl, true);
17617 push_scope ();
17618 tree stmt = push_stmt_list ();
17619 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17620 warn about these. */
17621 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17622 get_identifier ("omp_out"), type);
17623 DECL_ARTIFICIAL (omp_out) = 1;
17624 DECL_CONTEXT (omp_out) = fndecl;
17625 pushdecl (omp_out);
17626 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17627 get_identifier ("omp_in"), type);
17628 DECL_ARTIFICIAL (omp_in) = 1;
17629 DECL_CONTEXT (omp_in) = fndecl;
17630 pushdecl (omp_in);
17631 struct c_expr combiner = c_parser_expression (parser);
17632 struct c_expr initializer;
17633 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17634 bool bad = false;
17635 initializer.value = error_mark_node;
17636 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17637 bad = true;
17638 else if (c_parser_next_token_is (parser, CPP_NAME)
17639 && strcmp (IDENTIFIER_POINTER
17640 (c_parser_peek_token (parser)->value),
17641 "initializer") == 0)
17643 c_parser_consume_token (parser);
17644 pop_scope ();
17645 push_scope ();
17646 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17647 get_identifier ("omp_priv"), type);
17648 DECL_ARTIFICIAL (omp_priv) = 1;
17649 DECL_INITIAL (omp_priv) = error_mark_node;
17650 DECL_CONTEXT (omp_priv) = fndecl;
17651 pushdecl (omp_priv);
17652 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17653 get_identifier ("omp_orig"), type);
17654 DECL_ARTIFICIAL (omp_orig) = 1;
17655 DECL_CONTEXT (omp_orig) = fndecl;
17656 pushdecl (omp_orig);
17657 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17658 bad = true;
17659 else if (!c_parser_next_token_is (parser, CPP_NAME))
17661 c_parser_error (parser, "expected %<omp_priv%> or "
17662 "function-name");
17663 bad = true;
17665 else if (strcmp (IDENTIFIER_POINTER
17666 (c_parser_peek_token (parser)->value),
17667 "omp_priv") != 0)
17669 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17670 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17672 c_parser_error (parser, "expected function-name %<(%>");
17673 bad = true;
17675 else
17676 initializer = c_parser_postfix_expression (parser);
17677 if (initializer.value
17678 && TREE_CODE (initializer.value) == CALL_EXPR)
17680 int j;
17681 tree c = initializer.value;
17682 for (j = 0; j < call_expr_nargs (c); j++)
17684 tree a = CALL_EXPR_ARG (c, j);
17685 STRIP_NOPS (a);
17686 if (TREE_CODE (a) == ADDR_EXPR
17687 && TREE_OPERAND (a, 0) == omp_priv)
17688 break;
17690 if (j == call_expr_nargs (c))
17691 error ("one of the initializer call arguments should be "
17692 "%<&omp_priv%>");
17695 else
17697 c_parser_consume_token (parser);
17698 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17699 bad = true;
17700 else
17702 tree st = push_stmt_list ();
17703 location_t loc = c_parser_peek_token (parser)->location;
17704 rich_location richloc (line_table, loc);
17705 start_init (omp_priv, NULL_TREE, 0, &richloc);
17706 struct c_expr init = c_parser_initializer (parser);
17707 finish_init ();
17708 finish_decl (omp_priv, loc, init.value,
17709 init.original_type, NULL_TREE);
17710 pop_stmt_list (st);
17713 if (!bad
17714 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17715 bad = true;
17718 if (!bad)
17720 c_parser_skip_to_pragma_eol (parser);
17722 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17723 DECL_INITIAL (reduc_decl));
17724 DECL_INITIAL (reduc_decl) = t;
17725 DECL_SOURCE_LOCATION (omp_out) = rloc;
17726 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17727 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17728 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17729 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17730 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17731 if (omp_priv)
17733 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17734 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17735 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17736 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17737 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17738 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17739 walk_tree (&DECL_INITIAL (omp_priv),
17740 c_check_omp_declare_reduction_r,
17741 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17745 pop_stmt_list (stmt);
17746 pop_scope ();
17747 if (cfun->language != NULL)
17749 ggc_free (cfun->language);
17750 cfun->language = NULL;
17752 set_cfun (NULL);
17753 current_function_decl = NULL_TREE;
17754 if (nested)
17755 c_pop_function_context ();
17757 if (!clauses.is_empty ())
17759 parser->tokens = &parser->tokens_buf[0];
17760 parser->tokens_avail = tokens_avail;
17762 if (bad)
17763 goto fail;
17764 if (errs != errorcount)
17765 break;
17768 clauses.release ();
17769 types.release ();
17773 /* OpenMP 4.0
17774 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17775 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17776 initializer-clause[opt] new-line
17777 #pragma omp declare target new-line */
17779 static void
17780 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17782 c_parser_consume_pragma (parser);
17783 if (c_parser_next_token_is (parser, CPP_NAME))
17785 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17786 if (strcmp (p, "simd") == 0)
17788 /* c_parser_consume_token (parser); done in
17789 c_parser_omp_declare_simd. */
17790 c_parser_omp_declare_simd (parser, context);
17791 return;
17793 if (strcmp (p, "reduction") == 0)
17795 c_parser_consume_token (parser);
17796 c_parser_omp_declare_reduction (parser, context);
17797 return;
17799 if (!flag_openmp) /* flag_openmp_simd */
17801 c_parser_skip_to_pragma_eol (parser, false);
17802 return;
17804 if (strcmp (p, "target") == 0)
17806 c_parser_consume_token (parser);
17807 c_parser_omp_declare_target (parser);
17808 return;
17812 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17813 "or %<target%>");
17814 c_parser_skip_to_pragma_eol (parser);
17817 /* OpenMP 4.5:
17818 #pragma omp taskloop taskloop-clause[optseq] new-line
17819 for-loop
17821 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17822 for-loop */
17824 #define OMP_TASKLOOP_CLAUSE_MASK \
17825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17840 static tree
17841 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17842 char *p_name, omp_clause_mask mask, tree *cclauses,
17843 bool *if_p)
17845 tree clauses, block, ret;
17847 strcat (p_name, " taskloop");
17848 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17850 if (c_parser_next_token_is (parser, CPP_NAME))
17852 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17854 if (strcmp (p, "simd") == 0)
17856 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17857 if (cclauses == NULL)
17858 cclauses = cclauses_buf;
17859 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17860 c_parser_consume_token (parser);
17861 if (!flag_openmp) /* flag_openmp_simd */
17862 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17863 if_p);
17864 block = c_begin_compound_stmt (true);
17865 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17866 block = c_end_compound_stmt (loc, block, true);
17867 if (ret == NULL)
17868 return ret;
17869 ret = make_node (OMP_TASKLOOP);
17870 TREE_TYPE (ret) = void_type_node;
17871 OMP_FOR_BODY (ret) = block;
17872 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17873 SET_EXPR_LOCATION (ret, loc);
17874 add_stmt (ret);
17875 return ret;
17878 if (!flag_openmp) /* flag_openmp_simd */
17880 c_parser_skip_to_pragma_eol (parser, false);
17881 return NULL_TREE;
17884 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17885 if (cclauses)
17887 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17888 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17891 block = c_begin_compound_stmt (true);
17892 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17893 block = c_end_compound_stmt (loc, block, true);
17894 add_stmt (block);
17896 return ret;
17899 /* Main entry point to parsing most OpenMP pragmas. */
17901 static void
17902 c_parser_omp_construct (c_parser *parser, bool *if_p)
17904 enum pragma_kind p_kind;
17905 location_t loc;
17906 tree stmt;
17907 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17908 omp_clause_mask mask (0);
17910 loc = c_parser_peek_token (parser)->location;
17911 p_kind = c_parser_peek_token (parser)->pragma_kind;
17912 c_parser_consume_pragma (parser);
17914 switch (p_kind)
17916 case PRAGMA_OACC_ATOMIC:
17917 c_parser_omp_atomic (loc, parser);
17918 return;
17919 case PRAGMA_OACC_CACHE:
17920 strcpy (p_name, "#pragma acc");
17921 stmt = c_parser_oacc_cache (loc, parser);
17922 break;
17923 case PRAGMA_OACC_DATA:
17924 stmt = c_parser_oacc_data (loc, parser, if_p);
17925 break;
17926 case PRAGMA_OACC_HOST_DATA:
17927 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17928 break;
17929 case PRAGMA_OACC_KERNELS:
17930 case PRAGMA_OACC_PARALLEL:
17931 strcpy (p_name, "#pragma acc");
17932 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17933 if_p);
17934 break;
17935 case PRAGMA_OACC_LOOP:
17936 strcpy (p_name, "#pragma acc");
17937 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17938 break;
17939 case PRAGMA_OACC_WAIT:
17940 strcpy (p_name, "#pragma wait");
17941 stmt = c_parser_oacc_wait (loc, parser, p_name);
17942 break;
17943 case PRAGMA_OMP_ATOMIC:
17944 c_parser_omp_atomic (loc, parser);
17945 return;
17946 case PRAGMA_OMP_CRITICAL:
17947 stmt = c_parser_omp_critical (loc, parser, if_p);
17948 break;
17949 case PRAGMA_OMP_DISTRIBUTE:
17950 strcpy (p_name, "#pragma omp");
17951 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17952 break;
17953 case PRAGMA_OMP_FOR:
17954 strcpy (p_name, "#pragma omp");
17955 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17956 break;
17957 case PRAGMA_OMP_MASTER:
17958 stmt = c_parser_omp_master (loc, parser, if_p);
17959 break;
17960 case PRAGMA_OMP_PARALLEL:
17961 strcpy (p_name, "#pragma omp");
17962 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17963 break;
17964 case PRAGMA_OMP_SECTIONS:
17965 strcpy (p_name, "#pragma omp");
17966 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17967 break;
17968 case PRAGMA_OMP_SIMD:
17969 strcpy (p_name, "#pragma omp");
17970 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17971 break;
17972 case PRAGMA_OMP_SINGLE:
17973 stmt = c_parser_omp_single (loc, parser, if_p);
17974 break;
17975 case PRAGMA_OMP_TASK:
17976 stmt = c_parser_omp_task (loc, parser, if_p);
17977 break;
17978 case PRAGMA_OMP_TASKGROUP:
17979 stmt = c_parser_omp_taskgroup (parser, if_p);
17980 break;
17981 case PRAGMA_OMP_TASKLOOP:
17982 strcpy (p_name, "#pragma omp");
17983 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17984 break;
17985 case PRAGMA_OMP_TEAMS:
17986 strcpy (p_name, "#pragma omp");
17987 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17988 break;
17989 default:
17990 gcc_unreachable ();
17993 if (stmt)
17994 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17998 /* OpenMP 2.5:
17999 # pragma omp threadprivate (variable-list) */
18001 static void
18002 c_parser_omp_threadprivate (c_parser *parser)
18004 tree vars, t;
18005 location_t loc;
18007 c_parser_consume_pragma (parser);
18008 loc = c_parser_peek_token (parser)->location;
18009 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18011 /* Mark every variable in VARS to be assigned thread local storage. */
18012 for (t = vars; t; t = TREE_CHAIN (t))
18014 tree v = TREE_PURPOSE (t);
18016 /* FIXME diagnostics: Ideally we should keep individual
18017 locations for all the variables in the var list to make the
18018 following errors more precise. Perhaps
18019 c_parser_omp_var_list_parens() should construct a list of
18020 locations to go along with the var list. */
18022 /* If V had already been marked threadprivate, it doesn't matter
18023 whether it had been used prior to this point. */
18024 if (!VAR_P (v))
18025 error_at (loc, "%qD is not a variable", v);
18026 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18027 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18028 else if (! is_global_var (v))
18029 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18030 else if (TREE_TYPE (v) == error_mark_node)
18032 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18033 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18034 else
18036 if (! DECL_THREAD_LOCAL_P (v))
18038 set_decl_tls_model (v, decl_default_tls_model (v));
18039 /* If rtl has been already set for this var, call
18040 make_decl_rtl once again, so that encode_section_info
18041 has a chance to look at the new decl flags. */
18042 if (DECL_RTL_SET_P (v))
18043 make_decl_rtl (v);
18045 C_DECL_THREADPRIVATE_P (v) = 1;
18049 c_parser_skip_to_pragma_eol (parser);
18052 /* Parse a transaction attribute (GCC Extension).
18054 transaction-attribute:
18055 attributes
18056 [ [ any-word ] ]
18058 The transactional memory language description is written for C++,
18059 and uses the C++0x attribute syntax. For compatibility, allow the
18060 bracket style for transactions in C as well. */
18062 static tree
18063 c_parser_transaction_attributes (c_parser *parser)
18065 tree attr_name, attr = NULL;
18067 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18068 return c_parser_attributes (parser);
18070 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18071 return NULL_TREE;
18072 c_parser_consume_token (parser);
18073 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18074 goto error1;
18076 attr_name = c_parser_attribute_any_word (parser);
18077 if (attr_name)
18079 c_parser_consume_token (parser);
18080 attr = build_tree_list (attr_name, NULL_TREE);
18082 else
18083 c_parser_error (parser, "expected identifier");
18085 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18086 error1:
18087 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18088 return attr;
18091 /* Parse a __transaction_atomic or __transaction_relaxed statement
18092 (GCC Extension).
18094 transaction-statement:
18095 __transaction_atomic transaction-attribute[opt] compound-statement
18096 __transaction_relaxed compound-statement
18098 Note that the only valid attribute is: "outer".
18101 static tree
18102 c_parser_transaction (c_parser *parser, enum rid keyword)
18104 unsigned int old_in = parser->in_transaction;
18105 unsigned int this_in = 1, new_in;
18106 location_t loc = c_parser_peek_token (parser)->location;
18107 tree stmt, attrs;
18109 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18110 || keyword == RID_TRANSACTION_RELAXED)
18111 && c_parser_next_token_is_keyword (parser, keyword));
18112 c_parser_consume_token (parser);
18114 if (keyword == RID_TRANSACTION_RELAXED)
18115 this_in |= TM_STMT_ATTR_RELAXED;
18116 else
18118 attrs = c_parser_transaction_attributes (parser);
18119 if (attrs)
18120 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18123 /* Keep track if we're in the lexical scope of an outer transaction. */
18124 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18126 parser->in_transaction = new_in;
18127 stmt = c_parser_compound_statement (parser);
18128 parser->in_transaction = old_in;
18130 if (flag_tm)
18131 stmt = c_finish_transaction (loc, stmt, this_in);
18132 else
18133 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18134 "%<__transaction_atomic%> without transactional memory support enabled"
18135 : "%<__transaction_relaxed %> "
18136 "without transactional memory support enabled"));
18138 return stmt;
18141 /* Parse a __transaction_atomic or __transaction_relaxed expression
18142 (GCC Extension).
18144 transaction-expression:
18145 __transaction_atomic ( expression )
18146 __transaction_relaxed ( expression )
18149 static struct c_expr
18150 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18152 struct c_expr ret;
18153 unsigned int old_in = parser->in_transaction;
18154 unsigned int this_in = 1;
18155 location_t loc = c_parser_peek_token (parser)->location;
18156 tree attrs;
18158 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18159 || keyword == RID_TRANSACTION_RELAXED)
18160 && c_parser_next_token_is_keyword (parser, keyword));
18161 c_parser_consume_token (parser);
18163 if (keyword == RID_TRANSACTION_RELAXED)
18164 this_in |= TM_STMT_ATTR_RELAXED;
18165 else
18167 attrs = c_parser_transaction_attributes (parser);
18168 if (attrs)
18169 this_in |= parse_tm_stmt_attr (attrs, 0);
18172 parser->in_transaction = this_in;
18173 matching_parens parens;
18174 if (parens.require_open (parser))
18176 tree expr = c_parser_expression (parser).value;
18177 ret.original_type = TREE_TYPE (expr);
18178 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18179 if (this_in & TM_STMT_ATTR_RELAXED)
18180 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18181 SET_EXPR_LOCATION (ret.value, loc);
18182 ret.original_code = TRANSACTION_EXPR;
18183 if (!parens.require_close (parser))
18185 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18186 goto error;
18189 else
18191 error:
18192 ret.value = error_mark_node;
18193 ret.original_code = ERROR_MARK;
18194 ret.original_type = NULL;
18196 parser->in_transaction = old_in;
18198 if (!flag_tm)
18199 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18200 "%<__transaction_atomic%> without transactional memory support enabled"
18201 : "%<__transaction_relaxed %> "
18202 "without transactional memory support enabled"));
18204 set_c_expr_source_range (&ret, loc, loc);
18206 return ret;
18209 /* Parse a __transaction_cancel statement (GCC Extension).
18211 transaction-cancel-statement:
18212 __transaction_cancel transaction-attribute[opt] ;
18214 Note that the only valid attribute is "outer".
18217 static tree
18218 c_parser_transaction_cancel (c_parser *parser)
18220 location_t loc = c_parser_peek_token (parser)->location;
18221 tree attrs;
18222 bool is_outer = false;
18224 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18225 c_parser_consume_token (parser);
18227 attrs = c_parser_transaction_attributes (parser);
18228 if (attrs)
18229 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18231 if (!flag_tm)
18233 error_at (loc, "%<__transaction_cancel%> without "
18234 "transactional memory support enabled");
18235 goto ret_error;
18237 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18239 error_at (loc, "%<__transaction_cancel%> within a "
18240 "%<__transaction_relaxed%>");
18241 goto ret_error;
18243 else if (is_outer)
18245 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18246 && !is_tm_may_cancel_outer (current_function_decl))
18248 error_at (loc, "outer %<__transaction_cancel%> not "
18249 "within outer %<__transaction_atomic%>");
18250 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18251 goto ret_error;
18254 else if (parser->in_transaction == 0)
18256 error_at (loc, "%<__transaction_cancel%> not within "
18257 "%<__transaction_atomic%>");
18258 goto ret_error;
18261 return add_stmt (build_tm_abort_call (loc, is_outer));
18263 ret_error:
18264 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18267 /* Parse a single source file. */
18269 void
18270 c_parse_file (void)
18272 /* Use local storage to begin. If the first token is a pragma, parse it.
18273 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18274 which will cause garbage collection. */
18275 c_parser tparser;
18277 memset (&tparser, 0, sizeof tparser);
18278 tparser.tokens = &tparser.tokens_buf[0];
18279 the_parser = &tparser;
18281 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18282 c_parser_pragma_pch_preprocess (&tparser);
18284 the_parser = ggc_alloc<c_parser> ();
18285 *the_parser = tparser;
18286 if (tparser.tokens == &tparser.tokens_buf[0])
18287 the_parser->tokens = &the_parser->tokens_buf[0];
18289 /* Initialize EH, if we've been told to do so. */
18290 if (flag_exceptions)
18291 using_eh_for_cleanups ();
18293 c_parser_translation_unit (the_parser);
18294 the_parser = NULL;
18297 /* Parse the body of a function declaration marked with "__RTL".
18299 The RTL parser works on the level of characters read from a
18300 FILE *, whereas c_parser works at the level of tokens.
18301 Square this circle by consuming all of the tokens up to and
18302 including the closing brace, recording the start/end of the RTL
18303 fragment, and reopening the file and re-reading the relevant
18304 lines within the RTL parser.
18306 This requires the opening and closing braces of the C function
18307 to be on separate lines from the RTL they wrap.
18309 Take ownership of START_WITH_PASS, if non-NULL. */
18311 void
18312 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18314 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18316 free (start_with_pass);
18317 return;
18320 location_t start_loc = c_parser_peek_token (parser)->location;
18322 /* Consume all tokens, up to the closing brace, handling
18323 matching pairs of braces in the rtl dump. */
18324 int num_open_braces = 1;
18325 while (1)
18327 switch (c_parser_peek_token (parser)->type)
18329 case CPP_OPEN_BRACE:
18330 num_open_braces++;
18331 break;
18332 case CPP_CLOSE_BRACE:
18333 if (--num_open_braces == 0)
18334 goto found_closing_brace;
18335 break;
18336 case CPP_EOF:
18337 error_at (start_loc, "no closing brace");
18338 free (start_with_pass);
18339 return;
18340 default:
18341 break;
18343 c_parser_consume_token (parser);
18346 found_closing_brace:
18347 /* At the closing brace; record its location. */
18348 location_t end_loc = c_parser_peek_token (parser)->location;
18350 /* Consume the closing brace. */
18351 c_parser_consume_token (parser);
18353 /* Invoke the RTL parser. */
18354 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18356 free (start_with_pass);
18357 return;
18360 /* If a pass name was provided for START_WITH_PASS, run the backend
18361 accordingly now, on the cfun created above, transferring
18362 ownership of START_WITH_PASS. */
18363 if (start_with_pass)
18364 run_rtl_passes (start_with_pass);
18367 #include "gt-c-c-parser.h"