Daily bump.
[official-gcc.git] / gcc / c / c-parser.c
blob7a926285f3a98fea7444ccd617a8d206e2449c35
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
72 /* We need to walk over decls with incomplete struct/union/enum types
73 after parsing the whole translation unit.
74 In finish_decl(), if the decl is static, has incomplete
75 struct/union/enum type, it is appeneded to incomplete_record_decls.
76 In c_parser_translation_unit(), we iterate over incomplete_record_decls
77 and report error if any of the decls are still incomplete. */
79 vec<tree> incomplete_record_decls;
81 void
82 set_c_expr_source_range (c_expr *expr,
83 location_t start, location_t finish)
85 expr->src_range.m_start = start;
86 expr->src_range.m_finish = finish;
87 if (expr->value)
88 set_source_range (expr->value, start, finish);
91 void
92 set_c_expr_source_range (c_expr *expr,
93 source_range src_range)
95 expr->src_range = src_range;
96 if (expr->value)
97 set_source_range (expr->value, src_range);
101 /* Initialization routine for this file. */
103 void
104 c_parse_init (void)
106 /* The only initialization required is of the reserved word
107 identifiers. */
108 unsigned int i;
109 tree id;
110 int mask = 0;
112 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
113 the c_token structure. */
114 gcc_assert (RID_MAX <= 255);
116 mask |= D_CXXONLY;
117 if (!flag_isoc99)
118 mask |= D_C99;
119 if (flag_no_asm)
121 mask |= D_ASM | D_EXT;
122 if (!flag_isoc99)
123 mask |= D_EXT89;
125 if (!c_dialect_objc ())
126 mask |= D_OBJC | D_CXX_OBJC;
128 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
129 for (i = 0; i < num_c_common_reswords; i++)
131 /* If a keyword is disabled, do not enter it into the table
132 and so create a canonical spelling that isn't a keyword. */
133 if (c_common_reswords[i].disable & mask)
135 if (warn_cxx_compat
136 && (c_common_reswords[i].disable & D_CXXWARN))
138 id = get_identifier (c_common_reswords[i].word);
139 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
140 C_IS_RESERVED_WORD (id) = 1;
142 continue;
145 id = get_identifier (c_common_reswords[i].word);
146 C_SET_RID_CODE (id, c_common_reswords[i].rid);
147 C_IS_RESERVED_WORD (id) = 1;
148 ridpointers [(int) c_common_reswords[i].rid] = id;
151 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 /* We always create the symbols but they aren't always supported. */
154 char name[50];
155 sprintf (name, "__int%d", int_n_data[i].bitsize);
156 id = get_identifier (name);
157 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
158 C_IS_RESERVED_WORD (id) = 1;
162 /* A parser structure recording information about the state and
163 context of parsing. Includes lexer information with up to two
164 tokens of look-ahead; more are not needed for C. */
165 struct GTY(()) c_parser {
166 /* The look-ahead tokens. */
167 c_token * GTY((skip)) tokens;
168 /* Buffer for look-ahead tokens. */
169 c_token tokens_buf[4];
170 /* How many look-ahead tokens are available (0 - 4, or
171 more if parsing from pre-lexed tokens). */
172 unsigned int tokens_avail;
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
185 /* Objective-C specific parser/lexer information. */
187 /* True if we are in a context where the Objective-C "PQ" keywords
188 are considered keywords. */
189 BOOL_BITFIELD objc_pq_context : 1;
190 /* True if we are parsing a (potential) Objective-C foreach
191 statement. This is set to true after we parsed 'for (' and while
192 we wait for 'in' or ';' to decide if it's a standard C for loop or an
193 Objective-C foreach loop. */
194 BOOL_BITFIELD objc_could_be_foreach_context : 1;
195 /* The following flag is needed to contextualize Objective-C lexical
196 analysis. In some cases (e.g., 'int NSObject;'), it is
197 undesirable to bind an identifier to an Objective-C class, even
198 if a class with that name exists. */
199 BOOL_BITFIELD objc_need_raw_identifier : 1;
200 /* Nonzero if we're processing a __transaction statement. The value
201 is 1 | TM_STMT_ATTR_*. */
202 unsigned int in_transaction : 4;
203 /* True if we are in a context where the Objective-C "Property attribute"
204 keywords are valid. */
205 BOOL_BITFIELD objc_property_attr_context : 1;
207 /* Location of the last consumed token. */
208 location_t last_token_location;
211 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
213 c_token *
214 c_parser_tokens_buf (c_parser *parser, unsigned n)
216 return &parser->tokens_buf[n];
219 /* Return the error state of PARSER. */
221 bool
222 c_parser_error (c_parser *parser)
224 return parser->error;
227 /* Set the error state of PARSER to ERR. */
229 void
230 c_parser_set_error (c_parser *parser, bool err)
232 parser->error = err;
236 /* The actual parser and external interface. ??? Does this need to be
237 garbage-collected? */
239 static GTY (()) c_parser *the_parser;
241 /* Read in and lex a single token, storing it in *TOKEN. */
243 static void
244 c_lex_one_token (c_parser *parser, c_token *token)
246 timevar_push (TV_LEX);
248 token->type = c_lex_with_flags (&token->value, &token->location,
249 &token->flags,
250 (parser->lex_untranslated_string
251 ? C_LEX_STRING_NO_TRANSLATE : 0));
252 token->id_kind = C_ID_NONE;
253 token->keyword = RID_MAX;
254 token->pragma_kind = PRAGMA_NONE;
256 switch (token->type)
258 case CPP_NAME:
260 tree decl;
262 bool objc_force_identifier = parser->objc_need_raw_identifier;
263 if (c_dialect_objc ())
264 parser->objc_need_raw_identifier = false;
266 if (C_IS_RESERVED_WORD (token->value))
268 enum rid rid_code = C_RID_CODE (token->value);
270 if (rid_code == RID_CXX_COMPAT_WARN)
272 warning_at (token->location,
273 OPT_Wc___compat,
274 "identifier %qE conflicts with C++ keyword",
275 token->value);
277 else if (rid_code >= RID_FIRST_ADDR_SPACE
278 && rid_code <= RID_LAST_ADDR_SPACE)
280 addr_space_t as;
281 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
282 targetm.addr_space.diagnose_usage (as, token->location);
283 token->id_kind = C_ID_ADDRSPACE;
284 token->keyword = rid_code;
285 break;
287 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
289 /* We found an Objective-C "pq" keyword (in, out,
290 inout, bycopy, byref, oneway). They need special
291 care because the interpretation depends on the
292 context. */
293 if (parser->objc_pq_context)
295 token->type = CPP_KEYWORD;
296 token->keyword = rid_code;
297 break;
299 else if (parser->objc_could_be_foreach_context
300 && rid_code == RID_IN)
302 /* We are in Objective-C, inside a (potential)
303 foreach context (which means after having
304 parsed 'for (', but before having parsed ';'),
305 and we found 'in'. We consider it the keyword
306 which terminates the declaration at the
307 beginning of a foreach-statement. Note that
308 this means you can't use 'in' for anything else
309 in that context; in particular, in Objective-C
310 you can't use 'in' as the name of the running
311 variable in a C for loop. We could potentially
312 try to add code here to disambiguate, but it
313 seems a reasonable limitation. */
314 token->type = CPP_KEYWORD;
315 token->keyword = rid_code;
316 break;
318 /* Else, "pq" keywords outside of the "pq" context are
319 not keywords, and we fall through to the code for
320 normal tokens. */
322 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
324 /* We found an Objective-C "property attribute"
325 keyword (getter, setter, readonly, etc). These are
326 only valid in the property context. */
327 if (parser->objc_property_attr_context)
329 token->type = CPP_KEYWORD;
330 token->keyword = rid_code;
331 break;
333 /* Else they are not special keywords.
336 else if (c_dialect_objc ()
337 && (OBJC_IS_AT_KEYWORD (rid_code)
338 || OBJC_IS_CXX_KEYWORD (rid_code)))
340 /* We found one of the Objective-C "@" keywords (defs,
341 selector, synchronized, etc) or one of the
342 Objective-C "cxx" keywords (class, private,
343 protected, public, try, catch, throw) without a
344 preceding '@' sign. Do nothing and fall through to
345 the code for normal tokens (in C++ we would still
346 consider the CXX ones keywords, but not in C). */
349 else
351 token->type = CPP_KEYWORD;
352 token->keyword = rid_code;
353 break;
357 decl = lookup_name (token->value);
358 if (decl)
360 if (TREE_CODE (decl) == TYPE_DECL)
362 token->id_kind = C_ID_TYPENAME;
363 break;
366 else if (c_dialect_objc ())
368 tree objc_interface_decl = objc_is_class_name (token->value);
369 /* Objective-C class names are in the same namespace as
370 variables and typedefs, and hence are shadowed by local
371 declarations. */
372 if (objc_interface_decl
373 && (!objc_force_identifier || global_bindings_p ()))
375 token->value = objc_interface_decl;
376 token->id_kind = C_ID_CLASSNAME;
377 break;
380 token->id_kind = C_ID_ID;
382 break;
383 case CPP_AT_NAME:
384 /* This only happens in Objective-C; it must be a keyword. */
385 token->type = CPP_KEYWORD;
386 switch (C_RID_CODE (token->value))
388 /* Replace 'class' with '@class', 'private' with '@private',
389 etc. This prevents confusion with the C++ keyword
390 'class', and makes the tokens consistent with other
391 Objective-C 'AT' keywords. For example '@class' is
392 reported as RID_AT_CLASS which is consistent with
393 '@synchronized', which is reported as
394 RID_AT_SYNCHRONIZED.
396 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
397 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
398 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
399 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
400 case RID_THROW: token->keyword = RID_AT_THROW; break;
401 case RID_TRY: token->keyword = RID_AT_TRY; break;
402 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
403 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
404 default: token->keyword = C_RID_CODE (token->value);
406 break;
407 case CPP_COLON:
408 case CPP_COMMA:
409 case CPP_CLOSE_PAREN:
410 case CPP_SEMICOLON:
411 /* These tokens may affect the interpretation of any identifiers
412 following, if doing Objective-C. */
413 if (c_dialect_objc ())
414 parser->objc_need_raw_identifier = false;
415 break;
416 case CPP_PRAGMA:
417 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
418 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
419 token->value = NULL;
420 break;
421 default:
422 break;
424 timevar_pop (TV_LEX);
427 /* Return a pointer to the next token from PARSER, reading it in if
428 necessary. */
430 c_token *
431 c_parser_peek_token (c_parser *parser)
433 if (parser->tokens_avail == 0)
435 c_lex_one_token (parser, &parser->tokens[0]);
436 parser->tokens_avail = 1;
438 return &parser->tokens[0];
441 /* Return a pointer to the next-but-one token from PARSER, reading it
442 in if necessary. The next token is already read in. */
444 c_token *
445 c_parser_peek_2nd_token (c_parser *parser)
447 if (parser->tokens_avail >= 2)
448 return &parser->tokens[1];
449 gcc_assert (parser->tokens_avail == 1);
450 gcc_assert (parser->tokens[0].type != CPP_EOF);
451 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
452 c_lex_one_token (parser, &parser->tokens[1]);
453 parser->tokens_avail = 2;
454 return &parser->tokens[1];
457 /* Return a pointer to the Nth token from PARSER, reading it
458 in if necessary. The N-1th token is already read in. */
460 c_token *
461 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
463 /* N is 1-based, not zero-based. */
464 gcc_assert (n > 0);
466 if (parser->tokens_avail >= n)
467 return &parser->tokens[n - 1];
468 gcc_assert (parser->tokens_avail == n - 1);
469 c_lex_one_token (parser, &parser->tokens[n - 1]);
470 parser->tokens_avail = n;
471 return &parser->tokens[n - 1];
474 bool
475 c_keyword_starts_typename (enum rid keyword)
477 switch (keyword)
479 case RID_UNSIGNED:
480 case RID_LONG:
481 case RID_SHORT:
482 case RID_SIGNED:
483 case RID_COMPLEX:
484 case RID_INT:
485 case RID_CHAR:
486 case RID_FLOAT:
487 case RID_DOUBLE:
488 case RID_VOID:
489 case RID_DFLOAT32:
490 case RID_DFLOAT64:
491 case RID_DFLOAT128:
492 CASE_RID_FLOATN_NX:
493 case RID_BOOL:
494 case RID_ENUM:
495 case RID_STRUCT:
496 case RID_UNION:
497 case RID_TYPEOF:
498 case RID_CONST:
499 case RID_ATOMIC:
500 case RID_VOLATILE:
501 case RID_RESTRICT:
502 case RID_ATTRIBUTE:
503 case RID_FRACT:
504 case RID_ACCUM:
505 case RID_SAT:
506 case RID_AUTO_TYPE:
507 case RID_ALIGNAS:
508 return true;
509 default:
510 if (keyword >= RID_FIRST_INT_N
511 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
512 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
513 return true;
514 return false;
518 /* Return true if TOKEN can start a type name,
519 false otherwise. */
520 bool
521 c_token_starts_typename (c_token *token)
523 switch (token->type)
525 case CPP_NAME:
526 switch (token->id_kind)
528 case C_ID_ID:
529 return false;
530 case C_ID_ADDRSPACE:
531 return true;
532 case C_ID_TYPENAME:
533 return true;
534 case C_ID_CLASSNAME:
535 gcc_assert (c_dialect_objc ());
536 return true;
537 default:
538 gcc_unreachable ();
540 case CPP_KEYWORD:
541 return c_keyword_starts_typename (token->keyword);
542 case CPP_LESS:
543 if (c_dialect_objc ())
544 return true;
545 return false;
546 default:
547 return false;
551 /* Return true if the next token from PARSER can start a type name,
552 false otherwise. LA specifies how to do lookahead in order to
553 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
555 static inline bool
556 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
558 c_token *token = c_parser_peek_token (parser);
559 if (c_token_starts_typename (token))
560 return true;
562 /* Try a bit harder to detect an unknown typename. */
563 if (la != cla_prefer_id
564 && token->type == CPP_NAME
565 && token->id_kind == C_ID_ID
567 /* Do not try too hard when we could have "object in array". */
568 && !parser->objc_could_be_foreach_context
570 && (la == cla_prefer_type
571 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
572 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
574 /* Only unknown identifiers. */
575 && !lookup_name (token->value))
576 return true;
578 return false;
581 /* Return true if TOKEN is a type qualifier, false otherwise. */
582 static bool
583 c_token_is_qualifier (c_token *token)
585 switch (token->type)
587 case CPP_NAME:
588 switch (token->id_kind)
590 case C_ID_ADDRSPACE:
591 return true;
592 default:
593 return false;
595 case CPP_KEYWORD:
596 switch (token->keyword)
598 case RID_CONST:
599 case RID_VOLATILE:
600 case RID_RESTRICT:
601 case RID_ATTRIBUTE:
602 case RID_ATOMIC:
603 return true;
604 default:
605 return false;
607 case CPP_LESS:
608 return false;
609 default:
610 gcc_unreachable ();
614 /* Return true if the next token from PARSER is a type qualifier,
615 false otherwise. */
616 static inline bool
617 c_parser_next_token_is_qualifier (c_parser *parser)
619 c_token *token = c_parser_peek_token (parser);
620 return c_token_is_qualifier (token);
623 /* Return true if TOKEN can start declaration specifiers, false
624 otherwise. */
625 static bool
626 c_token_starts_declspecs (c_token *token)
628 switch (token->type)
630 case CPP_NAME:
631 switch (token->id_kind)
633 case C_ID_ID:
634 return false;
635 case C_ID_ADDRSPACE:
636 return true;
637 case C_ID_TYPENAME:
638 return true;
639 case C_ID_CLASSNAME:
640 gcc_assert (c_dialect_objc ());
641 return true;
642 default:
643 gcc_unreachable ();
645 case CPP_KEYWORD:
646 switch (token->keyword)
648 case RID_STATIC:
649 case RID_EXTERN:
650 case RID_REGISTER:
651 case RID_TYPEDEF:
652 case RID_INLINE:
653 case RID_NORETURN:
654 case RID_AUTO:
655 case RID_THREAD:
656 case RID_UNSIGNED:
657 case RID_LONG:
658 case RID_SHORT:
659 case RID_SIGNED:
660 case RID_COMPLEX:
661 case RID_INT:
662 case RID_CHAR:
663 case RID_FLOAT:
664 case RID_DOUBLE:
665 case RID_VOID:
666 case RID_DFLOAT32:
667 case RID_DFLOAT64:
668 case RID_DFLOAT128:
669 CASE_RID_FLOATN_NX:
670 case RID_BOOL:
671 case RID_ENUM:
672 case RID_STRUCT:
673 case RID_UNION:
674 case RID_TYPEOF:
675 case RID_CONST:
676 case RID_VOLATILE:
677 case RID_RESTRICT:
678 case RID_ATTRIBUTE:
679 case RID_FRACT:
680 case RID_ACCUM:
681 case RID_SAT:
682 case RID_ALIGNAS:
683 case RID_ATOMIC:
684 case RID_AUTO_TYPE:
685 return true;
686 default:
687 if (token->keyword >= RID_FIRST_INT_N
688 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
689 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
690 return true;
691 return false;
693 case CPP_LESS:
694 if (c_dialect_objc ())
695 return true;
696 return false;
697 default:
698 return false;
703 /* Return true if TOKEN can start declaration specifiers or a static
704 assertion, false otherwise. */
705 static bool
706 c_token_starts_declaration (c_token *token)
708 if (c_token_starts_declspecs (token)
709 || token->keyword == RID_STATIC_ASSERT)
710 return true;
711 else
712 return false;
715 /* Return true if the next token from PARSER can start declaration
716 specifiers, false otherwise. */
717 bool
718 c_parser_next_token_starts_declspecs (c_parser *parser)
720 c_token *token = c_parser_peek_token (parser);
722 /* In Objective-C, a classname normally starts a declspecs unless it
723 is immediately followed by a dot. In that case, it is the
724 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
725 setter/getter on the class. c_token_starts_declspecs() can't
726 differentiate between the two cases because it only checks the
727 current token, so we have a special check here. */
728 if (c_dialect_objc ()
729 && token->type == CPP_NAME
730 && token->id_kind == C_ID_CLASSNAME
731 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
732 return false;
734 return c_token_starts_declspecs (token);
737 /* Return true if the next tokens from PARSER can start declaration
738 specifiers or a static assertion, false otherwise. */
739 bool
740 c_parser_next_tokens_start_declaration (c_parser *parser)
742 c_token *token = c_parser_peek_token (parser);
744 /* Same as above. */
745 if (c_dialect_objc ()
746 && token->type == CPP_NAME
747 && token->id_kind == C_ID_CLASSNAME
748 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
749 return false;
751 /* Labels do not start declarations. */
752 if (token->type == CPP_NAME
753 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
754 return false;
756 if (c_token_starts_declaration (token))
757 return true;
759 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
760 return true;
762 return false;
765 /* Consume the next token from PARSER. */
767 void
768 c_parser_consume_token (c_parser *parser)
770 gcc_assert (parser->tokens_avail >= 1);
771 gcc_assert (parser->tokens[0].type != CPP_EOF);
772 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
773 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
774 parser->last_token_location = parser->tokens[0].location;
775 if (parser->tokens != &parser->tokens_buf[0])
776 parser->tokens++;
777 else if (parser->tokens_avail == 2)
778 parser->tokens[0] = parser->tokens[1];
779 parser->tokens_avail--;
782 /* Expect the current token to be a #pragma. Consume it and remember
783 that we've begun parsing a pragma. */
785 static void
786 c_parser_consume_pragma (c_parser *parser)
788 gcc_assert (!parser->in_pragma);
789 gcc_assert (parser->tokens_avail >= 1);
790 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
791 if (parser->tokens != &parser->tokens_buf[0])
792 parser->tokens++;
793 else if (parser->tokens_avail == 2)
794 parser->tokens[0] = parser->tokens[1];
795 parser->tokens_avail--;
796 parser->in_pragma = true;
799 /* Update the global input_location from TOKEN. */
800 static inline void
801 c_parser_set_source_position_from_token (c_token *token)
803 if (token->type != CPP_EOF)
805 input_location = token->location;
809 /* Helper function for c_parser_error.
810 Having peeked a token of kind TOK1_KIND that might signify
811 a conflict marker, peek successor tokens to determine
812 if we actually do have a conflict marker.
813 Specifically, we consider a run of 7 '<', '=' or '>' characters
814 at the start of a line as a conflict marker.
815 These come through the lexer as three pairs and a single,
816 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
817 If it returns true, *OUT_LOC is written to with the location/range
818 of the marker. */
820 static bool
821 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
822 location_t *out_loc)
824 c_token *token2 = c_parser_peek_2nd_token (parser);
825 if (token2->type != tok1_kind)
826 return false;
827 c_token *token3 = c_parser_peek_nth_token (parser, 3);
828 if (token3->type != tok1_kind)
829 return false;
830 c_token *token4 = c_parser_peek_nth_token (parser, 4);
831 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
832 return false;
834 /* It must be at the start of the line. */
835 location_t start_loc = c_parser_peek_token (parser)->location;
836 if (LOCATION_COLUMN (start_loc) != 1)
837 return false;
839 /* We have a conflict marker. Construct a location of the form:
840 <<<<<<<
841 ^~~~~~~
842 with start == caret, finishing at the end of the marker. */
843 location_t finish_loc = get_finish (token4->location);
844 *out_loc = make_location (start_loc, start_loc, finish_loc);
846 return true;
849 /* Issue a diagnostic of the form
850 FILE:LINE: MESSAGE before TOKEN
851 where TOKEN is the next token in the input stream of PARSER.
852 MESSAGE (specified by the caller) is usually of the form "expected
853 OTHER-TOKEN".
855 Use RICHLOC as the location of the diagnostic.
857 Do not issue a diagnostic if still recovering from an error.
859 Return true iff an error was actually emitted.
861 ??? This is taken from the C++ parser, but building up messages in
862 this way is not i18n-friendly and some other approach should be
863 used. */
865 static bool
866 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
867 rich_location *richloc)
869 c_token *token = c_parser_peek_token (parser);
870 if (parser->error)
871 return false;
872 parser->error = true;
873 if (!gmsgid)
874 return false;
876 /* If this is actually a conflict marker, report it as such. */
877 if (token->type == CPP_LSHIFT
878 || token->type == CPP_RSHIFT
879 || token->type == CPP_EQ_EQ)
881 location_t loc;
882 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
884 error_at (loc, "version control conflict marker in file");
885 return true;
889 c_parse_error (gmsgid,
890 /* Because c_parse_error does not understand
891 CPP_KEYWORD, keywords are treated like
892 identifiers. */
893 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
894 /* ??? The C parser does not save the cpp flags of a
895 token, we need to pass 0 here and we will not get
896 the source spelling of some tokens but rather the
897 canonical spelling. */
898 token->value, /*flags=*/0, richloc);
899 return true;
902 /* As c_parser_error_richloc, but issue the message at the
903 location of PARSER's next token, or at input_location
904 if the next token is EOF. */
906 bool
907 c_parser_error (c_parser *parser, const char *gmsgid)
909 c_token *token = c_parser_peek_token (parser);
910 c_parser_set_source_position_from_token (token);
911 rich_location richloc (line_table, input_location);
912 return c_parser_error_richloc (parser, gmsgid, &richloc);
915 /* Some tokens naturally come in pairs e.g.'(' and ')'.
916 This class is for tracking such a matching pair of symbols.
917 In particular, it tracks the location of the first token,
918 so that if the second token is missing, we can highlight the
919 location of the first token when notifying the user about the
920 problem. */
922 template <typename traits_t>
923 class token_pair
925 public:
926 /* token_pair's ctor. */
927 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
929 /* If the next token is the opening symbol for this pair, consume it and
930 return true.
931 Otherwise, issue an error and return false.
932 In either case, record the location of the opening token. */
934 bool require_open (c_parser *parser)
936 c_token *token = c_parser_peek_token (parser);
937 if (token)
938 m_open_loc = token->location;
940 return c_parser_require (parser, traits_t::open_token_type,
941 traits_t::open_gmsgid);
944 /* Consume the next token from PARSER, recording its location as
945 that of the opening token within the pair. */
947 void consume_open (c_parser *parser)
949 c_token *token = c_parser_peek_token (parser);
950 gcc_assert (token->type == traits_t::open_token_type);
951 m_open_loc = token->location;
952 c_parser_consume_token (parser);
955 /* If the next token is the closing symbol for this pair, consume it
956 and return true.
957 Otherwise, issue an error, highlighting the location of the
958 corresponding opening token, and return false. */
960 bool require_close (c_parser *parser) const
962 return c_parser_require (parser, traits_t::close_token_type,
963 traits_t::close_gmsgid, m_open_loc);
966 /* Like token_pair::require_close, except that tokens will be skipped
967 until the desired token is found. An error message is still produced
968 if the next token is not as expected. */
970 void skip_until_found_close (c_parser *parser) const
972 c_parser_skip_until_found (parser, traits_t::close_token_type,
973 traits_t::close_gmsgid, m_open_loc);
976 private:
977 location_t m_open_loc;
980 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
982 struct matching_paren_traits
984 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
985 static const char * const open_gmsgid;
986 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
987 static const char * const close_gmsgid;
990 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
991 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
993 /* "matching_parens" is a token_pair<T> class for tracking matching
994 pairs of parentheses. */
996 typedef token_pair<matching_paren_traits> matching_parens;
998 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1000 struct matching_brace_traits
1002 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1003 static const char * const open_gmsgid;
1004 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1005 static const char * const close_gmsgid;
1008 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1009 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1011 /* "matching_braces" is a token_pair<T> class for tracking matching
1012 pairs of braces. */
1014 typedef token_pair<matching_brace_traits> matching_braces;
1016 /* Get a description of the matching symbol to TYPE e.g. "(" for
1017 CPP_CLOSE_PAREN. */
1019 static const char *
1020 get_matching_symbol (enum cpp_ttype type)
1022 switch (type)
1024 default:
1025 gcc_unreachable ();
1026 return "";
1027 case CPP_CLOSE_PAREN:
1028 return "(";
1029 case CPP_CLOSE_BRACE:
1030 return "{";
1034 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1035 issue the error MSGID. If MSGID is NULL then a message has already
1036 been produced and no message will be produced this time. Returns
1037 true if found, false otherwise.
1039 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1040 within any error as the location of an "opening" token matching
1041 the close token TYPE (e.g. the location of the '(' when TYPE is
1042 CPP_CLOSE_PAREN).
1044 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1045 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1046 attempt to generate a fix-it hint for the problem.
1047 Otherwise msgid describes multiple token types (e.g.
1048 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1049 generate a fix-it hint. */
1051 bool
1052 c_parser_require (c_parser *parser,
1053 enum cpp_ttype type,
1054 const char *msgid,
1055 location_t matching_location,
1056 bool type_is_unique)
1058 if (c_parser_next_token_is (parser, type))
1060 c_parser_consume_token (parser);
1061 return true;
1063 else
1065 location_t next_token_loc = c_parser_peek_token (parser)->location;
1066 gcc_rich_location richloc (next_token_loc);
1068 /* Potentially supply a fix-it hint, suggesting to add the
1069 missing token immediately after the *previous* token.
1070 This may move the primary location within richloc. */
1071 if (!parser->error && type_is_unique)
1072 maybe_suggest_missing_token_insertion (&richloc, type,
1073 parser->last_token_location);
1075 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1076 Attempt to consolidate diagnostics by printing it as a
1077 secondary range within the main diagnostic. */
1078 bool added_matching_location = false;
1079 if (matching_location != UNKNOWN_LOCATION)
1080 added_matching_location
1081 = richloc.add_location_if_nearby (matching_location);
1083 if (c_parser_error_richloc (parser, msgid, &richloc))
1084 /* If we weren't able to consolidate matching_location, then
1085 print it as a secondary diagnostic. */
1086 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1087 inform (matching_location, "to match this %qs",
1088 get_matching_symbol (type));
1090 return false;
1094 /* If the next token is the indicated keyword, consume it. Otherwise,
1095 issue the error MSGID. Returns true if found, false otherwise. */
1097 static bool
1098 c_parser_require_keyword (c_parser *parser,
1099 enum rid keyword,
1100 const char *msgid)
1102 if (c_parser_next_token_is_keyword (parser, keyword))
1104 c_parser_consume_token (parser);
1105 return true;
1107 else
1109 c_parser_error (parser, msgid);
1110 return false;
1114 /* Like c_parser_require, except that tokens will be skipped until the
1115 desired token is found. An error message is still produced if the
1116 next token is not as expected. If MSGID is NULL then a message has
1117 already been produced and no message will be produced this
1118 time.
1120 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1121 within any error as the location of an "opening" token matching
1122 the close token TYPE (e.g. the location of the '(' when TYPE is
1123 CPP_CLOSE_PAREN). */
1125 void
1126 c_parser_skip_until_found (c_parser *parser,
1127 enum cpp_ttype type,
1128 const char *msgid,
1129 location_t matching_location)
1131 unsigned nesting_depth = 0;
1133 if (c_parser_require (parser, type, msgid, matching_location))
1134 return;
1136 /* Skip tokens until the desired token is found. */
1137 while (true)
1139 /* Peek at the next token. */
1140 c_token *token = c_parser_peek_token (parser);
1141 /* If we've reached the token we want, consume it and stop. */
1142 if (token->type == type && !nesting_depth)
1144 c_parser_consume_token (parser);
1145 break;
1148 /* If we've run out of tokens, stop. */
1149 if (token->type == CPP_EOF)
1150 return;
1151 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1152 return;
1153 if (token->type == CPP_OPEN_BRACE
1154 || token->type == CPP_OPEN_PAREN
1155 || token->type == CPP_OPEN_SQUARE)
1156 ++nesting_depth;
1157 else if (token->type == CPP_CLOSE_BRACE
1158 || token->type == CPP_CLOSE_PAREN
1159 || token->type == CPP_CLOSE_SQUARE)
1161 if (nesting_depth-- == 0)
1162 break;
1164 /* Consume this token. */
1165 c_parser_consume_token (parser);
1167 parser->error = false;
1170 /* Skip tokens until the end of a parameter is found, but do not
1171 consume the comma, semicolon or closing delimiter. */
1173 static void
1174 c_parser_skip_to_end_of_parameter (c_parser *parser)
1176 unsigned nesting_depth = 0;
1178 while (true)
1180 c_token *token = c_parser_peek_token (parser);
1181 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1182 && !nesting_depth)
1183 break;
1184 /* If we've run out of tokens, stop. */
1185 if (token->type == CPP_EOF)
1186 return;
1187 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1188 return;
1189 if (token->type == CPP_OPEN_BRACE
1190 || token->type == CPP_OPEN_PAREN
1191 || token->type == CPP_OPEN_SQUARE)
1192 ++nesting_depth;
1193 else if (token->type == CPP_CLOSE_BRACE
1194 || token->type == CPP_CLOSE_PAREN
1195 || token->type == CPP_CLOSE_SQUARE)
1197 if (nesting_depth-- == 0)
1198 break;
1200 /* Consume this token. */
1201 c_parser_consume_token (parser);
1203 parser->error = false;
1206 /* Expect to be at the end of the pragma directive and consume an
1207 end of line marker. */
1209 static void
1210 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1212 gcc_assert (parser->in_pragma);
1213 parser->in_pragma = false;
1215 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1216 c_parser_error (parser, "expected end of line");
1218 cpp_ttype token_type;
1221 c_token *token = c_parser_peek_token (parser);
1222 token_type = token->type;
1223 if (token_type == CPP_EOF)
1224 break;
1225 c_parser_consume_token (parser);
1227 while (token_type != CPP_PRAGMA_EOL);
1229 parser->error = false;
1232 /* Skip tokens until we have consumed an entire block, or until we
1233 have consumed a non-nested ';'. */
1235 static void
1236 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1238 unsigned nesting_depth = 0;
1239 bool save_error = parser->error;
1241 while (true)
1243 c_token *token;
1245 /* Peek at the next token. */
1246 token = c_parser_peek_token (parser);
1248 switch (token->type)
1250 case CPP_EOF:
1251 return;
1253 case CPP_PRAGMA_EOL:
1254 if (parser->in_pragma)
1255 return;
1256 break;
1258 case CPP_SEMICOLON:
1259 /* If the next token is a ';', we have reached the
1260 end of the statement. */
1261 if (!nesting_depth)
1263 /* Consume the ';'. */
1264 c_parser_consume_token (parser);
1265 goto finished;
1267 break;
1269 case CPP_CLOSE_BRACE:
1270 /* If the next token is a non-nested '}', then we have
1271 reached the end of the current block. */
1272 if (nesting_depth == 0 || --nesting_depth == 0)
1274 c_parser_consume_token (parser);
1275 goto finished;
1277 break;
1279 case CPP_OPEN_BRACE:
1280 /* If it the next token is a '{', then we are entering a new
1281 block. Consume the entire block. */
1282 ++nesting_depth;
1283 break;
1285 case CPP_PRAGMA:
1286 /* If we see a pragma, consume the whole thing at once. We
1287 have some safeguards against consuming pragmas willy-nilly.
1288 Normally, we'd expect to be here with parser->error set,
1289 which disables these safeguards. But it's possible to get
1290 here for secondary error recovery, after parser->error has
1291 been cleared. */
1292 c_parser_consume_pragma (parser);
1293 c_parser_skip_to_pragma_eol (parser);
1294 parser->error = save_error;
1295 continue;
1297 default:
1298 break;
1301 c_parser_consume_token (parser);
1304 finished:
1305 parser->error = false;
1308 /* CPP's options (initialized by c-opts.c). */
1309 extern cpp_options *cpp_opts;
1311 /* Save the warning flags which are controlled by __extension__. */
1313 static inline int
1314 disable_extension_diagnostics (void)
1316 int ret = (pedantic
1317 | (warn_pointer_arith << 1)
1318 | (warn_traditional << 2)
1319 | (flag_iso << 3)
1320 | (warn_long_long << 4)
1321 | (warn_cxx_compat << 5)
1322 | (warn_overlength_strings << 6)
1323 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1324 play tricks to properly restore it. */
1325 | ((warn_c90_c99_compat == 1) << 7)
1326 | ((warn_c90_c99_compat == -1) << 8)
1327 /* Similarly for warn_c99_c11_compat. */
1328 | ((warn_c99_c11_compat == 1) << 9)
1329 | ((warn_c99_c11_compat == -1) << 10)
1331 cpp_opts->cpp_pedantic = pedantic = 0;
1332 warn_pointer_arith = 0;
1333 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1334 flag_iso = 0;
1335 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1336 warn_cxx_compat = 0;
1337 warn_overlength_strings = 0;
1338 warn_c90_c99_compat = 0;
1339 warn_c99_c11_compat = 0;
1340 return ret;
1343 /* Restore the warning flags which are controlled by __extension__.
1344 FLAGS is the return value from disable_extension_diagnostics. */
1346 static inline void
1347 restore_extension_diagnostics (int flags)
1349 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1350 warn_pointer_arith = (flags >> 1) & 1;
1351 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1352 flag_iso = (flags >> 3) & 1;
1353 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1354 warn_cxx_compat = (flags >> 5) & 1;
1355 warn_overlength_strings = (flags >> 6) & 1;
1356 /* See above for why is this needed. */
1357 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1358 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1361 /* Helper data structure for parsing #pragma acc routine. */
1362 struct oacc_routine_data {
1363 bool error_seen; /* Set if error has been reported. */
1364 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1365 tree clauses;
1366 location_t loc;
1369 static void c_parser_external_declaration (c_parser *);
1370 static void c_parser_asm_definition (c_parser *);
1371 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1372 bool, bool, tree *, vec<c_token>,
1373 struct oacc_routine_data * = NULL,
1374 bool * = NULL);
1375 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1376 static void c_parser_static_assert_declaration (c_parser *);
1377 static struct c_typespec c_parser_enum_specifier (c_parser *);
1378 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1379 static tree c_parser_struct_declaration (c_parser *);
1380 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1381 static tree c_parser_alignas_specifier (c_parser *);
1382 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1383 c_dtr_syn, bool *);
1384 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1385 bool,
1386 struct c_declarator *);
1387 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1388 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1389 tree);
1390 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1391 static tree c_parser_simple_asm_expr (c_parser *);
1392 static tree c_parser_attributes (c_parser *);
1393 static struct c_expr c_parser_initializer (c_parser *);
1394 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1395 struct obstack *);
1396 static void c_parser_initelt (c_parser *, struct obstack *);
1397 static void c_parser_initval (c_parser *, struct c_expr *,
1398 struct obstack *);
1399 static tree c_parser_compound_statement (c_parser *);
1400 static void c_parser_compound_statement_nostart (c_parser *);
1401 static void c_parser_label (c_parser *);
1402 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1403 static void c_parser_statement_after_labels (c_parser *, bool *,
1404 vec<tree> * = NULL);
1405 static tree c_parser_c99_block_statement (c_parser *, bool *,
1406 location_t * = NULL);
1407 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1408 static void c_parser_switch_statement (c_parser *, bool *);
1409 static void c_parser_while_statement (c_parser *, bool, unsigned short, bool *);
1410 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1411 static void c_parser_for_statement (c_parser *, bool, unsigned short, bool *);
1412 static tree c_parser_asm_statement (c_parser *);
1413 static tree c_parser_asm_operands (c_parser *);
1414 static tree c_parser_asm_goto_operands (c_parser *);
1415 static tree c_parser_asm_clobbers (c_parser *);
1416 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1417 tree = NULL_TREE);
1418 static struct c_expr c_parser_conditional_expression (c_parser *,
1419 struct c_expr *, tree);
1420 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1421 tree);
1422 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1423 static struct c_expr c_parser_unary_expression (c_parser *);
1424 static struct c_expr c_parser_sizeof_expression (c_parser *);
1425 static struct c_expr c_parser_alignof_expression (c_parser *);
1426 static struct c_expr c_parser_postfix_expression (c_parser *);
1427 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1428 struct c_type_name *,
1429 location_t);
1430 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1431 location_t loc,
1432 struct c_expr);
1433 static tree c_parser_transaction (c_parser *, enum rid);
1434 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1435 static tree c_parser_transaction_cancel (c_parser *);
1436 static struct c_expr c_parser_expression (c_parser *);
1437 static struct c_expr c_parser_expression_conv (c_parser *);
1438 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1439 vec<tree, va_gc> **, location_t *,
1440 tree *, vec<location_t> *,
1441 unsigned int * = NULL);
1442 static void c_parser_oacc_declare (c_parser *);
1443 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1444 static void c_parser_oacc_update (c_parser *);
1445 static void c_parser_omp_construct (c_parser *, bool *);
1446 static void c_parser_omp_threadprivate (c_parser *);
1447 static void c_parser_omp_barrier (c_parser *);
1448 static void c_parser_omp_flush (c_parser *);
1449 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1450 tree, tree *, bool *);
1451 static void c_parser_omp_taskwait (c_parser *);
1452 static void c_parser_omp_taskyield (c_parser *);
1453 static void c_parser_omp_cancel (c_parser *);
1455 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1456 pragma_stmt, pragma_compound };
1457 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1458 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1459 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1460 static void c_parser_omp_end_declare_target (c_parser *);
1461 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1462 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1463 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1465 /* These Objective-C parser functions are only ever called when
1466 compiling Objective-C. */
1467 static void c_parser_objc_class_definition (c_parser *, tree);
1468 static void c_parser_objc_class_instance_variables (c_parser *);
1469 static void c_parser_objc_class_declaration (c_parser *);
1470 static void c_parser_objc_alias_declaration (c_parser *);
1471 static void c_parser_objc_protocol_definition (c_parser *, tree);
1472 static bool c_parser_objc_method_type (c_parser *);
1473 static void c_parser_objc_method_definition (c_parser *);
1474 static void c_parser_objc_methodprotolist (c_parser *);
1475 static void c_parser_objc_methodproto (c_parser *);
1476 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1477 static tree c_parser_objc_type_name (c_parser *);
1478 static tree c_parser_objc_protocol_refs (c_parser *);
1479 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1480 static void c_parser_objc_synchronized_statement (c_parser *);
1481 static tree c_parser_objc_selector (c_parser *);
1482 static tree c_parser_objc_selector_arg (c_parser *);
1483 static tree c_parser_objc_receiver (c_parser *);
1484 static tree c_parser_objc_message_args (c_parser *);
1485 static tree c_parser_objc_keywordexpr (c_parser *);
1486 static void c_parser_objc_at_property_declaration (c_parser *);
1487 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1488 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1489 static bool c_parser_objc_diagnose_bad_element_prefix
1490 (c_parser *, struct c_declspecs *);
1492 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1494 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1496 translation-unit:
1497 external-declarations
1499 external-declarations:
1500 external-declaration
1501 external-declarations external-declaration
1503 GNU extensions:
1505 translation-unit:
1506 empty
1509 static void
1510 c_parser_translation_unit (c_parser *parser)
1512 if (c_parser_next_token_is (parser, CPP_EOF))
1514 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1515 "ISO C forbids an empty translation unit");
1517 else
1519 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1520 mark_valid_location_for_stdc_pragma (false);
1523 ggc_collect ();
1524 c_parser_external_declaration (parser);
1525 obstack_free (&parser_obstack, obstack_position);
1527 while (c_parser_next_token_is_not (parser, CPP_EOF));
1530 unsigned int i;
1531 tree decl;
1532 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1533 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1534 error ("storage size of %q+D isn%'t known", decl);
1537 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1539 external-declaration:
1540 function-definition
1541 declaration
1543 GNU extensions:
1545 external-declaration:
1546 asm-definition
1548 __extension__ external-declaration
1550 Objective-C:
1552 external-declaration:
1553 objc-class-definition
1554 objc-class-declaration
1555 objc-alias-declaration
1556 objc-protocol-definition
1557 objc-method-definition
1558 @end
1561 static void
1562 c_parser_external_declaration (c_parser *parser)
1564 int ext;
1565 switch (c_parser_peek_token (parser)->type)
1567 case CPP_KEYWORD:
1568 switch (c_parser_peek_token (parser)->keyword)
1570 case RID_EXTENSION:
1571 ext = disable_extension_diagnostics ();
1572 c_parser_consume_token (parser);
1573 c_parser_external_declaration (parser);
1574 restore_extension_diagnostics (ext);
1575 break;
1576 case RID_ASM:
1577 c_parser_asm_definition (parser);
1578 break;
1579 case RID_AT_INTERFACE:
1580 case RID_AT_IMPLEMENTATION:
1581 gcc_assert (c_dialect_objc ());
1582 c_parser_objc_class_definition (parser, NULL_TREE);
1583 break;
1584 case RID_AT_CLASS:
1585 gcc_assert (c_dialect_objc ());
1586 c_parser_objc_class_declaration (parser);
1587 break;
1588 case RID_AT_ALIAS:
1589 gcc_assert (c_dialect_objc ());
1590 c_parser_objc_alias_declaration (parser);
1591 break;
1592 case RID_AT_PROTOCOL:
1593 gcc_assert (c_dialect_objc ());
1594 c_parser_objc_protocol_definition (parser, NULL_TREE);
1595 break;
1596 case RID_AT_PROPERTY:
1597 gcc_assert (c_dialect_objc ());
1598 c_parser_objc_at_property_declaration (parser);
1599 break;
1600 case RID_AT_SYNTHESIZE:
1601 gcc_assert (c_dialect_objc ());
1602 c_parser_objc_at_synthesize_declaration (parser);
1603 break;
1604 case RID_AT_DYNAMIC:
1605 gcc_assert (c_dialect_objc ());
1606 c_parser_objc_at_dynamic_declaration (parser);
1607 break;
1608 case RID_AT_END:
1609 gcc_assert (c_dialect_objc ());
1610 c_parser_consume_token (parser);
1611 objc_finish_implementation ();
1612 break;
1613 default:
1614 goto decl_or_fndef;
1616 break;
1617 case CPP_SEMICOLON:
1618 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1619 "ISO C does not allow extra %<;%> outside of a function");
1620 c_parser_consume_token (parser);
1621 break;
1622 case CPP_PRAGMA:
1623 mark_valid_location_for_stdc_pragma (true);
1624 c_parser_pragma (parser, pragma_external, NULL);
1625 mark_valid_location_for_stdc_pragma (false);
1626 break;
1627 case CPP_PLUS:
1628 case CPP_MINUS:
1629 if (c_dialect_objc ())
1631 c_parser_objc_method_definition (parser);
1632 break;
1634 /* Else fall through, and yield a syntax error trying to parse
1635 as a declaration or function definition. */
1636 /* FALLTHRU */
1637 default:
1638 decl_or_fndef:
1639 /* A declaration or a function definition (or, in Objective-C,
1640 an @interface or @protocol with prefix attributes). We can
1641 only tell which after parsing the declaration specifiers, if
1642 any, and the first declarator. */
1643 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1644 NULL, vNULL);
1645 break;
1649 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1650 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1652 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
1654 static void
1655 add_debug_begin_stmt (location_t loc)
1657 /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721. */
1658 if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1659 return;
1661 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1662 SET_EXPR_LOCATION (stmt, loc);
1663 add_stmt (stmt);
1666 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1667 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1668 is accepted; otherwise (old-style parameter declarations) only other
1669 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1670 assertion is accepted; otherwise (old-style parameter declarations)
1671 it is not. If NESTED is true, we are inside a function or parsing
1672 old-style parameter declarations; any functions encountered are
1673 nested functions and declaration specifiers are required; otherwise
1674 we are at top level and functions are normal functions and
1675 declaration specifiers may be optional. If EMPTY_OK is true, empty
1676 declarations are OK (subject to all other constraints); otherwise
1677 (old-style parameter declarations) they are diagnosed. If
1678 START_ATTR_OK is true, the declaration specifiers may start with
1679 attributes; otherwise they may not.
1680 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1681 declaration when parsing an Objective-C foreach statement.
1682 FALLTHRU_ATTR_P is used to signal whether this function parsed
1683 "__attribute__((fallthrough));".
1685 declaration:
1686 declaration-specifiers init-declarator-list[opt] ;
1687 static_assert-declaration
1689 function-definition:
1690 declaration-specifiers[opt] declarator declaration-list[opt]
1691 compound-statement
1693 declaration-list:
1694 declaration
1695 declaration-list declaration
1697 init-declarator-list:
1698 init-declarator
1699 init-declarator-list , init-declarator
1701 init-declarator:
1702 declarator simple-asm-expr[opt] attributes[opt]
1703 declarator simple-asm-expr[opt] attributes[opt] = initializer
1705 GNU extensions:
1707 nested-function-definition:
1708 declaration-specifiers declarator declaration-list[opt]
1709 compound-statement
1711 attribute ;
1713 Objective-C:
1714 attributes objc-class-definition
1715 attributes objc-category-definition
1716 attributes objc-protocol-definition
1718 The simple-asm-expr and attributes are GNU extensions.
1720 This function does not handle __extension__; that is handled in its
1721 callers. ??? Following the old parser, __extension__ may start
1722 external declarations, declarations in functions and declarations
1723 at the start of "for" loops, but not old-style parameter
1724 declarations.
1726 C99 requires declaration specifiers in a function definition; the
1727 absence is diagnosed through the diagnosis of implicit int. In GNU
1728 C we also allow but diagnose declarations without declaration
1729 specifiers, but only at top level (elsewhere they conflict with
1730 other syntax).
1732 In Objective-C, declarations of the looping variable in a foreach
1733 statement are exceptionally terminated by 'in' (for example, 'for
1734 (NSObject *object in array) { ... }').
1736 OpenMP:
1738 declaration:
1739 threadprivate-directive
1741 GIMPLE:
1743 gimple-function-definition:
1744 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1745 declaration-list[opt] compound-statement
1747 rtl-function-definition:
1748 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1749 declaration-list[opt] compound-statement */
1751 static void
1752 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1753 bool static_assert_ok, bool empty_ok,
1754 bool nested, bool start_attr_ok,
1755 tree *objc_foreach_object_declaration,
1756 vec<c_token> omp_declare_simd_clauses,
1757 struct oacc_routine_data *oacc_routine_data,
1758 bool *fallthru_attr_p)
1760 struct c_declspecs *specs;
1761 tree prefix_attrs;
1762 tree all_prefix_attrs;
1763 bool diagnosed_no_specs = false;
1764 location_t here = c_parser_peek_token (parser)->location;
1766 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1768 if (static_assert_ok
1769 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1771 c_parser_static_assert_declaration (parser);
1772 return;
1774 specs = build_null_declspecs ();
1776 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1777 if (c_parser_peek_token (parser)->type == CPP_NAME
1778 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1779 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1780 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1781 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1783 tree name = c_parser_peek_token (parser)->value;
1785 /* Issue a warning about NAME being an unknown type name, perhaps
1786 with some kind of hint.
1787 If the user forgot a "struct" etc, suggest inserting
1788 it. Otherwise, attempt to look for misspellings. */
1789 gcc_rich_location richloc (here);
1790 if (tag_exists_p (RECORD_TYPE, name))
1792 /* This is not C++ with its implicit typedef. */
1793 richloc.add_fixit_insert_before ("struct ");
1794 error_at (&richloc,
1795 "unknown type name %qE;"
1796 " use %<struct%> keyword to refer to the type",
1797 name);
1799 else if (tag_exists_p (UNION_TYPE, name))
1801 richloc.add_fixit_insert_before ("union ");
1802 error_at (&richloc,
1803 "unknown type name %qE;"
1804 " use %<union%> keyword to refer to the type",
1805 name);
1807 else if (tag_exists_p (ENUMERAL_TYPE, name))
1809 richloc.add_fixit_insert_before ("enum ");
1810 error_at (&richloc,
1811 "unknown type name %qE;"
1812 " use %<enum%> keyword to refer to the type",
1813 name);
1815 else
1817 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1818 here);
1819 if (hint)
1821 richloc.add_fixit_replace (hint.suggestion ());
1822 error_at (&richloc,
1823 "unknown type name %qE; did you mean %qs?",
1824 name, hint.suggestion ());
1826 else
1827 error_at (here, "unknown type name %qE", name);
1830 /* Parse declspecs normally to get a correct pointer type, but avoid
1831 a further "fails to be a type name" error. Refuse nested functions
1832 since it is not how the user likely wants us to recover. */
1833 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1834 c_parser_peek_token (parser)->keyword = RID_VOID;
1835 c_parser_peek_token (parser)->value = error_mark_node;
1836 fndef_ok = !nested;
1839 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1840 true, true, cla_nonabstract_decl);
1841 if (parser->error)
1843 c_parser_skip_to_end_of_block_or_statement (parser);
1844 return;
1846 if (nested && !specs->declspecs_seen_p)
1848 c_parser_error (parser, "expected declaration specifiers");
1849 c_parser_skip_to_end_of_block_or_statement (parser);
1850 return;
1853 finish_declspecs (specs);
1854 bool auto_type_p = specs->typespec_word == cts_auto_type;
1855 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1857 if (auto_type_p)
1858 error_at (here, "%<__auto_type%> in empty declaration");
1859 else if (specs->typespec_kind == ctsk_none
1860 && attribute_fallthrough_p (specs->attrs))
1862 if (fallthru_attr_p != NULL)
1863 *fallthru_attr_p = true;
1864 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1865 void_type_node, 0);
1866 add_stmt (fn);
1868 else if (empty_ok)
1869 shadow_tag (specs);
1870 else
1872 shadow_tag_warned (specs, 1);
1873 pedwarn (here, 0, "empty declaration");
1875 c_parser_consume_token (parser);
1876 if (oacc_routine_data)
1877 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1878 return;
1881 /* Provide better error recovery. Note that a type name here is usually
1882 better diagnosed as a redeclaration. */
1883 if (empty_ok
1884 && specs->typespec_kind == ctsk_tagdef
1885 && c_parser_next_token_starts_declspecs (parser)
1886 && !c_parser_next_token_is (parser, CPP_NAME))
1888 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1889 parser->error = false;
1890 shadow_tag_warned (specs, 1);
1891 return;
1893 else if (c_dialect_objc () && !auto_type_p)
1895 /* Prefix attributes are an error on method decls. */
1896 switch (c_parser_peek_token (parser)->type)
1898 case CPP_PLUS:
1899 case CPP_MINUS:
1900 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1901 return;
1902 if (specs->attrs)
1904 warning_at (c_parser_peek_token (parser)->location,
1905 OPT_Wattributes,
1906 "prefix attributes are ignored for methods");
1907 specs->attrs = NULL_TREE;
1909 if (fndef_ok)
1910 c_parser_objc_method_definition (parser);
1911 else
1912 c_parser_objc_methodproto (parser);
1913 return;
1914 break;
1915 default:
1916 break;
1918 /* This is where we parse 'attributes @interface ...',
1919 'attributes @implementation ...', 'attributes @protocol ...'
1920 (where attributes could be, for example, __attribute__
1921 ((deprecated)).
1923 switch (c_parser_peek_token (parser)->keyword)
1925 case RID_AT_INTERFACE:
1927 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1928 return;
1929 c_parser_objc_class_definition (parser, specs->attrs);
1930 return;
1932 break;
1933 case RID_AT_IMPLEMENTATION:
1935 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1936 return;
1937 if (specs->attrs)
1939 warning_at (c_parser_peek_token (parser)->location,
1940 OPT_Wattributes,
1941 "prefix attributes are ignored for implementations");
1942 specs->attrs = NULL_TREE;
1944 c_parser_objc_class_definition (parser, NULL_TREE);
1945 return;
1947 break;
1948 case RID_AT_PROTOCOL:
1950 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1951 return;
1952 c_parser_objc_protocol_definition (parser, specs->attrs);
1953 return;
1955 break;
1956 case RID_AT_ALIAS:
1957 case RID_AT_CLASS:
1958 case RID_AT_END:
1959 case RID_AT_PROPERTY:
1960 if (specs->attrs)
1962 c_parser_error (parser, "unexpected attribute");
1963 specs->attrs = NULL;
1965 break;
1966 default:
1967 break;
1970 else if (attribute_fallthrough_p (specs->attrs))
1971 warning_at (here, OPT_Wattributes,
1972 "%<fallthrough%> attribute not followed by %<;%>");
1974 pending_xref_error ();
1975 prefix_attrs = specs->attrs;
1976 all_prefix_attrs = prefix_attrs;
1977 specs->attrs = NULL_TREE;
1978 while (true)
1980 struct c_declarator *declarator;
1981 bool dummy = false;
1982 timevar_id_t tv;
1983 tree fnbody = NULL_TREE;
1984 /* Declaring either one or more declarators (in which case we
1985 should diagnose if there were no declaration specifiers) or a
1986 function definition (in which case the diagnostic for
1987 implicit int suffices). */
1988 declarator = c_parser_declarator (parser,
1989 specs->typespec_kind != ctsk_none,
1990 C_DTR_NORMAL, &dummy);
1991 if (declarator == NULL)
1993 if (omp_declare_simd_clauses.exists ())
1994 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1995 omp_declare_simd_clauses);
1996 if (oacc_routine_data)
1997 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1998 c_parser_skip_to_end_of_block_or_statement (parser);
1999 return;
2001 if (auto_type_p && declarator->kind != cdk_id)
2003 error_at (here,
2004 "%<__auto_type%> requires a plain identifier"
2005 " as declarator");
2006 c_parser_skip_to_end_of_block_or_statement (parser);
2007 return;
2009 if (c_parser_next_token_is (parser, CPP_EQ)
2010 || c_parser_next_token_is (parser, CPP_COMMA)
2011 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2012 || c_parser_next_token_is_keyword (parser, RID_ASM)
2013 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2014 || c_parser_next_token_is_keyword (parser, RID_IN))
2016 tree asm_name = NULL_TREE;
2017 tree postfix_attrs = NULL_TREE;
2018 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2020 diagnosed_no_specs = true;
2021 pedwarn (here, 0, "data definition has no type or storage class");
2023 /* Having seen a data definition, there cannot now be a
2024 function definition. */
2025 fndef_ok = false;
2026 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2027 asm_name = c_parser_simple_asm_expr (parser);
2028 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2030 postfix_attrs = c_parser_attributes (parser);
2031 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2033 /* This means there is an attribute specifier after
2034 the declarator in a function definition. Provide
2035 some more information for the user. */
2036 error_at (here, "attributes should be specified before the "
2037 "declarator in a function definition");
2038 c_parser_skip_to_end_of_block_or_statement (parser);
2039 return;
2042 if (c_parser_next_token_is (parser, CPP_EQ))
2044 tree d;
2045 struct c_expr init;
2046 location_t init_loc;
2047 c_parser_consume_token (parser);
2048 if (auto_type_p)
2050 init_loc = c_parser_peek_token (parser)->location;
2051 rich_location richloc (line_table, init_loc);
2052 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2053 /* A parameter is initialized, which is invalid. Don't
2054 attempt to instrument the initializer. */
2055 int flag_sanitize_save = flag_sanitize;
2056 if (nested && !empty_ok)
2057 flag_sanitize = 0;
2058 init = c_parser_expr_no_commas (parser, NULL);
2059 flag_sanitize = flag_sanitize_save;
2060 if (TREE_CODE (init.value) == COMPONENT_REF
2061 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2062 error_at (here,
2063 "%<__auto_type%> used with a bit-field"
2064 " initializer");
2065 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2066 tree init_type = TREE_TYPE (init.value);
2067 /* As with typeof, remove all qualifiers from atomic types. */
2068 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2069 init_type
2070 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2071 bool vm_type = variably_modified_type_p (init_type,
2072 NULL_TREE);
2073 if (vm_type)
2074 init.value = save_expr (init.value);
2075 finish_init ();
2076 specs->typespec_kind = ctsk_typeof;
2077 specs->locations[cdw_typedef] = init_loc;
2078 specs->typedef_p = true;
2079 specs->type = init_type;
2080 if (vm_type)
2082 bool maybe_const = true;
2083 tree type_expr = c_fully_fold (init.value, false,
2084 &maybe_const);
2085 specs->expr_const_operands &= maybe_const;
2086 if (specs->expr)
2087 specs->expr = build2 (COMPOUND_EXPR,
2088 TREE_TYPE (type_expr),
2089 specs->expr, type_expr);
2090 else
2091 specs->expr = type_expr;
2093 d = start_decl (declarator, specs, true,
2094 chainon (postfix_attrs, all_prefix_attrs));
2095 if (!d)
2096 d = error_mark_node;
2097 if (omp_declare_simd_clauses.exists ())
2098 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2099 omp_declare_simd_clauses);
2101 else
2103 /* The declaration of the variable is in effect while
2104 its initializer is parsed. */
2105 d = start_decl (declarator, specs, true,
2106 chainon (postfix_attrs, all_prefix_attrs));
2107 if (!d)
2108 d = error_mark_node;
2109 if (omp_declare_simd_clauses.exists ())
2110 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2111 omp_declare_simd_clauses);
2112 init_loc = c_parser_peek_token (parser)->location;
2113 rich_location richloc (line_table, init_loc);
2114 start_init (d, asm_name, global_bindings_p (), &richloc);
2115 /* A parameter is initialized, which is invalid. Don't
2116 attempt to instrument the initializer. */
2117 int flag_sanitize_save = flag_sanitize;
2118 if (TREE_CODE (d) == PARM_DECL)
2119 flag_sanitize = 0;
2120 init = c_parser_initializer (parser);
2121 flag_sanitize = flag_sanitize_save;
2122 finish_init ();
2124 if (oacc_routine_data)
2125 c_finish_oacc_routine (oacc_routine_data, d, false);
2126 if (d != error_mark_node)
2128 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2129 finish_decl (d, init_loc, init.value,
2130 init.original_type, asm_name);
2133 else
2135 if (auto_type_p)
2137 error_at (here,
2138 "%<__auto_type%> requires an initialized "
2139 "data declaration");
2140 c_parser_skip_to_end_of_block_or_statement (parser);
2141 return;
2143 tree d = start_decl (declarator, specs, false,
2144 chainon (postfix_attrs,
2145 all_prefix_attrs));
2146 if (d && TREE_CODE (d) == FUNCTION_DECL)
2147 if (declarator->kind == cdk_function)
2148 if (DECL_ARGUMENTS (d) == NULL_TREE)
2149 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2150 if (omp_declare_simd_clauses.exists ())
2152 tree parms = NULL_TREE;
2153 if (d && TREE_CODE (d) == FUNCTION_DECL)
2155 struct c_declarator *ce = declarator;
2156 while (ce != NULL)
2157 if (ce->kind == cdk_function)
2159 parms = ce->u.arg_info->parms;
2160 break;
2162 else
2163 ce = ce->declarator;
2165 if (parms)
2166 temp_store_parm_decls (d, parms);
2167 c_finish_omp_declare_simd (parser, d, parms,
2168 omp_declare_simd_clauses);
2169 if (parms)
2170 temp_pop_parm_decls ();
2172 if (oacc_routine_data)
2173 c_finish_oacc_routine (oacc_routine_data, d, false);
2174 if (d)
2175 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2176 NULL_TREE, asm_name);
2178 if (c_parser_next_token_is_keyword (parser, RID_IN))
2180 if (d)
2181 *objc_foreach_object_declaration = d;
2182 else
2183 *objc_foreach_object_declaration = error_mark_node;
2186 if (c_parser_next_token_is (parser, CPP_COMMA))
2188 if (auto_type_p)
2190 error_at (here,
2191 "%<__auto_type%> may only be used with"
2192 " a single declarator");
2193 c_parser_skip_to_end_of_block_or_statement (parser);
2194 return;
2196 c_parser_consume_token (parser);
2197 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2198 all_prefix_attrs = chainon (c_parser_attributes (parser),
2199 prefix_attrs);
2200 else
2201 all_prefix_attrs = prefix_attrs;
2202 continue;
2204 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2206 c_parser_consume_token (parser);
2207 return;
2209 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2211 /* This can only happen in Objective-C: we found the
2212 'in' that terminates the declaration inside an
2213 Objective-C foreach statement. Do not consume the
2214 token, so that the caller can use it to determine
2215 that this indeed is a foreach context. */
2216 return;
2218 else
2220 c_parser_error (parser, "expected %<,%> or %<;%>");
2221 c_parser_skip_to_end_of_block_or_statement (parser);
2222 return;
2225 else if (auto_type_p)
2227 error_at (here,
2228 "%<__auto_type%> requires an initialized data declaration");
2229 c_parser_skip_to_end_of_block_or_statement (parser);
2230 return;
2232 else if (!fndef_ok)
2234 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2235 "%<asm%> or %<__attribute__%>");
2236 c_parser_skip_to_end_of_block_or_statement (parser);
2237 return;
2239 /* Function definition (nested or otherwise). */
2240 if (nested)
2242 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2243 c_push_function_context ();
2245 if (!start_function (specs, declarator, all_prefix_attrs))
2247 /* At this point we've consumed:
2248 declaration-specifiers declarator
2249 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2250 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2251 but the
2252 declaration-specifiers declarator
2253 aren't grokkable as a function definition, so we have
2254 an error. */
2255 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2256 if (c_parser_next_token_starts_declspecs (parser))
2258 /* If we have
2259 declaration-specifiers declarator decl-specs
2260 then assume we have a missing semicolon, which would
2261 give us:
2262 declaration-specifiers declarator decl-specs
2265 <~~~~~~~~~ declaration ~~~~~~~~~~>
2266 Use c_parser_require to get an error with a fix-it hint. */
2267 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2268 parser->error = false;
2270 else
2272 /* This can appear in many cases looking nothing like a
2273 function definition, so we don't give a more specific
2274 error suggesting there was one. */
2275 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2276 "or %<__attribute__%>");
2278 if (nested)
2279 c_pop_function_context ();
2280 break;
2283 if (DECL_DECLARED_INLINE_P (current_function_decl))
2284 tv = TV_PARSE_INLINE;
2285 else
2286 tv = TV_PARSE_FUNC;
2287 auto_timevar at (g_timer, tv);
2289 /* Parse old-style parameter declarations. ??? Attributes are
2290 not allowed to start declaration specifiers here because of a
2291 syntax conflict between a function declaration with attribute
2292 suffix and a function definition with an attribute prefix on
2293 first old-style parameter declaration. Following the old
2294 parser, they are not accepted on subsequent old-style
2295 parameter declarations either. However, there is no
2296 ambiguity after the first declaration, nor indeed on the
2297 first as long as we don't allow postfix attributes after a
2298 declarator with a nonempty identifier list in a definition;
2299 and postfix attributes have never been accepted here in
2300 function definitions either. */
2301 while (c_parser_next_token_is_not (parser, CPP_EOF)
2302 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2303 c_parser_declaration_or_fndef (parser, false, false, false,
2304 true, false, NULL, vNULL);
2305 store_parm_decls ();
2306 if (omp_declare_simd_clauses.exists ())
2307 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2308 omp_declare_simd_clauses);
2309 if (oacc_routine_data)
2310 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2311 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2312 = c_parser_peek_token (parser)->location;
2314 /* If the definition was marked with __GIMPLE then parse the
2315 function body as GIMPLE. */
2316 if (specs->gimple_p)
2318 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2319 bool saved = in_late_binary_op;
2320 in_late_binary_op = true;
2321 c_parser_parse_gimple_body (parser);
2322 in_late_binary_op = saved;
2324 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2325 consuming the function body. */
2326 else if (specs->rtl_p)
2328 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2330 /* Normally, store_parm_decls sets next_is_function_body,
2331 anticipating a function body. We need a push_scope/pop_scope
2332 pair to flush out this state, or subsequent function parsing
2333 will go wrong. */
2334 push_scope ();
2335 pop_scope ();
2337 finish_function ();
2338 return;
2340 else
2341 fnbody = c_parser_compound_statement (parser);
2342 tree fndecl = current_function_decl;
2343 if (nested)
2345 tree decl = current_function_decl;
2346 /* Mark nested functions as needing static-chain initially.
2347 lower_nested_functions will recompute it but the
2348 DECL_STATIC_CHAIN flag is also used before that happens,
2349 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2350 DECL_STATIC_CHAIN (decl) = 1;
2351 add_stmt (fnbody);
2352 finish_function ();
2353 c_pop_function_context ();
2354 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2356 else
2358 if (fnbody)
2359 add_stmt (fnbody);
2360 finish_function ();
2362 /* Get rid of the empty stmt list for GIMPLE. */
2363 if (specs->gimple_p)
2364 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2366 break;
2370 /* Parse an asm-definition (asm() outside a function body). This is a
2371 GNU extension.
2373 asm-definition:
2374 simple-asm-expr ;
2377 static void
2378 c_parser_asm_definition (c_parser *parser)
2380 tree asm_str = c_parser_simple_asm_expr (parser);
2381 if (asm_str)
2382 symtab->finalize_toplevel_asm (asm_str);
2383 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2386 /* Parse a static assertion (C11 6.7.10).
2388 static_assert-declaration:
2389 static_assert-declaration-no-semi ;
2392 static void
2393 c_parser_static_assert_declaration (c_parser *parser)
2395 c_parser_static_assert_declaration_no_semi (parser);
2396 if (parser->error
2397 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2398 c_parser_skip_to_end_of_block_or_statement (parser);
2401 /* Parse a static assertion (C11 6.7.10), without the trailing
2402 semicolon.
2404 static_assert-declaration-no-semi:
2405 _Static_assert ( constant-expression , string-literal )
2408 static void
2409 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2411 location_t assert_loc, value_loc;
2412 tree value;
2413 tree string;
2415 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2416 assert_loc = c_parser_peek_token (parser)->location;
2417 if (flag_isoc99)
2418 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2419 "ISO C99 does not support %<_Static_assert%>");
2420 else
2421 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2422 "ISO C90 does not support %<_Static_assert%>");
2423 c_parser_consume_token (parser);
2424 matching_parens parens;
2425 if (!parens.require_open (parser))
2426 return;
2427 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2428 value = c_parser_expr_no_commas (parser, NULL).value;
2429 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2430 parser->lex_untranslated_string = true;
2431 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2433 parser->lex_untranslated_string = false;
2434 return;
2436 switch (c_parser_peek_token (parser)->type)
2438 case CPP_STRING:
2439 case CPP_STRING16:
2440 case CPP_STRING32:
2441 case CPP_WSTRING:
2442 case CPP_UTF8STRING:
2443 string = c_parser_peek_token (parser)->value;
2444 c_parser_consume_token (parser);
2445 parser->lex_untranslated_string = false;
2446 break;
2447 default:
2448 c_parser_error (parser, "expected string literal");
2449 parser->lex_untranslated_string = false;
2450 return;
2452 parens.require_close (parser);
2454 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2456 error_at (value_loc, "expression in static assertion is not an integer");
2457 return;
2459 if (TREE_CODE (value) != INTEGER_CST)
2461 value = c_fully_fold (value, false, NULL);
2462 /* Strip no-op conversions. */
2463 STRIP_TYPE_NOPS (value);
2464 if (TREE_CODE (value) == INTEGER_CST)
2465 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2466 "is not an integer constant expression");
2468 if (TREE_CODE (value) != INTEGER_CST)
2470 error_at (value_loc, "expression in static assertion is not constant");
2471 return;
2473 constant_expression_warning (value);
2474 if (integer_zerop (value))
2475 error_at (assert_loc, "static assertion failed: %E", string);
2478 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2479 6.7, C11 6.7), adding them to SPECS (which may already include some).
2480 Storage class specifiers are accepted iff SCSPEC_OK; type
2481 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2482 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2483 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2485 declaration-specifiers:
2486 storage-class-specifier declaration-specifiers[opt]
2487 type-specifier declaration-specifiers[opt]
2488 type-qualifier declaration-specifiers[opt]
2489 function-specifier declaration-specifiers[opt]
2490 alignment-specifier declaration-specifiers[opt]
2492 Function specifiers (inline) are from C99, and are currently
2493 handled as storage class specifiers, as is __thread. Alignment
2494 specifiers are from C11.
2496 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2497 storage-class-specifier:
2498 typedef
2499 extern
2500 static
2501 auto
2502 register
2503 _Thread_local
2505 (_Thread_local is new in C11.)
2507 C99 6.7.4, C11 6.7.4:
2508 function-specifier:
2509 inline
2510 _Noreturn
2512 (_Noreturn is new in C11.)
2514 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2515 type-specifier:
2516 void
2517 char
2518 short
2520 long
2521 float
2522 double
2523 signed
2524 unsigned
2525 _Bool
2526 _Complex
2527 [_Imaginary removed in C99 TC2]
2528 struct-or-union-specifier
2529 enum-specifier
2530 typedef-name
2531 atomic-type-specifier
2533 (_Bool and _Complex are new in C99.)
2534 (atomic-type-specifier is new in C11.)
2536 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2538 type-qualifier:
2539 const
2540 restrict
2541 volatile
2542 address-space-qualifier
2543 _Atomic
2545 (restrict is new in C99.)
2546 (_Atomic is new in C11.)
2548 GNU extensions:
2550 declaration-specifiers:
2551 attributes declaration-specifiers[opt]
2553 type-qualifier:
2554 address-space
2556 address-space:
2557 identifier recognized by the target
2559 storage-class-specifier:
2560 __thread
2562 type-specifier:
2563 typeof-specifier
2564 __auto_type
2565 __intN
2566 _Decimal32
2567 _Decimal64
2568 _Decimal128
2569 _Fract
2570 _Accum
2571 _Sat
2573 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2574 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2576 atomic-type-specifier
2577 _Atomic ( type-name )
2579 Objective-C:
2581 type-specifier:
2582 class-name objc-protocol-refs[opt]
2583 typedef-name objc-protocol-refs
2584 objc-protocol-refs
2587 void
2588 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2589 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2590 bool alignspec_ok, bool auto_type_ok,
2591 enum c_lookahead_kind la)
2593 bool attrs_ok = start_attr_ok;
2594 bool seen_type = specs->typespec_kind != ctsk_none;
2596 if (!typespec_ok)
2597 gcc_assert (la == cla_prefer_id);
2599 while (c_parser_next_token_is (parser, CPP_NAME)
2600 || c_parser_next_token_is (parser, CPP_KEYWORD)
2601 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2603 struct c_typespec t;
2604 tree attrs;
2605 tree align;
2606 location_t loc = c_parser_peek_token (parser)->location;
2608 /* If we cannot accept a type, exit if the next token must start
2609 one. Also, if we already have seen a tagged definition,
2610 a typename would be an error anyway and likely the user
2611 has simply forgotten a semicolon, so we exit. */
2612 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2613 && c_parser_next_tokens_start_typename (parser, la)
2614 && !c_parser_next_token_is_qualifier (parser)
2615 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2616 break;
2618 if (c_parser_next_token_is (parser, CPP_NAME))
2620 c_token *name_token = c_parser_peek_token (parser);
2621 tree value = name_token->value;
2622 c_id_kind kind = name_token->id_kind;
2624 if (kind == C_ID_ADDRSPACE)
2626 addr_space_t as
2627 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2628 declspecs_add_addrspace (name_token->location, specs, as);
2629 c_parser_consume_token (parser);
2630 attrs_ok = true;
2631 continue;
2634 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2636 /* If we cannot accept a type, and the next token must start one,
2637 exit. Do the same if we already have seen a tagged definition,
2638 since it would be an error anyway and likely the user has simply
2639 forgotten a semicolon. */
2640 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2641 break;
2643 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2644 a C_ID_CLASSNAME. */
2645 c_parser_consume_token (parser);
2646 seen_type = true;
2647 attrs_ok = true;
2648 if (kind == C_ID_ID)
2650 error_at (loc, "unknown type name %qE", value);
2651 t.kind = ctsk_typedef;
2652 t.spec = error_mark_node;
2654 else if (kind == C_ID_TYPENAME
2655 && (!c_dialect_objc ()
2656 || c_parser_next_token_is_not (parser, CPP_LESS)))
2658 t.kind = ctsk_typedef;
2659 /* For a typedef name, record the meaning, not the name.
2660 In case of 'foo foo, bar;'. */
2661 t.spec = lookup_name (value);
2663 else
2665 tree proto = NULL_TREE;
2666 gcc_assert (c_dialect_objc ());
2667 t.kind = ctsk_objc;
2668 if (c_parser_next_token_is (parser, CPP_LESS))
2669 proto = c_parser_objc_protocol_refs (parser);
2670 t.spec = objc_get_protocol_qualified_type (value, proto);
2672 t.expr = NULL_TREE;
2673 t.expr_const_operands = true;
2674 declspecs_add_type (name_token->location, specs, t);
2675 continue;
2677 if (c_parser_next_token_is (parser, CPP_LESS))
2679 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2680 nisse@lysator.liu.se. */
2681 tree proto;
2682 gcc_assert (c_dialect_objc ());
2683 if (!typespec_ok || seen_type)
2684 break;
2685 proto = c_parser_objc_protocol_refs (parser);
2686 t.kind = ctsk_objc;
2687 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2688 t.expr = NULL_TREE;
2689 t.expr_const_operands = true;
2690 declspecs_add_type (loc, specs, t);
2691 continue;
2693 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2694 switch (c_parser_peek_token (parser)->keyword)
2696 case RID_STATIC:
2697 case RID_EXTERN:
2698 case RID_REGISTER:
2699 case RID_TYPEDEF:
2700 case RID_INLINE:
2701 case RID_NORETURN:
2702 case RID_AUTO:
2703 case RID_THREAD:
2704 if (!scspec_ok)
2705 goto out;
2706 attrs_ok = true;
2707 /* TODO: Distinguish between function specifiers (inline, noreturn)
2708 and storage class specifiers, either here or in
2709 declspecs_add_scspec. */
2710 declspecs_add_scspec (loc, specs,
2711 c_parser_peek_token (parser)->value);
2712 c_parser_consume_token (parser);
2713 break;
2714 case RID_AUTO_TYPE:
2715 if (!auto_type_ok)
2716 goto out;
2717 /* Fall through. */
2718 case RID_UNSIGNED:
2719 case RID_LONG:
2720 case RID_SHORT:
2721 case RID_SIGNED:
2722 case RID_COMPLEX:
2723 case RID_INT:
2724 case RID_CHAR:
2725 case RID_FLOAT:
2726 case RID_DOUBLE:
2727 case RID_VOID:
2728 case RID_DFLOAT32:
2729 case RID_DFLOAT64:
2730 case RID_DFLOAT128:
2731 CASE_RID_FLOATN_NX:
2732 case RID_BOOL:
2733 case RID_FRACT:
2734 case RID_ACCUM:
2735 case RID_SAT:
2736 case RID_INT_N_0:
2737 case RID_INT_N_1:
2738 case RID_INT_N_2:
2739 case RID_INT_N_3:
2740 if (!typespec_ok)
2741 goto out;
2742 attrs_ok = true;
2743 seen_type = true;
2744 if (c_dialect_objc ())
2745 parser->objc_need_raw_identifier = true;
2746 t.kind = ctsk_resword;
2747 t.spec = c_parser_peek_token (parser)->value;
2748 t.expr = NULL_TREE;
2749 t.expr_const_operands = true;
2750 declspecs_add_type (loc, specs, t);
2751 c_parser_consume_token (parser);
2752 break;
2753 case RID_ENUM:
2754 if (!typespec_ok)
2755 goto out;
2756 attrs_ok = true;
2757 seen_type = true;
2758 t = c_parser_enum_specifier (parser);
2759 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2760 declspecs_add_type (loc, specs, t);
2761 break;
2762 case RID_STRUCT:
2763 case RID_UNION:
2764 if (!typespec_ok)
2765 goto out;
2766 attrs_ok = true;
2767 seen_type = true;
2768 t = c_parser_struct_or_union_specifier (parser);
2769 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2770 declspecs_add_type (loc, specs, t);
2771 break;
2772 case RID_TYPEOF:
2773 /* ??? The old parser rejected typeof after other type
2774 specifiers, but is a syntax error the best way of
2775 handling this? */
2776 if (!typespec_ok || seen_type)
2777 goto out;
2778 attrs_ok = true;
2779 seen_type = true;
2780 t = c_parser_typeof_specifier (parser);
2781 declspecs_add_type (loc, specs, t);
2782 break;
2783 case RID_ATOMIC:
2784 /* C parser handling of Objective-C constructs needs
2785 checking for correct lvalue-to-rvalue conversions, and
2786 the code in build_modify_expr handling various
2787 Objective-C cases, and that in build_unary_op handling
2788 Objective-C cases for increment / decrement, also needs
2789 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2790 and objc_types_are_equivalent may also need updates. */
2791 if (c_dialect_objc ())
2792 sorry ("%<_Atomic%> in Objective-C");
2793 if (flag_isoc99)
2794 pedwarn_c99 (loc, OPT_Wpedantic,
2795 "ISO C99 does not support the %<_Atomic%> qualifier");
2796 else
2797 pedwarn_c99 (loc, OPT_Wpedantic,
2798 "ISO C90 does not support the %<_Atomic%> qualifier");
2799 attrs_ok = true;
2800 tree value;
2801 value = c_parser_peek_token (parser)->value;
2802 c_parser_consume_token (parser);
2803 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2805 /* _Atomic ( type-name ). */
2806 seen_type = true;
2807 c_parser_consume_token (parser);
2808 struct c_type_name *type = c_parser_type_name (parser);
2809 t.kind = ctsk_typeof;
2810 t.spec = error_mark_node;
2811 t.expr = NULL_TREE;
2812 t.expr_const_operands = true;
2813 if (type != NULL)
2814 t.spec = groktypename (type, &t.expr,
2815 &t.expr_const_operands);
2816 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2817 "expected %<)%>");
2818 if (t.spec != error_mark_node)
2820 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2821 error_at (loc, "%<_Atomic%>-qualified array type");
2822 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2823 error_at (loc, "%<_Atomic%>-qualified function type");
2824 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2825 error_at (loc, "%<_Atomic%> applied to a qualified type");
2826 else
2827 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2829 declspecs_add_type (loc, specs, t);
2831 else
2832 declspecs_add_qual (loc, specs, value);
2833 break;
2834 case RID_CONST:
2835 case RID_VOLATILE:
2836 case RID_RESTRICT:
2837 attrs_ok = true;
2838 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2839 c_parser_consume_token (parser);
2840 break;
2841 case RID_ATTRIBUTE:
2842 if (!attrs_ok)
2843 goto out;
2844 attrs = c_parser_attributes (parser);
2845 declspecs_add_attrs (loc, specs, attrs);
2846 break;
2847 case RID_ALIGNAS:
2848 if (!alignspec_ok)
2849 goto out;
2850 align = c_parser_alignas_specifier (parser);
2851 declspecs_add_alignas (loc, specs, align);
2852 break;
2853 case RID_GIMPLE:
2854 if (! flag_gimple)
2855 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2856 c_parser_consume_token (parser);
2857 specs->gimple_p = true;
2858 specs->locations[cdw_gimple] = loc;
2859 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2860 break;
2861 case RID_RTL:
2862 c_parser_consume_token (parser);
2863 specs->rtl_p = true;
2864 specs->locations[cdw_rtl] = loc;
2865 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2866 break;
2867 default:
2868 goto out;
2871 out: ;
2874 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2876 enum-specifier:
2877 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2878 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2879 enum attributes[opt] identifier
2881 The form with trailing comma is new in C99. The forms with
2882 attributes are GNU extensions. In GNU C, we accept any expression
2883 without commas in the syntax (assignment expressions, not just
2884 conditional expressions); assignment expressions will be diagnosed
2885 as non-constant.
2887 enumerator-list:
2888 enumerator
2889 enumerator-list , enumerator
2891 enumerator:
2892 enumeration-constant
2893 enumeration-constant = constant-expression
2895 GNU Extensions:
2897 enumerator:
2898 enumeration-constant attributes[opt]
2899 enumeration-constant attributes[opt] = constant-expression
2903 static struct c_typespec
2904 c_parser_enum_specifier (c_parser *parser)
2906 struct c_typespec ret;
2907 tree attrs;
2908 tree ident = NULL_TREE;
2909 location_t enum_loc;
2910 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2911 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2912 c_parser_consume_token (parser);
2913 attrs = c_parser_attributes (parser);
2914 enum_loc = c_parser_peek_token (parser)->location;
2915 /* Set the location in case we create a decl now. */
2916 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2917 if (c_parser_next_token_is (parser, CPP_NAME))
2919 ident = c_parser_peek_token (parser)->value;
2920 ident_loc = c_parser_peek_token (parser)->location;
2921 enum_loc = ident_loc;
2922 c_parser_consume_token (parser);
2924 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2926 /* Parse an enum definition. */
2927 struct c_enum_contents the_enum;
2928 tree type;
2929 tree postfix_attrs;
2930 /* We chain the enumerators in reverse order, then put them in
2931 forward order at the end. */
2932 tree values;
2933 timevar_push (TV_PARSE_ENUM);
2934 type = start_enum (enum_loc, &the_enum, ident);
2935 values = NULL_TREE;
2936 c_parser_consume_token (parser);
2937 while (true)
2939 tree enum_id;
2940 tree enum_value;
2941 tree enum_decl;
2942 bool seen_comma;
2943 c_token *token;
2944 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2945 location_t decl_loc, value_loc;
2946 if (c_parser_next_token_is_not (parser, CPP_NAME))
2948 /* Give a nicer error for "enum {}". */
2949 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2950 && !parser->error)
2952 error_at (c_parser_peek_token (parser)->location,
2953 "empty enum is invalid");
2954 parser->error = true;
2956 else
2957 c_parser_error (parser, "expected identifier");
2958 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2959 values = error_mark_node;
2960 break;
2962 token = c_parser_peek_token (parser);
2963 enum_id = token->value;
2964 /* Set the location in case we create a decl now. */
2965 c_parser_set_source_position_from_token (token);
2966 decl_loc = value_loc = token->location;
2967 c_parser_consume_token (parser);
2968 /* Parse any specified attributes. */
2969 tree enum_attrs = c_parser_attributes (parser);
2970 if (c_parser_next_token_is (parser, CPP_EQ))
2972 c_parser_consume_token (parser);
2973 value_loc = c_parser_peek_token (parser)->location;
2974 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2976 else
2977 enum_value = NULL_TREE;
2978 enum_decl = build_enumerator (decl_loc, value_loc,
2979 &the_enum, enum_id, enum_value);
2980 if (enum_attrs)
2981 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2982 TREE_CHAIN (enum_decl) = values;
2983 values = enum_decl;
2984 seen_comma = false;
2985 if (c_parser_next_token_is (parser, CPP_COMMA))
2987 comma_loc = c_parser_peek_token (parser)->location;
2988 seen_comma = true;
2989 c_parser_consume_token (parser);
2991 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2993 if (seen_comma)
2994 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2995 "comma at end of enumerator list");
2996 c_parser_consume_token (parser);
2997 break;
2999 if (!seen_comma)
3001 c_parser_error (parser, "expected %<,%> or %<}%>");
3002 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3003 values = error_mark_node;
3004 break;
3007 postfix_attrs = c_parser_attributes (parser);
3008 ret.spec = finish_enum (type, nreverse (values),
3009 chainon (attrs, postfix_attrs));
3010 ret.kind = ctsk_tagdef;
3011 ret.expr = NULL_TREE;
3012 ret.expr_const_operands = true;
3013 timevar_pop (TV_PARSE_ENUM);
3014 return ret;
3016 else if (!ident)
3018 c_parser_error (parser, "expected %<{%>");
3019 ret.spec = error_mark_node;
3020 ret.kind = ctsk_tagref;
3021 ret.expr = NULL_TREE;
3022 ret.expr_const_operands = true;
3023 return ret;
3025 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3026 /* In ISO C, enumerated types can be referred to only if already
3027 defined. */
3028 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3030 gcc_assert (ident);
3031 pedwarn (enum_loc, OPT_Wpedantic,
3032 "ISO C forbids forward references to %<enum%> types");
3034 return ret;
3037 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3039 struct-or-union-specifier:
3040 struct-or-union attributes[opt] identifier[opt]
3041 { struct-contents } attributes[opt]
3042 struct-or-union attributes[opt] identifier
3044 struct-contents:
3045 struct-declaration-list
3047 struct-declaration-list:
3048 struct-declaration ;
3049 struct-declaration-list struct-declaration ;
3051 GNU extensions:
3053 struct-contents:
3054 empty
3055 struct-declaration
3056 struct-declaration-list struct-declaration
3058 struct-declaration-list:
3059 struct-declaration-list ;
3062 (Note that in the syntax here, unlike that in ISO C, the semicolons
3063 are included here rather than in struct-declaration, in order to
3064 describe the syntax with extra semicolons and missing semicolon at
3065 end.)
3067 Objective-C:
3069 struct-declaration-list:
3070 @defs ( class-name )
3072 (Note this does not include a trailing semicolon, but can be
3073 followed by further declarations, and gets a pedwarn-if-pedantic
3074 when followed by a semicolon.) */
3076 static struct c_typespec
3077 c_parser_struct_or_union_specifier (c_parser *parser)
3079 struct c_typespec ret;
3080 tree attrs;
3081 tree ident = NULL_TREE;
3082 location_t struct_loc;
3083 location_t ident_loc = UNKNOWN_LOCATION;
3084 enum tree_code code;
3085 switch (c_parser_peek_token (parser)->keyword)
3087 case RID_STRUCT:
3088 code = RECORD_TYPE;
3089 break;
3090 case RID_UNION:
3091 code = UNION_TYPE;
3092 break;
3093 default:
3094 gcc_unreachable ();
3096 struct_loc = c_parser_peek_token (parser)->location;
3097 c_parser_consume_token (parser);
3098 attrs = c_parser_attributes (parser);
3100 /* Set the location in case we create a decl now. */
3101 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3103 if (c_parser_next_token_is (parser, CPP_NAME))
3105 ident = c_parser_peek_token (parser)->value;
3106 ident_loc = c_parser_peek_token (parser)->location;
3107 struct_loc = ident_loc;
3108 c_parser_consume_token (parser);
3110 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3112 /* Parse a struct or union definition. Start the scope of the
3113 tag before parsing components. */
3114 struct c_struct_parse_info *struct_info;
3115 tree type = start_struct (struct_loc, code, ident, &struct_info);
3116 tree postfix_attrs;
3117 /* We chain the components in reverse order, then put them in
3118 forward order at the end. Each struct-declaration may
3119 declare multiple components (comma-separated), so we must use
3120 chainon to join them, although when parsing each
3121 struct-declaration we can use TREE_CHAIN directly.
3123 The theory behind all this is that there will be more
3124 semicolon separated fields than comma separated fields, and
3125 so we'll be minimizing the number of node traversals required
3126 by chainon. */
3127 tree contents;
3128 timevar_push (TV_PARSE_STRUCT);
3129 contents = NULL_TREE;
3130 c_parser_consume_token (parser);
3131 /* Handle the Objective-C @defs construct,
3132 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3133 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3135 tree name;
3136 gcc_assert (c_dialect_objc ());
3137 c_parser_consume_token (parser);
3138 matching_parens parens;
3139 if (!parens.require_open (parser))
3140 goto end_at_defs;
3141 if (c_parser_next_token_is (parser, CPP_NAME)
3142 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3144 name = c_parser_peek_token (parser)->value;
3145 c_parser_consume_token (parser);
3147 else
3149 c_parser_error (parser, "expected class name");
3150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3151 goto end_at_defs;
3153 parens.skip_until_found_close (parser);
3154 contents = nreverse (objc_get_class_ivars (name));
3156 end_at_defs:
3157 /* Parse the struct-declarations and semicolons. Problems with
3158 semicolons are diagnosed here; empty structures are diagnosed
3159 elsewhere. */
3160 while (true)
3162 tree decls;
3163 /* Parse any stray semicolon. */
3164 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3166 location_t semicolon_loc
3167 = c_parser_peek_token (parser)->location;
3168 gcc_rich_location richloc (semicolon_loc);
3169 richloc.add_fixit_remove ();
3170 pedwarn (&richloc, OPT_Wpedantic,
3171 "extra semicolon in struct or union specified");
3172 c_parser_consume_token (parser);
3173 continue;
3175 /* Stop if at the end of the struct or union contents. */
3176 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3178 c_parser_consume_token (parser);
3179 break;
3181 /* Accept #pragmas at struct scope. */
3182 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3184 c_parser_pragma (parser, pragma_struct, NULL);
3185 continue;
3187 /* Parse some comma-separated declarations, but not the
3188 trailing semicolon if any. */
3189 decls = c_parser_struct_declaration (parser);
3190 contents = chainon (decls, contents);
3191 /* If no semicolon follows, either we have a parse error or
3192 are at the end of the struct or union and should
3193 pedwarn. */
3194 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3195 c_parser_consume_token (parser);
3196 else
3198 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3199 pedwarn (c_parser_peek_token (parser)->location, 0,
3200 "no semicolon at end of struct or union");
3201 else if (parser->error
3202 || !c_parser_next_token_starts_declspecs (parser))
3204 c_parser_error (parser, "expected %<;%>");
3205 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3206 break;
3209 /* If we come here, we have already emitted an error
3210 for an expected `;', identifier or `(', and we also
3211 recovered already. Go on with the next field. */
3214 postfix_attrs = c_parser_attributes (parser);
3215 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3216 chainon (attrs, postfix_attrs), struct_info);
3217 ret.kind = ctsk_tagdef;
3218 ret.expr = NULL_TREE;
3219 ret.expr_const_operands = true;
3220 timevar_pop (TV_PARSE_STRUCT);
3221 return ret;
3223 else if (!ident)
3225 c_parser_error (parser, "expected %<{%>");
3226 ret.spec = error_mark_node;
3227 ret.kind = ctsk_tagref;
3228 ret.expr = NULL_TREE;
3229 ret.expr_const_operands = true;
3230 return ret;
3232 ret = parser_xref_tag (ident_loc, code, ident);
3233 return ret;
3236 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3237 *without* the trailing semicolon.
3239 struct-declaration:
3240 specifier-qualifier-list struct-declarator-list
3241 static_assert-declaration-no-semi
3243 specifier-qualifier-list:
3244 type-specifier specifier-qualifier-list[opt]
3245 type-qualifier specifier-qualifier-list[opt]
3246 alignment-specifier specifier-qualifier-list[opt]
3247 attributes specifier-qualifier-list[opt]
3249 struct-declarator-list:
3250 struct-declarator
3251 struct-declarator-list , attributes[opt] struct-declarator
3253 struct-declarator:
3254 declarator attributes[opt]
3255 declarator[opt] : constant-expression attributes[opt]
3257 GNU extensions:
3259 struct-declaration:
3260 __extension__ struct-declaration
3261 specifier-qualifier-list
3263 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3264 of attributes where shown is a GNU extension. In GNU C, we accept
3265 any expression without commas in the syntax (assignment
3266 expressions, not just conditional expressions); assignment
3267 expressions will be diagnosed as non-constant. */
3269 static tree
3270 c_parser_struct_declaration (c_parser *parser)
3272 struct c_declspecs *specs;
3273 tree prefix_attrs;
3274 tree all_prefix_attrs;
3275 tree decls;
3276 location_t decl_loc;
3277 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3279 int ext;
3280 tree decl;
3281 ext = disable_extension_diagnostics ();
3282 c_parser_consume_token (parser);
3283 decl = c_parser_struct_declaration (parser);
3284 restore_extension_diagnostics (ext);
3285 return decl;
3287 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3289 c_parser_static_assert_declaration_no_semi (parser);
3290 return NULL_TREE;
3292 specs = build_null_declspecs ();
3293 decl_loc = c_parser_peek_token (parser)->location;
3294 /* Strictly by the standard, we shouldn't allow _Alignas here,
3295 but it appears to have been intended to allow it there, so
3296 we're keeping it as it is until WG14 reaches a conclusion
3297 of N1731.
3298 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3299 c_parser_declspecs (parser, specs, false, true, true,
3300 true, false, cla_nonabstract_decl);
3301 if (parser->error)
3302 return NULL_TREE;
3303 if (!specs->declspecs_seen_p)
3305 c_parser_error (parser, "expected specifier-qualifier-list");
3306 return NULL_TREE;
3308 finish_declspecs (specs);
3309 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3310 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3312 tree ret;
3313 if (specs->typespec_kind == ctsk_none)
3315 pedwarn (decl_loc, OPT_Wpedantic,
3316 "ISO C forbids member declarations with no members");
3317 shadow_tag_warned (specs, pedantic);
3318 ret = NULL_TREE;
3320 else
3322 /* Support for unnamed structs or unions as members of
3323 structs or unions (which is [a] useful and [b] supports
3324 MS P-SDK). */
3325 tree attrs = NULL;
3327 ret = grokfield (c_parser_peek_token (parser)->location,
3328 build_id_declarator (NULL_TREE), specs,
3329 NULL_TREE, &attrs);
3330 if (ret)
3331 decl_attributes (&ret, attrs, 0);
3333 return ret;
3336 /* Provide better error recovery. Note that a type name here is valid,
3337 and will be treated as a field name. */
3338 if (specs->typespec_kind == ctsk_tagdef
3339 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3340 && c_parser_next_token_starts_declspecs (parser)
3341 && !c_parser_next_token_is (parser, CPP_NAME))
3343 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3344 parser->error = false;
3345 return NULL_TREE;
3348 pending_xref_error ();
3349 prefix_attrs = specs->attrs;
3350 all_prefix_attrs = prefix_attrs;
3351 specs->attrs = NULL_TREE;
3352 decls = NULL_TREE;
3353 while (true)
3355 /* Declaring one or more declarators or un-named bit-fields. */
3356 struct c_declarator *declarator;
3357 bool dummy = false;
3358 if (c_parser_next_token_is (parser, CPP_COLON))
3359 declarator = build_id_declarator (NULL_TREE);
3360 else
3361 declarator = c_parser_declarator (parser,
3362 specs->typespec_kind != ctsk_none,
3363 C_DTR_NORMAL, &dummy);
3364 if (declarator == NULL)
3366 c_parser_skip_to_end_of_block_or_statement (parser);
3367 break;
3369 if (c_parser_next_token_is (parser, CPP_COLON)
3370 || c_parser_next_token_is (parser, CPP_COMMA)
3371 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3372 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3373 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3375 tree postfix_attrs = NULL_TREE;
3376 tree width = NULL_TREE;
3377 tree d;
3378 if (c_parser_next_token_is (parser, CPP_COLON))
3380 c_parser_consume_token (parser);
3381 width = c_parser_expr_no_commas (parser, NULL).value;
3383 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3384 postfix_attrs = c_parser_attributes (parser);
3385 d = grokfield (c_parser_peek_token (parser)->location,
3386 declarator, specs, width, &all_prefix_attrs);
3387 decl_attributes (&d, chainon (postfix_attrs,
3388 all_prefix_attrs), 0);
3389 DECL_CHAIN (d) = decls;
3390 decls = d;
3391 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3392 all_prefix_attrs = chainon (c_parser_attributes (parser),
3393 prefix_attrs);
3394 else
3395 all_prefix_attrs = prefix_attrs;
3396 if (c_parser_next_token_is (parser, CPP_COMMA))
3397 c_parser_consume_token (parser);
3398 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3399 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3401 /* Semicolon consumed in caller. */
3402 break;
3404 else
3406 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3407 break;
3410 else
3412 c_parser_error (parser,
3413 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3414 "%<__attribute__%>");
3415 break;
3418 return decls;
3421 /* Parse a typeof specifier (a GNU extension).
3423 typeof-specifier:
3424 typeof ( expression )
3425 typeof ( type-name )
3428 static struct c_typespec
3429 c_parser_typeof_specifier (c_parser *parser)
3431 struct c_typespec ret;
3432 ret.kind = ctsk_typeof;
3433 ret.spec = error_mark_node;
3434 ret.expr = NULL_TREE;
3435 ret.expr_const_operands = true;
3436 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3437 c_parser_consume_token (parser);
3438 c_inhibit_evaluation_warnings++;
3439 in_typeof++;
3440 matching_parens parens;
3441 if (!parens.require_open (parser))
3443 c_inhibit_evaluation_warnings--;
3444 in_typeof--;
3445 return ret;
3447 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3449 struct c_type_name *type = c_parser_type_name (parser);
3450 c_inhibit_evaluation_warnings--;
3451 in_typeof--;
3452 if (type != NULL)
3454 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3455 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3458 else
3460 bool was_vm;
3461 location_t here = c_parser_peek_token (parser)->location;
3462 struct c_expr expr = c_parser_expression (parser);
3463 c_inhibit_evaluation_warnings--;
3464 in_typeof--;
3465 if (TREE_CODE (expr.value) == COMPONENT_REF
3466 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3467 error_at (here, "%<typeof%> applied to a bit-field");
3468 mark_exp_read (expr.value);
3469 ret.spec = TREE_TYPE (expr.value);
3470 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3471 /* This is returned with the type so that when the type is
3472 evaluated, this can be evaluated. */
3473 if (was_vm)
3474 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3475 pop_maybe_used (was_vm);
3476 /* For use in macros such as those in <stdatomic.h>, remove all
3477 qualifiers from atomic types. (const can be an issue for more macros
3478 using typeof than just the <stdatomic.h> ones.) */
3479 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3480 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3482 parens.skip_until_found_close (parser);
3483 return ret;
3486 /* Parse an alignment-specifier.
3488 C11 6.7.5:
3490 alignment-specifier:
3491 _Alignas ( type-name )
3492 _Alignas ( constant-expression )
3495 static tree
3496 c_parser_alignas_specifier (c_parser * parser)
3498 tree ret = error_mark_node;
3499 location_t loc = c_parser_peek_token (parser)->location;
3500 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3501 c_parser_consume_token (parser);
3502 if (flag_isoc99)
3503 pedwarn_c99 (loc, OPT_Wpedantic,
3504 "ISO C99 does not support %<_Alignas%>");
3505 else
3506 pedwarn_c99 (loc, OPT_Wpedantic,
3507 "ISO C90 does not support %<_Alignas%>");
3508 matching_parens parens;
3509 if (!parens.require_open (parser))
3510 return ret;
3511 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3513 struct c_type_name *type = c_parser_type_name (parser);
3514 if (type != NULL)
3515 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3516 false, true, 1);
3518 else
3519 ret = c_parser_expr_no_commas (parser, NULL).value;
3520 parens.skip_until_found_close (parser);
3521 return ret;
3524 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3525 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3526 a typedef name may be redeclared; otherwise it may not. KIND
3527 indicates which kind of declarator is wanted. Returns a valid
3528 declarator except in the case of a syntax error in which case NULL is
3529 returned. *SEEN_ID is set to true if an identifier being declared is
3530 seen; this is used to diagnose bad forms of abstract array declarators
3531 and to determine whether an identifier list is syntactically permitted.
3533 declarator:
3534 pointer[opt] direct-declarator
3536 direct-declarator:
3537 identifier
3538 ( attributes[opt] declarator )
3539 direct-declarator array-declarator
3540 direct-declarator ( parameter-type-list )
3541 direct-declarator ( identifier-list[opt] )
3543 pointer:
3544 * type-qualifier-list[opt]
3545 * type-qualifier-list[opt] pointer
3547 type-qualifier-list:
3548 type-qualifier
3549 attributes
3550 type-qualifier-list type-qualifier
3551 type-qualifier-list attributes
3553 array-declarator:
3554 [ type-qualifier-list[opt] assignment-expression[opt] ]
3555 [ static type-qualifier-list[opt] assignment-expression ]
3556 [ type-qualifier-list static assignment-expression ]
3557 [ type-qualifier-list[opt] * ]
3559 parameter-type-list:
3560 parameter-list
3561 parameter-list , ...
3563 parameter-list:
3564 parameter-declaration
3565 parameter-list , parameter-declaration
3567 parameter-declaration:
3568 declaration-specifiers declarator attributes[opt]
3569 declaration-specifiers abstract-declarator[opt] attributes[opt]
3571 identifier-list:
3572 identifier
3573 identifier-list , identifier
3575 abstract-declarator:
3576 pointer
3577 pointer[opt] direct-abstract-declarator
3579 direct-abstract-declarator:
3580 ( attributes[opt] abstract-declarator )
3581 direct-abstract-declarator[opt] array-declarator
3582 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3584 GNU extensions:
3586 direct-declarator:
3587 direct-declarator ( parameter-forward-declarations
3588 parameter-type-list[opt] )
3590 direct-abstract-declarator:
3591 direct-abstract-declarator[opt] ( parameter-forward-declarations
3592 parameter-type-list[opt] )
3594 parameter-forward-declarations:
3595 parameter-list ;
3596 parameter-forward-declarations parameter-list ;
3598 The uses of attributes shown above are GNU extensions.
3600 Some forms of array declarator are not included in C99 in the
3601 syntax for abstract declarators; these are disallowed elsewhere.
3602 This may be a defect (DR#289).
3604 This function also accepts an omitted abstract declarator as being
3605 an abstract declarator, although not part of the formal syntax. */
3607 struct c_declarator *
3608 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3609 bool *seen_id)
3611 /* Parse any initial pointer part. */
3612 if (c_parser_next_token_is (parser, CPP_MULT))
3614 struct c_declspecs *quals_attrs = build_null_declspecs ();
3615 struct c_declarator *inner;
3616 c_parser_consume_token (parser);
3617 c_parser_declspecs (parser, quals_attrs, false, false, true,
3618 false, false, cla_prefer_id);
3619 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3620 if (inner == NULL)
3621 return NULL;
3622 else
3623 return make_pointer_declarator (quals_attrs, inner);
3625 /* Now we have a direct declarator, direct abstract declarator or
3626 nothing (which counts as a direct abstract declarator here). */
3627 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3630 /* Parse a direct declarator or direct abstract declarator; arguments
3631 as c_parser_declarator. */
3633 static struct c_declarator *
3634 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3635 bool *seen_id)
3637 /* The direct declarator must start with an identifier (possibly
3638 omitted) or a parenthesized declarator (possibly abstract). In
3639 an ordinary declarator, initial parentheses must start a
3640 parenthesized declarator. In an abstract declarator or parameter
3641 declarator, they could start a parenthesized declarator or a
3642 parameter list. To tell which, the open parenthesis and any
3643 following attributes must be read. If a declaration specifier
3644 follows, then it is a parameter list; if the specifier is a
3645 typedef name, there might be an ambiguity about redeclaring it,
3646 which is resolved in the direction of treating it as a typedef
3647 name. If a close parenthesis follows, it is also an empty
3648 parameter list, as the syntax does not permit empty abstract
3649 declarators. Otherwise, it is a parenthesized declarator (in
3650 which case the analysis may be repeated inside it, recursively).
3652 ??? There is an ambiguity in a parameter declaration "int
3653 (__attribute__((foo)) x)", where x is not a typedef name: it
3654 could be an abstract declarator for a function, or declare x with
3655 parentheses. The proper resolution of this ambiguity needs
3656 documenting. At present we follow an accident of the old
3657 parser's implementation, whereby the first parameter must have
3658 some declaration specifiers other than just attributes. Thus as
3659 a parameter declaration it is treated as a parenthesized
3660 parameter named x, and as an abstract declarator it is
3661 rejected.
3663 ??? Also following the old parser, attributes inside an empty
3664 parameter list are ignored, making it a list not yielding a
3665 prototype, rather than giving an error or making it have one
3666 parameter with implicit type int.
3668 ??? Also following the old parser, typedef names may be
3669 redeclared in declarators, but not Objective-C class names. */
3671 if (kind != C_DTR_ABSTRACT
3672 && c_parser_next_token_is (parser, CPP_NAME)
3673 && ((type_seen_p
3674 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3675 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3676 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3678 struct c_declarator *inner
3679 = build_id_declarator (c_parser_peek_token (parser)->value);
3680 *seen_id = true;
3681 inner->id_loc = c_parser_peek_token (parser)->location;
3682 c_parser_consume_token (parser);
3683 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3686 if (kind != C_DTR_NORMAL
3687 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3689 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3690 inner->id_loc = c_parser_peek_token (parser)->location;
3691 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3694 /* Either we are at the end of an abstract declarator, or we have
3695 parentheses. */
3697 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3699 tree attrs;
3700 struct c_declarator *inner;
3701 c_parser_consume_token (parser);
3702 attrs = c_parser_attributes (parser);
3703 if (kind != C_DTR_NORMAL
3704 && (c_parser_next_token_starts_declspecs (parser)
3705 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3707 struct c_arg_info *args
3708 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3709 attrs);
3710 if (args == NULL)
3711 return NULL;
3712 else
3714 inner
3715 = build_function_declarator (args,
3716 build_id_declarator (NULL_TREE));
3717 return c_parser_direct_declarator_inner (parser, *seen_id,
3718 inner);
3721 /* A parenthesized declarator. */
3722 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3723 if (inner != NULL && attrs != NULL)
3724 inner = build_attrs_declarator (attrs, inner);
3725 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3727 c_parser_consume_token (parser);
3728 if (inner == NULL)
3729 return NULL;
3730 else
3731 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3733 else
3735 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3736 "expected %<)%>");
3737 return NULL;
3740 else
3742 if (kind == C_DTR_NORMAL)
3744 c_parser_error (parser, "expected identifier or %<(%>");
3745 return NULL;
3747 else
3748 return build_id_declarator (NULL_TREE);
3752 /* Parse part of a direct declarator or direct abstract declarator,
3753 given that some (in INNER) has already been parsed; ID_PRESENT is
3754 true if an identifier is present, false for an abstract
3755 declarator. */
3757 static struct c_declarator *
3758 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3759 struct c_declarator *inner)
3761 /* Parse a sequence of array declarators and parameter lists. */
3762 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3764 location_t brace_loc = c_parser_peek_token (parser)->location;
3765 struct c_declarator *declarator;
3766 struct c_declspecs *quals_attrs = build_null_declspecs ();
3767 bool static_seen;
3768 bool star_seen;
3769 struct c_expr dimen;
3770 dimen.value = NULL_TREE;
3771 dimen.original_code = ERROR_MARK;
3772 dimen.original_type = NULL_TREE;
3773 c_parser_consume_token (parser);
3774 c_parser_declspecs (parser, quals_attrs, false, false, true,
3775 false, false, cla_prefer_id);
3776 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3777 if (static_seen)
3778 c_parser_consume_token (parser);
3779 if (static_seen && !quals_attrs->declspecs_seen_p)
3780 c_parser_declspecs (parser, quals_attrs, false, false, true,
3781 false, false, cla_prefer_id);
3782 if (!quals_attrs->declspecs_seen_p)
3783 quals_attrs = NULL;
3784 /* If "static" is present, there must be an array dimension.
3785 Otherwise, there may be a dimension, "*", or no
3786 dimension. */
3787 if (static_seen)
3789 star_seen = false;
3790 dimen = c_parser_expr_no_commas (parser, NULL);
3792 else
3794 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3796 dimen.value = NULL_TREE;
3797 star_seen = false;
3799 else if (c_parser_next_token_is (parser, CPP_MULT))
3801 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3803 dimen.value = NULL_TREE;
3804 star_seen = true;
3805 c_parser_consume_token (parser);
3807 else
3809 star_seen = false;
3810 dimen = c_parser_expr_no_commas (parser, NULL);
3813 else
3815 star_seen = false;
3816 dimen = c_parser_expr_no_commas (parser, NULL);
3819 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3820 c_parser_consume_token (parser);
3821 else
3823 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3824 "expected %<]%>");
3825 return NULL;
3827 if (dimen.value)
3828 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3829 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3830 static_seen, star_seen);
3831 if (declarator == NULL)
3832 return NULL;
3833 inner = set_array_declarator_inner (declarator, inner);
3834 return c_parser_direct_declarator_inner (parser, id_present, inner);
3836 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3838 tree attrs;
3839 struct c_arg_info *args;
3840 c_parser_consume_token (parser);
3841 attrs = c_parser_attributes (parser);
3842 args = c_parser_parms_declarator (parser, id_present, attrs);
3843 if (args == NULL)
3844 return NULL;
3845 else
3847 inner = build_function_declarator (args, inner);
3848 return c_parser_direct_declarator_inner (parser, id_present, inner);
3851 return inner;
3854 /* Parse a parameter list or identifier list, including the closing
3855 parenthesis but not the opening one. ATTRS are the attributes at
3856 the start of the list. ID_LIST_OK is true if an identifier list is
3857 acceptable; such a list must not have attributes at the start. */
3859 static struct c_arg_info *
3860 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3862 push_scope ();
3863 declare_parm_level ();
3864 /* If the list starts with an identifier, it is an identifier list.
3865 Otherwise, it is either a prototype list or an empty list. */
3866 if (id_list_ok
3867 && !attrs
3868 && c_parser_next_token_is (parser, CPP_NAME)
3869 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3871 /* Look ahead to detect typos in type names. */
3872 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3873 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3874 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3875 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3876 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3878 tree list = NULL_TREE, *nextp = &list;
3879 while (c_parser_next_token_is (parser, CPP_NAME)
3880 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3882 *nextp = build_tree_list (NULL_TREE,
3883 c_parser_peek_token (parser)->value);
3884 nextp = & TREE_CHAIN (*nextp);
3885 c_parser_consume_token (parser);
3886 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3887 break;
3888 c_parser_consume_token (parser);
3889 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3891 c_parser_error (parser, "expected identifier");
3892 break;
3895 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3897 struct c_arg_info *ret = build_arg_info ();
3898 ret->types = list;
3899 c_parser_consume_token (parser);
3900 pop_scope ();
3901 return ret;
3903 else
3905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3906 "expected %<)%>");
3907 pop_scope ();
3908 return NULL;
3911 else
3913 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3914 NULL);
3915 pop_scope ();
3916 return ret;
3920 /* Parse a parameter list (possibly empty), including the closing
3921 parenthesis but not the opening one. ATTRS are the attributes at
3922 the start of the list. EXPR is NULL or an expression that needs to
3923 be evaluated for the side effects of array size expressions in the
3924 parameters. */
3926 static struct c_arg_info *
3927 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3929 bool bad_parm = false;
3931 /* ??? Following the old parser, forward parameter declarations may
3932 use abstract declarators, and if no real parameter declarations
3933 follow the forward declarations then this is not diagnosed. Also
3934 note as above that attributes are ignored as the only contents of
3935 the parentheses, or as the only contents after forward
3936 declarations. */
3937 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3939 struct c_arg_info *ret = build_arg_info ();
3940 c_parser_consume_token (parser);
3941 return ret;
3943 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3945 struct c_arg_info *ret = build_arg_info ();
3947 if (flag_allow_parameterless_variadic_functions)
3949 /* F (...) is allowed. */
3950 ret->types = NULL_TREE;
3952 else
3954 /* Suppress -Wold-style-definition for this case. */
3955 ret->types = error_mark_node;
3956 error_at (c_parser_peek_token (parser)->location,
3957 "ISO C requires a named argument before %<...%>");
3959 c_parser_consume_token (parser);
3960 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3962 c_parser_consume_token (parser);
3963 return ret;
3965 else
3967 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3968 "expected %<)%>");
3969 return NULL;
3972 /* Nonempty list of parameters, either terminated with semicolon
3973 (forward declarations; recurse) or with close parenthesis (normal
3974 function) or with ", ... )" (variadic function). */
3975 while (true)
3977 /* Parse a parameter. */
3978 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3979 attrs = NULL_TREE;
3980 if (parm == NULL)
3981 bad_parm = true;
3982 else
3983 push_parm_decl (parm, &expr);
3984 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3986 tree new_attrs;
3987 c_parser_consume_token (parser);
3988 mark_forward_parm_decls ();
3989 new_attrs = c_parser_attributes (parser);
3990 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3992 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3994 c_parser_consume_token (parser);
3995 if (bad_parm)
3996 return NULL;
3997 else
3998 return get_parm_info (false, expr);
4000 if (!c_parser_require (parser, CPP_COMMA,
4001 "expected %<;%>, %<,%> or %<)%>",
4002 UNKNOWN_LOCATION, false))
4004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4005 return NULL;
4007 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4009 c_parser_consume_token (parser);
4010 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4012 c_parser_consume_token (parser);
4013 if (bad_parm)
4014 return NULL;
4015 else
4016 return get_parm_info (true, expr);
4018 else
4020 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4021 "expected %<)%>");
4022 return NULL;
4028 /* Parse a parameter declaration. ATTRS are the attributes at the
4029 start of the declaration if it is the first parameter. */
4031 static struct c_parm *
4032 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4034 struct c_declspecs *specs;
4035 struct c_declarator *declarator;
4036 tree prefix_attrs;
4037 tree postfix_attrs = NULL_TREE;
4038 bool dummy = false;
4040 /* Accept #pragmas between parameter declarations. */
4041 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4042 c_parser_pragma (parser, pragma_param, NULL);
4044 if (!c_parser_next_token_starts_declspecs (parser))
4046 c_token *token = c_parser_peek_token (parser);
4047 if (parser->error)
4048 return NULL;
4049 c_parser_set_source_position_from_token (token);
4050 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4052 name_hint hint = lookup_name_fuzzy (token->value,
4053 FUZZY_LOOKUP_TYPENAME,
4054 token->location);
4055 if (hint)
4057 gcc_rich_location richloc (token->location);
4058 richloc.add_fixit_replace (hint.suggestion ());
4059 error_at (&richloc,
4060 "unknown type name %qE; did you mean %qs?",
4061 token->value, hint.suggestion ());
4063 else
4064 error_at (token->location, "unknown type name %qE", token->value);
4065 parser->error = true;
4067 /* ??? In some Objective-C cases '...' isn't applicable so there
4068 should be a different message. */
4069 else
4070 c_parser_error (parser,
4071 "expected declaration specifiers or %<...%>");
4072 c_parser_skip_to_end_of_parameter (parser);
4073 return NULL;
4076 location_t start_loc = c_parser_peek_token (parser)->location;
4078 specs = build_null_declspecs ();
4079 if (attrs)
4081 declspecs_add_attrs (input_location, specs, attrs);
4082 attrs = NULL_TREE;
4084 c_parser_declspecs (parser, specs, true, true, true, true, false,
4085 cla_nonabstract_decl);
4086 finish_declspecs (specs);
4087 pending_xref_error ();
4088 prefix_attrs = specs->attrs;
4089 specs->attrs = NULL_TREE;
4090 declarator = c_parser_declarator (parser,
4091 specs->typespec_kind != ctsk_none,
4092 C_DTR_PARM, &dummy);
4093 if (declarator == NULL)
4095 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4096 return NULL;
4098 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4099 postfix_attrs = c_parser_attributes (parser);
4101 /* Generate a location for the parameter, ranging from the start of the
4102 initial token to the end of the final token.
4104 If we have a identifier, then use it for the caret location, e.g.
4106 extern int callee (int one, int (*two)(int, int), float three);
4107 ~~~~~~^~~~~~~~~~~~~~
4109 otherwise, reuse the start location for the caret location e.g.:
4111 extern int callee (int one, int (*)(int, int), float three);
4112 ^~~~~~~~~~~~~~~~~
4114 location_t end_loc = parser->last_token_location;
4116 /* Find any cdk_id declarator; determine if we have an identifier. */
4117 c_declarator *id_declarator = declarator;
4118 while (id_declarator && id_declarator->kind != cdk_id)
4119 id_declarator = id_declarator->declarator;
4120 location_t caret_loc = (id_declarator->u.id
4121 ? id_declarator->id_loc
4122 : start_loc);
4123 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4125 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4126 declarator, param_loc);
4129 /* Parse a string literal in an asm expression. It should not be
4130 translated, and wide string literals are an error although
4131 permitted by the syntax. This is a GNU extension.
4133 asm-string-literal:
4134 string-literal
4136 ??? At present, following the old parser, the caller needs to have
4137 set lex_untranslated_string to 1. It would be better to follow the
4138 C++ parser rather than using this kludge. */
4140 static tree
4141 c_parser_asm_string_literal (c_parser *parser)
4143 tree str;
4144 int save_flag = warn_overlength_strings;
4145 warn_overlength_strings = 0;
4146 if (c_parser_next_token_is (parser, CPP_STRING))
4148 str = c_parser_peek_token (parser)->value;
4149 c_parser_consume_token (parser);
4151 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4153 error_at (c_parser_peek_token (parser)->location,
4154 "wide string literal in %<asm%>");
4155 str = build_string (1, "");
4156 c_parser_consume_token (parser);
4158 else
4160 c_parser_error (parser, "expected string literal");
4161 str = NULL_TREE;
4163 warn_overlength_strings = save_flag;
4164 return str;
4167 /* Parse a simple asm expression. This is used in restricted
4168 contexts, where a full expression with inputs and outputs does not
4169 make sense. This is a GNU extension.
4171 simple-asm-expr:
4172 asm ( asm-string-literal )
4175 static tree
4176 c_parser_simple_asm_expr (c_parser *parser)
4178 tree str;
4179 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4180 /* ??? Follow the C++ parser rather than using the
4181 lex_untranslated_string kludge. */
4182 parser->lex_untranslated_string = true;
4183 c_parser_consume_token (parser);
4184 matching_parens parens;
4185 if (!parens.require_open (parser))
4187 parser->lex_untranslated_string = false;
4188 return NULL_TREE;
4190 str = c_parser_asm_string_literal (parser);
4191 parser->lex_untranslated_string = false;
4192 if (!parens.require_close (parser))
4194 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4195 return NULL_TREE;
4197 return str;
4200 static tree
4201 c_parser_attribute_any_word (c_parser *parser)
4203 tree attr_name = NULL_TREE;
4205 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4207 /* ??? See comment above about what keywords are accepted here. */
4208 bool ok;
4209 switch (c_parser_peek_token (parser)->keyword)
4211 case RID_STATIC:
4212 case RID_UNSIGNED:
4213 case RID_LONG:
4214 case RID_CONST:
4215 case RID_EXTERN:
4216 case RID_REGISTER:
4217 case RID_TYPEDEF:
4218 case RID_SHORT:
4219 case RID_INLINE:
4220 case RID_NORETURN:
4221 case RID_VOLATILE:
4222 case RID_SIGNED:
4223 case RID_AUTO:
4224 case RID_RESTRICT:
4225 case RID_COMPLEX:
4226 case RID_THREAD:
4227 case RID_INT:
4228 case RID_CHAR:
4229 case RID_FLOAT:
4230 case RID_DOUBLE:
4231 case RID_VOID:
4232 case RID_DFLOAT32:
4233 case RID_DFLOAT64:
4234 case RID_DFLOAT128:
4235 CASE_RID_FLOATN_NX:
4236 case RID_BOOL:
4237 case RID_FRACT:
4238 case RID_ACCUM:
4239 case RID_SAT:
4240 case RID_TRANSACTION_ATOMIC:
4241 case RID_TRANSACTION_CANCEL:
4242 case RID_ATOMIC:
4243 case RID_AUTO_TYPE:
4244 case RID_INT_N_0:
4245 case RID_INT_N_1:
4246 case RID_INT_N_2:
4247 case RID_INT_N_3:
4248 ok = true;
4249 break;
4250 default:
4251 ok = false;
4252 break;
4254 if (!ok)
4255 return NULL_TREE;
4257 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4258 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4260 else if (c_parser_next_token_is (parser, CPP_NAME))
4261 attr_name = c_parser_peek_token (parser)->value;
4263 return attr_name;
4266 /* Parse (possibly empty) attributes. This is a GNU extension.
4268 attributes:
4269 empty
4270 attributes attribute
4272 attribute:
4273 __attribute__ ( ( attribute-list ) )
4275 attribute-list:
4276 attrib
4277 attribute_list , attrib
4279 attrib:
4280 empty
4281 any-word
4282 any-word ( identifier )
4283 any-word ( identifier , nonempty-expr-list )
4284 any-word ( expr-list )
4286 where the "identifier" must not be declared as a type, and
4287 "any-word" may be any identifier (including one declared as a
4288 type), a reserved word storage class specifier, type specifier or
4289 type qualifier. ??? This still leaves out most reserved keywords
4290 (following the old parser), shouldn't we include them, and why not
4291 allow identifiers declared as types to start the arguments? */
4293 static tree
4294 c_parser_attributes (c_parser *parser)
4296 tree attrs = NULL_TREE;
4297 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4299 /* ??? Follow the C++ parser rather than using the
4300 lex_untranslated_string kludge. */
4301 parser->lex_untranslated_string = true;
4302 /* Consume the `__attribute__' keyword. */
4303 c_parser_consume_token (parser);
4304 /* Look for the two `(' tokens. */
4305 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4307 parser->lex_untranslated_string = false;
4308 return attrs;
4310 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4312 parser->lex_untranslated_string = false;
4313 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4314 return attrs;
4316 /* Parse the attribute list. */
4317 while (c_parser_next_token_is (parser, CPP_COMMA)
4318 || c_parser_next_token_is (parser, CPP_NAME)
4319 || c_parser_next_token_is (parser, CPP_KEYWORD))
4321 tree attr, attr_name, attr_args;
4322 vec<tree, va_gc> *expr_list;
4323 if (c_parser_next_token_is (parser, CPP_COMMA))
4325 c_parser_consume_token (parser);
4326 continue;
4329 attr_name = c_parser_attribute_any_word (parser);
4330 if (attr_name == NULL)
4331 break;
4332 attr_name = canonicalize_attr_name (attr_name);
4333 c_parser_consume_token (parser);
4334 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4336 attr = build_tree_list (attr_name, NULL_TREE);
4337 /* Add this attribute to the list. */
4338 attrs = chainon (attrs, attr);
4339 /* If the next token isn't a comma, we're done. */
4340 if (!c_parser_next_token_is (parser, CPP_COMMA))
4341 break;
4342 continue;
4344 c_parser_consume_token (parser);
4345 /* Parse the attribute contents. If they start with an
4346 identifier which is followed by a comma or close
4347 parenthesis, then the arguments start with that
4348 identifier; otherwise they are an expression list.
4349 In objective-c the identifier may be a classname. */
4350 if (c_parser_next_token_is (parser, CPP_NAME)
4351 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4352 || (c_dialect_objc ()
4353 && c_parser_peek_token (parser)->id_kind
4354 == C_ID_CLASSNAME))
4355 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4356 || (c_parser_peek_2nd_token (parser)->type
4357 == CPP_CLOSE_PAREN))
4358 && (attribute_takes_identifier_p (attr_name)
4359 || (c_dialect_objc ()
4360 && c_parser_peek_token (parser)->id_kind
4361 == C_ID_CLASSNAME)))
4363 tree arg1 = c_parser_peek_token (parser)->value;
4364 c_parser_consume_token (parser);
4365 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4366 attr_args = build_tree_list (NULL_TREE, arg1);
4367 else
4369 tree tree_list;
4370 c_parser_consume_token (parser);
4371 expr_list = c_parser_expr_list (parser, false, true,
4372 NULL, NULL, NULL, NULL);
4373 tree_list = build_tree_list_vec (expr_list);
4374 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4375 release_tree_vector (expr_list);
4378 else
4380 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4381 attr_args = NULL_TREE;
4382 else
4384 expr_list = c_parser_expr_list (parser, false, true,
4385 NULL, NULL, NULL, NULL);
4386 attr_args = build_tree_list_vec (expr_list);
4387 release_tree_vector (expr_list);
4391 attr = build_tree_list (attr_name, attr_args);
4392 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4393 c_parser_consume_token (parser);
4394 else
4396 parser->lex_untranslated_string = false;
4397 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4398 "expected %<)%>");
4399 return attrs;
4401 /* Add this attribute to the list. */
4402 attrs = chainon (attrs, attr);
4403 /* If the next token isn't a comma, we're done. */
4404 if (!c_parser_next_token_is (parser, CPP_COMMA))
4405 break;
4407 /* Look for the two `)' tokens. */
4408 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4409 c_parser_consume_token (parser);
4410 else
4412 parser->lex_untranslated_string = false;
4413 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4414 "expected %<)%>");
4415 return attrs;
4417 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4418 c_parser_consume_token (parser);
4419 else
4421 parser->lex_untranslated_string = false;
4422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4423 "expected %<)%>");
4424 return attrs;
4426 parser->lex_untranslated_string = false;
4429 return attrs;
4432 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4433 says whether alignment specifiers are OK (only in cases that might
4434 be the type name of a compound literal).
4436 type-name:
4437 specifier-qualifier-list abstract-declarator[opt]
4440 struct c_type_name *
4441 c_parser_type_name (c_parser *parser, bool alignas_ok)
4443 struct c_declspecs *specs = build_null_declspecs ();
4444 struct c_declarator *declarator;
4445 struct c_type_name *ret;
4446 bool dummy = false;
4447 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4448 cla_prefer_type);
4449 if (!specs->declspecs_seen_p)
4451 c_parser_error (parser, "expected specifier-qualifier-list");
4452 return NULL;
4454 if (specs->type != error_mark_node)
4456 pending_xref_error ();
4457 finish_declspecs (specs);
4459 declarator = c_parser_declarator (parser,
4460 specs->typespec_kind != ctsk_none,
4461 C_DTR_ABSTRACT, &dummy);
4462 if (declarator == NULL)
4463 return NULL;
4464 ret = XOBNEW (&parser_obstack, struct c_type_name);
4465 ret->specs = specs;
4466 ret->declarator = declarator;
4467 return ret;
4470 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4472 initializer:
4473 assignment-expression
4474 { initializer-list }
4475 { initializer-list , }
4477 initializer-list:
4478 designation[opt] initializer
4479 initializer-list , designation[opt] initializer
4481 designation:
4482 designator-list =
4484 designator-list:
4485 designator
4486 designator-list designator
4488 designator:
4489 array-designator
4490 . identifier
4492 array-designator:
4493 [ constant-expression ]
4495 GNU extensions:
4497 initializer:
4500 designation:
4501 array-designator
4502 identifier :
4504 array-designator:
4505 [ constant-expression ... constant-expression ]
4507 Any expression without commas is accepted in the syntax for the
4508 constant-expressions, with non-constant expressions rejected later.
4510 This function is only used for top-level initializers; for nested
4511 ones, see c_parser_initval. */
4513 static struct c_expr
4514 c_parser_initializer (c_parser *parser)
4516 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4517 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4518 else
4520 struct c_expr ret;
4521 location_t loc = c_parser_peek_token (parser)->location;
4522 ret = c_parser_expr_no_commas (parser, NULL);
4523 if (TREE_CODE (ret.value) != STRING_CST
4524 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4525 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4526 return ret;
4530 /* The location of the last comma within the current initializer list,
4531 or UNKNOWN_LOCATION if not within one. */
4533 location_t last_init_list_comma;
4535 /* Parse a braced initializer list. TYPE is the type specified for a
4536 compound literal, and NULL_TREE for other initializers and for
4537 nested braced lists. NESTED_P is true for nested braced lists,
4538 false for the list of a compound literal or the list that is the
4539 top-level initializer in a declaration. */
4541 static struct c_expr
4542 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4543 struct obstack *outer_obstack)
4545 struct c_expr ret;
4546 struct obstack braced_init_obstack;
4547 location_t brace_loc = c_parser_peek_token (parser)->location;
4548 gcc_obstack_init (&braced_init_obstack);
4549 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4550 matching_braces braces;
4551 braces.consume_open (parser);
4552 if (nested_p)
4554 finish_implicit_inits (brace_loc, outer_obstack);
4555 push_init_level (brace_loc, 0, &braced_init_obstack);
4557 else
4558 really_start_incremental_init (type);
4559 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4561 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4563 else
4565 /* Parse a non-empty initializer list, possibly with a trailing
4566 comma. */
4567 while (true)
4569 c_parser_initelt (parser, &braced_init_obstack);
4570 if (parser->error)
4571 break;
4572 if (c_parser_next_token_is (parser, CPP_COMMA))
4574 last_init_list_comma = c_parser_peek_token (parser)->location;
4575 c_parser_consume_token (parser);
4577 else
4578 break;
4579 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4580 break;
4583 c_token *next_tok = c_parser_peek_token (parser);
4584 if (next_tok->type != CPP_CLOSE_BRACE)
4586 ret.set_error ();
4587 ret.original_code = ERROR_MARK;
4588 ret.original_type = NULL;
4589 braces.skip_until_found_close (parser);
4590 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4591 obstack_free (&braced_init_obstack, NULL);
4592 return ret;
4594 location_t close_loc = next_tok->location;
4595 c_parser_consume_token (parser);
4596 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4597 obstack_free (&braced_init_obstack, NULL);
4598 set_c_expr_source_range (&ret, brace_loc, close_loc);
4599 return ret;
4602 /* Parse a nested initializer, including designators. */
4604 static void
4605 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4607 /* Parse any designator or designator list. A single array
4608 designator may have the subsequent "=" omitted in GNU C, but a
4609 longer list or a structure member designator may not. */
4610 if (c_parser_next_token_is (parser, CPP_NAME)
4611 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4613 /* Old-style structure member designator. */
4614 set_init_label (c_parser_peek_token (parser)->location,
4615 c_parser_peek_token (parser)->value,
4616 c_parser_peek_token (parser)->location,
4617 braced_init_obstack);
4618 /* Use the colon as the error location. */
4619 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4620 "obsolete use of designated initializer with %<:%>");
4621 c_parser_consume_token (parser);
4622 c_parser_consume_token (parser);
4624 else
4626 /* des_seen is 0 if there have been no designators, 1 if there
4627 has been a single array designator and 2 otherwise. */
4628 int des_seen = 0;
4629 /* Location of a designator. */
4630 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4631 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4632 || c_parser_next_token_is (parser, CPP_DOT))
4634 int des_prev = des_seen;
4635 if (!des_seen)
4636 des_loc = c_parser_peek_token (parser)->location;
4637 if (des_seen < 2)
4638 des_seen++;
4639 if (c_parser_next_token_is (parser, CPP_DOT))
4641 des_seen = 2;
4642 c_parser_consume_token (parser);
4643 if (c_parser_next_token_is (parser, CPP_NAME))
4645 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4646 c_parser_peek_token (parser)->location,
4647 braced_init_obstack);
4648 c_parser_consume_token (parser);
4650 else
4652 struct c_expr init;
4653 init.set_error ();
4654 init.original_code = ERROR_MARK;
4655 init.original_type = NULL;
4656 c_parser_error (parser, "expected identifier");
4657 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4658 process_init_element (input_location, init, false,
4659 braced_init_obstack);
4660 return;
4663 else
4665 tree first, second;
4666 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4667 location_t array_index_loc = UNKNOWN_LOCATION;
4668 /* ??? Following the old parser, [ objc-receiver
4669 objc-message-args ] is accepted as an initializer,
4670 being distinguished from a designator by what follows
4671 the first assignment expression inside the square
4672 brackets, but after a first array designator a
4673 subsequent square bracket is for Objective-C taken to
4674 start an expression, using the obsolete form of
4675 designated initializer without '=', rather than
4676 possibly being a second level of designation: in LALR
4677 terms, the '[' is shifted rather than reducing
4678 designator to designator-list. */
4679 if (des_prev == 1 && c_dialect_objc ())
4681 des_seen = des_prev;
4682 break;
4684 if (des_prev == 0 && c_dialect_objc ())
4686 /* This might be an array designator or an
4687 Objective-C message expression. If the former,
4688 continue parsing here; if the latter, parse the
4689 remainder of the initializer given the starting
4690 primary-expression. ??? It might make sense to
4691 distinguish when des_prev == 1 as well; see
4692 previous comment. */
4693 tree rec, args;
4694 struct c_expr mexpr;
4695 c_parser_consume_token (parser);
4696 if (c_parser_peek_token (parser)->type == CPP_NAME
4697 && ((c_parser_peek_token (parser)->id_kind
4698 == C_ID_TYPENAME)
4699 || (c_parser_peek_token (parser)->id_kind
4700 == C_ID_CLASSNAME)))
4702 /* Type name receiver. */
4703 tree id = c_parser_peek_token (parser)->value;
4704 c_parser_consume_token (parser);
4705 rec = objc_get_class_reference (id);
4706 goto parse_message_args;
4708 first = c_parser_expr_no_commas (parser, NULL).value;
4709 mark_exp_read (first);
4710 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4711 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4712 goto array_desig_after_first;
4713 /* Expression receiver. So far only one part
4714 without commas has been parsed; there might be
4715 more of the expression. */
4716 rec = first;
4717 while (c_parser_next_token_is (parser, CPP_COMMA))
4719 struct c_expr next;
4720 location_t comma_loc, exp_loc;
4721 comma_loc = c_parser_peek_token (parser)->location;
4722 c_parser_consume_token (parser);
4723 exp_loc = c_parser_peek_token (parser)->location;
4724 next = c_parser_expr_no_commas (parser, NULL);
4725 next = convert_lvalue_to_rvalue (exp_loc, next,
4726 true, true);
4727 rec = build_compound_expr (comma_loc, rec, next.value);
4729 parse_message_args:
4730 /* Now parse the objc-message-args. */
4731 args = c_parser_objc_message_args (parser);
4732 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4733 "expected %<]%>");
4734 mexpr.value
4735 = objc_build_message_expr (rec, args);
4736 mexpr.original_code = ERROR_MARK;
4737 mexpr.original_type = NULL;
4738 /* Now parse and process the remainder of the
4739 initializer, starting with this message
4740 expression as a primary-expression. */
4741 c_parser_initval (parser, &mexpr, braced_init_obstack);
4742 return;
4744 c_parser_consume_token (parser);
4745 array_index_loc = c_parser_peek_token (parser)->location;
4746 first = c_parser_expr_no_commas (parser, NULL).value;
4747 mark_exp_read (first);
4748 array_desig_after_first:
4749 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4751 ellipsis_loc = c_parser_peek_token (parser)->location;
4752 c_parser_consume_token (parser);
4753 second = c_parser_expr_no_commas (parser, NULL).value;
4754 mark_exp_read (second);
4756 else
4757 second = NULL_TREE;
4758 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4760 c_parser_consume_token (parser);
4761 set_init_index (array_index_loc, first, second,
4762 braced_init_obstack);
4763 if (second)
4764 pedwarn (ellipsis_loc, OPT_Wpedantic,
4765 "ISO C forbids specifying range of elements to initialize");
4767 else
4768 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4769 "expected %<]%>");
4772 if (des_seen >= 1)
4774 if (c_parser_next_token_is (parser, CPP_EQ))
4776 pedwarn_c90 (des_loc, OPT_Wpedantic,
4777 "ISO C90 forbids specifying subobject "
4778 "to initialize");
4779 c_parser_consume_token (parser);
4781 else
4783 if (des_seen == 1)
4784 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4785 "obsolete use of designated initializer without %<=%>");
4786 else
4788 struct c_expr init;
4789 init.set_error ();
4790 init.original_code = ERROR_MARK;
4791 init.original_type = NULL;
4792 c_parser_error (parser, "expected %<=%>");
4793 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4794 process_init_element (input_location, init, false,
4795 braced_init_obstack);
4796 return;
4801 c_parser_initval (parser, NULL, braced_init_obstack);
4804 /* Parse a nested initializer; as c_parser_initializer but parses
4805 initializers within braced lists, after any designators have been
4806 applied. If AFTER is not NULL then it is an Objective-C message
4807 expression which is the primary-expression starting the
4808 initializer. */
4810 static void
4811 c_parser_initval (c_parser *parser, struct c_expr *after,
4812 struct obstack * braced_init_obstack)
4814 struct c_expr init;
4815 gcc_assert (!after || c_dialect_objc ());
4816 location_t loc = c_parser_peek_token (parser)->location;
4818 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4819 init = c_parser_braced_init (parser, NULL_TREE, true,
4820 braced_init_obstack);
4821 else
4823 init = c_parser_expr_no_commas (parser, after);
4824 if (init.value != NULL_TREE
4825 && TREE_CODE (init.value) != STRING_CST
4826 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4827 init = convert_lvalue_to_rvalue (loc, init, true, true);
4829 process_init_element (loc, init, false, braced_init_obstack);
4832 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4833 C99 6.8.2, C11 6.8.2).
4835 compound-statement:
4836 { block-item-list[opt] }
4837 { label-declarations block-item-list }
4839 block-item-list:
4840 block-item
4841 block-item-list block-item
4843 block-item:
4844 nested-declaration
4845 statement
4847 nested-declaration:
4848 declaration
4850 GNU extensions:
4852 compound-statement:
4853 { label-declarations block-item-list }
4855 nested-declaration:
4856 __extension__ nested-declaration
4857 nested-function-definition
4859 label-declarations:
4860 label-declaration
4861 label-declarations label-declaration
4863 label-declaration:
4864 __label__ identifier-list ;
4866 Allowing the mixing of declarations and code is new in C99. The
4867 GNU syntax also permits (not shown above) labels at the end of
4868 compound statements, which yield an error. We don't allow labels
4869 on declarations; this might seem like a natural extension, but
4870 there would be a conflict between attributes on the label and
4871 prefix attributes on the declaration. ??? The syntax follows the
4872 old parser in requiring something after label declarations.
4873 Although they are erroneous if the labels declared aren't defined,
4874 is it useful for the syntax to be this way?
4876 OpenACC:
4878 block-item:
4879 openacc-directive
4881 openacc-directive:
4882 update-directive
4884 OpenMP:
4886 block-item:
4887 openmp-directive
4889 openmp-directive:
4890 barrier-directive
4891 flush-directive
4892 taskwait-directive
4893 taskyield-directive
4894 cancel-directive
4895 cancellation-point-directive */
4897 static tree
4898 c_parser_compound_statement (c_parser *parser)
4900 tree stmt;
4901 location_t brace_loc;
4902 brace_loc = c_parser_peek_token (parser)->location;
4903 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4905 /* Ensure a scope is entered and left anyway to avoid confusion
4906 if we have just prepared to enter a function body. */
4907 stmt = c_begin_compound_stmt (true);
4908 c_end_compound_stmt (brace_loc, stmt, true);
4909 return error_mark_node;
4911 stmt = c_begin_compound_stmt (true);
4912 c_parser_compound_statement_nostart (parser);
4914 return c_end_compound_stmt (brace_loc, stmt, true);
4917 /* Parse a compound statement except for the opening brace. This is
4918 used for parsing both compound statements and statement expressions
4919 (which follow different paths to handling the opening). */
4921 static void
4922 c_parser_compound_statement_nostart (c_parser *parser)
4924 bool last_stmt = false;
4925 bool last_label = false;
4926 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4927 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4928 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4930 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4931 c_parser_consume_token (parser);
4932 return;
4934 mark_valid_location_for_stdc_pragma (true);
4935 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4937 /* Read zero or more forward-declarations for labels that nested
4938 functions can jump to. */
4939 mark_valid_location_for_stdc_pragma (false);
4940 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4942 label_loc = c_parser_peek_token (parser)->location;
4943 c_parser_consume_token (parser);
4944 /* Any identifiers, including those declared as type names,
4945 are OK here. */
4946 while (true)
4948 tree label;
4949 if (c_parser_next_token_is_not (parser, CPP_NAME))
4951 c_parser_error (parser, "expected identifier");
4952 break;
4954 label
4955 = declare_label (c_parser_peek_token (parser)->value);
4956 C_DECLARED_LABEL_FLAG (label) = 1;
4957 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4958 c_parser_consume_token (parser);
4959 if (c_parser_next_token_is (parser, CPP_COMMA))
4960 c_parser_consume_token (parser);
4961 else
4962 break;
4964 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4966 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4968 /* We must now have at least one statement, label or declaration. */
4969 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4971 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4972 c_parser_error (parser, "expected declaration or statement");
4973 c_parser_consume_token (parser);
4974 return;
4976 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4978 location_t loc = c_parser_peek_token (parser)->location;
4979 loc = expansion_point_location_if_in_system_header (loc);
4980 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4981 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4982 || (c_parser_next_token_is (parser, CPP_NAME)
4983 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4985 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4986 label_loc = c_parser_peek_2nd_token (parser)->location;
4987 else
4988 label_loc = c_parser_peek_token (parser)->location;
4989 last_label = true;
4990 last_stmt = false;
4991 mark_valid_location_for_stdc_pragma (false);
4992 c_parser_label (parser);
4994 else if (!last_label
4995 && c_parser_next_tokens_start_declaration (parser))
4997 last_label = false;
4998 mark_valid_location_for_stdc_pragma (false);
4999 bool fallthru_attr_p = false;
5000 c_parser_declaration_or_fndef (parser, true, true, true, true,
5001 true, NULL, vNULL, NULL,
5002 &fallthru_attr_p);
5003 if (last_stmt && !fallthru_attr_p)
5004 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5005 "ISO C90 forbids mixed declarations and code");
5006 last_stmt = fallthru_attr_p;
5008 else if (!last_label
5009 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5011 /* __extension__ can start a declaration, but is also an
5012 unary operator that can start an expression. Consume all
5013 but the last of a possible series of __extension__ to
5014 determine which. */
5015 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5016 && (c_parser_peek_2nd_token (parser)->keyword
5017 == RID_EXTENSION))
5018 c_parser_consume_token (parser);
5019 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5021 int ext;
5022 ext = disable_extension_diagnostics ();
5023 c_parser_consume_token (parser);
5024 last_label = false;
5025 mark_valid_location_for_stdc_pragma (false);
5026 c_parser_declaration_or_fndef (parser, true, true, true, true,
5027 true, NULL, vNULL);
5028 /* Following the old parser, __extension__ does not
5029 disable this diagnostic. */
5030 restore_extension_diagnostics (ext);
5031 if (last_stmt)
5032 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5033 "ISO C90 forbids mixed declarations and code");
5034 last_stmt = false;
5036 else
5037 goto statement;
5039 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5041 /* External pragmas, and some omp pragmas, are not associated
5042 with regular c code, and so are not to be considered statements
5043 syntactically. This ensures that the user doesn't put them
5044 places that would turn into syntax errors if the directive
5045 were ignored. */
5046 if (c_parser_pragma (parser,
5047 last_label ? pragma_stmt : pragma_compound,
5048 NULL))
5049 last_label = false, last_stmt = true;
5051 else if (c_parser_next_token_is (parser, CPP_EOF))
5053 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5054 c_parser_error (parser, "expected declaration or statement");
5055 return;
5057 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5059 if (parser->in_if_block)
5061 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5062 error_at (loc, "expected %<}%> before %<else%>");
5063 return;
5065 else
5067 error_at (loc, "%<else%> without a previous %<if%>");
5068 c_parser_consume_token (parser);
5069 continue;
5072 else
5074 statement:
5075 last_label = false;
5076 last_stmt = true;
5077 mark_valid_location_for_stdc_pragma (false);
5078 c_parser_statement_after_labels (parser, NULL);
5081 parser->error = false;
5083 if (last_label)
5084 error_at (label_loc, "label at end of compound statement");
5085 c_parser_consume_token (parser);
5086 /* Restore the value we started with. */
5087 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5090 /* Parse all consecutive labels. */
5092 static void
5093 c_parser_all_labels (c_parser *parser)
5095 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5096 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5097 || (c_parser_next_token_is (parser, CPP_NAME)
5098 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5099 c_parser_label (parser);
5102 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5104 label:
5105 identifier : attributes[opt]
5106 case constant-expression :
5107 default :
5109 GNU extensions:
5111 label:
5112 case constant-expression ... constant-expression :
5114 The use of attributes on labels is a GNU extension. The syntax in
5115 GNU C accepts any expressions without commas, non-constant
5116 expressions being rejected later. */
5118 static void
5119 c_parser_label (c_parser *parser)
5121 location_t loc1 = c_parser_peek_token (parser)->location;
5122 tree label = NULL_TREE;
5124 /* Remember whether this case or a user-defined label is allowed to fall
5125 through to. */
5126 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5128 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5130 tree exp1, exp2;
5131 c_parser_consume_token (parser);
5132 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5133 if (c_parser_next_token_is (parser, CPP_COLON))
5135 c_parser_consume_token (parser);
5136 label = do_case (loc1, exp1, NULL_TREE);
5138 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5140 c_parser_consume_token (parser);
5141 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5142 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5143 label = do_case (loc1, exp1, exp2);
5145 else
5146 c_parser_error (parser, "expected %<:%> or %<...%>");
5148 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5150 c_parser_consume_token (parser);
5151 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5152 label = do_case (loc1, NULL_TREE, NULL_TREE);
5154 else
5156 tree name = c_parser_peek_token (parser)->value;
5157 tree tlab;
5158 tree attrs;
5159 location_t loc2 = c_parser_peek_token (parser)->location;
5160 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5161 c_parser_consume_token (parser);
5162 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5163 c_parser_consume_token (parser);
5164 attrs = c_parser_attributes (parser);
5165 tlab = define_label (loc2, name);
5166 if (tlab)
5168 decl_attributes (&tlab, attrs, 0);
5169 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5172 if (label)
5174 if (TREE_CODE (label) == LABEL_EXPR)
5175 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5176 else
5177 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5179 /* Allow '__attribute__((fallthrough));'. */
5180 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5182 location_t loc = c_parser_peek_token (parser)->location;
5183 tree attrs = c_parser_attributes (parser);
5184 if (attribute_fallthrough_p (attrs))
5186 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5188 tree fn = build_call_expr_internal_loc (loc,
5189 IFN_FALLTHROUGH,
5190 void_type_node, 0);
5191 add_stmt (fn);
5193 else
5194 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5195 "not followed by %<;%>");
5197 else if (attrs != NULL_TREE)
5198 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5199 " can be applied to a null statement");
5201 if (c_parser_next_tokens_start_declaration (parser))
5203 error_at (c_parser_peek_token (parser)->location,
5204 "a label can only be part of a statement and "
5205 "a declaration is not a statement");
5206 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5207 /*static_assert_ok*/ true,
5208 /*empty_ok*/ true, /*nested*/ true,
5209 /*start_attr_ok*/ true, NULL,
5210 vNULL);
5215 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5217 statement:
5218 labeled-statement
5219 compound-statement
5220 expression-statement
5221 selection-statement
5222 iteration-statement
5223 jump-statement
5225 labeled-statement:
5226 label statement
5228 expression-statement:
5229 expression[opt] ;
5231 selection-statement:
5232 if-statement
5233 switch-statement
5235 iteration-statement:
5236 while-statement
5237 do-statement
5238 for-statement
5240 jump-statement:
5241 goto identifier ;
5242 continue ;
5243 break ;
5244 return expression[opt] ;
5246 GNU extensions:
5248 statement:
5249 asm-statement
5251 jump-statement:
5252 goto * expression ;
5254 expression-statement:
5255 attributes ;
5257 Objective-C:
5259 statement:
5260 objc-throw-statement
5261 objc-try-catch-statement
5262 objc-synchronized-statement
5264 objc-throw-statement:
5265 @throw expression ;
5266 @throw ;
5268 OpenACC:
5270 statement:
5271 openacc-construct
5273 openacc-construct:
5274 parallel-construct
5275 kernels-construct
5276 data-construct
5277 loop-construct
5279 parallel-construct:
5280 parallel-directive structured-block
5282 kernels-construct:
5283 kernels-directive structured-block
5285 data-construct:
5286 data-directive structured-block
5288 loop-construct:
5289 loop-directive structured-block
5291 OpenMP:
5293 statement:
5294 openmp-construct
5296 openmp-construct:
5297 parallel-construct
5298 for-construct
5299 simd-construct
5300 for-simd-construct
5301 sections-construct
5302 single-construct
5303 parallel-for-construct
5304 parallel-for-simd-construct
5305 parallel-sections-construct
5306 master-construct
5307 critical-construct
5308 atomic-construct
5309 ordered-construct
5311 parallel-construct:
5312 parallel-directive structured-block
5314 for-construct:
5315 for-directive iteration-statement
5317 simd-construct:
5318 simd-directive iteration-statements
5320 for-simd-construct:
5321 for-simd-directive iteration-statements
5323 sections-construct:
5324 sections-directive section-scope
5326 single-construct:
5327 single-directive structured-block
5329 parallel-for-construct:
5330 parallel-for-directive iteration-statement
5332 parallel-for-simd-construct:
5333 parallel-for-simd-directive iteration-statement
5335 parallel-sections-construct:
5336 parallel-sections-directive section-scope
5338 master-construct:
5339 master-directive structured-block
5341 critical-construct:
5342 critical-directive structured-block
5344 atomic-construct:
5345 atomic-directive expression-statement
5347 ordered-construct:
5348 ordered-directive structured-block
5350 Transactional Memory:
5352 statement:
5353 transaction-statement
5354 transaction-cancel-statement
5356 IF_P is used to track whether there's a (possibly labeled) if statement
5357 which is not enclosed in braces and has an else clause. This is used to
5358 implement -Wparentheses. */
5360 static void
5361 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5363 c_parser_all_labels (parser);
5364 if (loc_after_labels)
5365 *loc_after_labels = c_parser_peek_token (parser)->location;
5366 c_parser_statement_after_labels (parser, if_p, NULL);
5369 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5370 of if-else-if conditions.
5372 IF_P is used to track whether there's a (possibly labeled) if statement
5373 which is not enclosed in braces and has an else clause. This is used to
5374 implement -Wparentheses. */
5376 static void
5377 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5378 vec<tree> *chain)
5380 location_t loc = c_parser_peek_token (parser)->location;
5381 tree stmt = NULL_TREE;
5382 bool in_if_block = parser->in_if_block;
5383 parser->in_if_block = false;
5384 if (if_p != NULL)
5385 *if_p = false;
5387 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5388 add_debug_begin_stmt (loc);
5390 switch (c_parser_peek_token (parser)->type)
5392 case CPP_OPEN_BRACE:
5393 add_stmt (c_parser_compound_statement (parser));
5394 break;
5395 case CPP_KEYWORD:
5396 switch (c_parser_peek_token (parser)->keyword)
5398 case RID_IF:
5399 c_parser_if_statement (parser, if_p, chain);
5400 break;
5401 case RID_SWITCH:
5402 c_parser_switch_statement (parser, if_p);
5403 break;
5404 case RID_WHILE:
5405 c_parser_while_statement (parser, false, 0, if_p);
5406 break;
5407 case RID_DO:
5408 c_parser_do_statement (parser, 0, false);
5409 break;
5410 case RID_FOR:
5411 c_parser_for_statement (parser, false, 0, if_p);
5412 break;
5413 case RID_GOTO:
5414 c_parser_consume_token (parser);
5415 if (c_parser_next_token_is (parser, CPP_NAME))
5417 stmt = c_finish_goto_label (loc,
5418 c_parser_peek_token (parser)->value);
5419 c_parser_consume_token (parser);
5421 else if (c_parser_next_token_is (parser, CPP_MULT))
5423 struct c_expr val;
5425 c_parser_consume_token (parser);
5426 val = c_parser_expression (parser);
5427 val = convert_lvalue_to_rvalue (loc, val, false, true);
5428 stmt = c_finish_goto_ptr (loc, val.value);
5430 else
5431 c_parser_error (parser, "expected identifier or %<*%>");
5432 goto expect_semicolon;
5433 case RID_CONTINUE:
5434 c_parser_consume_token (parser);
5435 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5436 goto expect_semicolon;
5437 case RID_BREAK:
5438 c_parser_consume_token (parser);
5439 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5440 goto expect_semicolon;
5441 case RID_RETURN:
5442 c_parser_consume_token (parser);
5443 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5445 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5446 c_parser_consume_token (parser);
5448 else
5450 location_t xloc = c_parser_peek_token (parser)->location;
5451 struct c_expr expr = c_parser_expression_conv (parser);
5452 mark_exp_read (expr.value);
5453 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5454 expr.value, expr.original_type);
5455 goto expect_semicolon;
5457 break;
5458 case RID_ASM:
5459 stmt = c_parser_asm_statement (parser);
5460 break;
5461 case RID_TRANSACTION_ATOMIC:
5462 case RID_TRANSACTION_RELAXED:
5463 stmt = c_parser_transaction (parser,
5464 c_parser_peek_token (parser)->keyword);
5465 break;
5466 case RID_TRANSACTION_CANCEL:
5467 stmt = c_parser_transaction_cancel (parser);
5468 goto expect_semicolon;
5469 case RID_AT_THROW:
5470 gcc_assert (c_dialect_objc ());
5471 c_parser_consume_token (parser);
5472 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5474 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5475 c_parser_consume_token (parser);
5477 else
5479 struct c_expr expr = c_parser_expression (parser);
5480 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5481 expr.value = c_fully_fold (expr.value, false, NULL);
5482 stmt = objc_build_throw_stmt (loc, expr.value);
5483 goto expect_semicolon;
5485 break;
5486 case RID_AT_TRY:
5487 gcc_assert (c_dialect_objc ());
5488 c_parser_objc_try_catch_finally_statement (parser);
5489 break;
5490 case RID_AT_SYNCHRONIZED:
5491 gcc_assert (c_dialect_objc ());
5492 c_parser_objc_synchronized_statement (parser);
5493 break;
5494 case RID_ATTRIBUTE:
5496 /* Allow '__attribute__((fallthrough));'. */
5497 tree attrs = c_parser_attributes (parser);
5498 if (attribute_fallthrough_p (attrs))
5500 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5502 tree fn = build_call_expr_internal_loc (loc,
5503 IFN_FALLTHROUGH,
5504 void_type_node, 0);
5505 add_stmt (fn);
5506 /* Eat the ';'. */
5507 c_parser_consume_token (parser);
5509 else
5510 warning_at (loc, OPT_Wattributes,
5511 "%<fallthrough%> attribute not followed "
5512 "by %<;%>");
5514 else if (attrs != NULL_TREE)
5515 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5516 " can be applied to a null statement");
5517 break;
5519 default:
5520 goto expr_stmt;
5522 break;
5523 case CPP_SEMICOLON:
5524 c_parser_consume_token (parser);
5525 break;
5526 case CPP_CLOSE_PAREN:
5527 case CPP_CLOSE_SQUARE:
5528 /* Avoid infinite loop in error recovery:
5529 c_parser_skip_until_found stops at a closing nesting
5530 delimiter without consuming it, but here we need to consume
5531 it to proceed further. */
5532 c_parser_error (parser, "expected statement");
5533 c_parser_consume_token (parser);
5534 break;
5535 case CPP_PRAGMA:
5536 c_parser_pragma (parser, pragma_stmt, if_p);
5537 break;
5538 default:
5539 expr_stmt:
5540 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5541 expect_semicolon:
5542 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5543 break;
5545 /* Two cases cannot and do not have line numbers associated: If stmt
5546 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5547 cannot hold line numbers. But that's OK because the statement
5548 will either be changed to a MODIFY_EXPR during gimplification of
5549 the statement expr, or discarded. If stmt was compound, but
5550 without new variables, we will have skipped the creation of a
5551 BIND and will have a bare STATEMENT_LIST. But that's OK because
5552 (recursively) all of the component statements should already have
5553 line numbers assigned. ??? Can we discard no-op statements
5554 earlier? */
5555 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5556 protected_set_expr_location (stmt, loc);
5558 parser->in_if_block = in_if_block;
5561 /* Parse the condition from an if, do, while or for statements. */
5563 static tree
5564 c_parser_condition (c_parser *parser)
5566 location_t loc = c_parser_peek_token (parser)->location;
5567 tree cond;
5568 cond = c_parser_expression_conv (parser).value;
5569 cond = c_objc_common_truthvalue_conversion (loc, cond);
5570 cond = c_fully_fold (cond, false, NULL);
5571 if (warn_sequence_point)
5572 verify_sequence_points (cond);
5573 return cond;
5576 /* Parse a parenthesized condition from an if, do or while statement.
5578 condition:
5579 ( expression )
5581 static tree
5582 c_parser_paren_condition (c_parser *parser)
5584 tree cond;
5585 matching_parens parens;
5586 if (!parens.require_open (parser))
5587 return error_mark_node;
5588 cond = c_parser_condition (parser);
5589 parens.skip_until_found_close (parser);
5590 return cond;
5593 /* Parse a statement which is a block in C99.
5595 IF_P is used to track whether there's a (possibly labeled) if statement
5596 which is not enclosed in braces and has an else clause. This is used to
5597 implement -Wparentheses. */
5599 static tree
5600 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5601 location_t *loc_after_labels)
5603 tree block = c_begin_compound_stmt (flag_isoc99);
5604 location_t loc = c_parser_peek_token (parser)->location;
5605 c_parser_statement (parser, if_p, loc_after_labels);
5606 return c_end_compound_stmt (loc, block, flag_isoc99);
5609 /* Parse the body of an if statement. This is just parsing a
5610 statement but (a) it is a block in C99, (b) we track whether the
5611 body is an if statement for the sake of -Wparentheses warnings, (c)
5612 we handle an empty body specially for the sake of -Wempty-body
5613 warnings, and (d) we call parser_compound_statement directly
5614 because c_parser_statement_after_labels resets
5615 parser->in_if_block.
5617 IF_P is used to track whether there's a (possibly labeled) if statement
5618 which is not enclosed in braces and has an else clause. This is used to
5619 implement -Wparentheses. */
5621 static tree
5622 c_parser_if_body (c_parser *parser, bool *if_p,
5623 const token_indent_info &if_tinfo)
5625 tree block = c_begin_compound_stmt (flag_isoc99);
5626 location_t body_loc = c_parser_peek_token (parser)->location;
5627 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5628 token_indent_info body_tinfo
5629 = get_token_indent_info (c_parser_peek_token (parser));
5631 c_parser_all_labels (parser);
5632 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5634 location_t loc = c_parser_peek_token (parser)->location;
5635 add_stmt (build_empty_stmt (loc));
5636 c_parser_consume_token (parser);
5637 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5638 warning_at (loc, OPT_Wempty_body,
5639 "suggest braces around empty body in an %<if%> statement");
5641 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5642 add_stmt (c_parser_compound_statement (parser));
5643 else
5645 body_loc_after_labels = c_parser_peek_token (parser)->location;
5646 c_parser_statement_after_labels (parser, if_p);
5649 token_indent_info next_tinfo
5650 = get_token_indent_info (c_parser_peek_token (parser));
5651 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5652 if (body_loc_after_labels != UNKNOWN_LOCATION
5653 && next_tinfo.type != CPP_SEMICOLON)
5654 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5655 if_tinfo.location, RID_IF);
5657 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5660 /* Parse the else body of an if statement. This is just parsing a
5661 statement but (a) it is a block in C99, (b) we handle an empty body
5662 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5663 of if-else-if conditions. */
5665 static tree
5666 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5667 vec<tree> *chain)
5669 location_t body_loc = c_parser_peek_token (parser)->location;
5670 tree block = c_begin_compound_stmt (flag_isoc99);
5671 token_indent_info body_tinfo
5672 = get_token_indent_info (c_parser_peek_token (parser));
5673 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5675 c_parser_all_labels (parser);
5676 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5678 location_t loc = c_parser_peek_token (parser)->location;
5679 warning_at (loc,
5680 OPT_Wempty_body,
5681 "suggest braces around empty body in an %<else%> statement");
5682 add_stmt (build_empty_stmt (loc));
5683 c_parser_consume_token (parser);
5685 else
5687 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5688 body_loc_after_labels = c_parser_peek_token (parser)->location;
5689 c_parser_statement_after_labels (parser, NULL, chain);
5692 token_indent_info next_tinfo
5693 = get_token_indent_info (c_parser_peek_token (parser));
5694 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5695 if (body_loc_after_labels != UNKNOWN_LOCATION
5696 && next_tinfo.type != CPP_SEMICOLON)
5697 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5698 else_tinfo.location, RID_ELSE);
5700 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5703 /* We might need to reclassify any previously-lexed identifier, e.g.
5704 when we've left a for loop with an if-statement without else in the
5705 body - we might have used a wrong scope for the token. See PR67784. */
5707 static void
5708 c_parser_maybe_reclassify_token (c_parser *parser)
5710 if (c_parser_next_token_is (parser, CPP_NAME))
5712 c_token *token = c_parser_peek_token (parser);
5714 if (token->id_kind != C_ID_CLASSNAME)
5716 tree decl = lookup_name (token->value);
5718 token->id_kind = C_ID_ID;
5719 if (decl)
5721 if (TREE_CODE (decl) == TYPE_DECL)
5722 token->id_kind = C_ID_TYPENAME;
5724 else if (c_dialect_objc ())
5726 tree objc_interface_decl = objc_is_class_name (token->value);
5727 /* Objective-C class names are in the same namespace as
5728 variables and typedefs, and hence are shadowed by local
5729 declarations. */
5730 if (objc_interface_decl)
5732 token->value = objc_interface_decl;
5733 token->id_kind = C_ID_CLASSNAME;
5740 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5742 if-statement:
5743 if ( expression ) statement
5744 if ( expression ) statement else statement
5746 CHAIN is a vector of if-else-if conditions.
5747 IF_P is used to track whether there's a (possibly labeled) if statement
5748 which is not enclosed in braces and has an else clause. This is used to
5749 implement -Wparentheses. */
5751 static void
5752 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5754 tree block;
5755 location_t loc;
5756 tree cond;
5757 bool nested_if = false;
5758 tree first_body, second_body;
5759 bool in_if_block;
5761 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5762 token_indent_info if_tinfo
5763 = get_token_indent_info (c_parser_peek_token (parser));
5764 c_parser_consume_token (parser);
5765 block = c_begin_compound_stmt (flag_isoc99);
5766 loc = c_parser_peek_token (parser)->location;
5767 cond = c_parser_paren_condition (parser);
5768 in_if_block = parser->in_if_block;
5769 parser->in_if_block = true;
5770 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5771 parser->in_if_block = in_if_block;
5773 if (warn_duplicated_cond)
5774 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5776 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5778 token_indent_info else_tinfo
5779 = get_token_indent_info (c_parser_peek_token (parser));
5780 c_parser_consume_token (parser);
5781 if (warn_duplicated_cond)
5783 if (c_parser_next_token_is_keyword (parser, RID_IF)
5784 && chain == NULL)
5786 /* We've got "if (COND) else if (COND2)". Start the
5787 condition chain and add COND as the first element. */
5788 chain = new vec<tree> ();
5789 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5790 chain->safe_push (cond);
5792 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5794 /* This is if-else without subsequent if. Zap the condition
5795 chain; we would have already warned at this point. */
5796 delete chain;
5797 chain = NULL;
5800 second_body = c_parser_else_body (parser, else_tinfo, chain);
5801 /* Set IF_P to true to indicate that this if statement has an
5802 else clause. This may trigger the Wparentheses warning
5803 below when we get back up to the parent if statement. */
5804 if (if_p != NULL)
5805 *if_p = true;
5807 else
5809 second_body = NULL_TREE;
5811 /* Diagnose an ambiguous else if if-then-else is nested inside
5812 if-then. */
5813 if (nested_if)
5814 warning_at (loc, OPT_Wdangling_else,
5815 "suggest explicit braces to avoid ambiguous %<else%>");
5817 if (warn_duplicated_cond)
5819 /* This if statement does not have an else clause. We don't
5820 need the condition chain anymore. */
5821 delete chain;
5822 chain = NULL;
5825 c_finish_if_stmt (loc, cond, first_body, second_body);
5826 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5828 c_parser_maybe_reclassify_token (parser);
5831 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5833 switch-statement:
5834 switch (expression) statement
5837 static void
5838 c_parser_switch_statement (c_parser *parser, bool *if_p)
5840 struct c_expr ce;
5841 tree block, expr, body, save_break;
5842 location_t switch_loc = c_parser_peek_token (parser)->location;
5843 location_t switch_cond_loc;
5844 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5845 c_parser_consume_token (parser);
5846 block = c_begin_compound_stmt (flag_isoc99);
5847 bool explicit_cast_p = false;
5848 matching_parens parens;
5849 if (parens.require_open (parser))
5851 switch_cond_loc = c_parser_peek_token (parser)->location;
5852 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5853 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5854 explicit_cast_p = true;
5855 ce = c_parser_expression (parser);
5856 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5857 expr = ce.value;
5858 /* ??? expr has no valid location? */
5859 parens.skip_until_found_close (parser);
5861 else
5863 switch_cond_loc = UNKNOWN_LOCATION;
5864 expr = error_mark_node;
5865 ce.original_type = error_mark_node;
5867 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5868 save_break = c_break_label;
5869 c_break_label = NULL_TREE;
5870 location_t loc_after_labels;
5871 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5872 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5873 location_t next_loc = c_parser_peek_token (parser)->location;
5874 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5875 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5876 RID_SWITCH);
5877 if (c_break_label)
5879 location_t here = c_parser_peek_token (parser)->location;
5880 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5881 SET_EXPR_LOCATION (t, here);
5882 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5883 append_to_statement_list_force (t, &body);
5885 c_finish_case (body, ce.original_type);
5886 c_break_label = save_break;
5887 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5888 c_parser_maybe_reclassify_token (parser);
5891 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5893 while-statement:
5894 while (expression) statement
5896 IF_P is used to track whether there's a (possibly labeled) if statement
5897 which is not enclosed in braces and has an else clause. This is used to
5898 implement -Wparentheses. */
5900 static void
5901 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5902 bool *if_p)
5904 tree block, cond, body, save_break, save_cont;
5905 location_t loc;
5906 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5907 token_indent_info while_tinfo
5908 = get_token_indent_info (c_parser_peek_token (parser));
5909 c_parser_consume_token (parser);
5910 block = c_begin_compound_stmt (flag_isoc99);
5911 loc = c_parser_peek_token (parser)->location;
5912 cond = c_parser_paren_condition (parser);
5913 if (ivdep && cond != error_mark_node)
5914 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5915 build_int_cst (integer_type_node,
5916 annot_expr_ivdep_kind),
5917 integer_zero_node);
5918 if (unroll && cond != error_mark_node)
5919 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5920 build_int_cst (integer_type_node,
5921 annot_expr_unroll_kind),
5922 build_int_cst (integer_type_node, unroll));
5923 save_break = c_break_label;
5924 c_break_label = NULL_TREE;
5925 save_cont = c_cont_label;
5926 c_cont_label = NULL_TREE;
5928 token_indent_info body_tinfo
5929 = get_token_indent_info (c_parser_peek_token (parser));
5931 location_t loc_after_labels;
5932 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5933 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5934 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5935 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5936 c_parser_maybe_reclassify_token (parser);
5938 token_indent_info next_tinfo
5939 = get_token_indent_info (c_parser_peek_token (parser));
5940 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5942 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5943 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5944 while_tinfo.location, RID_WHILE);
5946 c_break_label = save_break;
5947 c_cont_label = save_cont;
5950 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5952 do-statement:
5953 do statement while ( expression ) ;
5956 static void
5957 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
5959 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5960 location_t loc;
5961 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5962 c_parser_consume_token (parser);
5963 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5964 warning_at (c_parser_peek_token (parser)->location,
5965 OPT_Wempty_body,
5966 "suggest braces around empty body in %<do%> statement");
5967 block = c_begin_compound_stmt (flag_isoc99);
5968 loc = c_parser_peek_token (parser)->location;
5969 save_break = c_break_label;
5970 c_break_label = NULL_TREE;
5971 save_cont = c_cont_label;
5972 c_cont_label = NULL_TREE;
5973 body = c_parser_c99_block_statement (parser, NULL);
5974 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5975 new_break = c_break_label;
5976 c_break_label = save_break;
5977 new_cont = c_cont_label;
5978 c_cont_label = save_cont;
5979 cond = c_parser_paren_condition (parser);
5980 if (ivdep && cond != error_mark_node)
5981 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5982 build_int_cst (integer_type_node,
5983 annot_expr_ivdep_kind),
5984 integer_zero_node);
5985 if (unroll && cond != error_mark_node)
5986 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5987 build_int_cst (integer_type_node,
5988 annot_expr_unroll_kind),
5989 build_int_cst (integer_type_node, unroll));
5990 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5991 c_parser_skip_to_end_of_block_or_statement (parser);
5992 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5993 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5996 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5998 for-statement:
5999 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6000 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6002 The form with a declaration is new in C99.
6004 ??? In accordance with the old parser, the declaration may be a
6005 nested function, which is then rejected in check_for_loop_decls,
6006 but does it make any sense for this to be included in the grammar?
6007 Note in particular that the nested function does not include a
6008 trailing ';', whereas the "declaration" production includes one.
6009 Also, can we reject bad declarations earlier and cheaper than
6010 check_for_loop_decls?
6012 In Objective-C, there are two additional variants:
6014 foreach-statement:
6015 for ( expression in expresssion ) statement
6016 for ( declaration in expression ) statement
6018 This is inconsistent with C, because the second variant is allowed
6019 even if c99 is not enabled.
6021 The rest of the comment documents these Objective-C foreach-statement.
6023 Here is the canonical example of the first variant:
6024 for (object in array) { do something with object }
6025 we call the first expression ("object") the "object_expression" and
6026 the second expression ("array") the "collection_expression".
6027 object_expression must be an lvalue of type "id" (a generic Objective-C
6028 object) because the loop works by assigning to object_expression the
6029 various objects from the collection_expression. collection_expression
6030 must evaluate to something of type "id" which responds to the method
6031 countByEnumeratingWithState:objects:count:.
6033 The canonical example of the second variant is:
6034 for (id object in array) { do something with object }
6035 which is completely equivalent to
6037 id object;
6038 for (object in array) { do something with object }
6040 Note that initizializing 'object' in some way (eg, "for ((object =
6041 xxx) in array) { do something with object }") is possibly
6042 technically valid, but completely pointless as 'object' will be
6043 assigned to something else as soon as the loop starts. We should
6044 most likely reject it (TODO).
6046 The beginning of the Objective-C foreach-statement looks exactly
6047 like the beginning of the for-statement, and we can tell it is a
6048 foreach-statement only because the initial declaration or
6049 expression is terminated by 'in' instead of ';'.
6051 IF_P is used to track whether there's a (possibly labeled) if statement
6052 which is not enclosed in braces and has an else clause. This is used to
6053 implement -Wparentheses. */
6055 static void
6056 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6057 bool *if_p)
6059 tree block, cond, incr, save_break, save_cont, body;
6060 /* The following are only used when parsing an ObjC foreach statement. */
6061 tree object_expression;
6062 /* Silence the bogus uninitialized warning. */
6063 tree collection_expression = NULL;
6064 location_t loc = c_parser_peek_token (parser)->location;
6065 location_t for_loc = c_parser_peek_token (parser)->location;
6066 bool is_foreach_statement = false;
6067 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6068 token_indent_info for_tinfo
6069 = get_token_indent_info (c_parser_peek_token (parser));
6070 c_parser_consume_token (parser);
6071 /* Open a compound statement in Objective-C as well, just in case this is
6072 as foreach expression. */
6073 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6074 cond = error_mark_node;
6075 incr = error_mark_node;
6076 matching_parens parens;
6077 if (parens.require_open (parser))
6079 /* Parse the initialization declaration or expression. */
6080 object_expression = error_mark_node;
6081 parser->objc_could_be_foreach_context = c_dialect_objc ();
6082 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6084 parser->objc_could_be_foreach_context = false;
6085 c_parser_consume_token (parser);
6086 c_finish_expr_stmt (loc, NULL_TREE);
6088 else if (c_parser_next_tokens_start_declaration (parser))
6090 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6091 &object_expression, vNULL);
6092 parser->objc_could_be_foreach_context = false;
6094 if (c_parser_next_token_is_keyword (parser, RID_IN))
6096 c_parser_consume_token (parser);
6097 is_foreach_statement = true;
6098 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6099 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6101 else
6102 check_for_loop_decls (for_loc, flag_isoc99);
6104 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6106 /* __extension__ can start a declaration, but is also an
6107 unary operator that can start an expression. Consume all
6108 but the last of a possible series of __extension__ to
6109 determine which. */
6110 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6111 && (c_parser_peek_2nd_token (parser)->keyword
6112 == RID_EXTENSION))
6113 c_parser_consume_token (parser);
6114 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6116 int ext;
6117 ext = disable_extension_diagnostics ();
6118 c_parser_consume_token (parser);
6119 c_parser_declaration_or_fndef (parser, true, true, true, true,
6120 true, &object_expression, vNULL);
6121 parser->objc_could_be_foreach_context = false;
6123 restore_extension_diagnostics (ext);
6124 if (c_parser_next_token_is_keyword (parser, RID_IN))
6126 c_parser_consume_token (parser);
6127 is_foreach_statement = true;
6128 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6129 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6131 else
6132 check_for_loop_decls (for_loc, flag_isoc99);
6134 else
6135 goto init_expr;
6137 else
6139 init_expr:
6141 struct c_expr ce;
6142 tree init_expression;
6143 ce = c_parser_expression (parser);
6144 init_expression = ce.value;
6145 parser->objc_could_be_foreach_context = false;
6146 if (c_parser_next_token_is_keyword (parser, RID_IN))
6148 c_parser_consume_token (parser);
6149 is_foreach_statement = true;
6150 if (! lvalue_p (init_expression))
6151 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6152 object_expression = c_fully_fold (init_expression, false, NULL);
6154 else
6156 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6157 init_expression = ce.value;
6158 c_finish_expr_stmt (loc, init_expression);
6159 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6163 /* Parse the loop condition. In the case of a foreach
6164 statement, there is no loop condition. */
6165 gcc_assert (!parser->objc_could_be_foreach_context);
6166 if (!is_foreach_statement)
6168 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6170 if (ivdep)
6172 c_parser_error (parser, "missing loop condition in loop with "
6173 "%<GCC ivdep%> pragma");
6174 cond = error_mark_node;
6176 else if (unroll)
6178 c_parser_error (parser, "missing loop condition in loop with "
6179 "%<GCC unroll%> pragma");
6180 cond = error_mark_node;
6182 else
6184 c_parser_consume_token (parser);
6185 cond = NULL_TREE;
6188 else
6190 cond = c_parser_condition (parser);
6191 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6192 "expected %<;%>");
6194 if (ivdep && cond != error_mark_node)
6195 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6196 build_int_cst (integer_type_node,
6197 annot_expr_ivdep_kind),
6198 integer_zero_node);
6199 if (unroll && cond != error_mark_node)
6200 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6201 build_int_cst (integer_type_node,
6202 annot_expr_unroll_kind),
6203 build_int_cst (integer_type_node, unroll));
6205 /* Parse the increment expression (the third expression in a
6206 for-statement). In the case of a foreach-statement, this is
6207 the expression that follows the 'in'. */
6208 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6210 if (is_foreach_statement)
6212 c_parser_error (parser, "missing collection in fast enumeration");
6213 collection_expression = error_mark_node;
6215 else
6216 incr = c_process_expr_stmt (loc, NULL_TREE);
6218 else
6220 if (is_foreach_statement)
6221 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6222 false, NULL);
6223 else
6225 struct c_expr ce = c_parser_expression (parser);
6226 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6227 incr = c_process_expr_stmt (loc, ce.value);
6230 parens.skip_until_found_close (parser);
6232 save_break = c_break_label;
6233 c_break_label = NULL_TREE;
6234 save_cont = c_cont_label;
6235 c_cont_label = NULL_TREE;
6237 token_indent_info body_tinfo
6238 = get_token_indent_info (c_parser_peek_token (parser));
6240 location_t loc_after_labels;
6241 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6242 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6244 if (is_foreach_statement)
6245 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6246 else
6247 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6248 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6249 c_parser_maybe_reclassify_token (parser);
6251 token_indent_info next_tinfo
6252 = get_token_indent_info (c_parser_peek_token (parser));
6253 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6255 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6256 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6257 for_tinfo.location, RID_FOR);
6259 c_break_label = save_break;
6260 c_cont_label = save_cont;
6263 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6264 statement with inputs, outputs, clobbers, and volatile tag
6265 allowed.
6267 asm-statement:
6268 asm type-qualifier[opt] ( asm-argument ) ;
6269 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6271 asm-argument:
6272 asm-string-literal
6273 asm-string-literal : asm-operands[opt]
6274 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6275 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6277 asm-goto-argument:
6278 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6279 : asm-goto-operands
6281 Qualifiers other than volatile are accepted in the syntax but
6282 warned for. */
6284 static tree
6285 c_parser_asm_statement (c_parser *parser)
6287 tree quals, str, outputs, inputs, clobbers, labels, ret;
6288 bool simple, is_goto;
6289 location_t asm_loc = c_parser_peek_token (parser)->location;
6290 int section, nsections;
6292 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6293 c_parser_consume_token (parser);
6294 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6296 quals = c_parser_peek_token (parser)->value;
6297 c_parser_consume_token (parser);
6299 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6300 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6302 warning_at (c_parser_peek_token (parser)->location,
6304 "%E qualifier ignored on asm",
6305 c_parser_peek_token (parser)->value);
6306 quals = NULL_TREE;
6307 c_parser_consume_token (parser);
6309 else
6310 quals = NULL_TREE;
6312 is_goto = false;
6313 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6315 c_parser_consume_token (parser);
6316 is_goto = true;
6319 /* ??? Follow the C++ parser rather than using the
6320 lex_untranslated_string kludge. */
6321 parser->lex_untranslated_string = true;
6322 ret = NULL;
6324 matching_parens parens;
6325 if (!parens.require_open (parser))
6326 goto error;
6328 str = c_parser_asm_string_literal (parser);
6329 if (str == NULL_TREE)
6330 goto error_close_paren;
6332 simple = true;
6333 outputs = NULL_TREE;
6334 inputs = NULL_TREE;
6335 clobbers = NULL_TREE;
6336 labels = NULL_TREE;
6338 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6339 goto done_asm;
6341 /* Parse each colon-delimited section of operands. */
6342 nsections = 3 + is_goto;
6343 for (section = 0; section < nsections; ++section)
6345 if (!c_parser_require (parser, CPP_COLON,
6346 is_goto
6347 ? G_("expected %<:%>")
6348 : G_("expected %<:%> or %<)%>"),
6349 UNKNOWN_LOCATION, is_goto))
6350 goto error_close_paren;
6352 /* Once past any colon, we're no longer a simple asm. */
6353 simple = false;
6355 if ((!c_parser_next_token_is (parser, CPP_COLON)
6356 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6357 || section == 3)
6358 switch (section)
6360 case 0:
6361 /* For asm goto, we don't allow output operands, but reserve
6362 the slot for a future extension that does allow them. */
6363 if (!is_goto)
6364 outputs = c_parser_asm_operands (parser);
6365 break;
6366 case 1:
6367 inputs = c_parser_asm_operands (parser);
6368 break;
6369 case 2:
6370 clobbers = c_parser_asm_clobbers (parser);
6371 break;
6372 case 3:
6373 labels = c_parser_asm_goto_operands (parser);
6374 break;
6375 default:
6376 gcc_unreachable ();
6379 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6380 goto done_asm;
6383 done_asm:
6384 if (!parens.require_close (parser))
6386 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6387 goto error;
6390 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6391 c_parser_skip_to_end_of_block_or_statement (parser);
6393 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6394 clobbers, labels, simple));
6396 error:
6397 parser->lex_untranslated_string = false;
6398 return ret;
6400 error_close_paren:
6401 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6402 goto error;
6405 /* Parse asm operands, a GNU extension.
6407 asm-operands:
6408 asm-operand
6409 asm-operands , asm-operand
6411 asm-operand:
6412 asm-string-literal ( expression )
6413 [ identifier ] asm-string-literal ( expression )
6416 static tree
6417 c_parser_asm_operands (c_parser *parser)
6419 tree list = NULL_TREE;
6420 while (true)
6422 tree name, str;
6423 struct c_expr expr;
6424 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6426 c_parser_consume_token (parser);
6427 if (c_parser_next_token_is (parser, CPP_NAME))
6429 tree id = c_parser_peek_token (parser)->value;
6430 c_parser_consume_token (parser);
6431 name = build_string (IDENTIFIER_LENGTH (id),
6432 IDENTIFIER_POINTER (id));
6434 else
6436 c_parser_error (parser, "expected identifier");
6437 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6438 return NULL_TREE;
6440 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6441 "expected %<]%>");
6443 else
6444 name = NULL_TREE;
6445 str = c_parser_asm_string_literal (parser);
6446 if (str == NULL_TREE)
6447 return NULL_TREE;
6448 parser->lex_untranslated_string = false;
6449 matching_parens parens;
6450 if (!parens.require_open (parser))
6452 parser->lex_untranslated_string = true;
6453 return NULL_TREE;
6455 expr = c_parser_expression (parser);
6456 mark_exp_read (expr.value);
6457 parser->lex_untranslated_string = true;
6458 if (!parens.require_close (parser))
6460 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6461 return NULL_TREE;
6463 list = chainon (list, build_tree_list (build_tree_list (name, str),
6464 expr.value));
6465 if (c_parser_next_token_is (parser, CPP_COMMA))
6466 c_parser_consume_token (parser);
6467 else
6468 break;
6470 return list;
6473 /* Parse asm clobbers, a GNU extension.
6475 asm-clobbers:
6476 asm-string-literal
6477 asm-clobbers , asm-string-literal
6480 static tree
6481 c_parser_asm_clobbers (c_parser *parser)
6483 tree list = NULL_TREE;
6484 while (true)
6486 tree str = c_parser_asm_string_literal (parser);
6487 if (str)
6488 list = tree_cons (NULL_TREE, str, list);
6489 else
6490 return NULL_TREE;
6491 if (c_parser_next_token_is (parser, CPP_COMMA))
6492 c_parser_consume_token (parser);
6493 else
6494 break;
6496 return list;
6499 /* Parse asm goto labels, a GNU extension.
6501 asm-goto-operands:
6502 identifier
6503 asm-goto-operands , identifier
6506 static tree
6507 c_parser_asm_goto_operands (c_parser *parser)
6509 tree list = NULL_TREE;
6510 while (true)
6512 tree name, label;
6514 if (c_parser_next_token_is (parser, CPP_NAME))
6516 c_token *tok = c_parser_peek_token (parser);
6517 name = tok->value;
6518 label = lookup_label_for_goto (tok->location, name);
6519 c_parser_consume_token (parser);
6520 TREE_USED (label) = 1;
6522 else
6524 c_parser_error (parser, "expected identifier");
6525 return NULL_TREE;
6528 name = build_string (IDENTIFIER_LENGTH (name),
6529 IDENTIFIER_POINTER (name));
6530 list = tree_cons (name, label, list);
6531 if (c_parser_next_token_is (parser, CPP_COMMA))
6532 c_parser_consume_token (parser);
6533 else
6534 return nreverse (list);
6538 /* Parse an expression other than a compound expression; that is, an
6539 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6540 AFTER is not NULL then it is an Objective-C message expression which
6541 is the primary-expression starting the expression as an initializer.
6543 assignment-expression:
6544 conditional-expression
6545 unary-expression assignment-operator assignment-expression
6547 assignment-operator: one of
6548 = *= /= %= += -= <<= >>= &= ^= |=
6550 In GNU C we accept any conditional expression on the LHS and
6551 diagnose the invalid lvalue rather than producing a syntax
6552 error. */
6554 static struct c_expr
6555 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6556 tree omp_atomic_lhs)
6558 struct c_expr lhs, rhs, ret;
6559 enum tree_code code;
6560 location_t op_location, exp_location;
6561 gcc_assert (!after || c_dialect_objc ());
6562 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6563 op_location = c_parser_peek_token (parser)->location;
6564 switch (c_parser_peek_token (parser)->type)
6566 case CPP_EQ:
6567 code = NOP_EXPR;
6568 break;
6569 case CPP_MULT_EQ:
6570 code = MULT_EXPR;
6571 break;
6572 case CPP_DIV_EQ:
6573 code = TRUNC_DIV_EXPR;
6574 break;
6575 case CPP_MOD_EQ:
6576 code = TRUNC_MOD_EXPR;
6577 break;
6578 case CPP_PLUS_EQ:
6579 code = PLUS_EXPR;
6580 break;
6581 case CPP_MINUS_EQ:
6582 code = MINUS_EXPR;
6583 break;
6584 case CPP_LSHIFT_EQ:
6585 code = LSHIFT_EXPR;
6586 break;
6587 case CPP_RSHIFT_EQ:
6588 code = RSHIFT_EXPR;
6589 break;
6590 case CPP_AND_EQ:
6591 code = BIT_AND_EXPR;
6592 break;
6593 case CPP_XOR_EQ:
6594 code = BIT_XOR_EXPR;
6595 break;
6596 case CPP_OR_EQ:
6597 code = BIT_IOR_EXPR;
6598 break;
6599 default:
6600 return lhs;
6602 c_parser_consume_token (parser);
6603 exp_location = c_parser_peek_token (parser)->location;
6604 rhs = c_parser_expr_no_commas (parser, NULL);
6605 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6607 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6608 code, exp_location, rhs.value,
6609 rhs.original_type);
6610 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6611 if (code == NOP_EXPR)
6612 ret.original_code = MODIFY_EXPR;
6613 else
6615 TREE_NO_WARNING (ret.value) = 1;
6616 ret.original_code = ERROR_MARK;
6618 ret.original_type = NULL;
6619 return ret;
6622 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6623 AFTER is not NULL then it is an Objective-C message expression which is
6624 the primary-expression starting the expression as an initializer.
6626 conditional-expression:
6627 logical-OR-expression
6628 logical-OR-expression ? expression : conditional-expression
6630 GNU extensions:
6632 conditional-expression:
6633 logical-OR-expression ? : conditional-expression
6636 static struct c_expr
6637 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6638 tree omp_atomic_lhs)
6640 struct c_expr cond, exp1, exp2, ret;
6641 location_t start, cond_loc, colon_loc;
6643 gcc_assert (!after || c_dialect_objc ());
6645 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6647 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6648 return cond;
6649 if (cond.value != error_mark_node)
6650 start = cond.get_start ();
6651 else
6652 start = UNKNOWN_LOCATION;
6653 cond_loc = c_parser_peek_token (parser)->location;
6654 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6655 c_parser_consume_token (parser);
6656 if (c_parser_next_token_is (parser, CPP_COLON))
6658 tree eptype = NULL_TREE;
6660 location_t middle_loc = c_parser_peek_token (parser)->location;
6661 pedwarn (middle_loc, OPT_Wpedantic,
6662 "ISO C forbids omitting the middle term of a ?: expression");
6663 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6665 eptype = TREE_TYPE (cond.value);
6666 cond.value = TREE_OPERAND (cond.value, 0);
6668 tree e = cond.value;
6669 while (TREE_CODE (e) == COMPOUND_EXPR)
6670 e = TREE_OPERAND (e, 1);
6671 warn_for_omitted_condop (middle_loc, e);
6672 /* Make sure first operand is calculated only once. */
6673 exp1.value = save_expr (default_conversion (cond.value));
6674 if (eptype)
6675 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6676 exp1.original_type = NULL;
6677 exp1.src_range = cond.src_range;
6678 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6679 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6681 else
6683 cond.value
6684 = c_objc_common_truthvalue_conversion
6685 (cond_loc, default_conversion (cond.value));
6686 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6687 exp1 = c_parser_expression_conv (parser);
6688 mark_exp_read (exp1.value);
6689 c_inhibit_evaluation_warnings +=
6690 ((cond.value == truthvalue_true_node)
6691 - (cond.value == truthvalue_false_node));
6694 colon_loc = c_parser_peek_token (parser)->location;
6695 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6697 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6698 ret.set_error ();
6699 ret.original_code = ERROR_MARK;
6700 ret.original_type = NULL;
6701 return ret;
6704 location_t exp2_loc = c_parser_peek_token (parser)->location;
6705 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6706 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6708 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6709 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6710 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6711 ret.value = build_conditional_expr (colon_loc, cond.value,
6712 cond.original_code == C_MAYBE_CONST_EXPR,
6713 exp1.value, exp1.original_type, loc1,
6714 exp2.value, exp2.original_type, loc2);
6715 ret.original_code = ERROR_MARK;
6716 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6717 ret.original_type = NULL;
6718 else
6720 tree t1, t2;
6722 /* If both sides are enum type, the default conversion will have
6723 made the type of the result be an integer type. We want to
6724 remember the enum types we started with. */
6725 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6726 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6727 ret.original_type = ((t1 != error_mark_node
6728 && t2 != error_mark_node
6729 && (TYPE_MAIN_VARIANT (t1)
6730 == TYPE_MAIN_VARIANT (t2)))
6731 ? t1
6732 : NULL);
6734 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6735 return ret;
6738 /* Parse a binary expression; that is, a logical-OR-expression (C90
6739 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6740 NULL then it is an Objective-C message expression which is the
6741 primary-expression starting the expression as an initializer.
6743 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6744 when it should be the unfolded lhs. In a valid OpenMP source,
6745 one of the operands of the toplevel binary expression must be equal
6746 to it. In that case, just return a build2 created binary operation
6747 rather than result of parser_build_binary_op.
6749 multiplicative-expression:
6750 cast-expression
6751 multiplicative-expression * cast-expression
6752 multiplicative-expression / cast-expression
6753 multiplicative-expression % cast-expression
6755 additive-expression:
6756 multiplicative-expression
6757 additive-expression + multiplicative-expression
6758 additive-expression - multiplicative-expression
6760 shift-expression:
6761 additive-expression
6762 shift-expression << additive-expression
6763 shift-expression >> additive-expression
6765 relational-expression:
6766 shift-expression
6767 relational-expression < shift-expression
6768 relational-expression > shift-expression
6769 relational-expression <= shift-expression
6770 relational-expression >= shift-expression
6772 equality-expression:
6773 relational-expression
6774 equality-expression == relational-expression
6775 equality-expression != relational-expression
6777 AND-expression:
6778 equality-expression
6779 AND-expression & equality-expression
6781 exclusive-OR-expression:
6782 AND-expression
6783 exclusive-OR-expression ^ AND-expression
6785 inclusive-OR-expression:
6786 exclusive-OR-expression
6787 inclusive-OR-expression | exclusive-OR-expression
6789 logical-AND-expression:
6790 inclusive-OR-expression
6791 logical-AND-expression && inclusive-OR-expression
6793 logical-OR-expression:
6794 logical-AND-expression
6795 logical-OR-expression || logical-AND-expression
6798 static struct c_expr
6799 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6800 tree omp_atomic_lhs)
6802 /* A binary expression is parsed using operator-precedence parsing,
6803 with the operands being cast expressions. All the binary
6804 operators are left-associative. Thus a binary expression is of
6805 form:
6807 E0 op1 E1 op2 E2 ...
6809 which we represent on a stack. On the stack, the precedence
6810 levels are strictly increasing. When a new operator is
6811 encountered of higher precedence than that at the top of the
6812 stack, it is pushed; its LHS is the top expression, and its RHS
6813 is everything parsed until it is popped. When a new operator is
6814 encountered with precedence less than or equal to that at the top
6815 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6816 by the result of the operation until the operator at the top of
6817 the stack has lower precedence than the new operator or there is
6818 only one element on the stack; then the top expression is the LHS
6819 of the new operator. In the case of logical AND and OR
6820 expressions, we also need to adjust c_inhibit_evaluation_warnings
6821 as appropriate when the operators are pushed and popped. */
6823 struct {
6824 /* The expression at this stack level. */
6825 struct c_expr expr;
6826 /* The precedence of the operator on its left, PREC_NONE at the
6827 bottom of the stack. */
6828 enum c_parser_prec prec;
6829 /* The operation on its left. */
6830 enum tree_code op;
6831 /* The source location of this operation. */
6832 location_t loc;
6833 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6834 tree sizeof_arg;
6835 } stack[NUM_PRECS];
6836 int sp;
6837 /* Location of the binary operator. */
6838 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6839 #define POP \
6840 do { \
6841 switch (stack[sp].op) \
6843 case TRUTH_ANDIF_EXPR: \
6844 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6845 == truthvalue_false_node); \
6846 break; \
6847 case TRUTH_ORIF_EXPR: \
6848 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6849 == truthvalue_true_node); \
6850 break; \
6851 case TRUNC_DIV_EXPR: \
6852 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6853 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6855 tree type0 = stack[sp - 1].sizeof_arg; \
6856 tree type1 = stack[sp].sizeof_arg; \
6857 tree first_arg = type0; \
6858 if (!TYPE_P (type0)) \
6859 type0 = TREE_TYPE (type0); \
6860 if (!TYPE_P (type1)) \
6861 type1 = TREE_TYPE (type1); \
6862 if (POINTER_TYPE_P (type0) \
6863 && comptypes (TREE_TYPE (type0), type1) \
6864 && !(TREE_CODE (first_arg) == PARM_DECL \
6865 && C_ARRAY_PARAMETER (first_arg) \
6866 && warn_sizeof_array_argument)) \
6867 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6868 "division %<sizeof (%T) / sizeof (%T)%> does " \
6869 "not compute the number of array elements", \
6870 type0, type1)) \
6871 if (DECL_P (first_arg)) \
6872 inform (DECL_SOURCE_LOCATION (first_arg), \
6873 "first %<sizeof%> operand was declared here"); \
6875 break; \
6876 default: \
6877 break; \
6879 stack[sp - 1].expr \
6880 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6881 stack[sp - 1].expr, true, true); \
6882 stack[sp].expr \
6883 = convert_lvalue_to_rvalue (stack[sp].loc, \
6884 stack[sp].expr, true, true); \
6885 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6886 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6887 && ((1 << stack[sp].prec) \
6888 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6889 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6890 && stack[sp].op != TRUNC_MOD_EXPR \
6891 && stack[0].expr.value != error_mark_node \
6892 && stack[1].expr.value != error_mark_node \
6893 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6894 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6895 stack[0].expr.value \
6896 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6897 stack[0].expr.value, stack[1].expr.value); \
6898 else \
6899 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6900 stack[sp].op, \
6901 stack[sp - 1].expr, \
6902 stack[sp].expr); \
6903 sp--; \
6904 } while (0)
6905 gcc_assert (!after || c_dialect_objc ());
6906 stack[0].loc = c_parser_peek_token (parser)->location;
6907 stack[0].expr = c_parser_cast_expression (parser, after);
6908 stack[0].prec = PREC_NONE;
6909 stack[0].sizeof_arg = c_last_sizeof_arg;
6910 sp = 0;
6911 while (true)
6913 enum c_parser_prec oprec;
6914 enum tree_code ocode;
6915 source_range src_range;
6916 if (parser->error)
6917 goto out;
6918 switch (c_parser_peek_token (parser)->type)
6920 case CPP_MULT:
6921 oprec = PREC_MULT;
6922 ocode = MULT_EXPR;
6923 break;
6924 case CPP_DIV:
6925 oprec = PREC_MULT;
6926 ocode = TRUNC_DIV_EXPR;
6927 break;
6928 case CPP_MOD:
6929 oprec = PREC_MULT;
6930 ocode = TRUNC_MOD_EXPR;
6931 break;
6932 case CPP_PLUS:
6933 oprec = PREC_ADD;
6934 ocode = PLUS_EXPR;
6935 break;
6936 case CPP_MINUS:
6937 oprec = PREC_ADD;
6938 ocode = MINUS_EXPR;
6939 break;
6940 case CPP_LSHIFT:
6941 oprec = PREC_SHIFT;
6942 ocode = LSHIFT_EXPR;
6943 break;
6944 case CPP_RSHIFT:
6945 oprec = PREC_SHIFT;
6946 ocode = RSHIFT_EXPR;
6947 break;
6948 case CPP_LESS:
6949 oprec = PREC_REL;
6950 ocode = LT_EXPR;
6951 break;
6952 case CPP_GREATER:
6953 oprec = PREC_REL;
6954 ocode = GT_EXPR;
6955 break;
6956 case CPP_LESS_EQ:
6957 oprec = PREC_REL;
6958 ocode = LE_EXPR;
6959 break;
6960 case CPP_GREATER_EQ:
6961 oprec = PREC_REL;
6962 ocode = GE_EXPR;
6963 break;
6964 case CPP_EQ_EQ:
6965 oprec = PREC_EQ;
6966 ocode = EQ_EXPR;
6967 break;
6968 case CPP_NOT_EQ:
6969 oprec = PREC_EQ;
6970 ocode = NE_EXPR;
6971 break;
6972 case CPP_AND:
6973 oprec = PREC_BITAND;
6974 ocode = BIT_AND_EXPR;
6975 break;
6976 case CPP_XOR:
6977 oprec = PREC_BITXOR;
6978 ocode = BIT_XOR_EXPR;
6979 break;
6980 case CPP_OR:
6981 oprec = PREC_BITOR;
6982 ocode = BIT_IOR_EXPR;
6983 break;
6984 case CPP_AND_AND:
6985 oprec = PREC_LOGAND;
6986 ocode = TRUTH_ANDIF_EXPR;
6987 break;
6988 case CPP_OR_OR:
6989 oprec = PREC_LOGOR;
6990 ocode = TRUTH_ORIF_EXPR;
6991 break;
6992 default:
6993 /* Not a binary operator, so end of the binary
6994 expression. */
6995 goto out;
6997 binary_loc = c_parser_peek_token (parser)->location;
6998 while (oprec <= stack[sp].prec)
6999 POP;
7000 c_parser_consume_token (parser);
7001 switch (ocode)
7003 case TRUTH_ANDIF_EXPR:
7004 src_range = stack[sp].expr.src_range;
7005 stack[sp].expr
7006 = convert_lvalue_to_rvalue (stack[sp].loc,
7007 stack[sp].expr, true, true);
7008 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7009 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7010 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7011 == truthvalue_false_node);
7012 set_c_expr_source_range (&stack[sp].expr, src_range);
7013 break;
7014 case TRUTH_ORIF_EXPR:
7015 src_range = stack[sp].expr.src_range;
7016 stack[sp].expr
7017 = convert_lvalue_to_rvalue (stack[sp].loc,
7018 stack[sp].expr, true, true);
7019 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7020 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7021 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7022 == truthvalue_true_node);
7023 set_c_expr_source_range (&stack[sp].expr, src_range);
7024 break;
7025 default:
7026 break;
7028 sp++;
7029 stack[sp].loc = binary_loc;
7030 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7031 stack[sp].prec = oprec;
7032 stack[sp].op = ocode;
7033 stack[sp].sizeof_arg = c_last_sizeof_arg;
7035 out:
7036 while (sp > 0)
7037 POP;
7038 return stack[0].expr;
7039 #undef POP
7042 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7043 is not NULL then it is an Objective-C message expression which is the
7044 primary-expression starting the expression as an initializer.
7046 cast-expression:
7047 unary-expression
7048 ( type-name ) unary-expression
7051 static struct c_expr
7052 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7054 location_t cast_loc = c_parser_peek_token (parser)->location;
7055 gcc_assert (!after || c_dialect_objc ());
7056 if (after)
7057 return c_parser_postfix_expression_after_primary (parser,
7058 cast_loc, *after);
7059 /* If the expression begins with a parenthesized type name, it may
7060 be either a cast or a compound literal; we need to see whether
7061 the next character is '{' to tell the difference. If not, it is
7062 an unary expression. Full detection of unknown typenames here
7063 would require a 3-token lookahead. */
7064 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7065 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7067 struct c_type_name *type_name;
7068 struct c_expr ret;
7069 struct c_expr expr;
7070 matching_parens parens;
7071 parens.consume_open (parser);
7072 type_name = c_parser_type_name (parser, true);
7073 parens.skip_until_found_close (parser);
7074 if (type_name == NULL)
7076 ret.set_error ();
7077 ret.original_code = ERROR_MARK;
7078 ret.original_type = NULL;
7079 return ret;
7082 /* Save casted types in the function's used types hash table. */
7083 used_types_insert (type_name->specs->type);
7085 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7086 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7087 cast_loc);
7088 if (type_name->specs->alignas_p)
7089 error_at (type_name->specs->locations[cdw_alignas],
7090 "alignment specified for type name in cast");
7092 location_t expr_loc = c_parser_peek_token (parser)->location;
7093 expr = c_parser_cast_expression (parser, NULL);
7094 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7096 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7097 if (ret.value && expr.value)
7098 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7099 ret.original_code = ERROR_MARK;
7100 ret.original_type = NULL;
7101 return ret;
7103 else
7104 return c_parser_unary_expression (parser);
7107 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7109 unary-expression:
7110 postfix-expression
7111 ++ unary-expression
7112 -- unary-expression
7113 unary-operator cast-expression
7114 sizeof unary-expression
7115 sizeof ( type-name )
7117 unary-operator: one of
7118 & * + - ~ !
7120 GNU extensions:
7122 unary-expression:
7123 __alignof__ unary-expression
7124 __alignof__ ( type-name )
7125 && identifier
7127 (C11 permits _Alignof with type names only.)
7129 unary-operator: one of
7130 __extension__ __real__ __imag__
7132 Transactional Memory:
7134 unary-expression:
7135 transaction-expression
7137 In addition, the GNU syntax treats ++ and -- as unary operators, so
7138 they may be applied to cast expressions with errors for non-lvalues
7139 given later. */
7141 static struct c_expr
7142 c_parser_unary_expression (c_parser *parser)
7144 int ext;
7145 struct c_expr ret, op;
7146 location_t op_loc = c_parser_peek_token (parser)->location;
7147 location_t exp_loc;
7148 location_t finish;
7149 ret.original_code = ERROR_MARK;
7150 ret.original_type = NULL;
7151 switch (c_parser_peek_token (parser)->type)
7153 case CPP_PLUS_PLUS:
7154 c_parser_consume_token (parser);
7155 exp_loc = c_parser_peek_token (parser)->location;
7156 op = c_parser_cast_expression (parser, NULL);
7158 op = default_function_array_read_conversion (exp_loc, op);
7159 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7160 case CPP_MINUS_MINUS:
7161 c_parser_consume_token (parser);
7162 exp_loc = c_parser_peek_token (parser)->location;
7163 op = c_parser_cast_expression (parser, NULL);
7165 op = default_function_array_read_conversion (exp_loc, op);
7166 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7167 case CPP_AND:
7168 c_parser_consume_token (parser);
7169 op = c_parser_cast_expression (parser, NULL);
7170 mark_exp_read (op.value);
7171 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7172 case CPP_MULT:
7174 c_parser_consume_token (parser);
7175 exp_loc = c_parser_peek_token (parser)->location;
7176 op = c_parser_cast_expression (parser, NULL);
7177 finish = op.get_finish ();
7178 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7179 location_t combined_loc = make_location (op_loc, op_loc, finish);
7180 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7181 ret.src_range.m_start = op_loc;
7182 ret.src_range.m_finish = finish;
7183 return ret;
7185 case CPP_PLUS:
7186 if (!c_dialect_objc () && !in_system_header_at (input_location))
7187 warning_at (op_loc,
7188 OPT_Wtraditional,
7189 "traditional C rejects the unary plus operator");
7190 c_parser_consume_token (parser);
7191 exp_loc = c_parser_peek_token (parser)->location;
7192 op = c_parser_cast_expression (parser, NULL);
7193 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7194 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7195 case CPP_MINUS:
7196 c_parser_consume_token (parser);
7197 exp_loc = c_parser_peek_token (parser)->location;
7198 op = c_parser_cast_expression (parser, NULL);
7199 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7200 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7201 case CPP_COMPL:
7202 c_parser_consume_token (parser);
7203 exp_loc = c_parser_peek_token (parser)->location;
7204 op = c_parser_cast_expression (parser, NULL);
7205 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7206 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7207 case CPP_NOT:
7208 c_parser_consume_token (parser);
7209 exp_loc = c_parser_peek_token (parser)->location;
7210 op = c_parser_cast_expression (parser, NULL);
7211 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7212 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7213 case CPP_AND_AND:
7214 /* Refer to the address of a label as a pointer. */
7215 c_parser_consume_token (parser);
7216 if (c_parser_next_token_is (parser, CPP_NAME))
7218 ret.value = finish_label_address_expr
7219 (c_parser_peek_token (parser)->value, op_loc);
7220 set_c_expr_source_range (&ret, op_loc,
7221 c_parser_peek_token (parser)->get_finish ());
7222 c_parser_consume_token (parser);
7224 else
7226 c_parser_error (parser, "expected identifier");
7227 ret.set_error ();
7229 return ret;
7230 case CPP_KEYWORD:
7231 switch (c_parser_peek_token (parser)->keyword)
7233 case RID_SIZEOF:
7234 return c_parser_sizeof_expression (parser);
7235 case RID_ALIGNOF:
7236 return c_parser_alignof_expression (parser);
7237 case RID_EXTENSION:
7238 c_parser_consume_token (parser);
7239 ext = disable_extension_diagnostics ();
7240 ret = c_parser_cast_expression (parser, NULL);
7241 restore_extension_diagnostics (ext);
7242 return ret;
7243 case RID_REALPART:
7244 c_parser_consume_token (parser);
7245 exp_loc = c_parser_peek_token (parser)->location;
7246 op = c_parser_cast_expression (parser, NULL);
7247 op = default_function_array_conversion (exp_loc, op);
7248 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7249 case RID_IMAGPART:
7250 c_parser_consume_token (parser);
7251 exp_loc = c_parser_peek_token (parser)->location;
7252 op = c_parser_cast_expression (parser, NULL);
7253 op = default_function_array_conversion (exp_loc, op);
7254 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7255 case RID_TRANSACTION_ATOMIC:
7256 case RID_TRANSACTION_RELAXED:
7257 return c_parser_transaction_expression (parser,
7258 c_parser_peek_token (parser)->keyword);
7259 default:
7260 return c_parser_postfix_expression (parser);
7262 default:
7263 return c_parser_postfix_expression (parser);
7267 /* Parse a sizeof expression. */
7269 static struct c_expr
7270 c_parser_sizeof_expression (c_parser *parser)
7272 struct c_expr expr;
7273 struct c_expr result;
7274 location_t expr_loc;
7275 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7277 location_t start;
7278 location_t finish = UNKNOWN_LOCATION;
7280 start = c_parser_peek_token (parser)->location;
7282 c_parser_consume_token (parser);
7283 c_inhibit_evaluation_warnings++;
7284 in_sizeof++;
7285 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7286 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7288 /* Either sizeof ( type-name ) or sizeof unary-expression
7289 starting with a compound literal. */
7290 struct c_type_name *type_name;
7291 matching_parens parens;
7292 parens.consume_open (parser);
7293 expr_loc = c_parser_peek_token (parser)->location;
7294 type_name = c_parser_type_name (parser, true);
7295 parens.skip_until_found_close (parser);
7296 finish = parser->tokens_buf[0].location;
7297 if (type_name == NULL)
7299 struct c_expr ret;
7300 c_inhibit_evaluation_warnings--;
7301 in_sizeof--;
7302 ret.set_error ();
7303 ret.original_code = ERROR_MARK;
7304 ret.original_type = NULL;
7305 return ret;
7307 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7309 expr = c_parser_postfix_expression_after_paren_type (parser,
7310 type_name,
7311 expr_loc);
7312 finish = expr.get_finish ();
7313 goto sizeof_expr;
7315 /* sizeof ( type-name ). */
7316 if (type_name->specs->alignas_p)
7317 error_at (type_name->specs->locations[cdw_alignas],
7318 "alignment specified for type name in %<sizeof%>");
7319 c_inhibit_evaluation_warnings--;
7320 in_sizeof--;
7321 result = c_expr_sizeof_type (expr_loc, type_name);
7323 else
7325 expr_loc = c_parser_peek_token (parser)->location;
7326 expr = c_parser_unary_expression (parser);
7327 finish = expr.get_finish ();
7328 sizeof_expr:
7329 c_inhibit_evaluation_warnings--;
7330 in_sizeof--;
7331 mark_exp_read (expr.value);
7332 if (TREE_CODE (expr.value) == COMPONENT_REF
7333 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7334 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7335 result = c_expr_sizeof_expr (expr_loc, expr);
7337 if (finish != UNKNOWN_LOCATION)
7338 set_c_expr_source_range (&result, start, finish);
7339 return result;
7342 /* Parse an alignof expression. */
7344 static struct c_expr
7345 c_parser_alignof_expression (c_parser *parser)
7347 struct c_expr expr;
7348 location_t start_loc = c_parser_peek_token (parser)->location;
7349 location_t end_loc;
7350 tree alignof_spelling = c_parser_peek_token (parser)->value;
7351 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7352 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7353 "_Alignof") == 0;
7354 /* A diagnostic is not required for the use of this identifier in
7355 the implementation namespace; only diagnose it for the C11
7356 spelling because of existing code using the other spellings. */
7357 if (is_c11_alignof)
7359 if (flag_isoc99)
7360 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7361 alignof_spelling);
7362 else
7363 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7364 alignof_spelling);
7366 c_parser_consume_token (parser);
7367 c_inhibit_evaluation_warnings++;
7368 in_alignof++;
7369 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7370 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7372 /* Either __alignof__ ( type-name ) or __alignof__
7373 unary-expression starting with a compound literal. */
7374 location_t loc;
7375 struct c_type_name *type_name;
7376 struct c_expr ret;
7377 matching_parens parens;
7378 parens.consume_open (parser);
7379 loc = c_parser_peek_token (parser)->location;
7380 type_name = c_parser_type_name (parser, true);
7381 end_loc = c_parser_peek_token (parser)->location;
7382 parens.skip_until_found_close (parser);
7383 if (type_name == NULL)
7385 struct c_expr ret;
7386 c_inhibit_evaluation_warnings--;
7387 in_alignof--;
7388 ret.set_error ();
7389 ret.original_code = ERROR_MARK;
7390 ret.original_type = NULL;
7391 return ret;
7393 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7395 expr = c_parser_postfix_expression_after_paren_type (parser,
7396 type_name,
7397 loc);
7398 goto alignof_expr;
7400 /* alignof ( type-name ). */
7401 if (type_name->specs->alignas_p)
7402 error_at (type_name->specs->locations[cdw_alignas],
7403 "alignment specified for type name in %qE",
7404 alignof_spelling);
7405 c_inhibit_evaluation_warnings--;
7406 in_alignof--;
7407 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7408 NULL, NULL),
7409 false, is_c11_alignof, 1);
7410 ret.original_code = ERROR_MARK;
7411 ret.original_type = NULL;
7412 set_c_expr_source_range (&ret, start_loc, end_loc);
7413 return ret;
7415 else
7417 struct c_expr ret;
7418 expr = c_parser_unary_expression (parser);
7419 end_loc = expr.src_range.m_finish;
7420 alignof_expr:
7421 mark_exp_read (expr.value);
7422 c_inhibit_evaluation_warnings--;
7423 in_alignof--;
7424 if (is_c11_alignof)
7425 pedwarn (start_loc,
7426 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7427 alignof_spelling);
7428 ret.value = c_alignof_expr (start_loc, expr.value);
7429 ret.original_code = ERROR_MARK;
7430 ret.original_type = NULL;
7431 set_c_expr_source_range (&ret, start_loc, end_loc);
7432 return ret;
7436 /* Helper function to read arguments of builtins which are interfaces
7437 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7438 others. The name of the builtin is passed using BNAME parameter.
7439 Function returns true if there were no errors while parsing and
7440 stores the arguments in CEXPR_LIST. If it returns true,
7441 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7442 parenthesis. */
7443 static bool
7444 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7445 vec<c_expr_t, va_gc> **ret_cexpr_list,
7446 bool choose_expr_p,
7447 location_t *out_close_paren_loc)
7449 location_t loc = c_parser_peek_token (parser)->location;
7450 vec<c_expr_t, va_gc> *cexpr_list;
7451 c_expr_t expr;
7452 bool saved_force_folding_builtin_constant_p;
7454 *ret_cexpr_list = NULL;
7455 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7457 error_at (loc, "cannot take address of %qs", bname);
7458 return false;
7461 c_parser_consume_token (parser);
7463 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7465 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7466 c_parser_consume_token (parser);
7467 return true;
7470 saved_force_folding_builtin_constant_p
7471 = force_folding_builtin_constant_p;
7472 force_folding_builtin_constant_p |= choose_expr_p;
7473 expr = c_parser_expr_no_commas (parser, NULL);
7474 force_folding_builtin_constant_p
7475 = saved_force_folding_builtin_constant_p;
7476 vec_alloc (cexpr_list, 1);
7477 vec_safe_push (cexpr_list, expr);
7478 while (c_parser_next_token_is (parser, CPP_COMMA))
7480 c_parser_consume_token (parser);
7481 expr = c_parser_expr_no_commas (parser, NULL);
7482 vec_safe_push (cexpr_list, expr);
7485 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7486 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7487 return false;
7489 *ret_cexpr_list = cexpr_list;
7490 return true;
7493 /* This represents a single generic-association. */
7495 struct c_generic_association
7497 /* The location of the starting token of the type. */
7498 location_t type_location;
7499 /* The association's type, or NULL_TREE for 'default'. */
7500 tree type;
7501 /* The association's expression. */
7502 struct c_expr expression;
7505 /* Parse a generic-selection. (C11 6.5.1.1).
7507 generic-selection:
7508 _Generic ( assignment-expression , generic-assoc-list )
7510 generic-assoc-list:
7511 generic-association
7512 generic-assoc-list , generic-association
7514 generic-association:
7515 type-name : assignment-expression
7516 default : assignment-expression
7519 static struct c_expr
7520 c_parser_generic_selection (c_parser *parser)
7522 struct c_expr selector, error_expr;
7523 tree selector_type;
7524 struct c_generic_association matched_assoc;
7525 bool match_found = false;
7526 location_t generic_loc, selector_loc;
7528 error_expr.original_code = ERROR_MARK;
7529 error_expr.original_type = NULL;
7530 error_expr.set_error ();
7531 matched_assoc.type_location = UNKNOWN_LOCATION;
7532 matched_assoc.type = NULL_TREE;
7533 matched_assoc.expression = error_expr;
7535 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7536 generic_loc = c_parser_peek_token (parser)->location;
7537 c_parser_consume_token (parser);
7538 if (flag_isoc99)
7539 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7540 "ISO C99 does not support %<_Generic%>");
7541 else
7542 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7543 "ISO C90 does not support %<_Generic%>");
7545 matching_parens parens;
7546 if (!parens.require_open (parser))
7547 return error_expr;
7549 c_inhibit_evaluation_warnings++;
7550 selector_loc = c_parser_peek_token (parser)->location;
7551 selector = c_parser_expr_no_commas (parser, NULL);
7552 selector = default_function_array_conversion (selector_loc, selector);
7553 c_inhibit_evaluation_warnings--;
7555 if (selector.value == error_mark_node)
7557 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7558 return selector;
7560 selector_type = TREE_TYPE (selector.value);
7561 /* In ISO C terms, rvalues (including the controlling expression of
7562 _Generic) do not have qualified types. */
7563 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7564 selector_type = TYPE_MAIN_VARIANT (selector_type);
7565 /* In ISO C terms, _Noreturn is not part of the type of expressions
7566 such as &abort, but in GCC it is represented internally as a type
7567 qualifier. */
7568 if (FUNCTION_POINTER_TYPE_P (selector_type)
7569 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7570 selector_type
7571 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7573 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7576 return error_expr;
7579 auto_vec<c_generic_association> associations;
7580 while (1)
7582 struct c_generic_association assoc, *iter;
7583 unsigned int ix;
7584 c_token *token = c_parser_peek_token (parser);
7586 assoc.type_location = token->location;
7587 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7589 c_parser_consume_token (parser);
7590 assoc.type = NULL_TREE;
7592 else
7594 struct c_type_name *type_name;
7596 type_name = c_parser_type_name (parser);
7597 if (type_name == NULL)
7599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7600 return error_expr;
7602 assoc.type = groktypename (type_name, NULL, NULL);
7603 if (assoc.type == error_mark_node)
7605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7606 return error_expr;
7609 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7610 error_at (assoc.type_location,
7611 "%<_Generic%> association has function type");
7612 else if (!COMPLETE_TYPE_P (assoc.type))
7613 error_at (assoc.type_location,
7614 "%<_Generic%> association has incomplete type");
7616 if (variably_modified_type_p (assoc.type, NULL_TREE))
7617 error_at (assoc.type_location,
7618 "%<_Generic%> association has "
7619 "variable length type");
7622 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7624 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7625 return error_expr;
7628 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7629 if (assoc.expression.value == error_mark_node)
7631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7632 return error_expr;
7635 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7637 if (assoc.type == NULL_TREE)
7639 if (iter->type == NULL_TREE)
7641 error_at (assoc.type_location,
7642 "duplicate %<default%> case in %<_Generic%>");
7643 inform (iter->type_location, "original %<default%> is here");
7646 else if (iter->type != NULL_TREE)
7648 if (comptypes (assoc.type, iter->type))
7650 error_at (assoc.type_location,
7651 "%<_Generic%> specifies two compatible types");
7652 inform (iter->type_location, "compatible type is here");
7657 if (assoc.type == NULL_TREE)
7659 if (!match_found)
7661 matched_assoc = assoc;
7662 match_found = true;
7665 else if (comptypes (assoc.type, selector_type))
7667 if (!match_found || matched_assoc.type == NULL_TREE)
7669 matched_assoc = assoc;
7670 match_found = true;
7672 else
7674 error_at (assoc.type_location,
7675 "%<_Generic%> selector matches multiple associations");
7676 inform (matched_assoc.type_location,
7677 "other match is here");
7681 associations.safe_push (assoc);
7683 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7684 break;
7685 c_parser_consume_token (parser);
7688 if (!parens.require_close (parser))
7690 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7691 return error_expr;
7694 if (!match_found)
7696 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7697 "compatible with any association",
7698 selector_type);
7699 return error_expr;
7702 return matched_assoc.expression;
7705 /* Check the validity of a function pointer argument *EXPR (argument
7706 position POS) to __builtin_tgmath. Return the number of function
7707 arguments if possibly valid; return 0 having reported an error if
7708 not valid. */
7710 static unsigned int
7711 check_tgmath_function (c_expr *expr, unsigned int pos)
7713 tree type = TREE_TYPE (expr->value);
7714 if (!FUNCTION_POINTER_TYPE_P (type))
7716 error_at (expr->get_location (),
7717 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7718 pos);
7719 return 0;
7721 type = TREE_TYPE (type);
7722 if (!prototype_p (type))
7724 error_at (expr->get_location (),
7725 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7726 return 0;
7728 if (stdarg_p (type))
7730 error_at (expr->get_location (),
7731 "argument %u of %<__builtin_tgmath%> has variable arguments",
7732 pos);
7733 return 0;
7735 unsigned int nargs = 0;
7736 function_args_iterator iter;
7737 tree t;
7738 FOREACH_FUNCTION_ARGS (type, t, iter)
7740 if (t == void_type_node)
7741 break;
7742 nargs++;
7744 if (nargs == 0)
7746 error_at (expr->get_location (),
7747 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7748 return 0;
7750 return nargs;
7753 /* Ways in which a parameter or return value of a type-generic macro
7754 may vary between the different functions the macro may call. */
7755 enum tgmath_parm_kind
7757 tgmath_fixed, tgmath_real, tgmath_complex
7760 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7761 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7762 call c_parser_postfix_expression_after_paren_type on encountering them.
7764 postfix-expression:
7765 primary-expression
7766 postfix-expression [ expression ]
7767 postfix-expression ( argument-expression-list[opt] )
7768 postfix-expression . identifier
7769 postfix-expression -> identifier
7770 postfix-expression ++
7771 postfix-expression --
7772 ( type-name ) { initializer-list }
7773 ( type-name ) { initializer-list , }
7775 argument-expression-list:
7776 argument-expression
7777 argument-expression-list , argument-expression
7779 primary-expression:
7780 identifier
7781 constant
7782 string-literal
7783 ( expression )
7784 generic-selection
7786 GNU extensions:
7788 primary-expression:
7789 __func__
7790 (treated as a keyword in GNU C)
7791 __FUNCTION__
7792 __PRETTY_FUNCTION__
7793 ( compound-statement )
7794 __builtin_va_arg ( assignment-expression , type-name )
7795 __builtin_offsetof ( type-name , offsetof-member-designator )
7796 __builtin_choose_expr ( assignment-expression ,
7797 assignment-expression ,
7798 assignment-expression )
7799 __builtin_types_compatible_p ( type-name , type-name )
7800 __builtin_tgmath ( expr-list )
7801 __builtin_complex ( assignment-expression , assignment-expression )
7802 __builtin_shuffle ( assignment-expression , assignment-expression )
7803 __builtin_shuffle ( assignment-expression ,
7804 assignment-expression ,
7805 assignment-expression, )
7807 offsetof-member-designator:
7808 identifier
7809 offsetof-member-designator . identifier
7810 offsetof-member-designator [ expression ]
7812 Objective-C:
7814 primary-expression:
7815 [ objc-receiver objc-message-args ]
7816 @selector ( objc-selector-arg )
7817 @protocol ( identifier )
7818 @encode ( type-name )
7819 objc-string-literal
7820 Classname . identifier
7823 static struct c_expr
7824 c_parser_postfix_expression (c_parser *parser)
7826 struct c_expr expr, e1;
7827 struct c_type_name *t1, *t2;
7828 location_t loc = c_parser_peek_token (parser)->location;
7829 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7830 expr.original_code = ERROR_MARK;
7831 expr.original_type = NULL;
7832 switch (c_parser_peek_token (parser)->type)
7834 case CPP_NUMBER:
7835 expr.value = c_parser_peek_token (parser)->value;
7836 set_c_expr_source_range (&expr, tok_range);
7837 loc = c_parser_peek_token (parser)->location;
7838 c_parser_consume_token (parser);
7839 if (TREE_CODE (expr.value) == FIXED_CST
7840 && !targetm.fixed_point_supported_p ())
7842 error_at (loc, "fixed-point types not supported for this target");
7843 expr.set_error ();
7845 break;
7846 case CPP_CHAR:
7847 case CPP_CHAR16:
7848 case CPP_CHAR32:
7849 case CPP_WCHAR:
7850 expr.value = c_parser_peek_token (parser)->value;
7851 /* For the purpose of warning when a pointer is compared with
7852 a zero character constant. */
7853 expr.original_type = char_type_node;
7854 set_c_expr_source_range (&expr, tok_range);
7855 c_parser_consume_token (parser);
7856 break;
7857 case CPP_STRING:
7858 case CPP_STRING16:
7859 case CPP_STRING32:
7860 case CPP_WSTRING:
7861 case CPP_UTF8STRING:
7862 expr.value = c_parser_peek_token (parser)->value;
7863 set_c_expr_source_range (&expr, tok_range);
7864 expr.original_code = STRING_CST;
7865 c_parser_consume_token (parser);
7866 break;
7867 case CPP_OBJC_STRING:
7868 gcc_assert (c_dialect_objc ());
7869 expr.value
7870 = objc_build_string_object (c_parser_peek_token (parser)->value);
7871 set_c_expr_source_range (&expr, tok_range);
7872 c_parser_consume_token (parser);
7873 break;
7874 case CPP_NAME:
7875 switch (c_parser_peek_token (parser)->id_kind)
7877 case C_ID_ID:
7879 tree id = c_parser_peek_token (parser)->value;
7880 c_parser_consume_token (parser);
7881 expr.value = build_external_ref (loc, id,
7882 (c_parser_peek_token (parser)->type
7883 == CPP_OPEN_PAREN),
7884 &expr.original_type);
7885 set_c_expr_source_range (&expr, tok_range);
7886 break;
7888 case C_ID_CLASSNAME:
7890 /* Here we parse the Objective-C 2.0 Class.name dot
7891 syntax. */
7892 tree class_name = c_parser_peek_token (parser)->value;
7893 tree component;
7894 c_parser_consume_token (parser);
7895 gcc_assert (c_dialect_objc ());
7896 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7898 expr.set_error ();
7899 break;
7901 if (c_parser_next_token_is_not (parser, CPP_NAME))
7903 c_parser_error (parser, "expected identifier");
7904 expr.set_error ();
7905 break;
7907 c_token *component_tok = c_parser_peek_token (parser);
7908 component = component_tok->value;
7909 location_t end_loc = component_tok->get_finish ();
7910 c_parser_consume_token (parser);
7911 expr.value = objc_build_class_component_ref (class_name,
7912 component);
7913 set_c_expr_source_range (&expr, loc, end_loc);
7914 break;
7916 default:
7917 c_parser_error (parser, "expected expression");
7918 expr.set_error ();
7919 break;
7921 break;
7922 case CPP_OPEN_PAREN:
7923 /* A parenthesized expression, statement expression or compound
7924 literal. */
7925 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7927 /* A statement expression. */
7928 tree stmt;
7929 location_t brace_loc;
7930 c_parser_consume_token (parser);
7931 brace_loc = c_parser_peek_token (parser)->location;
7932 c_parser_consume_token (parser);
7933 /* If we've not yet started the current function's statement list,
7934 or we're in the parameter scope of an old-style function
7935 declaration, statement expressions are not allowed. */
7936 if (!building_stmt_list_p () || old_style_parameter_scope ())
7938 error_at (loc, "braced-group within expression allowed "
7939 "only inside a function");
7940 parser->error = true;
7941 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7943 expr.set_error ();
7944 break;
7946 stmt = c_begin_stmt_expr ();
7947 c_parser_compound_statement_nostart (parser);
7948 location_t close_loc = c_parser_peek_token (parser)->location;
7949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7950 "expected %<)%>");
7951 pedwarn (loc, OPT_Wpedantic,
7952 "ISO C forbids braced-groups within expressions");
7953 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7954 set_c_expr_source_range (&expr, loc, close_loc);
7955 mark_exp_read (expr.value);
7957 else
7959 /* A parenthesized expression. */
7960 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7961 c_parser_consume_token (parser);
7962 expr = c_parser_expression (parser);
7963 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7964 TREE_NO_WARNING (expr.value) = 1;
7965 if (expr.original_code != C_MAYBE_CONST_EXPR
7966 && expr.original_code != SIZEOF_EXPR)
7967 expr.original_code = ERROR_MARK;
7968 /* Don't change EXPR.ORIGINAL_TYPE. */
7969 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7970 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7971 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7972 "expected %<)%>", loc_open_paren);
7974 break;
7975 case CPP_KEYWORD:
7976 switch (c_parser_peek_token (parser)->keyword)
7978 case RID_FUNCTION_NAME:
7979 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7980 "%<__FUNCTION__%> predefined identifier");
7981 expr.value = fname_decl (loc,
7982 c_parser_peek_token (parser)->keyword,
7983 c_parser_peek_token (parser)->value);
7984 set_c_expr_source_range (&expr, loc, loc);
7985 c_parser_consume_token (parser);
7986 break;
7987 case RID_PRETTY_FUNCTION_NAME:
7988 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7989 "%<__PRETTY_FUNCTION__%> predefined identifier");
7990 expr.value = fname_decl (loc,
7991 c_parser_peek_token (parser)->keyword,
7992 c_parser_peek_token (parser)->value);
7993 set_c_expr_source_range (&expr, loc, loc);
7994 c_parser_consume_token (parser);
7995 break;
7996 case RID_C99_FUNCTION_NAME:
7997 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7998 "%<__func__%> predefined identifier");
7999 expr.value = fname_decl (loc,
8000 c_parser_peek_token (parser)->keyword,
8001 c_parser_peek_token (parser)->value);
8002 set_c_expr_source_range (&expr, loc, loc);
8003 c_parser_consume_token (parser);
8004 break;
8005 case RID_VA_ARG:
8007 location_t start_loc = loc;
8008 c_parser_consume_token (parser);
8009 matching_parens parens;
8010 if (!parens.require_open (parser))
8012 expr.set_error ();
8013 break;
8015 e1 = c_parser_expr_no_commas (parser, NULL);
8016 mark_exp_read (e1.value);
8017 e1.value = c_fully_fold (e1.value, false, NULL);
8018 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8020 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8021 expr.set_error ();
8022 break;
8024 loc = c_parser_peek_token (parser)->location;
8025 t1 = c_parser_type_name (parser);
8026 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8028 "expected %<)%>");
8029 if (t1 == NULL)
8031 expr.set_error ();
8033 else
8035 tree type_expr = NULL_TREE;
8036 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8037 groktypename (t1, &type_expr, NULL));
8038 if (type_expr)
8040 expr.value = build2 (C_MAYBE_CONST_EXPR,
8041 TREE_TYPE (expr.value), type_expr,
8042 expr.value);
8043 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8045 set_c_expr_source_range (&expr, start_loc, end_loc);
8048 break;
8049 case RID_OFFSETOF:
8051 c_parser_consume_token (parser);
8052 matching_parens parens;
8053 if (!parens.require_open (parser))
8055 expr.set_error ();
8056 break;
8058 t1 = c_parser_type_name (parser);
8059 if (t1 == NULL)
8060 parser->error = true;
8061 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8062 gcc_assert (parser->error);
8063 if (parser->error)
8065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8066 expr.set_error ();
8067 break;
8069 tree type = groktypename (t1, NULL, NULL);
8070 tree offsetof_ref;
8071 if (type == error_mark_node)
8072 offsetof_ref = error_mark_node;
8073 else
8075 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8076 SET_EXPR_LOCATION (offsetof_ref, loc);
8078 /* Parse the second argument to __builtin_offsetof. We
8079 must have one identifier, and beyond that we want to
8080 accept sub structure and sub array references. */
8081 if (c_parser_next_token_is (parser, CPP_NAME))
8083 c_token *comp_tok = c_parser_peek_token (parser);
8084 offsetof_ref = build_component_ref
8085 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8086 c_parser_consume_token (parser);
8087 while (c_parser_next_token_is (parser, CPP_DOT)
8088 || c_parser_next_token_is (parser,
8089 CPP_OPEN_SQUARE)
8090 || c_parser_next_token_is (parser,
8091 CPP_DEREF))
8093 if (c_parser_next_token_is (parser, CPP_DEREF))
8095 loc = c_parser_peek_token (parser)->location;
8096 offsetof_ref = build_array_ref (loc,
8097 offsetof_ref,
8098 integer_zero_node);
8099 goto do_dot;
8101 else if (c_parser_next_token_is (parser, CPP_DOT))
8103 do_dot:
8104 c_parser_consume_token (parser);
8105 if (c_parser_next_token_is_not (parser,
8106 CPP_NAME))
8108 c_parser_error (parser, "expected identifier");
8109 break;
8111 c_token *comp_tok = c_parser_peek_token (parser);
8112 offsetof_ref = build_component_ref
8113 (loc, offsetof_ref, comp_tok->value,
8114 comp_tok->location);
8115 c_parser_consume_token (parser);
8117 else
8119 struct c_expr ce;
8120 tree idx;
8121 loc = c_parser_peek_token (parser)->location;
8122 c_parser_consume_token (parser);
8123 ce = c_parser_expression (parser);
8124 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8125 idx = ce.value;
8126 idx = c_fully_fold (idx, false, NULL);
8127 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8128 "expected %<]%>");
8129 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8133 else
8134 c_parser_error (parser, "expected identifier");
8135 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8136 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8137 "expected %<)%>");
8138 expr.value = fold_offsetof (offsetof_ref);
8139 set_c_expr_source_range (&expr, loc, end_loc);
8141 break;
8142 case RID_CHOOSE_EXPR:
8144 vec<c_expr_t, va_gc> *cexpr_list;
8145 c_expr_t *e1_p, *e2_p, *e3_p;
8146 tree c;
8147 location_t close_paren_loc;
8149 c_parser_consume_token (parser);
8150 if (!c_parser_get_builtin_args (parser,
8151 "__builtin_choose_expr",
8152 &cexpr_list, true,
8153 &close_paren_loc))
8155 expr.set_error ();
8156 break;
8159 if (vec_safe_length (cexpr_list) != 3)
8161 error_at (loc, "wrong number of arguments to "
8162 "%<__builtin_choose_expr%>");
8163 expr.set_error ();
8164 break;
8167 e1_p = &(*cexpr_list)[0];
8168 e2_p = &(*cexpr_list)[1];
8169 e3_p = &(*cexpr_list)[2];
8171 c = e1_p->value;
8172 mark_exp_read (e2_p->value);
8173 mark_exp_read (e3_p->value);
8174 if (TREE_CODE (c) != INTEGER_CST
8175 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8176 error_at (loc,
8177 "first argument to %<__builtin_choose_expr%> not"
8178 " a constant");
8179 constant_expression_warning (c);
8180 expr = integer_zerop (c) ? *e3_p : *e2_p;
8181 set_c_expr_source_range (&expr, loc, close_paren_loc);
8182 break;
8184 case RID_TYPES_COMPATIBLE_P:
8186 c_parser_consume_token (parser);
8187 matching_parens parens;
8188 if (!parens.require_open (parser))
8190 expr.set_error ();
8191 break;
8193 t1 = c_parser_type_name (parser);
8194 if (t1 == NULL)
8196 expr.set_error ();
8197 break;
8199 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8201 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8202 expr.set_error ();
8203 break;
8205 t2 = c_parser_type_name (parser);
8206 if (t2 == NULL)
8208 expr.set_error ();
8209 break;
8211 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8212 parens.skip_until_found_close (parser);
8213 tree e1, e2;
8214 e1 = groktypename (t1, NULL, NULL);
8215 e2 = groktypename (t2, NULL, NULL);
8216 if (e1 == error_mark_node || e2 == error_mark_node)
8218 expr.set_error ();
8219 break;
8222 e1 = TYPE_MAIN_VARIANT (e1);
8223 e2 = TYPE_MAIN_VARIANT (e2);
8225 expr.value
8226 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8227 set_c_expr_source_range (&expr, loc, close_paren_loc);
8229 break;
8230 case RID_BUILTIN_TGMATH:
8232 vec<c_expr_t, va_gc> *cexpr_list;
8233 location_t close_paren_loc;
8235 c_parser_consume_token (parser);
8236 if (!c_parser_get_builtin_args (parser,
8237 "__builtin_tgmath",
8238 &cexpr_list, false,
8239 &close_paren_loc))
8241 expr.set_error ();
8242 break;
8245 if (vec_safe_length (cexpr_list) < 3)
8247 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8248 expr.set_error ();
8249 break;
8252 unsigned int i;
8253 c_expr_t *p;
8254 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8255 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8256 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8257 if (nargs == 0)
8259 expr.set_error ();
8260 break;
8262 if (vec_safe_length (cexpr_list) < nargs)
8264 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8265 expr.set_error ();
8266 break;
8268 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8269 if (num_functions < 2)
8271 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8272 expr.set_error ();
8273 break;
8276 /* The first NUM_FUNCTIONS expressions are the function
8277 pointers. The remaining NARGS expressions are the
8278 arguments that are to be passed to one of those
8279 functions, chosen following <tgmath.h> rules. */
8280 for (unsigned int j = 1; j < num_functions; j++)
8282 unsigned int this_nargs
8283 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8284 if (this_nargs == 0)
8286 expr.set_error ();
8287 goto out;
8289 if (this_nargs != nargs)
8291 error_at ((*cexpr_list)[j].get_location (),
8292 "argument %u of %<__builtin_tgmath%> has "
8293 "wrong number of arguments", j + 1);
8294 expr.set_error ();
8295 goto out;
8299 /* The functions all have the same number of arguments.
8300 Determine whether arguments and return types vary in
8301 ways permitted for <tgmath.h> functions. */
8302 /* The first entry in each of these vectors is for the
8303 return type, subsequent entries for parameter
8304 types. */
8305 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8306 auto_vec<tree> parm_first (nargs + 1);
8307 auto_vec<bool> parm_complex (nargs + 1);
8308 auto_vec<bool> parm_varies (nargs + 1);
8309 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8310 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8311 parm_first.quick_push (first_ret);
8312 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8313 parm_varies.quick_push (false);
8314 function_args_iterator iter;
8315 tree t;
8316 unsigned int argpos;
8317 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8319 if (t == void_type_node)
8320 break;
8321 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8322 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8323 parm_varies.quick_push (false);
8325 for (unsigned int j = 1; j < num_functions; j++)
8327 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8328 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8329 if (ret != parm_first[0])
8331 parm_varies[0] = true;
8332 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8333 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8335 error_at ((*cexpr_list)[0].get_location (),
8336 "invalid type-generic return type for "
8337 "argument %u of %<__builtin_tgmath%>",
8339 expr.set_error ();
8340 goto out;
8342 if (!SCALAR_FLOAT_TYPE_P (ret)
8343 && !COMPLEX_FLOAT_TYPE_P (ret))
8345 error_at ((*cexpr_list)[j].get_location (),
8346 "invalid type-generic return type for "
8347 "argument %u of %<__builtin_tgmath%>",
8348 j + 1);
8349 expr.set_error ();
8350 goto out;
8353 if (TREE_CODE (ret) == COMPLEX_TYPE)
8354 parm_complex[0] = true;
8355 argpos = 1;
8356 FOREACH_FUNCTION_ARGS (type, t, iter)
8358 if (t == void_type_node)
8359 break;
8360 t = TYPE_MAIN_VARIANT (t);
8361 if (t != parm_first[argpos])
8363 parm_varies[argpos] = true;
8364 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8365 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8367 error_at ((*cexpr_list)[0].get_location (),
8368 "invalid type-generic type for "
8369 "argument %u of argument %u of "
8370 "%<__builtin_tgmath%>", argpos, 1);
8371 expr.set_error ();
8372 goto out;
8374 if (!SCALAR_FLOAT_TYPE_P (t)
8375 && !COMPLEX_FLOAT_TYPE_P (t))
8377 error_at ((*cexpr_list)[j].get_location (),
8378 "invalid type-generic type for "
8379 "argument %u of argument %u of "
8380 "%<__builtin_tgmath%>", argpos, j + 1);
8381 expr.set_error ();
8382 goto out;
8385 if (TREE_CODE (t) == COMPLEX_TYPE)
8386 parm_complex[argpos] = true;
8387 argpos++;
8390 enum tgmath_parm_kind max_variation = tgmath_fixed;
8391 for (unsigned int j = 0; j <= nargs; j++)
8393 enum tgmath_parm_kind this_kind;
8394 if (parm_varies[j])
8396 if (parm_complex[j])
8397 max_variation = this_kind = tgmath_complex;
8398 else
8400 this_kind = tgmath_real;
8401 if (max_variation != tgmath_complex)
8402 max_variation = tgmath_real;
8405 else
8406 this_kind = tgmath_fixed;
8407 parm_kind.quick_push (this_kind);
8409 if (max_variation == tgmath_fixed)
8411 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8412 "all have the same type");
8413 expr.set_error ();
8414 break;
8417 /* Identify a parameter (not the return type) that varies,
8418 including with complex types if any variation includes
8419 complex types; there must be at least one such
8420 parameter. */
8421 unsigned int tgarg = 0;
8422 for (unsigned int j = 1; j <= nargs; j++)
8423 if (parm_kind[j] == max_variation)
8425 tgarg = j;
8426 break;
8428 if (tgarg == 0)
8430 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8431 "lack type-generic parameter");
8432 expr.set_error ();
8433 break;
8436 /* Determine the type of the relevant parameter for each
8437 function. */
8438 auto_vec<tree> tg_type (num_functions);
8439 for (unsigned int j = 0; j < num_functions; j++)
8441 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8442 argpos = 1;
8443 FOREACH_FUNCTION_ARGS (type, t, iter)
8445 if (argpos == tgarg)
8447 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8448 break;
8450 argpos++;
8454 /* Verify that the corresponding types are different for
8455 all the listed functions. Also determine whether all
8456 the types are complex, whether all the types are
8457 standard or binary, and whether all the types are
8458 decimal. */
8459 bool all_complex = true;
8460 bool all_binary = true;
8461 bool all_decimal = true;
8462 hash_set<tree> tg_types;
8463 FOR_EACH_VEC_ELT (tg_type, i, t)
8465 if (TREE_CODE (t) == COMPLEX_TYPE)
8466 all_decimal = false;
8467 else
8469 all_complex = false;
8470 if (DECIMAL_FLOAT_TYPE_P (t))
8471 all_binary = false;
8472 else
8473 all_decimal = false;
8475 if (tg_types.add (t))
8477 error_at ((*cexpr_list)[i].get_location (),
8478 "duplicate type-generic parameter type for "
8479 "function argument %u of %<__builtin_tgmath%>",
8480 i + 1);
8481 expr.set_error ();
8482 goto out;
8486 /* Verify that other parameters and the return type whose
8487 types vary have their types varying in the correct
8488 way. */
8489 for (unsigned int j = 0; j < num_functions; j++)
8491 tree exp_type = tg_type[j];
8492 tree exp_real_type = exp_type;
8493 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8494 exp_real_type = TREE_TYPE (exp_type);
8495 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8496 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8497 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8498 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8500 error_at ((*cexpr_list)[j].get_location (),
8501 "bad return type for function argument %u "
8502 "of %<__builtin_tgmath%>", j + 1);
8503 expr.set_error ();
8504 goto out;
8506 argpos = 1;
8507 FOREACH_FUNCTION_ARGS (type, t, iter)
8509 if (t == void_type_node)
8510 break;
8511 t = TYPE_MAIN_VARIANT (t);
8512 if ((parm_kind[argpos] == tgmath_complex
8513 && t != exp_type)
8514 || (parm_kind[argpos] == tgmath_real
8515 && t != exp_real_type))
8517 error_at ((*cexpr_list)[j].get_location (),
8518 "bad type for argument %u of "
8519 "function argument %u of "
8520 "%<__builtin_tgmath%>", argpos, j + 1);
8521 expr.set_error ();
8522 goto out;
8524 argpos++;
8528 /* The functions listed are a valid set of functions for a
8529 <tgmath.h> macro to select between. Identify the
8530 matching function, if any. First, the argument types
8531 must be combined following <tgmath.h> rules. Integer
8532 types are treated as _Decimal64 if any type-generic
8533 argument is decimal, or if the only alternatives for
8534 type-generic arguments are of decimal types, and are
8535 otherwise treated as double (or _Complex double for
8536 complex integer types, or _Float64 or _Complex _Float64
8537 if all the return types are the same _FloatN or
8538 _FloatNx type). After that adjustment, types are
8539 combined following the usual arithmetic conversions.
8540 If the function only accepts complex arguments, a
8541 complex type is produced. */
8542 bool arg_complex = all_complex;
8543 bool arg_binary = all_binary;
8544 bool arg_int_decimal = all_decimal;
8545 for (unsigned int j = 1; j <= nargs; j++)
8547 if (parm_kind[j] == tgmath_fixed)
8548 continue;
8549 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8550 tree type = TREE_TYPE (ce->value);
8551 if (!INTEGRAL_TYPE_P (type)
8552 && !SCALAR_FLOAT_TYPE_P (type)
8553 && TREE_CODE (type) != COMPLEX_TYPE)
8555 error_at (ce->get_location (),
8556 "invalid type of argument %u of type-generic "
8557 "function", j);
8558 expr.set_error ();
8559 goto out;
8561 if (DECIMAL_FLOAT_TYPE_P (type))
8563 arg_int_decimal = true;
8564 if (all_complex)
8566 error_at (ce->get_location (),
8567 "decimal floating-point argument %u to "
8568 "complex-only type-generic function", j);
8569 expr.set_error ();
8570 goto out;
8572 else if (all_binary)
8574 error_at (ce->get_location (),
8575 "decimal floating-point argument %u to "
8576 "binary-only type-generic function", j);
8577 expr.set_error ();
8578 goto out;
8580 else if (arg_complex)
8582 error_at (ce->get_location (),
8583 "both complex and decimal floating-point "
8584 "arguments to type-generic function");
8585 expr.set_error ();
8586 goto out;
8588 else if (arg_binary)
8590 error_at (ce->get_location (),
8591 "both binary and decimal floating-point "
8592 "arguments to type-generic function");
8593 expr.set_error ();
8594 goto out;
8597 else if (TREE_CODE (type) == COMPLEX_TYPE)
8599 arg_complex = true;
8600 if (COMPLEX_FLOAT_TYPE_P (type))
8601 arg_binary = true;
8602 if (all_decimal)
8604 error_at (ce->get_location (),
8605 "complex argument %u to "
8606 "decimal-only type-generic function", j);
8607 expr.set_error ();
8608 goto out;
8610 else if (arg_int_decimal)
8612 error_at (ce->get_location (),
8613 "both complex and decimal floating-point "
8614 "arguments to type-generic function");
8615 expr.set_error ();
8616 goto out;
8619 else if (SCALAR_FLOAT_TYPE_P (type))
8621 arg_binary = true;
8622 if (all_decimal)
8624 error_at (ce->get_location (),
8625 "binary argument %u to "
8626 "decimal-only type-generic function", j);
8627 expr.set_error ();
8628 goto out;
8630 else if (arg_int_decimal)
8632 error_at (ce->get_location (),
8633 "both binary and decimal floating-point "
8634 "arguments to type-generic function");
8635 expr.set_error ();
8636 goto out;
8640 /* For a macro rounding its result to a narrower type, map
8641 integer types to _Float64 not double if the return type
8642 is a _FloatN or _FloatNx type. */
8643 bool arg_int_float64 = false;
8644 if (parm_kind[0] == tgmath_fixed
8645 && SCALAR_FLOAT_TYPE_P (parm_first[0])
8646 && float64_type_node != NULL_TREE)
8647 for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
8648 if (parm_first[0] == FLOATN_TYPE_NODE (j))
8650 arg_int_float64 = true;
8651 break;
8653 tree arg_real = NULL_TREE;
8654 for (unsigned int j = 1; j <= nargs; j++)
8656 if (parm_kind[j] == tgmath_fixed)
8657 continue;
8658 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8659 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8660 if (TREE_CODE (type) == COMPLEX_TYPE)
8661 type = TREE_TYPE (type);
8662 if (INTEGRAL_TYPE_P (type))
8663 type = (arg_int_decimal
8664 ? dfloat64_type_node
8665 : arg_int_float64
8666 ? float64_type_node
8667 : double_type_node);
8668 if (arg_real == NULL_TREE)
8669 arg_real = type;
8670 else
8671 arg_real = common_type (arg_real, type);
8672 if (arg_real == error_mark_node)
8674 expr.set_error ();
8675 goto out;
8678 tree arg_type = (arg_complex
8679 ? build_complex_type (arg_real)
8680 : arg_real);
8682 /* Look for a function to call with type-generic parameter
8683 type ARG_TYPE. */
8684 c_expr_t *fn = NULL;
8685 for (unsigned int j = 0; j < num_functions; j++)
8687 if (tg_type[j] == arg_type)
8689 fn = &(*cexpr_list)[j];
8690 break;
8693 if (fn == NULL
8694 && parm_kind[0] == tgmath_fixed
8695 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8697 /* Presume this is a macro that rounds its result to a
8698 narrower type, and look for the first function with
8699 at least the range and precision of the argument
8700 type. */
8701 for (unsigned int j = 0; j < num_functions; j++)
8703 if (arg_complex
8704 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8705 continue;
8706 tree real_tg_type = (arg_complex
8707 ? TREE_TYPE (tg_type[j])
8708 : tg_type[j]);
8709 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8710 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8711 continue;
8712 scalar_float_mode arg_mode
8713 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8714 scalar_float_mode tg_mode
8715 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8716 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8717 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8718 if (arg_fmt->b == tg_fmt->b
8719 && arg_fmt->p <= tg_fmt->p
8720 && arg_fmt->emax <= tg_fmt->emax
8721 && (arg_fmt->emin - arg_fmt->p
8722 >= tg_fmt->emin - tg_fmt->p))
8724 fn = &(*cexpr_list)[j];
8725 break;
8729 if (fn == NULL)
8731 error_at (loc, "no matching function for type-generic call");
8732 expr.set_error ();
8733 break;
8736 /* Construct a call to FN. */
8737 vec<tree, va_gc> *args;
8738 vec_alloc (args, nargs);
8739 vec<tree, va_gc> *origtypes;
8740 vec_alloc (origtypes, nargs);
8741 auto_vec<location_t> arg_loc (nargs);
8742 for (unsigned int j = 0; j < nargs; j++)
8744 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8745 args->quick_push (ce->value);
8746 arg_loc.quick_push (ce->get_location ());
8747 origtypes->quick_push (ce->original_type);
8749 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8750 args, origtypes);
8751 set_c_expr_source_range (&expr, loc, close_paren_loc);
8752 break;
8754 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8756 vec<c_expr_t, va_gc> *cexpr_list;
8757 c_expr_t *e2_p;
8758 tree chain_value;
8759 location_t close_paren_loc;
8761 c_parser_consume_token (parser);
8762 if (!c_parser_get_builtin_args (parser,
8763 "__builtin_call_with_static_chain",
8764 &cexpr_list, false,
8765 &close_paren_loc))
8767 expr.set_error ();
8768 break;
8770 if (vec_safe_length (cexpr_list) != 2)
8772 error_at (loc, "wrong number of arguments to "
8773 "%<__builtin_call_with_static_chain%>");
8774 expr.set_error ();
8775 break;
8778 expr = (*cexpr_list)[0];
8779 e2_p = &(*cexpr_list)[1];
8780 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8781 chain_value = e2_p->value;
8782 mark_exp_read (chain_value);
8784 if (TREE_CODE (expr.value) != CALL_EXPR)
8785 error_at (loc, "first argument to "
8786 "%<__builtin_call_with_static_chain%> "
8787 "must be a call expression");
8788 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8789 error_at (loc, "second argument to "
8790 "%<__builtin_call_with_static_chain%> "
8791 "must be a pointer type");
8792 else
8793 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8794 set_c_expr_source_range (&expr, loc, close_paren_loc);
8795 break;
8797 case RID_BUILTIN_COMPLEX:
8799 vec<c_expr_t, va_gc> *cexpr_list;
8800 c_expr_t *e1_p, *e2_p;
8801 location_t close_paren_loc;
8803 c_parser_consume_token (parser);
8804 if (!c_parser_get_builtin_args (parser,
8805 "__builtin_complex",
8806 &cexpr_list, false,
8807 &close_paren_loc))
8809 expr.set_error ();
8810 break;
8813 if (vec_safe_length (cexpr_list) != 2)
8815 error_at (loc, "wrong number of arguments to "
8816 "%<__builtin_complex%>");
8817 expr.set_error ();
8818 break;
8821 e1_p = &(*cexpr_list)[0];
8822 e2_p = &(*cexpr_list)[1];
8824 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8825 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8826 e1_p->value = convert (TREE_TYPE (e1_p->value),
8827 TREE_OPERAND (e1_p->value, 0));
8828 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8829 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8830 e2_p->value = convert (TREE_TYPE (e2_p->value),
8831 TREE_OPERAND (e2_p->value, 0));
8832 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8833 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8834 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8835 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8837 error_at (loc, "%<__builtin_complex%> operand "
8838 "not of real binary floating-point type");
8839 expr.set_error ();
8840 break;
8842 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8843 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8845 error_at (loc,
8846 "%<__builtin_complex%> operands of different types");
8847 expr.set_error ();
8848 break;
8850 pedwarn_c90 (loc, OPT_Wpedantic,
8851 "ISO C90 does not support complex types");
8852 expr.value = build2_loc (loc, COMPLEX_EXPR,
8853 build_complex_type
8854 (TYPE_MAIN_VARIANT
8855 (TREE_TYPE (e1_p->value))),
8856 e1_p->value, e2_p->value);
8857 set_c_expr_source_range (&expr, loc, close_paren_loc);
8858 break;
8860 case RID_BUILTIN_SHUFFLE:
8862 vec<c_expr_t, va_gc> *cexpr_list;
8863 unsigned int i;
8864 c_expr_t *p;
8865 location_t close_paren_loc;
8867 c_parser_consume_token (parser);
8868 if (!c_parser_get_builtin_args (parser,
8869 "__builtin_shuffle",
8870 &cexpr_list, false,
8871 &close_paren_loc))
8873 expr.set_error ();
8874 break;
8877 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8878 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8880 if (vec_safe_length (cexpr_list) == 2)
8881 expr.value =
8882 c_build_vec_perm_expr
8883 (loc, (*cexpr_list)[0].value,
8884 NULL_TREE, (*cexpr_list)[1].value);
8886 else if (vec_safe_length (cexpr_list) == 3)
8887 expr.value =
8888 c_build_vec_perm_expr
8889 (loc, (*cexpr_list)[0].value,
8890 (*cexpr_list)[1].value,
8891 (*cexpr_list)[2].value);
8892 else
8894 error_at (loc, "wrong number of arguments to "
8895 "%<__builtin_shuffle%>");
8896 expr.set_error ();
8898 set_c_expr_source_range (&expr, loc, close_paren_loc);
8899 break;
8901 case RID_AT_SELECTOR:
8903 gcc_assert (c_dialect_objc ());
8904 c_parser_consume_token (parser);
8905 matching_parens parens;
8906 if (!parens.require_open (parser))
8908 expr.set_error ();
8909 break;
8911 tree sel = c_parser_objc_selector_arg (parser);
8912 location_t close_loc = c_parser_peek_token (parser)->location;
8913 parens.skip_until_found_close (parser);
8914 expr.value = objc_build_selector_expr (loc, sel);
8915 set_c_expr_source_range (&expr, loc, close_loc);
8917 break;
8918 case RID_AT_PROTOCOL:
8920 gcc_assert (c_dialect_objc ());
8921 c_parser_consume_token (parser);
8922 matching_parens parens;
8923 if (!parens.require_open (parser))
8925 expr.set_error ();
8926 break;
8928 if (c_parser_next_token_is_not (parser, CPP_NAME))
8930 c_parser_error (parser, "expected identifier");
8931 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8932 expr.set_error ();
8933 break;
8935 tree id = c_parser_peek_token (parser)->value;
8936 c_parser_consume_token (parser);
8937 location_t close_loc = c_parser_peek_token (parser)->location;
8938 parens.skip_until_found_close (parser);
8939 expr.value = objc_build_protocol_expr (id);
8940 set_c_expr_source_range (&expr, loc, close_loc);
8942 break;
8943 case RID_AT_ENCODE:
8945 /* Extension to support C-structures in the archiver. */
8946 gcc_assert (c_dialect_objc ());
8947 c_parser_consume_token (parser);
8948 matching_parens parens;
8949 if (!parens.require_open (parser))
8951 expr.set_error ();
8952 break;
8954 t1 = c_parser_type_name (parser);
8955 if (t1 == NULL)
8957 expr.set_error ();
8958 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8959 break;
8961 location_t close_loc = c_parser_peek_token (parser)->location;
8962 parens.skip_until_found_close (parser);
8963 tree type = groktypename (t1, NULL, NULL);
8964 expr.value = objc_build_encode_expr (type);
8965 set_c_expr_source_range (&expr, loc, close_loc);
8967 break;
8968 case RID_GENERIC:
8969 expr = c_parser_generic_selection (parser);
8970 break;
8971 default:
8972 c_parser_error (parser, "expected expression");
8973 expr.set_error ();
8974 break;
8976 break;
8977 case CPP_OPEN_SQUARE:
8978 if (c_dialect_objc ())
8980 tree receiver, args;
8981 c_parser_consume_token (parser);
8982 receiver = c_parser_objc_receiver (parser);
8983 args = c_parser_objc_message_args (parser);
8984 location_t close_loc = c_parser_peek_token (parser)->location;
8985 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8986 "expected %<]%>");
8987 expr.value = objc_build_message_expr (receiver, args);
8988 set_c_expr_source_range (&expr, loc, close_loc);
8989 break;
8991 /* Else fall through to report error. */
8992 /* FALLTHRU */
8993 default:
8994 c_parser_error (parser, "expected expression");
8995 expr.set_error ();
8996 break;
8998 out:
8999 return c_parser_postfix_expression_after_primary
9000 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9003 /* Parse a postfix expression after a parenthesized type name: the
9004 brace-enclosed initializer of a compound literal, possibly followed
9005 by some postfix operators. This is separate because it is not
9006 possible to tell until after the type name whether a cast
9007 expression has a cast or a compound literal, or whether the operand
9008 of sizeof is a parenthesized type name or starts with a compound
9009 literal. TYPE_LOC is the location where TYPE_NAME starts--the
9010 location of the first token after the parentheses around the type
9011 name. */
9013 static struct c_expr
9014 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9015 struct c_type_name *type_name,
9016 location_t type_loc)
9018 tree type;
9019 struct c_expr init;
9020 bool non_const;
9021 struct c_expr expr;
9022 location_t start_loc;
9023 tree type_expr = NULL_TREE;
9024 bool type_expr_const = true;
9025 check_compound_literal_type (type_loc, type_name);
9026 rich_location richloc (line_table, type_loc);
9027 start_init (NULL_TREE, NULL, 0, &richloc);
9028 type = groktypename (type_name, &type_expr, &type_expr_const);
9029 start_loc = c_parser_peek_token (parser)->location;
9030 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9032 error_at (type_loc, "compound literal has variable size");
9033 type = error_mark_node;
9035 init = c_parser_braced_init (parser, type, false, NULL);
9036 finish_init ();
9037 maybe_warn_string_init (type_loc, type, init);
9039 if (type != error_mark_node
9040 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9041 && current_function_decl)
9043 error ("compound literal qualified by address-space qualifier");
9044 type = error_mark_node;
9047 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9048 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9049 ? CONSTRUCTOR_NON_CONST (init.value)
9050 : init.original_code == C_MAYBE_CONST_EXPR);
9051 non_const |= !type_expr_const;
9052 unsigned int alignas_align = 0;
9053 if (type != error_mark_node
9054 && type_name->specs->align_log != -1)
9056 alignas_align = 1U << type_name->specs->align_log;
9057 if (alignas_align < min_align_of_type (type))
9059 error_at (type_name->specs->locations[cdw_alignas],
9060 "%<_Alignas%> specifiers cannot reduce "
9061 "alignment of compound literal");
9062 alignas_align = 0;
9065 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9066 alignas_align);
9067 set_c_expr_source_range (&expr, init.src_range);
9068 expr.original_code = ERROR_MARK;
9069 expr.original_type = NULL;
9070 if (type != error_mark_node
9071 && expr.value != error_mark_node
9072 && type_expr)
9074 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9076 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9077 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9079 else
9081 gcc_assert (!non_const);
9082 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9083 type_expr, expr.value);
9086 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9089 /* Callback function for sizeof_pointer_memaccess_warning to compare
9090 types. */
9092 static bool
9093 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9095 return comptypes (type1, type2) == 1;
9098 /* Parse a postfix expression after the initial primary or compound
9099 literal; that is, parse a series of postfix operators.
9101 EXPR_LOC is the location of the primary expression. */
9103 static struct c_expr
9104 c_parser_postfix_expression_after_primary (c_parser *parser,
9105 location_t expr_loc,
9106 struct c_expr expr)
9108 struct c_expr orig_expr;
9109 tree ident, idx;
9110 location_t sizeof_arg_loc[3], comp_loc;
9111 tree sizeof_arg[3];
9112 unsigned int literal_zero_mask;
9113 unsigned int i;
9114 vec<tree, va_gc> *exprlist;
9115 vec<tree, va_gc> *origtypes = NULL;
9116 vec<location_t> arg_loc = vNULL;
9117 location_t start;
9118 location_t finish;
9120 while (true)
9122 location_t op_loc = c_parser_peek_token (parser)->location;
9123 switch (c_parser_peek_token (parser)->type)
9125 case CPP_OPEN_SQUARE:
9126 /* Array reference. */
9127 c_parser_consume_token (parser);
9128 idx = c_parser_expression (parser).value;
9129 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9130 "expected %<]%>");
9131 start = expr.get_start ();
9132 finish = parser->tokens_buf[0].location;
9133 expr.value = build_array_ref (op_loc, expr.value, idx);
9134 set_c_expr_source_range (&expr, start, finish);
9135 expr.original_code = ERROR_MARK;
9136 expr.original_type = NULL;
9137 break;
9138 case CPP_OPEN_PAREN:
9139 /* Function call. */
9140 c_parser_consume_token (parser);
9141 for (i = 0; i < 3; i++)
9143 sizeof_arg[i] = NULL_TREE;
9144 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9146 literal_zero_mask = 0;
9147 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9148 exprlist = NULL;
9149 else
9150 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9151 sizeof_arg_loc, sizeof_arg,
9152 &arg_loc, &literal_zero_mask);
9153 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9154 "expected %<)%>");
9155 orig_expr = expr;
9156 mark_exp_read (expr.value);
9157 if (warn_sizeof_pointer_memaccess)
9158 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9159 expr.value, exprlist,
9160 sizeof_arg,
9161 sizeof_ptr_memacc_comptypes);
9162 if (TREE_CODE (expr.value) == FUNCTION_DECL
9163 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9164 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9165 && vec_safe_length (exprlist) == 3)
9167 tree arg0 = (*exprlist)[0];
9168 tree arg2 = (*exprlist)[2];
9169 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9172 start = expr.get_start ();
9173 finish = parser->tokens_buf[0].get_finish ();
9174 expr.value
9175 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9176 exprlist, origtypes);
9177 set_c_expr_source_range (&expr, start, finish);
9179 expr.original_code = ERROR_MARK;
9180 if (TREE_CODE (expr.value) == INTEGER_CST
9181 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9182 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9183 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9184 expr.original_code = C_MAYBE_CONST_EXPR;
9185 expr.original_type = NULL;
9186 if (exprlist)
9188 release_tree_vector (exprlist);
9189 release_tree_vector (origtypes);
9191 arg_loc.release ();
9192 break;
9193 case CPP_DOT:
9194 /* Structure element reference. */
9195 c_parser_consume_token (parser);
9196 expr = default_function_array_conversion (expr_loc, expr);
9197 if (c_parser_next_token_is (parser, CPP_NAME))
9199 c_token *comp_tok = c_parser_peek_token (parser);
9200 ident = comp_tok->value;
9201 comp_loc = comp_tok->location;
9203 else
9205 c_parser_error (parser, "expected identifier");
9206 expr.set_error ();
9207 expr.original_code = ERROR_MARK;
9208 expr.original_type = NULL;
9209 return expr;
9211 start = expr.get_start ();
9212 finish = c_parser_peek_token (parser)->get_finish ();
9213 c_parser_consume_token (parser);
9214 expr.value = build_component_ref (op_loc, expr.value, ident,
9215 comp_loc);
9216 set_c_expr_source_range (&expr, start, finish);
9217 expr.original_code = ERROR_MARK;
9218 if (TREE_CODE (expr.value) != COMPONENT_REF)
9219 expr.original_type = NULL;
9220 else
9222 /* Remember the original type of a bitfield. */
9223 tree field = TREE_OPERAND (expr.value, 1);
9224 if (TREE_CODE (field) != FIELD_DECL)
9225 expr.original_type = NULL;
9226 else
9227 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9229 break;
9230 case CPP_DEREF:
9231 /* Structure element reference. */
9232 c_parser_consume_token (parser);
9233 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9234 if (c_parser_next_token_is (parser, CPP_NAME))
9236 c_token *comp_tok = c_parser_peek_token (parser);
9237 ident = comp_tok->value;
9238 comp_loc = comp_tok->location;
9240 else
9242 c_parser_error (parser, "expected identifier");
9243 expr.set_error ();
9244 expr.original_code = ERROR_MARK;
9245 expr.original_type = NULL;
9246 return expr;
9248 start = expr.get_start ();
9249 finish = c_parser_peek_token (parser)->get_finish ();
9250 c_parser_consume_token (parser);
9251 expr.value = build_component_ref (op_loc,
9252 build_indirect_ref (op_loc,
9253 expr.value,
9254 RO_ARROW),
9255 ident, comp_loc);
9256 set_c_expr_source_range (&expr, start, finish);
9257 expr.original_code = ERROR_MARK;
9258 if (TREE_CODE (expr.value) != COMPONENT_REF)
9259 expr.original_type = NULL;
9260 else
9262 /* Remember the original type of a bitfield. */
9263 tree field = TREE_OPERAND (expr.value, 1);
9264 if (TREE_CODE (field) != FIELD_DECL)
9265 expr.original_type = NULL;
9266 else
9267 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9269 break;
9270 case CPP_PLUS_PLUS:
9271 /* Postincrement. */
9272 start = expr.get_start ();
9273 finish = c_parser_peek_token (parser)->get_finish ();
9274 c_parser_consume_token (parser);
9275 expr = default_function_array_read_conversion (expr_loc, expr);
9276 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9277 expr.value, false);
9278 set_c_expr_source_range (&expr, start, finish);
9279 expr.original_code = ERROR_MARK;
9280 expr.original_type = NULL;
9281 break;
9282 case CPP_MINUS_MINUS:
9283 /* Postdecrement. */
9284 start = expr.get_start ();
9285 finish = c_parser_peek_token (parser)->get_finish ();
9286 c_parser_consume_token (parser);
9287 expr = default_function_array_read_conversion (expr_loc, expr);
9288 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9289 expr.value, false);
9290 set_c_expr_source_range (&expr, start, finish);
9291 expr.original_code = ERROR_MARK;
9292 expr.original_type = NULL;
9293 break;
9294 default:
9295 return expr;
9300 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9302 expression:
9303 assignment-expression
9304 expression , assignment-expression
9307 static struct c_expr
9308 c_parser_expression (c_parser *parser)
9310 location_t tloc = c_parser_peek_token (parser)->location;
9311 struct c_expr expr;
9312 expr = c_parser_expr_no_commas (parser, NULL);
9313 if (c_parser_next_token_is (parser, CPP_COMMA))
9314 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9315 while (c_parser_next_token_is (parser, CPP_COMMA))
9317 struct c_expr next;
9318 tree lhsval;
9319 location_t loc = c_parser_peek_token (parser)->location;
9320 location_t expr_loc;
9321 c_parser_consume_token (parser);
9322 expr_loc = c_parser_peek_token (parser)->location;
9323 lhsval = expr.value;
9324 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9325 lhsval = TREE_OPERAND (lhsval, 1);
9326 if (DECL_P (lhsval) || handled_component_p (lhsval))
9327 mark_exp_read (lhsval);
9328 next = c_parser_expr_no_commas (parser, NULL);
9329 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9330 expr.value = build_compound_expr (loc, expr.value, next.value);
9331 expr.original_code = COMPOUND_EXPR;
9332 expr.original_type = next.original_type;
9334 return expr;
9337 /* Parse an expression and convert functions or arrays to pointers and
9338 lvalues to rvalues. */
9340 static struct c_expr
9341 c_parser_expression_conv (c_parser *parser)
9343 struct c_expr expr;
9344 location_t loc = c_parser_peek_token (parser)->location;
9345 expr = c_parser_expression (parser);
9346 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9347 return expr;
9350 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9351 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9353 static inline void
9354 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9355 unsigned int idx)
9357 if (idx >= HOST_BITS_PER_INT)
9358 return;
9360 c_token *tok = c_parser_peek_token (parser);
9361 switch (tok->type)
9363 case CPP_NUMBER:
9364 case CPP_CHAR:
9365 case CPP_WCHAR:
9366 case CPP_CHAR16:
9367 case CPP_CHAR32:
9368 /* If a parameter is literal zero alone, remember it
9369 for -Wmemset-transposed-args warning. */
9370 if (integer_zerop (tok->value)
9371 && !TREE_OVERFLOW (tok->value)
9372 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9373 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9374 *literal_zero_mask |= 1U << idx;
9375 default:
9376 break;
9380 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9381 functions and arrays to pointers and lvalues to rvalues. If
9382 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9383 locations of function arguments into this vector.
9385 nonempty-expr-list:
9386 assignment-expression
9387 nonempty-expr-list , assignment-expression
9390 static vec<tree, va_gc> *
9391 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9392 vec<tree, va_gc> **p_orig_types,
9393 location_t *sizeof_arg_loc, tree *sizeof_arg,
9394 vec<location_t> *locations,
9395 unsigned int *literal_zero_mask)
9397 vec<tree, va_gc> *ret;
9398 vec<tree, va_gc> *orig_types;
9399 struct c_expr expr;
9400 unsigned int idx = 0;
9402 ret = make_tree_vector ();
9403 if (p_orig_types == NULL)
9404 orig_types = NULL;
9405 else
9406 orig_types = make_tree_vector ();
9408 if (literal_zero_mask)
9409 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9410 expr = c_parser_expr_no_commas (parser, NULL);
9411 if (convert_p)
9412 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9413 if (fold_p)
9414 expr.value = c_fully_fold (expr.value, false, NULL);
9415 ret->quick_push (expr.value);
9416 if (orig_types)
9417 orig_types->quick_push (expr.original_type);
9418 if (locations)
9419 locations->safe_push (expr.get_location ());
9420 if (sizeof_arg != NULL
9421 && expr.original_code == SIZEOF_EXPR)
9423 sizeof_arg[0] = c_last_sizeof_arg;
9424 sizeof_arg_loc[0] = c_last_sizeof_loc;
9426 while (c_parser_next_token_is (parser, CPP_COMMA))
9428 c_parser_consume_token (parser);
9429 if (literal_zero_mask)
9430 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9431 expr = c_parser_expr_no_commas (parser, NULL);
9432 if (convert_p)
9433 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9434 true);
9435 if (fold_p)
9436 expr.value = c_fully_fold (expr.value, false, NULL);
9437 vec_safe_push (ret, expr.value);
9438 if (orig_types)
9439 vec_safe_push (orig_types, expr.original_type);
9440 if (locations)
9441 locations->safe_push (expr.get_location ());
9442 if (++idx < 3
9443 && sizeof_arg != NULL
9444 && expr.original_code == SIZEOF_EXPR)
9446 sizeof_arg[idx] = c_last_sizeof_arg;
9447 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9450 if (orig_types)
9451 *p_orig_types = orig_types;
9452 return ret;
9455 /* Parse Objective-C-specific constructs. */
9457 /* Parse an objc-class-definition.
9459 objc-class-definition:
9460 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9461 objc-class-instance-variables[opt] objc-methodprotolist @end
9462 @implementation identifier objc-superclass[opt]
9463 objc-class-instance-variables[opt]
9464 @interface identifier ( identifier ) objc-protocol-refs[opt]
9465 objc-methodprotolist @end
9466 @interface identifier ( ) objc-protocol-refs[opt]
9467 objc-methodprotolist @end
9468 @implementation identifier ( identifier )
9470 objc-superclass:
9471 : identifier
9473 "@interface identifier (" must start "@interface identifier (
9474 identifier ) ...": objc-methodprotolist in the first production may
9475 not start with a parenthesized identifier as a declarator of a data
9476 definition with no declaration specifiers if the objc-superclass,
9477 objc-protocol-refs and objc-class-instance-variables are omitted. */
9479 static void
9480 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9482 bool iface_p;
9483 tree id1;
9484 tree superclass;
9485 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9486 iface_p = true;
9487 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9488 iface_p = false;
9489 else
9490 gcc_unreachable ();
9492 c_parser_consume_token (parser);
9493 if (c_parser_next_token_is_not (parser, CPP_NAME))
9495 c_parser_error (parser, "expected identifier");
9496 return;
9498 id1 = c_parser_peek_token (parser)->value;
9499 c_parser_consume_token (parser);
9500 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9502 /* We have a category or class extension. */
9503 tree id2;
9504 tree proto = NULL_TREE;
9505 matching_parens parens;
9506 parens.consume_open (parser);
9507 if (c_parser_next_token_is_not (parser, CPP_NAME))
9509 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9511 /* We have a class extension. */
9512 id2 = NULL_TREE;
9514 else
9516 c_parser_error (parser, "expected identifier or %<)%>");
9517 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9518 return;
9521 else
9523 id2 = c_parser_peek_token (parser)->value;
9524 c_parser_consume_token (parser);
9526 parens.skip_until_found_close (parser);
9527 if (!iface_p)
9529 objc_start_category_implementation (id1, id2);
9530 return;
9532 if (c_parser_next_token_is (parser, CPP_LESS))
9533 proto = c_parser_objc_protocol_refs (parser);
9534 objc_start_category_interface (id1, id2, proto, attributes);
9535 c_parser_objc_methodprotolist (parser);
9536 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9537 objc_finish_interface ();
9538 return;
9540 if (c_parser_next_token_is (parser, CPP_COLON))
9542 c_parser_consume_token (parser);
9543 if (c_parser_next_token_is_not (parser, CPP_NAME))
9545 c_parser_error (parser, "expected identifier");
9546 return;
9548 superclass = c_parser_peek_token (parser)->value;
9549 c_parser_consume_token (parser);
9551 else
9552 superclass = NULL_TREE;
9553 if (iface_p)
9555 tree proto = NULL_TREE;
9556 if (c_parser_next_token_is (parser, CPP_LESS))
9557 proto = c_parser_objc_protocol_refs (parser);
9558 objc_start_class_interface (id1, superclass, proto, attributes);
9560 else
9561 objc_start_class_implementation (id1, superclass);
9562 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9563 c_parser_objc_class_instance_variables (parser);
9564 if (iface_p)
9566 objc_continue_interface ();
9567 c_parser_objc_methodprotolist (parser);
9568 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9569 objc_finish_interface ();
9571 else
9573 objc_continue_implementation ();
9574 return;
9578 /* Parse objc-class-instance-variables.
9580 objc-class-instance-variables:
9581 { objc-instance-variable-decl-list[opt] }
9583 objc-instance-variable-decl-list:
9584 objc-visibility-spec
9585 objc-instance-variable-decl ;
9587 objc-instance-variable-decl-list objc-visibility-spec
9588 objc-instance-variable-decl-list objc-instance-variable-decl ;
9589 objc-instance-variable-decl-list ;
9591 objc-visibility-spec:
9592 @private
9593 @protected
9594 @public
9596 objc-instance-variable-decl:
9597 struct-declaration
9600 static void
9601 c_parser_objc_class_instance_variables (c_parser *parser)
9603 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9604 c_parser_consume_token (parser);
9605 while (c_parser_next_token_is_not (parser, CPP_EOF))
9607 tree decls;
9608 /* Parse any stray semicolon. */
9609 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9611 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9612 "extra semicolon");
9613 c_parser_consume_token (parser);
9614 continue;
9616 /* Stop if at the end of the instance variables. */
9617 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9619 c_parser_consume_token (parser);
9620 break;
9622 /* Parse any objc-visibility-spec. */
9623 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9625 c_parser_consume_token (parser);
9626 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9627 continue;
9629 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9631 c_parser_consume_token (parser);
9632 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9633 continue;
9635 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9637 c_parser_consume_token (parser);
9638 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9639 continue;
9641 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9643 c_parser_consume_token (parser);
9644 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9645 continue;
9647 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9649 c_parser_pragma (parser, pragma_external, NULL);
9650 continue;
9653 /* Parse some comma-separated declarations. */
9654 decls = c_parser_struct_declaration (parser);
9655 if (decls == NULL)
9657 /* There is a syntax error. We want to skip the offending
9658 tokens up to the next ';' (included) or '}'
9659 (excluded). */
9661 /* First, skip manually a ')' or ']'. This is because they
9662 reduce the nesting level, so c_parser_skip_until_found()
9663 wouldn't be able to skip past them. */
9664 c_token *token = c_parser_peek_token (parser);
9665 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9666 c_parser_consume_token (parser);
9668 /* Then, do the standard skipping. */
9669 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9671 /* We hopefully recovered. Start normal parsing again. */
9672 parser->error = false;
9673 continue;
9675 else
9677 /* Comma-separated instance variables are chained together
9678 in reverse order; add them one by one. */
9679 tree ivar = nreverse (decls);
9680 for (; ivar; ivar = DECL_CHAIN (ivar))
9681 objc_add_instance_variable (copy_node (ivar));
9683 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9687 /* Parse an objc-class-declaration.
9689 objc-class-declaration:
9690 @class identifier-list ;
9693 static void
9694 c_parser_objc_class_declaration (c_parser *parser)
9696 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9697 c_parser_consume_token (parser);
9698 /* Any identifiers, including those declared as type names, are OK
9699 here. */
9700 while (true)
9702 tree id;
9703 if (c_parser_next_token_is_not (parser, CPP_NAME))
9705 c_parser_error (parser, "expected identifier");
9706 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9707 parser->error = false;
9708 return;
9710 id = c_parser_peek_token (parser)->value;
9711 objc_declare_class (id);
9712 c_parser_consume_token (parser);
9713 if (c_parser_next_token_is (parser, CPP_COMMA))
9714 c_parser_consume_token (parser);
9715 else
9716 break;
9718 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9721 /* Parse an objc-alias-declaration.
9723 objc-alias-declaration:
9724 @compatibility_alias identifier identifier ;
9727 static void
9728 c_parser_objc_alias_declaration (c_parser *parser)
9730 tree id1, id2;
9731 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9732 c_parser_consume_token (parser);
9733 if (c_parser_next_token_is_not (parser, CPP_NAME))
9735 c_parser_error (parser, "expected identifier");
9736 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9737 return;
9739 id1 = c_parser_peek_token (parser)->value;
9740 c_parser_consume_token (parser);
9741 if (c_parser_next_token_is_not (parser, CPP_NAME))
9743 c_parser_error (parser, "expected identifier");
9744 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9745 return;
9747 id2 = c_parser_peek_token (parser)->value;
9748 c_parser_consume_token (parser);
9749 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9750 objc_declare_alias (id1, id2);
9753 /* Parse an objc-protocol-definition.
9755 objc-protocol-definition:
9756 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9757 @protocol identifier-list ;
9759 "@protocol identifier ;" should be resolved as "@protocol
9760 identifier-list ;": objc-methodprotolist may not start with a
9761 semicolon in the first alternative if objc-protocol-refs are
9762 omitted. */
9764 static void
9765 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9767 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9769 c_parser_consume_token (parser);
9770 if (c_parser_next_token_is_not (parser, CPP_NAME))
9772 c_parser_error (parser, "expected identifier");
9773 return;
9775 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9776 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9778 /* Any identifiers, including those declared as type names, are
9779 OK here. */
9780 while (true)
9782 tree id;
9783 if (c_parser_next_token_is_not (parser, CPP_NAME))
9785 c_parser_error (parser, "expected identifier");
9786 break;
9788 id = c_parser_peek_token (parser)->value;
9789 objc_declare_protocol (id, attributes);
9790 c_parser_consume_token (parser);
9791 if (c_parser_next_token_is (parser, CPP_COMMA))
9792 c_parser_consume_token (parser);
9793 else
9794 break;
9796 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9798 else
9800 tree id = c_parser_peek_token (parser)->value;
9801 tree proto = NULL_TREE;
9802 c_parser_consume_token (parser);
9803 if (c_parser_next_token_is (parser, CPP_LESS))
9804 proto = c_parser_objc_protocol_refs (parser);
9805 parser->objc_pq_context = true;
9806 objc_start_protocol (id, proto, attributes);
9807 c_parser_objc_methodprotolist (parser);
9808 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9809 parser->objc_pq_context = false;
9810 objc_finish_interface ();
9814 /* Parse an objc-method-type.
9816 objc-method-type:
9820 Return true if it is a class method (+) and false if it is
9821 an instance method (-).
9823 static inline bool
9824 c_parser_objc_method_type (c_parser *parser)
9826 switch (c_parser_peek_token (parser)->type)
9828 case CPP_PLUS:
9829 c_parser_consume_token (parser);
9830 return true;
9831 case CPP_MINUS:
9832 c_parser_consume_token (parser);
9833 return false;
9834 default:
9835 gcc_unreachable ();
9839 /* Parse an objc-method-definition.
9841 objc-method-definition:
9842 objc-method-type objc-method-decl ;[opt] compound-statement
9845 static void
9846 c_parser_objc_method_definition (c_parser *parser)
9848 bool is_class_method = c_parser_objc_method_type (parser);
9849 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9850 parser->objc_pq_context = true;
9851 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9852 &expr);
9853 if (decl == error_mark_node)
9854 return; /* Bail here. */
9856 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9858 c_parser_consume_token (parser);
9859 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9860 "extra semicolon in method definition specified");
9863 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9865 c_parser_error (parser, "expected %<{%>");
9866 return;
9869 parser->objc_pq_context = false;
9870 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9872 add_stmt (c_parser_compound_statement (parser));
9873 objc_finish_method_definition (current_function_decl);
9875 else
9877 /* This code is executed when we find a method definition
9878 outside of an @implementation context (or invalid for other
9879 reasons). Parse the method (to keep going) but do not emit
9880 any code.
9882 c_parser_compound_statement (parser);
9886 /* Parse an objc-methodprotolist.
9888 objc-methodprotolist:
9889 empty
9890 objc-methodprotolist objc-methodproto
9891 objc-methodprotolist declaration
9892 objc-methodprotolist ;
9893 @optional
9894 @required
9896 The declaration is a data definition, which may be missing
9897 declaration specifiers under the same rules and diagnostics as
9898 other data definitions outside functions, and the stray semicolon
9899 is diagnosed the same way as a stray semicolon outside a
9900 function. */
9902 static void
9903 c_parser_objc_methodprotolist (c_parser *parser)
9905 while (true)
9907 /* The list is terminated by @end. */
9908 switch (c_parser_peek_token (parser)->type)
9910 case CPP_SEMICOLON:
9911 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9912 "ISO C does not allow extra %<;%> outside of a function");
9913 c_parser_consume_token (parser);
9914 break;
9915 case CPP_PLUS:
9916 case CPP_MINUS:
9917 c_parser_objc_methodproto (parser);
9918 break;
9919 case CPP_PRAGMA:
9920 c_parser_pragma (parser, pragma_external, NULL);
9921 break;
9922 case CPP_EOF:
9923 return;
9924 default:
9925 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9926 return;
9927 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9928 c_parser_objc_at_property_declaration (parser);
9929 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9931 objc_set_method_opt (true);
9932 c_parser_consume_token (parser);
9934 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9936 objc_set_method_opt (false);
9937 c_parser_consume_token (parser);
9939 else
9940 c_parser_declaration_or_fndef (parser, false, false, true,
9941 false, true, NULL, vNULL);
9942 break;
9947 /* Parse an objc-methodproto.
9949 objc-methodproto:
9950 objc-method-type objc-method-decl ;
9953 static void
9954 c_parser_objc_methodproto (c_parser *parser)
9956 bool is_class_method = c_parser_objc_method_type (parser);
9957 tree decl, attributes = NULL_TREE;
9959 /* Remember protocol qualifiers in prototypes. */
9960 parser->objc_pq_context = true;
9961 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9962 NULL);
9963 /* Forget protocol qualifiers now. */
9964 parser->objc_pq_context = false;
9966 /* Do not allow the presence of attributes to hide an erroneous
9967 method implementation in the interface section. */
9968 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9970 c_parser_error (parser, "expected %<;%>");
9971 return;
9974 if (decl != error_mark_node)
9975 objc_add_method_declaration (is_class_method, decl, attributes);
9977 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9980 /* If we are at a position that method attributes may be present, check that
9981 there are not any parsed already (a syntax error) and then collect any
9982 specified at the current location. Finally, if new attributes were present,
9983 check that the next token is legal ( ';' for decls and '{' for defs). */
9985 static bool
9986 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9988 bool bad = false;
9989 if (*attributes)
9991 c_parser_error (parser,
9992 "method attributes must be specified at the end only");
9993 *attributes = NULL_TREE;
9994 bad = true;
9997 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9998 *attributes = c_parser_attributes (parser);
10000 /* If there were no attributes here, just report any earlier error. */
10001 if (*attributes == NULL_TREE || bad)
10002 return bad;
10004 /* If the attributes are followed by a ; or {, then just report any earlier
10005 error. */
10006 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10007 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10008 return bad;
10010 /* We've got attributes, but not at the end. */
10011 c_parser_error (parser,
10012 "expected %<;%> or %<{%> after method attribute definition");
10013 return true;
10016 /* Parse an objc-method-decl.
10018 objc-method-decl:
10019 ( objc-type-name ) objc-selector
10020 objc-selector
10021 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10022 objc-keyword-selector objc-optparmlist
10023 attributes
10025 objc-keyword-selector:
10026 objc-keyword-decl
10027 objc-keyword-selector objc-keyword-decl
10029 objc-keyword-decl:
10030 objc-selector : ( objc-type-name ) identifier
10031 objc-selector : identifier
10032 : ( objc-type-name ) identifier
10033 : identifier
10035 objc-optparmlist:
10036 objc-optparms objc-optellipsis
10038 objc-optparms:
10039 empty
10040 objc-opt-parms , parameter-declaration
10042 objc-optellipsis:
10043 empty
10044 , ...
10047 static tree
10048 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10049 tree *attributes, tree *expr)
10051 tree type = NULL_TREE;
10052 tree sel;
10053 tree parms = NULL_TREE;
10054 bool ellipsis = false;
10055 bool attr_err = false;
10057 *attributes = NULL_TREE;
10058 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10060 matching_parens parens;
10061 parens.consume_open (parser);
10062 type = c_parser_objc_type_name (parser);
10063 parens.skip_until_found_close (parser);
10065 sel = c_parser_objc_selector (parser);
10066 /* If there is no selector, or a colon follows, we have an
10067 objc-keyword-selector. If there is a selector, and a colon does
10068 not follow, that selector ends the objc-method-decl. */
10069 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10071 tree tsel = sel;
10072 tree list = NULL_TREE;
10073 while (true)
10075 tree atype = NULL_TREE, id, keyworddecl;
10076 tree param_attr = NULL_TREE;
10077 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10078 break;
10079 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10081 c_parser_consume_token (parser);
10082 atype = c_parser_objc_type_name (parser);
10083 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10084 "expected %<)%>");
10086 /* New ObjC allows attributes on method parameters. */
10087 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10088 param_attr = c_parser_attributes (parser);
10089 if (c_parser_next_token_is_not (parser, CPP_NAME))
10091 c_parser_error (parser, "expected identifier");
10092 return error_mark_node;
10094 id = c_parser_peek_token (parser)->value;
10095 c_parser_consume_token (parser);
10096 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10097 list = chainon (list, keyworddecl);
10098 tsel = c_parser_objc_selector (parser);
10099 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10100 break;
10103 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10105 /* Parse the optional parameter list. Optional Objective-C
10106 method parameters follow the C syntax, and may include '...'
10107 to denote a variable number of arguments. */
10108 parms = make_node (TREE_LIST);
10109 while (c_parser_next_token_is (parser, CPP_COMMA))
10111 struct c_parm *parm;
10112 c_parser_consume_token (parser);
10113 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10115 ellipsis = true;
10116 c_parser_consume_token (parser);
10117 attr_err |= c_parser_objc_maybe_method_attributes
10118 (parser, attributes) ;
10119 break;
10121 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10122 if (parm == NULL)
10123 break;
10124 parms = chainon (parms,
10125 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10127 sel = list;
10129 else
10130 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10132 if (sel == NULL)
10134 c_parser_error (parser, "objective-c method declaration is expected");
10135 return error_mark_node;
10138 if (attr_err)
10139 return error_mark_node;
10141 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10144 /* Parse an objc-type-name.
10146 objc-type-name:
10147 objc-type-qualifiers[opt] type-name
10148 objc-type-qualifiers[opt]
10150 objc-type-qualifiers:
10151 objc-type-qualifier
10152 objc-type-qualifiers objc-type-qualifier
10154 objc-type-qualifier: one of
10155 in out inout bycopy byref oneway
10158 static tree
10159 c_parser_objc_type_name (c_parser *parser)
10161 tree quals = NULL_TREE;
10162 struct c_type_name *type_name = NULL;
10163 tree type = NULL_TREE;
10164 while (true)
10166 c_token *token = c_parser_peek_token (parser);
10167 if (token->type == CPP_KEYWORD
10168 && (token->keyword == RID_IN
10169 || token->keyword == RID_OUT
10170 || token->keyword == RID_INOUT
10171 || token->keyword == RID_BYCOPY
10172 || token->keyword == RID_BYREF
10173 || token->keyword == RID_ONEWAY))
10175 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10176 c_parser_consume_token (parser);
10178 else
10179 break;
10181 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10182 type_name = c_parser_type_name (parser);
10183 if (type_name)
10184 type = groktypename (type_name, NULL, NULL);
10186 /* If the type is unknown, and error has already been produced and
10187 we need to recover from the error. In that case, use NULL_TREE
10188 for the type, as if no type had been specified; this will use the
10189 default type ('id') which is good for error recovery. */
10190 if (type == error_mark_node)
10191 type = NULL_TREE;
10193 return build_tree_list (quals, type);
10196 /* Parse objc-protocol-refs.
10198 objc-protocol-refs:
10199 < identifier-list >
10202 static tree
10203 c_parser_objc_protocol_refs (c_parser *parser)
10205 tree list = NULL_TREE;
10206 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10207 c_parser_consume_token (parser);
10208 /* Any identifiers, including those declared as type names, are OK
10209 here. */
10210 while (true)
10212 tree id;
10213 if (c_parser_next_token_is_not (parser, CPP_NAME))
10215 c_parser_error (parser, "expected identifier");
10216 break;
10218 id = c_parser_peek_token (parser)->value;
10219 list = chainon (list, build_tree_list (NULL_TREE, id));
10220 c_parser_consume_token (parser);
10221 if (c_parser_next_token_is (parser, CPP_COMMA))
10222 c_parser_consume_token (parser);
10223 else
10224 break;
10226 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10227 return list;
10230 /* Parse an objc-try-catch-finally-statement.
10232 objc-try-catch-finally-statement:
10233 @try compound-statement objc-catch-list[opt]
10234 @try compound-statement objc-catch-list[opt] @finally compound-statement
10236 objc-catch-list:
10237 @catch ( objc-catch-parameter-declaration ) compound-statement
10238 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10240 objc-catch-parameter-declaration:
10241 parameter-declaration
10242 '...'
10244 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10246 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10247 for C++. Keep them in sync. */
10249 static void
10250 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10252 location_t location;
10253 tree stmt;
10255 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10256 c_parser_consume_token (parser);
10257 location = c_parser_peek_token (parser)->location;
10258 objc_maybe_warn_exceptions (location);
10259 stmt = c_parser_compound_statement (parser);
10260 objc_begin_try_stmt (location, stmt);
10262 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10264 struct c_parm *parm;
10265 tree parameter_declaration = error_mark_node;
10266 bool seen_open_paren = false;
10268 c_parser_consume_token (parser);
10269 matching_parens parens;
10270 if (!parens.require_open (parser))
10271 seen_open_paren = true;
10272 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10274 /* We have "@catch (...)" (where the '...' are literally
10275 what is in the code). Skip the '...'.
10276 parameter_declaration is set to NULL_TREE, and
10277 objc_being_catch_clauses() knows that that means
10278 '...'. */
10279 c_parser_consume_token (parser);
10280 parameter_declaration = NULL_TREE;
10282 else
10284 /* We have "@catch (NSException *exception)" or something
10285 like that. Parse the parameter declaration. */
10286 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10287 if (parm == NULL)
10288 parameter_declaration = error_mark_node;
10289 else
10290 parameter_declaration = grokparm (parm, NULL);
10292 if (seen_open_paren)
10293 parens.require_close (parser);
10294 else
10296 /* If there was no open parenthesis, we are recovering from
10297 an error, and we are trying to figure out what mistake
10298 the user has made. */
10300 /* If there is an immediate closing parenthesis, the user
10301 probably forgot the opening one (ie, they typed "@catch
10302 NSException *e)". Parse the closing parenthesis and keep
10303 going. */
10304 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10305 c_parser_consume_token (parser);
10307 /* If these is no immediate closing parenthesis, the user
10308 probably doesn't know that parenthesis are required at
10309 all (ie, they typed "@catch NSException *e"). So, just
10310 forget about the closing parenthesis and keep going. */
10312 objc_begin_catch_clause (parameter_declaration);
10313 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10314 c_parser_compound_statement_nostart (parser);
10315 objc_finish_catch_clause ();
10317 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10319 c_parser_consume_token (parser);
10320 location = c_parser_peek_token (parser)->location;
10321 stmt = c_parser_compound_statement (parser);
10322 objc_build_finally_clause (location, stmt);
10324 objc_finish_try_stmt ();
10327 /* Parse an objc-synchronized-statement.
10329 objc-synchronized-statement:
10330 @synchronized ( expression ) compound-statement
10333 static void
10334 c_parser_objc_synchronized_statement (c_parser *parser)
10336 location_t loc;
10337 tree expr, stmt;
10338 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10339 c_parser_consume_token (parser);
10340 loc = c_parser_peek_token (parser)->location;
10341 objc_maybe_warn_exceptions (loc);
10342 matching_parens parens;
10343 if (parens.require_open (parser))
10345 struct c_expr ce = c_parser_expression (parser);
10346 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10347 expr = ce.value;
10348 expr = c_fully_fold (expr, false, NULL);
10349 parens.skip_until_found_close (parser);
10351 else
10352 expr = error_mark_node;
10353 stmt = c_parser_compound_statement (parser);
10354 objc_build_synchronized (loc, expr, stmt);
10357 /* Parse an objc-selector; return NULL_TREE without an error if the
10358 next token is not an objc-selector.
10360 objc-selector:
10361 identifier
10362 one of
10363 enum struct union if else while do for switch case default
10364 break continue return goto asm sizeof typeof __alignof
10365 unsigned long const short volatile signed restrict _Complex
10366 in out inout bycopy byref oneway int char float double void _Bool
10367 _Atomic
10369 ??? Why this selection of keywords but not, for example, storage
10370 class specifiers? */
10372 static tree
10373 c_parser_objc_selector (c_parser *parser)
10375 c_token *token = c_parser_peek_token (parser);
10376 tree value = token->value;
10377 if (token->type == CPP_NAME)
10379 c_parser_consume_token (parser);
10380 return value;
10382 if (token->type != CPP_KEYWORD)
10383 return NULL_TREE;
10384 switch (token->keyword)
10386 case RID_ENUM:
10387 case RID_STRUCT:
10388 case RID_UNION:
10389 case RID_IF:
10390 case RID_ELSE:
10391 case RID_WHILE:
10392 case RID_DO:
10393 case RID_FOR:
10394 case RID_SWITCH:
10395 case RID_CASE:
10396 case RID_DEFAULT:
10397 case RID_BREAK:
10398 case RID_CONTINUE:
10399 case RID_RETURN:
10400 case RID_GOTO:
10401 case RID_ASM:
10402 case RID_SIZEOF:
10403 case RID_TYPEOF:
10404 case RID_ALIGNOF:
10405 case RID_UNSIGNED:
10406 case RID_LONG:
10407 case RID_CONST:
10408 case RID_SHORT:
10409 case RID_VOLATILE:
10410 case RID_SIGNED:
10411 case RID_RESTRICT:
10412 case RID_COMPLEX:
10413 case RID_IN:
10414 case RID_OUT:
10415 case RID_INOUT:
10416 case RID_BYCOPY:
10417 case RID_BYREF:
10418 case RID_ONEWAY:
10419 case RID_INT:
10420 case RID_CHAR:
10421 case RID_FLOAT:
10422 case RID_DOUBLE:
10423 CASE_RID_FLOATN_NX:
10424 case RID_VOID:
10425 case RID_BOOL:
10426 case RID_ATOMIC:
10427 case RID_AUTO_TYPE:
10428 case RID_INT_N_0:
10429 case RID_INT_N_1:
10430 case RID_INT_N_2:
10431 case RID_INT_N_3:
10432 c_parser_consume_token (parser);
10433 return value;
10434 default:
10435 return NULL_TREE;
10439 /* Parse an objc-selector-arg.
10441 objc-selector-arg:
10442 objc-selector
10443 objc-keywordname-list
10445 objc-keywordname-list:
10446 objc-keywordname
10447 objc-keywordname-list objc-keywordname
10449 objc-keywordname:
10450 objc-selector :
10454 static tree
10455 c_parser_objc_selector_arg (c_parser *parser)
10457 tree sel = c_parser_objc_selector (parser);
10458 tree list = NULL_TREE;
10459 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10460 return sel;
10461 while (true)
10463 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10464 return list;
10465 list = chainon (list, build_tree_list (sel, NULL_TREE));
10466 sel = c_parser_objc_selector (parser);
10467 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10468 break;
10470 return list;
10473 /* Parse an objc-receiver.
10475 objc-receiver:
10476 expression
10477 class-name
10478 type-name
10481 static tree
10482 c_parser_objc_receiver (c_parser *parser)
10484 location_t loc = c_parser_peek_token (parser)->location;
10486 if (c_parser_peek_token (parser)->type == CPP_NAME
10487 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10488 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10490 tree id = c_parser_peek_token (parser)->value;
10491 c_parser_consume_token (parser);
10492 return objc_get_class_reference (id);
10494 struct c_expr ce = c_parser_expression (parser);
10495 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10496 return c_fully_fold (ce.value, false, NULL);
10499 /* Parse objc-message-args.
10501 objc-message-args:
10502 objc-selector
10503 objc-keywordarg-list
10505 objc-keywordarg-list:
10506 objc-keywordarg
10507 objc-keywordarg-list objc-keywordarg
10509 objc-keywordarg:
10510 objc-selector : objc-keywordexpr
10511 : objc-keywordexpr
10514 static tree
10515 c_parser_objc_message_args (c_parser *parser)
10517 tree sel = c_parser_objc_selector (parser);
10518 tree list = NULL_TREE;
10519 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10520 return sel;
10521 while (true)
10523 tree keywordexpr;
10524 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10525 return error_mark_node;
10526 keywordexpr = c_parser_objc_keywordexpr (parser);
10527 list = chainon (list, build_tree_list (sel, keywordexpr));
10528 sel = c_parser_objc_selector (parser);
10529 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10530 break;
10532 return list;
10535 /* Parse an objc-keywordexpr.
10537 objc-keywordexpr:
10538 nonempty-expr-list
10541 static tree
10542 c_parser_objc_keywordexpr (c_parser *parser)
10544 tree ret;
10545 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10546 NULL, NULL, NULL, NULL);
10547 if (vec_safe_length (expr_list) == 1)
10549 /* Just return the expression, remove a level of
10550 indirection. */
10551 ret = (*expr_list)[0];
10553 else
10555 /* We have a comma expression, we will collapse later. */
10556 ret = build_tree_list_vec (expr_list);
10558 release_tree_vector (expr_list);
10559 return ret;
10562 /* A check, needed in several places, that ObjC interface, implementation or
10563 method definitions are not prefixed by incorrect items. */
10564 static bool
10565 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10566 struct c_declspecs *specs)
10568 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10569 || specs->typespec_kind != ctsk_none)
10571 c_parser_error (parser,
10572 "no type or storage class may be specified here,");
10573 c_parser_skip_to_end_of_block_or_statement (parser);
10574 return true;
10576 return false;
10579 /* Parse an Objective-C @property declaration. The syntax is:
10581 objc-property-declaration:
10582 '@property' objc-property-attributes[opt] struct-declaration ;
10584 objc-property-attributes:
10585 '(' objc-property-attribute-list ')'
10587 objc-property-attribute-list:
10588 objc-property-attribute
10589 objc-property-attribute-list, objc-property-attribute
10591 objc-property-attribute
10592 'getter' = identifier
10593 'setter' = identifier
10594 'readonly'
10595 'readwrite'
10596 'assign'
10597 'retain'
10598 'copy'
10599 'nonatomic'
10601 For example:
10602 @property NSString *name;
10603 @property (readonly) id object;
10604 @property (retain, nonatomic, getter=getTheName) id name;
10605 @property int a, b, c;
10607 PS: This function is identical to cp_parser_objc_at_propery_declaration
10608 for C++. Keep them in sync. */
10609 static void
10610 c_parser_objc_at_property_declaration (c_parser *parser)
10612 /* The following variables hold the attributes of the properties as
10613 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10614 seen. When we see an attribute, we set them to 'true' (if they
10615 are boolean properties) or to the identifier (if they have an
10616 argument, ie, for getter and setter). Note that here we only
10617 parse the list of attributes, check the syntax and accumulate the
10618 attributes that we find. objc_add_property_declaration() will
10619 then process the information. */
10620 bool property_assign = false;
10621 bool property_copy = false;
10622 tree property_getter_ident = NULL_TREE;
10623 bool property_nonatomic = false;
10624 bool property_readonly = false;
10625 bool property_readwrite = false;
10626 bool property_retain = false;
10627 tree property_setter_ident = NULL_TREE;
10629 /* 'properties' is the list of properties that we read. Usually a
10630 single one, but maybe more (eg, in "@property int a, b, c;" there
10631 are three). */
10632 tree properties;
10633 location_t loc;
10635 loc = c_parser_peek_token (parser)->location;
10636 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10638 c_parser_consume_token (parser); /* Eat '@property'. */
10640 /* Parse the optional attribute list... */
10641 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10643 matching_parens parens;
10645 /* Eat the '(' */
10646 parens.consume_open (parser);
10648 /* Property attribute keywords are valid now. */
10649 parser->objc_property_attr_context = true;
10651 while (true)
10653 bool syntax_error = false;
10654 c_token *token = c_parser_peek_token (parser);
10655 enum rid keyword;
10657 if (token->type != CPP_KEYWORD)
10659 if (token->type == CPP_CLOSE_PAREN)
10660 c_parser_error (parser, "expected identifier");
10661 else
10663 c_parser_consume_token (parser);
10664 c_parser_error (parser, "unknown property attribute");
10666 break;
10668 keyword = token->keyword;
10669 c_parser_consume_token (parser);
10670 switch (keyword)
10672 case RID_ASSIGN: property_assign = true; break;
10673 case RID_COPY: property_copy = true; break;
10674 case RID_NONATOMIC: property_nonatomic = true; break;
10675 case RID_READONLY: property_readonly = true; break;
10676 case RID_READWRITE: property_readwrite = true; break;
10677 case RID_RETAIN: property_retain = true; break;
10679 case RID_GETTER:
10680 case RID_SETTER:
10681 if (c_parser_next_token_is_not (parser, CPP_EQ))
10683 if (keyword == RID_GETTER)
10684 c_parser_error (parser,
10685 "missing %<=%> (after %<getter%> attribute)");
10686 else
10687 c_parser_error (parser,
10688 "missing %<=%> (after %<setter%> attribute)");
10689 syntax_error = true;
10690 break;
10692 c_parser_consume_token (parser); /* eat the = */
10693 if (c_parser_next_token_is_not (parser, CPP_NAME))
10695 c_parser_error (parser, "expected identifier");
10696 syntax_error = true;
10697 break;
10699 if (keyword == RID_SETTER)
10701 if (property_setter_ident != NULL_TREE)
10702 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10703 else
10704 property_setter_ident = c_parser_peek_token (parser)->value;
10705 c_parser_consume_token (parser);
10706 if (c_parser_next_token_is_not (parser, CPP_COLON))
10707 c_parser_error (parser, "setter name must terminate with %<:%>");
10708 else
10709 c_parser_consume_token (parser);
10711 else
10713 if (property_getter_ident != NULL_TREE)
10714 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10715 else
10716 property_getter_ident = c_parser_peek_token (parser)->value;
10717 c_parser_consume_token (parser);
10719 break;
10720 default:
10721 c_parser_error (parser, "unknown property attribute");
10722 syntax_error = true;
10723 break;
10726 if (syntax_error)
10727 break;
10729 if (c_parser_next_token_is (parser, CPP_COMMA))
10730 c_parser_consume_token (parser);
10731 else
10732 break;
10734 parser->objc_property_attr_context = false;
10735 parens.skip_until_found_close (parser);
10737 /* ... and the property declaration(s). */
10738 properties = c_parser_struct_declaration (parser);
10740 if (properties == error_mark_node)
10742 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10743 parser->error = false;
10744 return;
10747 if (properties == NULL_TREE)
10748 c_parser_error (parser, "expected identifier");
10749 else
10751 /* Comma-separated properties are chained together in
10752 reverse order; add them one by one. */
10753 properties = nreverse (properties);
10755 for (; properties; properties = TREE_CHAIN (properties))
10756 objc_add_property_declaration (loc, copy_node (properties),
10757 property_readonly, property_readwrite,
10758 property_assign, property_retain,
10759 property_copy, property_nonatomic,
10760 property_getter_ident, property_setter_ident);
10763 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10764 parser->error = false;
10767 /* Parse an Objective-C @synthesize declaration. The syntax is:
10769 objc-synthesize-declaration:
10770 @synthesize objc-synthesize-identifier-list ;
10772 objc-synthesize-identifier-list:
10773 objc-synthesize-identifier
10774 objc-synthesize-identifier-list, objc-synthesize-identifier
10776 objc-synthesize-identifier
10777 identifier
10778 identifier = identifier
10780 For example:
10781 @synthesize MyProperty;
10782 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10784 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10785 for C++. Keep them in sync.
10787 static void
10788 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10790 tree list = NULL_TREE;
10791 location_t loc;
10792 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10793 loc = c_parser_peek_token (parser)->location;
10795 c_parser_consume_token (parser);
10796 while (true)
10798 tree property, ivar;
10799 if (c_parser_next_token_is_not (parser, CPP_NAME))
10801 c_parser_error (parser, "expected identifier");
10802 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10803 /* Once we find the semicolon, we can resume normal parsing.
10804 We have to reset parser->error manually because
10805 c_parser_skip_until_found() won't reset it for us if the
10806 next token is precisely a semicolon. */
10807 parser->error = false;
10808 return;
10810 property = c_parser_peek_token (parser)->value;
10811 c_parser_consume_token (parser);
10812 if (c_parser_next_token_is (parser, CPP_EQ))
10814 c_parser_consume_token (parser);
10815 if (c_parser_next_token_is_not (parser, CPP_NAME))
10817 c_parser_error (parser, "expected identifier");
10818 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10819 parser->error = false;
10820 return;
10822 ivar = c_parser_peek_token (parser)->value;
10823 c_parser_consume_token (parser);
10825 else
10826 ivar = NULL_TREE;
10827 list = chainon (list, build_tree_list (ivar, property));
10828 if (c_parser_next_token_is (parser, CPP_COMMA))
10829 c_parser_consume_token (parser);
10830 else
10831 break;
10833 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10834 objc_add_synthesize_declaration (loc, list);
10837 /* Parse an Objective-C @dynamic declaration. The syntax is:
10839 objc-dynamic-declaration:
10840 @dynamic identifier-list ;
10842 For example:
10843 @dynamic MyProperty;
10844 @dynamic MyProperty, AnotherProperty;
10846 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10847 for C++. Keep them in sync.
10849 static void
10850 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10852 tree list = NULL_TREE;
10853 location_t loc;
10854 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10855 loc = c_parser_peek_token (parser)->location;
10857 c_parser_consume_token (parser);
10858 while (true)
10860 tree property;
10861 if (c_parser_next_token_is_not (parser, CPP_NAME))
10863 c_parser_error (parser, "expected identifier");
10864 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10865 parser->error = false;
10866 return;
10868 property = c_parser_peek_token (parser)->value;
10869 list = chainon (list, build_tree_list (NULL_TREE, property));
10870 c_parser_consume_token (parser);
10871 if (c_parser_next_token_is (parser, CPP_COMMA))
10872 c_parser_consume_token (parser);
10873 else
10874 break;
10876 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10877 objc_add_dynamic_declaration (loc, list);
10881 /* Parse a pragma GCC ivdep. */
10883 static bool
10884 c_parse_pragma_ivdep (c_parser *parser)
10886 c_parser_consume_pragma (parser);
10887 c_parser_skip_to_pragma_eol (parser);
10888 return true;
10891 /* Parse a pragma GCC unroll. */
10893 static unsigned short
10894 c_parser_pragma_unroll (c_parser *parser)
10896 unsigned short unroll;
10897 c_parser_consume_pragma (parser);
10898 location_t location = c_parser_peek_token (parser)->location;
10899 tree expr = c_parser_expr_no_commas (parser, NULL).value;
10900 mark_exp_read (expr);
10901 expr = c_fully_fold (expr, false, NULL);
10902 HOST_WIDE_INT lunroll = 0;
10903 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
10904 || TREE_CODE (expr) != INTEGER_CST
10905 || (lunroll = tree_to_shwi (expr)) < 0
10906 || lunroll >= USHRT_MAX)
10908 error_at (location, "%<#pragma GCC unroll%> requires an"
10909 " assignment-expression that evaluates to a non-negative"
10910 " integral constant less than %u", USHRT_MAX);
10911 unroll = 0;
10913 else
10915 unroll = (unsigned short)lunroll;
10916 if (unroll == 0)
10917 unroll = 1;
10920 c_parser_skip_to_pragma_eol (parser);
10921 return unroll;
10924 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10925 should be considered, statements. ALLOW_STMT is true if we're within
10926 the context of a function and such pragmas are to be allowed. Returns
10927 true if we actually parsed such a pragma. */
10929 static bool
10930 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10932 unsigned int id;
10933 const char *construct = NULL;
10935 id = c_parser_peek_token (parser)->pragma_kind;
10936 gcc_assert (id != PRAGMA_NONE);
10938 switch (id)
10940 case PRAGMA_OACC_DECLARE:
10941 c_parser_oacc_declare (parser);
10942 return false;
10944 case PRAGMA_OACC_ENTER_DATA:
10945 if (context != pragma_compound)
10947 construct = "acc enter data";
10948 in_compound:
10949 if (context == pragma_stmt)
10951 error_at (c_parser_peek_token (parser)->location,
10952 "%<#pragma %s%> may only be used in compound "
10953 "statements", construct);
10954 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10955 return false;
10957 goto bad_stmt;
10959 c_parser_oacc_enter_exit_data (parser, true);
10960 return false;
10962 case PRAGMA_OACC_EXIT_DATA:
10963 if (context != pragma_compound)
10965 construct = "acc exit data";
10966 goto in_compound;
10968 c_parser_oacc_enter_exit_data (parser, false);
10969 return false;
10971 case PRAGMA_OACC_ROUTINE:
10972 if (context != pragma_external)
10974 error_at (c_parser_peek_token (parser)->location,
10975 "%<#pragma acc routine%> must be at file scope");
10976 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10977 return false;
10979 c_parser_oacc_routine (parser, context);
10980 return false;
10982 case PRAGMA_OACC_UPDATE:
10983 if (context != pragma_compound)
10985 construct = "acc update";
10986 goto in_compound;
10988 c_parser_oacc_update (parser);
10989 return false;
10991 case PRAGMA_OMP_BARRIER:
10992 if (context != pragma_compound)
10994 construct = "omp barrier";
10995 goto in_compound;
10997 c_parser_omp_barrier (parser);
10998 return false;
11000 case PRAGMA_OMP_FLUSH:
11001 if (context != pragma_compound)
11003 construct = "omp flush";
11004 goto in_compound;
11006 c_parser_omp_flush (parser);
11007 return false;
11009 case PRAGMA_OMP_TASKWAIT:
11010 if (context != pragma_compound)
11012 construct = "omp taskwait";
11013 goto in_compound;
11015 c_parser_omp_taskwait (parser);
11016 return false;
11018 case PRAGMA_OMP_TASKYIELD:
11019 if (context != pragma_compound)
11021 construct = "omp taskyield";
11022 goto in_compound;
11024 c_parser_omp_taskyield (parser);
11025 return false;
11027 case PRAGMA_OMP_CANCEL:
11028 if (context != pragma_compound)
11030 construct = "omp cancel";
11031 goto in_compound;
11033 c_parser_omp_cancel (parser);
11034 return false;
11036 case PRAGMA_OMP_CANCELLATION_POINT:
11037 c_parser_omp_cancellation_point (parser, context);
11038 return false;
11040 case PRAGMA_OMP_THREADPRIVATE:
11041 c_parser_omp_threadprivate (parser);
11042 return false;
11044 case PRAGMA_OMP_TARGET:
11045 return c_parser_omp_target (parser, context, if_p);
11047 case PRAGMA_OMP_END_DECLARE_TARGET:
11048 c_parser_omp_end_declare_target (parser);
11049 return false;
11051 case PRAGMA_OMP_SECTION:
11052 error_at (c_parser_peek_token (parser)->location,
11053 "%<#pragma omp section%> may only be used in "
11054 "%<#pragma omp sections%> construct");
11055 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11056 return false;
11058 case PRAGMA_OMP_DECLARE:
11059 c_parser_omp_declare (parser, context);
11060 return false;
11062 case PRAGMA_OMP_ORDERED:
11063 return c_parser_omp_ordered (parser, context, if_p);
11065 case PRAGMA_IVDEP:
11067 const bool ivdep = c_parse_pragma_ivdep (parser);
11068 unsigned short unroll;
11069 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11070 unroll = c_parser_pragma_unroll (parser);
11071 else
11072 unroll = 0;
11073 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11074 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11075 && !c_parser_next_token_is_keyword (parser, RID_DO))
11077 c_parser_error (parser, "for, while or do statement expected");
11078 return false;
11080 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11081 c_parser_for_statement (parser, ivdep, unroll, if_p);
11082 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11083 c_parser_while_statement (parser, ivdep, unroll, if_p);
11084 else
11085 c_parser_do_statement (parser, ivdep, unroll);
11087 return false;
11089 case PRAGMA_UNROLL:
11091 unsigned short unroll = c_parser_pragma_unroll (parser);
11092 bool ivdep;
11093 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11094 ivdep = c_parse_pragma_ivdep (parser);
11095 else
11096 ivdep = false;
11097 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11098 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11099 && !c_parser_next_token_is_keyword (parser, RID_DO))
11101 c_parser_error (parser, "for, while or do statement expected");
11102 return false;
11104 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11105 c_parser_for_statement (parser, ivdep, unroll, if_p);
11106 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11107 c_parser_while_statement (parser, ivdep, unroll, if_p);
11108 else
11109 c_parser_do_statement (parser, ivdep, unroll);
11111 return false;
11113 case PRAGMA_GCC_PCH_PREPROCESS:
11114 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11115 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11116 return false;
11118 case PRAGMA_OACC_WAIT:
11119 if (context != pragma_compound)
11121 construct = "acc wait";
11122 goto in_compound;
11124 /* FALL THROUGH. */
11126 default:
11127 if (id < PRAGMA_FIRST_EXTERNAL)
11129 if (context != pragma_stmt && context != pragma_compound)
11131 bad_stmt:
11132 c_parser_error (parser, "expected declaration specifiers");
11133 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11134 return false;
11136 c_parser_omp_construct (parser, if_p);
11137 return true;
11139 break;
11142 c_parser_consume_pragma (parser);
11143 c_invoke_pragma_handler (id);
11145 /* Skip to EOL, but suppress any error message. Those will have been
11146 generated by the handler routine through calling error, as opposed
11147 to calling c_parser_error. */
11148 parser->error = true;
11149 c_parser_skip_to_pragma_eol (parser);
11151 return false;
11154 /* The interface the pragma parsers have to the lexer. */
11156 enum cpp_ttype
11157 pragma_lex (tree *value, location_t *loc)
11159 c_token *tok = c_parser_peek_token (the_parser);
11160 enum cpp_ttype ret = tok->type;
11162 *value = tok->value;
11163 if (loc)
11164 *loc = tok->location;
11166 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11167 ret = CPP_EOF;
11168 else
11170 if (ret == CPP_KEYWORD)
11171 ret = CPP_NAME;
11172 c_parser_consume_token (the_parser);
11175 return ret;
11178 static void
11179 c_parser_pragma_pch_preprocess (c_parser *parser)
11181 tree name = NULL;
11183 c_parser_consume_pragma (parser);
11184 if (c_parser_next_token_is (parser, CPP_STRING))
11186 name = c_parser_peek_token (parser)->value;
11187 c_parser_consume_token (parser);
11189 else
11190 c_parser_error (parser, "expected string literal");
11191 c_parser_skip_to_pragma_eol (parser);
11193 if (name)
11194 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11197 /* OpenACC and OpenMP parsing routines. */
11199 /* Returns name of the next clause.
11200 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11201 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11202 returned and the token is consumed. */
11204 static pragma_omp_clause
11205 c_parser_omp_clause_name (c_parser *parser)
11207 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11209 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11210 result = PRAGMA_OACC_CLAUSE_AUTO;
11211 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11212 result = PRAGMA_OMP_CLAUSE_IF;
11213 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11214 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11215 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11216 result = PRAGMA_OMP_CLAUSE_FOR;
11217 else if (c_parser_next_token_is (parser, CPP_NAME))
11219 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11221 switch (p[0])
11223 case 'a':
11224 if (!strcmp ("aligned", p))
11225 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11226 else if (!strcmp ("async", p))
11227 result = PRAGMA_OACC_CLAUSE_ASYNC;
11228 break;
11229 case 'c':
11230 if (!strcmp ("collapse", p))
11231 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11232 else if (!strcmp ("copy", p))
11233 result = PRAGMA_OACC_CLAUSE_COPY;
11234 else if (!strcmp ("copyin", p))
11235 result = PRAGMA_OMP_CLAUSE_COPYIN;
11236 else if (!strcmp ("copyout", p))
11237 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11238 else if (!strcmp ("copyprivate", p))
11239 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11240 else if (!strcmp ("create", p))
11241 result = PRAGMA_OACC_CLAUSE_CREATE;
11242 break;
11243 case 'd':
11244 if (!strcmp ("defaultmap", p))
11245 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11246 else if (!strcmp ("delete", p))
11247 result = PRAGMA_OACC_CLAUSE_DELETE;
11248 else if (!strcmp ("depend", p))
11249 result = PRAGMA_OMP_CLAUSE_DEPEND;
11250 else if (!strcmp ("device", p))
11251 result = PRAGMA_OMP_CLAUSE_DEVICE;
11252 else if (!strcmp ("deviceptr", p))
11253 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11254 else if (!strcmp ("device_resident", p))
11255 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11256 else if (!strcmp ("dist_schedule", p))
11257 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11258 break;
11259 case 'f':
11260 if (!strcmp ("final", p))
11261 result = PRAGMA_OMP_CLAUSE_FINAL;
11262 else if (!strcmp ("finalize", p))
11263 result = PRAGMA_OACC_CLAUSE_FINALIZE;
11264 else if (!strcmp ("firstprivate", p))
11265 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11266 else if (!strcmp ("from", p))
11267 result = PRAGMA_OMP_CLAUSE_FROM;
11268 break;
11269 case 'g':
11270 if (!strcmp ("gang", p))
11271 result = PRAGMA_OACC_CLAUSE_GANG;
11272 else if (!strcmp ("grainsize", p))
11273 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11274 break;
11275 case 'h':
11276 if (!strcmp ("hint", p))
11277 result = PRAGMA_OMP_CLAUSE_HINT;
11278 else if (!strcmp ("host", p))
11279 result = PRAGMA_OACC_CLAUSE_HOST;
11280 break;
11281 case 'i':
11282 if (!strcmp ("if_present", p))
11283 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
11284 else if (!strcmp ("inbranch", p))
11285 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11286 else if (!strcmp ("independent", p))
11287 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11288 else if (!strcmp ("is_device_ptr", p))
11289 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11290 break;
11291 case 'l':
11292 if (!strcmp ("lastprivate", p))
11293 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11294 else if (!strcmp ("linear", p))
11295 result = PRAGMA_OMP_CLAUSE_LINEAR;
11296 else if (!strcmp ("link", p))
11297 result = PRAGMA_OMP_CLAUSE_LINK;
11298 break;
11299 case 'm':
11300 if (!strcmp ("map", p))
11301 result = PRAGMA_OMP_CLAUSE_MAP;
11302 else if (!strcmp ("mergeable", p))
11303 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11304 break;
11305 case 'n':
11306 if (!strcmp ("nogroup", p))
11307 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11308 else if (!strcmp ("notinbranch", p))
11309 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11310 else if (!strcmp ("nowait", p))
11311 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11312 else if (!strcmp ("num_gangs", p))
11313 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11314 else if (!strcmp ("num_tasks", p))
11315 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11316 else if (!strcmp ("num_teams", p))
11317 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11318 else if (!strcmp ("num_threads", p))
11319 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11320 else if (!strcmp ("num_workers", p))
11321 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11322 break;
11323 case 'o':
11324 if (!strcmp ("ordered", p))
11325 result = PRAGMA_OMP_CLAUSE_ORDERED;
11326 break;
11327 case 'p':
11328 if (!strcmp ("parallel", p))
11329 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11330 else if (!strcmp ("present", p))
11331 result = PRAGMA_OACC_CLAUSE_PRESENT;
11332 /* As of OpenACC 2.5, these are now aliases of the non-present_or
11333 clauses. */
11334 else if (!strcmp ("present_or_copy", p)
11335 || !strcmp ("pcopy", p))
11336 result = PRAGMA_OACC_CLAUSE_COPY;
11337 else if (!strcmp ("present_or_copyin", p)
11338 || !strcmp ("pcopyin", p))
11339 result = PRAGMA_OACC_CLAUSE_COPYIN;
11340 else if (!strcmp ("present_or_copyout", p)
11341 || !strcmp ("pcopyout", p))
11342 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11343 else if (!strcmp ("present_or_create", p)
11344 || !strcmp ("pcreate", p))
11345 result = PRAGMA_OACC_CLAUSE_CREATE;
11346 else if (!strcmp ("priority", p))
11347 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11348 else if (!strcmp ("private", p))
11349 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11350 else if (!strcmp ("proc_bind", p))
11351 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11352 break;
11353 case 'r':
11354 if (!strcmp ("reduction", p))
11355 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11356 break;
11357 case 's':
11358 if (!strcmp ("safelen", p))
11359 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11360 else if (!strcmp ("schedule", p))
11361 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11362 else if (!strcmp ("sections", p))
11363 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11364 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
11365 result = PRAGMA_OACC_CLAUSE_HOST;
11366 else if (!strcmp ("seq", p))
11367 result = PRAGMA_OACC_CLAUSE_SEQ;
11368 else if (!strcmp ("shared", p))
11369 result = PRAGMA_OMP_CLAUSE_SHARED;
11370 else if (!strcmp ("simd", p))
11371 result = PRAGMA_OMP_CLAUSE_SIMD;
11372 else if (!strcmp ("simdlen", p))
11373 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11374 break;
11375 case 't':
11376 if (!strcmp ("taskgroup", p))
11377 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11378 else if (!strcmp ("thread_limit", p))
11379 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11380 else if (!strcmp ("threads", p))
11381 result = PRAGMA_OMP_CLAUSE_THREADS;
11382 else if (!strcmp ("tile", p))
11383 result = PRAGMA_OACC_CLAUSE_TILE;
11384 else if (!strcmp ("to", p))
11385 result = PRAGMA_OMP_CLAUSE_TO;
11386 break;
11387 case 'u':
11388 if (!strcmp ("uniform", p))
11389 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11390 else if (!strcmp ("untied", p))
11391 result = PRAGMA_OMP_CLAUSE_UNTIED;
11392 else if (!strcmp ("use_device", p))
11393 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11394 else if (!strcmp ("use_device_ptr", p))
11395 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11396 break;
11397 case 'v':
11398 if (!strcmp ("vector", p))
11399 result = PRAGMA_OACC_CLAUSE_VECTOR;
11400 else if (!strcmp ("vector_length", p))
11401 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11402 break;
11403 case 'w':
11404 if (!strcmp ("wait", p))
11405 result = PRAGMA_OACC_CLAUSE_WAIT;
11406 else if (!strcmp ("worker", p))
11407 result = PRAGMA_OACC_CLAUSE_WORKER;
11408 break;
11412 if (result != PRAGMA_OMP_CLAUSE_NONE)
11413 c_parser_consume_token (parser);
11415 return result;
11418 /* Validate that a clause of the given type does not already exist. */
11420 static void
11421 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11422 const char *name)
11424 tree c;
11426 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11427 if (OMP_CLAUSE_CODE (c) == code)
11429 location_t loc = OMP_CLAUSE_LOCATION (c);
11430 error_at (loc, "too many %qs clauses", name);
11431 break;
11435 /* OpenACC 2.0
11436 Parse wait clause or wait directive parameters. */
11438 static tree
11439 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11441 vec<tree, va_gc> *args;
11442 tree t, args_tree;
11444 matching_parens parens;
11445 if (!parens.require_open (parser))
11446 return list;
11448 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11450 if (args->length () == 0)
11452 c_parser_error (parser, "expected integer expression before ')'");
11453 release_tree_vector (args);
11454 return list;
11457 args_tree = build_tree_list_vec (args);
11459 for (t = args_tree; t; t = TREE_CHAIN (t))
11461 tree targ = TREE_VALUE (t);
11463 if (targ != error_mark_node)
11465 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11467 c_parser_error (parser, "expression must be integral");
11468 targ = error_mark_node;
11470 else
11472 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11474 OMP_CLAUSE_DECL (c) = targ;
11475 OMP_CLAUSE_CHAIN (c) = list;
11476 list = c;
11481 release_tree_vector (args);
11482 parens.require_close (parser);
11483 return list;
11486 /* OpenACC 2.0, OpenMP 2.5:
11487 variable-list:
11488 identifier
11489 variable-list , identifier
11491 If KIND is nonzero, create the appropriate node and install the
11492 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11493 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11495 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11496 return the list created. */
11498 static tree
11499 c_parser_omp_variable_list (c_parser *parser,
11500 location_t clause_loc,
11501 enum omp_clause_code kind, tree list)
11503 if (c_parser_next_token_is_not (parser, CPP_NAME)
11504 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11505 c_parser_error (parser, "expected identifier");
11507 while (c_parser_next_token_is (parser, CPP_NAME)
11508 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11510 tree t = lookup_name (c_parser_peek_token (parser)->value);
11512 if (t == NULL_TREE)
11514 undeclared_variable (c_parser_peek_token (parser)->location,
11515 c_parser_peek_token (parser)->value);
11516 t = error_mark_node;
11519 c_parser_consume_token (parser);
11521 if (t == error_mark_node)
11523 else if (kind != 0)
11525 switch (kind)
11527 case OMP_CLAUSE__CACHE_:
11528 /* The OpenACC cache directive explicitly only allows "array
11529 elements or subarrays". */
11530 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11532 c_parser_error (parser, "expected %<[%>");
11533 t = error_mark_node;
11534 break;
11536 /* FALLTHROUGH */
11537 case OMP_CLAUSE_MAP:
11538 case OMP_CLAUSE_FROM:
11539 case OMP_CLAUSE_TO:
11540 while (c_parser_next_token_is (parser, CPP_DOT))
11542 location_t op_loc = c_parser_peek_token (parser)->location;
11543 c_parser_consume_token (parser);
11544 if (!c_parser_next_token_is (parser, CPP_NAME))
11546 c_parser_error (parser, "expected identifier");
11547 t = error_mark_node;
11548 break;
11551 c_token *comp_tok = c_parser_peek_token (parser);
11552 tree ident = comp_tok->value;
11553 location_t comp_loc = comp_tok->location;
11554 c_parser_consume_token (parser);
11555 t = build_component_ref (op_loc, t, ident, comp_loc);
11557 /* FALLTHROUGH */
11558 case OMP_CLAUSE_DEPEND:
11559 case OMP_CLAUSE_REDUCTION:
11560 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11562 tree low_bound = NULL_TREE, length = NULL_TREE;
11564 c_parser_consume_token (parser);
11565 if (!c_parser_next_token_is (parser, CPP_COLON))
11567 location_t expr_loc
11568 = c_parser_peek_token (parser)->location;
11569 c_expr expr = c_parser_expression (parser);
11570 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11571 false, true);
11572 low_bound = expr.value;
11574 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11575 length = integer_one_node;
11576 else
11578 /* Look for `:'. */
11579 if (!c_parser_require (parser, CPP_COLON,
11580 "expected %<:%>"))
11582 t = error_mark_node;
11583 break;
11585 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11587 location_t expr_loc
11588 = c_parser_peek_token (parser)->location;
11589 c_expr expr = c_parser_expression (parser);
11590 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11591 false, true);
11592 length = expr.value;
11595 /* Look for the closing `]'. */
11596 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11597 "expected %<]%>"))
11599 t = error_mark_node;
11600 break;
11603 t = tree_cons (low_bound, length, t);
11605 break;
11606 default:
11607 break;
11610 if (t != error_mark_node)
11612 tree u = build_omp_clause (clause_loc, kind);
11613 OMP_CLAUSE_DECL (u) = t;
11614 OMP_CLAUSE_CHAIN (u) = list;
11615 list = u;
11618 else
11619 list = tree_cons (t, NULL_TREE, list);
11621 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11622 break;
11624 c_parser_consume_token (parser);
11627 return list;
11630 /* Similarly, but expect leading and trailing parenthesis. This is a very
11631 common case for OpenACC and OpenMP clauses. */
11633 static tree
11634 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11635 tree list)
11637 /* The clauses location. */
11638 location_t loc = c_parser_peek_token (parser)->location;
11640 matching_parens parens;
11641 if (parens.require_open (parser))
11643 list = c_parser_omp_variable_list (parser, loc, kind, list);
11644 parens.skip_until_found_close (parser);
11646 return list;
11649 /* OpenACC 2.0:
11650 copy ( variable-list )
11651 copyin ( variable-list )
11652 copyout ( variable-list )
11653 create ( variable-list )
11654 delete ( variable-list )
11655 present ( variable-list ) */
11657 static tree
11658 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11659 tree list)
11661 enum gomp_map_kind kind;
11662 switch (c_kind)
11664 case PRAGMA_OACC_CLAUSE_COPY:
11665 kind = GOMP_MAP_TOFROM;
11666 break;
11667 case PRAGMA_OACC_CLAUSE_COPYIN:
11668 kind = GOMP_MAP_TO;
11669 break;
11670 case PRAGMA_OACC_CLAUSE_COPYOUT:
11671 kind = GOMP_MAP_FROM;
11672 break;
11673 case PRAGMA_OACC_CLAUSE_CREATE:
11674 kind = GOMP_MAP_ALLOC;
11675 break;
11676 case PRAGMA_OACC_CLAUSE_DELETE:
11677 kind = GOMP_MAP_RELEASE;
11678 break;
11679 case PRAGMA_OACC_CLAUSE_DEVICE:
11680 kind = GOMP_MAP_FORCE_TO;
11681 break;
11682 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11683 kind = GOMP_MAP_DEVICE_RESIDENT;
11684 break;
11685 case PRAGMA_OACC_CLAUSE_HOST:
11686 kind = GOMP_MAP_FORCE_FROM;
11687 break;
11688 case PRAGMA_OACC_CLAUSE_LINK:
11689 kind = GOMP_MAP_LINK;
11690 break;
11691 case PRAGMA_OACC_CLAUSE_PRESENT:
11692 kind = GOMP_MAP_FORCE_PRESENT;
11693 break;
11694 default:
11695 gcc_unreachable ();
11697 tree nl, c;
11698 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11700 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11701 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11703 return nl;
11706 /* OpenACC 2.0:
11707 deviceptr ( variable-list ) */
11709 static tree
11710 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11712 location_t loc = c_parser_peek_token (parser)->location;
11713 tree vars, t;
11715 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11716 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11717 variable-list must only allow for pointer variables. */
11718 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11719 for (t = vars; t && t; t = TREE_CHAIN (t))
11721 tree v = TREE_PURPOSE (t);
11723 /* FIXME diagnostics: Ideally we should keep individual
11724 locations for all the variables in the var list to make the
11725 following errors more precise. Perhaps
11726 c_parser_omp_var_list_parens() should construct a list of
11727 locations to go along with the var list. */
11729 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11730 error_at (loc, "%qD is not a variable", v);
11731 else if (TREE_TYPE (v) == error_mark_node)
11733 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11734 error_at (loc, "%qD is not a pointer variable", v);
11736 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11737 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11738 OMP_CLAUSE_DECL (u) = v;
11739 OMP_CLAUSE_CHAIN (u) = list;
11740 list = u;
11743 return list;
11746 /* OpenACC 2.0, OpenMP 3.0:
11747 collapse ( constant-expression ) */
11749 static tree
11750 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11752 tree c, num = error_mark_node;
11753 HOST_WIDE_INT n;
11754 location_t loc;
11756 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11757 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11759 loc = c_parser_peek_token (parser)->location;
11760 matching_parens parens;
11761 if (parens.require_open (parser))
11763 num = c_parser_expr_no_commas (parser, NULL).value;
11764 parens.skip_until_found_close (parser);
11766 if (num == error_mark_node)
11767 return list;
11768 mark_exp_read (num);
11769 num = c_fully_fold (num, false, NULL);
11770 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11771 || !tree_fits_shwi_p (num)
11772 || (n = tree_to_shwi (num)) <= 0
11773 || (int) n != n)
11775 error_at (loc,
11776 "collapse argument needs positive constant integer expression");
11777 return list;
11779 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11780 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11781 OMP_CLAUSE_CHAIN (c) = list;
11782 return c;
11785 /* OpenMP 2.5:
11786 copyin ( variable-list ) */
11788 static tree
11789 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11791 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11794 /* OpenMP 2.5:
11795 copyprivate ( variable-list ) */
11797 static tree
11798 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11800 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11803 /* OpenMP 2.5:
11804 default ( none | shared )
11806 OpenACC:
11807 default ( none | present ) */
11809 static tree
11810 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11812 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11813 location_t loc = c_parser_peek_token (parser)->location;
11814 tree c;
11816 matching_parens parens;
11817 if (!parens.require_open (parser))
11818 return list;
11819 if (c_parser_next_token_is (parser, CPP_NAME))
11821 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11823 switch (p[0])
11825 case 'n':
11826 if (strcmp ("none", p) != 0)
11827 goto invalid_kind;
11828 kind = OMP_CLAUSE_DEFAULT_NONE;
11829 break;
11831 case 'p':
11832 if (strcmp ("present", p) != 0 || !is_oacc)
11833 goto invalid_kind;
11834 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11835 break;
11837 case 's':
11838 if (strcmp ("shared", p) != 0 || is_oacc)
11839 goto invalid_kind;
11840 kind = OMP_CLAUSE_DEFAULT_SHARED;
11841 break;
11843 default:
11844 goto invalid_kind;
11847 c_parser_consume_token (parser);
11849 else
11851 invalid_kind:
11852 if (is_oacc)
11853 c_parser_error (parser, "expected %<none%> or %<present%>");
11854 else
11855 c_parser_error (parser, "expected %<none%> or %<shared%>");
11857 parens.skip_until_found_close (parser);
11859 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11860 return list;
11862 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11863 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11864 OMP_CLAUSE_CHAIN (c) = list;
11865 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11867 return c;
11870 /* OpenMP 2.5:
11871 firstprivate ( variable-list ) */
11873 static tree
11874 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11876 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11879 /* OpenMP 3.1:
11880 final ( expression ) */
11882 static tree
11883 c_parser_omp_clause_final (c_parser *parser, tree list)
11885 location_t loc = c_parser_peek_token (parser)->location;
11886 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11888 tree t = c_parser_paren_condition (parser);
11889 tree c;
11891 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11893 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11894 OMP_CLAUSE_FINAL_EXPR (c) = t;
11895 OMP_CLAUSE_CHAIN (c) = list;
11896 list = c;
11898 else
11899 c_parser_error (parser, "expected %<(%>");
11901 return list;
11904 /* OpenACC, OpenMP 2.5:
11905 if ( expression )
11907 OpenMP 4.5:
11908 if ( directive-name-modifier : expression )
11910 directive-name-modifier:
11911 parallel | task | taskloop | target data | target | target update
11912 | target enter data | target exit data */
11914 static tree
11915 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11917 location_t location = c_parser_peek_token (parser)->location;
11918 enum tree_code if_modifier = ERROR_MARK;
11920 matching_parens parens;
11921 if (!parens.require_open (parser))
11922 return list;
11924 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11926 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11927 int n = 2;
11928 if (strcmp (p, "parallel") == 0)
11929 if_modifier = OMP_PARALLEL;
11930 else if (strcmp (p, "task") == 0)
11931 if_modifier = OMP_TASK;
11932 else if (strcmp (p, "taskloop") == 0)
11933 if_modifier = OMP_TASKLOOP;
11934 else if (strcmp (p, "target") == 0)
11936 if_modifier = OMP_TARGET;
11937 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11939 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11940 if (strcmp ("data", p) == 0)
11941 if_modifier = OMP_TARGET_DATA;
11942 else if (strcmp ("update", p) == 0)
11943 if_modifier = OMP_TARGET_UPDATE;
11944 else if (strcmp ("enter", p) == 0)
11945 if_modifier = OMP_TARGET_ENTER_DATA;
11946 else if (strcmp ("exit", p) == 0)
11947 if_modifier = OMP_TARGET_EXIT_DATA;
11948 if (if_modifier != OMP_TARGET)
11950 n = 3;
11951 c_parser_consume_token (parser);
11953 else
11955 location_t loc = c_parser_peek_2nd_token (parser)->location;
11956 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11957 "or %<exit%>");
11958 if_modifier = ERROR_MARK;
11960 if (if_modifier == OMP_TARGET_ENTER_DATA
11961 || if_modifier == OMP_TARGET_EXIT_DATA)
11963 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11965 p = IDENTIFIER_POINTER
11966 (c_parser_peek_2nd_token (parser)->value);
11967 if (strcmp ("data", p) == 0)
11968 n = 4;
11970 if (n == 4)
11971 c_parser_consume_token (parser);
11972 else
11974 location_t loc
11975 = c_parser_peek_2nd_token (parser)->location;
11976 error_at (loc, "expected %<data%>");
11977 if_modifier = ERROR_MARK;
11982 if (if_modifier != ERROR_MARK)
11984 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11986 c_parser_consume_token (parser);
11987 c_parser_consume_token (parser);
11989 else
11991 if (n > 2)
11993 location_t loc = c_parser_peek_2nd_token (parser)->location;
11994 error_at (loc, "expected %<:%>");
11996 if_modifier = ERROR_MARK;
12001 tree t = c_parser_condition (parser), c;
12002 parens.skip_until_found_close (parser);
12004 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12005 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12007 if (if_modifier != ERROR_MARK
12008 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12010 const char *p = NULL;
12011 switch (if_modifier)
12013 case OMP_PARALLEL: p = "parallel"; break;
12014 case OMP_TASK: p = "task"; break;
12015 case OMP_TASKLOOP: p = "taskloop"; break;
12016 case OMP_TARGET_DATA: p = "target data"; break;
12017 case OMP_TARGET: p = "target"; break;
12018 case OMP_TARGET_UPDATE: p = "target update"; break;
12019 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12020 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12021 default: gcc_unreachable ();
12023 error_at (location, "too many %<if%> clauses with %qs modifier",
12025 return list;
12027 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12029 if (!is_omp)
12030 error_at (location, "too many %<if%> clauses");
12031 else
12032 error_at (location, "too many %<if%> clauses without modifier");
12033 return list;
12035 else if (if_modifier == ERROR_MARK
12036 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12038 error_at (location, "if any %<if%> clause has modifier, then all "
12039 "%<if%> clauses have to use modifier");
12040 return list;
12044 c = build_omp_clause (location, OMP_CLAUSE_IF);
12045 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12046 OMP_CLAUSE_IF_EXPR (c) = t;
12047 OMP_CLAUSE_CHAIN (c) = list;
12048 return c;
12051 /* OpenMP 2.5:
12052 lastprivate ( variable-list ) */
12054 static tree
12055 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12057 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12060 /* OpenMP 3.1:
12061 mergeable */
12063 static tree
12064 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12066 tree c;
12068 /* FIXME: Should we allow duplicates? */
12069 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12071 c = build_omp_clause (c_parser_peek_token (parser)->location,
12072 OMP_CLAUSE_MERGEABLE);
12073 OMP_CLAUSE_CHAIN (c) = list;
12075 return c;
12078 /* OpenMP 2.5:
12079 nowait */
12081 static tree
12082 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12084 tree c;
12085 location_t loc = c_parser_peek_token (parser)->location;
12087 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12089 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12090 OMP_CLAUSE_CHAIN (c) = list;
12091 return c;
12094 /* OpenMP 2.5:
12095 num_threads ( expression ) */
12097 static tree
12098 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12100 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12101 matching_parens parens;
12102 if (parens.require_open (parser))
12104 location_t expr_loc = c_parser_peek_token (parser)->location;
12105 c_expr expr = c_parser_expression (parser);
12106 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12107 tree c, t = expr.value;
12108 t = c_fully_fold (t, false, NULL);
12110 parens.skip_until_found_close (parser);
12112 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12114 c_parser_error (parser, "expected integer expression");
12115 return list;
12118 /* Attempt to statically determine when the number isn't positive. */
12119 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12120 build_int_cst (TREE_TYPE (t), 0));
12121 protected_set_expr_location (c, expr_loc);
12122 if (c == boolean_true_node)
12124 warning_at (expr_loc, 0,
12125 "%<num_threads%> value must be positive");
12126 t = integer_one_node;
12129 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12131 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12132 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12133 OMP_CLAUSE_CHAIN (c) = list;
12134 list = c;
12137 return list;
12140 /* OpenMP 4.5:
12141 num_tasks ( expression ) */
12143 static tree
12144 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12146 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12147 matching_parens parens;
12148 if (parens.require_open (parser))
12150 location_t expr_loc = c_parser_peek_token (parser)->location;
12151 c_expr expr = c_parser_expression (parser);
12152 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12153 tree c, t = expr.value;
12154 t = c_fully_fold (t, false, NULL);
12156 parens.skip_until_found_close (parser);
12158 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12160 c_parser_error (parser, "expected integer expression");
12161 return list;
12164 /* Attempt to statically determine when the number isn't positive. */
12165 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12166 build_int_cst (TREE_TYPE (t), 0));
12167 if (CAN_HAVE_LOCATION_P (c))
12168 SET_EXPR_LOCATION (c, expr_loc);
12169 if (c == boolean_true_node)
12171 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12172 t = integer_one_node;
12175 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12177 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12178 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12179 OMP_CLAUSE_CHAIN (c) = list;
12180 list = c;
12183 return list;
12186 /* OpenMP 4.5:
12187 grainsize ( expression ) */
12189 static tree
12190 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12192 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12193 matching_parens parens;
12194 if (parens.require_open (parser))
12196 location_t expr_loc = c_parser_peek_token (parser)->location;
12197 c_expr expr = c_parser_expression (parser);
12198 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12199 tree c, t = expr.value;
12200 t = c_fully_fold (t, false, NULL);
12202 parens.skip_until_found_close (parser);
12204 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12206 c_parser_error (parser, "expected integer expression");
12207 return list;
12210 /* Attempt to statically determine when the number isn't positive. */
12211 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12212 build_int_cst (TREE_TYPE (t), 0));
12213 if (CAN_HAVE_LOCATION_P (c))
12214 SET_EXPR_LOCATION (c, expr_loc);
12215 if (c == boolean_true_node)
12217 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12218 t = integer_one_node;
12221 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12223 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12224 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12225 OMP_CLAUSE_CHAIN (c) = list;
12226 list = c;
12229 return list;
12232 /* OpenMP 4.5:
12233 priority ( expression ) */
12235 static tree
12236 c_parser_omp_clause_priority (c_parser *parser, tree list)
12238 location_t priority_loc = c_parser_peek_token (parser)->location;
12239 matching_parens parens;
12240 if (parens.require_open (parser))
12242 location_t expr_loc = c_parser_peek_token (parser)->location;
12243 c_expr expr = c_parser_expression (parser);
12244 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12245 tree c, t = expr.value;
12246 t = c_fully_fold (t, false, NULL);
12248 parens.skip_until_found_close (parser);
12250 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12252 c_parser_error (parser, "expected integer expression");
12253 return list;
12256 /* Attempt to statically determine when the number isn't
12257 non-negative. */
12258 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12259 build_int_cst (TREE_TYPE (t), 0));
12260 if (CAN_HAVE_LOCATION_P (c))
12261 SET_EXPR_LOCATION (c, expr_loc);
12262 if (c == boolean_true_node)
12264 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12265 t = integer_one_node;
12268 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12270 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12271 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12272 OMP_CLAUSE_CHAIN (c) = list;
12273 list = c;
12276 return list;
12279 /* OpenMP 4.5:
12280 hint ( expression ) */
12282 static tree
12283 c_parser_omp_clause_hint (c_parser *parser, tree list)
12285 location_t hint_loc = c_parser_peek_token (parser)->location;
12286 matching_parens parens;
12287 if (parens.require_open (parser))
12289 location_t expr_loc = c_parser_peek_token (parser)->location;
12290 c_expr expr = c_parser_expression (parser);
12291 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12292 tree c, t = expr.value;
12293 t = c_fully_fold (t, false, NULL);
12295 parens.skip_until_found_close (parser);
12297 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12299 c_parser_error (parser, "expected integer expression");
12300 return list;
12303 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12305 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12306 OMP_CLAUSE_HINT_EXPR (c) = t;
12307 OMP_CLAUSE_CHAIN (c) = list;
12308 list = c;
12311 return list;
12314 /* OpenMP 4.5:
12315 defaultmap ( tofrom : scalar ) */
12317 static tree
12318 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12320 location_t loc = c_parser_peek_token (parser)->location;
12321 tree c;
12322 const char *p;
12324 matching_parens parens;
12325 if (!parens.require_open (parser))
12326 return list;
12327 if (!c_parser_next_token_is (parser, CPP_NAME))
12329 c_parser_error (parser, "expected %<tofrom%>");
12330 goto out_err;
12332 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12333 if (strcmp (p, "tofrom") != 0)
12335 c_parser_error (parser, "expected %<tofrom%>");
12336 goto out_err;
12338 c_parser_consume_token (parser);
12339 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12340 goto out_err;
12341 if (!c_parser_next_token_is (parser, CPP_NAME))
12343 c_parser_error (parser, "expected %<scalar%>");
12344 goto out_err;
12346 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12347 if (strcmp (p, "scalar") != 0)
12349 c_parser_error (parser, "expected %<scalar%>");
12350 goto out_err;
12352 c_parser_consume_token (parser);
12353 parens.skip_until_found_close (parser);
12354 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12355 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12356 OMP_CLAUSE_CHAIN (c) = list;
12357 return c;
12359 out_err:
12360 parens.skip_until_found_close (parser);
12361 return list;
12364 /* OpenACC 2.0:
12365 use_device ( variable-list )
12367 OpenMP 4.5:
12368 use_device_ptr ( variable-list ) */
12370 static tree
12371 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12373 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12374 list);
12377 /* OpenMP 4.5:
12378 is_device_ptr ( variable-list ) */
12380 static tree
12381 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12383 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12386 /* OpenACC:
12387 num_gangs ( expression )
12388 num_workers ( expression )
12389 vector_length ( expression ) */
12391 static tree
12392 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12393 tree list)
12395 location_t loc = c_parser_peek_token (parser)->location;
12397 matching_parens parens;
12398 if (!parens.require_open (parser))
12399 return list;
12401 location_t expr_loc = c_parser_peek_token (parser)->location;
12402 c_expr expr = c_parser_expression (parser);
12403 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12404 tree c, t = expr.value;
12405 t = c_fully_fold (t, false, NULL);
12407 parens.skip_until_found_close (parser);
12409 if (t == error_mark_node)
12410 return list;
12411 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12413 error_at (expr_loc, "%qs expression must be integral",
12414 omp_clause_code_name[code]);
12415 return list;
12418 /* Attempt to statically determine when the number isn't positive. */
12419 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12420 build_int_cst (TREE_TYPE (t), 0));
12421 protected_set_expr_location (c, expr_loc);
12422 if (c == boolean_true_node)
12424 warning_at (expr_loc, 0,
12425 "%qs value must be positive",
12426 omp_clause_code_name[code]);
12427 t = integer_one_node;
12430 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12432 c = build_omp_clause (loc, code);
12433 OMP_CLAUSE_OPERAND (c, 0) = t;
12434 OMP_CLAUSE_CHAIN (c) = list;
12435 return c;
12438 /* OpenACC:
12440 gang [( gang-arg-list )]
12441 worker [( [num:] int-expr )]
12442 vector [( [length:] int-expr )]
12444 where gang-arg is one of:
12446 [num:] int-expr
12447 static: size-expr
12449 and size-expr may be:
12452 int-expr
12455 static tree
12456 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12457 const char *str, tree list)
12459 const char *id = "num";
12460 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12461 location_t loc = c_parser_peek_token (parser)->location;
12463 if (kind == OMP_CLAUSE_VECTOR)
12464 id = "length";
12466 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12468 c_parser_consume_token (parser);
12472 c_token *next = c_parser_peek_token (parser);
12473 int idx = 0;
12475 /* Gang static argument. */
12476 if (kind == OMP_CLAUSE_GANG
12477 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12479 c_parser_consume_token (parser);
12481 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12482 goto cleanup_error;
12484 idx = 1;
12485 if (ops[idx] != NULL_TREE)
12487 c_parser_error (parser, "too many %<static%> arguments");
12488 goto cleanup_error;
12491 /* Check for the '*' argument. */
12492 if (c_parser_next_token_is (parser, CPP_MULT)
12493 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12494 || c_parser_peek_2nd_token (parser)->type
12495 == CPP_CLOSE_PAREN))
12497 c_parser_consume_token (parser);
12498 ops[idx] = integer_minus_one_node;
12500 if (c_parser_next_token_is (parser, CPP_COMMA))
12502 c_parser_consume_token (parser);
12503 continue;
12505 else
12506 break;
12509 /* Worker num: argument and vector length: arguments. */
12510 else if (c_parser_next_token_is (parser, CPP_NAME)
12511 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12512 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12514 c_parser_consume_token (parser); /* id */
12515 c_parser_consume_token (parser); /* ':' */
12518 /* Now collect the actual argument. */
12519 if (ops[idx] != NULL_TREE)
12521 c_parser_error (parser, "unexpected argument");
12522 goto cleanup_error;
12525 location_t expr_loc = c_parser_peek_token (parser)->location;
12526 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12527 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12528 tree expr = cexpr.value;
12529 if (expr == error_mark_node)
12530 goto cleanup_error;
12532 expr = c_fully_fold (expr, false, NULL);
12534 /* Attempt to statically determine when the number isn't a
12535 positive integer. */
12537 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12539 c_parser_error (parser, "expected integer expression");
12540 return list;
12543 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12544 build_int_cst (TREE_TYPE (expr), 0));
12545 if (c == boolean_true_node)
12547 warning_at (loc, 0,
12548 "%qs value must be positive", str);
12549 expr = integer_one_node;
12552 ops[idx] = expr;
12554 if (kind == OMP_CLAUSE_GANG
12555 && c_parser_next_token_is (parser, CPP_COMMA))
12557 c_parser_consume_token (parser);
12558 continue;
12560 break;
12562 while (1);
12564 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12565 goto cleanup_error;
12568 check_no_duplicate_clause (list, kind, str);
12570 c = build_omp_clause (loc, kind);
12572 if (ops[1])
12573 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12575 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12576 OMP_CLAUSE_CHAIN (c) = list;
12578 return c;
12580 cleanup_error:
12581 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12582 return list;
12585 /* OpenACC 2.5:
12586 auto
12587 finalize
12588 independent
12589 nohost
12590 seq */
12592 static tree
12593 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12594 tree list)
12596 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12598 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12599 OMP_CLAUSE_CHAIN (c) = list;
12601 return c;
12604 /* OpenACC:
12605 async [( int-expr )] */
12607 static tree
12608 c_parser_oacc_clause_async (c_parser *parser, tree list)
12610 tree c, t;
12611 location_t loc = c_parser_peek_token (parser)->location;
12613 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12615 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12617 c_parser_consume_token (parser);
12619 t = c_parser_expression (parser).value;
12620 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12621 c_parser_error (parser, "expected integer expression");
12622 else if (t == error_mark_node
12623 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12624 return list;
12626 else
12627 t = c_fully_fold (t, false, NULL);
12629 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12631 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12632 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12633 OMP_CLAUSE_CHAIN (c) = list;
12634 list = c;
12636 return list;
12639 /* OpenACC 2.0:
12640 tile ( size-expr-list ) */
12642 static tree
12643 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12645 tree c, expr = error_mark_node;
12646 location_t loc;
12647 tree tile = NULL_TREE;
12649 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12650 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12652 loc = c_parser_peek_token (parser)->location;
12653 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12654 return list;
12658 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12659 return list;
12661 if (c_parser_next_token_is (parser, CPP_MULT)
12662 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12663 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12665 c_parser_consume_token (parser);
12666 expr = integer_zero_node;
12668 else
12670 location_t expr_loc = c_parser_peek_token (parser)->location;
12671 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12672 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12673 expr = cexpr.value;
12675 if (expr == error_mark_node)
12677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12678 "expected %<)%>");
12679 return list;
12682 expr = c_fully_fold (expr, false, NULL);
12684 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12685 || !tree_fits_shwi_p (expr)
12686 || tree_to_shwi (expr) <= 0)
12688 error_at (expr_loc, "%<tile%> argument needs positive"
12689 " integral constant");
12690 expr = integer_zero_node;
12694 tile = tree_cons (NULL_TREE, expr, tile);
12696 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12698 /* Consume the trailing ')'. */
12699 c_parser_consume_token (parser);
12701 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12702 tile = nreverse (tile);
12703 OMP_CLAUSE_TILE_LIST (c) = tile;
12704 OMP_CLAUSE_CHAIN (c) = list;
12705 return c;
12708 /* OpenACC:
12709 wait ( int-expr-list ) */
12711 static tree
12712 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12714 location_t clause_loc = c_parser_peek_token (parser)->location;
12716 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12717 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12719 return list;
12722 /* OpenMP 2.5:
12723 ordered
12725 OpenMP 4.5:
12726 ordered ( constant-expression ) */
12728 static tree
12729 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12731 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12733 tree c, num = NULL_TREE;
12734 HOST_WIDE_INT n;
12735 location_t loc = c_parser_peek_token (parser)->location;
12736 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12738 matching_parens parens;
12739 parens.consume_open (parser);
12740 num = c_parser_expr_no_commas (parser, NULL).value;
12741 parens.skip_until_found_close (parser);
12743 if (num == error_mark_node)
12744 return list;
12745 if (num)
12747 mark_exp_read (num);
12748 num = c_fully_fold (num, false, NULL);
12749 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12750 || !tree_fits_shwi_p (num)
12751 || (n = tree_to_shwi (num)) <= 0
12752 || (int) n != n)
12754 error_at (loc, "ordered argument needs positive "
12755 "constant integer expression");
12756 return list;
12759 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12760 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12761 OMP_CLAUSE_CHAIN (c) = list;
12762 return c;
12765 /* OpenMP 2.5:
12766 private ( variable-list ) */
12768 static tree
12769 c_parser_omp_clause_private (c_parser *parser, tree list)
12771 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12774 /* OpenMP 2.5:
12775 reduction ( reduction-operator : variable-list )
12777 reduction-operator:
12778 One of: + * - & ^ | && ||
12780 OpenMP 3.1:
12782 reduction-operator:
12783 One of: + * - & ^ | && || max min
12785 OpenMP 4.0:
12787 reduction-operator:
12788 One of: + * - & ^ | && ||
12789 identifier */
12791 static tree
12792 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12794 location_t clause_loc = c_parser_peek_token (parser)->location;
12795 matching_parens parens;
12796 if (parens.require_open (parser))
12798 enum tree_code code = ERROR_MARK;
12799 tree reduc_id = NULL_TREE;
12801 switch (c_parser_peek_token (parser)->type)
12803 case CPP_PLUS:
12804 code = PLUS_EXPR;
12805 break;
12806 case CPP_MULT:
12807 code = MULT_EXPR;
12808 break;
12809 case CPP_MINUS:
12810 code = MINUS_EXPR;
12811 break;
12812 case CPP_AND:
12813 code = BIT_AND_EXPR;
12814 break;
12815 case CPP_XOR:
12816 code = BIT_XOR_EXPR;
12817 break;
12818 case CPP_OR:
12819 code = BIT_IOR_EXPR;
12820 break;
12821 case CPP_AND_AND:
12822 code = TRUTH_ANDIF_EXPR;
12823 break;
12824 case CPP_OR_OR:
12825 code = TRUTH_ORIF_EXPR;
12826 break;
12827 case CPP_NAME:
12829 const char *p
12830 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12831 if (strcmp (p, "min") == 0)
12833 code = MIN_EXPR;
12834 break;
12836 if (strcmp (p, "max") == 0)
12838 code = MAX_EXPR;
12839 break;
12841 reduc_id = c_parser_peek_token (parser)->value;
12842 break;
12844 default:
12845 c_parser_error (parser,
12846 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12847 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12849 return list;
12851 c_parser_consume_token (parser);
12852 reduc_id = c_omp_reduction_id (code, reduc_id);
12853 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12855 tree nl, c;
12857 nl = c_parser_omp_variable_list (parser, clause_loc,
12858 OMP_CLAUSE_REDUCTION, list);
12859 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12861 tree d = OMP_CLAUSE_DECL (c), type;
12862 if (TREE_CODE (d) != TREE_LIST)
12863 type = TREE_TYPE (d);
12864 else
12866 int cnt = 0;
12867 tree t;
12868 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12869 cnt++;
12870 type = TREE_TYPE (t);
12871 while (cnt > 0)
12873 if (TREE_CODE (type) != POINTER_TYPE
12874 && TREE_CODE (type) != ARRAY_TYPE)
12875 break;
12876 type = TREE_TYPE (type);
12877 cnt--;
12880 while (TREE_CODE (type) == ARRAY_TYPE)
12881 type = TREE_TYPE (type);
12882 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12883 if (code == ERROR_MARK
12884 || !(INTEGRAL_TYPE_P (type)
12885 || TREE_CODE (type) == REAL_TYPE
12886 || TREE_CODE (type) == COMPLEX_TYPE))
12887 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12888 = c_omp_reduction_lookup (reduc_id,
12889 TYPE_MAIN_VARIANT (type));
12892 list = nl;
12894 parens.skip_until_found_close (parser);
12896 return list;
12899 /* OpenMP 2.5:
12900 schedule ( schedule-kind )
12901 schedule ( schedule-kind , expression )
12903 schedule-kind:
12904 static | dynamic | guided | runtime | auto
12906 OpenMP 4.5:
12907 schedule ( schedule-modifier : schedule-kind )
12908 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12910 schedule-modifier:
12911 simd
12912 monotonic
12913 nonmonotonic */
12915 static tree
12916 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12918 tree c, t;
12919 location_t loc = c_parser_peek_token (parser)->location;
12920 int modifiers = 0, nmodifiers = 0;
12922 matching_parens parens;
12923 if (!parens.require_open (parser))
12924 return list;
12926 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12928 while (c_parser_next_token_is (parser, CPP_NAME))
12930 tree kind = c_parser_peek_token (parser)->value;
12931 const char *p = IDENTIFIER_POINTER (kind);
12932 if (strcmp ("simd", p) == 0)
12933 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12934 else if (strcmp ("monotonic", p) == 0)
12935 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12936 else if (strcmp ("nonmonotonic", p) == 0)
12937 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12938 else
12939 break;
12940 c_parser_consume_token (parser);
12941 if (nmodifiers++ == 0
12942 && c_parser_next_token_is (parser, CPP_COMMA))
12943 c_parser_consume_token (parser);
12944 else
12946 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12947 break;
12951 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12952 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12953 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12954 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12956 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12957 "specified");
12958 modifiers = 0;
12961 if (c_parser_next_token_is (parser, CPP_NAME))
12963 tree kind = c_parser_peek_token (parser)->value;
12964 const char *p = IDENTIFIER_POINTER (kind);
12966 switch (p[0])
12968 case 'd':
12969 if (strcmp ("dynamic", p) != 0)
12970 goto invalid_kind;
12971 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12972 break;
12974 case 'g':
12975 if (strcmp ("guided", p) != 0)
12976 goto invalid_kind;
12977 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12978 break;
12980 case 'r':
12981 if (strcmp ("runtime", p) != 0)
12982 goto invalid_kind;
12983 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12984 break;
12986 default:
12987 goto invalid_kind;
12990 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12991 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12992 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12993 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12994 else
12995 goto invalid_kind;
12997 c_parser_consume_token (parser);
12998 if (c_parser_next_token_is (parser, CPP_COMMA))
13000 location_t here;
13001 c_parser_consume_token (parser);
13003 here = c_parser_peek_token (parser)->location;
13004 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13005 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13006 t = expr.value;
13007 t = c_fully_fold (t, false, NULL);
13009 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13010 error_at (here, "schedule %<runtime%> does not take "
13011 "a %<chunk_size%> parameter");
13012 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13013 error_at (here,
13014 "schedule %<auto%> does not take "
13015 "a %<chunk_size%> parameter");
13016 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13018 /* Attempt to statically determine when the number isn't
13019 positive. */
13020 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13021 build_int_cst (TREE_TYPE (t), 0));
13022 protected_set_expr_location (s, loc);
13023 if (s == boolean_true_node)
13025 warning_at (loc, 0,
13026 "chunk size value must be positive");
13027 t = integer_one_node;
13029 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13031 else
13032 c_parser_error (parser, "expected integer expression");
13034 parens.skip_until_found_close (parser);
13036 else
13037 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13038 "expected %<,%> or %<)%>");
13040 OMP_CLAUSE_SCHEDULE_KIND (c)
13041 = (enum omp_clause_schedule_kind)
13042 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13044 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13045 OMP_CLAUSE_CHAIN (c) = list;
13046 return c;
13048 invalid_kind:
13049 c_parser_error (parser, "invalid schedule kind");
13050 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13051 return list;
13054 /* OpenMP 2.5:
13055 shared ( variable-list ) */
13057 static tree
13058 c_parser_omp_clause_shared (c_parser *parser, tree list)
13060 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13063 /* OpenMP 3.0:
13064 untied */
13066 static tree
13067 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13069 tree c;
13071 /* FIXME: Should we allow duplicates? */
13072 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13074 c = build_omp_clause (c_parser_peek_token (parser)->location,
13075 OMP_CLAUSE_UNTIED);
13076 OMP_CLAUSE_CHAIN (c) = list;
13078 return c;
13081 /* OpenMP 4.0:
13082 inbranch
13083 notinbranch */
13085 static tree
13086 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13087 enum omp_clause_code code, tree list)
13089 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13091 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13092 OMP_CLAUSE_CHAIN (c) = list;
13094 return c;
13097 /* OpenMP 4.0:
13098 parallel
13100 sections
13101 taskgroup */
13103 static tree
13104 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13105 enum omp_clause_code code, tree list)
13107 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13108 OMP_CLAUSE_CHAIN (c) = list;
13110 return c;
13113 /* OpenMP 4.5:
13114 nogroup */
13116 static tree
13117 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13119 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13120 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13121 OMP_CLAUSE_NOGROUP);
13122 OMP_CLAUSE_CHAIN (c) = list;
13123 return c;
13126 /* OpenMP 4.5:
13127 simd
13128 threads */
13130 static tree
13131 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13132 enum omp_clause_code code, tree list)
13134 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13135 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13136 OMP_CLAUSE_CHAIN (c) = list;
13137 return c;
13140 /* OpenMP 4.0:
13141 num_teams ( expression ) */
13143 static tree
13144 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13146 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13147 matching_parens parens;
13148 if (parens.require_open (parser))
13150 location_t expr_loc = c_parser_peek_token (parser)->location;
13151 c_expr expr = c_parser_expression (parser);
13152 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13153 tree c, t = expr.value;
13154 t = c_fully_fold (t, false, NULL);
13156 parens.skip_until_found_close (parser);
13158 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13160 c_parser_error (parser, "expected integer expression");
13161 return list;
13164 /* Attempt to statically determine when the number isn't positive. */
13165 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13166 build_int_cst (TREE_TYPE (t), 0));
13167 protected_set_expr_location (c, expr_loc);
13168 if (c == boolean_true_node)
13170 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13171 t = integer_one_node;
13174 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13176 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13177 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13178 OMP_CLAUSE_CHAIN (c) = list;
13179 list = c;
13182 return list;
13185 /* OpenMP 4.0:
13186 thread_limit ( expression ) */
13188 static tree
13189 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13191 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13192 matching_parens parens;
13193 if (parens.require_open (parser))
13195 location_t expr_loc = c_parser_peek_token (parser)->location;
13196 c_expr expr = c_parser_expression (parser);
13197 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13198 tree c, t = expr.value;
13199 t = c_fully_fold (t, false, NULL);
13201 parens.skip_until_found_close (parser);
13203 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13205 c_parser_error (parser, "expected integer expression");
13206 return list;
13209 /* Attempt to statically determine when the number isn't positive. */
13210 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13211 build_int_cst (TREE_TYPE (t), 0));
13212 protected_set_expr_location (c, expr_loc);
13213 if (c == boolean_true_node)
13215 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13216 t = integer_one_node;
13219 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13220 "thread_limit");
13222 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13223 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13224 OMP_CLAUSE_CHAIN (c) = list;
13225 list = c;
13228 return list;
13231 /* OpenMP 4.0:
13232 aligned ( variable-list )
13233 aligned ( variable-list : constant-expression ) */
13235 static tree
13236 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13238 location_t clause_loc = c_parser_peek_token (parser)->location;
13239 tree nl, c;
13241 matching_parens parens;
13242 if (!parens.require_open (parser))
13243 return list;
13245 nl = c_parser_omp_variable_list (parser, clause_loc,
13246 OMP_CLAUSE_ALIGNED, list);
13248 if (c_parser_next_token_is (parser, CPP_COLON))
13250 c_parser_consume_token (parser);
13251 location_t expr_loc = c_parser_peek_token (parser)->location;
13252 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13253 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13254 tree alignment = expr.value;
13255 alignment = c_fully_fold (alignment, false, NULL);
13256 if (TREE_CODE (alignment) != INTEGER_CST
13257 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13258 || tree_int_cst_sgn (alignment) != 1)
13260 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13261 "be positive constant integer expression");
13262 alignment = NULL_TREE;
13265 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13266 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13269 parens.skip_until_found_close (parser);
13270 return nl;
13273 /* OpenMP 4.0:
13274 linear ( variable-list )
13275 linear ( variable-list : expression )
13277 OpenMP 4.5:
13278 linear ( modifier ( variable-list ) )
13279 linear ( modifier ( variable-list ) : expression ) */
13281 static tree
13282 c_parser_omp_clause_linear (c_parser *parser, tree list)
13284 location_t clause_loc = c_parser_peek_token (parser)->location;
13285 tree nl, c, step;
13286 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13288 matching_parens parens;
13289 if (!parens.require_open (parser))
13290 return list;
13292 if (c_parser_next_token_is (parser, CPP_NAME))
13294 c_token *tok = c_parser_peek_token (parser);
13295 const char *p = IDENTIFIER_POINTER (tok->value);
13296 if (strcmp ("val", p) == 0)
13297 kind = OMP_CLAUSE_LINEAR_VAL;
13298 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13299 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13300 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13302 c_parser_consume_token (parser);
13303 c_parser_consume_token (parser);
13307 nl = c_parser_omp_variable_list (parser, clause_loc,
13308 OMP_CLAUSE_LINEAR, list);
13310 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13311 parens.skip_until_found_close (parser);
13313 if (c_parser_next_token_is (parser, CPP_COLON))
13315 c_parser_consume_token (parser);
13316 location_t expr_loc = c_parser_peek_token (parser)->location;
13317 c_expr expr = c_parser_expression (parser);
13318 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13319 step = expr.value;
13320 step = c_fully_fold (step, false, NULL);
13321 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13323 error_at (clause_loc, "%<linear%> clause step expression must "
13324 "be integral");
13325 step = integer_one_node;
13329 else
13330 step = integer_one_node;
13332 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13334 OMP_CLAUSE_LINEAR_STEP (c) = step;
13335 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13338 parens.skip_until_found_close (parser);
13339 return nl;
13342 /* OpenMP 4.0:
13343 safelen ( constant-expression ) */
13345 static tree
13346 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13348 location_t clause_loc = c_parser_peek_token (parser)->location;
13349 tree c, t;
13351 matching_parens parens;
13352 if (!parens.require_open (parser))
13353 return list;
13355 location_t expr_loc = c_parser_peek_token (parser)->location;
13356 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13357 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13358 t = expr.value;
13359 t = c_fully_fold (t, false, NULL);
13360 if (TREE_CODE (t) != INTEGER_CST
13361 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13362 || tree_int_cst_sgn (t) != 1)
13364 error_at (clause_loc, "%<safelen%> clause expression must "
13365 "be positive constant integer expression");
13366 t = NULL_TREE;
13369 parens.skip_until_found_close (parser);
13370 if (t == NULL_TREE || t == error_mark_node)
13371 return list;
13373 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13375 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13376 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13377 OMP_CLAUSE_CHAIN (c) = list;
13378 return c;
13381 /* OpenMP 4.0:
13382 simdlen ( constant-expression ) */
13384 static tree
13385 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13387 location_t clause_loc = c_parser_peek_token (parser)->location;
13388 tree c, t;
13390 matching_parens parens;
13391 if (!parens.require_open (parser))
13392 return list;
13394 location_t expr_loc = c_parser_peek_token (parser)->location;
13395 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13396 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13397 t = expr.value;
13398 t = c_fully_fold (t, false, NULL);
13399 if (TREE_CODE (t) != INTEGER_CST
13400 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13401 || tree_int_cst_sgn (t) != 1)
13403 error_at (clause_loc, "%<simdlen%> clause expression must "
13404 "be positive constant integer expression");
13405 t = NULL_TREE;
13408 parens.skip_until_found_close (parser);
13409 if (t == NULL_TREE || t == error_mark_node)
13410 return list;
13412 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13414 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13415 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13416 OMP_CLAUSE_CHAIN (c) = list;
13417 return c;
13420 /* OpenMP 4.5:
13421 vec:
13422 identifier [+/- integer]
13423 vec , identifier [+/- integer]
13426 static tree
13427 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13428 tree list)
13430 tree vec = NULL;
13431 if (c_parser_next_token_is_not (parser, CPP_NAME)
13432 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13434 c_parser_error (parser, "expected identifier");
13435 return list;
13438 while (c_parser_next_token_is (parser, CPP_NAME)
13439 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13441 tree t = lookup_name (c_parser_peek_token (parser)->value);
13442 tree addend = NULL;
13444 if (t == NULL_TREE)
13446 undeclared_variable (c_parser_peek_token (parser)->location,
13447 c_parser_peek_token (parser)->value);
13448 t = error_mark_node;
13451 c_parser_consume_token (parser);
13453 bool neg = false;
13454 if (c_parser_next_token_is (parser, CPP_MINUS))
13455 neg = true;
13456 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13458 addend = integer_zero_node;
13459 neg = false;
13460 goto add_to_vector;
13462 c_parser_consume_token (parser);
13464 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13466 c_parser_error (parser, "expected integer");
13467 return list;
13470 addend = c_parser_peek_token (parser)->value;
13471 if (TREE_CODE (addend) != INTEGER_CST)
13473 c_parser_error (parser, "expected integer");
13474 return list;
13476 c_parser_consume_token (parser);
13478 add_to_vector:
13479 if (t != error_mark_node)
13481 vec = tree_cons (addend, t, vec);
13482 if (neg)
13483 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13486 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13487 break;
13489 c_parser_consume_token (parser);
13492 if (vec == NULL_TREE)
13493 return list;
13495 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13496 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13497 OMP_CLAUSE_DECL (u) = nreverse (vec);
13498 OMP_CLAUSE_CHAIN (u) = list;
13499 return u;
13502 /* OpenMP 4.0:
13503 depend ( depend-kind: variable-list )
13505 depend-kind:
13506 in | out | inout
13508 OpenMP 4.5:
13509 depend ( source )
13511 depend ( sink : vec ) */
13513 static tree
13514 c_parser_omp_clause_depend (c_parser *parser, tree list)
13516 location_t clause_loc = c_parser_peek_token (parser)->location;
13517 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13518 tree nl, c;
13520 matching_parens parens;
13521 if (!parens.require_open (parser))
13522 return list;
13524 if (c_parser_next_token_is (parser, CPP_NAME))
13526 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13527 if (strcmp ("in", p) == 0)
13528 kind = OMP_CLAUSE_DEPEND_IN;
13529 else if (strcmp ("inout", p) == 0)
13530 kind = OMP_CLAUSE_DEPEND_INOUT;
13531 else if (strcmp ("out", p) == 0)
13532 kind = OMP_CLAUSE_DEPEND_OUT;
13533 else if (strcmp ("source", p) == 0)
13534 kind = OMP_CLAUSE_DEPEND_SOURCE;
13535 else if (strcmp ("sink", p) == 0)
13536 kind = OMP_CLAUSE_DEPEND_SINK;
13537 else
13538 goto invalid_kind;
13540 else
13541 goto invalid_kind;
13543 c_parser_consume_token (parser);
13545 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13547 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13548 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13549 OMP_CLAUSE_DECL (c) = NULL_TREE;
13550 OMP_CLAUSE_CHAIN (c) = list;
13551 parens.skip_until_found_close (parser);
13552 return c;
13555 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13556 goto resync_fail;
13558 if (kind == OMP_CLAUSE_DEPEND_SINK)
13559 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13560 else
13562 nl = c_parser_omp_variable_list (parser, clause_loc,
13563 OMP_CLAUSE_DEPEND, list);
13565 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13566 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13569 parens.skip_until_found_close (parser);
13570 return nl;
13572 invalid_kind:
13573 c_parser_error (parser, "invalid depend kind");
13574 resync_fail:
13575 parens.skip_until_found_close (parser);
13576 return list;
13579 /* OpenMP 4.0:
13580 map ( map-kind: variable-list )
13581 map ( variable-list )
13583 map-kind:
13584 alloc | to | from | tofrom
13586 OpenMP 4.5:
13587 map-kind:
13588 alloc | to | from | tofrom | release | delete
13590 map ( always [,] map-kind: variable-list ) */
13592 static tree
13593 c_parser_omp_clause_map (c_parser *parser, tree list)
13595 location_t clause_loc = c_parser_peek_token (parser)->location;
13596 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13597 int always = 0;
13598 enum c_id_kind always_id_kind = C_ID_NONE;
13599 location_t always_loc = UNKNOWN_LOCATION;
13600 tree always_id = NULL_TREE;
13601 tree nl, c;
13603 matching_parens parens;
13604 if (!parens.require_open (parser))
13605 return list;
13607 if (c_parser_next_token_is (parser, CPP_NAME))
13609 c_token *tok = c_parser_peek_token (parser);
13610 const char *p = IDENTIFIER_POINTER (tok->value);
13611 always_id_kind = tok->id_kind;
13612 always_loc = tok->location;
13613 always_id = tok->value;
13614 if (strcmp ("always", p) == 0)
13616 c_token *sectok = c_parser_peek_2nd_token (parser);
13617 if (sectok->type == CPP_COMMA)
13619 c_parser_consume_token (parser);
13620 c_parser_consume_token (parser);
13621 always = 2;
13623 else if (sectok->type == CPP_NAME)
13625 p = IDENTIFIER_POINTER (sectok->value);
13626 if (strcmp ("alloc", p) == 0
13627 || strcmp ("to", p) == 0
13628 || strcmp ("from", p) == 0
13629 || strcmp ("tofrom", p) == 0
13630 || strcmp ("release", p) == 0
13631 || strcmp ("delete", p) == 0)
13633 c_parser_consume_token (parser);
13634 always = 1;
13640 if (c_parser_next_token_is (parser, CPP_NAME)
13641 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13643 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13644 if (strcmp ("alloc", p) == 0)
13645 kind = GOMP_MAP_ALLOC;
13646 else if (strcmp ("to", p) == 0)
13647 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13648 else if (strcmp ("from", p) == 0)
13649 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13650 else if (strcmp ("tofrom", p) == 0)
13651 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13652 else if (strcmp ("release", p) == 0)
13653 kind = GOMP_MAP_RELEASE;
13654 else if (strcmp ("delete", p) == 0)
13655 kind = GOMP_MAP_DELETE;
13656 else
13658 c_parser_error (parser, "invalid map kind");
13659 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13660 "expected %<)%>");
13661 return list;
13663 c_parser_consume_token (parser);
13664 c_parser_consume_token (parser);
13666 else if (always)
13668 if (always_id_kind != C_ID_ID)
13670 c_parser_error (parser, "expected identifier");
13671 parens.skip_until_found_close (parser);
13672 return list;
13675 tree t = lookup_name (always_id);
13676 if (t == NULL_TREE)
13678 undeclared_variable (always_loc, always_id);
13679 t = error_mark_node;
13681 if (t != error_mark_node)
13683 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13684 OMP_CLAUSE_DECL (u) = t;
13685 OMP_CLAUSE_CHAIN (u) = list;
13686 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13687 list = u;
13689 if (always == 1)
13691 parens.skip_until_found_close (parser);
13692 return list;
13696 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13698 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13699 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13701 parens.skip_until_found_close (parser);
13702 return nl;
13705 /* OpenMP 4.0:
13706 device ( expression ) */
13708 static tree
13709 c_parser_omp_clause_device (c_parser *parser, tree list)
13711 location_t clause_loc = c_parser_peek_token (parser)->location;
13712 matching_parens parens;
13713 if (parens.require_open (parser))
13715 location_t expr_loc = c_parser_peek_token (parser)->location;
13716 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13717 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13718 tree c, t = expr.value;
13719 t = c_fully_fold (t, false, NULL);
13721 parens.skip_until_found_close (parser);
13723 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13725 c_parser_error (parser, "expected integer expression");
13726 return list;
13729 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13731 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13732 OMP_CLAUSE_DEVICE_ID (c) = t;
13733 OMP_CLAUSE_CHAIN (c) = list;
13734 list = c;
13737 return list;
13740 /* OpenMP 4.0:
13741 dist_schedule ( static )
13742 dist_schedule ( static , expression ) */
13744 static tree
13745 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13747 tree c, t = NULL_TREE;
13748 location_t loc = c_parser_peek_token (parser)->location;
13750 matching_parens parens;
13751 if (!parens.require_open (parser))
13752 return list;
13754 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13756 c_parser_error (parser, "invalid dist_schedule kind");
13757 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13758 "expected %<)%>");
13759 return list;
13762 c_parser_consume_token (parser);
13763 if (c_parser_next_token_is (parser, CPP_COMMA))
13765 c_parser_consume_token (parser);
13767 location_t expr_loc = c_parser_peek_token (parser)->location;
13768 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13769 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13770 t = expr.value;
13771 t = c_fully_fold (t, false, NULL);
13772 parens.skip_until_found_close (parser);
13774 else
13775 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13776 "expected %<,%> or %<)%>");
13778 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13779 if (t == error_mark_node)
13780 return list;
13782 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13783 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13784 OMP_CLAUSE_CHAIN (c) = list;
13785 return c;
13788 /* OpenMP 4.0:
13789 proc_bind ( proc-bind-kind )
13791 proc-bind-kind:
13792 master | close | spread */
13794 static tree
13795 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13797 location_t clause_loc = c_parser_peek_token (parser)->location;
13798 enum omp_clause_proc_bind_kind kind;
13799 tree c;
13801 matching_parens parens;
13802 if (!parens.require_open (parser))
13803 return list;
13805 if (c_parser_next_token_is (parser, CPP_NAME))
13807 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13808 if (strcmp ("master", p) == 0)
13809 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13810 else if (strcmp ("close", p) == 0)
13811 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13812 else if (strcmp ("spread", p) == 0)
13813 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13814 else
13815 goto invalid_kind;
13817 else
13818 goto invalid_kind;
13820 c_parser_consume_token (parser);
13821 parens.skip_until_found_close (parser);
13822 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13823 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13824 OMP_CLAUSE_CHAIN (c) = list;
13825 return c;
13827 invalid_kind:
13828 c_parser_error (parser, "invalid proc_bind kind");
13829 parens.skip_until_found_close (parser);
13830 return list;
13833 /* OpenMP 4.0:
13834 to ( variable-list ) */
13836 static tree
13837 c_parser_omp_clause_to (c_parser *parser, tree list)
13839 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13842 /* OpenMP 4.0:
13843 from ( variable-list ) */
13845 static tree
13846 c_parser_omp_clause_from (c_parser *parser, tree list)
13848 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13851 /* OpenMP 4.0:
13852 uniform ( variable-list ) */
13854 static tree
13855 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13857 /* The clauses location. */
13858 location_t loc = c_parser_peek_token (parser)->location;
13860 matching_parens parens;
13861 if (parens.require_open (parser))
13863 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13864 list);
13865 parens.skip_until_found_close (parser);
13867 return list;
13870 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13871 is a bitmask in MASK. Return the list of clauses found. */
13873 static tree
13874 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13875 const char *where, bool finish_p = true)
13877 tree clauses = NULL;
13878 bool first = true;
13880 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13882 location_t here;
13883 pragma_omp_clause c_kind;
13884 const char *c_name;
13885 tree prev = clauses;
13887 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13888 c_parser_consume_token (parser);
13890 here = c_parser_peek_token (parser)->location;
13891 c_kind = c_parser_omp_clause_name (parser);
13893 switch (c_kind)
13895 case PRAGMA_OACC_CLAUSE_ASYNC:
13896 clauses = c_parser_oacc_clause_async (parser, clauses);
13897 c_name = "async";
13898 break;
13899 case PRAGMA_OACC_CLAUSE_AUTO:
13900 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13901 clauses);
13902 c_name = "auto";
13903 break;
13904 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13905 clauses = c_parser_omp_clause_collapse (parser, clauses);
13906 c_name = "collapse";
13907 break;
13908 case PRAGMA_OACC_CLAUSE_COPY:
13909 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13910 c_name = "copy";
13911 break;
13912 case PRAGMA_OACC_CLAUSE_COPYIN:
13913 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13914 c_name = "copyin";
13915 break;
13916 case PRAGMA_OACC_CLAUSE_COPYOUT:
13917 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13918 c_name = "copyout";
13919 break;
13920 case PRAGMA_OACC_CLAUSE_CREATE:
13921 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13922 c_name = "create";
13923 break;
13924 case PRAGMA_OACC_CLAUSE_DELETE:
13925 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13926 c_name = "delete";
13927 break;
13928 case PRAGMA_OMP_CLAUSE_DEFAULT:
13929 clauses = c_parser_omp_clause_default (parser, clauses, true);
13930 c_name = "default";
13931 break;
13932 case PRAGMA_OACC_CLAUSE_DEVICE:
13933 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13934 c_name = "device";
13935 break;
13936 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13937 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13938 c_name = "deviceptr";
13939 break;
13940 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13941 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13942 c_name = "device_resident";
13943 break;
13944 case PRAGMA_OACC_CLAUSE_FINALIZE:
13945 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
13946 clauses);
13947 c_name = "finalize";
13948 break;
13949 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13950 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13951 c_name = "firstprivate";
13952 break;
13953 case PRAGMA_OACC_CLAUSE_GANG:
13954 c_name = "gang";
13955 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13956 c_name, clauses);
13957 break;
13958 case PRAGMA_OACC_CLAUSE_HOST:
13959 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13960 c_name = "host";
13961 break;
13962 case PRAGMA_OACC_CLAUSE_IF:
13963 clauses = c_parser_omp_clause_if (parser, clauses, false);
13964 c_name = "if";
13965 break;
13966 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
13967 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_IF_PRESENT,
13968 clauses);
13969 c_name = "if_present";
13970 break;
13971 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13972 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13973 clauses);
13974 c_name = "independent";
13975 break;
13976 case PRAGMA_OACC_CLAUSE_LINK:
13977 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13978 c_name = "link";
13979 break;
13980 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13981 clauses = c_parser_oacc_single_int_clause (parser,
13982 OMP_CLAUSE_NUM_GANGS,
13983 clauses);
13984 c_name = "num_gangs";
13985 break;
13986 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13987 clauses = c_parser_oacc_single_int_clause (parser,
13988 OMP_CLAUSE_NUM_WORKERS,
13989 clauses);
13990 c_name = "num_workers";
13991 break;
13992 case PRAGMA_OACC_CLAUSE_PRESENT:
13993 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13994 c_name = "present";
13995 break;
13996 case PRAGMA_OACC_CLAUSE_PRIVATE:
13997 clauses = c_parser_omp_clause_private (parser, clauses);
13998 c_name = "private";
13999 break;
14000 case PRAGMA_OACC_CLAUSE_REDUCTION:
14001 clauses = c_parser_omp_clause_reduction (parser, clauses);
14002 c_name = "reduction";
14003 break;
14004 case PRAGMA_OACC_CLAUSE_SEQ:
14005 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14006 clauses);
14007 c_name = "seq";
14008 break;
14009 case PRAGMA_OACC_CLAUSE_TILE:
14010 clauses = c_parser_oacc_clause_tile (parser, clauses);
14011 c_name = "tile";
14012 break;
14013 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14014 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14015 c_name = "use_device";
14016 break;
14017 case PRAGMA_OACC_CLAUSE_VECTOR:
14018 c_name = "vector";
14019 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14020 c_name, clauses);
14021 break;
14022 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14023 clauses = c_parser_oacc_single_int_clause (parser,
14024 OMP_CLAUSE_VECTOR_LENGTH,
14025 clauses);
14026 c_name = "vector_length";
14027 break;
14028 case PRAGMA_OACC_CLAUSE_WAIT:
14029 clauses = c_parser_oacc_clause_wait (parser, clauses);
14030 c_name = "wait";
14031 break;
14032 case PRAGMA_OACC_CLAUSE_WORKER:
14033 c_name = "worker";
14034 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14035 c_name, clauses);
14036 break;
14037 default:
14038 c_parser_error (parser, "expected %<#pragma acc%> clause");
14039 goto saw_error;
14042 first = false;
14044 if (((mask >> c_kind) & 1) == 0)
14046 /* Remove the invalid clause(s) from the list to avoid
14047 confusing the rest of the compiler. */
14048 clauses = prev;
14049 error_at (here, "%qs is not valid for %qs", c_name, where);
14053 saw_error:
14054 c_parser_skip_to_pragma_eol (parser);
14056 if (finish_p)
14057 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14059 return clauses;
14062 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14063 is a bitmask in MASK. Return the list of clauses found. */
14065 static tree
14066 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14067 const char *where, bool finish_p = true)
14069 tree clauses = NULL;
14070 bool first = true;
14072 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14074 location_t here;
14075 pragma_omp_clause c_kind;
14076 const char *c_name;
14077 tree prev = clauses;
14079 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14080 c_parser_consume_token (parser);
14082 here = c_parser_peek_token (parser)->location;
14083 c_kind = c_parser_omp_clause_name (parser);
14085 switch (c_kind)
14087 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14088 clauses = c_parser_omp_clause_collapse (parser, clauses);
14089 c_name = "collapse";
14090 break;
14091 case PRAGMA_OMP_CLAUSE_COPYIN:
14092 clauses = c_parser_omp_clause_copyin (parser, clauses);
14093 c_name = "copyin";
14094 break;
14095 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14096 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14097 c_name = "copyprivate";
14098 break;
14099 case PRAGMA_OMP_CLAUSE_DEFAULT:
14100 clauses = c_parser_omp_clause_default (parser, clauses, false);
14101 c_name = "default";
14102 break;
14103 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14104 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14105 c_name = "firstprivate";
14106 break;
14107 case PRAGMA_OMP_CLAUSE_FINAL:
14108 clauses = c_parser_omp_clause_final (parser, clauses);
14109 c_name = "final";
14110 break;
14111 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14112 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14113 c_name = "grainsize";
14114 break;
14115 case PRAGMA_OMP_CLAUSE_HINT:
14116 clauses = c_parser_omp_clause_hint (parser, clauses);
14117 c_name = "hint";
14118 break;
14119 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14120 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14121 c_name = "defaultmap";
14122 break;
14123 case PRAGMA_OMP_CLAUSE_IF:
14124 clauses = c_parser_omp_clause_if (parser, clauses, true);
14125 c_name = "if";
14126 break;
14127 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14128 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14129 c_name = "lastprivate";
14130 break;
14131 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14132 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14133 c_name = "mergeable";
14134 break;
14135 case PRAGMA_OMP_CLAUSE_NOWAIT:
14136 clauses = c_parser_omp_clause_nowait (parser, clauses);
14137 c_name = "nowait";
14138 break;
14139 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14140 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14141 c_name = "num_tasks";
14142 break;
14143 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14144 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14145 c_name = "num_threads";
14146 break;
14147 case PRAGMA_OMP_CLAUSE_ORDERED:
14148 clauses = c_parser_omp_clause_ordered (parser, clauses);
14149 c_name = "ordered";
14150 break;
14151 case PRAGMA_OMP_CLAUSE_PRIORITY:
14152 clauses = c_parser_omp_clause_priority (parser, clauses);
14153 c_name = "priority";
14154 break;
14155 case PRAGMA_OMP_CLAUSE_PRIVATE:
14156 clauses = c_parser_omp_clause_private (parser, clauses);
14157 c_name = "private";
14158 break;
14159 case PRAGMA_OMP_CLAUSE_REDUCTION:
14160 clauses = c_parser_omp_clause_reduction (parser, clauses);
14161 c_name = "reduction";
14162 break;
14163 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14164 clauses = c_parser_omp_clause_schedule (parser, clauses);
14165 c_name = "schedule";
14166 break;
14167 case PRAGMA_OMP_CLAUSE_SHARED:
14168 clauses = c_parser_omp_clause_shared (parser, clauses);
14169 c_name = "shared";
14170 break;
14171 case PRAGMA_OMP_CLAUSE_UNTIED:
14172 clauses = c_parser_omp_clause_untied (parser, clauses);
14173 c_name = "untied";
14174 break;
14175 case PRAGMA_OMP_CLAUSE_INBRANCH:
14176 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14177 clauses);
14178 c_name = "inbranch";
14179 break;
14180 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14181 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14182 clauses);
14183 c_name = "notinbranch";
14184 break;
14185 case PRAGMA_OMP_CLAUSE_PARALLEL:
14186 clauses
14187 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14188 clauses);
14189 c_name = "parallel";
14190 if (!first)
14192 clause_not_first:
14193 error_at (here, "%qs must be the first clause of %qs",
14194 c_name, where);
14195 clauses = prev;
14197 break;
14198 case PRAGMA_OMP_CLAUSE_FOR:
14199 clauses
14200 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14201 clauses);
14202 c_name = "for";
14203 if (!first)
14204 goto clause_not_first;
14205 break;
14206 case PRAGMA_OMP_CLAUSE_SECTIONS:
14207 clauses
14208 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14209 clauses);
14210 c_name = "sections";
14211 if (!first)
14212 goto clause_not_first;
14213 break;
14214 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14215 clauses
14216 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14217 clauses);
14218 c_name = "taskgroup";
14219 if (!first)
14220 goto clause_not_first;
14221 break;
14222 case PRAGMA_OMP_CLAUSE_LINK:
14223 clauses
14224 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14225 c_name = "link";
14226 break;
14227 case PRAGMA_OMP_CLAUSE_TO:
14228 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14229 clauses
14230 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14231 clauses);
14232 else
14233 clauses = c_parser_omp_clause_to (parser, clauses);
14234 c_name = "to";
14235 break;
14236 case PRAGMA_OMP_CLAUSE_FROM:
14237 clauses = c_parser_omp_clause_from (parser, clauses);
14238 c_name = "from";
14239 break;
14240 case PRAGMA_OMP_CLAUSE_UNIFORM:
14241 clauses = c_parser_omp_clause_uniform (parser, clauses);
14242 c_name = "uniform";
14243 break;
14244 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14245 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14246 c_name = "num_teams";
14247 break;
14248 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14249 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14250 c_name = "thread_limit";
14251 break;
14252 case PRAGMA_OMP_CLAUSE_ALIGNED:
14253 clauses = c_parser_omp_clause_aligned (parser, clauses);
14254 c_name = "aligned";
14255 break;
14256 case PRAGMA_OMP_CLAUSE_LINEAR:
14257 clauses = c_parser_omp_clause_linear (parser, clauses);
14258 c_name = "linear";
14259 break;
14260 case PRAGMA_OMP_CLAUSE_DEPEND:
14261 clauses = c_parser_omp_clause_depend (parser, clauses);
14262 c_name = "depend";
14263 break;
14264 case PRAGMA_OMP_CLAUSE_MAP:
14265 clauses = c_parser_omp_clause_map (parser, clauses);
14266 c_name = "map";
14267 break;
14268 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14269 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14270 c_name = "use_device_ptr";
14271 break;
14272 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14273 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14274 c_name = "is_device_ptr";
14275 break;
14276 case PRAGMA_OMP_CLAUSE_DEVICE:
14277 clauses = c_parser_omp_clause_device (parser, clauses);
14278 c_name = "device";
14279 break;
14280 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14281 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14282 c_name = "dist_schedule";
14283 break;
14284 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14285 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14286 c_name = "proc_bind";
14287 break;
14288 case PRAGMA_OMP_CLAUSE_SAFELEN:
14289 clauses = c_parser_omp_clause_safelen (parser, clauses);
14290 c_name = "safelen";
14291 break;
14292 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14293 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14294 c_name = "simdlen";
14295 break;
14296 case PRAGMA_OMP_CLAUSE_NOGROUP:
14297 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14298 c_name = "nogroup";
14299 break;
14300 case PRAGMA_OMP_CLAUSE_THREADS:
14301 clauses
14302 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14303 clauses);
14304 c_name = "threads";
14305 break;
14306 case PRAGMA_OMP_CLAUSE_SIMD:
14307 clauses
14308 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14309 clauses);
14310 c_name = "simd";
14311 break;
14312 default:
14313 c_parser_error (parser, "expected %<#pragma omp%> clause");
14314 goto saw_error;
14317 first = false;
14319 if (((mask >> c_kind) & 1) == 0)
14321 /* Remove the invalid clause(s) from the list to avoid
14322 confusing the rest of the compiler. */
14323 clauses = prev;
14324 error_at (here, "%qs is not valid for %qs", c_name, where);
14328 saw_error:
14329 c_parser_skip_to_pragma_eol (parser);
14331 if (finish_p)
14333 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14334 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14335 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14338 return clauses;
14341 /* OpenACC 2.0, OpenMP 2.5:
14342 structured-block:
14343 statement
14345 In practice, we're also interested in adding the statement to an
14346 outer node. So it is convenient if we work around the fact that
14347 c_parser_statement calls add_stmt. */
14349 static tree
14350 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14352 tree stmt = push_stmt_list ();
14353 c_parser_statement (parser, if_p);
14354 return pop_stmt_list (stmt);
14357 /* OpenACC 2.0:
14358 # pragma acc cache (variable-list) new-line
14360 LOC is the location of the #pragma token.
14363 static tree
14364 c_parser_oacc_cache (location_t loc, c_parser *parser)
14366 tree stmt, clauses;
14368 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14369 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14371 c_parser_skip_to_pragma_eol (parser);
14373 stmt = make_node (OACC_CACHE);
14374 TREE_TYPE (stmt) = void_type_node;
14375 OACC_CACHE_CLAUSES (stmt) = clauses;
14376 SET_EXPR_LOCATION (stmt, loc);
14377 add_stmt (stmt);
14379 return stmt;
14382 /* OpenACC 2.0:
14383 # pragma acc data oacc-data-clause[optseq] new-line
14384 structured-block
14386 LOC is the location of the #pragma token.
14389 #define OACC_DATA_CLAUSE_MASK \
14390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
14398 static tree
14399 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14401 tree stmt, clauses, block;
14403 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14404 "#pragma acc data");
14406 block = c_begin_omp_parallel ();
14407 add_stmt (c_parser_omp_structured_block (parser, if_p));
14409 stmt = c_finish_oacc_data (loc, clauses, block);
14411 return stmt;
14414 /* OpenACC 2.0:
14415 # pragma acc declare oacc-data-clause[optseq] new-line
14418 #define OACC_DECLARE_CLAUSE_MASK \
14419 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
14428 static void
14429 c_parser_oacc_declare (c_parser *parser)
14431 location_t pragma_loc = c_parser_peek_token (parser)->location;
14432 tree clauses, stmt, t, decl;
14434 bool error = false;
14436 c_parser_consume_pragma (parser);
14438 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14439 "#pragma acc declare");
14440 if (!clauses)
14442 error_at (pragma_loc,
14443 "no valid clauses specified in %<#pragma acc declare%>");
14444 return;
14447 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14449 location_t loc = OMP_CLAUSE_LOCATION (t);
14450 decl = OMP_CLAUSE_DECL (t);
14451 if (!DECL_P (decl))
14453 error_at (loc, "array section in %<#pragma acc declare%>");
14454 error = true;
14455 continue;
14458 switch (OMP_CLAUSE_MAP_KIND (t))
14460 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14461 case GOMP_MAP_ALLOC:
14462 case GOMP_MAP_TO:
14463 case GOMP_MAP_FORCE_DEVICEPTR:
14464 case GOMP_MAP_DEVICE_RESIDENT:
14465 break;
14467 case GOMP_MAP_LINK:
14468 if (!global_bindings_p ()
14469 && (TREE_STATIC (decl)
14470 || !DECL_EXTERNAL (decl)))
14472 error_at (loc,
14473 "%qD must be a global variable in "
14474 "%<#pragma acc declare link%>",
14475 decl);
14476 error = true;
14477 continue;
14479 break;
14481 default:
14482 if (global_bindings_p ())
14484 error_at (loc, "invalid OpenACC clause at file scope");
14485 error = true;
14486 continue;
14488 if (DECL_EXTERNAL (decl))
14490 error_at (loc,
14491 "invalid use of %<extern%> variable %qD "
14492 "in %<#pragma acc declare%>", decl);
14493 error = true;
14494 continue;
14496 else if (TREE_PUBLIC (decl))
14498 error_at (loc,
14499 "invalid use of %<global%> variable %qD "
14500 "in %<#pragma acc declare%>", decl);
14501 error = true;
14502 continue;
14504 break;
14507 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14508 || lookup_attribute ("omp declare target link",
14509 DECL_ATTRIBUTES (decl)))
14511 error_at (loc, "variable %qD used more than once with "
14512 "%<#pragma acc declare%>", decl);
14513 error = true;
14514 continue;
14517 if (!error)
14519 tree id;
14521 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14522 id = get_identifier ("omp declare target link");
14523 else
14524 id = get_identifier ("omp declare target");
14526 DECL_ATTRIBUTES (decl)
14527 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14529 if (global_bindings_p ())
14531 symtab_node *node = symtab_node::get (decl);
14532 if (node != NULL)
14534 node->offloadable = 1;
14535 if (ENABLE_OFFLOADING)
14537 g->have_offload = true;
14538 if (is_a <varpool_node *> (node))
14539 vec_safe_push (offload_vars, decl);
14546 if (error || global_bindings_p ())
14547 return;
14549 stmt = make_node (OACC_DECLARE);
14550 TREE_TYPE (stmt) = void_type_node;
14551 OACC_DECLARE_CLAUSES (stmt) = clauses;
14552 SET_EXPR_LOCATION (stmt, pragma_loc);
14554 add_stmt (stmt);
14556 return;
14559 /* OpenACC 2.0:
14560 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14564 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14567 LOC is the location of the #pragma token.
14570 #define OACC_ENTER_DATA_CLAUSE_MASK \
14571 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14577 #define OACC_EXIT_DATA_CLAUSE_MASK \
14578 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
14583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14585 static void
14586 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14588 location_t loc = c_parser_peek_token (parser)->location;
14589 tree clauses, stmt;
14590 const char *p = "";
14592 c_parser_consume_pragma (parser);
14594 if (c_parser_next_token_is (parser, CPP_NAME))
14596 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14597 c_parser_consume_token (parser);
14600 if (strcmp (p, "data") != 0)
14602 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14603 enter ? "enter" : "exit");
14604 parser->error = true;
14605 c_parser_skip_to_pragma_eol (parser);
14606 return;
14609 if (enter)
14610 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14611 "#pragma acc enter data");
14612 else
14613 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14614 "#pragma acc exit data");
14616 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14618 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14619 enter ? "enter" : "exit");
14620 return;
14623 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14624 TREE_TYPE (stmt) = void_type_node;
14625 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14626 SET_EXPR_LOCATION (stmt, loc);
14627 add_stmt (stmt);
14631 /* OpenACC 2.0:
14632 # pragma acc host_data oacc-data-clause[optseq] new-line
14633 structured-block
14636 #define OACC_HOST_DATA_CLAUSE_MASK \
14637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14639 static tree
14640 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14642 tree stmt, clauses, block;
14644 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14645 "#pragma acc host_data");
14647 block = c_begin_omp_parallel ();
14648 add_stmt (c_parser_omp_structured_block (parser, if_p));
14649 stmt = c_finish_oacc_host_data (loc, clauses, block);
14650 return stmt;
14654 /* OpenACC 2.0:
14656 # pragma acc loop oacc-loop-clause[optseq] new-line
14657 structured-block
14659 LOC is the location of the #pragma token.
14662 #define OACC_LOOP_CLAUSE_MASK \
14663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14673 static tree
14674 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14675 omp_clause_mask mask, tree *cclauses, bool *if_p)
14677 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14679 strcat (p_name, " loop");
14680 mask |= OACC_LOOP_CLAUSE_MASK;
14682 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14683 cclauses == NULL);
14684 if (cclauses)
14686 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14687 if (*cclauses)
14688 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14689 if (clauses)
14690 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14693 tree block = c_begin_compound_stmt (true);
14694 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14695 if_p);
14696 block = c_end_compound_stmt (loc, block, true);
14697 add_stmt (block);
14699 return stmt;
14702 /* OpenACC 2.0:
14703 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14704 structured-block
14708 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14709 structured-block
14711 LOC is the location of the #pragma token.
14714 #define OACC_KERNELS_CLAUSE_MASK \
14715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14729 #define OACC_PARALLEL_CLAUSE_MASK \
14730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14747 static tree
14748 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14749 enum pragma_kind p_kind, char *p_name,
14750 bool *if_p)
14752 omp_clause_mask mask;
14753 enum tree_code code;
14754 switch (p_kind)
14756 case PRAGMA_OACC_KERNELS:
14757 strcat (p_name, " kernels");
14758 mask = OACC_KERNELS_CLAUSE_MASK;
14759 code = OACC_KERNELS;
14760 break;
14761 case PRAGMA_OACC_PARALLEL:
14762 strcat (p_name, " parallel");
14763 mask = OACC_PARALLEL_CLAUSE_MASK;
14764 code = OACC_PARALLEL;
14765 break;
14766 default:
14767 gcc_unreachable ();
14770 if (c_parser_next_token_is (parser, CPP_NAME))
14772 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14773 if (strcmp (p, "loop") == 0)
14775 c_parser_consume_token (parser);
14776 tree block = c_begin_omp_parallel ();
14777 tree clauses;
14778 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14779 return c_finish_omp_construct (loc, code, block, clauses);
14783 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14785 tree block = c_begin_omp_parallel ();
14786 add_stmt (c_parser_omp_structured_block (parser, if_p));
14788 return c_finish_omp_construct (loc, code, block, clauses);
14791 /* OpenACC 2.0:
14792 # pragma acc routine oacc-routine-clause[optseq] new-line
14793 function-definition
14795 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14798 #define OACC_ROUTINE_CLAUSE_MASK \
14799 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14804 /* Parse an OpenACC routine directive. For named directives, we apply
14805 immediately to the named function. For unnamed ones we then parse
14806 a declaration or definition, which must be for a function. */
14808 static void
14809 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14811 gcc_checking_assert (context == pragma_external);
14813 oacc_routine_data data;
14814 data.error_seen = false;
14815 data.fndecl_seen = false;
14816 data.clauses = NULL_TREE;
14817 data.loc = c_parser_peek_token (parser)->location;
14819 c_parser_consume_pragma (parser);
14821 /* Look for optional '( name )'. */
14822 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14824 c_parser_consume_token (parser); /* '(' */
14826 tree decl = NULL_TREE;
14827 c_token *name_token = c_parser_peek_token (parser);
14828 location_t name_loc = name_token->location;
14829 if (name_token->type == CPP_NAME
14830 && (name_token->id_kind == C_ID_ID
14831 || name_token->id_kind == C_ID_TYPENAME))
14833 decl = lookup_name (name_token->value);
14834 if (!decl)
14835 error_at (name_loc,
14836 "%qE has not been declared", name_token->value);
14837 c_parser_consume_token (parser);
14839 else
14840 c_parser_error (parser, "expected function name");
14842 if (!decl
14843 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14845 c_parser_skip_to_pragma_eol (parser, false);
14846 return;
14849 data.clauses
14850 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14851 "#pragma acc routine");
14853 if (TREE_CODE (decl) != FUNCTION_DECL)
14855 error_at (name_loc, "%qD does not refer to a function", decl);
14856 return;
14859 c_finish_oacc_routine (&data, decl, false);
14861 else /* No optional '( name )'. */
14863 data.clauses
14864 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14865 "#pragma acc routine");
14867 /* Emit a helpful diagnostic if there's another pragma following this
14868 one. Also don't allow a static assertion declaration, as in the
14869 following we'll just parse a *single* "declaration or function
14870 definition", and the static assertion counts an one. */
14871 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14872 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14874 error_at (data.loc,
14875 "%<#pragma acc routine%> not immediately followed by"
14876 " function declaration or definition");
14877 /* ..., and then just keep going. */
14878 return;
14881 /* We only have to consider the pragma_external case here. */
14882 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14883 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14885 int ext = disable_extension_diagnostics ();
14887 c_parser_consume_token (parser);
14888 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14889 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14890 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14891 NULL, vNULL, &data);
14892 restore_extension_diagnostics (ext);
14894 else
14895 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14896 NULL, vNULL, &data);
14900 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14901 IS_DEFN is true if we're applying it to the definition. */
14903 static void
14904 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14905 bool is_defn)
14907 /* Keep going if we're in error reporting mode. */
14908 if (data->error_seen
14909 || fndecl == error_mark_node)
14910 return;
14912 if (data->fndecl_seen)
14914 error_at (data->loc,
14915 "%<#pragma acc routine%> not immediately followed by"
14916 " a single function declaration or definition");
14917 data->error_seen = true;
14918 return;
14920 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14922 error_at (data->loc,
14923 "%<#pragma acc routine%> not immediately followed by"
14924 " function declaration or definition");
14925 data->error_seen = true;
14926 return;
14929 if (oacc_get_fn_attrib (fndecl))
14931 error_at (data->loc,
14932 "%<#pragma acc routine%> already applied to %qD", fndecl);
14933 data->error_seen = true;
14934 return;
14937 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14939 error_at (data->loc,
14940 TREE_USED (fndecl)
14941 ? G_("%<#pragma acc routine%> must be applied before use")
14942 : G_("%<#pragma acc routine%> must be applied before "
14943 "definition"));
14944 data->error_seen = true;
14945 return;
14948 /* Process the routine's dimension clauses. */
14949 tree dims = oacc_build_routine_dims (data->clauses);
14950 oacc_replace_fn_attrib (fndecl, dims);
14952 /* Add an "omp declare target" attribute. */
14953 DECL_ATTRIBUTES (fndecl)
14954 = tree_cons (get_identifier ("omp declare target"),
14955 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14957 /* Remember that we've used this "#pragma acc routine". */
14958 data->fndecl_seen = true;
14961 /* OpenACC 2.0:
14962 # pragma acc update oacc-update-clause[optseq] new-line
14965 #define OACC_UPDATE_CLAUSE_MASK \
14966 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
14971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14973 static void
14974 c_parser_oacc_update (c_parser *parser)
14976 location_t loc = c_parser_peek_token (parser)->location;
14978 c_parser_consume_pragma (parser);
14980 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14981 "#pragma acc update");
14982 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14984 error_at (loc,
14985 "%<#pragma acc update%> must contain at least one "
14986 "%<device%> or %<host%> or %<self%> clause");
14987 return;
14990 if (parser->error)
14991 return;
14993 tree stmt = make_node (OACC_UPDATE);
14994 TREE_TYPE (stmt) = void_type_node;
14995 OACC_UPDATE_CLAUSES (stmt) = clauses;
14996 SET_EXPR_LOCATION (stmt, loc);
14997 add_stmt (stmt);
15000 /* OpenACC 2.0:
15001 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15003 LOC is the location of the #pragma token.
15006 #define OACC_WAIT_CLAUSE_MASK \
15007 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15009 static tree
15010 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15012 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15014 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15015 list = c_parser_oacc_wait_list (parser, loc, list);
15017 strcpy (p_name, " wait");
15018 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15019 stmt = c_finish_oacc_wait (loc, list, clauses);
15020 add_stmt (stmt);
15022 return stmt;
15025 /* OpenMP 2.5:
15026 # pragma omp atomic new-line
15027 expression-stmt
15029 expression-stmt:
15030 x binop= expr | x++ | ++x | x-- | --x
15031 binop:
15032 +, *, -, /, &, ^, |, <<, >>
15034 where x is an lvalue expression with scalar type.
15036 OpenMP 3.1:
15037 # pragma omp atomic new-line
15038 update-stmt
15040 # pragma omp atomic read new-line
15041 read-stmt
15043 # pragma omp atomic write new-line
15044 write-stmt
15046 # pragma omp atomic update new-line
15047 update-stmt
15049 # pragma omp atomic capture new-line
15050 capture-stmt
15052 # pragma omp atomic capture new-line
15053 capture-block
15055 read-stmt:
15056 v = x
15057 write-stmt:
15058 x = expr
15059 update-stmt:
15060 expression-stmt | x = x binop expr
15061 capture-stmt:
15062 v = expression-stmt
15063 capture-block:
15064 { v = x; update-stmt; } | { update-stmt; v = x; }
15066 OpenMP 4.0:
15067 update-stmt:
15068 expression-stmt | x = x binop expr | x = expr binop x
15069 capture-stmt:
15070 v = update-stmt
15071 capture-block:
15072 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15074 where x and v are lvalue expressions with scalar type.
15076 LOC is the location of the #pragma token. */
15078 static void
15079 c_parser_omp_atomic (location_t loc, c_parser *parser)
15081 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15082 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15083 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15084 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15085 struct c_expr expr;
15086 location_t eloc;
15087 bool structured_block = false;
15088 bool swapped = false;
15089 bool seq_cst = false;
15090 bool non_lvalue_p;
15092 if (c_parser_next_token_is (parser, CPP_NAME))
15094 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15095 if (!strcmp (p, "seq_cst"))
15097 seq_cst = true;
15098 c_parser_consume_token (parser);
15099 if (c_parser_next_token_is (parser, CPP_COMMA)
15100 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15101 c_parser_consume_token (parser);
15104 if (c_parser_next_token_is (parser, CPP_NAME))
15106 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15108 if (!strcmp (p, "read"))
15109 code = OMP_ATOMIC_READ;
15110 else if (!strcmp (p, "write"))
15111 code = NOP_EXPR;
15112 else if (!strcmp (p, "update"))
15113 code = OMP_ATOMIC;
15114 else if (!strcmp (p, "capture"))
15115 code = OMP_ATOMIC_CAPTURE_NEW;
15116 else
15117 p = NULL;
15118 if (p)
15119 c_parser_consume_token (parser);
15121 if (!seq_cst)
15123 if (c_parser_next_token_is (parser, CPP_COMMA)
15124 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15125 c_parser_consume_token (parser);
15127 if (c_parser_next_token_is (parser, CPP_NAME))
15129 const char *p
15130 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15131 if (!strcmp (p, "seq_cst"))
15133 seq_cst = true;
15134 c_parser_consume_token (parser);
15138 c_parser_skip_to_pragma_eol (parser);
15140 switch (code)
15142 case OMP_ATOMIC_READ:
15143 case NOP_EXPR: /* atomic write */
15144 v = c_parser_cast_expression (parser, NULL).value;
15145 non_lvalue_p = !lvalue_p (v);
15146 v = c_fully_fold (v, false, NULL, true);
15147 if (v == error_mark_node)
15148 goto saw_error;
15149 if (non_lvalue_p)
15150 v = non_lvalue (v);
15151 loc = c_parser_peek_token (parser)->location;
15152 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15153 goto saw_error;
15154 if (code == NOP_EXPR)
15156 lhs = c_parser_expression (parser).value;
15157 lhs = c_fully_fold (lhs, false, NULL);
15158 if (lhs == error_mark_node)
15159 goto saw_error;
15161 else
15163 lhs = c_parser_cast_expression (parser, NULL).value;
15164 non_lvalue_p = !lvalue_p (lhs);
15165 lhs = c_fully_fold (lhs, false, NULL, true);
15166 if (lhs == error_mark_node)
15167 goto saw_error;
15168 if (non_lvalue_p)
15169 lhs = non_lvalue (lhs);
15171 if (code == NOP_EXPR)
15173 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15174 opcode. */
15175 code = OMP_ATOMIC;
15176 rhs = lhs;
15177 lhs = v;
15178 v = NULL_TREE;
15180 goto done;
15181 case OMP_ATOMIC_CAPTURE_NEW:
15182 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15184 c_parser_consume_token (parser);
15185 structured_block = true;
15187 else
15189 v = c_parser_cast_expression (parser, NULL).value;
15190 non_lvalue_p = !lvalue_p (v);
15191 v = c_fully_fold (v, false, NULL, true);
15192 if (v == error_mark_node)
15193 goto saw_error;
15194 if (non_lvalue_p)
15195 v = non_lvalue (v);
15196 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15197 goto saw_error;
15199 break;
15200 default:
15201 break;
15204 /* For structured_block case we don't know yet whether
15205 old or new x should be captured. */
15206 restart:
15207 eloc = c_parser_peek_token (parser)->location;
15208 expr = c_parser_cast_expression (parser, NULL);
15209 lhs = expr.value;
15210 expr = default_function_array_conversion (eloc, expr);
15211 unfolded_lhs = expr.value;
15212 lhs = c_fully_fold (lhs, false, NULL, true);
15213 orig_lhs = lhs;
15214 switch (TREE_CODE (lhs))
15216 case ERROR_MARK:
15217 saw_error:
15218 c_parser_skip_to_end_of_block_or_statement (parser);
15219 if (structured_block)
15221 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15222 c_parser_consume_token (parser);
15223 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15225 c_parser_skip_to_end_of_block_or_statement (parser);
15226 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15227 c_parser_consume_token (parser);
15230 return;
15232 case POSTINCREMENT_EXPR:
15233 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15234 code = OMP_ATOMIC_CAPTURE_OLD;
15235 /* FALLTHROUGH */
15236 case PREINCREMENT_EXPR:
15237 lhs = TREE_OPERAND (lhs, 0);
15238 unfolded_lhs = NULL_TREE;
15239 opcode = PLUS_EXPR;
15240 rhs = integer_one_node;
15241 break;
15243 case POSTDECREMENT_EXPR:
15244 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15245 code = OMP_ATOMIC_CAPTURE_OLD;
15246 /* FALLTHROUGH */
15247 case PREDECREMENT_EXPR:
15248 lhs = TREE_OPERAND (lhs, 0);
15249 unfolded_lhs = NULL_TREE;
15250 opcode = MINUS_EXPR;
15251 rhs = integer_one_node;
15252 break;
15254 case COMPOUND_EXPR:
15255 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15256 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15257 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15258 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15259 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15260 (TREE_OPERAND (lhs, 1), 0), 0)))
15261 == BOOLEAN_TYPE)
15262 /* Undo effects of boolean_increment for post {in,de}crement. */
15263 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15264 /* FALLTHRU */
15265 case MODIFY_EXPR:
15266 if (TREE_CODE (lhs) == MODIFY_EXPR
15267 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15269 /* Undo effects of boolean_increment. */
15270 if (integer_onep (TREE_OPERAND (lhs, 1)))
15272 /* This is pre or post increment. */
15273 rhs = TREE_OPERAND (lhs, 1);
15274 lhs = TREE_OPERAND (lhs, 0);
15275 unfolded_lhs = NULL_TREE;
15276 opcode = NOP_EXPR;
15277 if (code == OMP_ATOMIC_CAPTURE_NEW
15278 && !structured_block
15279 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15280 code = OMP_ATOMIC_CAPTURE_OLD;
15281 break;
15283 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15284 && TREE_OPERAND (lhs, 0)
15285 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15287 /* This is pre or post decrement. */
15288 rhs = TREE_OPERAND (lhs, 1);
15289 lhs = TREE_OPERAND (lhs, 0);
15290 unfolded_lhs = NULL_TREE;
15291 opcode = NOP_EXPR;
15292 if (code == OMP_ATOMIC_CAPTURE_NEW
15293 && !structured_block
15294 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15295 code = OMP_ATOMIC_CAPTURE_OLD;
15296 break;
15299 /* FALLTHRU */
15300 default:
15301 if (!lvalue_p (unfolded_lhs))
15302 lhs = non_lvalue (lhs);
15303 switch (c_parser_peek_token (parser)->type)
15305 case CPP_MULT_EQ:
15306 opcode = MULT_EXPR;
15307 break;
15308 case CPP_DIV_EQ:
15309 opcode = TRUNC_DIV_EXPR;
15310 break;
15311 case CPP_PLUS_EQ:
15312 opcode = PLUS_EXPR;
15313 break;
15314 case CPP_MINUS_EQ:
15315 opcode = MINUS_EXPR;
15316 break;
15317 case CPP_LSHIFT_EQ:
15318 opcode = LSHIFT_EXPR;
15319 break;
15320 case CPP_RSHIFT_EQ:
15321 opcode = RSHIFT_EXPR;
15322 break;
15323 case CPP_AND_EQ:
15324 opcode = BIT_AND_EXPR;
15325 break;
15326 case CPP_OR_EQ:
15327 opcode = BIT_IOR_EXPR;
15328 break;
15329 case CPP_XOR_EQ:
15330 opcode = BIT_XOR_EXPR;
15331 break;
15332 case CPP_EQ:
15333 c_parser_consume_token (parser);
15334 eloc = c_parser_peek_token (parser)->location;
15335 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15336 rhs1 = expr.value;
15337 switch (TREE_CODE (rhs1))
15339 case MULT_EXPR:
15340 case TRUNC_DIV_EXPR:
15341 case RDIV_EXPR:
15342 case PLUS_EXPR:
15343 case MINUS_EXPR:
15344 case LSHIFT_EXPR:
15345 case RSHIFT_EXPR:
15346 case BIT_AND_EXPR:
15347 case BIT_IOR_EXPR:
15348 case BIT_XOR_EXPR:
15349 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15351 opcode = TREE_CODE (rhs1);
15352 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15353 true);
15354 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15355 true);
15356 goto stmt_done;
15358 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15360 opcode = TREE_CODE (rhs1);
15361 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15362 true);
15363 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15364 true);
15365 swapped = !commutative_tree_code (opcode);
15366 goto stmt_done;
15368 break;
15369 case ERROR_MARK:
15370 goto saw_error;
15371 default:
15372 break;
15374 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15376 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15378 code = OMP_ATOMIC_CAPTURE_OLD;
15379 v = lhs;
15380 lhs = NULL_TREE;
15381 expr = default_function_array_read_conversion (eloc, expr);
15382 unfolded_lhs1 = expr.value;
15383 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15384 rhs1 = NULL_TREE;
15385 c_parser_consume_token (parser);
15386 goto restart;
15388 if (structured_block)
15390 opcode = NOP_EXPR;
15391 expr = default_function_array_read_conversion (eloc, expr);
15392 rhs = c_fully_fold (expr.value, false, NULL, true);
15393 rhs1 = NULL_TREE;
15394 goto stmt_done;
15397 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15398 goto saw_error;
15399 default:
15400 c_parser_error (parser,
15401 "invalid operator for %<#pragma omp atomic%>");
15402 goto saw_error;
15405 /* Arrange to pass the location of the assignment operator to
15406 c_finish_omp_atomic. */
15407 loc = c_parser_peek_token (parser)->location;
15408 c_parser_consume_token (parser);
15409 eloc = c_parser_peek_token (parser)->location;
15410 expr = c_parser_expression (parser);
15411 expr = default_function_array_read_conversion (eloc, expr);
15412 rhs = expr.value;
15413 rhs = c_fully_fold (rhs, false, NULL, true);
15414 break;
15416 stmt_done:
15417 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15419 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15420 goto saw_error;
15421 v = c_parser_cast_expression (parser, NULL).value;
15422 non_lvalue_p = !lvalue_p (v);
15423 v = c_fully_fold (v, false, NULL, true);
15424 if (v == error_mark_node)
15425 goto saw_error;
15426 if (non_lvalue_p)
15427 v = non_lvalue (v);
15428 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15429 goto saw_error;
15430 eloc = c_parser_peek_token (parser)->location;
15431 expr = c_parser_cast_expression (parser, NULL);
15432 lhs1 = expr.value;
15433 expr = default_function_array_read_conversion (eloc, expr);
15434 unfolded_lhs1 = expr.value;
15435 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15436 if (lhs1 == error_mark_node)
15437 goto saw_error;
15438 if (!lvalue_p (unfolded_lhs1))
15439 lhs1 = non_lvalue (lhs1);
15441 if (structured_block)
15443 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15444 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15446 done:
15447 if (unfolded_lhs && unfolded_lhs1
15448 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15450 error ("%<#pragma omp atomic capture%> uses two different "
15451 "expressions for memory");
15452 stmt = error_mark_node;
15454 else
15455 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15456 swapped, seq_cst);
15457 if (stmt != error_mark_node)
15458 add_stmt (stmt);
15460 if (!structured_block)
15461 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15465 /* OpenMP 2.5:
15466 # pragma omp barrier new-line
15469 static void
15470 c_parser_omp_barrier (c_parser *parser)
15472 location_t loc = c_parser_peek_token (parser)->location;
15473 c_parser_consume_pragma (parser);
15474 c_parser_skip_to_pragma_eol (parser);
15476 c_finish_omp_barrier (loc);
15479 /* OpenMP 2.5:
15480 # pragma omp critical [(name)] new-line
15481 structured-block
15483 OpenMP 4.5:
15484 # pragma omp critical [(name) [hint(expression)]] new-line
15486 LOC is the location of the #pragma itself. */
15488 #define OMP_CRITICAL_CLAUSE_MASK \
15489 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15491 static tree
15492 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15494 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15496 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15498 c_parser_consume_token (parser);
15499 if (c_parser_next_token_is (parser, CPP_NAME))
15501 name = c_parser_peek_token (parser)->value;
15502 c_parser_consume_token (parser);
15503 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15505 else
15506 c_parser_error (parser, "expected identifier");
15508 clauses = c_parser_omp_all_clauses (parser,
15509 OMP_CRITICAL_CLAUSE_MASK,
15510 "#pragma omp critical");
15512 else
15514 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15515 c_parser_error (parser, "expected %<(%> or end of line");
15516 c_parser_skip_to_pragma_eol (parser);
15519 stmt = c_parser_omp_structured_block (parser, if_p);
15520 return c_finish_omp_critical (loc, stmt, name, clauses);
15523 /* OpenMP 2.5:
15524 # pragma omp flush flush-vars[opt] new-line
15526 flush-vars:
15527 ( variable-list ) */
15529 static void
15530 c_parser_omp_flush (c_parser *parser)
15532 location_t loc = c_parser_peek_token (parser)->location;
15533 c_parser_consume_pragma (parser);
15534 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15535 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15536 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15537 c_parser_error (parser, "expected %<(%> or end of line");
15538 c_parser_skip_to_pragma_eol (parser);
15540 c_finish_omp_flush (loc);
15543 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15544 The real trick here is to determine the loop control variable early
15545 so that we can push a new decl if necessary to make it private.
15546 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15547 respectively. */
15549 static tree
15550 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15551 tree clauses, tree *cclauses, bool *if_p)
15553 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15554 tree declv, condv, incrv, initv, ret = NULL_TREE;
15555 tree pre_body = NULL_TREE, this_pre_body;
15556 tree ordered_cl = NULL_TREE;
15557 bool fail = false, open_brace_parsed = false;
15558 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15559 location_t for_loc;
15560 bool tiling = false;
15561 vec<tree, va_gc> *for_block = make_tree_vector ();
15563 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15564 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15565 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15566 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15568 tiling = true;
15569 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15571 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15572 && OMP_CLAUSE_ORDERED_EXPR (cl))
15574 ordered_cl = cl;
15575 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15578 if (ordered && ordered < collapse)
15580 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15581 "%<ordered%> clause parameter is less than %<collapse%>");
15582 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15583 = build_int_cst (NULL_TREE, collapse);
15584 ordered = collapse;
15586 if (ordered)
15588 for (tree *pc = &clauses; *pc; )
15589 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15591 error_at (OMP_CLAUSE_LOCATION (*pc),
15592 "%<linear%> clause may not be specified together "
15593 "with %<ordered%> clause with a parameter");
15594 *pc = OMP_CLAUSE_CHAIN (*pc);
15596 else
15597 pc = &OMP_CLAUSE_CHAIN (*pc);
15600 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15601 count = ordered ? ordered : collapse;
15603 declv = make_tree_vec (count);
15604 initv = make_tree_vec (count);
15605 condv = make_tree_vec (count);
15606 incrv = make_tree_vec (count);
15608 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15610 c_parser_error (parser, "for statement expected");
15611 return NULL;
15613 for_loc = c_parser_peek_token (parser)->location;
15614 c_parser_consume_token (parser);
15616 for (i = 0; i < count; i++)
15618 int bracecount = 0;
15620 matching_parens parens;
15621 if (!parens.require_open (parser))
15622 goto pop_scopes;
15624 /* Parse the initialization declaration or expression. */
15625 if (c_parser_next_tokens_start_declaration (parser))
15627 if (i > 0)
15628 vec_safe_push (for_block, c_begin_compound_stmt (true));
15629 this_pre_body = push_stmt_list ();
15630 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15631 NULL, vNULL);
15632 if (this_pre_body)
15634 this_pre_body = pop_stmt_list (this_pre_body);
15635 if (pre_body)
15637 tree t = pre_body;
15638 pre_body = push_stmt_list ();
15639 add_stmt (t);
15640 add_stmt (this_pre_body);
15641 pre_body = pop_stmt_list (pre_body);
15643 else
15644 pre_body = this_pre_body;
15646 decl = check_for_loop_decls (for_loc, flag_isoc99);
15647 if (decl == NULL)
15648 goto error_init;
15649 if (DECL_INITIAL (decl) == error_mark_node)
15650 decl = error_mark_node;
15651 init = decl;
15653 else if (c_parser_next_token_is (parser, CPP_NAME)
15654 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15656 struct c_expr decl_exp;
15657 struct c_expr init_exp;
15658 location_t init_loc;
15660 decl_exp = c_parser_postfix_expression (parser);
15661 decl = decl_exp.value;
15663 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15665 init_loc = c_parser_peek_token (parser)->location;
15666 init_exp = c_parser_expr_no_commas (parser, NULL);
15667 init_exp = default_function_array_read_conversion (init_loc,
15668 init_exp);
15669 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15670 NOP_EXPR, init_loc, init_exp.value,
15671 init_exp.original_type);
15672 init = c_process_expr_stmt (init_loc, init);
15674 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15676 else
15678 error_init:
15679 c_parser_error (parser,
15680 "expected iteration declaration or initialization");
15681 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15682 "expected %<)%>");
15683 fail = true;
15684 goto parse_next;
15687 /* Parse the loop condition. */
15688 cond = NULL_TREE;
15689 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15691 location_t cond_loc = c_parser_peek_token (parser)->location;
15692 struct c_expr cond_expr
15693 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15695 cond = cond_expr.value;
15696 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15697 if (COMPARISON_CLASS_P (cond))
15699 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15700 op0 = c_fully_fold (op0, false, NULL);
15701 op1 = c_fully_fold (op1, false, NULL);
15702 TREE_OPERAND (cond, 0) = op0;
15703 TREE_OPERAND (cond, 1) = op1;
15705 switch (cond_expr.original_code)
15707 case GT_EXPR:
15708 case GE_EXPR:
15709 case LT_EXPR:
15710 case LE_EXPR:
15711 break;
15712 default:
15713 /* Can't be cond = error_mark_node, because we want to preserve
15714 the location until c_finish_omp_for. */
15715 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15716 break;
15718 protected_set_expr_location (cond, cond_loc);
15720 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15722 /* Parse the increment expression. */
15723 incr = NULL_TREE;
15724 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15726 location_t incr_loc = c_parser_peek_token (parser)->location;
15728 incr = c_process_expr_stmt (incr_loc,
15729 c_parser_expression (parser).value);
15731 parens.skip_until_found_close (parser);
15733 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15734 fail = true;
15735 else
15737 TREE_VEC_ELT (declv, i) = decl;
15738 TREE_VEC_ELT (initv, i) = init;
15739 TREE_VEC_ELT (condv, i) = cond;
15740 TREE_VEC_ELT (incrv, i) = incr;
15743 parse_next:
15744 if (i == count - 1)
15745 break;
15747 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15748 in between the collapsed for loops to be still considered perfectly
15749 nested. Hopefully the final version clarifies this.
15750 For now handle (multiple) {'s and empty statements. */
15753 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15755 c_parser_consume_token (parser);
15756 break;
15758 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15760 c_parser_consume_token (parser);
15761 bracecount++;
15763 else if (bracecount
15764 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15765 c_parser_consume_token (parser);
15766 else
15768 c_parser_error (parser, "not enough perfectly nested loops");
15769 if (bracecount)
15771 open_brace_parsed = true;
15772 bracecount--;
15774 fail = true;
15775 count = 0;
15776 break;
15779 while (1);
15781 nbraces += bracecount;
15784 if (nbraces)
15785 if_p = NULL;
15787 save_break = c_break_label;
15788 c_break_label = size_one_node;
15789 save_cont = c_cont_label;
15790 c_cont_label = NULL_TREE;
15791 body = push_stmt_list ();
15793 if (open_brace_parsed)
15795 location_t here = c_parser_peek_token (parser)->location;
15796 stmt = c_begin_compound_stmt (true);
15797 c_parser_compound_statement_nostart (parser);
15798 add_stmt (c_end_compound_stmt (here, stmt, true));
15800 else
15801 add_stmt (c_parser_c99_block_statement (parser, if_p));
15802 if (c_cont_label)
15804 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15805 SET_EXPR_LOCATION (t, loc);
15806 add_stmt (t);
15809 body = pop_stmt_list (body);
15810 c_break_label = save_break;
15811 c_cont_label = save_cont;
15813 while (nbraces)
15815 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15817 c_parser_consume_token (parser);
15818 nbraces--;
15820 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15821 c_parser_consume_token (parser);
15822 else
15824 c_parser_error (parser, "collapsed loops not perfectly nested");
15825 while (nbraces)
15827 location_t here = c_parser_peek_token (parser)->location;
15828 stmt = c_begin_compound_stmt (true);
15829 add_stmt (body);
15830 c_parser_compound_statement_nostart (parser);
15831 body = c_end_compound_stmt (here, stmt, true);
15832 nbraces--;
15834 goto pop_scopes;
15838 /* Only bother calling c_finish_omp_for if we haven't already generated
15839 an error from the initialization parsing. */
15840 if (!fail)
15842 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15843 incrv, body, pre_body);
15845 /* Check for iterators appearing in lb, b or incr expressions. */
15846 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15847 stmt = NULL_TREE;
15849 if (stmt)
15851 add_stmt (stmt);
15853 if (cclauses != NULL
15854 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15856 tree *c;
15857 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15858 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15859 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15860 c = &OMP_CLAUSE_CHAIN (*c);
15861 else
15863 for (i = 0; i < count; i++)
15864 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15865 break;
15866 if (i == count)
15867 c = &OMP_CLAUSE_CHAIN (*c);
15868 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15870 error_at (loc,
15871 "iteration variable %qD should not be firstprivate",
15872 OMP_CLAUSE_DECL (*c));
15873 *c = OMP_CLAUSE_CHAIN (*c);
15875 else
15877 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15878 tree l = *c;
15879 *c = OMP_CLAUSE_CHAIN (*c);
15880 if (code == OMP_SIMD)
15882 OMP_CLAUSE_CHAIN (l)
15883 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15884 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15886 else
15888 OMP_CLAUSE_CHAIN (l) = clauses;
15889 clauses = l;
15894 OMP_FOR_CLAUSES (stmt) = clauses;
15896 ret = stmt;
15898 pop_scopes:
15899 while (!for_block->is_empty ())
15901 /* FIXME diagnostics: LOC below should be the actual location of
15902 this particular for block. We need to build a list of
15903 locations to go along with FOR_BLOCK. */
15904 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15905 add_stmt (stmt);
15907 release_tree_vector (for_block);
15908 return ret;
15911 /* Helper function for OpenMP parsing, split clauses and call
15912 finish_omp_clauses on each of the set of clauses afterwards. */
15914 static void
15915 omp_split_clauses (location_t loc, enum tree_code code,
15916 omp_clause_mask mask, tree clauses, tree *cclauses)
15918 int i;
15919 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15920 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15921 if (cclauses[i])
15922 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15925 /* OpenMP 4.0:
15926 #pragma omp simd simd-clause[optseq] new-line
15927 for-loop
15929 LOC is the location of the #pragma token.
15932 #define OMP_SIMD_CLAUSE_MASK \
15933 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15942 static tree
15943 c_parser_omp_simd (location_t loc, c_parser *parser,
15944 char *p_name, omp_clause_mask mask, tree *cclauses,
15945 bool *if_p)
15947 tree block, clauses, ret;
15949 strcat (p_name, " simd");
15950 mask |= OMP_SIMD_CLAUSE_MASK;
15952 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15953 if (cclauses)
15955 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15956 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15957 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15958 OMP_CLAUSE_ORDERED);
15959 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15961 error_at (OMP_CLAUSE_LOCATION (c),
15962 "%<ordered%> clause with parameter may not be specified "
15963 "on %qs construct", p_name);
15964 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15968 block = c_begin_compound_stmt (true);
15969 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15970 block = c_end_compound_stmt (loc, block, true);
15971 add_stmt (block);
15973 return ret;
15976 /* OpenMP 2.5:
15977 #pragma omp for for-clause[optseq] new-line
15978 for-loop
15980 OpenMP 4.0:
15981 #pragma omp for simd for-simd-clause[optseq] new-line
15982 for-loop
15984 LOC is the location of the #pragma token.
15987 #define OMP_FOR_CLAUSE_MASK \
15988 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15998 static tree
15999 c_parser_omp_for (location_t loc, c_parser *parser,
16000 char *p_name, omp_clause_mask mask, tree *cclauses,
16001 bool *if_p)
16003 tree block, clauses, ret;
16005 strcat (p_name, " for");
16006 mask |= OMP_FOR_CLAUSE_MASK;
16007 /* parallel for{, simd} disallows nowait clause, but for
16008 target {teams distribute ,}parallel for{, simd} it should be accepted. */
16009 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16010 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16011 /* Composite distribute parallel for{, simd} disallows ordered clause. */
16012 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16013 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16015 if (c_parser_next_token_is (parser, CPP_NAME))
16017 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16019 if (strcmp (p, "simd") == 0)
16021 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16022 if (cclauses == NULL)
16023 cclauses = cclauses_buf;
16025 c_parser_consume_token (parser);
16026 if (!flag_openmp) /* flag_openmp_simd */
16027 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16028 if_p);
16029 block = c_begin_compound_stmt (true);
16030 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16031 block = c_end_compound_stmt (loc, block, true);
16032 if (ret == NULL_TREE)
16033 return ret;
16034 ret = make_node (OMP_FOR);
16035 TREE_TYPE (ret) = void_type_node;
16036 OMP_FOR_BODY (ret) = block;
16037 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16038 SET_EXPR_LOCATION (ret, loc);
16039 add_stmt (ret);
16040 return ret;
16043 if (!flag_openmp) /* flag_openmp_simd */
16045 c_parser_skip_to_pragma_eol (parser, false);
16046 return NULL_TREE;
16049 /* Composite distribute parallel for disallows linear clause. */
16050 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16051 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16053 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16054 if (cclauses)
16056 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16057 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16060 block = c_begin_compound_stmt (true);
16061 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16062 block = c_end_compound_stmt (loc, block, true);
16063 add_stmt (block);
16065 return ret;
16068 /* OpenMP 2.5:
16069 # pragma omp master new-line
16070 structured-block
16072 LOC is the location of the #pragma token.
16075 static tree
16076 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16078 c_parser_skip_to_pragma_eol (parser);
16079 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16080 if_p));
16083 /* OpenMP 2.5:
16084 # pragma omp ordered new-line
16085 structured-block
16087 OpenMP 4.5:
16088 # pragma omp ordered ordered-clauses new-line
16089 structured-block
16091 # pragma omp ordered depend-clauses new-line */
16093 #define OMP_ORDERED_CLAUSE_MASK \
16094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16097 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16098 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16100 static bool
16101 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16102 bool *if_p)
16104 location_t loc = c_parser_peek_token (parser)->location;
16105 c_parser_consume_pragma (parser);
16107 if (context != pragma_stmt && context != pragma_compound)
16109 c_parser_error (parser, "expected declaration specifiers");
16110 c_parser_skip_to_pragma_eol (parser, false);
16111 return false;
16114 if (c_parser_next_token_is (parser, CPP_NAME))
16116 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16118 if (!strcmp ("depend", p))
16120 if (!flag_openmp) /* flag_openmp_simd */
16122 c_parser_skip_to_pragma_eol (parser, false);
16123 return false;
16125 if (context == pragma_stmt)
16127 error_at (loc,
16128 "%<#pragma omp ordered%> with %<depend%> clause may "
16129 "only be used in compound statements");
16130 c_parser_skip_to_pragma_eol (parser, false);
16131 return false;
16134 tree clauses
16135 = c_parser_omp_all_clauses (parser,
16136 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16137 "#pragma omp ordered");
16138 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16139 return false;
16143 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16144 "#pragma omp ordered");
16146 if (!flag_openmp /* flag_openmp_simd */
16147 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16148 return false;
16150 c_finish_omp_ordered (loc, clauses,
16151 c_parser_omp_structured_block (parser, if_p));
16152 return true;
16155 /* OpenMP 2.5:
16157 section-scope:
16158 { section-sequence }
16160 section-sequence:
16161 section-directive[opt] structured-block
16162 section-sequence section-directive structured-block
16164 SECTIONS_LOC is the location of the #pragma omp sections. */
16166 static tree
16167 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16169 tree stmt, substmt;
16170 bool error_suppress = false;
16171 location_t loc;
16173 loc = c_parser_peek_token (parser)->location;
16174 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16176 /* Avoid skipping until the end of the block. */
16177 parser->error = false;
16178 return NULL_TREE;
16181 stmt = push_stmt_list ();
16183 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16185 substmt = c_parser_omp_structured_block (parser, NULL);
16186 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16187 SET_EXPR_LOCATION (substmt, loc);
16188 add_stmt (substmt);
16191 while (1)
16193 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16194 break;
16195 if (c_parser_next_token_is (parser, CPP_EOF))
16196 break;
16198 loc = c_parser_peek_token (parser)->location;
16199 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16201 c_parser_consume_pragma (parser);
16202 c_parser_skip_to_pragma_eol (parser);
16203 error_suppress = false;
16205 else if (!error_suppress)
16207 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16208 error_suppress = true;
16211 substmt = c_parser_omp_structured_block (parser, NULL);
16212 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16213 SET_EXPR_LOCATION (substmt, loc);
16214 add_stmt (substmt);
16216 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16217 "expected %<#pragma omp section%> or %<}%>");
16219 substmt = pop_stmt_list (stmt);
16221 stmt = make_node (OMP_SECTIONS);
16222 SET_EXPR_LOCATION (stmt, sections_loc);
16223 TREE_TYPE (stmt) = void_type_node;
16224 OMP_SECTIONS_BODY (stmt) = substmt;
16226 return add_stmt (stmt);
16229 /* OpenMP 2.5:
16230 # pragma omp sections sections-clause[optseq] newline
16231 sections-scope
16233 LOC is the location of the #pragma token.
16236 #define OMP_SECTIONS_CLAUSE_MASK \
16237 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16243 static tree
16244 c_parser_omp_sections (location_t loc, c_parser *parser,
16245 char *p_name, omp_clause_mask mask, tree *cclauses)
16247 tree block, clauses, ret;
16249 strcat (p_name, " sections");
16250 mask |= OMP_SECTIONS_CLAUSE_MASK;
16251 if (cclauses)
16252 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16254 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16255 if (cclauses)
16257 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16258 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16261 block = c_begin_compound_stmt (true);
16262 ret = c_parser_omp_sections_scope (loc, parser);
16263 if (ret)
16264 OMP_SECTIONS_CLAUSES (ret) = clauses;
16265 block = c_end_compound_stmt (loc, block, true);
16266 add_stmt (block);
16268 return ret;
16271 /* OpenMP 2.5:
16272 # pragma omp parallel parallel-clause[optseq] new-line
16273 structured-block
16274 # pragma omp parallel for parallel-for-clause[optseq] new-line
16275 structured-block
16276 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16277 structured-block
16279 OpenMP 4.0:
16280 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16281 structured-block
16283 LOC is the location of the #pragma token.
16286 #define OMP_PARALLEL_CLAUSE_MASK \
16287 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16297 static tree
16298 c_parser_omp_parallel (location_t loc, c_parser *parser,
16299 char *p_name, omp_clause_mask mask, tree *cclauses,
16300 bool *if_p)
16302 tree stmt, clauses, block;
16304 strcat (p_name, " parallel");
16305 mask |= OMP_PARALLEL_CLAUSE_MASK;
16306 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16307 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16308 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16309 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16311 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16313 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16314 if (cclauses == NULL)
16315 cclauses = cclauses_buf;
16317 c_parser_consume_token (parser);
16318 if (!flag_openmp) /* flag_openmp_simd */
16319 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16320 block = c_begin_omp_parallel ();
16321 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16322 stmt
16323 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16324 block);
16325 if (ret == NULL_TREE)
16326 return ret;
16327 OMP_PARALLEL_COMBINED (stmt) = 1;
16328 return stmt;
16330 /* When combined with distribute, parallel has to be followed by for.
16331 #pragma omp target parallel is allowed though. */
16332 else if (cclauses
16333 && (mask & (OMP_CLAUSE_MASK_1
16334 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16336 error_at (loc, "expected %<for%> after %qs", p_name);
16337 c_parser_skip_to_pragma_eol (parser);
16338 return NULL_TREE;
16340 else if (!flag_openmp) /* flag_openmp_simd */
16342 c_parser_skip_to_pragma_eol (parser, false);
16343 return NULL_TREE;
16345 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16347 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16348 if (strcmp (p, "sections") == 0)
16350 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16351 if (cclauses == NULL)
16352 cclauses = cclauses_buf;
16354 c_parser_consume_token (parser);
16355 block = c_begin_omp_parallel ();
16356 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16357 stmt = c_finish_omp_parallel (loc,
16358 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16359 block);
16360 OMP_PARALLEL_COMBINED (stmt) = 1;
16361 return stmt;
16365 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16366 if (cclauses)
16368 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16369 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16372 block = c_begin_omp_parallel ();
16373 c_parser_statement (parser, if_p);
16374 stmt = c_finish_omp_parallel (loc, clauses, block);
16376 return stmt;
16379 /* OpenMP 2.5:
16380 # pragma omp single single-clause[optseq] new-line
16381 structured-block
16383 LOC is the location of the #pragma.
16386 #define OMP_SINGLE_CLAUSE_MASK \
16387 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16392 static tree
16393 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16395 tree stmt = make_node (OMP_SINGLE);
16396 SET_EXPR_LOCATION (stmt, loc);
16397 TREE_TYPE (stmt) = void_type_node;
16399 OMP_SINGLE_CLAUSES (stmt)
16400 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16401 "#pragma omp single");
16402 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16404 return add_stmt (stmt);
16407 /* OpenMP 3.0:
16408 # pragma omp task task-clause[optseq] new-line
16410 LOC is the location of the #pragma.
16413 #define OMP_TASK_CLAUSE_MASK \
16414 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16425 static tree
16426 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16428 tree clauses, block;
16430 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16431 "#pragma omp task");
16433 block = c_begin_omp_task ();
16434 c_parser_statement (parser, if_p);
16435 return c_finish_omp_task (loc, clauses, block);
16438 /* OpenMP 3.0:
16439 # pragma omp taskwait new-line
16442 static void
16443 c_parser_omp_taskwait (c_parser *parser)
16445 location_t loc = c_parser_peek_token (parser)->location;
16446 c_parser_consume_pragma (parser);
16447 c_parser_skip_to_pragma_eol (parser);
16449 c_finish_omp_taskwait (loc);
16452 /* OpenMP 3.1:
16453 # pragma omp taskyield new-line
16456 static void
16457 c_parser_omp_taskyield (c_parser *parser)
16459 location_t loc = c_parser_peek_token (parser)->location;
16460 c_parser_consume_pragma (parser);
16461 c_parser_skip_to_pragma_eol (parser);
16463 c_finish_omp_taskyield (loc);
16466 /* OpenMP 4.0:
16467 # pragma omp taskgroup new-line
16470 static tree
16471 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16473 location_t loc = c_parser_peek_token (parser)->location;
16474 c_parser_skip_to_pragma_eol (parser);
16475 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16476 if_p));
16479 /* OpenMP 4.0:
16480 # pragma omp cancel cancel-clause[optseq] new-line
16482 LOC is the location of the #pragma.
16485 #define OMP_CANCEL_CLAUSE_MASK \
16486 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16492 static void
16493 c_parser_omp_cancel (c_parser *parser)
16495 location_t loc = c_parser_peek_token (parser)->location;
16497 c_parser_consume_pragma (parser);
16498 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16499 "#pragma omp cancel");
16501 c_finish_omp_cancel (loc, clauses);
16504 /* OpenMP 4.0:
16505 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16507 LOC is the location of the #pragma.
16510 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16511 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16516 static void
16517 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16519 location_t loc = c_parser_peek_token (parser)->location;
16520 tree clauses;
16521 bool point_seen = false;
16523 c_parser_consume_pragma (parser);
16524 if (c_parser_next_token_is (parser, CPP_NAME))
16526 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16527 if (strcmp (p, "point") == 0)
16529 c_parser_consume_token (parser);
16530 point_seen = true;
16533 if (!point_seen)
16535 c_parser_error (parser, "expected %<point%>");
16536 c_parser_skip_to_pragma_eol (parser);
16537 return;
16540 if (context != pragma_compound)
16542 if (context == pragma_stmt)
16543 error_at (loc,
16544 "%<#pragma %s%> may only be used in compound statements",
16545 "omp cancellation point");
16546 else
16547 c_parser_error (parser, "expected declaration specifiers");
16548 c_parser_skip_to_pragma_eol (parser, false);
16549 return;
16552 clauses
16553 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16554 "#pragma omp cancellation point");
16556 c_finish_omp_cancellation_point (loc, clauses);
16559 /* OpenMP 4.0:
16560 #pragma omp distribute distribute-clause[optseq] new-line
16561 for-loop */
16563 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16564 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16570 static tree
16571 c_parser_omp_distribute (location_t loc, c_parser *parser,
16572 char *p_name, omp_clause_mask mask, tree *cclauses,
16573 bool *if_p)
16575 tree clauses, block, ret;
16577 strcat (p_name, " distribute");
16578 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16580 if (c_parser_next_token_is (parser, CPP_NAME))
16582 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16583 bool simd = false;
16584 bool parallel = false;
16586 if (strcmp (p, "simd") == 0)
16587 simd = true;
16588 else
16589 parallel = strcmp (p, "parallel") == 0;
16590 if (parallel || simd)
16592 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16593 if (cclauses == NULL)
16594 cclauses = cclauses_buf;
16595 c_parser_consume_token (parser);
16596 if (!flag_openmp) /* flag_openmp_simd */
16598 if (simd)
16599 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16600 if_p);
16601 else
16602 return c_parser_omp_parallel (loc, parser, p_name, mask,
16603 cclauses, if_p);
16605 block = c_begin_compound_stmt (true);
16606 if (simd)
16607 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16608 if_p);
16609 else
16610 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16611 if_p);
16612 block = c_end_compound_stmt (loc, block, true);
16613 if (ret == NULL)
16614 return ret;
16615 ret = make_node (OMP_DISTRIBUTE);
16616 TREE_TYPE (ret) = void_type_node;
16617 OMP_FOR_BODY (ret) = block;
16618 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16619 SET_EXPR_LOCATION (ret, loc);
16620 add_stmt (ret);
16621 return ret;
16624 if (!flag_openmp) /* flag_openmp_simd */
16626 c_parser_skip_to_pragma_eol (parser, false);
16627 return NULL_TREE;
16630 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16631 if (cclauses)
16633 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16634 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16637 block = c_begin_compound_stmt (true);
16638 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16639 if_p);
16640 block = c_end_compound_stmt (loc, block, true);
16641 add_stmt (block);
16643 return ret;
16646 /* OpenMP 4.0:
16647 # pragma omp teams teams-clause[optseq] new-line
16648 structured-block */
16650 #define OMP_TEAMS_CLAUSE_MASK \
16651 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16659 static tree
16660 c_parser_omp_teams (location_t loc, c_parser *parser,
16661 char *p_name, omp_clause_mask mask, tree *cclauses,
16662 bool *if_p)
16664 tree clauses, block, ret;
16666 strcat (p_name, " teams");
16667 mask |= OMP_TEAMS_CLAUSE_MASK;
16669 if (c_parser_next_token_is (parser, CPP_NAME))
16671 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16672 if (strcmp (p, "distribute") == 0)
16674 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16675 if (cclauses == NULL)
16676 cclauses = cclauses_buf;
16678 c_parser_consume_token (parser);
16679 if (!flag_openmp) /* flag_openmp_simd */
16680 return c_parser_omp_distribute (loc, parser, p_name, mask,
16681 cclauses, if_p);
16682 block = c_begin_compound_stmt (true);
16683 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16684 if_p);
16685 block = c_end_compound_stmt (loc, block, true);
16686 if (ret == NULL)
16687 return ret;
16688 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16689 ret = make_node (OMP_TEAMS);
16690 TREE_TYPE (ret) = void_type_node;
16691 OMP_TEAMS_CLAUSES (ret) = clauses;
16692 OMP_TEAMS_BODY (ret) = block;
16693 OMP_TEAMS_COMBINED (ret) = 1;
16694 return add_stmt (ret);
16697 if (!flag_openmp) /* flag_openmp_simd */
16699 c_parser_skip_to_pragma_eol (parser, false);
16700 return NULL_TREE;
16703 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16704 if (cclauses)
16706 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16707 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16710 tree stmt = make_node (OMP_TEAMS);
16711 TREE_TYPE (stmt) = void_type_node;
16712 OMP_TEAMS_CLAUSES (stmt) = clauses;
16713 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16715 return add_stmt (stmt);
16718 /* OpenMP 4.0:
16719 # pragma omp target data target-data-clause[optseq] new-line
16720 structured-block */
16722 #define OMP_TARGET_DATA_CLAUSE_MASK \
16723 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16728 static tree
16729 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16731 tree clauses
16732 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16733 "#pragma omp target data");
16734 int map_seen = 0;
16735 for (tree *pc = &clauses; *pc;)
16737 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16738 switch (OMP_CLAUSE_MAP_KIND (*pc))
16740 case GOMP_MAP_TO:
16741 case GOMP_MAP_ALWAYS_TO:
16742 case GOMP_MAP_FROM:
16743 case GOMP_MAP_ALWAYS_FROM:
16744 case GOMP_MAP_TOFROM:
16745 case GOMP_MAP_ALWAYS_TOFROM:
16746 case GOMP_MAP_ALLOC:
16747 map_seen = 3;
16748 break;
16749 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16750 case GOMP_MAP_ALWAYS_POINTER:
16751 break;
16752 default:
16753 map_seen |= 1;
16754 error_at (OMP_CLAUSE_LOCATION (*pc),
16755 "%<#pragma omp target data%> with map-type other "
16756 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16757 "on %<map%> clause");
16758 *pc = OMP_CLAUSE_CHAIN (*pc);
16759 continue;
16761 pc = &OMP_CLAUSE_CHAIN (*pc);
16764 if (map_seen != 3)
16766 if (map_seen == 0)
16767 error_at (loc,
16768 "%<#pragma omp target data%> must contain at least "
16769 "one %<map%> clause");
16770 return NULL_TREE;
16773 tree stmt = make_node (OMP_TARGET_DATA);
16774 TREE_TYPE (stmt) = void_type_node;
16775 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16776 keep_next_level ();
16777 tree block = c_begin_compound_stmt (true);
16778 add_stmt (c_parser_omp_structured_block (parser, if_p));
16779 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16781 SET_EXPR_LOCATION (stmt, loc);
16782 return add_stmt (stmt);
16785 /* OpenMP 4.0:
16786 # pragma omp target update target-update-clause[optseq] new-line */
16788 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16789 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16796 static bool
16797 c_parser_omp_target_update (location_t loc, c_parser *parser,
16798 enum pragma_context context)
16800 if (context == pragma_stmt)
16802 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16803 "omp target update");
16804 c_parser_skip_to_pragma_eol (parser, false);
16805 return false;
16808 tree clauses
16809 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16810 "#pragma omp target update");
16811 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16812 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16814 error_at (loc,
16815 "%<#pragma omp target update%> must contain at least one "
16816 "%<from%> or %<to%> clauses");
16817 return false;
16820 tree stmt = make_node (OMP_TARGET_UPDATE);
16821 TREE_TYPE (stmt) = void_type_node;
16822 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16823 SET_EXPR_LOCATION (stmt, loc);
16824 add_stmt (stmt);
16825 return false;
16828 /* OpenMP 4.5:
16829 # pragma omp target enter data target-data-clause[optseq] new-line */
16831 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16838 static tree
16839 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16840 enum pragma_context context)
16842 bool data_seen = false;
16843 if (c_parser_next_token_is (parser, CPP_NAME))
16845 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16846 if (strcmp (p, "data") == 0)
16848 c_parser_consume_token (parser);
16849 data_seen = true;
16852 if (!data_seen)
16854 c_parser_error (parser, "expected %<data%>");
16855 c_parser_skip_to_pragma_eol (parser);
16856 return NULL_TREE;
16859 if (context == pragma_stmt)
16861 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16862 "omp target enter data");
16863 c_parser_skip_to_pragma_eol (parser, false);
16864 return NULL_TREE;
16867 tree clauses
16868 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16869 "#pragma omp target enter data");
16870 int map_seen = 0;
16871 for (tree *pc = &clauses; *pc;)
16873 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16874 switch (OMP_CLAUSE_MAP_KIND (*pc))
16876 case GOMP_MAP_TO:
16877 case GOMP_MAP_ALWAYS_TO:
16878 case GOMP_MAP_ALLOC:
16879 map_seen = 3;
16880 break;
16881 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16882 case GOMP_MAP_ALWAYS_POINTER:
16883 break;
16884 default:
16885 map_seen |= 1;
16886 error_at (OMP_CLAUSE_LOCATION (*pc),
16887 "%<#pragma omp target enter data%> with map-type other "
16888 "than %<to%> or %<alloc%> on %<map%> clause");
16889 *pc = OMP_CLAUSE_CHAIN (*pc);
16890 continue;
16892 pc = &OMP_CLAUSE_CHAIN (*pc);
16895 if (map_seen != 3)
16897 if (map_seen == 0)
16898 error_at (loc,
16899 "%<#pragma omp target enter data%> must contain at least "
16900 "one %<map%> clause");
16901 return NULL_TREE;
16904 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16905 TREE_TYPE (stmt) = void_type_node;
16906 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16907 SET_EXPR_LOCATION (stmt, loc);
16908 add_stmt (stmt);
16909 return stmt;
16912 /* OpenMP 4.5:
16913 # pragma omp target exit data target-data-clause[optseq] new-line */
16915 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16916 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16922 static tree
16923 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16924 enum pragma_context context)
16926 bool data_seen = false;
16927 if (c_parser_next_token_is (parser, CPP_NAME))
16929 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16930 if (strcmp (p, "data") == 0)
16932 c_parser_consume_token (parser);
16933 data_seen = true;
16936 if (!data_seen)
16938 c_parser_error (parser, "expected %<data%>");
16939 c_parser_skip_to_pragma_eol (parser);
16940 return NULL_TREE;
16943 if (context == pragma_stmt)
16945 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16946 "omp target exit data");
16947 c_parser_skip_to_pragma_eol (parser, false);
16948 return NULL_TREE;
16951 tree clauses
16952 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16953 "#pragma omp target exit data");
16955 int map_seen = 0;
16956 for (tree *pc = &clauses; *pc;)
16958 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16959 switch (OMP_CLAUSE_MAP_KIND (*pc))
16961 case GOMP_MAP_FROM:
16962 case GOMP_MAP_ALWAYS_FROM:
16963 case GOMP_MAP_RELEASE:
16964 case GOMP_MAP_DELETE:
16965 map_seen = 3;
16966 break;
16967 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16968 case GOMP_MAP_ALWAYS_POINTER:
16969 break;
16970 default:
16971 map_seen |= 1;
16972 error_at (OMP_CLAUSE_LOCATION (*pc),
16973 "%<#pragma omp target exit data%> with map-type other "
16974 "than %<from%>, %<release%> or %<delete%> on %<map%>"
16975 " clause");
16976 *pc = OMP_CLAUSE_CHAIN (*pc);
16977 continue;
16979 pc = &OMP_CLAUSE_CHAIN (*pc);
16982 if (map_seen != 3)
16984 if (map_seen == 0)
16985 error_at (loc,
16986 "%<#pragma omp target exit data%> must contain at least one "
16987 "%<map%> clause");
16988 return NULL_TREE;
16991 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16992 TREE_TYPE (stmt) = void_type_node;
16993 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16994 SET_EXPR_LOCATION (stmt, loc);
16995 add_stmt (stmt);
16996 return stmt;
16999 /* OpenMP 4.0:
17000 # pragma omp target target-clause[optseq] new-line
17001 structured-block */
17003 #define OMP_TARGET_CLAUSE_MASK \
17004 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
17009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
17012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17014 static bool
17015 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17017 location_t loc = c_parser_peek_token (parser)->location;
17018 c_parser_consume_pragma (parser);
17019 tree *pc = NULL, stmt, block;
17021 if (context != pragma_stmt && context != pragma_compound)
17023 c_parser_error (parser, "expected declaration specifiers");
17024 c_parser_skip_to_pragma_eol (parser);
17025 return false;
17028 if (c_parser_next_token_is (parser, CPP_NAME))
17030 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17031 enum tree_code ccode = ERROR_MARK;
17033 if (strcmp (p, "teams") == 0)
17034 ccode = OMP_TEAMS;
17035 else if (strcmp (p, "parallel") == 0)
17036 ccode = OMP_PARALLEL;
17037 else if (strcmp (p, "simd") == 0)
17038 ccode = OMP_SIMD;
17039 if (ccode != ERROR_MARK)
17041 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17042 char p_name[sizeof ("#pragma omp target teams distribute "
17043 "parallel for simd")];
17045 c_parser_consume_token (parser);
17046 strcpy (p_name, "#pragma omp target");
17047 if (!flag_openmp) /* flag_openmp_simd */
17049 tree stmt;
17050 switch (ccode)
17052 case OMP_TEAMS:
17053 stmt = c_parser_omp_teams (loc, parser, p_name,
17054 OMP_TARGET_CLAUSE_MASK,
17055 cclauses, if_p);
17056 break;
17057 case OMP_PARALLEL:
17058 stmt = c_parser_omp_parallel (loc, parser, p_name,
17059 OMP_TARGET_CLAUSE_MASK,
17060 cclauses, if_p);
17061 break;
17062 case OMP_SIMD:
17063 stmt = c_parser_omp_simd (loc, parser, p_name,
17064 OMP_TARGET_CLAUSE_MASK,
17065 cclauses, if_p);
17066 break;
17067 default:
17068 gcc_unreachable ();
17070 return stmt != NULL_TREE;
17072 keep_next_level ();
17073 tree block = c_begin_compound_stmt (true), ret;
17074 switch (ccode)
17076 case OMP_TEAMS:
17077 ret = c_parser_omp_teams (loc, parser, p_name,
17078 OMP_TARGET_CLAUSE_MASK, cclauses,
17079 if_p);
17080 break;
17081 case OMP_PARALLEL:
17082 ret = c_parser_omp_parallel (loc, parser, p_name,
17083 OMP_TARGET_CLAUSE_MASK, cclauses,
17084 if_p);
17085 break;
17086 case OMP_SIMD:
17087 ret = c_parser_omp_simd (loc, parser, p_name,
17088 OMP_TARGET_CLAUSE_MASK, cclauses,
17089 if_p);
17090 break;
17091 default:
17092 gcc_unreachable ();
17094 block = c_end_compound_stmt (loc, block, true);
17095 if (ret == NULL_TREE)
17096 return false;
17097 if (ccode == OMP_TEAMS)
17099 /* For combined target teams, ensure the num_teams and
17100 thread_limit clause expressions are evaluated on the host,
17101 before entering the target construct. */
17102 tree c;
17103 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17104 c; c = OMP_CLAUSE_CHAIN (c))
17105 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17106 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17107 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17109 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17110 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17111 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17112 expr, NULL_TREE, NULL_TREE);
17113 add_stmt (expr);
17114 OMP_CLAUSE_OPERAND (c, 0) = expr;
17115 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17116 OMP_CLAUSE_FIRSTPRIVATE);
17117 OMP_CLAUSE_DECL (tc) = tmp;
17118 OMP_CLAUSE_CHAIN (tc)
17119 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17120 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17123 tree stmt = make_node (OMP_TARGET);
17124 TREE_TYPE (stmt) = void_type_node;
17125 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17126 OMP_TARGET_BODY (stmt) = block;
17127 OMP_TARGET_COMBINED (stmt) = 1;
17128 add_stmt (stmt);
17129 pc = &OMP_TARGET_CLAUSES (stmt);
17130 goto check_clauses;
17132 else if (!flag_openmp) /* flag_openmp_simd */
17134 c_parser_skip_to_pragma_eol (parser, false);
17135 return false;
17137 else if (strcmp (p, "data") == 0)
17139 c_parser_consume_token (parser);
17140 c_parser_omp_target_data (loc, parser, if_p);
17141 return true;
17143 else if (strcmp (p, "enter") == 0)
17145 c_parser_consume_token (parser);
17146 c_parser_omp_target_enter_data (loc, parser, context);
17147 return false;
17149 else if (strcmp (p, "exit") == 0)
17151 c_parser_consume_token (parser);
17152 c_parser_omp_target_exit_data (loc, parser, context);
17153 return false;
17155 else if (strcmp (p, "update") == 0)
17157 c_parser_consume_token (parser);
17158 return c_parser_omp_target_update (loc, parser, context);
17161 if (!flag_openmp) /* flag_openmp_simd */
17163 c_parser_skip_to_pragma_eol (parser, false);
17164 return false;
17167 stmt = make_node (OMP_TARGET);
17168 TREE_TYPE (stmt) = void_type_node;
17170 OMP_TARGET_CLAUSES (stmt)
17171 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17172 "#pragma omp target");
17173 pc = &OMP_TARGET_CLAUSES (stmt);
17174 keep_next_level ();
17175 block = c_begin_compound_stmt (true);
17176 add_stmt (c_parser_omp_structured_block (parser, if_p));
17177 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17179 SET_EXPR_LOCATION (stmt, loc);
17180 add_stmt (stmt);
17182 check_clauses:
17183 while (*pc)
17185 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17186 switch (OMP_CLAUSE_MAP_KIND (*pc))
17188 case GOMP_MAP_TO:
17189 case GOMP_MAP_ALWAYS_TO:
17190 case GOMP_MAP_FROM:
17191 case GOMP_MAP_ALWAYS_FROM:
17192 case GOMP_MAP_TOFROM:
17193 case GOMP_MAP_ALWAYS_TOFROM:
17194 case GOMP_MAP_ALLOC:
17195 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17196 case GOMP_MAP_ALWAYS_POINTER:
17197 break;
17198 default:
17199 error_at (OMP_CLAUSE_LOCATION (*pc),
17200 "%<#pragma omp target%> with map-type other "
17201 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17202 "on %<map%> clause");
17203 *pc = OMP_CLAUSE_CHAIN (*pc);
17204 continue;
17206 pc = &OMP_CLAUSE_CHAIN (*pc);
17208 return true;
17211 /* OpenMP 4.0:
17212 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17214 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17215 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17222 static void
17223 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17225 auto_vec<c_token> clauses;
17226 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17228 c_token *token = c_parser_peek_token (parser);
17229 if (token->type == CPP_EOF)
17231 c_parser_skip_to_pragma_eol (parser);
17232 return;
17234 clauses.safe_push (*token);
17235 c_parser_consume_token (parser);
17237 clauses.safe_push (*c_parser_peek_token (parser));
17238 c_parser_skip_to_pragma_eol (parser);
17240 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17242 if (c_parser_peek_token (parser)->pragma_kind
17243 != PRAGMA_OMP_DECLARE
17244 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17245 || strcmp (IDENTIFIER_POINTER
17246 (c_parser_peek_2nd_token (parser)->value),
17247 "simd") != 0)
17249 c_parser_error (parser,
17250 "%<#pragma omp declare simd%> must be followed by "
17251 "function declaration or definition or another "
17252 "%<#pragma omp declare simd%>");
17253 return;
17255 c_parser_consume_pragma (parser);
17256 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17258 c_token *token = c_parser_peek_token (parser);
17259 if (token->type == CPP_EOF)
17261 c_parser_skip_to_pragma_eol (parser);
17262 return;
17264 clauses.safe_push (*token);
17265 c_parser_consume_token (parser);
17267 clauses.safe_push (*c_parser_peek_token (parser));
17268 c_parser_skip_to_pragma_eol (parser);
17271 /* Make sure nothing tries to read past the end of the tokens. */
17272 c_token eof_token;
17273 memset (&eof_token, 0, sizeof (eof_token));
17274 eof_token.type = CPP_EOF;
17275 clauses.safe_push (eof_token);
17276 clauses.safe_push (eof_token);
17278 switch (context)
17280 case pragma_external:
17281 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17282 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17284 int ext = disable_extension_diagnostics ();
17286 c_parser_consume_token (parser);
17287 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17288 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17289 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17290 NULL, clauses);
17291 restore_extension_diagnostics (ext);
17293 else
17294 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17295 NULL, clauses);
17296 break;
17297 case pragma_struct:
17298 case pragma_param:
17299 case pragma_stmt:
17300 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17301 "function declaration or definition");
17302 break;
17303 case pragma_compound:
17304 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17305 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17307 int ext = disable_extension_diagnostics ();
17309 c_parser_consume_token (parser);
17310 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17311 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17312 if (c_parser_next_tokens_start_declaration (parser))
17314 c_parser_declaration_or_fndef (parser, true, true, true, true,
17315 true, NULL, clauses);
17316 restore_extension_diagnostics (ext);
17317 break;
17319 restore_extension_diagnostics (ext);
17321 else if (c_parser_next_tokens_start_declaration (parser))
17323 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17324 NULL, clauses);
17325 break;
17327 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17328 "function declaration or definition");
17329 break;
17330 default:
17331 gcc_unreachable ();
17335 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17336 and put that into "omp declare simd" attribute. */
17338 static void
17339 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17340 vec<c_token> clauses)
17342 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17343 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17344 has already processed the tokens. */
17345 if (clauses.exists () && clauses[0].type == CPP_EOF)
17346 return;
17347 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17349 error ("%<#pragma omp declare simd%> not immediately followed by "
17350 "a function declaration or definition");
17351 clauses[0].type = CPP_EOF;
17352 return;
17354 if (clauses.exists () && clauses[0].type != CPP_NAME)
17356 error_at (DECL_SOURCE_LOCATION (fndecl),
17357 "%<#pragma omp declare simd%> not immediately followed by "
17358 "a single function declaration or definition");
17359 clauses[0].type = CPP_EOF;
17360 return;
17363 if (parms == NULL_TREE)
17364 parms = DECL_ARGUMENTS (fndecl);
17366 unsigned int tokens_avail = parser->tokens_avail;
17367 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17370 parser->tokens = clauses.address ();
17371 parser->tokens_avail = clauses.length ();
17373 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17374 while (parser->tokens_avail > 3)
17376 c_token *token = c_parser_peek_token (parser);
17377 gcc_assert (token->type == CPP_NAME
17378 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17379 c_parser_consume_token (parser);
17380 parser->in_pragma = true;
17382 tree c = NULL_TREE;
17383 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17384 "#pragma omp declare simd");
17385 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17386 if (c != NULL_TREE)
17387 c = tree_cons (NULL_TREE, c, NULL_TREE);
17388 c = build_tree_list (get_identifier ("omp declare simd"), c);
17389 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17390 DECL_ATTRIBUTES (fndecl) = c;
17393 parser->tokens = &parser->tokens_buf[0];
17394 parser->tokens_avail = tokens_avail;
17395 if (clauses.exists ())
17396 clauses[0].type = CPP_PRAGMA;
17400 /* OpenMP 4.0:
17401 # pragma omp declare target new-line
17402 declarations and definitions
17403 # pragma omp end declare target new-line
17405 OpenMP 4.5:
17406 # pragma omp declare target ( extended-list ) new-line
17408 # pragma omp declare target declare-target-clauses[seq] new-line */
17410 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17411 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17414 static void
17415 c_parser_omp_declare_target (c_parser *parser)
17417 location_t loc = c_parser_peek_token (parser)->location;
17418 tree clauses = NULL_TREE;
17419 if (c_parser_next_token_is (parser, CPP_NAME))
17420 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17421 "#pragma omp declare target");
17422 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17424 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17425 clauses);
17426 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17427 c_parser_skip_to_pragma_eol (parser);
17429 else
17431 c_parser_skip_to_pragma_eol (parser);
17432 current_omp_declare_target_attribute++;
17433 return;
17435 if (current_omp_declare_target_attribute)
17436 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17437 "%<#pragma omp declare target%> without clauses and "
17438 "%<#pragma omp end declare target%>");
17439 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17441 tree t = OMP_CLAUSE_DECL (c), id;
17442 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17443 tree at2 = lookup_attribute ("omp declare target link",
17444 DECL_ATTRIBUTES (t));
17445 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17447 id = get_identifier ("omp declare target link");
17448 std::swap (at1, at2);
17450 else
17451 id = get_identifier ("omp declare target");
17452 if (at2)
17454 error_at (OMP_CLAUSE_LOCATION (c),
17455 "%qD specified both in declare target %<link%> and %<to%>"
17456 " clauses", t);
17457 continue;
17459 if (!at1)
17461 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17462 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17463 continue;
17465 symtab_node *node = symtab_node::get (t);
17466 if (node != NULL)
17468 node->offloadable = 1;
17469 if (ENABLE_OFFLOADING)
17471 g->have_offload = true;
17472 if (is_a <varpool_node *> (node))
17473 vec_safe_push (offload_vars, t);
17480 static void
17481 c_parser_omp_end_declare_target (c_parser *parser)
17483 location_t loc = c_parser_peek_token (parser)->location;
17484 c_parser_consume_pragma (parser);
17485 if (c_parser_next_token_is (parser, CPP_NAME)
17486 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17487 "declare") == 0)
17489 c_parser_consume_token (parser);
17490 if (c_parser_next_token_is (parser, CPP_NAME)
17491 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17492 "target") == 0)
17493 c_parser_consume_token (parser);
17494 else
17496 c_parser_error (parser, "expected %<target%>");
17497 c_parser_skip_to_pragma_eol (parser);
17498 return;
17501 else
17503 c_parser_error (parser, "expected %<declare%>");
17504 c_parser_skip_to_pragma_eol (parser);
17505 return;
17507 c_parser_skip_to_pragma_eol (parser);
17508 if (!current_omp_declare_target_attribute)
17509 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17510 "%<#pragma omp declare target%>");
17511 else
17512 current_omp_declare_target_attribute--;
17516 /* OpenMP 4.0
17517 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17518 initializer-clause[opt] new-line
17520 initializer-clause:
17521 initializer (omp_priv = initializer)
17522 initializer (function-name (argument-list)) */
17524 static void
17525 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17527 unsigned int tokens_avail = 0, i;
17528 vec<tree> types = vNULL;
17529 vec<c_token> clauses = vNULL;
17530 enum tree_code reduc_code = ERROR_MARK;
17531 tree reduc_id = NULL_TREE;
17532 tree type;
17533 location_t rloc = c_parser_peek_token (parser)->location;
17535 if (context == pragma_struct || context == pragma_param)
17537 error ("%<#pragma omp declare reduction%> not at file or block scope");
17538 goto fail;
17541 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17542 goto fail;
17544 switch (c_parser_peek_token (parser)->type)
17546 case CPP_PLUS:
17547 reduc_code = PLUS_EXPR;
17548 break;
17549 case CPP_MULT:
17550 reduc_code = MULT_EXPR;
17551 break;
17552 case CPP_MINUS:
17553 reduc_code = MINUS_EXPR;
17554 break;
17555 case CPP_AND:
17556 reduc_code = BIT_AND_EXPR;
17557 break;
17558 case CPP_XOR:
17559 reduc_code = BIT_XOR_EXPR;
17560 break;
17561 case CPP_OR:
17562 reduc_code = BIT_IOR_EXPR;
17563 break;
17564 case CPP_AND_AND:
17565 reduc_code = TRUTH_ANDIF_EXPR;
17566 break;
17567 case CPP_OR_OR:
17568 reduc_code = TRUTH_ORIF_EXPR;
17569 break;
17570 case CPP_NAME:
17571 const char *p;
17572 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17573 if (strcmp (p, "min") == 0)
17575 reduc_code = MIN_EXPR;
17576 break;
17578 if (strcmp (p, "max") == 0)
17580 reduc_code = MAX_EXPR;
17581 break;
17583 reduc_id = c_parser_peek_token (parser)->value;
17584 break;
17585 default:
17586 c_parser_error (parser,
17587 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17588 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17589 goto fail;
17592 tree orig_reduc_id, reduc_decl;
17593 orig_reduc_id = reduc_id;
17594 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17595 reduc_decl = c_omp_reduction_decl (reduc_id);
17596 c_parser_consume_token (parser);
17598 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17599 goto fail;
17601 while (true)
17603 location_t loc = c_parser_peek_token (parser)->location;
17604 struct c_type_name *ctype = c_parser_type_name (parser);
17605 if (ctype != NULL)
17607 type = groktypename (ctype, NULL, NULL);
17608 if (type == error_mark_node)
17610 else if ((INTEGRAL_TYPE_P (type)
17611 || TREE_CODE (type) == REAL_TYPE
17612 || TREE_CODE (type) == COMPLEX_TYPE)
17613 && orig_reduc_id == NULL_TREE)
17614 error_at (loc, "predeclared arithmetic type in "
17615 "%<#pragma omp declare reduction%>");
17616 else if (TREE_CODE (type) == FUNCTION_TYPE
17617 || TREE_CODE (type) == ARRAY_TYPE)
17618 error_at (loc, "function or array type in "
17619 "%<#pragma omp declare reduction%>");
17620 else if (TYPE_ATOMIC (type))
17621 error_at (loc, "%<_Atomic%> qualified type in "
17622 "%<#pragma omp declare reduction%>");
17623 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17624 error_at (loc, "const, volatile or restrict qualified type in "
17625 "%<#pragma omp declare reduction%>");
17626 else
17628 tree t;
17629 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17630 if (comptypes (TREE_PURPOSE (t), type))
17632 error_at (loc, "redeclaration of %qs "
17633 "%<#pragma omp declare reduction%> for "
17634 "type %qT",
17635 IDENTIFIER_POINTER (reduc_id)
17636 + sizeof ("omp declare reduction ") - 1,
17637 type);
17638 location_t ploc
17639 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17640 0));
17641 error_at (ploc, "previous %<#pragma omp declare "
17642 "reduction%>");
17643 break;
17645 if (t == NULL_TREE)
17646 types.safe_push (type);
17648 if (c_parser_next_token_is (parser, CPP_COMMA))
17649 c_parser_consume_token (parser);
17650 else
17651 break;
17653 else
17654 break;
17657 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17658 || types.is_empty ())
17660 fail:
17661 clauses.release ();
17662 types.release ();
17663 while (true)
17665 c_token *token = c_parser_peek_token (parser);
17666 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17667 break;
17668 c_parser_consume_token (parser);
17670 c_parser_skip_to_pragma_eol (parser);
17671 return;
17674 if (types.length () > 1)
17676 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17678 c_token *token = c_parser_peek_token (parser);
17679 if (token->type == CPP_EOF)
17680 goto fail;
17681 clauses.safe_push (*token);
17682 c_parser_consume_token (parser);
17684 clauses.safe_push (*c_parser_peek_token (parser));
17685 c_parser_skip_to_pragma_eol (parser);
17687 /* Make sure nothing tries to read past the end of the tokens. */
17688 c_token eof_token;
17689 memset (&eof_token, 0, sizeof (eof_token));
17690 eof_token.type = CPP_EOF;
17691 clauses.safe_push (eof_token);
17692 clauses.safe_push (eof_token);
17695 int errs = errorcount;
17696 FOR_EACH_VEC_ELT (types, i, type)
17698 tokens_avail = parser->tokens_avail;
17699 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17700 if (!clauses.is_empty ())
17702 parser->tokens = clauses.address ();
17703 parser->tokens_avail = clauses.length ();
17704 parser->in_pragma = true;
17707 bool nested = current_function_decl != NULL_TREE;
17708 if (nested)
17709 c_push_function_context ();
17710 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17711 reduc_id, default_function_type);
17712 current_function_decl = fndecl;
17713 allocate_struct_function (fndecl, true);
17714 push_scope ();
17715 tree stmt = push_stmt_list ();
17716 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17717 warn about these. */
17718 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17719 get_identifier ("omp_out"), type);
17720 DECL_ARTIFICIAL (omp_out) = 1;
17721 DECL_CONTEXT (omp_out) = fndecl;
17722 pushdecl (omp_out);
17723 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17724 get_identifier ("omp_in"), type);
17725 DECL_ARTIFICIAL (omp_in) = 1;
17726 DECL_CONTEXT (omp_in) = fndecl;
17727 pushdecl (omp_in);
17728 struct c_expr combiner = c_parser_expression (parser);
17729 struct c_expr initializer;
17730 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17731 bool bad = false;
17732 initializer.set_error ();
17733 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17734 bad = true;
17735 else if (c_parser_next_token_is (parser, CPP_NAME)
17736 && strcmp (IDENTIFIER_POINTER
17737 (c_parser_peek_token (parser)->value),
17738 "initializer") == 0)
17740 c_parser_consume_token (parser);
17741 pop_scope ();
17742 push_scope ();
17743 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17744 get_identifier ("omp_priv"), type);
17745 DECL_ARTIFICIAL (omp_priv) = 1;
17746 DECL_INITIAL (omp_priv) = error_mark_node;
17747 DECL_CONTEXT (omp_priv) = fndecl;
17748 pushdecl (omp_priv);
17749 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17750 get_identifier ("omp_orig"), type);
17751 DECL_ARTIFICIAL (omp_orig) = 1;
17752 DECL_CONTEXT (omp_orig) = fndecl;
17753 pushdecl (omp_orig);
17754 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17755 bad = true;
17756 else if (!c_parser_next_token_is (parser, CPP_NAME))
17758 c_parser_error (parser, "expected %<omp_priv%> or "
17759 "function-name");
17760 bad = true;
17762 else if (strcmp (IDENTIFIER_POINTER
17763 (c_parser_peek_token (parser)->value),
17764 "omp_priv") != 0)
17766 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17767 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17769 c_parser_error (parser, "expected function-name %<(%>");
17770 bad = true;
17772 else
17773 initializer = c_parser_postfix_expression (parser);
17774 if (initializer.value
17775 && TREE_CODE (initializer.value) == CALL_EXPR)
17777 int j;
17778 tree c = initializer.value;
17779 for (j = 0; j < call_expr_nargs (c); j++)
17781 tree a = CALL_EXPR_ARG (c, j);
17782 STRIP_NOPS (a);
17783 if (TREE_CODE (a) == ADDR_EXPR
17784 && TREE_OPERAND (a, 0) == omp_priv)
17785 break;
17787 if (j == call_expr_nargs (c))
17788 error ("one of the initializer call arguments should be "
17789 "%<&omp_priv%>");
17792 else
17794 c_parser_consume_token (parser);
17795 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17796 bad = true;
17797 else
17799 tree st = push_stmt_list ();
17800 location_t loc = c_parser_peek_token (parser)->location;
17801 rich_location richloc (line_table, loc);
17802 start_init (omp_priv, NULL_TREE, 0, &richloc);
17803 struct c_expr init = c_parser_initializer (parser);
17804 finish_init ();
17805 finish_decl (omp_priv, loc, init.value,
17806 init.original_type, NULL_TREE);
17807 pop_stmt_list (st);
17810 if (!bad
17811 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17812 bad = true;
17815 if (!bad)
17817 c_parser_skip_to_pragma_eol (parser);
17819 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17820 DECL_INITIAL (reduc_decl));
17821 DECL_INITIAL (reduc_decl) = t;
17822 DECL_SOURCE_LOCATION (omp_out) = rloc;
17823 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17824 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17825 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17826 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17827 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17828 if (omp_priv)
17830 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17831 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17832 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17833 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17834 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17835 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17836 walk_tree (&DECL_INITIAL (omp_priv),
17837 c_check_omp_declare_reduction_r,
17838 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17842 pop_stmt_list (stmt);
17843 pop_scope ();
17844 if (cfun->language != NULL)
17846 ggc_free (cfun->language);
17847 cfun->language = NULL;
17849 set_cfun (NULL);
17850 current_function_decl = NULL_TREE;
17851 if (nested)
17852 c_pop_function_context ();
17854 if (!clauses.is_empty ())
17856 parser->tokens = &parser->tokens_buf[0];
17857 parser->tokens_avail = tokens_avail;
17859 if (bad)
17860 goto fail;
17861 if (errs != errorcount)
17862 break;
17865 clauses.release ();
17866 types.release ();
17870 /* OpenMP 4.0
17871 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17872 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17873 initializer-clause[opt] new-line
17874 #pragma omp declare target new-line */
17876 static void
17877 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17879 c_parser_consume_pragma (parser);
17880 if (c_parser_next_token_is (parser, CPP_NAME))
17882 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17883 if (strcmp (p, "simd") == 0)
17885 /* c_parser_consume_token (parser); done in
17886 c_parser_omp_declare_simd. */
17887 c_parser_omp_declare_simd (parser, context);
17888 return;
17890 if (strcmp (p, "reduction") == 0)
17892 c_parser_consume_token (parser);
17893 c_parser_omp_declare_reduction (parser, context);
17894 return;
17896 if (!flag_openmp) /* flag_openmp_simd */
17898 c_parser_skip_to_pragma_eol (parser, false);
17899 return;
17901 if (strcmp (p, "target") == 0)
17903 c_parser_consume_token (parser);
17904 c_parser_omp_declare_target (parser);
17905 return;
17909 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17910 "or %<target%>");
17911 c_parser_skip_to_pragma_eol (parser);
17914 /* OpenMP 4.5:
17915 #pragma omp taskloop taskloop-clause[optseq] new-line
17916 for-loop
17918 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17919 for-loop */
17921 #define OMP_TASKLOOP_CLAUSE_MASK \
17922 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17937 static tree
17938 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17939 char *p_name, omp_clause_mask mask, tree *cclauses,
17940 bool *if_p)
17942 tree clauses, block, ret;
17944 strcat (p_name, " taskloop");
17945 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17947 if (c_parser_next_token_is (parser, CPP_NAME))
17949 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17951 if (strcmp (p, "simd") == 0)
17953 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17954 if (cclauses == NULL)
17955 cclauses = cclauses_buf;
17956 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17957 c_parser_consume_token (parser);
17958 if (!flag_openmp) /* flag_openmp_simd */
17959 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17960 if_p);
17961 block = c_begin_compound_stmt (true);
17962 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17963 block = c_end_compound_stmt (loc, block, true);
17964 if (ret == NULL)
17965 return ret;
17966 ret = make_node (OMP_TASKLOOP);
17967 TREE_TYPE (ret) = void_type_node;
17968 OMP_FOR_BODY (ret) = block;
17969 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17970 SET_EXPR_LOCATION (ret, loc);
17971 add_stmt (ret);
17972 return ret;
17975 if (!flag_openmp) /* flag_openmp_simd */
17977 c_parser_skip_to_pragma_eol (parser, false);
17978 return NULL_TREE;
17981 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17982 if (cclauses)
17984 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17985 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17988 block = c_begin_compound_stmt (true);
17989 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17990 block = c_end_compound_stmt (loc, block, true);
17991 add_stmt (block);
17993 return ret;
17996 /* Main entry point to parsing most OpenMP pragmas. */
17998 static void
17999 c_parser_omp_construct (c_parser *parser, bool *if_p)
18001 enum pragma_kind p_kind;
18002 location_t loc;
18003 tree stmt;
18004 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18005 omp_clause_mask mask (0);
18007 loc = c_parser_peek_token (parser)->location;
18008 p_kind = c_parser_peek_token (parser)->pragma_kind;
18009 c_parser_consume_pragma (parser);
18011 switch (p_kind)
18013 case PRAGMA_OACC_ATOMIC:
18014 c_parser_omp_atomic (loc, parser);
18015 return;
18016 case PRAGMA_OACC_CACHE:
18017 strcpy (p_name, "#pragma acc");
18018 stmt = c_parser_oacc_cache (loc, parser);
18019 break;
18020 case PRAGMA_OACC_DATA:
18021 stmt = c_parser_oacc_data (loc, parser, if_p);
18022 break;
18023 case PRAGMA_OACC_HOST_DATA:
18024 stmt = c_parser_oacc_host_data (loc, parser, if_p);
18025 break;
18026 case PRAGMA_OACC_KERNELS:
18027 case PRAGMA_OACC_PARALLEL:
18028 strcpy (p_name, "#pragma acc");
18029 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18030 if_p);
18031 break;
18032 case PRAGMA_OACC_LOOP:
18033 strcpy (p_name, "#pragma acc");
18034 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18035 break;
18036 case PRAGMA_OACC_WAIT:
18037 strcpy (p_name, "#pragma wait");
18038 stmt = c_parser_oacc_wait (loc, parser, p_name);
18039 break;
18040 case PRAGMA_OMP_ATOMIC:
18041 c_parser_omp_atomic (loc, parser);
18042 return;
18043 case PRAGMA_OMP_CRITICAL:
18044 stmt = c_parser_omp_critical (loc, parser, if_p);
18045 break;
18046 case PRAGMA_OMP_DISTRIBUTE:
18047 strcpy (p_name, "#pragma omp");
18048 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18049 break;
18050 case PRAGMA_OMP_FOR:
18051 strcpy (p_name, "#pragma omp");
18052 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18053 break;
18054 case PRAGMA_OMP_MASTER:
18055 stmt = c_parser_omp_master (loc, parser, if_p);
18056 break;
18057 case PRAGMA_OMP_PARALLEL:
18058 strcpy (p_name, "#pragma omp");
18059 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18060 break;
18061 case PRAGMA_OMP_SECTIONS:
18062 strcpy (p_name, "#pragma omp");
18063 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18064 break;
18065 case PRAGMA_OMP_SIMD:
18066 strcpy (p_name, "#pragma omp");
18067 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18068 break;
18069 case PRAGMA_OMP_SINGLE:
18070 stmt = c_parser_omp_single (loc, parser, if_p);
18071 break;
18072 case PRAGMA_OMP_TASK:
18073 stmt = c_parser_omp_task (loc, parser, if_p);
18074 break;
18075 case PRAGMA_OMP_TASKGROUP:
18076 stmt = c_parser_omp_taskgroup (parser, if_p);
18077 break;
18078 case PRAGMA_OMP_TASKLOOP:
18079 strcpy (p_name, "#pragma omp");
18080 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18081 break;
18082 case PRAGMA_OMP_TEAMS:
18083 strcpy (p_name, "#pragma omp");
18084 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18085 break;
18086 default:
18087 gcc_unreachable ();
18090 if (stmt)
18091 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18095 /* OpenMP 2.5:
18096 # pragma omp threadprivate (variable-list) */
18098 static void
18099 c_parser_omp_threadprivate (c_parser *parser)
18101 tree vars, t;
18102 location_t loc;
18104 c_parser_consume_pragma (parser);
18105 loc = c_parser_peek_token (parser)->location;
18106 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18108 /* Mark every variable in VARS to be assigned thread local storage. */
18109 for (t = vars; t; t = TREE_CHAIN (t))
18111 tree v = TREE_PURPOSE (t);
18113 /* FIXME diagnostics: Ideally we should keep individual
18114 locations for all the variables in the var list to make the
18115 following errors more precise. Perhaps
18116 c_parser_omp_var_list_parens() should construct a list of
18117 locations to go along with the var list. */
18119 /* If V had already been marked threadprivate, it doesn't matter
18120 whether it had been used prior to this point. */
18121 if (!VAR_P (v))
18122 error_at (loc, "%qD is not a variable", v);
18123 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18124 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18125 else if (! is_global_var (v))
18126 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18127 else if (TREE_TYPE (v) == error_mark_node)
18129 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18130 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18131 else
18133 if (! DECL_THREAD_LOCAL_P (v))
18135 set_decl_tls_model (v, decl_default_tls_model (v));
18136 /* If rtl has been already set for this var, call
18137 make_decl_rtl once again, so that encode_section_info
18138 has a chance to look at the new decl flags. */
18139 if (DECL_RTL_SET_P (v))
18140 make_decl_rtl (v);
18142 C_DECL_THREADPRIVATE_P (v) = 1;
18146 c_parser_skip_to_pragma_eol (parser);
18149 /* Parse a transaction attribute (GCC Extension).
18151 transaction-attribute:
18152 attributes
18153 [ [ any-word ] ]
18155 The transactional memory language description is written for C++,
18156 and uses the C++0x attribute syntax. For compatibility, allow the
18157 bracket style for transactions in C as well. */
18159 static tree
18160 c_parser_transaction_attributes (c_parser *parser)
18162 tree attr_name, attr = NULL;
18164 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18165 return c_parser_attributes (parser);
18167 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18168 return NULL_TREE;
18169 c_parser_consume_token (parser);
18170 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18171 goto error1;
18173 attr_name = c_parser_attribute_any_word (parser);
18174 if (attr_name)
18176 c_parser_consume_token (parser);
18177 attr = build_tree_list (attr_name, NULL_TREE);
18179 else
18180 c_parser_error (parser, "expected identifier");
18182 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18183 error1:
18184 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18185 return attr;
18188 /* Parse a __transaction_atomic or __transaction_relaxed statement
18189 (GCC Extension).
18191 transaction-statement:
18192 __transaction_atomic transaction-attribute[opt] compound-statement
18193 __transaction_relaxed compound-statement
18195 Note that the only valid attribute is: "outer".
18198 static tree
18199 c_parser_transaction (c_parser *parser, enum rid keyword)
18201 unsigned int old_in = parser->in_transaction;
18202 unsigned int this_in = 1, new_in;
18203 location_t loc = c_parser_peek_token (parser)->location;
18204 tree stmt, attrs;
18206 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18207 || keyword == RID_TRANSACTION_RELAXED)
18208 && c_parser_next_token_is_keyword (parser, keyword));
18209 c_parser_consume_token (parser);
18211 if (keyword == RID_TRANSACTION_RELAXED)
18212 this_in |= TM_STMT_ATTR_RELAXED;
18213 else
18215 attrs = c_parser_transaction_attributes (parser);
18216 if (attrs)
18217 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18220 /* Keep track if we're in the lexical scope of an outer transaction. */
18221 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18223 parser->in_transaction = new_in;
18224 stmt = c_parser_compound_statement (parser);
18225 parser->in_transaction = old_in;
18227 if (flag_tm)
18228 stmt = c_finish_transaction (loc, stmt, this_in);
18229 else
18230 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18231 "%<__transaction_atomic%> without transactional memory support enabled"
18232 : "%<__transaction_relaxed %> "
18233 "without transactional memory support enabled"));
18235 return stmt;
18238 /* Parse a __transaction_atomic or __transaction_relaxed expression
18239 (GCC Extension).
18241 transaction-expression:
18242 __transaction_atomic ( expression )
18243 __transaction_relaxed ( expression )
18246 static struct c_expr
18247 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18249 struct c_expr ret;
18250 unsigned int old_in = parser->in_transaction;
18251 unsigned int this_in = 1;
18252 location_t loc = c_parser_peek_token (parser)->location;
18253 tree attrs;
18255 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18256 || keyword == RID_TRANSACTION_RELAXED)
18257 && c_parser_next_token_is_keyword (parser, keyword));
18258 c_parser_consume_token (parser);
18260 if (keyword == RID_TRANSACTION_RELAXED)
18261 this_in |= TM_STMT_ATTR_RELAXED;
18262 else
18264 attrs = c_parser_transaction_attributes (parser);
18265 if (attrs)
18266 this_in |= parse_tm_stmt_attr (attrs, 0);
18269 parser->in_transaction = this_in;
18270 matching_parens parens;
18271 if (parens.require_open (parser))
18273 tree expr = c_parser_expression (parser).value;
18274 ret.original_type = TREE_TYPE (expr);
18275 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18276 if (this_in & TM_STMT_ATTR_RELAXED)
18277 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18278 SET_EXPR_LOCATION (ret.value, loc);
18279 ret.original_code = TRANSACTION_EXPR;
18280 if (!parens.require_close (parser))
18282 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18283 goto error;
18286 else
18288 error:
18289 ret.set_error ();
18290 ret.original_code = ERROR_MARK;
18291 ret.original_type = NULL;
18293 parser->in_transaction = old_in;
18295 if (!flag_tm)
18296 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18297 "%<__transaction_atomic%> without transactional memory support enabled"
18298 : "%<__transaction_relaxed %> "
18299 "without transactional memory support enabled"));
18301 set_c_expr_source_range (&ret, loc, loc);
18303 return ret;
18306 /* Parse a __transaction_cancel statement (GCC Extension).
18308 transaction-cancel-statement:
18309 __transaction_cancel transaction-attribute[opt] ;
18311 Note that the only valid attribute is "outer".
18314 static tree
18315 c_parser_transaction_cancel (c_parser *parser)
18317 location_t loc = c_parser_peek_token (parser)->location;
18318 tree attrs;
18319 bool is_outer = false;
18321 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18322 c_parser_consume_token (parser);
18324 attrs = c_parser_transaction_attributes (parser);
18325 if (attrs)
18326 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18328 if (!flag_tm)
18330 error_at (loc, "%<__transaction_cancel%> without "
18331 "transactional memory support enabled");
18332 goto ret_error;
18334 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18336 error_at (loc, "%<__transaction_cancel%> within a "
18337 "%<__transaction_relaxed%>");
18338 goto ret_error;
18340 else if (is_outer)
18342 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18343 && !is_tm_may_cancel_outer (current_function_decl))
18345 error_at (loc, "outer %<__transaction_cancel%> not "
18346 "within outer %<__transaction_atomic%>");
18347 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18348 goto ret_error;
18351 else if (parser->in_transaction == 0)
18353 error_at (loc, "%<__transaction_cancel%> not within "
18354 "%<__transaction_atomic%>");
18355 goto ret_error;
18358 return add_stmt (build_tm_abort_call (loc, is_outer));
18360 ret_error:
18361 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18364 /* Parse a single source file. */
18366 void
18367 c_parse_file (void)
18369 /* Use local storage to begin. If the first token is a pragma, parse it.
18370 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18371 which will cause garbage collection. */
18372 c_parser tparser;
18374 memset (&tparser, 0, sizeof tparser);
18375 tparser.tokens = &tparser.tokens_buf[0];
18376 the_parser = &tparser;
18378 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18379 c_parser_pragma_pch_preprocess (&tparser);
18381 the_parser = ggc_alloc<c_parser> ();
18382 *the_parser = tparser;
18383 if (tparser.tokens == &tparser.tokens_buf[0])
18384 the_parser->tokens = &the_parser->tokens_buf[0];
18386 /* Initialize EH, if we've been told to do so. */
18387 if (flag_exceptions)
18388 using_eh_for_cleanups ();
18390 c_parser_translation_unit (the_parser);
18391 the_parser = NULL;
18394 /* Parse the body of a function declaration marked with "__RTL".
18396 The RTL parser works on the level of characters read from a
18397 FILE *, whereas c_parser works at the level of tokens.
18398 Square this circle by consuming all of the tokens up to and
18399 including the closing brace, recording the start/end of the RTL
18400 fragment, and reopening the file and re-reading the relevant
18401 lines within the RTL parser.
18403 This requires the opening and closing braces of the C function
18404 to be on separate lines from the RTL they wrap.
18406 Take ownership of START_WITH_PASS, if non-NULL. */
18408 void
18409 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18411 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18413 free (start_with_pass);
18414 return;
18417 location_t start_loc = c_parser_peek_token (parser)->location;
18419 /* Consume all tokens, up to the closing brace, handling
18420 matching pairs of braces in the rtl dump. */
18421 int num_open_braces = 1;
18422 while (1)
18424 switch (c_parser_peek_token (parser)->type)
18426 case CPP_OPEN_BRACE:
18427 num_open_braces++;
18428 break;
18429 case CPP_CLOSE_BRACE:
18430 if (--num_open_braces == 0)
18431 goto found_closing_brace;
18432 break;
18433 case CPP_EOF:
18434 error_at (start_loc, "no closing brace");
18435 free (start_with_pass);
18436 return;
18437 default:
18438 break;
18440 c_parser_consume_token (parser);
18443 found_closing_brace:
18444 /* At the closing brace; record its location. */
18445 location_t end_loc = c_parser_peek_token (parser)->location;
18447 /* Consume the closing brace. */
18448 c_parser_consume_token (parser);
18450 /* Invoke the RTL parser. */
18451 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18453 free (start_with_pass);
18454 return;
18457 /* If a pass name was provided for START_WITH_PASS, run the backend
18458 accordingly now, on the cfun created above, transferring
18459 ownership of START_WITH_PASS. */
18460 if (start_with_pass)
18461 run_rtl_passes (start_with_pass);
18464 #include "gt-c-c-parser.h"