[PATCH c++/86881] -Wshadow-local-compatible ICE
[official-gcc.git] / gcc / c / c-parser.c
blob1766a256633bc41a67e0f42fc1c62ac5fe564eef
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 auto_diagnostic_group d;
1818 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1819 here);
1820 if (hint)
1822 richloc.add_fixit_replace (hint.suggestion ());
1823 error_at (&richloc,
1824 "unknown type name %qE; did you mean %qs?",
1825 name, hint.suggestion ());
1827 else
1828 error_at (here, "unknown type name %qE", name);
1831 /* Parse declspecs normally to get a correct pointer type, but avoid
1832 a further "fails to be a type name" error. Refuse nested functions
1833 since it is not how the user likely wants us to recover. */
1834 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1835 c_parser_peek_token (parser)->keyword = RID_VOID;
1836 c_parser_peek_token (parser)->value = error_mark_node;
1837 fndef_ok = !nested;
1840 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1841 true, true, cla_nonabstract_decl);
1842 if (parser->error)
1844 c_parser_skip_to_end_of_block_or_statement (parser);
1845 return;
1847 if (nested && !specs->declspecs_seen_p)
1849 c_parser_error (parser, "expected declaration specifiers");
1850 c_parser_skip_to_end_of_block_or_statement (parser);
1851 return;
1854 finish_declspecs (specs);
1855 bool auto_type_p = specs->typespec_word == cts_auto_type;
1856 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1858 if (auto_type_p)
1859 error_at (here, "%<__auto_type%> in empty declaration");
1860 else if (specs->typespec_kind == ctsk_none
1861 && attribute_fallthrough_p (specs->attrs))
1863 if (fallthru_attr_p != NULL)
1864 *fallthru_attr_p = true;
1865 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1866 void_type_node, 0);
1867 add_stmt (fn);
1869 else if (empty_ok)
1870 shadow_tag (specs);
1871 else
1873 shadow_tag_warned (specs, 1);
1874 pedwarn (here, 0, "empty declaration");
1876 c_parser_consume_token (parser);
1877 if (oacc_routine_data)
1878 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1879 return;
1882 /* Provide better error recovery. Note that a type name here is usually
1883 better diagnosed as a redeclaration. */
1884 if (empty_ok
1885 && specs->typespec_kind == ctsk_tagdef
1886 && c_parser_next_token_starts_declspecs (parser)
1887 && !c_parser_next_token_is (parser, CPP_NAME))
1889 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1890 parser->error = false;
1891 shadow_tag_warned (specs, 1);
1892 return;
1894 else if (c_dialect_objc () && !auto_type_p)
1896 /* Prefix attributes are an error on method decls. */
1897 switch (c_parser_peek_token (parser)->type)
1899 case CPP_PLUS:
1900 case CPP_MINUS:
1901 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1902 return;
1903 if (specs->attrs)
1905 warning_at (c_parser_peek_token (parser)->location,
1906 OPT_Wattributes,
1907 "prefix attributes are ignored for methods");
1908 specs->attrs = NULL_TREE;
1910 if (fndef_ok)
1911 c_parser_objc_method_definition (parser);
1912 else
1913 c_parser_objc_methodproto (parser);
1914 return;
1915 break;
1916 default:
1917 break;
1919 /* This is where we parse 'attributes @interface ...',
1920 'attributes @implementation ...', 'attributes @protocol ...'
1921 (where attributes could be, for example, __attribute__
1922 ((deprecated)).
1924 switch (c_parser_peek_token (parser)->keyword)
1926 case RID_AT_INTERFACE:
1928 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1929 return;
1930 c_parser_objc_class_definition (parser, specs->attrs);
1931 return;
1933 break;
1934 case RID_AT_IMPLEMENTATION:
1936 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1937 return;
1938 if (specs->attrs)
1940 warning_at (c_parser_peek_token (parser)->location,
1941 OPT_Wattributes,
1942 "prefix attributes are ignored for implementations");
1943 specs->attrs = NULL_TREE;
1945 c_parser_objc_class_definition (parser, NULL_TREE);
1946 return;
1948 break;
1949 case RID_AT_PROTOCOL:
1951 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1952 return;
1953 c_parser_objc_protocol_definition (parser, specs->attrs);
1954 return;
1956 break;
1957 case RID_AT_ALIAS:
1958 case RID_AT_CLASS:
1959 case RID_AT_END:
1960 case RID_AT_PROPERTY:
1961 if (specs->attrs)
1963 c_parser_error (parser, "unexpected attribute");
1964 specs->attrs = NULL;
1966 break;
1967 default:
1968 break;
1971 else if (attribute_fallthrough_p (specs->attrs))
1972 warning_at (here, OPT_Wattributes,
1973 "%<fallthrough%> attribute not followed by %<;%>");
1975 pending_xref_error ();
1976 prefix_attrs = specs->attrs;
1977 all_prefix_attrs = prefix_attrs;
1978 specs->attrs = NULL_TREE;
1979 while (true)
1981 struct c_declarator *declarator;
1982 bool dummy = false;
1983 timevar_id_t tv;
1984 tree fnbody = NULL_TREE;
1985 /* Declaring either one or more declarators (in which case we
1986 should diagnose if there were no declaration specifiers) or a
1987 function definition (in which case the diagnostic for
1988 implicit int suffices). */
1989 declarator = c_parser_declarator (parser,
1990 specs->typespec_kind != ctsk_none,
1991 C_DTR_NORMAL, &dummy);
1992 if (declarator == NULL)
1994 if (omp_declare_simd_clauses.exists ())
1995 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1996 omp_declare_simd_clauses);
1997 if (oacc_routine_data)
1998 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1999 c_parser_skip_to_end_of_block_or_statement (parser);
2000 return;
2002 if (auto_type_p && declarator->kind != cdk_id)
2004 error_at (here,
2005 "%<__auto_type%> requires a plain identifier"
2006 " as declarator");
2007 c_parser_skip_to_end_of_block_or_statement (parser);
2008 return;
2010 if (c_parser_next_token_is (parser, CPP_EQ)
2011 || c_parser_next_token_is (parser, CPP_COMMA)
2012 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2013 || c_parser_next_token_is_keyword (parser, RID_ASM)
2014 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2015 || c_parser_next_token_is_keyword (parser, RID_IN))
2017 tree asm_name = NULL_TREE;
2018 tree postfix_attrs = NULL_TREE;
2019 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2021 diagnosed_no_specs = true;
2022 pedwarn (here, 0, "data definition has no type or storage class");
2024 /* Having seen a data definition, there cannot now be a
2025 function definition. */
2026 fndef_ok = false;
2027 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2028 asm_name = c_parser_simple_asm_expr (parser);
2029 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2031 postfix_attrs = c_parser_attributes (parser);
2032 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2034 /* This means there is an attribute specifier after
2035 the declarator in a function definition. Provide
2036 some more information for the user. */
2037 error_at (here, "attributes should be specified before the "
2038 "declarator in a function definition");
2039 c_parser_skip_to_end_of_block_or_statement (parser);
2040 return;
2043 if (c_parser_next_token_is (parser, CPP_EQ))
2045 tree d;
2046 struct c_expr init;
2047 location_t init_loc;
2048 c_parser_consume_token (parser);
2049 if (auto_type_p)
2051 init_loc = c_parser_peek_token (parser)->location;
2052 rich_location richloc (line_table, init_loc);
2053 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2054 /* A parameter is initialized, which is invalid. Don't
2055 attempt to instrument the initializer. */
2056 int flag_sanitize_save = flag_sanitize;
2057 if (nested && !empty_ok)
2058 flag_sanitize = 0;
2059 init = c_parser_expr_no_commas (parser, NULL);
2060 flag_sanitize = flag_sanitize_save;
2061 if (TREE_CODE (init.value) == COMPONENT_REF
2062 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2063 error_at (here,
2064 "%<__auto_type%> used with a bit-field"
2065 " initializer");
2066 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2067 tree init_type = TREE_TYPE (init.value);
2068 /* As with typeof, remove all qualifiers from atomic types. */
2069 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2070 init_type
2071 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2072 bool vm_type = variably_modified_type_p (init_type,
2073 NULL_TREE);
2074 if (vm_type)
2075 init.value = save_expr (init.value);
2076 finish_init ();
2077 specs->typespec_kind = ctsk_typeof;
2078 specs->locations[cdw_typedef] = init_loc;
2079 specs->typedef_p = true;
2080 specs->type = init_type;
2081 if (vm_type)
2083 bool maybe_const = true;
2084 tree type_expr = c_fully_fold (init.value, false,
2085 &maybe_const);
2086 specs->expr_const_operands &= maybe_const;
2087 if (specs->expr)
2088 specs->expr = build2 (COMPOUND_EXPR,
2089 TREE_TYPE (type_expr),
2090 specs->expr, type_expr);
2091 else
2092 specs->expr = type_expr;
2094 d = start_decl (declarator, specs, true,
2095 chainon (postfix_attrs, all_prefix_attrs));
2096 if (!d)
2097 d = error_mark_node;
2098 if (omp_declare_simd_clauses.exists ())
2099 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2100 omp_declare_simd_clauses);
2102 else
2104 /* The declaration of the variable is in effect while
2105 its initializer is parsed. */
2106 d = start_decl (declarator, specs, true,
2107 chainon (postfix_attrs, all_prefix_attrs));
2108 if (!d)
2109 d = error_mark_node;
2110 if (omp_declare_simd_clauses.exists ())
2111 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2112 omp_declare_simd_clauses);
2113 init_loc = c_parser_peek_token (parser)->location;
2114 rich_location richloc (line_table, init_loc);
2115 start_init (d, asm_name, global_bindings_p (), &richloc);
2116 /* A parameter is initialized, which is invalid. Don't
2117 attempt to instrument the initializer. */
2118 int flag_sanitize_save = flag_sanitize;
2119 if (TREE_CODE (d) == PARM_DECL)
2120 flag_sanitize = 0;
2121 init = c_parser_initializer (parser);
2122 flag_sanitize = flag_sanitize_save;
2123 finish_init ();
2125 if (oacc_routine_data)
2126 c_finish_oacc_routine (oacc_routine_data, d, false);
2127 if (d != error_mark_node)
2129 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2130 finish_decl (d, init_loc, init.value,
2131 init.original_type, asm_name);
2134 else
2136 if (auto_type_p)
2138 error_at (here,
2139 "%<__auto_type%> requires an initialized "
2140 "data declaration");
2141 c_parser_skip_to_end_of_block_or_statement (parser);
2142 return;
2144 tree d = start_decl (declarator, specs, false,
2145 chainon (postfix_attrs,
2146 all_prefix_attrs));
2147 if (d && TREE_CODE (d) == FUNCTION_DECL)
2148 if (declarator->kind == cdk_function)
2149 if (DECL_ARGUMENTS (d) == NULL_TREE)
2150 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2151 if (omp_declare_simd_clauses.exists ())
2153 tree parms = NULL_TREE;
2154 if (d && TREE_CODE (d) == FUNCTION_DECL)
2156 struct c_declarator *ce = declarator;
2157 while (ce != NULL)
2158 if (ce->kind == cdk_function)
2160 parms = ce->u.arg_info->parms;
2161 break;
2163 else
2164 ce = ce->declarator;
2166 if (parms)
2167 temp_store_parm_decls (d, parms);
2168 c_finish_omp_declare_simd (parser, d, parms,
2169 omp_declare_simd_clauses);
2170 if (parms)
2171 temp_pop_parm_decls ();
2173 if (oacc_routine_data)
2174 c_finish_oacc_routine (oacc_routine_data, d, false);
2175 if (d)
2176 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2177 NULL_TREE, asm_name);
2179 if (c_parser_next_token_is_keyword (parser, RID_IN))
2181 if (d)
2182 *objc_foreach_object_declaration = d;
2183 else
2184 *objc_foreach_object_declaration = error_mark_node;
2187 if (c_parser_next_token_is (parser, CPP_COMMA))
2189 if (auto_type_p)
2191 error_at (here,
2192 "%<__auto_type%> may only be used with"
2193 " a single declarator");
2194 c_parser_skip_to_end_of_block_or_statement (parser);
2195 return;
2197 c_parser_consume_token (parser);
2198 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2199 all_prefix_attrs = chainon (c_parser_attributes (parser),
2200 prefix_attrs);
2201 else
2202 all_prefix_attrs = prefix_attrs;
2203 continue;
2205 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2207 c_parser_consume_token (parser);
2208 return;
2210 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2212 /* This can only happen in Objective-C: we found the
2213 'in' that terminates the declaration inside an
2214 Objective-C foreach statement. Do not consume the
2215 token, so that the caller can use it to determine
2216 that this indeed is a foreach context. */
2217 return;
2219 else
2221 c_parser_error (parser, "expected %<,%> or %<;%>");
2222 c_parser_skip_to_end_of_block_or_statement (parser);
2223 return;
2226 else if (auto_type_p)
2228 error_at (here,
2229 "%<__auto_type%> requires an initialized data declaration");
2230 c_parser_skip_to_end_of_block_or_statement (parser);
2231 return;
2233 else if (!fndef_ok)
2235 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2236 "%<asm%> or %<__attribute__%>");
2237 c_parser_skip_to_end_of_block_or_statement (parser);
2238 return;
2240 /* Function definition (nested or otherwise). */
2241 if (nested)
2243 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2244 c_push_function_context ();
2246 if (!start_function (specs, declarator, all_prefix_attrs))
2248 /* At this point we've consumed:
2249 declaration-specifiers declarator
2250 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2251 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2252 but the
2253 declaration-specifiers declarator
2254 aren't grokkable as a function definition, so we have
2255 an error. */
2256 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2257 if (c_parser_next_token_starts_declspecs (parser))
2259 /* If we have
2260 declaration-specifiers declarator decl-specs
2261 then assume we have a missing semicolon, which would
2262 give us:
2263 declaration-specifiers declarator decl-specs
2266 <~~~~~~~~~ declaration ~~~~~~~~~~>
2267 Use c_parser_require to get an error with a fix-it hint. */
2268 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2269 parser->error = false;
2271 else
2273 /* This can appear in many cases looking nothing like a
2274 function definition, so we don't give a more specific
2275 error suggesting there was one. */
2276 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2277 "or %<__attribute__%>");
2279 if (nested)
2280 c_pop_function_context ();
2281 break;
2284 if (DECL_DECLARED_INLINE_P (current_function_decl))
2285 tv = TV_PARSE_INLINE;
2286 else
2287 tv = TV_PARSE_FUNC;
2288 auto_timevar at (g_timer, tv);
2290 /* Parse old-style parameter declarations. ??? Attributes are
2291 not allowed to start declaration specifiers here because of a
2292 syntax conflict between a function declaration with attribute
2293 suffix and a function definition with an attribute prefix on
2294 first old-style parameter declaration. Following the old
2295 parser, they are not accepted on subsequent old-style
2296 parameter declarations either. However, there is no
2297 ambiguity after the first declaration, nor indeed on the
2298 first as long as we don't allow postfix attributes after a
2299 declarator with a nonempty identifier list in a definition;
2300 and postfix attributes have never been accepted here in
2301 function definitions either. */
2302 while (c_parser_next_token_is_not (parser, CPP_EOF)
2303 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2304 c_parser_declaration_or_fndef (parser, false, false, false,
2305 true, false, NULL, vNULL);
2306 store_parm_decls ();
2307 if (omp_declare_simd_clauses.exists ())
2308 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2309 omp_declare_simd_clauses);
2310 if (oacc_routine_data)
2311 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2312 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2313 = c_parser_peek_token (parser)->location;
2315 /* If the definition was marked with __GIMPLE then parse the
2316 function body as GIMPLE. */
2317 if (specs->gimple_p)
2319 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2320 bool saved = in_late_binary_op;
2321 in_late_binary_op = true;
2322 c_parser_parse_gimple_body (parser);
2323 in_late_binary_op = saved;
2325 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2326 consuming the function body. */
2327 else if (specs->rtl_p)
2329 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2331 /* Normally, store_parm_decls sets next_is_function_body,
2332 anticipating a function body. We need a push_scope/pop_scope
2333 pair to flush out this state, or subsequent function parsing
2334 will go wrong. */
2335 push_scope ();
2336 pop_scope ();
2338 finish_function ();
2339 return;
2341 else
2342 fnbody = c_parser_compound_statement (parser);
2343 tree fndecl = current_function_decl;
2344 if (nested)
2346 tree decl = current_function_decl;
2347 /* Mark nested functions as needing static-chain initially.
2348 lower_nested_functions will recompute it but the
2349 DECL_STATIC_CHAIN flag is also used before that happens,
2350 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2351 DECL_STATIC_CHAIN (decl) = 1;
2352 add_stmt (fnbody);
2353 finish_function ();
2354 c_pop_function_context ();
2355 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2357 else
2359 if (fnbody)
2360 add_stmt (fnbody);
2361 finish_function ();
2363 /* Get rid of the empty stmt list for GIMPLE. */
2364 if (specs->gimple_p)
2365 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2367 break;
2371 /* Parse an asm-definition (asm() outside a function body). This is a
2372 GNU extension.
2374 asm-definition:
2375 simple-asm-expr ;
2378 static void
2379 c_parser_asm_definition (c_parser *parser)
2381 tree asm_str = c_parser_simple_asm_expr (parser);
2382 if (asm_str)
2383 symtab->finalize_toplevel_asm (asm_str);
2384 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2387 /* Parse a static assertion (C11 6.7.10).
2389 static_assert-declaration:
2390 static_assert-declaration-no-semi ;
2393 static void
2394 c_parser_static_assert_declaration (c_parser *parser)
2396 c_parser_static_assert_declaration_no_semi (parser);
2397 if (parser->error
2398 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2399 c_parser_skip_to_end_of_block_or_statement (parser);
2402 /* Parse a static assertion (C11 6.7.10), without the trailing
2403 semicolon.
2405 static_assert-declaration-no-semi:
2406 _Static_assert ( constant-expression , string-literal )
2409 static void
2410 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2412 location_t assert_loc, value_loc;
2413 tree value;
2414 tree string;
2416 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2417 assert_loc = c_parser_peek_token (parser)->location;
2418 if (flag_isoc99)
2419 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2420 "ISO C99 does not support %<_Static_assert%>");
2421 else
2422 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2423 "ISO C90 does not support %<_Static_assert%>");
2424 c_parser_consume_token (parser);
2425 matching_parens parens;
2426 if (!parens.require_open (parser))
2427 return;
2428 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2429 value = c_parser_expr_no_commas (parser, NULL).value;
2430 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2431 parser->lex_untranslated_string = true;
2432 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2434 parser->lex_untranslated_string = false;
2435 return;
2437 switch (c_parser_peek_token (parser)->type)
2439 case CPP_STRING:
2440 case CPP_STRING16:
2441 case CPP_STRING32:
2442 case CPP_WSTRING:
2443 case CPP_UTF8STRING:
2444 string = c_parser_peek_token (parser)->value;
2445 c_parser_consume_token (parser);
2446 parser->lex_untranslated_string = false;
2447 break;
2448 default:
2449 c_parser_error (parser, "expected string literal");
2450 parser->lex_untranslated_string = false;
2451 return;
2453 parens.require_close (parser);
2455 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2457 error_at (value_loc, "expression in static assertion is not an integer");
2458 return;
2460 if (TREE_CODE (value) != INTEGER_CST)
2462 value = c_fully_fold (value, false, NULL);
2463 /* Strip no-op conversions. */
2464 STRIP_TYPE_NOPS (value);
2465 if (TREE_CODE (value) == INTEGER_CST)
2466 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2467 "is not an integer constant expression");
2469 if (TREE_CODE (value) != INTEGER_CST)
2471 error_at (value_loc, "expression in static assertion is not constant");
2472 return;
2474 constant_expression_warning (value);
2475 if (integer_zerop (value))
2476 error_at (assert_loc, "static assertion failed: %E", string);
2479 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2480 6.7, C11 6.7), adding them to SPECS (which may already include some).
2481 Storage class specifiers are accepted iff SCSPEC_OK; type
2482 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2483 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2484 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2486 declaration-specifiers:
2487 storage-class-specifier declaration-specifiers[opt]
2488 type-specifier declaration-specifiers[opt]
2489 type-qualifier declaration-specifiers[opt]
2490 function-specifier declaration-specifiers[opt]
2491 alignment-specifier declaration-specifiers[opt]
2493 Function specifiers (inline) are from C99, and are currently
2494 handled as storage class specifiers, as is __thread. Alignment
2495 specifiers are from C11.
2497 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2498 storage-class-specifier:
2499 typedef
2500 extern
2501 static
2502 auto
2503 register
2504 _Thread_local
2506 (_Thread_local is new in C11.)
2508 C99 6.7.4, C11 6.7.4:
2509 function-specifier:
2510 inline
2511 _Noreturn
2513 (_Noreturn is new in C11.)
2515 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2516 type-specifier:
2517 void
2518 char
2519 short
2521 long
2522 float
2523 double
2524 signed
2525 unsigned
2526 _Bool
2527 _Complex
2528 [_Imaginary removed in C99 TC2]
2529 struct-or-union-specifier
2530 enum-specifier
2531 typedef-name
2532 atomic-type-specifier
2534 (_Bool and _Complex are new in C99.)
2535 (atomic-type-specifier is new in C11.)
2537 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2539 type-qualifier:
2540 const
2541 restrict
2542 volatile
2543 address-space-qualifier
2544 _Atomic
2546 (restrict is new in C99.)
2547 (_Atomic is new in C11.)
2549 GNU extensions:
2551 declaration-specifiers:
2552 attributes declaration-specifiers[opt]
2554 type-qualifier:
2555 address-space
2557 address-space:
2558 identifier recognized by the target
2560 storage-class-specifier:
2561 __thread
2563 type-specifier:
2564 typeof-specifier
2565 __auto_type
2566 __intN
2567 _Decimal32
2568 _Decimal64
2569 _Decimal128
2570 _Fract
2571 _Accum
2572 _Sat
2574 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2575 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2577 atomic-type-specifier
2578 _Atomic ( type-name )
2580 Objective-C:
2582 type-specifier:
2583 class-name objc-protocol-refs[opt]
2584 typedef-name objc-protocol-refs
2585 objc-protocol-refs
2588 void
2589 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2590 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2591 bool alignspec_ok, bool auto_type_ok,
2592 enum c_lookahead_kind la)
2594 bool attrs_ok = start_attr_ok;
2595 bool seen_type = specs->typespec_kind != ctsk_none;
2597 if (!typespec_ok)
2598 gcc_assert (la == cla_prefer_id);
2600 while (c_parser_next_token_is (parser, CPP_NAME)
2601 || c_parser_next_token_is (parser, CPP_KEYWORD)
2602 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2604 struct c_typespec t;
2605 tree attrs;
2606 tree align;
2607 location_t loc = c_parser_peek_token (parser)->location;
2609 /* If we cannot accept a type, exit if the next token must start
2610 one. Also, if we already have seen a tagged definition,
2611 a typename would be an error anyway and likely the user
2612 has simply forgotten a semicolon, so we exit. */
2613 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2614 && c_parser_next_tokens_start_typename (parser, la)
2615 && !c_parser_next_token_is_qualifier (parser)
2616 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2617 break;
2619 if (c_parser_next_token_is (parser, CPP_NAME))
2621 c_token *name_token = c_parser_peek_token (parser);
2622 tree value = name_token->value;
2623 c_id_kind kind = name_token->id_kind;
2625 if (kind == C_ID_ADDRSPACE)
2627 addr_space_t as
2628 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2629 declspecs_add_addrspace (name_token->location, specs, as);
2630 c_parser_consume_token (parser);
2631 attrs_ok = true;
2632 continue;
2635 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2637 /* If we cannot accept a type, and the next token must start one,
2638 exit. Do the same if we already have seen a tagged definition,
2639 since it would be an error anyway and likely the user has simply
2640 forgotten a semicolon. */
2641 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2642 break;
2644 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2645 a C_ID_CLASSNAME. */
2646 c_parser_consume_token (parser);
2647 seen_type = true;
2648 attrs_ok = true;
2649 if (kind == C_ID_ID)
2651 error_at (loc, "unknown type name %qE", value);
2652 t.kind = ctsk_typedef;
2653 t.spec = error_mark_node;
2655 else if (kind == C_ID_TYPENAME
2656 && (!c_dialect_objc ()
2657 || c_parser_next_token_is_not (parser, CPP_LESS)))
2659 t.kind = ctsk_typedef;
2660 /* For a typedef name, record the meaning, not the name.
2661 In case of 'foo foo, bar;'. */
2662 t.spec = lookup_name (value);
2664 else
2666 tree proto = NULL_TREE;
2667 gcc_assert (c_dialect_objc ());
2668 t.kind = ctsk_objc;
2669 if (c_parser_next_token_is (parser, CPP_LESS))
2670 proto = c_parser_objc_protocol_refs (parser);
2671 t.spec = objc_get_protocol_qualified_type (value, proto);
2673 t.expr = NULL_TREE;
2674 t.expr_const_operands = true;
2675 declspecs_add_type (name_token->location, specs, t);
2676 continue;
2678 if (c_parser_next_token_is (parser, CPP_LESS))
2680 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2681 nisse@lysator.liu.se. */
2682 tree proto;
2683 gcc_assert (c_dialect_objc ());
2684 if (!typespec_ok || seen_type)
2685 break;
2686 proto = c_parser_objc_protocol_refs (parser);
2687 t.kind = ctsk_objc;
2688 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2689 t.expr = NULL_TREE;
2690 t.expr_const_operands = true;
2691 declspecs_add_type (loc, specs, t);
2692 continue;
2694 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2695 switch (c_parser_peek_token (parser)->keyword)
2697 case RID_STATIC:
2698 case RID_EXTERN:
2699 case RID_REGISTER:
2700 case RID_TYPEDEF:
2701 case RID_INLINE:
2702 case RID_NORETURN:
2703 case RID_AUTO:
2704 case RID_THREAD:
2705 if (!scspec_ok)
2706 goto out;
2707 attrs_ok = true;
2708 /* TODO: Distinguish between function specifiers (inline, noreturn)
2709 and storage class specifiers, either here or in
2710 declspecs_add_scspec. */
2711 declspecs_add_scspec (loc, specs,
2712 c_parser_peek_token (parser)->value);
2713 c_parser_consume_token (parser);
2714 break;
2715 case RID_AUTO_TYPE:
2716 if (!auto_type_ok)
2717 goto out;
2718 /* Fall through. */
2719 case RID_UNSIGNED:
2720 case RID_LONG:
2721 case RID_SHORT:
2722 case RID_SIGNED:
2723 case RID_COMPLEX:
2724 case RID_INT:
2725 case RID_CHAR:
2726 case RID_FLOAT:
2727 case RID_DOUBLE:
2728 case RID_VOID:
2729 case RID_DFLOAT32:
2730 case RID_DFLOAT64:
2731 case RID_DFLOAT128:
2732 CASE_RID_FLOATN_NX:
2733 case RID_BOOL:
2734 case RID_FRACT:
2735 case RID_ACCUM:
2736 case RID_SAT:
2737 case RID_INT_N_0:
2738 case RID_INT_N_1:
2739 case RID_INT_N_2:
2740 case RID_INT_N_3:
2741 if (!typespec_ok)
2742 goto out;
2743 attrs_ok = true;
2744 seen_type = true;
2745 if (c_dialect_objc ())
2746 parser->objc_need_raw_identifier = true;
2747 t.kind = ctsk_resword;
2748 t.spec = c_parser_peek_token (parser)->value;
2749 t.expr = NULL_TREE;
2750 t.expr_const_operands = true;
2751 declspecs_add_type (loc, specs, t);
2752 c_parser_consume_token (parser);
2753 break;
2754 case RID_ENUM:
2755 if (!typespec_ok)
2756 goto out;
2757 attrs_ok = true;
2758 seen_type = true;
2759 t = c_parser_enum_specifier (parser);
2760 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2761 declspecs_add_type (loc, specs, t);
2762 break;
2763 case RID_STRUCT:
2764 case RID_UNION:
2765 if (!typespec_ok)
2766 goto out;
2767 attrs_ok = true;
2768 seen_type = true;
2769 t = c_parser_struct_or_union_specifier (parser);
2770 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2771 declspecs_add_type (loc, specs, t);
2772 break;
2773 case RID_TYPEOF:
2774 /* ??? The old parser rejected typeof after other type
2775 specifiers, but is a syntax error the best way of
2776 handling this? */
2777 if (!typespec_ok || seen_type)
2778 goto out;
2779 attrs_ok = true;
2780 seen_type = true;
2781 t = c_parser_typeof_specifier (parser);
2782 declspecs_add_type (loc, specs, t);
2783 break;
2784 case RID_ATOMIC:
2785 /* C parser handling of Objective-C constructs needs
2786 checking for correct lvalue-to-rvalue conversions, and
2787 the code in build_modify_expr handling various
2788 Objective-C cases, and that in build_unary_op handling
2789 Objective-C cases for increment / decrement, also needs
2790 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2791 and objc_types_are_equivalent may also need updates. */
2792 if (c_dialect_objc ())
2793 sorry ("%<_Atomic%> in Objective-C");
2794 if (flag_isoc99)
2795 pedwarn_c99 (loc, OPT_Wpedantic,
2796 "ISO C99 does not support the %<_Atomic%> qualifier");
2797 else
2798 pedwarn_c99 (loc, OPT_Wpedantic,
2799 "ISO C90 does not support the %<_Atomic%> qualifier");
2800 attrs_ok = true;
2801 tree value;
2802 value = c_parser_peek_token (parser)->value;
2803 c_parser_consume_token (parser);
2804 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2806 /* _Atomic ( type-name ). */
2807 seen_type = true;
2808 c_parser_consume_token (parser);
2809 struct c_type_name *type = c_parser_type_name (parser);
2810 t.kind = ctsk_typeof;
2811 t.spec = error_mark_node;
2812 t.expr = NULL_TREE;
2813 t.expr_const_operands = true;
2814 if (type != NULL)
2815 t.spec = groktypename (type, &t.expr,
2816 &t.expr_const_operands);
2817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2818 "expected %<)%>");
2819 if (t.spec != error_mark_node)
2821 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2822 error_at (loc, "%<_Atomic%>-qualified array type");
2823 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2824 error_at (loc, "%<_Atomic%>-qualified function type");
2825 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2826 error_at (loc, "%<_Atomic%> applied to a qualified type");
2827 else
2828 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2830 declspecs_add_type (loc, specs, t);
2832 else
2833 declspecs_add_qual (loc, specs, value);
2834 break;
2835 case RID_CONST:
2836 case RID_VOLATILE:
2837 case RID_RESTRICT:
2838 attrs_ok = true;
2839 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2840 c_parser_consume_token (parser);
2841 break;
2842 case RID_ATTRIBUTE:
2843 if (!attrs_ok)
2844 goto out;
2845 attrs = c_parser_attributes (parser);
2846 declspecs_add_attrs (loc, specs, attrs);
2847 break;
2848 case RID_ALIGNAS:
2849 if (!alignspec_ok)
2850 goto out;
2851 align = c_parser_alignas_specifier (parser);
2852 declspecs_add_alignas (loc, specs, align);
2853 break;
2854 case RID_GIMPLE:
2855 if (! flag_gimple)
2856 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2857 c_parser_consume_token (parser);
2858 specs->gimple_p = true;
2859 specs->locations[cdw_gimple] = loc;
2860 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2861 break;
2862 case RID_RTL:
2863 c_parser_consume_token (parser);
2864 specs->rtl_p = true;
2865 specs->locations[cdw_rtl] = loc;
2866 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2867 break;
2868 default:
2869 goto out;
2872 out: ;
2875 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2877 enum-specifier:
2878 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2879 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2880 enum attributes[opt] identifier
2882 The form with trailing comma is new in C99. The forms with
2883 attributes are GNU extensions. In GNU C, we accept any expression
2884 without commas in the syntax (assignment expressions, not just
2885 conditional expressions); assignment expressions will be diagnosed
2886 as non-constant.
2888 enumerator-list:
2889 enumerator
2890 enumerator-list , enumerator
2892 enumerator:
2893 enumeration-constant
2894 enumeration-constant = constant-expression
2896 GNU Extensions:
2898 enumerator:
2899 enumeration-constant attributes[opt]
2900 enumeration-constant attributes[opt] = constant-expression
2904 static struct c_typespec
2905 c_parser_enum_specifier (c_parser *parser)
2907 struct c_typespec ret;
2908 tree attrs;
2909 tree ident = NULL_TREE;
2910 location_t enum_loc;
2911 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2912 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2913 c_parser_consume_token (parser);
2914 attrs = c_parser_attributes (parser);
2915 enum_loc = c_parser_peek_token (parser)->location;
2916 /* Set the location in case we create a decl now. */
2917 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2918 if (c_parser_next_token_is (parser, CPP_NAME))
2920 ident = c_parser_peek_token (parser)->value;
2921 ident_loc = c_parser_peek_token (parser)->location;
2922 enum_loc = ident_loc;
2923 c_parser_consume_token (parser);
2925 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2927 /* Parse an enum definition. */
2928 struct c_enum_contents the_enum;
2929 tree type;
2930 tree postfix_attrs;
2931 /* We chain the enumerators in reverse order, then put them in
2932 forward order at the end. */
2933 tree values;
2934 timevar_push (TV_PARSE_ENUM);
2935 type = start_enum (enum_loc, &the_enum, ident);
2936 values = NULL_TREE;
2937 c_parser_consume_token (parser);
2938 while (true)
2940 tree enum_id;
2941 tree enum_value;
2942 tree enum_decl;
2943 bool seen_comma;
2944 c_token *token;
2945 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2946 location_t decl_loc, value_loc;
2947 if (c_parser_next_token_is_not (parser, CPP_NAME))
2949 /* Give a nicer error for "enum {}". */
2950 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2951 && !parser->error)
2953 error_at (c_parser_peek_token (parser)->location,
2954 "empty enum is invalid");
2955 parser->error = true;
2957 else
2958 c_parser_error (parser, "expected identifier");
2959 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2960 values = error_mark_node;
2961 break;
2963 token = c_parser_peek_token (parser);
2964 enum_id = token->value;
2965 /* Set the location in case we create a decl now. */
2966 c_parser_set_source_position_from_token (token);
2967 decl_loc = value_loc = token->location;
2968 c_parser_consume_token (parser);
2969 /* Parse any specified attributes. */
2970 tree enum_attrs = c_parser_attributes (parser);
2971 if (c_parser_next_token_is (parser, CPP_EQ))
2973 c_parser_consume_token (parser);
2974 value_loc = c_parser_peek_token (parser)->location;
2975 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2977 else
2978 enum_value = NULL_TREE;
2979 enum_decl = build_enumerator (decl_loc, value_loc,
2980 &the_enum, enum_id, enum_value);
2981 if (enum_attrs)
2982 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2983 TREE_CHAIN (enum_decl) = values;
2984 values = enum_decl;
2985 seen_comma = false;
2986 if (c_parser_next_token_is (parser, CPP_COMMA))
2988 comma_loc = c_parser_peek_token (parser)->location;
2989 seen_comma = true;
2990 c_parser_consume_token (parser);
2992 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2994 if (seen_comma)
2995 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2996 "comma at end of enumerator list");
2997 c_parser_consume_token (parser);
2998 break;
3000 if (!seen_comma)
3002 c_parser_error (parser, "expected %<,%> or %<}%>");
3003 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3004 values = error_mark_node;
3005 break;
3008 postfix_attrs = c_parser_attributes (parser);
3009 ret.spec = finish_enum (type, nreverse (values),
3010 chainon (attrs, postfix_attrs));
3011 ret.kind = ctsk_tagdef;
3012 ret.expr = NULL_TREE;
3013 ret.expr_const_operands = true;
3014 timevar_pop (TV_PARSE_ENUM);
3015 return ret;
3017 else if (!ident)
3019 c_parser_error (parser, "expected %<{%>");
3020 ret.spec = error_mark_node;
3021 ret.kind = ctsk_tagref;
3022 ret.expr = NULL_TREE;
3023 ret.expr_const_operands = true;
3024 return ret;
3026 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3027 /* In ISO C, enumerated types can be referred to only if already
3028 defined. */
3029 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3031 gcc_assert (ident);
3032 pedwarn (enum_loc, OPT_Wpedantic,
3033 "ISO C forbids forward references to %<enum%> types");
3035 return ret;
3038 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3040 struct-or-union-specifier:
3041 struct-or-union attributes[opt] identifier[opt]
3042 { struct-contents } attributes[opt]
3043 struct-or-union attributes[opt] identifier
3045 struct-contents:
3046 struct-declaration-list
3048 struct-declaration-list:
3049 struct-declaration ;
3050 struct-declaration-list struct-declaration ;
3052 GNU extensions:
3054 struct-contents:
3055 empty
3056 struct-declaration
3057 struct-declaration-list struct-declaration
3059 struct-declaration-list:
3060 struct-declaration-list ;
3063 (Note that in the syntax here, unlike that in ISO C, the semicolons
3064 are included here rather than in struct-declaration, in order to
3065 describe the syntax with extra semicolons and missing semicolon at
3066 end.)
3068 Objective-C:
3070 struct-declaration-list:
3071 @defs ( class-name )
3073 (Note this does not include a trailing semicolon, but can be
3074 followed by further declarations, and gets a pedwarn-if-pedantic
3075 when followed by a semicolon.) */
3077 static struct c_typespec
3078 c_parser_struct_or_union_specifier (c_parser *parser)
3080 struct c_typespec ret;
3081 tree attrs;
3082 tree ident = NULL_TREE;
3083 location_t struct_loc;
3084 location_t ident_loc = UNKNOWN_LOCATION;
3085 enum tree_code code;
3086 switch (c_parser_peek_token (parser)->keyword)
3088 case RID_STRUCT:
3089 code = RECORD_TYPE;
3090 break;
3091 case RID_UNION:
3092 code = UNION_TYPE;
3093 break;
3094 default:
3095 gcc_unreachable ();
3097 struct_loc = c_parser_peek_token (parser)->location;
3098 c_parser_consume_token (parser);
3099 attrs = c_parser_attributes (parser);
3101 /* Set the location in case we create a decl now. */
3102 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3104 if (c_parser_next_token_is (parser, CPP_NAME))
3106 ident = c_parser_peek_token (parser)->value;
3107 ident_loc = c_parser_peek_token (parser)->location;
3108 struct_loc = ident_loc;
3109 c_parser_consume_token (parser);
3111 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3113 /* Parse a struct or union definition. Start the scope of the
3114 tag before parsing components. */
3115 struct c_struct_parse_info *struct_info;
3116 tree type = start_struct (struct_loc, code, ident, &struct_info);
3117 tree postfix_attrs;
3118 /* We chain the components in reverse order, then put them in
3119 forward order at the end. Each struct-declaration may
3120 declare multiple components (comma-separated), so we must use
3121 chainon to join them, although when parsing each
3122 struct-declaration we can use TREE_CHAIN directly.
3124 The theory behind all this is that there will be more
3125 semicolon separated fields than comma separated fields, and
3126 so we'll be minimizing the number of node traversals required
3127 by chainon. */
3128 tree contents;
3129 timevar_push (TV_PARSE_STRUCT);
3130 contents = NULL_TREE;
3131 c_parser_consume_token (parser);
3132 /* Handle the Objective-C @defs construct,
3133 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3134 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3136 tree name;
3137 gcc_assert (c_dialect_objc ());
3138 c_parser_consume_token (parser);
3139 matching_parens parens;
3140 if (!parens.require_open (parser))
3141 goto end_at_defs;
3142 if (c_parser_next_token_is (parser, CPP_NAME)
3143 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3145 name = c_parser_peek_token (parser)->value;
3146 c_parser_consume_token (parser);
3148 else
3150 c_parser_error (parser, "expected class name");
3151 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3152 goto end_at_defs;
3154 parens.skip_until_found_close (parser);
3155 contents = nreverse (objc_get_class_ivars (name));
3157 end_at_defs:
3158 /* Parse the struct-declarations and semicolons. Problems with
3159 semicolons are diagnosed here; empty structures are diagnosed
3160 elsewhere. */
3161 while (true)
3163 tree decls;
3164 /* Parse any stray semicolon. */
3165 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3167 location_t semicolon_loc
3168 = c_parser_peek_token (parser)->location;
3169 gcc_rich_location richloc (semicolon_loc);
3170 richloc.add_fixit_remove ();
3171 pedwarn (&richloc, OPT_Wpedantic,
3172 "extra semicolon in struct or union specified");
3173 c_parser_consume_token (parser);
3174 continue;
3176 /* Stop if at the end of the struct or union contents. */
3177 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3179 c_parser_consume_token (parser);
3180 break;
3182 /* Accept #pragmas at struct scope. */
3183 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3185 c_parser_pragma (parser, pragma_struct, NULL);
3186 continue;
3188 /* Parse some comma-separated declarations, but not the
3189 trailing semicolon if any. */
3190 decls = c_parser_struct_declaration (parser);
3191 contents = chainon (decls, contents);
3192 /* If no semicolon follows, either we have a parse error or
3193 are at the end of the struct or union and should
3194 pedwarn. */
3195 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3196 c_parser_consume_token (parser);
3197 else
3199 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3200 pedwarn (c_parser_peek_token (parser)->location, 0,
3201 "no semicolon at end of struct or union");
3202 else if (parser->error
3203 || !c_parser_next_token_starts_declspecs (parser))
3205 c_parser_error (parser, "expected %<;%>");
3206 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3207 break;
3210 /* If we come here, we have already emitted an error
3211 for an expected `;', identifier or `(', and we also
3212 recovered already. Go on with the next field. */
3215 postfix_attrs = c_parser_attributes (parser);
3216 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3217 chainon (attrs, postfix_attrs), struct_info);
3218 ret.kind = ctsk_tagdef;
3219 ret.expr = NULL_TREE;
3220 ret.expr_const_operands = true;
3221 timevar_pop (TV_PARSE_STRUCT);
3222 return ret;
3224 else if (!ident)
3226 c_parser_error (parser, "expected %<{%>");
3227 ret.spec = error_mark_node;
3228 ret.kind = ctsk_tagref;
3229 ret.expr = NULL_TREE;
3230 ret.expr_const_operands = true;
3231 return ret;
3233 ret = parser_xref_tag (ident_loc, code, ident);
3234 return ret;
3237 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3238 *without* the trailing semicolon.
3240 struct-declaration:
3241 specifier-qualifier-list struct-declarator-list
3242 static_assert-declaration-no-semi
3244 specifier-qualifier-list:
3245 type-specifier specifier-qualifier-list[opt]
3246 type-qualifier specifier-qualifier-list[opt]
3247 alignment-specifier specifier-qualifier-list[opt]
3248 attributes specifier-qualifier-list[opt]
3250 struct-declarator-list:
3251 struct-declarator
3252 struct-declarator-list , attributes[opt] struct-declarator
3254 struct-declarator:
3255 declarator attributes[opt]
3256 declarator[opt] : constant-expression attributes[opt]
3258 GNU extensions:
3260 struct-declaration:
3261 __extension__ struct-declaration
3262 specifier-qualifier-list
3264 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3265 of attributes where shown is a GNU extension. In GNU C, we accept
3266 any expression without commas in the syntax (assignment
3267 expressions, not just conditional expressions); assignment
3268 expressions will be diagnosed as non-constant. */
3270 static tree
3271 c_parser_struct_declaration (c_parser *parser)
3273 struct c_declspecs *specs;
3274 tree prefix_attrs;
3275 tree all_prefix_attrs;
3276 tree decls;
3277 location_t decl_loc;
3278 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3280 int ext;
3281 tree decl;
3282 ext = disable_extension_diagnostics ();
3283 c_parser_consume_token (parser);
3284 decl = c_parser_struct_declaration (parser);
3285 restore_extension_diagnostics (ext);
3286 return decl;
3288 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3290 c_parser_static_assert_declaration_no_semi (parser);
3291 return NULL_TREE;
3293 specs = build_null_declspecs ();
3294 decl_loc = c_parser_peek_token (parser)->location;
3295 /* Strictly by the standard, we shouldn't allow _Alignas here,
3296 but it appears to have been intended to allow it there, so
3297 we're keeping it as it is until WG14 reaches a conclusion
3298 of N1731.
3299 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3300 c_parser_declspecs (parser, specs, false, true, true,
3301 true, false, cla_nonabstract_decl);
3302 if (parser->error)
3303 return NULL_TREE;
3304 if (!specs->declspecs_seen_p)
3306 c_parser_error (parser, "expected specifier-qualifier-list");
3307 return NULL_TREE;
3309 finish_declspecs (specs);
3310 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3311 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3313 tree ret;
3314 if (specs->typespec_kind == ctsk_none)
3316 pedwarn (decl_loc, OPT_Wpedantic,
3317 "ISO C forbids member declarations with no members");
3318 shadow_tag_warned (specs, pedantic);
3319 ret = NULL_TREE;
3321 else
3323 /* Support for unnamed structs or unions as members of
3324 structs or unions (which is [a] useful and [b] supports
3325 MS P-SDK). */
3326 tree attrs = NULL;
3328 ret = grokfield (c_parser_peek_token (parser)->location,
3329 build_id_declarator (NULL_TREE), specs,
3330 NULL_TREE, &attrs);
3331 if (ret)
3332 decl_attributes (&ret, attrs, 0);
3334 return ret;
3337 /* Provide better error recovery. Note that a type name here is valid,
3338 and will be treated as a field name. */
3339 if (specs->typespec_kind == ctsk_tagdef
3340 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3341 && c_parser_next_token_starts_declspecs (parser)
3342 && !c_parser_next_token_is (parser, CPP_NAME))
3344 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3345 parser->error = false;
3346 return NULL_TREE;
3349 pending_xref_error ();
3350 prefix_attrs = specs->attrs;
3351 all_prefix_attrs = prefix_attrs;
3352 specs->attrs = NULL_TREE;
3353 decls = NULL_TREE;
3354 while (true)
3356 /* Declaring one or more declarators or un-named bit-fields. */
3357 struct c_declarator *declarator;
3358 bool dummy = false;
3359 if (c_parser_next_token_is (parser, CPP_COLON))
3360 declarator = build_id_declarator (NULL_TREE);
3361 else
3362 declarator = c_parser_declarator (parser,
3363 specs->typespec_kind != ctsk_none,
3364 C_DTR_NORMAL, &dummy);
3365 if (declarator == NULL)
3367 c_parser_skip_to_end_of_block_or_statement (parser);
3368 break;
3370 if (c_parser_next_token_is (parser, CPP_COLON)
3371 || c_parser_next_token_is (parser, CPP_COMMA)
3372 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3373 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3374 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3376 tree postfix_attrs = NULL_TREE;
3377 tree width = NULL_TREE;
3378 tree d;
3379 if (c_parser_next_token_is (parser, CPP_COLON))
3381 c_parser_consume_token (parser);
3382 width = c_parser_expr_no_commas (parser, NULL).value;
3384 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3385 postfix_attrs = c_parser_attributes (parser);
3386 d = grokfield (c_parser_peek_token (parser)->location,
3387 declarator, specs, width, &all_prefix_attrs);
3388 decl_attributes (&d, chainon (postfix_attrs,
3389 all_prefix_attrs), 0);
3390 DECL_CHAIN (d) = decls;
3391 decls = d;
3392 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3393 all_prefix_attrs = chainon (c_parser_attributes (parser),
3394 prefix_attrs);
3395 else
3396 all_prefix_attrs = prefix_attrs;
3397 if (c_parser_next_token_is (parser, CPP_COMMA))
3398 c_parser_consume_token (parser);
3399 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3400 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3402 /* Semicolon consumed in caller. */
3403 break;
3405 else
3407 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3408 break;
3411 else
3413 c_parser_error (parser,
3414 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3415 "%<__attribute__%>");
3416 break;
3419 return decls;
3422 /* Parse a typeof specifier (a GNU extension).
3424 typeof-specifier:
3425 typeof ( expression )
3426 typeof ( type-name )
3429 static struct c_typespec
3430 c_parser_typeof_specifier (c_parser *parser)
3432 struct c_typespec ret;
3433 ret.kind = ctsk_typeof;
3434 ret.spec = error_mark_node;
3435 ret.expr = NULL_TREE;
3436 ret.expr_const_operands = true;
3437 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3438 c_parser_consume_token (parser);
3439 c_inhibit_evaluation_warnings++;
3440 in_typeof++;
3441 matching_parens parens;
3442 if (!parens.require_open (parser))
3444 c_inhibit_evaluation_warnings--;
3445 in_typeof--;
3446 return ret;
3448 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3450 struct c_type_name *type = c_parser_type_name (parser);
3451 c_inhibit_evaluation_warnings--;
3452 in_typeof--;
3453 if (type != NULL)
3455 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3456 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3459 else
3461 bool was_vm;
3462 location_t here = c_parser_peek_token (parser)->location;
3463 struct c_expr expr = c_parser_expression (parser);
3464 c_inhibit_evaluation_warnings--;
3465 in_typeof--;
3466 if (TREE_CODE (expr.value) == COMPONENT_REF
3467 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3468 error_at (here, "%<typeof%> applied to a bit-field");
3469 mark_exp_read (expr.value);
3470 ret.spec = TREE_TYPE (expr.value);
3471 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3472 /* This is returned with the type so that when the type is
3473 evaluated, this can be evaluated. */
3474 if (was_vm)
3475 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3476 pop_maybe_used (was_vm);
3477 /* For use in macros such as those in <stdatomic.h>, remove all
3478 qualifiers from atomic types. (const can be an issue for more macros
3479 using typeof than just the <stdatomic.h> ones.) */
3480 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3481 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3483 parens.skip_until_found_close (parser);
3484 return ret;
3487 /* Parse an alignment-specifier.
3489 C11 6.7.5:
3491 alignment-specifier:
3492 _Alignas ( type-name )
3493 _Alignas ( constant-expression )
3496 static tree
3497 c_parser_alignas_specifier (c_parser * parser)
3499 tree ret = error_mark_node;
3500 location_t loc = c_parser_peek_token (parser)->location;
3501 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3502 c_parser_consume_token (parser);
3503 if (flag_isoc99)
3504 pedwarn_c99 (loc, OPT_Wpedantic,
3505 "ISO C99 does not support %<_Alignas%>");
3506 else
3507 pedwarn_c99 (loc, OPT_Wpedantic,
3508 "ISO C90 does not support %<_Alignas%>");
3509 matching_parens parens;
3510 if (!parens.require_open (parser))
3511 return ret;
3512 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3514 struct c_type_name *type = c_parser_type_name (parser);
3515 if (type != NULL)
3516 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3517 false, true, 1);
3519 else
3520 ret = c_parser_expr_no_commas (parser, NULL).value;
3521 parens.skip_until_found_close (parser);
3522 return ret;
3525 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3526 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3527 a typedef name may be redeclared; otherwise it may not. KIND
3528 indicates which kind of declarator is wanted. Returns a valid
3529 declarator except in the case of a syntax error in which case NULL is
3530 returned. *SEEN_ID is set to true if an identifier being declared is
3531 seen; this is used to diagnose bad forms of abstract array declarators
3532 and to determine whether an identifier list is syntactically permitted.
3534 declarator:
3535 pointer[opt] direct-declarator
3537 direct-declarator:
3538 identifier
3539 ( attributes[opt] declarator )
3540 direct-declarator array-declarator
3541 direct-declarator ( parameter-type-list )
3542 direct-declarator ( identifier-list[opt] )
3544 pointer:
3545 * type-qualifier-list[opt]
3546 * type-qualifier-list[opt] pointer
3548 type-qualifier-list:
3549 type-qualifier
3550 attributes
3551 type-qualifier-list type-qualifier
3552 type-qualifier-list attributes
3554 array-declarator:
3555 [ type-qualifier-list[opt] assignment-expression[opt] ]
3556 [ static type-qualifier-list[opt] assignment-expression ]
3557 [ type-qualifier-list static assignment-expression ]
3558 [ type-qualifier-list[opt] * ]
3560 parameter-type-list:
3561 parameter-list
3562 parameter-list , ...
3564 parameter-list:
3565 parameter-declaration
3566 parameter-list , parameter-declaration
3568 parameter-declaration:
3569 declaration-specifiers declarator attributes[opt]
3570 declaration-specifiers abstract-declarator[opt] attributes[opt]
3572 identifier-list:
3573 identifier
3574 identifier-list , identifier
3576 abstract-declarator:
3577 pointer
3578 pointer[opt] direct-abstract-declarator
3580 direct-abstract-declarator:
3581 ( attributes[opt] abstract-declarator )
3582 direct-abstract-declarator[opt] array-declarator
3583 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3585 GNU extensions:
3587 direct-declarator:
3588 direct-declarator ( parameter-forward-declarations
3589 parameter-type-list[opt] )
3591 direct-abstract-declarator:
3592 direct-abstract-declarator[opt] ( parameter-forward-declarations
3593 parameter-type-list[opt] )
3595 parameter-forward-declarations:
3596 parameter-list ;
3597 parameter-forward-declarations parameter-list ;
3599 The uses of attributes shown above are GNU extensions.
3601 Some forms of array declarator are not included in C99 in the
3602 syntax for abstract declarators; these are disallowed elsewhere.
3603 This may be a defect (DR#289).
3605 This function also accepts an omitted abstract declarator as being
3606 an abstract declarator, although not part of the formal syntax. */
3608 struct c_declarator *
3609 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3610 bool *seen_id)
3612 /* Parse any initial pointer part. */
3613 if (c_parser_next_token_is (parser, CPP_MULT))
3615 struct c_declspecs *quals_attrs = build_null_declspecs ();
3616 struct c_declarator *inner;
3617 c_parser_consume_token (parser);
3618 c_parser_declspecs (parser, quals_attrs, false, false, true,
3619 false, false, cla_prefer_id);
3620 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3621 if (inner == NULL)
3622 return NULL;
3623 else
3624 return make_pointer_declarator (quals_attrs, inner);
3626 /* Now we have a direct declarator, direct abstract declarator or
3627 nothing (which counts as a direct abstract declarator here). */
3628 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3631 /* Parse a direct declarator or direct abstract declarator; arguments
3632 as c_parser_declarator. */
3634 static struct c_declarator *
3635 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3636 bool *seen_id)
3638 /* The direct declarator must start with an identifier (possibly
3639 omitted) or a parenthesized declarator (possibly abstract). In
3640 an ordinary declarator, initial parentheses must start a
3641 parenthesized declarator. In an abstract declarator or parameter
3642 declarator, they could start a parenthesized declarator or a
3643 parameter list. To tell which, the open parenthesis and any
3644 following attributes must be read. If a declaration specifier
3645 follows, then it is a parameter list; if the specifier is a
3646 typedef name, there might be an ambiguity about redeclaring it,
3647 which is resolved in the direction of treating it as a typedef
3648 name. If a close parenthesis follows, it is also an empty
3649 parameter list, as the syntax does not permit empty abstract
3650 declarators. Otherwise, it is a parenthesized declarator (in
3651 which case the analysis may be repeated inside it, recursively).
3653 ??? There is an ambiguity in a parameter declaration "int
3654 (__attribute__((foo)) x)", where x is not a typedef name: it
3655 could be an abstract declarator for a function, or declare x with
3656 parentheses. The proper resolution of this ambiguity needs
3657 documenting. At present we follow an accident of the old
3658 parser's implementation, whereby the first parameter must have
3659 some declaration specifiers other than just attributes. Thus as
3660 a parameter declaration it is treated as a parenthesized
3661 parameter named x, and as an abstract declarator it is
3662 rejected.
3664 ??? Also following the old parser, attributes inside an empty
3665 parameter list are ignored, making it a list not yielding a
3666 prototype, rather than giving an error or making it have one
3667 parameter with implicit type int.
3669 ??? Also following the old parser, typedef names may be
3670 redeclared in declarators, but not Objective-C class names. */
3672 if (kind != C_DTR_ABSTRACT
3673 && c_parser_next_token_is (parser, CPP_NAME)
3674 && ((type_seen_p
3675 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3676 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3677 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3679 struct c_declarator *inner
3680 = build_id_declarator (c_parser_peek_token (parser)->value);
3681 *seen_id = true;
3682 inner->id_loc = c_parser_peek_token (parser)->location;
3683 c_parser_consume_token (parser);
3684 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3687 if (kind != C_DTR_NORMAL
3688 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3690 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3691 inner->id_loc = c_parser_peek_token (parser)->location;
3692 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3695 /* Either we are at the end of an abstract declarator, or we have
3696 parentheses. */
3698 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3700 tree attrs;
3701 struct c_declarator *inner;
3702 c_parser_consume_token (parser);
3703 attrs = c_parser_attributes (parser);
3704 if (kind != C_DTR_NORMAL
3705 && (c_parser_next_token_starts_declspecs (parser)
3706 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3708 struct c_arg_info *args
3709 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3710 attrs);
3711 if (args == NULL)
3712 return NULL;
3713 else
3715 inner
3716 = build_function_declarator (args,
3717 build_id_declarator (NULL_TREE));
3718 return c_parser_direct_declarator_inner (parser, *seen_id,
3719 inner);
3722 /* A parenthesized declarator. */
3723 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3724 if (inner != NULL && attrs != NULL)
3725 inner = build_attrs_declarator (attrs, inner);
3726 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3728 c_parser_consume_token (parser);
3729 if (inner == NULL)
3730 return NULL;
3731 else
3732 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3734 else
3736 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3737 "expected %<)%>");
3738 return NULL;
3741 else
3743 if (kind == C_DTR_NORMAL)
3745 c_parser_error (parser, "expected identifier or %<(%>");
3746 return NULL;
3748 else
3749 return build_id_declarator (NULL_TREE);
3753 /* Parse part of a direct declarator or direct abstract declarator,
3754 given that some (in INNER) has already been parsed; ID_PRESENT is
3755 true if an identifier is present, false for an abstract
3756 declarator. */
3758 static struct c_declarator *
3759 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3760 struct c_declarator *inner)
3762 /* Parse a sequence of array declarators and parameter lists. */
3763 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3765 location_t brace_loc = c_parser_peek_token (parser)->location;
3766 struct c_declarator *declarator;
3767 struct c_declspecs *quals_attrs = build_null_declspecs ();
3768 bool static_seen;
3769 bool star_seen;
3770 struct c_expr dimen;
3771 dimen.value = NULL_TREE;
3772 dimen.original_code = ERROR_MARK;
3773 dimen.original_type = NULL_TREE;
3774 c_parser_consume_token (parser);
3775 c_parser_declspecs (parser, quals_attrs, false, false, true,
3776 false, false, cla_prefer_id);
3777 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3778 if (static_seen)
3779 c_parser_consume_token (parser);
3780 if (static_seen && !quals_attrs->declspecs_seen_p)
3781 c_parser_declspecs (parser, quals_attrs, false, false, true,
3782 false, false, cla_prefer_id);
3783 if (!quals_attrs->declspecs_seen_p)
3784 quals_attrs = NULL;
3785 /* If "static" is present, there must be an array dimension.
3786 Otherwise, there may be a dimension, "*", or no
3787 dimension. */
3788 if (static_seen)
3790 star_seen = false;
3791 dimen = c_parser_expr_no_commas (parser, NULL);
3793 else
3795 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3797 dimen.value = NULL_TREE;
3798 star_seen = false;
3800 else if (c_parser_next_token_is (parser, CPP_MULT))
3802 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3804 dimen.value = NULL_TREE;
3805 star_seen = true;
3806 c_parser_consume_token (parser);
3808 else
3810 star_seen = false;
3811 dimen = c_parser_expr_no_commas (parser, NULL);
3814 else
3816 star_seen = false;
3817 dimen = c_parser_expr_no_commas (parser, NULL);
3820 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3821 c_parser_consume_token (parser);
3822 else
3824 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3825 "expected %<]%>");
3826 return NULL;
3828 if (dimen.value)
3829 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3830 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3831 static_seen, star_seen);
3832 if (declarator == NULL)
3833 return NULL;
3834 inner = set_array_declarator_inner (declarator, inner);
3835 return c_parser_direct_declarator_inner (parser, id_present, inner);
3837 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3839 tree attrs;
3840 struct c_arg_info *args;
3841 c_parser_consume_token (parser);
3842 attrs = c_parser_attributes (parser);
3843 args = c_parser_parms_declarator (parser, id_present, attrs);
3844 if (args == NULL)
3845 return NULL;
3846 else
3848 inner = build_function_declarator (args, inner);
3849 return c_parser_direct_declarator_inner (parser, id_present, inner);
3852 return inner;
3855 /* Parse a parameter list or identifier list, including the closing
3856 parenthesis but not the opening one. ATTRS are the attributes at
3857 the start of the list. ID_LIST_OK is true if an identifier list is
3858 acceptable; such a list must not have attributes at the start. */
3860 static struct c_arg_info *
3861 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3863 push_scope ();
3864 declare_parm_level ();
3865 /* If the list starts with an identifier, it is an identifier list.
3866 Otherwise, it is either a prototype list or an empty list. */
3867 if (id_list_ok
3868 && !attrs
3869 && c_parser_next_token_is (parser, CPP_NAME)
3870 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3872 /* Look ahead to detect typos in type names. */
3873 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3874 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3875 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3876 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3877 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3879 tree list = NULL_TREE, *nextp = &list;
3880 while (c_parser_next_token_is (parser, CPP_NAME)
3881 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3883 *nextp = build_tree_list (NULL_TREE,
3884 c_parser_peek_token (parser)->value);
3885 nextp = & TREE_CHAIN (*nextp);
3886 c_parser_consume_token (parser);
3887 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3888 break;
3889 c_parser_consume_token (parser);
3890 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3892 c_parser_error (parser, "expected identifier");
3893 break;
3896 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3898 struct c_arg_info *ret = build_arg_info ();
3899 ret->types = list;
3900 c_parser_consume_token (parser);
3901 pop_scope ();
3902 return ret;
3904 else
3906 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3907 "expected %<)%>");
3908 pop_scope ();
3909 return NULL;
3912 else
3914 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3915 NULL);
3916 pop_scope ();
3917 return ret;
3921 /* Parse a parameter list (possibly empty), including the closing
3922 parenthesis but not the opening one. ATTRS are the attributes at
3923 the start of the list. EXPR is NULL or an expression that needs to
3924 be evaluated for the side effects of array size expressions in the
3925 parameters. */
3927 static struct c_arg_info *
3928 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3930 bool bad_parm = false;
3932 /* ??? Following the old parser, forward parameter declarations may
3933 use abstract declarators, and if no real parameter declarations
3934 follow the forward declarations then this is not diagnosed. Also
3935 note as above that attributes are ignored as the only contents of
3936 the parentheses, or as the only contents after forward
3937 declarations. */
3938 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3940 struct c_arg_info *ret = build_arg_info ();
3941 c_parser_consume_token (parser);
3942 return ret;
3944 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3946 struct c_arg_info *ret = build_arg_info ();
3948 if (flag_allow_parameterless_variadic_functions)
3950 /* F (...) is allowed. */
3951 ret->types = NULL_TREE;
3953 else
3955 /* Suppress -Wold-style-definition for this case. */
3956 ret->types = error_mark_node;
3957 error_at (c_parser_peek_token (parser)->location,
3958 "ISO C requires a named argument before %<...%>");
3960 c_parser_consume_token (parser);
3961 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3963 c_parser_consume_token (parser);
3964 return ret;
3966 else
3968 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3969 "expected %<)%>");
3970 return NULL;
3973 /* Nonempty list of parameters, either terminated with semicolon
3974 (forward declarations; recurse) or with close parenthesis (normal
3975 function) or with ", ... )" (variadic function). */
3976 while (true)
3978 /* Parse a parameter. */
3979 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3980 attrs = NULL_TREE;
3981 if (parm == NULL)
3982 bad_parm = true;
3983 else
3984 push_parm_decl (parm, &expr);
3985 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3987 tree new_attrs;
3988 c_parser_consume_token (parser);
3989 mark_forward_parm_decls ();
3990 new_attrs = c_parser_attributes (parser);
3991 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3993 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3995 c_parser_consume_token (parser);
3996 if (bad_parm)
3997 return NULL;
3998 else
3999 return get_parm_info (false, expr);
4001 if (!c_parser_require (parser, CPP_COMMA,
4002 "expected %<;%>, %<,%> or %<)%>",
4003 UNKNOWN_LOCATION, false))
4005 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4006 return NULL;
4008 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4010 c_parser_consume_token (parser);
4011 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4013 c_parser_consume_token (parser);
4014 if (bad_parm)
4015 return NULL;
4016 else
4017 return get_parm_info (true, expr);
4019 else
4021 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4022 "expected %<)%>");
4023 return NULL;
4029 /* Parse a parameter declaration. ATTRS are the attributes at the
4030 start of the declaration if it is the first parameter. */
4032 static struct c_parm *
4033 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4035 struct c_declspecs *specs;
4036 struct c_declarator *declarator;
4037 tree prefix_attrs;
4038 tree postfix_attrs = NULL_TREE;
4039 bool dummy = false;
4041 /* Accept #pragmas between parameter declarations. */
4042 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4043 c_parser_pragma (parser, pragma_param, NULL);
4045 if (!c_parser_next_token_starts_declspecs (parser))
4047 c_token *token = c_parser_peek_token (parser);
4048 if (parser->error)
4049 return NULL;
4050 c_parser_set_source_position_from_token (token);
4051 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4053 auto_diagnostic_group d;
4054 name_hint hint = lookup_name_fuzzy (token->value,
4055 FUZZY_LOOKUP_TYPENAME,
4056 token->location);
4057 if (hint)
4059 gcc_rich_location richloc (token->location);
4060 richloc.add_fixit_replace (hint.suggestion ());
4061 error_at (&richloc,
4062 "unknown type name %qE; did you mean %qs?",
4063 token->value, hint.suggestion ());
4065 else
4066 error_at (token->location, "unknown type name %qE", token->value);
4067 parser->error = true;
4069 /* ??? In some Objective-C cases '...' isn't applicable so there
4070 should be a different message. */
4071 else
4072 c_parser_error (parser,
4073 "expected declaration specifiers or %<...%>");
4074 c_parser_skip_to_end_of_parameter (parser);
4075 return NULL;
4078 location_t start_loc = c_parser_peek_token (parser)->location;
4080 specs = build_null_declspecs ();
4081 if (attrs)
4083 declspecs_add_attrs (input_location, specs, attrs);
4084 attrs = NULL_TREE;
4086 c_parser_declspecs (parser, specs, true, true, true, true, false,
4087 cla_nonabstract_decl);
4088 finish_declspecs (specs);
4089 pending_xref_error ();
4090 prefix_attrs = specs->attrs;
4091 specs->attrs = NULL_TREE;
4092 declarator = c_parser_declarator (parser,
4093 specs->typespec_kind != ctsk_none,
4094 C_DTR_PARM, &dummy);
4095 if (declarator == NULL)
4097 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4098 return NULL;
4100 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4101 postfix_attrs = c_parser_attributes (parser);
4103 /* Generate a location for the parameter, ranging from the start of the
4104 initial token to the end of the final token.
4106 If we have a identifier, then use it for the caret location, e.g.
4108 extern int callee (int one, int (*two)(int, int), float three);
4109 ~~~~~~^~~~~~~~~~~~~~
4111 otherwise, reuse the start location for the caret location e.g.:
4113 extern int callee (int one, int (*)(int, int), float three);
4114 ^~~~~~~~~~~~~~~~~
4116 location_t end_loc = parser->last_token_location;
4118 /* Find any cdk_id declarator; determine if we have an identifier. */
4119 c_declarator *id_declarator = declarator;
4120 while (id_declarator && id_declarator->kind != cdk_id)
4121 id_declarator = id_declarator->declarator;
4122 location_t caret_loc = (id_declarator->u.id
4123 ? id_declarator->id_loc
4124 : start_loc);
4125 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4127 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4128 declarator, param_loc);
4131 /* Parse a string literal in an asm expression. It should not be
4132 translated, and wide string literals are an error although
4133 permitted by the syntax. This is a GNU extension.
4135 asm-string-literal:
4136 string-literal
4138 ??? At present, following the old parser, the caller needs to have
4139 set lex_untranslated_string to 1. It would be better to follow the
4140 C++ parser rather than using this kludge. */
4142 static tree
4143 c_parser_asm_string_literal (c_parser *parser)
4145 tree str;
4146 int save_flag = warn_overlength_strings;
4147 warn_overlength_strings = 0;
4148 if (c_parser_next_token_is (parser, CPP_STRING))
4150 str = c_parser_peek_token (parser)->value;
4151 c_parser_consume_token (parser);
4153 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4155 error_at (c_parser_peek_token (parser)->location,
4156 "wide string literal in %<asm%>");
4157 str = build_string (1, "");
4158 c_parser_consume_token (parser);
4160 else
4162 c_parser_error (parser, "expected string literal");
4163 str = NULL_TREE;
4165 warn_overlength_strings = save_flag;
4166 return str;
4169 /* Parse a simple asm expression. This is used in restricted
4170 contexts, where a full expression with inputs and outputs does not
4171 make sense. This is a GNU extension.
4173 simple-asm-expr:
4174 asm ( asm-string-literal )
4177 static tree
4178 c_parser_simple_asm_expr (c_parser *parser)
4180 tree str;
4181 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4182 /* ??? Follow the C++ parser rather than using the
4183 lex_untranslated_string kludge. */
4184 parser->lex_untranslated_string = true;
4185 c_parser_consume_token (parser);
4186 matching_parens parens;
4187 if (!parens.require_open (parser))
4189 parser->lex_untranslated_string = false;
4190 return NULL_TREE;
4192 str = c_parser_asm_string_literal (parser);
4193 parser->lex_untranslated_string = false;
4194 if (!parens.require_close (parser))
4196 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4197 return NULL_TREE;
4199 return str;
4202 static tree
4203 c_parser_attribute_any_word (c_parser *parser)
4205 tree attr_name = NULL_TREE;
4207 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4209 /* ??? See comment above about what keywords are accepted here. */
4210 bool ok;
4211 switch (c_parser_peek_token (parser)->keyword)
4213 case RID_STATIC:
4214 case RID_UNSIGNED:
4215 case RID_LONG:
4216 case RID_CONST:
4217 case RID_EXTERN:
4218 case RID_REGISTER:
4219 case RID_TYPEDEF:
4220 case RID_SHORT:
4221 case RID_INLINE:
4222 case RID_NORETURN:
4223 case RID_VOLATILE:
4224 case RID_SIGNED:
4225 case RID_AUTO:
4226 case RID_RESTRICT:
4227 case RID_COMPLEX:
4228 case RID_THREAD:
4229 case RID_INT:
4230 case RID_CHAR:
4231 case RID_FLOAT:
4232 case RID_DOUBLE:
4233 case RID_VOID:
4234 case RID_DFLOAT32:
4235 case RID_DFLOAT64:
4236 case RID_DFLOAT128:
4237 CASE_RID_FLOATN_NX:
4238 case RID_BOOL:
4239 case RID_FRACT:
4240 case RID_ACCUM:
4241 case RID_SAT:
4242 case RID_TRANSACTION_ATOMIC:
4243 case RID_TRANSACTION_CANCEL:
4244 case RID_ATOMIC:
4245 case RID_AUTO_TYPE:
4246 case RID_INT_N_0:
4247 case RID_INT_N_1:
4248 case RID_INT_N_2:
4249 case RID_INT_N_3:
4250 ok = true;
4251 break;
4252 default:
4253 ok = false;
4254 break;
4256 if (!ok)
4257 return NULL_TREE;
4259 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4260 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4262 else if (c_parser_next_token_is (parser, CPP_NAME))
4263 attr_name = c_parser_peek_token (parser)->value;
4265 return attr_name;
4268 /* Parse (possibly empty) attributes. This is a GNU extension.
4270 attributes:
4271 empty
4272 attributes attribute
4274 attribute:
4275 __attribute__ ( ( attribute-list ) )
4277 attribute-list:
4278 attrib
4279 attribute_list , attrib
4281 attrib:
4282 empty
4283 any-word
4284 any-word ( identifier )
4285 any-word ( identifier , nonempty-expr-list )
4286 any-word ( expr-list )
4288 where the "identifier" must not be declared as a type, and
4289 "any-word" may be any identifier (including one declared as a
4290 type), a reserved word storage class specifier, type specifier or
4291 type qualifier. ??? This still leaves out most reserved keywords
4292 (following the old parser), shouldn't we include them, and why not
4293 allow identifiers declared as types to start the arguments? */
4295 static tree
4296 c_parser_attributes (c_parser *parser)
4298 tree attrs = NULL_TREE;
4299 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4301 /* ??? Follow the C++ parser rather than using the
4302 lex_untranslated_string kludge. */
4303 parser->lex_untranslated_string = true;
4304 /* Consume the `__attribute__' keyword. */
4305 c_parser_consume_token (parser);
4306 /* Look for the two `(' tokens. */
4307 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4309 parser->lex_untranslated_string = false;
4310 return attrs;
4312 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4314 parser->lex_untranslated_string = false;
4315 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4316 return attrs;
4318 /* Parse the attribute list. */
4319 while (c_parser_next_token_is (parser, CPP_COMMA)
4320 || c_parser_next_token_is (parser, CPP_NAME)
4321 || c_parser_next_token_is (parser, CPP_KEYWORD))
4323 tree attr, attr_name, attr_args;
4324 vec<tree, va_gc> *expr_list;
4325 if (c_parser_next_token_is (parser, CPP_COMMA))
4327 c_parser_consume_token (parser);
4328 continue;
4331 attr_name = c_parser_attribute_any_word (parser);
4332 if (attr_name == NULL)
4333 break;
4334 attr_name = canonicalize_attr_name (attr_name);
4335 c_parser_consume_token (parser);
4336 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4338 attr = build_tree_list (attr_name, NULL_TREE);
4339 /* Add this attribute to the list. */
4340 attrs = chainon (attrs, attr);
4341 /* If the next token isn't a comma, we're done. */
4342 if (!c_parser_next_token_is (parser, CPP_COMMA))
4343 break;
4344 continue;
4346 c_parser_consume_token (parser);
4347 /* Parse the attribute contents. If they start with an
4348 identifier which is followed by a comma or close
4349 parenthesis, then the arguments start with that
4350 identifier; otherwise they are an expression list.
4351 In objective-c the identifier may be a classname. */
4352 if (c_parser_next_token_is (parser, CPP_NAME)
4353 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4354 || (c_dialect_objc ()
4355 && c_parser_peek_token (parser)->id_kind
4356 == C_ID_CLASSNAME))
4357 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4358 || (c_parser_peek_2nd_token (parser)->type
4359 == CPP_CLOSE_PAREN))
4360 && (attribute_takes_identifier_p (attr_name)
4361 || (c_dialect_objc ()
4362 && c_parser_peek_token (parser)->id_kind
4363 == C_ID_CLASSNAME)))
4365 tree arg1 = c_parser_peek_token (parser)->value;
4366 c_parser_consume_token (parser);
4367 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4368 attr_args = build_tree_list (NULL_TREE, arg1);
4369 else
4371 tree tree_list;
4372 c_parser_consume_token (parser);
4373 expr_list = c_parser_expr_list (parser, false, true,
4374 NULL, NULL, NULL, NULL);
4375 tree_list = build_tree_list_vec (expr_list);
4376 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4377 release_tree_vector (expr_list);
4380 else
4382 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4383 attr_args = NULL_TREE;
4384 else
4386 expr_list = c_parser_expr_list (parser, false, true,
4387 NULL, NULL, NULL, NULL);
4388 attr_args = build_tree_list_vec (expr_list);
4389 release_tree_vector (expr_list);
4393 attr = build_tree_list (attr_name, attr_args);
4394 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4395 c_parser_consume_token (parser);
4396 else
4398 parser->lex_untranslated_string = false;
4399 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4400 "expected %<)%>");
4401 return attrs;
4403 /* Add this attribute to the list. */
4404 attrs = chainon (attrs, attr);
4405 /* If the next token isn't a comma, we're done. */
4406 if (!c_parser_next_token_is (parser, CPP_COMMA))
4407 break;
4409 /* Look for the two `)' tokens. */
4410 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4411 c_parser_consume_token (parser);
4412 else
4414 parser->lex_untranslated_string = false;
4415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4416 "expected %<)%>");
4417 return attrs;
4419 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4420 c_parser_consume_token (parser);
4421 else
4423 parser->lex_untranslated_string = false;
4424 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4425 "expected %<)%>");
4426 return attrs;
4428 parser->lex_untranslated_string = false;
4431 return attrs;
4434 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4435 says whether alignment specifiers are OK (only in cases that might
4436 be the type name of a compound literal).
4438 type-name:
4439 specifier-qualifier-list abstract-declarator[opt]
4442 struct c_type_name *
4443 c_parser_type_name (c_parser *parser, bool alignas_ok)
4445 struct c_declspecs *specs = build_null_declspecs ();
4446 struct c_declarator *declarator;
4447 struct c_type_name *ret;
4448 bool dummy = false;
4449 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4450 cla_prefer_type);
4451 if (!specs->declspecs_seen_p)
4453 c_parser_error (parser, "expected specifier-qualifier-list");
4454 return NULL;
4456 if (specs->type != error_mark_node)
4458 pending_xref_error ();
4459 finish_declspecs (specs);
4461 declarator = c_parser_declarator (parser,
4462 specs->typespec_kind != ctsk_none,
4463 C_DTR_ABSTRACT, &dummy);
4464 if (declarator == NULL)
4465 return NULL;
4466 ret = XOBNEW (&parser_obstack, struct c_type_name);
4467 ret->specs = specs;
4468 ret->declarator = declarator;
4469 return ret;
4472 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4474 initializer:
4475 assignment-expression
4476 { initializer-list }
4477 { initializer-list , }
4479 initializer-list:
4480 designation[opt] initializer
4481 initializer-list , designation[opt] initializer
4483 designation:
4484 designator-list =
4486 designator-list:
4487 designator
4488 designator-list designator
4490 designator:
4491 array-designator
4492 . identifier
4494 array-designator:
4495 [ constant-expression ]
4497 GNU extensions:
4499 initializer:
4502 designation:
4503 array-designator
4504 identifier :
4506 array-designator:
4507 [ constant-expression ... constant-expression ]
4509 Any expression without commas is accepted in the syntax for the
4510 constant-expressions, with non-constant expressions rejected later.
4512 This function is only used for top-level initializers; for nested
4513 ones, see c_parser_initval. */
4515 static struct c_expr
4516 c_parser_initializer (c_parser *parser)
4518 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4519 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4520 else
4522 struct c_expr ret;
4523 location_t loc = c_parser_peek_token (parser)->location;
4524 ret = c_parser_expr_no_commas (parser, NULL);
4525 if (TREE_CODE (ret.value) != STRING_CST
4526 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4527 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4528 return ret;
4532 /* The location of the last comma within the current initializer list,
4533 or UNKNOWN_LOCATION if not within one. */
4535 location_t last_init_list_comma;
4537 /* Parse a braced initializer list. TYPE is the type specified for a
4538 compound literal, and NULL_TREE for other initializers and for
4539 nested braced lists. NESTED_P is true for nested braced lists,
4540 false for the list of a compound literal or the list that is the
4541 top-level initializer in a declaration. */
4543 static struct c_expr
4544 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4545 struct obstack *outer_obstack)
4547 struct c_expr ret;
4548 struct obstack braced_init_obstack;
4549 location_t brace_loc = c_parser_peek_token (parser)->location;
4550 gcc_obstack_init (&braced_init_obstack);
4551 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4552 matching_braces braces;
4553 braces.consume_open (parser);
4554 if (nested_p)
4556 finish_implicit_inits (brace_loc, outer_obstack);
4557 push_init_level (brace_loc, 0, &braced_init_obstack);
4559 else
4560 really_start_incremental_init (type);
4561 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4563 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4565 else
4567 /* Parse a non-empty initializer list, possibly with a trailing
4568 comma. */
4569 while (true)
4571 c_parser_initelt (parser, &braced_init_obstack);
4572 if (parser->error)
4573 break;
4574 if (c_parser_next_token_is (parser, CPP_COMMA))
4576 last_init_list_comma = c_parser_peek_token (parser)->location;
4577 c_parser_consume_token (parser);
4579 else
4580 break;
4581 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4582 break;
4585 c_token *next_tok = c_parser_peek_token (parser);
4586 if (next_tok->type != CPP_CLOSE_BRACE)
4588 ret.set_error ();
4589 ret.original_code = ERROR_MARK;
4590 ret.original_type = NULL;
4591 braces.skip_until_found_close (parser);
4592 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4593 obstack_free (&braced_init_obstack, NULL);
4594 return ret;
4596 location_t close_loc = next_tok->location;
4597 c_parser_consume_token (parser);
4598 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4599 obstack_free (&braced_init_obstack, NULL);
4600 set_c_expr_source_range (&ret, brace_loc, close_loc);
4601 return ret;
4604 /* Parse a nested initializer, including designators. */
4606 static void
4607 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4609 /* Parse any designator or designator list. A single array
4610 designator may have the subsequent "=" omitted in GNU C, but a
4611 longer list or a structure member designator may not. */
4612 if (c_parser_next_token_is (parser, CPP_NAME)
4613 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4615 /* Old-style structure member designator. */
4616 set_init_label (c_parser_peek_token (parser)->location,
4617 c_parser_peek_token (parser)->value,
4618 c_parser_peek_token (parser)->location,
4619 braced_init_obstack);
4620 /* Use the colon as the error location. */
4621 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4622 "obsolete use of designated initializer with %<:%>");
4623 c_parser_consume_token (parser);
4624 c_parser_consume_token (parser);
4626 else
4628 /* des_seen is 0 if there have been no designators, 1 if there
4629 has been a single array designator and 2 otherwise. */
4630 int des_seen = 0;
4631 /* Location of a designator. */
4632 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4633 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4634 || c_parser_next_token_is (parser, CPP_DOT))
4636 int des_prev = des_seen;
4637 if (!des_seen)
4638 des_loc = c_parser_peek_token (parser)->location;
4639 if (des_seen < 2)
4640 des_seen++;
4641 if (c_parser_next_token_is (parser, CPP_DOT))
4643 des_seen = 2;
4644 c_parser_consume_token (parser);
4645 if (c_parser_next_token_is (parser, CPP_NAME))
4647 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4648 c_parser_peek_token (parser)->location,
4649 braced_init_obstack);
4650 c_parser_consume_token (parser);
4652 else
4654 struct c_expr init;
4655 init.set_error ();
4656 init.original_code = ERROR_MARK;
4657 init.original_type = NULL;
4658 c_parser_error (parser, "expected identifier");
4659 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4660 process_init_element (input_location, init, false,
4661 braced_init_obstack);
4662 return;
4665 else
4667 tree first, second;
4668 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4669 location_t array_index_loc = UNKNOWN_LOCATION;
4670 /* ??? Following the old parser, [ objc-receiver
4671 objc-message-args ] is accepted as an initializer,
4672 being distinguished from a designator by what follows
4673 the first assignment expression inside the square
4674 brackets, but after a first array designator a
4675 subsequent square bracket is for Objective-C taken to
4676 start an expression, using the obsolete form of
4677 designated initializer without '=', rather than
4678 possibly being a second level of designation: in LALR
4679 terms, the '[' is shifted rather than reducing
4680 designator to designator-list. */
4681 if (des_prev == 1 && c_dialect_objc ())
4683 des_seen = des_prev;
4684 break;
4686 if (des_prev == 0 && c_dialect_objc ())
4688 /* This might be an array designator or an
4689 Objective-C message expression. If the former,
4690 continue parsing here; if the latter, parse the
4691 remainder of the initializer given the starting
4692 primary-expression. ??? It might make sense to
4693 distinguish when des_prev == 1 as well; see
4694 previous comment. */
4695 tree rec, args;
4696 struct c_expr mexpr;
4697 c_parser_consume_token (parser);
4698 if (c_parser_peek_token (parser)->type == CPP_NAME
4699 && ((c_parser_peek_token (parser)->id_kind
4700 == C_ID_TYPENAME)
4701 || (c_parser_peek_token (parser)->id_kind
4702 == C_ID_CLASSNAME)))
4704 /* Type name receiver. */
4705 tree id = c_parser_peek_token (parser)->value;
4706 c_parser_consume_token (parser);
4707 rec = objc_get_class_reference (id);
4708 goto parse_message_args;
4710 first = c_parser_expr_no_commas (parser, NULL).value;
4711 mark_exp_read (first);
4712 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4713 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4714 goto array_desig_after_first;
4715 /* Expression receiver. So far only one part
4716 without commas has been parsed; there might be
4717 more of the expression. */
4718 rec = first;
4719 while (c_parser_next_token_is (parser, CPP_COMMA))
4721 struct c_expr next;
4722 location_t comma_loc, exp_loc;
4723 comma_loc = c_parser_peek_token (parser)->location;
4724 c_parser_consume_token (parser);
4725 exp_loc = c_parser_peek_token (parser)->location;
4726 next = c_parser_expr_no_commas (parser, NULL);
4727 next = convert_lvalue_to_rvalue (exp_loc, next,
4728 true, true);
4729 rec = build_compound_expr (comma_loc, rec, next.value);
4731 parse_message_args:
4732 /* Now parse the objc-message-args. */
4733 args = c_parser_objc_message_args (parser);
4734 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4735 "expected %<]%>");
4736 mexpr.value
4737 = objc_build_message_expr (rec, args);
4738 mexpr.original_code = ERROR_MARK;
4739 mexpr.original_type = NULL;
4740 /* Now parse and process the remainder of the
4741 initializer, starting with this message
4742 expression as a primary-expression. */
4743 c_parser_initval (parser, &mexpr, braced_init_obstack);
4744 return;
4746 c_parser_consume_token (parser);
4747 array_index_loc = c_parser_peek_token (parser)->location;
4748 first = c_parser_expr_no_commas (parser, NULL).value;
4749 mark_exp_read (first);
4750 array_desig_after_first:
4751 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4753 ellipsis_loc = c_parser_peek_token (parser)->location;
4754 c_parser_consume_token (parser);
4755 second = c_parser_expr_no_commas (parser, NULL).value;
4756 mark_exp_read (second);
4758 else
4759 second = NULL_TREE;
4760 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4762 c_parser_consume_token (parser);
4763 set_init_index (array_index_loc, first, second,
4764 braced_init_obstack);
4765 if (second)
4766 pedwarn (ellipsis_loc, OPT_Wpedantic,
4767 "ISO C forbids specifying range of elements to initialize");
4769 else
4770 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4771 "expected %<]%>");
4774 if (des_seen >= 1)
4776 if (c_parser_next_token_is (parser, CPP_EQ))
4778 pedwarn_c90 (des_loc, OPT_Wpedantic,
4779 "ISO C90 forbids specifying subobject "
4780 "to initialize");
4781 c_parser_consume_token (parser);
4783 else
4785 if (des_seen == 1)
4786 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4787 "obsolete use of designated initializer without %<=%>");
4788 else
4790 struct c_expr init;
4791 init.set_error ();
4792 init.original_code = ERROR_MARK;
4793 init.original_type = NULL;
4794 c_parser_error (parser, "expected %<=%>");
4795 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4796 process_init_element (input_location, init, false,
4797 braced_init_obstack);
4798 return;
4803 c_parser_initval (parser, NULL, braced_init_obstack);
4806 /* Parse a nested initializer; as c_parser_initializer but parses
4807 initializers within braced lists, after any designators have been
4808 applied. If AFTER is not NULL then it is an Objective-C message
4809 expression which is the primary-expression starting the
4810 initializer. */
4812 static void
4813 c_parser_initval (c_parser *parser, struct c_expr *after,
4814 struct obstack * braced_init_obstack)
4816 struct c_expr init;
4817 gcc_assert (!after || c_dialect_objc ());
4818 location_t loc = c_parser_peek_token (parser)->location;
4820 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4821 init = c_parser_braced_init (parser, NULL_TREE, true,
4822 braced_init_obstack);
4823 else
4825 init = c_parser_expr_no_commas (parser, after);
4826 if (init.value != NULL_TREE
4827 && TREE_CODE (init.value) != STRING_CST
4828 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4829 init = convert_lvalue_to_rvalue (loc, init, true, true);
4831 process_init_element (loc, init, false, braced_init_obstack);
4834 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4835 C99 6.8.2, C11 6.8.2).
4837 compound-statement:
4838 { block-item-list[opt] }
4839 { label-declarations block-item-list }
4841 block-item-list:
4842 block-item
4843 block-item-list block-item
4845 block-item:
4846 nested-declaration
4847 statement
4849 nested-declaration:
4850 declaration
4852 GNU extensions:
4854 compound-statement:
4855 { label-declarations block-item-list }
4857 nested-declaration:
4858 __extension__ nested-declaration
4859 nested-function-definition
4861 label-declarations:
4862 label-declaration
4863 label-declarations label-declaration
4865 label-declaration:
4866 __label__ identifier-list ;
4868 Allowing the mixing of declarations and code is new in C99. The
4869 GNU syntax also permits (not shown above) labels at the end of
4870 compound statements, which yield an error. We don't allow labels
4871 on declarations; this might seem like a natural extension, but
4872 there would be a conflict between attributes on the label and
4873 prefix attributes on the declaration. ??? The syntax follows the
4874 old parser in requiring something after label declarations.
4875 Although they are erroneous if the labels declared aren't defined,
4876 is it useful for the syntax to be this way?
4878 OpenACC:
4880 block-item:
4881 openacc-directive
4883 openacc-directive:
4884 update-directive
4886 OpenMP:
4888 block-item:
4889 openmp-directive
4891 openmp-directive:
4892 barrier-directive
4893 flush-directive
4894 taskwait-directive
4895 taskyield-directive
4896 cancel-directive
4897 cancellation-point-directive */
4899 static tree
4900 c_parser_compound_statement (c_parser *parser)
4902 tree stmt;
4903 location_t brace_loc;
4904 brace_loc = c_parser_peek_token (parser)->location;
4905 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4907 /* Ensure a scope is entered and left anyway to avoid confusion
4908 if we have just prepared to enter a function body. */
4909 stmt = c_begin_compound_stmt (true);
4910 c_end_compound_stmt (brace_loc, stmt, true);
4911 return error_mark_node;
4913 stmt = c_begin_compound_stmt (true);
4914 c_parser_compound_statement_nostart (parser);
4916 return c_end_compound_stmt (brace_loc, stmt, true);
4919 /* Parse a compound statement except for the opening brace. This is
4920 used for parsing both compound statements and statement expressions
4921 (which follow different paths to handling the opening). */
4923 static void
4924 c_parser_compound_statement_nostart (c_parser *parser)
4926 bool last_stmt = false;
4927 bool last_label = false;
4928 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4929 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4930 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4932 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4933 c_parser_consume_token (parser);
4934 return;
4936 mark_valid_location_for_stdc_pragma (true);
4937 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4939 /* Read zero or more forward-declarations for labels that nested
4940 functions can jump to. */
4941 mark_valid_location_for_stdc_pragma (false);
4942 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4944 label_loc = c_parser_peek_token (parser)->location;
4945 c_parser_consume_token (parser);
4946 /* Any identifiers, including those declared as type names,
4947 are OK here. */
4948 while (true)
4950 tree label;
4951 if (c_parser_next_token_is_not (parser, CPP_NAME))
4953 c_parser_error (parser, "expected identifier");
4954 break;
4956 label
4957 = declare_label (c_parser_peek_token (parser)->value);
4958 C_DECLARED_LABEL_FLAG (label) = 1;
4959 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4960 c_parser_consume_token (parser);
4961 if (c_parser_next_token_is (parser, CPP_COMMA))
4962 c_parser_consume_token (parser);
4963 else
4964 break;
4966 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4968 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4970 /* We must now have at least one statement, label or declaration. */
4971 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4973 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4974 c_parser_error (parser, "expected declaration or statement");
4975 c_parser_consume_token (parser);
4976 return;
4978 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4980 location_t loc = c_parser_peek_token (parser)->location;
4981 loc = expansion_point_location_if_in_system_header (loc);
4982 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4983 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4984 || (c_parser_next_token_is (parser, CPP_NAME)
4985 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4987 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4988 label_loc = c_parser_peek_2nd_token (parser)->location;
4989 else
4990 label_loc = c_parser_peek_token (parser)->location;
4991 last_label = true;
4992 last_stmt = false;
4993 mark_valid_location_for_stdc_pragma (false);
4994 c_parser_label (parser);
4996 else if (!last_label
4997 && c_parser_next_tokens_start_declaration (parser))
4999 last_label = false;
5000 mark_valid_location_for_stdc_pragma (false);
5001 bool fallthru_attr_p = false;
5002 c_parser_declaration_or_fndef (parser, true, true, true, true,
5003 true, NULL, vNULL, NULL,
5004 &fallthru_attr_p);
5005 if (last_stmt && !fallthru_attr_p)
5006 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5007 "ISO C90 forbids mixed declarations and code");
5008 last_stmt = fallthru_attr_p;
5010 else if (!last_label
5011 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5013 /* __extension__ can start a declaration, but is also an
5014 unary operator that can start an expression. Consume all
5015 but the last of a possible series of __extension__ to
5016 determine which. */
5017 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5018 && (c_parser_peek_2nd_token (parser)->keyword
5019 == RID_EXTENSION))
5020 c_parser_consume_token (parser);
5021 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5023 int ext;
5024 ext = disable_extension_diagnostics ();
5025 c_parser_consume_token (parser);
5026 last_label = false;
5027 mark_valid_location_for_stdc_pragma (false);
5028 c_parser_declaration_or_fndef (parser, true, true, true, true,
5029 true, NULL, vNULL);
5030 /* Following the old parser, __extension__ does not
5031 disable this diagnostic. */
5032 restore_extension_diagnostics (ext);
5033 if (last_stmt)
5034 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5035 "ISO C90 forbids mixed declarations and code");
5036 last_stmt = false;
5038 else
5039 goto statement;
5041 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5043 /* External pragmas, and some omp pragmas, are not associated
5044 with regular c code, and so are not to be considered statements
5045 syntactically. This ensures that the user doesn't put them
5046 places that would turn into syntax errors if the directive
5047 were ignored. */
5048 if (c_parser_pragma (parser,
5049 last_label ? pragma_stmt : pragma_compound,
5050 NULL))
5051 last_label = false, last_stmt = true;
5053 else if (c_parser_next_token_is (parser, CPP_EOF))
5055 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5056 c_parser_error (parser, "expected declaration or statement");
5057 return;
5059 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5061 if (parser->in_if_block)
5063 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5064 error_at (loc, "expected %<}%> before %<else%>");
5065 return;
5067 else
5069 error_at (loc, "%<else%> without a previous %<if%>");
5070 c_parser_consume_token (parser);
5071 continue;
5074 else
5076 statement:
5077 last_label = false;
5078 last_stmt = true;
5079 mark_valid_location_for_stdc_pragma (false);
5080 c_parser_statement_after_labels (parser, NULL);
5083 parser->error = false;
5085 if (last_label)
5086 error_at (label_loc, "label at end of compound statement");
5087 c_parser_consume_token (parser);
5088 /* Restore the value we started with. */
5089 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5092 /* Parse all consecutive labels. */
5094 static void
5095 c_parser_all_labels (c_parser *parser)
5097 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5098 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5099 || (c_parser_next_token_is (parser, CPP_NAME)
5100 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5101 c_parser_label (parser);
5104 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5106 label:
5107 identifier : attributes[opt]
5108 case constant-expression :
5109 default :
5111 GNU extensions:
5113 label:
5114 case constant-expression ... constant-expression :
5116 The use of attributes on labels is a GNU extension. The syntax in
5117 GNU C accepts any expressions without commas, non-constant
5118 expressions being rejected later. */
5120 static void
5121 c_parser_label (c_parser *parser)
5123 location_t loc1 = c_parser_peek_token (parser)->location;
5124 tree label = NULL_TREE;
5126 /* Remember whether this case or a user-defined label is allowed to fall
5127 through to. */
5128 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5130 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5132 tree exp1, exp2;
5133 c_parser_consume_token (parser);
5134 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5135 if (c_parser_next_token_is (parser, CPP_COLON))
5137 c_parser_consume_token (parser);
5138 label = do_case (loc1, exp1, NULL_TREE);
5140 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5142 c_parser_consume_token (parser);
5143 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5144 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5145 label = do_case (loc1, exp1, exp2);
5147 else
5148 c_parser_error (parser, "expected %<:%> or %<...%>");
5150 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5152 c_parser_consume_token (parser);
5153 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5154 label = do_case (loc1, NULL_TREE, NULL_TREE);
5156 else
5158 tree name = c_parser_peek_token (parser)->value;
5159 tree tlab;
5160 tree attrs;
5161 location_t loc2 = c_parser_peek_token (parser)->location;
5162 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5163 c_parser_consume_token (parser);
5164 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5165 c_parser_consume_token (parser);
5166 attrs = c_parser_attributes (parser);
5167 tlab = define_label (loc2, name);
5168 if (tlab)
5170 decl_attributes (&tlab, attrs, 0);
5171 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5174 if (label)
5176 if (TREE_CODE (label) == LABEL_EXPR)
5177 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5178 else
5179 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5181 /* Allow '__attribute__((fallthrough));'. */
5182 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5184 location_t loc = c_parser_peek_token (parser)->location;
5185 tree attrs = c_parser_attributes (parser);
5186 if (attribute_fallthrough_p (attrs))
5188 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5190 tree fn = build_call_expr_internal_loc (loc,
5191 IFN_FALLTHROUGH,
5192 void_type_node, 0);
5193 add_stmt (fn);
5195 else
5196 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5197 "not followed by %<;%>");
5199 else if (attrs != NULL_TREE)
5200 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5201 " can be applied to a null statement");
5203 if (c_parser_next_tokens_start_declaration (parser))
5205 error_at (c_parser_peek_token (parser)->location,
5206 "a label can only be part of a statement and "
5207 "a declaration is not a statement");
5208 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5209 /*static_assert_ok*/ true,
5210 /*empty_ok*/ true, /*nested*/ true,
5211 /*start_attr_ok*/ true, NULL,
5212 vNULL);
5217 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5219 statement:
5220 labeled-statement
5221 compound-statement
5222 expression-statement
5223 selection-statement
5224 iteration-statement
5225 jump-statement
5227 labeled-statement:
5228 label statement
5230 expression-statement:
5231 expression[opt] ;
5233 selection-statement:
5234 if-statement
5235 switch-statement
5237 iteration-statement:
5238 while-statement
5239 do-statement
5240 for-statement
5242 jump-statement:
5243 goto identifier ;
5244 continue ;
5245 break ;
5246 return expression[opt] ;
5248 GNU extensions:
5250 statement:
5251 asm-statement
5253 jump-statement:
5254 goto * expression ;
5256 expression-statement:
5257 attributes ;
5259 Objective-C:
5261 statement:
5262 objc-throw-statement
5263 objc-try-catch-statement
5264 objc-synchronized-statement
5266 objc-throw-statement:
5267 @throw expression ;
5268 @throw ;
5270 OpenACC:
5272 statement:
5273 openacc-construct
5275 openacc-construct:
5276 parallel-construct
5277 kernels-construct
5278 data-construct
5279 loop-construct
5281 parallel-construct:
5282 parallel-directive structured-block
5284 kernels-construct:
5285 kernels-directive structured-block
5287 data-construct:
5288 data-directive structured-block
5290 loop-construct:
5291 loop-directive structured-block
5293 OpenMP:
5295 statement:
5296 openmp-construct
5298 openmp-construct:
5299 parallel-construct
5300 for-construct
5301 simd-construct
5302 for-simd-construct
5303 sections-construct
5304 single-construct
5305 parallel-for-construct
5306 parallel-for-simd-construct
5307 parallel-sections-construct
5308 master-construct
5309 critical-construct
5310 atomic-construct
5311 ordered-construct
5313 parallel-construct:
5314 parallel-directive structured-block
5316 for-construct:
5317 for-directive iteration-statement
5319 simd-construct:
5320 simd-directive iteration-statements
5322 for-simd-construct:
5323 for-simd-directive iteration-statements
5325 sections-construct:
5326 sections-directive section-scope
5328 single-construct:
5329 single-directive structured-block
5331 parallel-for-construct:
5332 parallel-for-directive iteration-statement
5334 parallel-for-simd-construct:
5335 parallel-for-simd-directive iteration-statement
5337 parallel-sections-construct:
5338 parallel-sections-directive section-scope
5340 master-construct:
5341 master-directive structured-block
5343 critical-construct:
5344 critical-directive structured-block
5346 atomic-construct:
5347 atomic-directive expression-statement
5349 ordered-construct:
5350 ordered-directive structured-block
5352 Transactional Memory:
5354 statement:
5355 transaction-statement
5356 transaction-cancel-statement
5358 IF_P is used to track whether there's a (possibly labeled) if statement
5359 which is not enclosed in braces and has an else clause. This is used to
5360 implement -Wparentheses. */
5362 static void
5363 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5365 c_parser_all_labels (parser);
5366 if (loc_after_labels)
5367 *loc_after_labels = c_parser_peek_token (parser)->location;
5368 c_parser_statement_after_labels (parser, if_p, NULL);
5371 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5372 of if-else-if conditions.
5374 IF_P is used to track whether there's a (possibly labeled) if statement
5375 which is not enclosed in braces and has an else clause. This is used to
5376 implement -Wparentheses. */
5378 static void
5379 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5380 vec<tree> *chain)
5382 location_t loc = c_parser_peek_token (parser)->location;
5383 tree stmt = NULL_TREE;
5384 bool in_if_block = parser->in_if_block;
5385 parser->in_if_block = false;
5386 if (if_p != NULL)
5387 *if_p = false;
5389 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5390 add_debug_begin_stmt (loc);
5392 switch (c_parser_peek_token (parser)->type)
5394 case CPP_OPEN_BRACE:
5395 add_stmt (c_parser_compound_statement (parser));
5396 break;
5397 case CPP_KEYWORD:
5398 switch (c_parser_peek_token (parser)->keyword)
5400 case RID_IF:
5401 c_parser_if_statement (parser, if_p, chain);
5402 break;
5403 case RID_SWITCH:
5404 c_parser_switch_statement (parser, if_p);
5405 break;
5406 case RID_WHILE:
5407 c_parser_while_statement (parser, false, 0, if_p);
5408 break;
5409 case RID_DO:
5410 c_parser_do_statement (parser, 0, false);
5411 break;
5412 case RID_FOR:
5413 c_parser_for_statement (parser, false, 0, if_p);
5414 break;
5415 case RID_GOTO:
5416 c_parser_consume_token (parser);
5417 if (c_parser_next_token_is (parser, CPP_NAME))
5419 stmt = c_finish_goto_label (loc,
5420 c_parser_peek_token (parser)->value);
5421 c_parser_consume_token (parser);
5423 else if (c_parser_next_token_is (parser, CPP_MULT))
5425 struct c_expr val;
5427 c_parser_consume_token (parser);
5428 val = c_parser_expression (parser);
5429 val = convert_lvalue_to_rvalue (loc, val, false, true);
5430 stmt = c_finish_goto_ptr (loc, val.value);
5432 else
5433 c_parser_error (parser, "expected identifier or %<*%>");
5434 goto expect_semicolon;
5435 case RID_CONTINUE:
5436 c_parser_consume_token (parser);
5437 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5438 goto expect_semicolon;
5439 case RID_BREAK:
5440 c_parser_consume_token (parser);
5441 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5442 goto expect_semicolon;
5443 case RID_RETURN:
5444 c_parser_consume_token (parser);
5445 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5447 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5448 c_parser_consume_token (parser);
5450 else
5452 location_t xloc = c_parser_peek_token (parser)->location;
5453 struct c_expr expr = c_parser_expression_conv (parser);
5454 mark_exp_read (expr.value);
5455 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5456 expr.value, expr.original_type);
5457 goto expect_semicolon;
5459 break;
5460 case RID_ASM:
5461 stmt = c_parser_asm_statement (parser);
5462 break;
5463 case RID_TRANSACTION_ATOMIC:
5464 case RID_TRANSACTION_RELAXED:
5465 stmt = c_parser_transaction (parser,
5466 c_parser_peek_token (parser)->keyword);
5467 break;
5468 case RID_TRANSACTION_CANCEL:
5469 stmt = c_parser_transaction_cancel (parser);
5470 goto expect_semicolon;
5471 case RID_AT_THROW:
5472 gcc_assert (c_dialect_objc ());
5473 c_parser_consume_token (parser);
5474 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5476 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5477 c_parser_consume_token (parser);
5479 else
5481 struct c_expr expr = c_parser_expression (parser);
5482 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5483 expr.value = c_fully_fold (expr.value, false, NULL);
5484 stmt = objc_build_throw_stmt (loc, expr.value);
5485 goto expect_semicolon;
5487 break;
5488 case RID_AT_TRY:
5489 gcc_assert (c_dialect_objc ());
5490 c_parser_objc_try_catch_finally_statement (parser);
5491 break;
5492 case RID_AT_SYNCHRONIZED:
5493 gcc_assert (c_dialect_objc ());
5494 c_parser_objc_synchronized_statement (parser);
5495 break;
5496 case RID_ATTRIBUTE:
5498 /* Allow '__attribute__((fallthrough));'. */
5499 tree attrs = c_parser_attributes (parser);
5500 if (attribute_fallthrough_p (attrs))
5502 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5504 tree fn = build_call_expr_internal_loc (loc,
5505 IFN_FALLTHROUGH,
5506 void_type_node, 0);
5507 add_stmt (fn);
5508 /* Eat the ';'. */
5509 c_parser_consume_token (parser);
5511 else
5512 warning_at (loc, OPT_Wattributes,
5513 "%<fallthrough%> attribute not followed "
5514 "by %<;%>");
5516 else if (attrs != NULL_TREE)
5517 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5518 " can be applied to a null statement");
5519 break;
5521 default:
5522 goto expr_stmt;
5524 break;
5525 case CPP_SEMICOLON:
5526 c_parser_consume_token (parser);
5527 break;
5528 case CPP_CLOSE_PAREN:
5529 case CPP_CLOSE_SQUARE:
5530 /* Avoid infinite loop in error recovery:
5531 c_parser_skip_until_found stops at a closing nesting
5532 delimiter without consuming it, but here we need to consume
5533 it to proceed further. */
5534 c_parser_error (parser, "expected statement");
5535 c_parser_consume_token (parser);
5536 break;
5537 case CPP_PRAGMA:
5538 c_parser_pragma (parser, pragma_stmt, if_p);
5539 break;
5540 default:
5541 expr_stmt:
5542 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5543 expect_semicolon:
5544 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5545 break;
5547 /* Two cases cannot and do not have line numbers associated: If stmt
5548 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5549 cannot hold line numbers. But that's OK because the statement
5550 will either be changed to a MODIFY_EXPR during gimplification of
5551 the statement expr, or discarded. If stmt was compound, but
5552 without new variables, we will have skipped the creation of a
5553 BIND and will have a bare STATEMENT_LIST. But that's OK because
5554 (recursively) all of the component statements should already have
5555 line numbers assigned. ??? Can we discard no-op statements
5556 earlier? */
5557 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5558 protected_set_expr_location (stmt, loc);
5560 parser->in_if_block = in_if_block;
5563 /* Parse the condition from an if, do, while or for statements. */
5565 static tree
5566 c_parser_condition (c_parser *parser)
5568 location_t loc = c_parser_peek_token (parser)->location;
5569 tree cond;
5570 cond = c_parser_expression_conv (parser).value;
5571 cond = c_objc_common_truthvalue_conversion (loc, cond);
5572 cond = c_fully_fold (cond, false, NULL);
5573 if (warn_sequence_point)
5574 verify_sequence_points (cond);
5575 return cond;
5578 /* Parse a parenthesized condition from an if, do or while statement.
5580 condition:
5581 ( expression )
5583 static tree
5584 c_parser_paren_condition (c_parser *parser)
5586 tree cond;
5587 matching_parens parens;
5588 if (!parens.require_open (parser))
5589 return error_mark_node;
5590 cond = c_parser_condition (parser);
5591 parens.skip_until_found_close (parser);
5592 return cond;
5595 /* Parse a statement which is a block in C99.
5597 IF_P is used to track whether there's a (possibly labeled) if statement
5598 which is not enclosed in braces and has an else clause. This is used to
5599 implement -Wparentheses. */
5601 static tree
5602 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5603 location_t *loc_after_labels)
5605 tree block = c_begin_compound_stmt (flag_isoc99);
5606 location_t loc = c_parser_peek_token (parser)->location;
5607 c_parser_statement (parser, if_p, loc_after_labels);
5608 return c_end_compound_stmt (loc, block, flag_isoc99);
5611 /* Parse the body of an if statement. This is just parsing a
5612 statement but (a) it is a block in C99, (b) we track whether the
5613 body is an if statement for the sake of -Wparentheses warnings, (c)
5614 we handle an empty body specially for the sake of -Wempty-body
5615 warnings, and (d) we call parser_compound_statement directly
5616 because c_parser_statement_after_labels resets
5617 parser->in_if_block.
5619 IF_P is used to track whether there's a (possibly labeled) if statement
5620 which is not enclosed in braces and has an else clause. This is used to
5621 implement -Wparentheses. */
5623 static tree
5624 c_parser_if_body (c_parser *parser, bool *if_p,
5625 const token_indent_info &if_tinfo)
5627 tree block = c_begin_compound_stmt (flag_isoc99);
5628 location_t body_loc = c_parser_peek_token (parser)->location;
5629 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5630 token_indent_info body_tinfo
5631 = get_token_indent_info (c_parser_peek_token (parser));
5633 c_parser_all_labels (parser);
5634 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5636 location_t loc = c_parser_peek_token (parser)->location;
5637 add_stmt (build_empty_stmt (loc));
5638 c_parser_consume_token (parser);
5639 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5640 warning_at (loc, OPT_Wempty_body,
5641 "suggest braces around empty body in an %<if%> statement");
5643 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5644 add_stmt (c_parser_compound_statement (parser));
5645 else
5647 body_loc_after_labels = c_parser_peek_token (parser)->location;
5648 c_parser_statement_after_labels (parser, if_p);
5651 token_indent_info next_tinfo
5652 = get_token_indent_info (c_parser_peek_token (parser));
5653 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5654 if (body_loc_after_labels != UNKNOWN_LOCATION
5655 && next_tinfo.type != CPP_SEMICOLON)
5656 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5657 if_tinfo.location, RID_IF);
5659 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5662 /* Parse the else body of an if statement. This is just parsing a
5663 statement but (a) it is a block in C99, (b) we handle an empty body
5664 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5665 of if-else-if conditions. */
5667 static tree
5668 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5669 vec<tree> *chain)
5671 location_t body_loc = c_parser_peek_token (parser)->location;
5672 tree block = c_begin_compound_stmt (flag_isoc99);
5673 token_indent_info body_tinfo
5674 = get_token_indent_info (c_parser_peek_token (parser));
5675 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5677 c_parser_all_labels (parser);
5678 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5680 location_t loc = c_parser_peek_token (parser)->location;
5681 warning_at (loc,
5682 OPT_Wempty_body,
5683 "suggest braces around empty body in an %<else%> statement");
5684 add_stmt (build_empty_stmt (loc));
5685 c_parser_consume_token (parser);
5687 else
5689 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5690 body_loc_after_labels = c_parser_peek_token (parser)->location;
5691 c_parser_statement_after_labels (parser, NULL, chain);
5694 token_indent_info next_tinfo
5695 = get_token_indent_info (c_parser_peek_token (parser));
5696 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5697 if (body_loc_after_labels != UNKNOWN_LOCATION
5698 && next_tinfo.type != CPP_SEMICOLON)
5699 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5700 else_tinfo.location, RID_ELSE);
5702 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5705 /* We might need to reclassify any previously-lexed identifier, e.g.
5706 when we've left a for loop with an if-statement without else in the
5707 body - we might have used a wrong scope for the token. See PR67784. */
5709 static void
5710 c_parser_maybe_reclassify_token (c_parser *parser)
5712 if (c_parser_next_token_is (parser, CPP_NAME))
5714 c_token *token = c_parser_peek_token (parser);
5716 if (token->id_kind != C_ID_CLASSNAME)
5718 tree decl = lookup_name (token->value);
5720 token->id_kind = C_ID_ID;
5721 if (decl)
5723 if (TREE_CODE (decl) == TYPE_DECL)
5724 token->id_kind = C_ID_TYPENAME;
5726 else if (c_dialect_objc ())
5728 tree objc_interface_decl = objc_is_class_name (token->value);
5729 /* Objective-C class names are in the same namespace as
5730 variables and typedefs, and hence are shadowed by local
5731 declarations. */
5732 if (objc_interface_decl)
5734 token->value = objc_interface_decl;
5735 token->id_kind = C_ID_CLASSNAME;
5742 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5744 if-statement:
5745 if ( expression ) statement
5746 if ( expression ) statement else statement
5748 CHAIN is a vector of if-else-if conditions.
5749 IF_P is used to track whether there's a (possibly labeled) if statement
5750 which is not enclosed in braces and has an else clause. This is used to
5751 implement -Wparentheses. */
5753 static void
5754 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5756 tree block;
5757 location_t loc;
5758 tree cond;
5759 bool nested_if = false;
5760 tree first_body, second_body;
5761 bool in_if_block;
5763 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5764 token_indent_info if_tinfo
5765 = get_token_indent_info (c_parser_peek_token (parser));
5766 c_parser_consume_token (parser);
5767 block = c_begin_compound_stmt (flag_isoc99);
5768 loc = c_parser_peek_token (parser)->location;
5769 cond = c_parser_paren_condition (parser);
5770 in_if_block = parser->in_if_block;
5771 parser->in_if_block = true;
5772 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5773 parser->in_if_block = in_if_block;
5775 if (warn_duplicated_cond)
5776 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5778 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5780 token_indent_info else_tinfo
5781 = get_token_indent_info (c_parser_peek_token (parser));
5782 c_parser_consume_token (parser);
5783 if (warn_duplicated_cond)
5785 if (c_parser_next_token_is_keyword (parser, RID_IF)
5786 && chain == NULL)
5788 /* We've got "if (COND) else if (COND2)". Start the
5789 condition chain and add COND as the first element. */
5790 chain = new vec<tree> ();
5791 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5792 chain->safe_push (cond);
5794 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5796 /* This is if-else without subsequent if. Zap the condition
5797 chain; we would have already warned at this point. */
5798 delete chain;
5799 chain = NULL;
5802 second_body = c_parser_else_body (parser, else_tinfo, chain);
5803 /* Set IF_P to true to indicate that this if statement has an
5804 else clause. This may trigger the Wparentheses warning
5805 below when we get back up to the parent if statement. */
5806 if (if_p != NULL)
5807 *if_p = true;
5809 else
5811 second_body = NULL_TREE;
5813 /* Diagnose an ambiguous else if if-then-else is nested inside
5814 if-then. */
5815 if (nested_if)
5816 warning_at (loc, OPT_Wdangling_else,
5817 "suggest explicit braces to avoid ambiguous %<else%>");
5819 if (warn_duplicated_cond)
5821 /* This if statement does not have an else clause. We don't
5822 need the condition chain anymore. */
5823 delete chain;
5824 chain = NULL;
5827 c_finish_if_stmt (loc, cond, first_body, second_body);
5828 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5830 c_parser_maybe_reclassify_token (parser);
5833 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5835 switch-statement:
5836 switch (expression) statement
5839 static void
5840 c_parser_switch_statement (c_parser *parser, bool *if_p)
5842 struct c_expr ce;
5843 tree block, expr, body, save_break;
5844 location_t switch_loc = c_parser_peek_token (parser)->location;
5845 location_t switch_cond_loc;
5846 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5847 c_parser_consume_token (parser);
5848 block = c_begin_compound_stmt (flag_isoc99);
5849 bool explicit_cast_p = false;
5850 matching_parens parens;
5851 if (parens.require_open (parser))
5853 switch_cond_loc = c_parser_peek_token (parser)->location;
5854 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5855 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5856 explicit_cast_p = true;
5857 ce = c_parser_expression (parser);
5858 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5859 expr = ce.value;
5860 /* ??? expr has no valid location? */
5861 parens.skip_until_found_close (parser);
5863 else
5865 switch_cond_loc = UNKNOWN_LOCATION;
5866 expr = error_mark_node;
5867 ce.original_type = error_mark_node;
5869 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5870 save_break = c_break_label;
5871 c_break_label = NULL_TREE;
5872 location_t loc_after_labels;
5873 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5874 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5875 location_t next_loc = c_parser_peek_token (parser)->location;
5876 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5877 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5878 RID_SWITCH);
5879 if (c_break_label)
5881 location_t here = c_parser_peek_token (parser)->location;
5882 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5883 SET_EXPR_LOCATION (t, here);
5884 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5885 append_to_statement_list_force (t, &body);
5887 c_finish_case (body, ce.original_type);
5888 c_break_label = save_break;
5889 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5890 c_parser_maybe_reclassify_token (parser);
5893 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5895 while-statement:
5896 while (expression) statement
5898 IF_P is used to track whether there's a (possibly labeled) if statement
5899 which is not enclosed in braces and has an else clause. This is used to
5900 implement -Wparentheses. */
5902 static void
5903 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5904 bool *if_p)
5906 tree block, cond, body, save_break, save_cont;
5907 location_t loc;
5908 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5909 token_indent_info while_tinfo
5910 = get_token_indent_info (c_parser_peek_token (parser));
5911 c_parser_consume_token (parser);
5912 block = c_begin_compound_stmt (flag_isoc99);
5913 loc = c_parser_peek_token (parser)->location;
5914 cond = c_parser_paren_condition (parser);
5915 if (ivdep && cond != error_mark_node)
5916 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5917 build_int_cst (integer_type_node,
5918 annot_expr_ivdep_kind),
5919 integer_zero_node);
5920 if (unroll && cond != error_mark_node)
5921 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5922 build_int_cst (integer_type_node,
5923 annot_expr_unroll_kind),
5924 build_int_cst (integer_type_node, unroll));
5925 save_break = c_break_label;
5926 c_break_label = NULL_TREE;
5927 save_cont = c_cont_label;
5928 c_cont_label = NULL_TREE;
5930 token_indent_info body_tinfo
5931 = get_token_indent_info (c_parser_peek_token (parser));
5933 location_t loc_after_labels;
5934 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5935 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5936 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5937 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5938 c_parser_maybe_reclassify_token (parser);
5940 token_indent_info next_tinfo
5941 = get_token_indent_info (c_parser_peek_token (parser));
5942 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5944 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5945 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5946 while_tinfo.location, RID_WHILE);
5948 c_break_label = save_break;
5949 c_cont_label = save_cont;
5952 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5954 do-statement:
5955 do statement while ( expression ) ;
5958 static void
5959 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
5961 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5962 location_t loc;
5963 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5964 c_parser_consume_token (parser);
5965 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5966 warning_at (c_parser_peek_token (parser)->location,
5967 OPT_Wempty_body,
5968 "suggest braces around empty body in %<do%> statement");
5969 block = c_begin_compound_stmt (flag_isoc99);
5970 loc = c_parser_peek_token (parser)->location;
5971 save_break = c_break_label;
5972 c_break_label = NULL_TREE;
5973 save_cont = c_cont_label;
5974 c_cont_label = NULL_TREE;
5975 body = c_parser_c99_block_statement (parser, NULL);
5976 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5977 new_break = c_break_label;
5978 c_break_label = save_break;
5979 new_cont = c_cont_label;
5980 c_cont_label = save_cont;
5981 cond = c_parser_paren_condition (parser);
5982 if (ivdep && cond != error_mark_node)
5983 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5984 build_int_cst (integer_type_node,
5985 annot_expr_ivdep_kind),
5986 integer_zero_node);
5987 if (unroll && cond != error_mark_node)
5988 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5989 build_int_cst (integer_type_node,
5990 annot_expr_unroll_kind),
5991 build_int_cst (integer_type_node, unroll));
5992 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5993 c_parser_skip_to_end_of_block_or_statement (parser);
5994 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5995 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5998 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6000 for-statement:
6001 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6002 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6004 The form with a declaration is new in C99.
6006 ??? In accordance with the old parser, the declaration may be a
6007 nested function, which is then rejected in check_for_loop_decls,
6008 but does it make any sense for this to be included in the grammar?
6009 Note in particular that the nested function does not include a
6010 trailing ';', whereas the "declaration" production includes one.
6011 Also, can we reject bad declarations earlier and cheaper than
6012 check_for_loop_decls?
6014 In Objective-C, there are two additional variants:
6016 foreach-statement:
6017 for ( expression in expresssion ) statement
6018 for ( declaration in expression ) statement
6020 This is inconsistent with C, because the second variant is allowed
6021 even if c99 is not enabled.
6023 The rest of the comment documents these Objective-C foreach-statement.
6025 Here is the canonical example of the first variant:
6026 for (object in array) { do something with object }
6027 we call the first expression ("object") the "object_expression" and
6028 the second expression ("array") the "collection_expression".
6029 object_expression must be an lvalue of type "id" (a generic Objective-C
6030 object) because the loop works by assigning to object_expression the
6031 various objects from the collection_expression. collection_expression
6032 must evaluate to something of type "id" which responds to the method
6033 countByEnumeratingWithState:objects:count:.
6035 The canonical example of the second variant is:
6036 for (id object in array) { do something with object }
6037 which is completely equivalent to
6039 id object;
6040 for (object in array) { do something with object }
6042 Note that initizializing 'object' in some way (eg, "for ((object =
6043 xxx) in array) { do something with object }") is possibly
6044 technically valid, but completely pointless as 'object' will be
6045 assigned to something else as soon as the loop starts. We should
6046 most likely reject it (TODO).
6048 The beginning of the Objective-C foreach-statement looks exactly
6049 like the beginning of the for-statement, and we can tell it is a
6050 foreach-statement only because the initial declaration or
6051 expression is terminated by 'in' instead of ';'.
6053 IF_P is used to track whether there's a (possibly labeled) if statement
6054 which is not enclosed in braces and has an else clause. This is used to
6055 implement -Wparentheses. */
6057 static void
6058 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6059 bool *if_p)
6061 tree block, cond, incr, save_break, save_cont, body;
6062 /* The following are only used when parsing an ObjC foreach statement. */
6063 tree object_expression;
6064 /* Silence the bogus uninitialized warning. */
6065 tree collection_expression = NULL;
6066 location_t loc = c_parser_peek_token (parser)->location;
6067 location_t for_loc = c_parser_peek_token (parser)->location;
6068 bool is_foreach_statement = false;
6069 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6070 token_indent_info for_tinfo
6071 = get_token_indent_info (c_parser_peek_token (parser));
6072 c_parser_consume_token (parser);
6073 /* Open a compound statement in Objective-C as well, just in case this is
6074 as foreach expression. */
6075 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6076 cond = error_mark_node;
6077 incr = error_mark_node;
6078 matching_parens parens;
6079 if (parens.require_open (parser))
6081 /* Parse the initialization declaration or expression. */
6082 object_expression = error_mark_node;
6083 parser->objc_could_be_foreach_context = c_dialect_objc ();
6084 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6086 parser->objc_could_be_foreach_context = false;
6087 c_parser_consume_token (parser);
6088 c_finish_expr_stmt (loc, NULL_TREE);
6090 else if (c_parser_next_tokens_start_declaration (parser))
6092 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6093 &object_expression, vNULL);
6094 parser->objc_could_be_foreach_context = false;
6096 if (c_parser_next_token_is_keyword (parser, RID_IN))
6098 c_parser_consume_token (parser);
6099 is_foreach_statement = true;
6100 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6101 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6103 else
6104 check_for_loop_decls (for_loc, flag_isoc99);
6106 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6108 /* __extension__ can start a declaration, but is also an
6109 unary operator that can start an expression. Consume all
6110 but the last of a possible series of __extension__ to
6111 determine which. */
6112 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6113 && (c_parser_peek_2nd_token (parser)->keyword
6114 == RID_EXTENSION))
6115 c_parser_consume_token (parser);
6116 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6118 int ext;
6119 ext = disable_extension_diagnostics ();
6120 c_parser_consume_token (parser);
6121 c_parser_declaration_or_fndef (parser, true, true, true, true,
6122 true, &object_expression, vNULL);
6123 parser->objc_could_be_foreach_context = false;
6125 restore_extension_diagnostics (ext);
6126 if (c_parser_next_token_is_keyword (parser, RID_IN))
6128 c_parser_consume_token (parser);
6129 is_foreach_statement = true;
6130 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6131 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6133 else
6134 check_for_loop_decls (for_loc, flag_isoc99);
6136 else
6137 goto init_expr;
6139 else
6141 init_expr:
6143 struct c_expr ce;
6144 tree init_expression;
6145 ce = c_parser_expression (parser);
6146 init_expression = ce.value;
6147 parser->objc_could_be_foreach_context = false;
6148 if (c_parser_next_token_is_keyword (parser, RID_IN))
6150 c_parser_consume_token (parser);
6151 is_foreach_statement = true;
6152 if (! lvalue_p (init_expression))
6153 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6154 object_expression = c_fully_fold (init_expression, false, NULL);
6156 else
6158 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6159 init_expression = ce.value;
6160 c_finish_expr_stmt (loc, init_expression);
6161 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6165 /* Parse the loop condition. In the case of a foreach
6166 statement, there is no loop condition. */
6167 gcc_assert (!parser->objc_could_be_foreach_context);
6168 if (!is_foreach_statement)
6170 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6172 if (ivdep)
6174 c_parser_error (parser, "missing loop condition in loop with "
6175 "%<GCC ivdep%> pragma");
6176 cond = error_mark_node;
6178 else if (unroll)
6180 c_parser_error (parser, "missing loop condition in loop with "
6181 "%<GCC unroll%> pragma");
6182 cond = error_mark_node;
6184 else
6186 c_parser_consume_token (parser);
6187 cond = NULL_TREE;
6190 else
6192 cond = c_parser_condition (parser);
6193 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6194 "expected %<;%>");
6196 if (ivdep && cond != error_mark_node)
6197 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6198 build_int_cst (integer_type_node,
6199 annot_expr_ivdep_kind),
6200 integer_zero_node);
6201 if (unroll && cond != error_mark_node)
6202 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6203 build_int_cst (integer_type_node,
6204 annot_expr_unroll_kind),
6205 build_int_cst (integer_type_node, unroll));
6207 /* Parse the increment expression (the third expression in a
6208 for-statement). In the case of a foreach-statement, this is
6209 the expression that follows the 'in'. */
6210 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6212 if (is_foreach_statement)
6214 c_parser_error (parser, "missing collection in fast enumeration");
6215 collection_expression = error_mark_node;
6217 else
6218 incr = c_process_expr_stmt (loc, NULL_TREE);
6220 else
6222 if (is_foreach_statement)
6223 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6224 false, NULL);
6225 else
6227 struct c_expr ce = c_parser_expression (parser);
6228 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6229 incr = c_process_expr_stmt (loc, ce.value);
6232 parens.skip_until_found_close (parser);
6234 save_break = c_break_label;
6235 c_break_label = NULL_TREE;
6236 save_cont = c_cont_label;
6237 c_cont_label = NULL_TREE;
6239 token_indent_info body_tinfo
6240 = get_token_indent_info (c_parser_peek_token (parser));
6242 location_t loc_after_labels;
6243 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6244 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6246 if (is_foreach_statement)
6247 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6248 else
6249 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6250 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6251 c_parser_maybe_reclassify_token (parser);
6253 token_indent_info next_tinfo
6254 = get_token_indent_info (c_parser_peek_token (parser));
6255 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6257 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6258 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6259 for_tinfo.location, RID_FOR);
6261 c_break_label = save_break;
6262 c_cont_label = save_cont;
6265 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6266 statement with inputs, outputs, clobbers, and volatile tag
6267 allowed.
6269 asm-statement:
6270 asm type-qualifier[opt] ( asm-argument ) ;
6271 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6273 asm-argument:
6274 asm-string-literal
6275 asm-string-literal : asm-operands[opt]
6276 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6277 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6279 asm-goto-argument:
6280 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6281 : asm-goto-operands
6283 Qualifiers other than volatile are accepted in the syntax but
6284 warned for. */
6286 static tree
6287 c_parser_asm_statement (c_parser *parser)
6289 tree quals, str, outputs, inputs, clobbers, labels, ret;
6290 bool simple, is_goto;
6291 location_t asm_loc = c_parser_peek_token (parser)->location;
6292 int section, nsections;
6294 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6295 c_parser_consume_token (parser);
6296 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6298 quals = c_parser_peek_token (parser)->value;
6299 c_parser_consume_token (parser);
6301 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6302 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6304 warning_at (c_parser_peek_token (parser)->location,
6306 "%E qualifier ignored on asm",
6307 c_parser_peek_token (parser)->value);
6308 quals = NULL_TREE;
6309 c_parser_consume_token (parser);
6311 else
6312 quals = NULL_TREE;
6314 is_goto = false;
6315 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6317 c_parser_consume_token (parser);
6318 is_goto = true;
6321 /* ??? Follow the C++ parser rather than using the
6322 lex_untranslated_string kludge. */
6323 parser->lex_untranslated_string = true;
6324 ret = NULL;
6326 matching_parens parens;
6327 if (!parens.require_open (parser))
6328 goto error;
6330 str = c_parser_asm_string_literal (parser);
6331 if (str == NULL_TREE)
6332 goto error_close_paren;
6334 simple = true;
6335 outputs = NULL_TREE;
6336 inputs = NULL_TREE;
6337 clobbers = NULL_TREE;
6338 labels = NULL_TREE;
6340 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6341 goto done_asm;
6343 /* Parse each colon-delimited section of operands. */
6344 nsections = 3 + is_goto;
6345 for (section = 0; section < nsections; ++section)
6347 if (!c_parser_require (parser, CPP_COLON,
6348 is_goto
6349 ? G_("expected %<:%>")
6350 : G_("expected %<:%> or %<)%>"),
6351 UNKNOWN_LOCATION, is_goto))
6352 goto error_close_paren;
6354 /* Once past any colon, we're no longer a simple asm. */
6355 simple = false;
6357 if ((!c_parser_next_token_is (parser, CPP_COLON)
6358 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6359 || section == 3)
6360 switch (section)
6362 case 0:
6363 /* For asm goto, we don't allow output operands, but reserve
6364 the slot for a future extension that does allow them. */
6365 if (!is_goto)
6366 outputs = c_parser_asm_operands (parser);
6367 break;
6368 case 1:
6369 inputs = c_parser_asm_operands (parser);
6370 break;
6371 case 2:
6372 clobbers = c_parser_asm_clobbers (parser);
6373 break;
6374 case 3:
6375 labels = c_parser_asm_goto_operands (parser);
6376 break;
6377 default:
6378 gcc_unreachable ();
6381 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6382 goto done_asm;
6385 done_asm:
6386 if (!parens.require_close (parser))
6388 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6389 goto error;
6392 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6393 c_parser_skip_to_end_of_block_or_statement (parser);
6395 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6396 clobbers, labels, simple));
6398 error:
6399 parser->lex_untranslated_string = false;
6400 return ret;
6402 error_close_paren:
6403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6404 goto error;
6407 /* Parse asm operands, a GNU extension.
6409 asm-operands:
6410 asm-operand
6411 asm-operands , asm-operand
6413 asm-operand:
6414 asm-string-literal ( expression )
6415 [ identifier ] asm-string-literal ( expression )
6418 static tree
6419 c_parser_asm_operands (c_parser *parser)
6421 tree list = NULL_TREE;
6422 while (true)
6424 tree name, str;
6425 struct c_expr expr;
6426 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6428 c_parser_consume_token (parser);
6429 if (c_parser_next_token_is (parser, CPP_NAME))
6431 tree id = c_parser_peek_token (parser)->value;
6432 c_parser_consume_token (parser);
6433 name = build_string (IDENTIFIER_LENGTH (id),
6434 IDENTIFIER_POINTER (id));
6436 else
6438 c_parser_error (parser, "expected identifier");
6439 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6440 return NULL_TREE;
6442 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6443 "expected %<]%>");
6445 else
6446 name = NULL_TREE;
6447 str = c_parser_asm_string_literal (parser);
6448 if (str == NULL_TREE)
6449 return NULL_TREE;
6450 parser->lex_untranslated_string = false;
6451 matching_parens parens;
6452 if (!parens.require_open (parser))
6454 parser->lex_untranslated_string = true;
6455 return NULL_TREE;
6457 expr = c_parser_expression (parser);
6458 mark_exp_read (expr.value);
6459 parser->lex_untranslated_string = true;
6460 if (!parens.require_close (parser))
6462 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6463 return NULL_TREE;
6465 list = chainon (list, build_tree_list (build_tree_list (name, str),
6466 expr.value));
6467 if (c_parser_next_token_is (parser, CPP_COMMA))
6468 c_parser_consume_token (parser);
6469 else
6470 break;
6472 return list;
6475 /* Parse asm clobbers, a GNU extension.
6477 asm-clobbers:
6478 asm-string-literal
6479 asm-clobbers , asm-string-literal
6482 static tree
6483 c_parser_asm_clobbers (c_parser *parser)
6485 tree list = NULL_TREE;
6486 while (true)
6488 tree str = c_parser_asm_string_literal (parser);
6489 if (str)
6490 list = tree_cons (NULL_TREE, str, list);
6491 else
6492 return NULL_TREE;
6493 if (c_parser_next_token_is (parser, CPP_COMMA))
6494 c_parser_consume_token (parser);
6495 else
6496 break;
6498 return list;
6501 /* Parse asm goto labels, a GNU extension.
6503 asm-goto-operands:
6504 identifier
6505 asm-goto-operands , identifier
6508 static tree
6509 c_parser_asm_goto_operands (c_parser *parser)
6511 tree list = NULL_TREE;
6512 while (true)
6514 tree name, label;
6516 if (c_parser_next_token_is (parser, CPP_NAME))
6518 c_token *tok = c_parser_peek_token (parser);
6519 name = tok->value;
6520 label = lookup_label_for_goto (tok->location, name);
6521 c_parser_consume_token (parser);
6522 TREE_USED (label) = 1;
6524 else
6526 c_parser_error (parser, "expected identifier");
6527 return NULL_TREE;
6530 name = build_string (IDENTIFIER_LENGTH (name),
6531 IDENTIFIER_POINTER (name));
6532 list = tree_cons (name, label, list);
6533 if (c_parser_next_token_is (parser, CPP_COMMA))
6534 c_parser_consume_token (parser);
6535 else
6536 return nreverse (list);
6540 /* Parse an expression other than a compound expression; that is, an
6541 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6542 AFTER is not NULL then it is an Objective-C message expression which
6543 is the primary-expression starting the expression as an initializer.
6545 assignment-expression:
6546 conditional-expression
6547 unary-expression assignment-operator assignment-expression
6549 assignment-operator: one of
6550 = *= /= %= += -= <<= >>= &= ^= |=
6552 In GNU C we accept any conditional expression on the LHS and
6553 diagnose the invalid lvalue rather than producing a syntax
6554 error. */
6556 static struct c_expr
6557 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6558 tree omp_atomic_lhs)
6560 struct c_expr lhs, rhs, ret;
6561 enum tree_code code;
6562 location_t op_location, exp_location;
6563 gcc_assert (!after || c_dialect_objc ());
6564 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6565 op_location = c_parser_peek_token (parser)->location;
6566 switch (c_parser_peek_token (parser)->type)
6568 case CPP_EQ:
6569 code = NOP_EXPR;
6570 break;
6571 case CPP_MULT_EQ:
6572 code = MULT_EXPR;
6573 break;
6574 case CPP_DIV_EQ:
6575 code = TRUNC_DIV_EXPR;
6576 break;
6577 case CPP_MOD_EQ:
6578 code = TRUNC_MOD_EXPR;
6579 break;
6580 case CPP_PLUS_EQ:
6581 code = PLUS_EXPR;
6582 break;
6583 case CPP_MINUS_EQ:
6584 code = MINUS_EXPR;
6585 break;
6586 case CPP_LSHIFT_EQ:
6587 code = LSHIFT_EXPR;
6588 break;
6589 case CPP_RSHIFT_EQ:
6590 code = RSHIFT_EXPR;
6591 break;
6592 case CPP_AND_EQ:
6593 code = BIT_AND_EXPR;
6594 break;
6595 case CPP_XOR_EQ:
6596 code = BIT_XOR_EXPR;
6597 break;
6598 case CPP_OR_EQ:
6599 code = BIT_IOR_EXPR;
6600 break;
6601 default:
6602 return lhs;
6604 c_parser_consume_token (parser);
6605 exp_location = c_parser_peek_token (parser)->location;
6606 rhs = c_parser_expr_no_commas (parser, NULL);
6607 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6609 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6610 code, exp_location, rhs.value,
6611 rhs.original_type);
6612 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6613 if (code == NOP_EXPR)
6614 ret.original_code = MODIFY_EXPR;
6615 else
6617 TREE_NO_WARNING (ret.value) = 1;
6618 ret.original_code = ERROR_MARK;
6620 ret.original_type = NULL;
6621 return ret;
6624 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6625 AFTER is not NULL then it is an Objective-C message expression which is
6626 the primary-expression starting the expression as an initializer.
6628 conditional-expression:
6629 logical-OR-expression
6630 logical-OR-expression ? expression : conditional-expression
6632 GNU extensions:
6634 conditional-expression:
6635 logical-OR-expression ? : conditional-expression
6638 static struct c_expr
6639 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6640 tree omp_atomic_lhs)
6642 struct c_expr cond, exp1, exp2, ret;
6643 location_t start, cond_loc, colon_loc;
6645 gcc_assert (!after || c_dialect_objc ());
6647 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6649 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6650 return cond;
6651 if (cond.value != error_mark_node)
6652 start = cond.get_start ();
6653 else
6654 start = UNKNOWN_LOCATION;
6655 cond_loc = c_parser_peek_token (parser)->location;
6656 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6657 c_parser_consume_token (parser);
6658 if (c_parser_next_token_is (parser, CPP_COLON))
6660 tree eptype = NULL_TREE;
6662 location_t middle_loc = c_parser_peek_token (parser)->location;
6663 pedwarn (middle_loc, OPT_Wpedantic,
6664 "ISO C forbids omitting the middle term of a ?: expression");
6665 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6667 eptype = TREE_TYPE (cond.value);
6668 cond.value = TREE_OPERAND (cond.value, 0);
6670 tree e = cond.value;
6671 while (TREE_CODE (e) == COMPOUND_EXPR)
6672 e = TREE_OPERAND (e, 1);
6673 warn_for_omitted_condop (middle_loc, e);
6674 /* Make sure first operand is calculated only once. */
6675 exp1.value = save_expr (default_conversion (cond.value));
6676 if (eptype)
6677 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6678 exp1.original_type = NULL;
6679 exp1.src_range = cond.src_range;
6680 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6681 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6683 else
6685 cond.value
6686 = c_objc_common_truthvalue_conversion
6687 (cond_loc, default_conversion (cond.value));
6688 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6689 exp1 = c_parser_expression_conv (parser);
6690 mark_exp_read (exp1.value);
6691 c_inhibit_evaluation_warnings +=
6692 ((cond.value == truthvalue_true_node)
6693 - (cond.value == truthvalue_false_node));
6696 colon_loc = c_parser_peek_token (parser)->location;
6697 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6699 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6700 ret.set_error ();
6701 ret.original_code = ERROR_MARK;
6702 ret.original_type = NULL;
6703 return ret;
6706 location_t exp2_loc = c_parser_peek_token (parser)->location;
6707 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6708 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6710 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6711 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6712 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6713 ret.value = build_conditional_expr (colon_loc, cond.value,
6714 cond.original_code == C_MAYBE_CONST_EXPR,
6715 exp1.value, exp1.original_type, loc1,
6716 exp2.value, exp2.original_type, loc2);
6717 ret.original_code = ERROR_MARK;
6718 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6719 ret.original_type = NULL;
6720 else
6722 tree t1, t2;
6724 /* If both sides are enum type, the default conversion will have
6725 made the type of the result be an integer type. We want to
6726 remember the enum types we started with. */
6727 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6728 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6729 ret.original_type = ((t1 != error_mark_node
6730 && t2 != error_mark_node
6731 && (TYPE_MAIN_VARIANT (t1)
6732 == TYPE_MAIN_VARIANT (t2)))
6733 ? t1
6734 : NULL);
6736 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6737 return ret;
6740 /* Parse a binary expression; that is, a logical-OR-expression (C90
6741 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6742 NULL then it is an Objective-C message expression which is the
6743 primary-expression starting the expression as an initializer.
6745 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6746 when it should be the unfolded lhs. In a valid OpenMP source,
6747 one of the operands of the toplevel binary expression must be equal
6748 to it. In that case, just return a build2 created binary operation
6749 rather than result of parser_build_binary_op.
6751 multiplicative-expression:
6752 cast-expression
6753 multiplicative-expression * cast-expression
6754 multiplicative-expression / cast-expression
6755 multiplicative-expression % cast-expression
6757 additive-expression:
6758 multiplicative-expression
6759 additive-expression + multiplicative-expression
6760 additive-expression - multiplicative-expression
6762 shift-expression:
6763 additive-expression
6764 shift-expression << additive-expression
6765 shift-expression >> additive-expression
6767 relational-expression:
6768 shift-expression
6769 relational-expression < shift-expression
6770 relational-expression > shift-expression
6771 relational-expression <= shift-expression
6772 relational-expression >= shift-expression
6774 equality-expression:
6775 relational-expression
6776 equality-expression == relational-expression
6777 equality-expression != relational-expression
6779 AND-expression:
6780 equality-expression
6781 AND-expression & equality-expression
6783 exclusive-OR-expression:
6784 AND-expression
6785 exclusive-OR-expression ^ AND-expression
6787 inclusive-OR-expression:
6788 exclusive-OR-expression
6789 inclusive-OR-expression | exclusive-OR-expression
6791 logical-AND-expression:
6792 inclusive-OR-expression
6793 logical-AND-expression && inclusive-OR-expression
6795 logical-OR-expression:
6796 logical-AND-expression
6797 logical-OR-expression || logical-AND-expression
6800 static struct c_expr
6801 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6802 tree omp_atomic_lhs)
6804 /* A binary expression is parsed using operator-precedence parsing,
6805 with the operands being cast expressions. All the binary
6806 operators are left-associative. Thus a binary expression is of
6807 form:
6809 E0 op1 E1 op2 E2 ...
6811 which we represent on a stack. On the stack, the precedence
6812 levels are strictly increasing. When a new operator is
6813 encountered of higher precedence than that at the top of the
6814 stack, it is pushed; its LHS is the top expression, and its RHS
6815 is everything parsed until it is popped. When a new operator is
6816 encountered with precedence less than or equal to that at the top
6817 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6818 by the result of the operation until the operator at the top of
6819 the stack has lower precedence than the new operator or there is
6820 only one element on the stack; then the top expression is the LHS
6821 of the new operator. In the case of logical AND and OR
6822 expressions, we also need to adjust c_inhibit_evaluation_warnings
6823 as appropriate when the operators are pushed and popped. */
6825 struct {
6826 /* The expression at this stack level. */
6827 struct c_expr expr;
6828 /* The precedence of the operator on its left, PREC_NONE at the
6829 bottom of the stack. */
6830 enum c_parser_prec prec;
6831 /* The operation on its left. */
6832 enum tree_code op;
6833 /* The source location of this operation. */
6834 location_t loc;
6835 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6836 tree sizeof_arg;
6837 } stack[NUM_PRECS];
6838 int sp;
6839 /* Location of the binary operator. */
6840 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6841 #define POP \
6842 do { \
6843 switch (stack[sp].op) \
6845 case TRUTH_ANDIF_EXPR: \
6846 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6847 == truthvalue_false_node); \
6848 break; \
6849 case TRUTH_ORIF_EXPR: \
6850 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6851 == truthvalue_true_node); \
6852 break; \
6853 case TRUNC_DIV_EXPR: \
6854 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6855 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6857 tree type0 = stack[sp - 1].sizeof_arg; \
6858 tree type1 = stack[sp].sizeof_arg; \
6859 tree first_arg = type0; \
6860 if (!TYPE_P (type0)) \
6861 type0 = TREE_TYPE (type0); \
6862 if (!TYPE_P (type1)) \
6863 type1 = TREE_TYPE (type1); \
6864 if (POINTER_TYPE_P (type0) \
6865 && comptypes (TREE_TYPE (type0), type1) \
6866 && !(TREE_CODE (first_arg) == PARM_DECL \
6867 && C_ARRAY_PARAMETER (first_arg) \
6868 && warn_sizeof_array_argument)) \
6870 auto_diagnostic_group d; \
6871 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6872 "division %<sizeof (%T) / sizeof (%T)%> " \
6873 "does not compute the number of array " \
6874 "elements", \
6875 type0, type1)) \
6876 if (DECL_P (first_arg)) \
6877 inform (DECL_SOURCE_LOCATION (first_arg), \
6878 "first %<sizeof%> operand was declared here"); \
6881 break; \
6882 default: \
6883 break; \
6885 stack[sp - 1].expr \
6886 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6887 stack[sp - 1].expr, true, true); \
6888 stack[sp].expr \
6889 = convert_lvalue_to_rvalue (stack[sp].loc, \
6890 stack[sp].expr, true, true); \
6891 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6892 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6893 && ((1 << stack[sp].prec) \
6894 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6895 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6896 && stack[sp].op != TRUNC_MOD_EXPR \
6897 && stack[0].expr.value != error_mark_node \
6898 && stack[1].expr.value != error_mark_node \
6899 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6900 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6901 stack[0].expr.value \
6902 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6903 stack[0].expr.value, stack[1].expr.value); \
6904 else \
6905 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6906 stack[sp].op, \
6907 stack[sp - 1].expr, \
6908 stack[sp].expr); \
6909 sp--; \
6910 } while (0)
6911 gcc_assert (!after || c_dialect_objc ());
6912 stack[0].loc = c_parser_peek_token (parser)->location;
6913 stack[0].expr = c_parser_cast_expression (parser, after);
6914 stack[0].prec = PREC_NONE;
6915 stack[0].sizeof_arg = c_last_sizeof_arg;
6916 sp = 0;
6917 while (true)
6919 enum c_parser_prec oprec;
6920 enum tree_code ocode;
6921 source_range src_range;
6922 if (parser->error)
6923 goto out;
6924 switch (c_parser_peek_token (parser)->type)
6926 case CPP_MULT:
6927 oprec = PREC_MULT;
6928 ocode = MULT_EXPR;
6929 break;
6930 case CPP_DIV:
6931 oprec = PREC_MULT;
6932 ocode = TRUNC_DIV_EXPR;
6933 break;
6934 case CPP_MOD:
6935 oprec = PREC_MULT;
6936 ocode = TRUNC_MOD_EXPR;
6937 break;
6938 case CPP_PLUS:
6939 oprec = PREC_ADD;
6940 ocode = PLUS_EXPR;
6941 break;
6942 case CPP_MINUS:
6943 oprec = PREC_ADD;
6944 ocode = MINUS_EXPR;
6945 break;
6946 case CPP_LSHIFT:
6947 oprec = PREC_SHIFT;
6948 ocode = LSHIFT_EXPR;
6949 break;
6950 case CPP_RSHIFT:
6951 oprec = PREC_SHIFT;
6952 ocode = RSHIFT_EXPR;
6953 break;
6954 case CPP_LESS:
6955 oprec = PREC_REL;
6956 ocode = LT_EXPR;
6957 break;
6958 case CPP_GREATER:
6959 oprec = PREC_REL;
6960 ocode = GT_EXPR;
6961 break;
6962 case CPP_LESS_EQ:
6963 oprec = PREC_REL;
6964 ocode = LE_EXPR;
6965 break;
6966 case CPP_GREATER_EQ:
6967 oprec = PREC_REL;
6968 ocode = GE_EXPR;
6969 break;
6970 case CPP_EQ_EQ:
6971 oprec = PREC_EQ;
6972 ocode = EQ_EXPR;
6973 break;
6974 case CPP_NOT_EQ:
6975 oprec = PREC_EQ;
6976 ocode = NE_EXPR;
6977 break;
6978 case CPP_AND:
6979 oprec = PREC_BITAND;
6980 ocode = BIT_AND_EXPR;
6981 break;
6982 case CPP_XOR:
6983 oprec = PREC_BITXOR;
6984 ocode = BIT_XOR_EXPR;
6985 break;
6986 case CPP_OR:
6987 oprec = PREC_BITOR;
6988 ocode = BIT_IOR_EXPR;
6989 break;
6990 case CPP_AND_AND:
6991 oprec = PREC_LOGAND;
6992 ocode = TRUTH_ANDIF_EXPR;
6993 break;
6994 case CPP_OR_OR:
6995 oprec = PREC_LOGOR;
6996 ocode = TRUTH_ORIF_EXPR;
6997 break;
6998 default:
6999 /* Not a binary operator, so end of the binary
7000 expression. */
7001 goto out;
7003 binary_loc = c_parser_peek_token (parser)->location;
7004 while (oprec <= stack[sp].prec)
7005 POP;
7006 c_parser_consume_token (parser);
7007 switch (ocode)
7009 case TRUTH_ANDIF_EXPR:
7010 src_range = stack[sp].expr.src_range;
7011 stack[sp].expr
7012 = convert_lvalue_to_rvalue (stack[sp].loc,
7013 stack[sp].expr, true, true);
7014 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7015 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7016 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7017 == truthvalue_false_node);
7018 set_c_expr_source_range (&stack[sp].expr, src_range);
7019 break;
7020 case TRUTH_ORIF_EXPR:
7021 src_range = stack[sp].expr.src_range;
7022 stack[sp].expr
7023 = convert_lvalue_to_rvalue (stack[sp].loc,
7024 stack[sp].expr, true, true);
7025 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7026 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7027 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7028 == truthvalue_true_node);
7029 set_c_expr_source_range (&stack[sp].expr, src_range);
7030 break;
7031 default:
7032 break;
7034 sp++;
7035 stack[sp].loc = binary_loc;
7036 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7037 stack[sp].prec = oprec;
7038 stack[sp].op = ocode;
7039 stack[sp].sizeof_arg = c_last_sizeof_arg;
7041 out:
7042 while (sp > 0)
7043 POP;
7044 return stack[0].expr;
7045 #undef POP
7048 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7049 is not NULL then it is an Objective-C message expression which is the
7050 primary-expression starting the expression as an initializer.
7052 cast-expression:
7053 unary-expression
7054 ( type-name ) unary-expression
7057 static struct c_expr
7058 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7060 location_t cast_loc = c_parser_peek_token (parser)->location;
7061 gcc_assert (!after || c_dialect_objc ());
7062 if (after)
7063 return c_parser_postfix_expression_after_primary (parser,
7064 cast_loc, *after);
7065 /* If the expression begins with a parenthesized type name, it may
7066 be either a cast or a compound literal; we need to see whether
7067 the next character is '{' to tell the difference. If not, it is
7068 an unary expression. Full detection of unknown typenames here
7069 would require a 3-token lookahead. */
7070 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7071 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7073 struct c_type_name *type_name;
7074 struct c_expr ret;
7075 struct c_expr expr;
7076 matching_parens parens;
7077 parens.consume_open (parser);
7078 type_name = c_parser_type_name (parser, true);
7079 parens.skip_until_found_close (parser);
7080 if (type_name == NULL)
7082 ret.set_error ();
7083 ret.original_code = ERROR_MARK;
7084 ret.original_type = NULL;
7085 return ret;
7088 /* Save casted types in the function's used types hash table. */
7089 used_types_insert (type_name->specs->type);
7091 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7092 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7093 cast_loc);
7094 if (type_name->specs->alignas_p)
7095 error_at (type_name->specs->locations[cdw_alignas],
7096 "alignment specified for type name in cast");
7098 location_t expr_loc = c_parser_peek_token (parser)->location;
7099 expr = c_parser_cast_expression (parser, NULL);
7100 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7102 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7103 if (ret.value && expr.value)
7104 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7105 ret.original_code = ERROR_MARK;
7106 ret.original_type = NULL;
7107 return ret;
7109 else
7110 return c_parser_unary_expression (parser);
7113 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7115 unary-expression:
7116 postfix-expression
7117 ++ unary-expression
7118 -- unary-expression
7119 unary-operator cast-expression
7120 sizeof unary-expression
7121 sizeof ( type-name )
7123 unary-operator: one of
7124 & * + - ~ !
7126 GNU extensions:
7128 unary-expression:
7129 __alignof__ unary-expression
7130 __alignof__ ( type-name )
7131 && identifier
7133 (C11 permits _Alignof with type names only.)
7135 unary-operator: one of
7136 __extension__ __real__ __imag__
7138 Transactional Memory:
7140 unary-expression:
7141 transaction-expression
7143 In addition, the GNU syntax treats ++ and -- as unary operators, so
7144 they may be applied to cast expressions with errors for non-lvalues
7145 given later. */
7147 static struct c_expr
7148 c_parser_unary_expression (c_parser *parser)
7150 int ext;
7151 struct c_expr ret, op;
7152 location_t op_loc = c_parser_peek_token (parser)->location;
7153 location_t exp_loc;
7154 location_t finish;
7155 ret.original_code = ERROR_MARK;
7156 ret.original_type = NULL;
7157 switch (c_parser_peek_token (parser)->type)
7159 case CPP_PLUS_PLUS:
7160 c_parser_consume_token (parser);
7161 exp_loc = c_parser_peek_token (parser)->location;
7162 op = c_parser_cast_expression (parser, NULL);
7164 op = default_function_array_read_conversion (exp_loc, op);
7165 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7166 case CPP_MINUS_MINUS:
7167 c_parser_consume_token (parser);
7168 exp_loc = c_parser_peek_token (parser)->location;
7169 op = c_parser_cast_expression (parser, NULL);
7171 op = default_function_array_read_conversion (exp_loc, op);
7172 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7173 case CPP_AND:
7174 c_parser_consume_token (parser);
7175 op = c_parser_cast_expression (parser, NULL);
7176 mark_exp_read (op.value);
7177 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7178 case CPP_MULT:
7180 c_parser_consume_token (parser);
7181 exp_loc = c_parser_peek_token (parser)->location;
7182 op = c_parser_cast_expression (parser, NULL);
7183 finish = op.get_finish ();
7184 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7185 location_t combined_loc = make_location (op_loc, op_loc, finish);
7186 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7187 ret.src_range.m_start = op_loc;
7188 ret.src_range.m_finish = finish;
7189 return ret;
7191 case CPP_PLUS:
7192 if (!c_dialect_objc () && !in_system_header_at (input_location))
7193 warning_at (op_loc,
7194 OPT_Wtraditional,
7195 "traditional C rejects the unary plus operator");
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, CONVERT_EXPR, op);
7201 case CPP_MINUS:
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, NEGATE_EXPR, op);
7207 case CPP_COMPL:
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, BIT_NOT_EXPR, op);
7213 case CPP_NOT:
7214 c_parser_consume_token (parser);
7215 exp_loc = c_parser_peek_token (parser)->location;
7216 op = c_parser_cast_expression (parser, NULL);
7217 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7218 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7219 case CPP_AND_AND:
7220 /* Refer to the address of a label as a pointer. */
7221 c_parser_consume_token (parser);
7222 if (c_parser_next_token_is (parser, CPP_NAME))
7224 ret.value = finish_label_address_expr
7225 (c_parser_peek_token (parser)->value, op_loc);
7226 set_c_expr_source_range (&ret, op_loc,
7227 c_parser_peek_token (parser)->get_finish ());
7228 c_parser_consume_token (parser);
7230 else
7232 c_parser_error (parser, "expected identifier");
7233 ret.set_error ();
7235 return ret;
7236 case CPP_KEYWORD:
7237 switch (c_parser_peek_token (parser)->keyword)
7239 case RID_SIZEOF:
7240 return c_parser_sizeof_expression (parser);
7241 case RID_ALIGNOF:
7242 return c_parser_alignof_expression (parser);
7243 case RID_EXTENSION:
7244 c_parser_consume_token (parser);
7245 ext = disable_extension_diagnostics ();
7246 ret = c_parser_cast_expression (parser, NULL);
7247 restore_extension_diagnostics (ext);
7248 return ret;
7249 case RID_REALPART:
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, REALPART_EXPR, op);
7255 case RID_IMAGPART:
7256 c_parser_consume_token (parser);
7257 exp_loc = c_parser_peek_token (parser)->location;
7258 op = c_parser_cast_expression (parser, NULL);
7259 op = default_function_array_conversion (exp_loc, op);
7260 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7261 case RID_TRANSACTION_ATOMIC:
7262 case RID_TRANSACTION_RELAXED:
7263 return c_parser_transaction_expression (parser,
7264 c_parser_peek_token (parser)->keyword);
7265 default:
7266 return c_parser_postfix_expression (parser);
7268 default:
7269 return c_parser_postfix_expression (parser);
7273 /* Parse a sizeof expression. */
7275 static struct c_expr
7276 c_parser_sizeof_expression (c_parser *parser)
7278 struct c_expr expr;
7279 struct c_expr result;
7280 location_t expr_loc;
7281 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7283 location_t start;
7284 location_t finish = UNKNOWN_LOCATION;
7286 start = c_parser_peek_token (parser)->location;
7288 c_parser_consume_token (parser);
7289 c_inhibit_evaluation_warnings++;
7290 in_sizeof++;
7291 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7292 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7294 /* Either sizeof ( type-name ) or sizeof unary-expression
7295 starting with a compound literal. */
7296 struct c_type_name *type_name;
7297 matching_parens parens;
7298 parens.consume_open (parser);
7299 expr_loc = c_parser_peek_token (parser)->location;
7300 type_name = c_parser_type_name (parser, true);
7301 parens.skip_until_found_close (parser);
7302 finish = parser->tokens_buf[0].location;
7303 if (type_name == NULL)
7305 struct c_expr ret;
7306 c_inhibit_evaluation_warnings--;
7307 in_sizeof--;
7308 ret.set_error ();
7309 ret.original_code = ERROR_MARK;
7310 ret.original_type = NULL;
7311 return ret;
7313 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7315 expr = c_parser_postfix_expression_after_paren_type (parser,
7316 type_name,
7317 expr_loc);
7318 finish = expr.get_finish ();
7319 goto sizeof_expr;
7321 /* sizeof ( type-name ). */
7322 if (type_name->specs->alignas_p)
7323 error_at (type_name->specs->locations[cdw_alignas],
7324 "alignment specified for type name in %<sizeof%>");
7325 c_inhibit_evaluation_warnings--;
7326 in_sizeof--;
7327 result = c_expr_sizeof_type (expr_loc, type_name);
7329 else
7331 expr_loc = c_parser_peek_token (parser)->location;
7332 expr = c_parser_unary_expression (parser);
7333 finish = expr.get_finish ();
7334 sizeof_expr:
7335 c_inhibit_evaluation_warnings--;
7336 in_sizeof--;
7337 mark_exp_read (expr.value);
7338 if (TREE_CODE (expr.value) == COMPONENT_REF
7339 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7340 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7341 result = c_expr_sizeof_expr (expr_loc, expr);
7343 if (finish != UNKNOWN_LOCATION)
7344 set_c_expr_source_range (&result, start, finish);
7345 return result;
7348 /* Parse an alignof expression. */
7350 static struct c_expr
7351 c_parser_alignof_expression (c_parser *parser)
7353 struct c_expr expr;
7354 location_t start_loc = c_parser_peek_token (parser)->location;
7355 location_t end_loc;
7356 tree alignof_spelling = c_parser_peek_token (parser)->value;
7357 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7358 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7359 "_Alignof") == 0;
7360 /* A diagnostic is not required for the use of this identifier in
7361 the implementation namespace; only diagnose it for the C11
7362 spelling because of existing code using the other spellings. */
7363 if (is_c11_alignof)
7365 if (flag_isoc99)
7366 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7367 alignof_spelling);
7368 else
7369 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7370 alignof_spelling);
7372 c_parser_consume_token (parser);
7373 c_inhibit_evaluation_warnings++;
7374 in_alignof++;
7375 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7376 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7378 /* Either __alignof__ ( type-name ) or __alignof__
7379 unary-expression starting with a compound literal. */
7380 location_t loc;
7381 struct c_type_name *type_name;
7382 struct c_expr ret;
7383 matching_parens parens;
7384 parens.consume_open (parser);
7385 loc = c_parser_peek_token (parser)->location;
7386 type_name = c_parser_type_name (parser, true);
7387 end_loc = c_parser_peek_token (parser)->location;
7388 parens.skip_until_found_close (parser);
7389 if (type_name == NULL)
7391 struct c_expr ret;
7392 c_inhibit_evaluation_warnings--;
7393 in_alignof--;
7394 ret.set_error ();
7395 ret.original_code = ERROR_MARK;
7396 ret.original_type = NULL;
7397 return ret;
7399 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7401 expr = c_parser_postfix_expression_after_paren_type (parser,
7402 type_name,
7403 loc);
7404 goto alignof_expr;
7406 /* alignof ( type-name ). */
7407 if (type_name->specs->alignas_p)
7408 error_at (type_name->specs->locations[cdw_alignas],
7409 "alignment specified for type name in %qE",
7410 alignof_spelling);
7411 c_inhibit_evaluation_warnings--;
7412 in_alignof--;
7413 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7414 NULL, NULL),
7415 false, is_c11_alignof, 1);
7416 ret.original_code = ERROR_MARK;
7417 ret.original_type = NULL;
7418 set_c_expr_source_range (&ret, start_loc, end_loc);
7419 return ret;
7421 else
7423 struct c_expr ret;
7424 expr = c_parser_unary_expression (parser);
7425 end_loc = expr.src_range.m_finish;
7426 alignof_expr:
7427 mark_exp_read (expr.value);
7428 c_inhibit_evaluation_warnings--;
7429 in_alignof--;
7430 if (is_c11_alignof)
7431 pedwarn (start_loc,
7432 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7433 alignof_spelling);
7434 ret.value = c_alignof_expr (start_loc, expr.value);
7435 ret.original_code = ERROR_MARK;
7436 ret.original_type = NULL;
7437 set_c_expr_source_range (&ret, start_loc, end_loc);
7438 return ret;
7442 /* Helper function to read arguments of builtins which are interfaces
7443 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7444 others. The name of the builtin is passed using BNAME parameter.
7445 Function returns true if there were no errors while parsing and
7446 stores the arguments in CEXPR_LIST. If it returns true,
7447 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7448 parenthesis. */
7449 static bool
7450 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7451 vec<c_expr_t, va_gc> **ret_cexpr_list,
7452 bool choose_expr_p,
7453 location_t *out_close_paren_loc)
7455 location_t loc = c_parser_peek_token (parser)->location;
7456 vec<c_expr_t, va_gc> *cexpr_list;
7457 c_expr_t expr;
7458 bool saved_force_folding_builtin_constant_p;
7460 *ret_cexpr_list = NULL;
7461 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7463 error_at (loc, "cannot take address of %qs", bname);
7464 return false;
7467 c_parser_consume_token (parser);
7469 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7471 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7472 c_parser_consume_token (parser);
7473 return true;
7476 saved_force_folding_builtin_constant_p
7477 = force_folding_builtin_constant_p;
7478 force_folding_builtin_constant_p |= choose_expr_p;
7479 expr = c_parser_expr_no_commas (parser, NULL);
7480 force_folding_builtin_constant_p
7481 = saved_force_folding_builtin_constant_p;
7482 vec_alloc (cexpr_list, 1);
7483 vec_safe_push (cexpr_list, expr);
7484 while (c_parser_next_token_is (parser, CPP_COMMA))
7486 c_parser_consume_token (parser);
7487 expr = c_parser_expr_no_commas (parser, NULL);
7488 vec_safe_push (cexpr_list, expr);
7491 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7492 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7493 return false;
7495 *ret_cexpr_list = cexpr_list;
7496 return true;
7499 /* This represents a single generic-association. */
7501 struct c_generic_association
7503 /* The location of the starting token of the type. */
7504 location_t type_location;
7505 /* The association's type, or NULL_TREE for 'default'. */
7506 tree type;
7507 /* The association's expression. */
7508 struct c_expr expression;
7511 /* Parse a generic-selection. (C11 6.5.1.1).
7513 generic-selection:
7514 _Generic ( assignment-expression , generic-assoc-list )
7516 generic-assoc-list:
7517 generic-association
7518 generic-assoc-list , generic-association
7520 generic-association:
7521 type-name : assignment-expression
7522 default : assignment-expression
7525 static struct c_expr
7526 c_parser_generic_selection (c_parser *parser)
7528 struct c_expr selector, error_expr;
7529 tree selector_type;
7530 struct c_generic_association matched_assoc;
7531 bool match_found = false;
7532 location_t generic_loc, selector_loc;
7534 error_expr.original_code = ERROR_MARK;
7535 error_expr.original_type = NULL;
7536 error_expr.set_error ();
7537 matched_assoc.type_location = UNKNOWN_LOCATION;
7538 matched_assoc.type = NULL_TREE;
7539 matched_assoc.expression = error_expr;
7541 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7542 generic_loc = c_parser_peek_token (parser)->location;
7543 c_parser_consume_token (parser);
7544 if (flag_isoc99)
7545 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7546 "ISO C99 does not support %<_Generic%>");
7547 else
7548 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7549 "ISO C90 does not support %<_Generic%>");
7551 matching_parens parens;
7552 if (!parens.require_open (parser))
7553 return error_expr;
7555 c_inhibit_evaluation_warnings++;
7556 selector_loc = c_parser_peek_token (parser)->location;
7557 selector = c_parser_expr_no_commas (parser, NULL);
7558 selector = default_function_array_conversion (selector_loc, selector);
7559 c_inhibit_evaluation_warnings--;
7561 if (selector.value == error_mark_node)
7563 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7564 return selector;
7566 selector_type = TREE_TYPE (selector.value);
7567 /* In ISO C terms, rvalues (including the controlling expression of
7568 _Generic) do not have qualified types. */
7569 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7570 selector_type = TYPE_MAIN_VARIANT (selector_type);
7571 /* In ISO C terms, _Noreturn is not part of the type of expressions
7572 such as &abort, but in GCC it is represented internally as a type
7573 qualifier. */
7574 if (FUNCTION_POINTER_TYPE_P (selector_type)
7575 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7576 selector_type
7577 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7579 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7581 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7582 return error_expr;
7585 auto_vec<c_generic_association> associations;
7586 while (1)
7588 struct c_generic_association assoc, *iter;
7589 unsigned int ix;
7590 c_token *token = c_parser_peek_token (parser);
7592 assoc.type_location = token->location;
7593 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7595 c_parser_consume_token (parser);
7596 assoc.type = NULL_TREE;
7598 else
7600 struct c_type_name *type_name;
7602 type_name = c_parser_type_name (parser);
7603 if (type_name == NULL)
7605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7606 return error_expr;
7608 assoc.type = groktypename (type_name, NULL, NULL);
7609 if (assoc.type == error_mark_node)
7611 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7612 return error_expr;
7615 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7616 error_at (assoc.type_location,
7617 "%<_Generic%> association has function type");
7618 else if (!COMPLETE_TYPE_P (assoc.type))
7619 error_at (assoc.type_location,
7620 "%<_Generic%> association has incomplete type");
7622 if (variably_modified_type_p (assoc.type, NULL_TREE))
7623 error_at (assoc.type_location,
7624 "%<_Generic%> association has "
7625 "variable length type");
7628 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7631 return error_expr;
7634 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7635 if (assoc.expression.value == error_mark_node)
7637 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7638 return error_expr;
7641 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7643 if (assoc.type == NULL_TREE)
7645 if (iter->type == NULL_TREE)
7647 error_at (assoc.type_location,
7648 "duplicate %<default%> case in %<_Generic%>");
7649 inform (iter->type_location, "original %<default%> is here");
7652 else if (iter->type != NULL_TREE)
7654 if (comptypes (assoc.type, iter->type))
7656 error_at (assoc.type_location,
7657 "%<_Generic%> specifies two compatible types");
7658 inform (iter->type_location, "compatible type is here");
7663 if (assoc.type == NULL_TREE)
7665 if (!match_found)
7667 matched_assoc = assoc;
7668 match_found = true;
7671 else if (comptypes (assoc.type, selector_type))
7673 if (!match_found || matched_assoc.type == NULL_TREE)
7675 matched_assoc = assoc;
7676 match_found = true;
7678 else
7680 error_at (assoc.type_location,
7681 "%<_Generic%> selector matches multiple associations");
7682 inform (matched_assoc.type_location,
7683 "other match is here");
7687 associations.safe_push (assoc);
7689 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7690 break;
7691 c_parser_consume_token (parser);
7694 if (!parens.require_close (parser))
7696 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7697 return error_expr;
7700 if (!match_found)
7702 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7703 "compatible with any association",
7704 selector_type);
7705 return error_expr;
7708 return matched_assoc.expression;
7711 /* Check the validity of a function pointer argument *EXPR (argument
7712 position POS) to __builtin_tgmath. Return the number of function
7713 arguments if possibly valid; return 0 having reported an error if
7714 not valid. */
7716 static unsigned int
7717 check_tgmath_function (c_expr *expr, unsigned int pos)
7719 tree type = TREE_TYPE (expr->value);
7720 if (!FUNCTION_POINTER_TYPE_P (type))
7722 error_at (expr->get_location (),
7723 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7724 pos);
7725 return 0;
7727 type = TREE_TYPE (type);
7728 if (!prototype_p (type))
7730 error_at (expr->get_location (),
7731 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7732 return 0;
7734 if (stdarg_p (type))
7736 error_at (expr->get_location (),
7737 "argument %u of %<__builtin_tgmath%> has variable arguments",
7738 pos);
7739 return 0;
7741 unsigned int nargs = 0;
7742 function_args_iterator iter;
7743 tree t;
7744 FOREACH_FUNCTION_ARGS (type, t, iter)
7746 if (t == void_type_node)
7747 break;
7748 nargs++;
7750 if (nargs == 0)
7752 error_at (expr->get_location (),
7753 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7754 return 0;
7756 return nargs;
7759 /* Ways in which a parameter or return value of a type-generic macro
7760 may vary between the different functions the macro may call. */
7761 enum tgmath_parm_kind
7763 tgmath_fixed, tgmath_real, tgmath_complex
7766 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7767 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7768 call c_parser_postfix_expression_after_paren_type on encountering them.
7770 postfix-expression:
7771 primary-expression
7772 postfix-expression [ expression ]
7773 postfix-expression ( argument-expression-list[opt] )
7774 postfix-expression . identifier
7775 postfix-expression -> identifier
7776 postfix-expression ++
7777 postfix-expression --
7778 ( type-name ) { initializer-list }
7779 ( type-name ) { initializer-list , }
7781 argument-expression-list:
7782 argument-expression
7783 argument-expression-list , argument-expression
7785 primary-expression:
7786 identifier
7787 constant
7788 string-literal
7789 ( expression )
7790 generic-selection
7792 GNU extensions:
7794 primary-expression:
7795 __func__
7796 (treated as a keyword in GNU C)
7797 __FUNCTION__
7798 __PRETTY_FUNCTION__
7799 ( compound-statement )
7800 __builtin_va_arg ( assignment-expression , type-name )
7801 __builtin_offsetof ( type-name , offsetof-member-designator )
7802 __builtin_choose_expr ( assignment-expression ,
7803 assignment-expression ,
7804 assignment-expression )
7805 __builtin_types_compatible_p ( type-name , type-name )
7806 __builtin_tgmath ( expr-list )
7807 __builtin_complex ( assignment-expression , assignment-expression )
7808 __builtin_shuffle ( assignment-expression , assignment-expression )
7809 __builtin_shuffle ( assignment-expression ,
7810 assignment-expression ,
7811 assignment-expression, )
7813 offsetof-member-designator:
7814 identifier
7815 offsetof-member-designator . identifier
7816 offsetof-member-designator [ expression ]
7818 Objective-C:
7820 primary-expression:
7821 [ objc-receiver objc-message-args ]
7822 @selector ( objc-selector-arg )
7823 @protocol ( identifier )
7824 @encode ( type-name )
7825 objc-string-literal
7826 Classname . identifier
7829 static struct c_expr
7830 c_parser_postfix_expression (c_parser *parser)
7832 struct c_expr expr, e1;
7833 struct c_type_name *t1, *t2;
7834 location_t loc = c_parser_peek_token (parser)->location;
7835 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7836 expr.original_code = ERROR_MARK;
7837 expr.original_type = NULL;
7838 switch (c_parser_peek_token (parser)->type)
7840 case CPP_NUMBER:
7841 expr.value = c_parser_peek_token (parser)->value;
7842 set_c_expr_source_range (&expr, tok_range);
7843 loc = c_parser_peek_token (parser)->location;
7844 c_parser_consume_token (parser);
7845 if (TREE_CODE (expr.value) == FIXED_CST
7846 && !targetm.fixed_point_supported_p ())
7848 error_at (loc, "fixed-point types not supported for this target");
7849 expr.set_error ();
7851 break;
7852 case CPP_CHAR:
7853 case CPP_CHAR16:
7854 case CPP_CHAR32:
7855 case CPP_WCHAR:
7856 expr.value = c_parser_peek_token (parser)->value;
7857 /* For the purpose of warning when a pointer is compared with
7858 a zero character constant. */
7859 expr.original_type = char_type_node;
7860 set_c_expr_source_range (&expr, tok_range);
7861 c_parser_consume_token (parser);
7862 break;
7863 case CPP_STRING:
7864 case CPP_STRING16:
7865 case CPP_STRING32:
7866 case CPP_WSTRING:
7867 case CPP_UTF8STRING:
7868 expr.value = c_parser_peek_token (parser)->value;
7869 set_c_expr_source_range (&expr, tok_range);
7870 expr.original_code = STRING_CST;
7871 c_parser_consume_token (parser);
7872 break;
7873 case CPP_OBJC_STRING:
7874 gcc_assert (c_dialect_objc ());
7875 expr.value
7876 = objc_build_string_object (c_parser_peek_token (parser)->value);
7877 set_c_expr_source_range (&expr, tok_range);
7878 c_parser_consume_token (parser);
7879 break;
7880 case CPP_NAME:
7881 switch (c_parser_peek_token (parser)->id_kind)
7883 case C_ID_ID:
7885 tree id = c_parser_peek_token (parser)->value;
7886 c_parser_consume_token (parser);
7887 expr.value = build_external_ref (loc, id,
7888 (c_parser_peek_token (parser)->type
7889 == CPP_OPEN_PAREN),
7890 &expr.original_type);
7891 set_c_expr_source_range (&expr, tok_range);
7892 break;
7894 case C_ID_CLASSNAME:
7896 /* Here we parse the Objective-C 2.0 Class.name dot
7897 syntax. */
7898 tree class_name = c_parser_peek_token (parser)->value;
7899 tree component;
7900 c_parser_consume_token (parser);
7901 gcc_assert (c_dialect_objc ());
7902 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7904 expr.set_error ();
7905 break;
7907 if (c_parser_next_token_is_not (parser, CPP_NAME))
7909 c_parser_error (parser, "expected identifier");
7910 expr.set_error ();
7911 break;
7913 c_token *component_tok = c_parser_peek_token (parser);
7914 component = component_tok->value;
7915 location_t end_loc = component_tok->get_finish ();
7916 c_parser_consume_token (parser);
7917 expr.value = objc_build_class_component_ref (class_name,
7918 component);
7919 set_c_expr_source_range (&expr, loc, end_loc);
7920 break;
7922 default:
7923 c_parser_error (parser, "expected expression");
7924 expr.set_error ();
7925 break;
7927 break;
7928 case CPP_OPEN_PAREN:
7929 /* A parenthesized expression, statement expression or compound
7930 literal. */
7931 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7933 /* A statement expression. */
7934 tree stmt;
7935 location_t brace_loc;
7936 c_parser_consume_token (parser);
7937 brace_loc = c_parser_peek_token (parser)->location;
7938 c_parser_consume_token (parser);
7939 /* If we've not yet started the current function's statement list,
7940 or we're in the parameter scope of an old-style function
7941 declaration, statement expressions are not allowed. */
7942 if (!building_stmt_list_p () || old_style_parameter_scope ())
7944 error_at (loc, "braced-group within expression allowed "
7945 "only inside a function");
7946 parser->error = true;
7947 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7948 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7949 expr.set_error ();
7950 break;
7952 stmt = c_begin_stmt_expr ();
7953 c_parser_compound_statement_nostart (parser);
7954 location_t close_loc = c_parser_peek_token (parser)->location;
7955 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7956 "expected %<)%>");
7957 pedwarn (loc, OPT_Wpedantic,
7958 "ISO C forbids braced-groups within expressions");
7959 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7960 set_c_expr_source_range (&expr, loc, close_loc);
7961 mark_exp_read (expr.value);
7963 else
7965 /* A parenthesized expression. */
7966 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7967 c_parser_consume_token (parser);
7968 expr = c_parser_expression (parser);
7969 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7970 TREE_NO_WARNING (expr.value) = 1;
7971 if (expr.original_code != C_MAYBE_CONST_EXPR
7972 && expr.original_code != SIZEOF_EXPR)
7973 expr.original_code = ERROR_MARK;
7974 /* Don't change EXPR.ORIGINAL_TYPE. */
7975 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7976 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7977 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7978 "expected %<)%>", loc_open_paren);
7980 break;
7981 case CPP_KEYWORD:
7982 switch (c_parser_peek_token (parser)->keyword)
7984 case RID_FUNCTION_NAME:
7985 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7986 "%<__FUNCTION__%> predefined identifier");
7987 expr.value = fname_decl (loc,
7988 c_parser_peek_token (parser)->keyword,
7989 c_parser_peek_token (parser)->value);
7990 set_c_expr_source_range (&expr, loc, loc);
7991 c_parser_consume_token (parser);
7992 break;
7993 case RID_PRETTY_FUNCTION_NAME:
7994 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7995 "%<__PRETTY_FUNCTION__%> predefined identifier");
7996 expr.value = fname_decl (loc,
7997 c_parser_peek_token (parser)->keyword,
7998 c_parser_peek_token (parser)->value);
7999 set_c_expr_source_range (&expr, loc, loc);
8000 c_parser_consume_token (parser);
8001 break;
8002 case RID_C99_FUNCTION_NAME:
8003 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8004 "%<__func__%> predefined identifier");
8005 expr.value = fname_decl (loc,
8006 c_parser_peek_token (parser)->keyword,
8007 c_parser_peek_token (parser)->value);
8008 set_c_expr_source_range (&expr, loc, loc);
8009 c_parser_consume_token (parser);
8010 break;
8011 case RID_VA_ARG:
8013 location_t start_loc = loc;
8014 c_parser_consume_token (parser);
8015 matching_parens parens;
8016 if (!parens.require_open (parser))
8018 expr.set_error ();
8019 break;
8021 e1 = c_parser_expr_no_commas (parser, NULL);
8022 mark_exp_read (e1.value);
8023 e1.value = c_fully_fold (e1.value, false, NULL);
8024 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8027 expr.set_error ();
8028 break;
8030 loc = c_parser_peek_token (parser)->location;
8031 t1 = c_parser_type_name (parser);
8032 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8033 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8034 "expected %<)%>");
8035 if (t1 == NULL)
8037 expr.set_error ();
8039 else
8041 tree type_expr = NULL_TREE;
8042 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8043 groktypename (t1, &type_expr, NULL));
8044 if (type_expr)
8046 expr.value = build2 (C_MAYBE_CONST_EXPR,
8047 TREE_TYPE (expr.value), type_expr,
8048 expr.value);
8049 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8051 set_c_expr_source_range (&expr, start_loc, end_loc);
8054 break;
8055 case RID_OFFSETOF:
8057 c_parser_consume_token (parser);
8058 matching_parens parens;
8059 if (!parens.require_open (parser))
8061 expr.set_error ();
8062 break;
8064 t1 = c_parser_type_name (parser);
8065 if (t1 == NULL)
8066 parser->error = true;
8067 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8068 gcc_assert (parser->error);
8069 if (parser->error)
8071 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8072 expr.set_error ();
8073 break;
8075 tree type = groktypename (t1, NULL, NULL);
8076 tree offsetof_ref;
8077 if (type == error_mark_node)
8078 offsetof_ref = error_mark_node;
8079 else
8081 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8082 SET_EXPR_LOCATION (offsetof_ref, loc);
8084 /* Parse the second argument to __builtin_offsetof. We
8085 must have one identifier, and beyond that we want to
8086 accept sub structure and sub array references. */
8087 if (c_parser_next_token_is (parser, CPP_NAME))
8089 c_token *comp_tok = c_parser_peek_token (parser);
8090 offsetof_ref = build_component_ref
8091 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8092 c_parser_consume_token (parser);
8093 while (c_parser_next_token_is (parser, CPP_DOT)
8094 || c_parser_next_token_is (parser,
8095 CPP_OPEN_SQUARE)
8096 || c_parser_next_token_is (parser,
8097 CPP_DEREF))
8099 if (c_parser_next_token_is (parser, CPP_DEREF))
8101 loc = c_parser_peek_token (parser)->location;
8102 offsetof_ref = build_array_ref (loc,
8103 offsetof_ref,
8104 integer_zero_node);
8105 goto do_dot;
8107 else if (c_parser_next_token_is (parser, CPP_DOT))
8109 do_dot:
8110 c_parser_consume_token (parser);
8111 if (c_parser_next_token_is_not (parser,
8112 CPP_NAME))
8114 c_parser_error (parser, "expected identifier");
8115 break;
8117 c_token *comp_tok = c_parser_peek_token (parser);
8118 offsetof_ref = build_component_ref
8119 (loc, offsetof_ref, comp_tok->value,
8120 comp_tok->location);
8121 c_parser_consume_token (parser);
8123 else
8125 struct c_expr ce;
8126 tree idx;
8127 loc = c_parser_peek_token (parser)->location;
8128 c_parser_consume_token (parser);
8129 ce = c_parser_expression (parser);
8130 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8131 idx = ce.value;
8132 idx = c_fully_fold (idx, false, NULL);
8133 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8134 "expected %<]%>");
8135 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8139 else
8140 c_parser_error (parser, "expected identifier");
8141 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8142 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8143 "expected %<)%>");
8144 expr.value = fold_offsetof (offsetof_ref);
8145 set_c_expr_source_range (&expr, loc, end_loc);
8147 break;
8148 case RID_CHOOSE_EXPR:
8150 vec<c_expr_t, va_gc> *cexpr_list;
8151 c_expr_t *e1_p, *e2_p, *e3_p;
8152 tree c;
8153 location_t close_paren_loc;
8155 c_parser_consume_token (parser);
8156 if (!c_parser_get_builtin_args (parser,
8157 "__builtin_choose_expr",
8158 &cexpr_list, true,
8159 &close_paren_loc))
8161 expr.set_error ();
8162 break;
8165 if (vec_safe_length (cexpr_list) != 3)
8167 error_at (loc, "wrong number of arguments to "
8168 "%<__builtin_choose_expr%>");
8169 expr.set_error ();
8170 break;
8173 e1_p = &(*cexpr_list)[0];
8174 e2_p = &(*cexpr_list)[1];
8175 e3_p = &(*cexpr_list)[2];
8177 c = e1_p->value;
8178 mark_exp_read (e2_p->value);
8179 mark_exp_read (e3_p->value);
8180 if (TREE_CODE (c) != INTEGER_CST
8181 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8182 error_at (loc,
8183 "first argument to %<__builtin_choose_expr%> not"
8184 " a constant");
8185 constant_expression_warning (c);
8186 expr = integer_zerop (c) ? *e3_p : *e2_p;
8187 set_c_expr_source_range (&expr, loc, close_paren_loc);
8188 break;
8190 case RID_TYPES_COMPATIBLE_P:
8192 c_parser_consume_token (parser);
8193 matching_parens parens;
8194 if (!parens.require_open (parser))
8196 expr.set_error ();
8197 break;
8199 t1 = c_parser_type_name (parser);
8200 if (t1 == NULL)
8202 expr.set_error ();
8203 break;
8205 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8207 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8208 expr.set_error ();
8209 break;
8211 t2 = c_parser_type_name (parser);
8212 if (t2 == NULL)
8214 expr.set_error ();
8215 break;
8217 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8218 parens.skip_until_found_close (parser);
8219 tree e1, e2;
8220 e1 = groktypename (t1, NULL, NULL);
8221 e2 = groktypename (t2, NULL, NULL);
8222 if (e1 == error_mark_node || e2 == error_mark_node)
8224 expr.set_error ();
8225 break;
8228 e1 = TYPE_MAIN_VARIANT (e1);
8229 e2 = TYPE_MAIN_VARIANT (e2);
8231 expr.value
8232 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8233 set_c_expr_source_range (&expr, loc, close_paren_loc);
8235 break;
8236 case RID_BUILTIN_TGMATH:
8238 vec<c_expr_t, va_gc> *cexpr_list;
8239 location_t close_paren_loc;
8241 c_parser_consume_token (parser);
8242 if (!c_parser_get_builtin_args (parser,
8243 "__builtin_tgmath",
8244 &cexpr_list, false,
8245 &close_paren_loc))
8247 expr.set_error ();
8248 break;
8251 if (vec_safe_length (cexpr_list) < 3)
8253 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8254 expr.set_error ();
8255 break;
8258 unsigned int i;
8259 c_expr_t *p;
8260 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8261 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8262 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8263 if (nargs == 0)
8265 expr.set_error ();
8266 break;
8268 if (vec_safe_length (cexpr_list) < nargs)
8270 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8271 expr.set_error ();
8272 break;
8274 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8275 if (num_functions < 2)
8277 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8278 expr.set_error ();
8279 break;
8282 /* The first NUM_FUNCTIONS expressions are the function
8283 pointers. The remaining NARGS expressions are the
8284 arguments that are to be passed to one of those
8285 functions, chosen following <tgmath.h> rules. */
8286 for (unsigned int j = 1; j < num_functions; j++)
8288 unsigned int this_nargs
8289 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8290 if (this_nargs == 0)
8292 expr.set_error ();
8293 goto out;
8295 if (this_nargs != nargs)
8297 error_at ((*cexpr_list)[j].get_location (),
8298 "argument %u of %<__builtin_tgmath%> has "
8299 "wrong number of arguments", j + 1);
8300 expr.set_error ();
8301 goto out;
8305 /* The functions all have the same number of arguments.
8306 Determine whether arguments and return types vary in
8307 ways permitted for <tgmath.h> functions. */
8308 /* The first entry in each of these vectors is for the
8309 return type, subsequent entries for parameter
8310 types. */
8311 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8312 auto_vec<tree> parm_first (nargs + 1);
8313 auto_vec<bool> parm_complex (nargs + 1);
8314 auto_vec<bool> parm_varies (nargs + 1);
8315 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8316 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8317 parm_first.quick_push (first_ret);
8318 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8319 parm_varies.quick_push (false);
8320 function_args_iterator iter;
8321 tree t;
8322 unsigned int argpos;
8323 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8325 if (t == void_type_node)
8326 break;
8327 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8328 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8329 parm_varies.quick_push (false);
8331 for (unsigned int j = 1; j < num_functions; j++)
8333 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8334 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8335 if (ret != parm_first[0])
8337 parm_varies[0] = true;
8338 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8339 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8341 error_at ((*cexpr_list)[0].get_location (),
8342 "invalid type-generic return type for "
8343 "argument %u of %<__builtin_tgmath%>",
8345 expr.set_error ();
8346 goto out;
8348 if (!SCALAR_FLOAT_TYPE_P (ret)
8349 && !COMPLEX_FLOAT_TYPE_P (ret))
8351 error_at ((*cexpr_list)[j].get_location (),
8352 "invalid type-generic return type for "
8353 "argument %u of %<__builtin_tgmath%>",
8354 j + 1);
8355 expr.set_error ();
8356 goto out;
8359 if (TREE_CODE (ret) == COMPLEX_TYPE)
8360 parm_complex[0] = true;
8361 argpos = 1;
8362 FOREACH_FUNCTION_ARGS (type, t, iter)
8364 if (t == void_type_node)
8365 break;
8366 t = TYPE_MAIN_VARIANT (t);
8367 if (t != parm_first[argpos])
8369 parm_varies[argpos] = true;
8370 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8371 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8373 error_at ((*cexpr_list)[0].get_location (),
8374 "invalid type-generic type for "
8375 "argument %u of argument %u of "
8376 "%<__builtin_tgmath%>", argpos, 1);
8377 expr.set_error ();
8378 goto out;
8380 if (!SCALAR_FLOAT_TYPE_P (t)
8381 && !COMPLEX_FLOAT_TYPE_P (t))
8383 error_at ((*cexpr_list)[j].get_location (),
8384 "invalid type-generic type for "
8385 "argument %u of argument %u of "
8386 "%<__builtin_tgmath%>", argpos, j + 1);
8387 expr.set_error ();
8388 goto out;
8391 if (TREE_CODE (t) == COMPLEX_TYPE)
8392 parm_complex[argpos] = true;
8393 argpos++;
8396 enum tgmath_parm_kind max_variation = tgmath_fixed;
8397 for (unsigned int j = 0; j <= nargs; j++)
8399 enum tgmath_parm_kind this_kind;
8400 if (parm_varies[j])
8402 if (parm_complex[j])
8403 max_variation = this_kind = tgmath_complex;
8404 else
8406 this_kind = tgmath_real;
8407 if (max_variation != tgmath_complex)
8408 max_variation = tgmath_real;
8411 else
8412 this_kind = tgmath_fixed;
8413 parm_kind.quick_push (this_kind);
8415 if (max_variation == tgmath_fixed)
8417 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8418 "all have the same type");
8419 expr.set_error ();
8420 break;
8423 /* Identify a parameter (not the return type) that varies,
8424 including with complex types if any variation includes
8425 complex types; there must be at least one such
8426 parameter. */
8427 unsigned int tgarg = 0;
8428 for (unsigned int j = 1; j <= nargs; j++)
8429 if (parm_kind[j] == max_variation)
8431 tgarg = j;
8432 break;
8434 if (tgarg == 0)
8436 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8437 "lack type-generic parameter");
8438 expr.set_error ();
8439 break;
8442 /* Determine the type of the relevant parameter for each
8443 function. */
8444 auto_vec<tree> tg_type (num_functions);
8445 for (unsigned int j = 0; j < num_functions; j++)
8447 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8448 argpos = 1;
8449 FOREACH_FUNCTION_ARGS (type, t, iter)
8451 if (argpos == tgarg)
8453 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8454 break;
8456 argpos++;
8460 /* Verify that the corresponding types are different for
8461 all the listed functions. Also determine whether all
8462 the types are complex, whether all the types are
8463 standard or binary, and whether all the types are
8464 decimal. */
8465 bool all_complex = true;
8466 bool all_binary = true;
8467 bool all_decimal = true;
8468 hash_set<tree> tg_types;
8469 FOR_EACH_VEC_ELT (tg_type, i, t)
8471 if (TREE_CODE (t) == COMPLEX_TYPE)
8472 all_decimal = false;
8473 else
8475 all_complex = false;
8476 if (DECIMAL_FLOAT_TYPE_P (t))
8477 all_binary = false;
8478 else
8479 all_decimal = false;
8481 if (tg_types.add (t))
8483 error_at ((*cexpr_list)[i].get_location (),
8484 "duplicate type-generic parameter type for "
8485 "function argument %u of %<__builtin_tgmath%>",
8486 i + 1);
8487 expr.set_error ();
8488 goto out;
8492 /* Verify that other parameters and the return type whose
8493 types vary have their types varying in the correct
8494 way. */
8495 for (unsigned int j = 0; j < num_functions; j++)
8497 tree exp_type = tg_type[j];
8498 tree exp_real_type = exp_type;
8499 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8500 exp_real_type = TREE_TYPE (exp_type);
8501 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8502 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8503 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8504 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8506 error_at ((*cexpr_list)[j].get_location (),
8507 "bad return type for function argument %u "
8508 "of %<__builtin_tgmath%>", j + 1);
8509 expr.set_error ();
8510 goto out;
8512 argpos = 1;
8513 FOREACH_FUNCTION_ARGS (type, t, iter)
8515 if (t == void_type_node)
8516 break;
8517 t = TYPE_MAIN_VARIANT (t);
8518 if ((parm_kind[argpos] == tgmath_complex
8519 && t != exp_type)
8520 || (parm_kind[argpos] == tgmath_real
8521 && t != exp_real_type))
8523 error_at ((*cexpr_list)[j].get_location (),
8524 "bad type for argument %u of "
8525 "function argument %u of "
8526 "%<__builtin_tgmath%>", argpos, j + 1);
8527 expr.set_error ();
8528 goto out;
8530 argpos++;
8534 /* The functions listed are a valid set of functions for a
8535 <tgmath.h> macro to select between. Identify the
8536 matching function, if any. First, the argument types
8537 must be combined following <tgmath.h> rules. Integer
8538 types are treated as _Decimal64 if any type-generic
8539 argument is decimal, or if the only alternatives for
8540 type-generic arguments are of decimal types, and are
8541 otherwise treated as double (or _Complex double for
8542 complex integer types, or _Float64 or _Complex _Float64
8543 if all the return types are the same _FloatN or
8544 _FloatNx type). After that adjustment, types are
8545 combined following the usual arithmetic conversions.
8546 If the function only accepts complex arguments, a
8547 complex type is produced. */
8548 bool arg_complex = all_complex;
8549 bool arg_binary = all_binary;
8550 bool arg_int_decimal = all_decimal;
8551 for (unsigned int j = 1; j <= nargs; j++)
8553 if (parm_kind[j] == tgmath_fixed)
8554 continue;
8555 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8556 tree type = TREE_TYPE (ce->value);
8557 if (!INTEGRAL_TYPE_P (type)
8558 && !SCALAR_FLOAT_TYPE_P (type)
8559 && TREE_CODE (type) != COMPLEX_TYPE)
8561 error_at (ce->get_location (),
8562 "invalid type of argument %u of type-generic "
8563 "function", j);
8564 expr.set_error ();
8565 goto out;
8567 if (DECIMAL_FLOAT_TYPE_P (type))
8569 arg_int_decimal = true;
8570 if (all_complex)
8572 error_at (ce->get_location (),
8573 "decimal floating-point argument %u to "
8574 "complex-only type-generic function", j);
8575 expr.set_error ();
8576 goto out;
8578 else if (all_binary)
8580 error_at (ce->get_location (),
8581 "decimal floating-point argument %u to "
8582 "binary-only type-generic function", j);
8583 expr.set_error ();
8584 goto out;
8586 else if (arg_complex)
8588 error_at (ce->get_location (),
8589 "both complex and decimal floating-point "
8590 "arguments to type-generic function");
8591 expr.set_error ();
8592 goto out;
8594 else if (arg_binary)
8596 error_at (ce->get_location (),
8597 "both binary and decimal floating-point "
8598 "arguments to type-generic function");
8599 expr.set_error ();
8600 goto out;
8603 else if (TREE_CODE (type) == COMPLEX_TYPE)
8605 arg_complex = true;
8606 if (COMPLEX_FLOAT_TYPE_P (type))
8607 arg_binary = true;
8608 if (all_decimal)
8610 error_at (ce->get_location (),
8611 "complex argument %u to "
8612 "decimal-only type-generic function", j);
8613 expr.set_error ();
8614 goto out;
8616 else if (arg_int_decimal)
8618 error_at (ce->get_location (),
8619 "both complex and decimal floating-point "
8620 "arguments to type-generic function");
8621 expr.set_error ();
8622 goto out;
8625 else if (SCALAR_FLOAT_TYPE_P (type))
8627 arg_binary = true;
8628 if (all_decimal)
8630 error_at (ce->get_location (),
8631 "binary argument %u to "
8632 "decimal-only type-generic function", j);
8633 expr.set_error ();
8634 goto out;
8636 else if (arg_int_decimal)
8638 error_at (ce->get_location (),
8639 "both binary and decimal floating-point "
8640 "arguments to type-generic function");
8641 expr.set_error ();
8642 goto out;
8646 /* For a macro rounding its result to a narrower type, map
8647 integer types to _Float64 not double if the return type
8648 is a _FloatN or _FloatNx type. */
8649 bool arg_int_float64 = false;
8650 if (parm_kind[0] == tgmath_fixed
8651 && SCALAR_FLOAT_TYPE_P (parm_first[0])
8652 && float64_type_node != NULL_TREE)
8653 for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
8654 if (parm_first[0] == FLOATN_TYPE_NODE (j))
8656 arg_int_float64 = true;
8657 break;
8659 tree arg_real = NULL_TREE;
8660 for (unsigned int j = 1; j <= nargs; j++)
8662 if (parm_kind[j] == tgmath_fixed)
8663 continue;
8664 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8665 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8666 if (TREE_CODE (type) == COMPLEX_TYPE)
8667 type = TREE_TYPE (type);
8668 if (INTEGRAL_TYPE_P (type))
8669 type = (arg_int_decimal
8670 ? dfloat64_type_node
8671 : arg_int_float64
8672 ? float64_type_node
8673 : double_type_node);
8674 if (arg_real == NULL_TREE)
8675 arg_real = type;
8676 else
8677 arg_real = common_type (arg_real, type);
8678 if (arg_real == error_mark_node)
8680 expr.set_error ();
8681 goto out;
8684 tree arg_type = (arg_complex
8685 ? build_complex_type (arg_real)
8686 : arg_real);
8688 /* Look for a function to call with type-generic parameter
8689 type ARG_TYPE. */
8690 c_expr_t *fn = NULL;
8691 for (unsigned int j = 0; j < num_functions; j++)
8693 if (tg_type[j] == arg_type)
8695 fn = &(*cexpr_list)[j];
8696 break;
8699 if (fn == NULL
8700 && parm_kind[0] == tgmath_fixed
8701 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8703 /* Presume this is a macro that rounds its result to a
8704 narrower type, and look for the first function with
8705 at least the range and precision of the argument
8706 type. */
8707 for (unsigned int j = 0; j < num_functions; j++)
8709 if (arg_complex
8710 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8711 continue;
8712 tree real_tg_type = (arg_complex
8713 ? TREE_TYPE (tg_type[j])
8714 : tg_type[j]);
8715 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8716 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8717 continue;
8718 scalar_float_mode arg_mode
8719 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8720 scalar_float_mode tg_mode
8721 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8722 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8723 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8724 if (arg_fmt->b == tg_fmt->b
8725 && arg_fmt->p <= tg_fmt->p
8726 && arg_fmt->emax <= tg_fmt->emax
8727 && (arg_fmt->emin - arg_fmt->p
8728 >= tg_fmt->emin - tg_fmt->p))
8730 fn = &(*cexpr_list)[j];
8731 break;
8735 if (fn == NULL)
8737 error_at (loc, "no matching function for type-generic call");
8738 expr.set_error ();
8739 break;
8742 /* Construct a call to FN. */
8743 vec<tree, va_gc> *args;
8744 vec_alloc (args, nargs);
8745 vec<tree, va_gc> *origtypes;
8746 vec_alloc (origtypes, nargs);
8747 auto_vec<location_t> arg_loc (nargs);
8748 for (unsigned int j = 0; j < nargs; j++)
8750 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8751 args->quick_push (ce->value);
8752 arg_loc.quick_push (ce->get_location ());
8753 origtypes->quick_push (ce->original_type);
8755 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8756 args, origtypes);
8757 set_c_expr_source_range (&expr, loc, close_paren_loc);
8758 break;
8760 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8762 vec<c_expr_t, va_gc> *cexpr_list;
8763 c_expr_t *e2_p;
8764 tree chain_value;
8765 location_t close_paren_loc;
8767 c_parser_consume_token (parser);
8768 if (!c_parser_get_builtin_args (parser,
8769 "__builtin_call_with_static_chain",
8770 &cexpr_list, false,
8771 &close_paren_loc))
8773 expr.set_error ();
8774 break;
8776 if (vec_safe_length (cexpr_list) != 2)
8778 error_at (loc, "wrong number of arguments to "
8779 "%<__builtin_call_with_static_chain%>");
8780 expr.set_error ();
8781 break;
8784 expr = (*cexpr_list)[0];
8785 e2_p = &(*cexpr_list)[1];
8786 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8787 chain_value = e2_p->value;
8788 mark_exp_read (chain_value);
8790 if (TREE_CODE (expr.value) != CALL_EXPR)
8791 error_at (loc, "first argument to "
8792 "%<__builtin_call_with_static_chain%> "
8793 "must be a call expression");
8794 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8795 error_at (loc, "second argument to "
8796 "%<__builtin_call_with_static_chain%> "
8797 "must be a pointer type");
8798 else
8799 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8800 set_c_expr_source_range (&expr, loc, close_paren_loc);
8801 break;
8803 case RID_BUILTIN_COMPLEX:
8805 vec<c_expr_t, va_gc> *cexpr_list;
8806 c_expr_t *e1_p, *e2_p;
8807 location_t close_paren_loc;
8809 c_parser_consume_token (parser);
8810 if (!c_parser_get_builtin_args (parser,
8811 "__builtin_complex",
8812 &cexpr_list, false,
8813 &close_paren_loc))
8815 expr.set_error ();
8816 break;
8819 if (vec_safe_length (cexpr_list) != 2)
8821 error_at (loc, "wrong number of arguments to "
8822 "%<__builtin_complex%>");
8823 expr.set_error ();
8824 break;
8827 e1_p = &(*cexpr_list)[0];
8828 e2_p = &(*cexpr_list)[1];
8830 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8831 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8832 e1_p->value = convert (TREE_TYPE (e1_p->value),
8833 TREE_OPERAND (e1_p->value, 0));
8834 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8835 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8836 e2_p->value = convert (TREE_TYPE (e2_p->value),
8837 TREE_OPERAND (e2_p->value, 0));
8838 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8839 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8840 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8841 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8843 error_at (loc, "%<__builtin_complex%> operand "
8844 "not of real binary floating-point type");
8845 expr.set_error ();
8846 break;
8848 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8849 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8851 error_at (loc,
8852 "%<__builtin_complex%> operands of different types");
8853 expr.set_error ();
8854 break;
8856 pedwarn_c90 (loc, OPT_Wpedantic,
8857 "ISO C90 does not support complex types");
8858 expr.value = build2_loc (loc, COMPLEX_EXPR,
8859 build_complex_type
8860 (TYPE_MAIN_VARIANT
8861 (TREE_TYPE (e1_p->value))),
8862 e1_p->value, e2_p->value);
8863 set_c_expr_source_range (&expr, loc, close_paren_loc);
8864 break;
8866 case RID_BUILTIN_SHUFFLE:
8868 vec<c_expr_t, va_gc> *cexpr_list;
8869 unsigned int i;
8870 c_expr_t *p;
8871 location_t close_paren_loc;
8873 c_parser_consume_token (parser);
8874 if (!c_parser_get_builtin_args (parser,
8875 "__builtin_shuffle",
8876 &cexpr_list, false,
8877 &close_paren_loc))
8879 expr.set_error ();
8880 break;
8883 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8884 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8886 if (vec_safe_length (cexpr_list) == 2)
8887 expr.value =
8888 c_build_vec_perm_expr
8889 (loc, (*cexpr_list)[0].value,
8890 NULL_TREE, (*cexpr_list)[1].value);
8892 else if (vec_safe_length (cexpr_list) == 3)
8893 expr.value =
8894 c_build_vec_perm_expr
8895 (loc, (*cexpr_list)[0].value,
8896 (*cexpr_list)[1].value,
8897 (*cexpr_list)[2].value);
8898 else
8900 error_at (loc, "wrong number of arguments to "
8901 "%<__builtin_shuffle%>");
8902 expr.set_error ();
8904 set_c_expr_source_range (&expr, loc, close_paren_loc);
8905 break;
8907 case RID_AT_SELECTOR:
8909 gcc_assert (c_dialect_objc ());
8910 c_parser_consume_token (parser);
8911 matching_parens parens;
8912 if (!parens.require_open (parser))
8914 expr.set_error ();
8915 break;
8917 tree sel = c_parser_objc_selector_arg (parser);
8918 location_t close_loc = c_parser_peek_token (parser)->location;
8919 parens.skip_until_found_close (parser);
8920 expr.value = objc_build_selector_expr (loc, sel);
8921 set_c_expr_source_range (&expr, loc, close_loc);
8923 break;
8924 case RID_AT_PROTOCOL:
8926 gcc_assert (c_dialect_objc ());
8927 c_parser_consume_token (parser);
8928 matching_parens parens;
8929 if (!parens.require_open (parser))
8931 expr.set_error ();
8932 break;
8934 if (c_parser_next_token_is_not (parser, CPP_NAME))
8936 c_parser_error (parser, "expected identifier");
8937 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8938 expr.set_error ();
8939 break;
8941 tree id = c_parser_peek_token (parser)->value;
8942 c_parser_consume_token (parser);
8943 location_t close_loc = c_parser_peek_token (parser)->location;
8944 parens.skip_until_found_close (parser);
8945 expr.value = objc_build_protocol_expr (id);
8946 set_c_expr_source_range (&expr, loc, close_loc);
8948 break;
8949 case RID_AT_ENCODE:
8951 /* Extension to support C-structures in the archiver. */
8952 gcc_assert (c_dialect_objc ());
8953 c_parser_consume_token (parser);
8954 matching_parens parens;
8955 if (!parens.require_open (parser))
8957 expr.set_error ();
8958 break;
8960 t1 = c_parser_type_name (parser);
8961 if (t1 == NULL)
8963 expr.set_error ();
8964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8965 break;
8967 location_t close_loc = c_parser_peek_token (parser)->location;
8968 parens.skip_until_found_close (parser);
8969 tree type = groktypename (t1, NULL, NULL);
8970 expr.value = objc_build_encode_expr (type);
8971 set_c_expr_source_range (&expr, loc, close_loc);
8973 break;
8974 case RID_GENERIC:
8975 expr = c_parser_generic_selection (parser);
8976 break;
8977 default:
8978 c_parser_error (parser, "expected expression");
8979 expr.set_error ();
8980 break;
8982 break;
8983 case CPP_OPEN_SQUARE:
8984 if (c_dialect_objc ())
8986 tree receiver, args;
8987 c_parser_consume_token (parser);
8988 receiver = c_parser_objc_receiver (parser);
8989 args = c_parser_objc_message_args (parser);
8990 location_t close_loc = c_parser_peek_token (parser)->location;
8991 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8992 "expected %<]%>");
8993 expr.value = objc_build_message_expr (receiver, args);
8994 set_c_expr_source_range (&expr, loc, close_loc);
8995 break;
8997 /* Else fall through to report error. */
8998 /* FALLTHRU */
8999 default:
9000 c_parser_error (parser, "expected expression");
9001 expr.set_error ();
9002 break;
9004 out:
9005 return c_parser_postfix_expression_after_primary
9006 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9009 /* Parse a postfix expression after a parenthesized type name: the
9010 brace-enclosed initializer of a compound literal, possibly followed
9011 by some postfix operators. This is separate because it is not
9012 possible to tell until after the type name whether a cast
9013 expression has a cast or a compound literal, or whether the operand
9014 of sizeof is a parenthesized type name or starts with a compound
9015 literal. TYPE_LOC is the location where TYPE_NAME starts--the
9016 location of the first token after the parentheses around the type
9017 name. */
9019 static struct c_expr
9020 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9021 struct c_type_name *type_name,
9022 location_t type_loc)
9024 tree type;
9025 struct c_expr init;
9026 bool non_const;
9027 struct c_expr expr;
9028 location_t start_loc;
9029 tree type_expr = NULL_TREE;
9030 bool type_expr_const = true;
9031 check_compound_literal_type (type_loc, type_name);
9032 rich_location richloc (line_table, type_loc);
9033 start_init (NULL_TREE, NULL, 0, &richloc);
9034 type = groktypename (type_name, &type_expr, &type_expr_const);
9035 start_loc = c_parser_peek_token (parser)->location;
9036 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9038 error_at (type_loc, "compound literal has variable size");
9039 type = error_mark_node;
9041 init = c_parser_braced_init (parser, type, false, NULL);
9042 finish_init ();
9043 maybe_warn_string_init (type_loc, type, init);
9045 if (type != error_mark_node
9046 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9047 && current_function_decl)
9049 error ("compound literal qualified by address-space qualifier");
9050 type = error_mark_node;
9053 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9054 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9055 ? CONSTRUCTOR_NON_CONST (init.value)
9056 : init.original_code == C_MAYBE_CONST_EXPR);
9057 non_const |= !type_expr_const;
9058 unsigned int alignas_align = 0;
9059 if (type != error_mark_node
9060 && type_name->specs->align_log != -1)
9062 alignas_align = 1U << type_name->specs->align_log;
9063 if (alignas_align < min_align_of_type (type))
9065 error_at (type_name->specs->locations[cdw_alignas],
9066 "%<_Alignas%> specifiers cannot reduce "
9067 "alignment of compound literal");
9068 alignas_align = 0;
9071 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9072 alignas_align);
9073 set_c_expr_source_range (&expr, init.src_range);
9074 expr.original_code = ERROR_MARK;
9075 expr.original_type = NULL;
9076 if (type != error_mark_node
9077 && expr.value != error_mark_node
9078 && type_expr)
9080 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9082 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9083 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9085 else
9087 gcc_assert (!non_const);
9088 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9089 type_expr, expr.value);
9092 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9095 /* Callback function for sizeof_pointer_memaccess_warning to compare
9096 types. */
9098 static bool
9099 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9101 return comptypes (type1, type2) == 1;
9104 /* Warn for patterns where abs-like function appears to be used incorrectly,
9105 gracely ignore any non-abs-like function. The warning location should be
9106 LOC. FNDECL is the declaration of called function, it must be a
9107 BUILT_IN_NORMAL function. ARG is the first and only argument of the
9108 call. */
9110 static void
9111 warn_for_abs (location_t loc, tree fndecl, tree arg)
9113 tree atype = TREE_TYPE (arg);
9115 /* Casts from pointers (and thus arrays and fndecls) will generate
9116 -Wint-conversion warnings. Most other wrong types hopefully lead to type
9117 mismatch errors. TODO: Think about what to do with FIXED_POINT_TYPE_P
9118 types and possibly other exotic types. */
9119 if (!INTEGRAL_TYPE_P (atype)
9120 && !SCALAR_FLOAT_TYPE_P (atype)
9121 && TREE_CODE (atype) != COMPLEX_TYPE)
9122 return;
9124 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9126 switch (fcode)
9128 case BUILT_IN_ABS:
9129 case BUILT_IN_LABS:
9130 case BUILT_IN_LLABS:
9131 case BUILT_IN_IMAXABS:
9132 if (!INTEGRAL_TYPE_P (atype))
9134 if (SCALAR_FLOAT_TYPE_P (atype))
9135 warning_at (loc, OPT_Wabsolute_value,
9136 "using integer absolute value function %qD when "
9137 "argument is of floating point type %qT",
9138 fndecl, atype);
9139 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9140 warning_at (loc, OPT_Wabsolute_value,
9141 "using integer absolute value function %qD when "
9142 "argument is of complex type %qT", fndecl, atype);
9143 else
9144 gcc_unreachable ();
9145 return;
9147 if (TYPE_UNSIGNED (atype))
9148 warning_at (loc, OPT_Wabsolute_value,
9149 "taking the absolute value of unsigned type %qT "
9150 "has no effect", atype);
9151 break;
9153 CASE_FLT_FN (BUILT_IN_FABS):
9154 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9155 if (!SCALAR_FLOAT_TYPE_P (atype)
9156 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (atype)))
9158 if (INTEGRAL_TYPE_P (atype))
9159 warning_at (loc, OPT_Wabsolute_value,
9160 "using floating point absolute value function %qD "
9161 "when argument is of integer type %qT", fndecl, atype);
9162 else if (DECIMAL_FLOAT_TYPE_P (atype))
9163 warning_at (loc, OPT_Wabsolute_value,
9164 "using floating point absolute value function %qD "
9165 "when argument is of decimal floating point type %qT",
9166 fndecl, atype);
9167 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9168 warning_at (loc, OPT_Wabsolute_value,
9169 "using floating point absolute value function %qD when "
9170 "argument is of complex type %qT", fndecl, atype);
9171 else
9172 gcc_unreachable ();
9173 return;
9175 break;
9177 CASE_FLT_FN (BUILT_IN_CABS):
9178 if (TREE_CODE (atype) != COMPLEX_TYPE)
9180 if (INTEGRAL_TYPE_P (atype))
9181 warning_at (loc, OPT_Wabsolute_value,
9182 "using complex absolute value function %qD when "
9183 "argument is of integer type %qT", fndecl, atype);
9184 else if (SCALAR_FLOAT_TYPE_P (atype))
9185 warning_at (loc, OPT_Wabsolute_value,
9186 "using complex absolute value function %qD when "
9187 "argument is of floating point type %qT",
9188 fndecl, atype);
9189 else
9190 gcc_unreachable ();
9192 return;
9194 break;
9196 case BUILT_IN_FABSD32:
9197 case BUILT_IN_FABSD64:
9198 case BUILT_IN_FABSD128:
9199 if (!DECIMAL_FLOAT_TYPE_P (atype))
9201 if (INTEGRAL_TYPE_P (atype))
9202 warning_at (loc, OPT_Wabsolute_value,
9203 "using decimal floating point absolute value "
9204 "function %qD when argument is of integer type %qT",
9205 fndecl, atype);
9206 else if (SCALAR_FLOAT_TYPE_P (atype))
9207 warning_at (loc, OPT_Wabsolute_value,
9208 "using decimal floating point absolute value "
9209 "function %qD when argument is of floating point "
9210 "type %qT", fndecl, atype);
9211 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9212 warning_at (loc, OPT_Wabsolute_value,
9213 "using decimal floating point absolute value "
9214 "function %qD when argument is of complex type %qT",
9215 fndecl, atype);
9216 else
9217 gcc_unreachable ();
9218 return;
9220 break;
9222 default:
9223 return;
9226 tree ftype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
9227 if (TREE_CODE (atype) == COMPLEX_TYPE)
9229 gcc_assert (TREE_CODE (ftype) == COMPLEX_TYPE);
9230 atype = TREE_TYPE (atype);
9231 ftype = TREE_TYPE (ftype);
9234 if (TYPE_PRECISION (ftype) < TYPE_PRECISION (atype))
9235 warning_at (loc, OPT_Wabsolute_value,
9236 "absolute value function %qD given an argument of type %qT "
9237 "but has parameter of type %qT which may cause truncation "
9238 "of value", fndecl, atype, ftype);
9242 /* Parse a postfix expression after the initial primary or compound
9243 literal; that is, parse a series of postfix operators.
9245 EXPR_LOC is the location of the primary expression. */
9247 static struct c_expr
9248 c_parser_postfix_expression_after_primary (c_parser *parser,
9249 location_t expr_loc,
9250 struct c_expr expr)
9252 struct c_expr orig_expr;
9253 tree ident, idx;
9254 location_t sizeof_arg_loc[3], comp_loc;
9255 tree sizeof_arg[3];
9256 unsigned int literal_zero_mask;
9257 unsigned int i;
9258 vec<tree, va_gc> *exprlist;
9259 vec<tree, va_gc> *origtypes = NULL;
9260 vec<location_t> arg_loc = vNULL;
9261 location_t start;
9262 location_t finish;
9264 while (true)
9266 location_t op_loc = c_parser_peek_token (parser)->location;
9267 switch (c_parser_peek_token (parser)->type)
9269 case CPP_OPEN_SQUARE:
9270 /* Array reference. */
9271 c_parser_consume_token (parser);
9272 idx = c_parser_expression (parser).value;
9273 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9274 "expected %<]%>");
9275 start = expr.get_start ();
9276 finish = parser->tokens_buf[0].location;
9277 expr.value = build_array_ref (op_loc, expr.value, idx);
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_OPEN_PAREN:
9283 /* Function call. */
9284 c_parser_consume_token (parser);
9285 for (i = 0; i < 3; i++)
9287 sizeof_arg[i] = NULL_TREE;
9288 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9290 literal_zero_mask = 0;
9291 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9292 exprlist = NULL;
9293 else
9294 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9295 sizeof_arg_loc, sizeof_arg,
9296 &arg_loc, &literal_zero_mask);
9297 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9298 "expected %<)%>");
9299 orig_expr = expr;
9300 mark_exp_read (expr.value);
9301 if (warn_sizeof_pointer_memaccess)
9302 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9303 expr.value, exprlist,
9304 sizeof_arg,
9305 sizeof_ptr_memacc_comptypes);
9306 if (TREE_CODE (expr.value) == FUNCTION_DECL)
9308 if (fndecl_built_in_p (expr.value, BUILT_IN_MEMSET)
9309 && vec_safe_length (exprlist) == 3)
9311 tree arg0 = (*exprlist)[0];
9312 tree arg2 = (*exprlist)[2];
9313 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9315 if (warn_absolute_value
9316 && fndecl_built_in_p (expr.value, BUILT_IN_NORMAL)
9317 && vec_safe_length (exprlist) == 1)
9318 warn_for_abs (expr_loc, expr.value, (*exprlist)[0]);
9321 start = expr.get_start ();
9322 finish = parser->tokens_buf[0].get_finish ();
9323 expr.value
9324 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9325 exprlist, origtypes);
9326 set_c_expr_source_range (&expr, start, finish);
9328 expr.original_code = ERROR_MARK;
9329 if (TREE_CODE (expr.value) == INTEGER_CST
9330 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9331 && fndecl_built_in_p (orig_expr.value, BUILT_IN_CONSTANT_P))
9332 expr.original_code = C_MAYBE_CONST_EXPR;
9333 expr.original_type = NULL;
9334 if (exprlist)
9336 release_tree_vector (exprlist);
9337 release_tree_vector (origtypes);
9339 arg_loc.release ();
9340 break;
9341 case CPP_DOT:
9342 /* Structure element reference. */
9343 c_parser_consume_token (parser);
9344 expr = default_function_array_conversion (expr_loc, expr);
9345 if (c_parser_next_token_is (parser, CPP_NAME))
9347 c_token *comp_tok = c_parser_peek_token (parser);
9348 ident = comp_tok->value;
9349 comp_loc = comp_tok->location;
9351 else
9353 c_parser_error (parser, "expected identifier");
9354 expr.set_error ();
9355 expr.original_code = ERROR_MARK;
9356 expr.original_type = NULL;
9357 return expr;
9359 start = expr.get_start ();
9360 finish = c_parser_peek_token (parser)->get_finish ();
9361 c_parser_consume_token (parser);
9362 expr.value = build_component_ref (op_loc, expr.value, ident,
9363 comp_loc);
9364 set_c_expr_source_range (&expr, start, finish);
9365 expr.original_code = ERROR_MARK;
9366 if (TREE_CODE (expr.value) != COMPONENT_REF)
9367 expr.original_type = NULL;
9368 else
9370 /* Remember the original type of a bitfield. */
9371 tree field = TREE_OPERAND (expr.value, 1);
9372 if (TREE_CODE (field) != FIELD_DECL)
9373 expr.original_type = NULL;
9374 else
9375 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9377 break;
9378 case CPP_DEREF:
9379 /* Structure element reference. */
9380 c_parser_consume_token (parser);
9381 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9382 if (c_parser_next_token_is (parser, CPP_NAME))
9384 c_token *comp_tok = c_parser_peek_token (parser);
9385 ident = comp_tok->value;
9386 comp_loc = comp_tok->location;
9388 else
9390 c_parser_error (parser, "expected identifier");
9391 expr.set_error ();
9392 expr.original_code = ERROR_MARK;
9393 expr.original_type = NULL;
9394 return expr;
9396 start = expr.get_start ();
9397 finish = c_parser_peek_token (parser)->get_finish ();
9398 c_parser_consume_token (parser);
9399 expr.value = build_component_ref (op_loc,
9400 build_indirect_ref (op_loc,
9401 expr.value,
9402 RO_ARROW),
9403 ident, comp_loc);
9404 set_c_expr_source_range (&expr, start, finish);
9405 expr.original_code = ERROR_MARK;
9406 if (TREE_CODE (expr.value) != COMPONENT_REF)
9407 expr.original_type = NULL;
9408 else
9410 /* Remember the original type of a bitfield. */
9411 tree field = TREE_OPERAND (expr.value, 1);
9412 if (TREE_CODE (field) != FIELD_DECL)
9413 expr.original_type = NULL;
9414 else
9415 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9417 break;
9418 case CPP_PLUS_PLUS:
9419 /* Postincrement. */
9420 start = expr.get_start ();
9421 finish = c_parser_peek_token (parser)->get_finish ();
9422 c_parser_consume_token (parser);
9423 expr = default_function_array_read_conversion (expr_loc, expr);
9424 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9425 expr.value, false);
9426 set_c_expr_source_range (&expr, start, finish);
9427 expr.original_code = ERROR_MARK;
9428 expr.original_type = NULL;
9429 break;
9430 case CPP_MINUS_MINUS:
9431 /* Postdecrement. */
9432 start = expr.get_start ();
9433 finish = c_parser_peek_token (parser)->get_finish ();
9434 c_parser_consume_token (parser);
9435 expr = default_function_array_read_conversion (expr_loc, expr);
9436 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9437 expr.value, false);
9438 set_c_expr_source_range (&expr, start, finish);
9439 expr.original_code = ERROR_MARK;
9440 expr.original_type = NULL;
9441 break;
9442 default:
9443 return expr;
9448 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9450 expression:
9451 assignment-expression
9452 expression , assignment-expression
9455 static struct c_expr
9456 c_parser_expression (c_parser *parser)
9458 location_t tloc = c_parser_peek_token (parser)->location;
9459 struct c_expr expr;
9460 expr = c_parser_expr_no_commas (parser, NULL);
9461 if (c_parser_next_token_is (parser, CPP_COMMA))
9462 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9463 while (c_parser_next_token_is (parser, CPP_COMMA))
9465 struct c_expr next;
9466 tree lhsval;
9467 location_t loc = c_parser_peek_token (parser)->location;
9468 location_t expr_loc;
9469 c_parser_consume_token (parser);
9470 expr_loc = c_parser_peek_token (parser)->location;
9471 lhsval = expr.value;
9472 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9473 lhsval = TREE_OPERAND (lhsval, 1);
9474 if (DECL_P (lhsval) || handled_component_p (lhsval))
9475 mark_exp_read (lhsval);
9476 next = c_parser_expr_no_commas (parser, NULL);
9477 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9478 expr.value = build_compound_expr (loc, expr.value, next.value);
9479 expr.original_code = COMPOUND_EXPR;
9480 expr.original_type = next.original_type;
9482 return expr;
9485 /* Parse an expression and convert functions or arrays to pointers and
9486 lvalues to rvalues. */
9488 static struct c_expr
9489 c_parser_expression_conv (c_parser *parser)
9491 struct c_expr expr;
9492 location_t loc = c_parser_peek_token (parser)->location;
9493 expr = c_parser_expression (parser);
9494 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9495 return expr;
9498 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9499 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9501 static inline void
9502 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9503 unsigned int idx)
9505 if (idx >= HOST_BITS_PER_INT)
9506 return;
9508 c_token *tok = c_parser_peek_token (parser);
9509 switch (tok->type)
9511 case CPP_NUMBER:
9512 case CPP_CHAR:
9513 case CPP_WCHAR:
9514 case CPP_CHAR16:
9515 case CPP_CHAR32:
9516 /* If a parameter is literal zero alone, remember it
9517 for -Wmemset-transposed-args warning. */
9518 if (integer_zerop (tok->value)
9519 && !TREE_OVERFLOW (tok->value)
9520 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9521 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9522 *literal_zero_mask |= 1U << idx;
9523 default:
9524 break;
9528 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9529 functions and arrays to pointers and lvalues to rvalues. If
9530 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9531 locations of function arguments into this vector.
9533 nonempty-expr-list:
9534 assignment-expression
9535 nonempty-expr-list , assignment-expression
9538 static vec<tree, va_gc> *
9539 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9540 vec<tree, va_gc> **p_orig_types,
9541 location_t *sizeof_arg_loc, tree *sizeof_arg,
9542 vec<location_t> *locations,
9543 unsigned int *literal_zero_mask)
9545 vec<tree, va_gc> *ret;
9546 vec<tree, va_gc> *orig_types;
9547 struct c_expr expr;
9548 unsigned int idx = 0;
9550 ret = make_tree_vector ();
9551 if (p_orig_types == NULL)
9552 orig_types = NULL;
9553 else
9554 orig_types = make_tree_vector ();
9556 if (literal_zero_mask)
9557 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9558 expr = c_parser_expr_no_commas (parser, NULL);
9559 if (convert_p)
9560 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9561 if (fold_p)
9562 expr.value = c_fully_fold (expr.value, false, NULL);
9563 ret->quick_push (expr.value);
9564 if (orig_types)
9565 orig_types->quick_push (expr.original_type);
9566 if (locations)
9567 locations->safe_push (expr.get_location ());
9568 if (sizeof_arg != NULL
9569 && expr.original_code == SIZEOF_EXPR)
9571 sizeof_arg[0] = c_last_sizeof_arg;
9572 sizeof_arg_loc[0] = c_last_sizeof_loc;
9574 while (c_parser_next_token_is (parser, CPP_COMMA))
9576 c_parser_consume_token (parser);
9577 if (literal_zero_mask)
9578 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9579 expr = c_parser_expr_no_commas (parser, NULL);
9580 if (convert_p)
9581 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9582 true);
9583 if (fold_p)
9584 expr.value = c_fully_fold (expr.value, false, NULL);
9585 vec_safe_push (ret, expr.value);
9586 if (orig_types)
9587 vec_safe_push (orig_types, expr.original_type);
9588 if (locations)
9589 locations->safe_push (expr.get_location ());
9590 if (++idx < 3
9591 && sizeof_arg != NULL
9592 && expr.original_code == SIZEOF_EXPR)
9594 sizeof_arg[idx] = c_last_sizeof_arg;
9595 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9598 if (orig_types)
9599 *p_orig_types = orig_types;
9600 return ret;
9603 /* Parse Objective-C-specific constructs. */
9605 /* Parse an objc-class-definition.
9607 objc-class-definition:
9608 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9609 objc-class-instance-variables[opt] objc-methodprotolist @end
9610 @implementation identifier objc-superclass[opt]
9611 objc-class-instance-variables[opt]
9612 @interface identifier ( identifier ) objc-protocol-refs[opt]
9613 objc-methodprotolist @end
9614 @interface identifier ( ) objc-protocol-refs[opt]
9615 objc-methodprotolist @end
9616 @implementation identifier ( identifier )
9618 objc-superclass:
9619 : identifier
9621 "@interface identifier (" must start "@interface identifier (
9622 identifier ) ...": objc-methodprotolist in the first production may
9623 not start with a parenthesized identifier as a declarator of a data
9624 definition with no declaration specifiers if the objc-superclass,
9625 objc-protocol-refs and objc-class-instance-variables are omitted. */
9627 static void
9628 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9630 bool iface_p;
9631 tree id1;
9632 tree superclass;
9633 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9634 iface_p = true;
9635 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9636 iface_p = false;
9637 else
9638 gcc_unreachable ();
9640 c_parser_consume_token (parser);
9641 if (c_parser_next_token_is_not (parser, CPP_NAME))
9643 c_parser_error (parser, "expected identifier");
9644 return;
9646 id1 = c_parser_peek_token (parser)->value;
9647 c_parser_consume_token (parser);
9648 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9650 /* We have a category or class extension. */
9651 tree id2;
9652 tree proto = NULL_TREE;
9653 matching_parens parens;
9654 parens.consume_open (parser);
9655 if (c_parser_next_token_is_not (parser, CPP_NAME))
9657 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9659 /* We have a class extension. */
9660 id2 = NULL_TREE;
9662 else
9664 c_parser_error (parser, "expected identifier or %<)%>");
9665 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9666 return;
9669 else
9671 id2 = c_parser_peek_token (parser)->value;
9672 c_parser_consume_token (parser);
9674 parens.skip_until_found_close (parser);
9675 if (!iface_p)
9677 objc_start_category_implementation (id1, id2);
9678 return;
9680 if (c_parser_next_token_is (parser, CPP_LESS))
9681 proto = c_parser_objc_protocol_refs (parser);
9682 objc_start_category_interface (id1, id2, proto, attributes);
9683 c_parser_objc_methodprotolist (parser);
9684 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9685 objc_finish_interface ();
9686 return;
9688 if (c_parser_next_token_is (parser, CPP_COLON))
9690 c_parser_consume_token (parser);
9691 if (c_parser_next_token_is_not (parser, CPP_NAME))
9693 c_parser_error (parser, "expected identifier");
9694 return;
9696 superclass = c_parser_peek_token (parser)->value;
9697 c_parser_consume_token (parser);
9699 else
9700 superclass = NULL_TREE;
9701 if (iface_p)
9703 tree proto = NULL_TREE;
9704 if (c_parser_next_token_is (parser, CPP_LESS))
9705 proto = c_parser_objc_protocol_refs (parser);
9706 objc_start_class_interface (id1, superclass, proto, attributes);
9708 else
9709 objc_start_class_implementation (id1, superclass);
9710 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9711 c_parser_objc_class_instance_variables (parser);
9712 if (iface_p)
9714 objc_continue_interface ();
9715 c_parser_objc_methodprotolist (parser);
9716 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9717 objc_finish_interface ();
9719 else
9721 objc_continue_implementation ();
9722 return;
9726 /* Parse objc-class-instance-variables.
9728 objc-class-instance-variables:
9729 { objc-instance-variable-decl-list[opt] }
9731 objc-instance-variable-decl-list:
9732 objc-visibility-spec
9733 objc-instance-variable-decl ;
9735 objc-instance-variable-decl-list objc-visibility-spec
9736 objc-instance-variable-decl-list objc-instance-variable-decl ;
9737 objc-instance-variable-decl-list ;
9739 objc-visibility-spec:
9740 @private
9741 @protected
9742 @public
9744 objc-instance-variable-decl:
9745 struct-declaration
9748 static void
9749 c_parser_objc_class_instance_variables (c_parser *parser)
9751 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9752 c_parser_consume_token (parser);
9753 while (c_parser_next_token_is_not (parser, CPP_EOF))
9755 tree decls;
9756 /* Parse any stray semicolon. */
9757 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9759 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9760 "extra semicolon");
9761 c_parser_consume_token (parser);
9762 continue;
9764 /* Stop if at the end of the instance variables. */
9765 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9767 c_parser_consume_token (parser);
9768 break;
9770 /* Parse any objc-visibility-spec. */
9771 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9773 c_parser_consume_token (parser);
9774 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9775 continue;
9777 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9779 c_parser_consume_token (parser);
9780 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9781 continue;
9783 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9785 c_parser_consume_token (parser);
9786 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9787 continue;
9789 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9791 c_parser_consume_token (parser);
9792 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9793 continue;
9795 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9797 c_parser_pragma (parser, pragma_external, NULL);
9798 continue;
9801 /* Parse some comma-separated declarations. */
9802 decls = c_parser_struct_declaration (parser);
9803 if (decls == NULL)
9805 /* There is a syntax error. We want to skip the offending
9806 tokens up to the next ';' (included) or '}'
9807 (excluded). */
9809 /* First, skip manually a ')' or ']'. This is because they
9810 reduce the nesting level, so c_parser_skip_until_found()
9811 wouldn't be able to skip past them. */
9812 c_token *token = c_parser_peek_token (parser);
9813 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9814 c_parser_consume_token (parser);
9816 /* Then, do the standard skipping. */
9817 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9819 /* We hopefully recovered. Start normal parsing again. */
9820 parser->error = false;
9821 continue;
9823 else
9825 /* Comma-separated instance variables are chained together
9826 in reverse order; add them one by one. */
9827 tree ivar = nreverse (decls);
9828 for (; ivar; ivar = DECL_CHAIN (ivar))
9829 objc_add_instance_variable (copy_node (ivar));
9831 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9835 /* Parse an objc-class-declaration.
9837 objc-class-declaration:
9838 @class identifier-list ;
9841 static void
9842 c_parser_objc_class_declaration (c_parser *parser)
9844 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9845 c_parser_consume_token (parser);
9846 /* Any identifiers, including those declared as type names, are OK
9847 here. */
9848 while (true)
9850 tree id;
9851 if (c_parser_next_token_is_not (parser, CPP_NAME))
9853 c_parser_error (parser, "expected identifier");
9854 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9855 parser->error = false;
9856 return;
9858 id = c_parser_peek_token (parser)->value;
9859 objc_declare_class (id);
9860 c_parser_consume_token (parser);
9861 if (c_parser_next_token_is (parser, CPP_COMMA))
9862 c_parser_consume_token (parser);
9863 else
9864 break;
9866 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9869 /* Parse an objc-alias-declaration.
9871 objc-alias-declaration:
9872 @compatibility_alias identifier identifier ;
9875 static void
9876 c_parser_objc_alias_declaration (c_parser *parser)
9878 tree id1, id2;
9879 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9880 c_parser_consume_token (parser);
9881 if (c_parser_next_token_is_not (parser, CPP_NAME))
9883 c_parser_error (parser, "expected identifier");
9884 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9885 return;
9887 id1 = c_parser_peek_token (parser)->value;
9888 c_parser_consume_token (parser);
9889 if (c_parser_next_token_is_not (parser, CPP_NAME))
9891 c_parser_error (parser, "expected identifier");
9892 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9893 return;
9895 id2 = c_parser_peek_token (parser)->value;
9896 c_parser_consume_token (parser);
9897 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9898 objc_declare_alias (id1, id2);
9901 /* Parse an objc-protocol-definition.
9903 objc-protocol-definition:
9904 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9905 @protocol identifier-list ;
9907 "@protocol identifier ;" should be resolved as "@protocol
9908 identifier-list ;": objc-methodprotolist may not start with a
9909 semicolon in the first alternative if objc-protocol-refs are
9910 omitted. */
9912 static void
9913 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9915 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9917 c_parser_consume_token (parser);
9918 if (c_parser_next_token_is_not (parser, CPP_NAME))
9920 c_parser_error (parser, "expected identifier");
9921 return;
9923 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9924 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9926 /* Any identifiers, including those declared as type names, are
9927 OK here. */
9928 while (true)
9930 tree id;
9931 if (c_parser_next_token_is_not (parser, CPP_NAME))
9933 c_parser_error (parser, "expected identifier");
9934 break;
9936 id = c_parser_peek_token (parser)->value;
9937 objc_declare_protocol (id, attributes);
9938 c_parser_consume_token (parser);
9939 if (c_parser_next_token_is (parser, CPP_COMMA))
9940 c_parser_consume_token (parser);
9941 else
9942 break;
9944 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9946 else
9948 tree id = c_parser_peek_token (parser)->value;
9949 tree proto = NULL_TREE;
9950 c_parser_consume_token (parser);
9951 if (c_parser_next_token_is (parser, CPP_LESS))
9952 proto = c_parser_objc_protocol_refs (parser);
9953 parser->objc_pq_context = true;
9954 objc_start_protocol (id, proto, attributes);
9955 c_parser_objc_methodprotolist (parser);
9956 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9957 parser->objc_pq_context = false;
9958 objc_finish_interface ();
9962 /* Parse an objc-method-type.
9964 objc-method-type:
9968 Return true if it is a class method (+) and false if it is
9969 an instance method (-).
9971 static inline bool
9972 c_parser_objc_method_type (c_parser *parser)
9974 switch (c_parser_peek_token (parser)->type)
9976 case CPP_PLUS:
9977 c_parser_consume_token (parser);
9978 return true;
9979 case CPP_MINUS:
9980 c_parser_consume_token (parser);
9981 return false;
9982 default:
9983 gcc_unreachable ();
9987 /* Parse an objc-method-definition.
9989 objc-method-definition:
9990 objc-method-type objc-method-decl ;[opt] compound-statement
9993 static void
9994 c_parser_objc_method_definition (c_parser *parser)
9996 bool is_class_method = c_parser_objc_method_type (parser);
9997 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9998 parser->objc_pq_context = true;
9999 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10000 &expr);
10001 if (decl == error_mark_node)
10002 return; /* Bail here. */
10004 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10006 c_parser_consume_token (parser);
10007 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10008 "extra semicolon in method definition specified");
10011 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10013 c_parser_error (parser, "expected %<{%>");
10014 return;
10017 parser->objc_pq_context = false;
10018 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
10020 add_stmt (c_parser_compound_statement (parser));
10021 objc_finish_method_definition (current_function_decl);
10023 else
10025 /* This code is executed when we find a method definition
10026 outside of an @implementation context (or invalid for other
10027 reasons). Parse the method (to keep going) but do not emit
10028 any code.
10030 c_parser_compound_statement (parser);
10034 /* Parse an objc-methodprotolist.
10036 objc-methodprotolist:
10037 empty
10038 objc-methodprotolist objc-methodproto
10039 objc-methodprotolist declaration
10040 objc-methodprotolist ;
10041 @optional
10042 @required
10044 The declaration is a data definition, which may be missing
10045 declaration specifiers under the same rules and diagnostics as
10046 other data definitions outside functions, and the stray semicolon
10047 is diagnosed the same way as a stray semicolon outside a
10048 function. */
10050 static void
10051 c_parser_objc_methodprotolist (c_parser *parser)
10053 while (true)
10055 /* The list is terminated by @end. */
10056 switch (c_parser_peek_token (parser)->type)
10058 case CPP_SEMICOLON:
10059 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10060 "ISO C does not allow extra %<;%> outside of a function");
10061 c_parser_consume_token (parser);
10062 break;
10063 case CPP_PLUS:
10064 case CPP_MINUS:
10065 c_parser_objc_methodproto (parser);
10066 break;
10067 case CPP_PRAGMA:
10068 c_parser_pragma (parser, pragma_external, NULL);
10069 break;
10070 case CPP_EOF:
10071 return;
10072 default:
10073 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
10074 return;
10075 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
10076 c_parser_objc_at_property_declaration (parser);
10077 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
10079 objc_set_method_opt (true);
10080 c_parser_consume_token (parser);
10082 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
10084 objc_set_method_opt (false);
10085 c_parser_consume_token (parser);
10087 else
10088 c_parser_declaration_or_fndef (parser, false, false, true,
10089 false, true, NULL, vNULL);
10090 break;
10095 /* Parse an objc-methodproto.
10097 objc-methodproto:
10098 objc-method-type objc-method-decl ;
10101 static void
10102 c_parser_objc_methodproto (c_parser *parser)
10104 bool is_class_method = c_parser_objc_method_type (parser);
10105 tree decl, attributes = NULL_TREE;
10107 /* Remember protocol qualifiers in prototypes. */
10108 parser->objc_pq_context = true;
10109 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10110 NULL);
10111 /* Forget protocol qualifiers now. */
10112 parser->objc_pq_context = false;
10114 /* Do not allow the presence of attributes to hide an erroneous
10115 method implementation in the interface section. */
10116 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
10118 c_parser_error (parser, "expected %<;%>");
10119 return;
10122 if (decl != error_mark_node)
10123 objc_add_method_declaration (is_class_method, decl, attributes);
10125 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10128 /* If we are at a position that method attributes may be present, check that
10129 there are not any parsed already (a syntax error) and then collect any
10130 specified at the current location. Finally, if new attributes were present,
10131 check that the next token is legal ( ';' for decls and '{' for defs). */
10133 static bool
10134 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
10136 bool bad = false;
10137 if (*attributes)
10139 c_parser_error (parser,
10140 "method attributes must be specified at the end only");
10141 *attributes = NULL_TREE;
10142 bad = true;
10145 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10146 *attributes = c_parser_attributes (parser);
10148 /* If there were no attributes here, just report any earlier error. */
10149 if (*attributes == NULL_TREE || bad)
10150 return bad;
10152 /* If the attributes are followed by a ; or {, then just report any earlier
10153 error. */
10154 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10155 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10156 return bad;
10158 /* We've got attributes, but not at the end. */
10159 c_parser_error (parser,
10160 "expected %<;%> or %<{%> after method attribute definition");
10161 return true;
10164 /* Parse an objc-method-decl.
10166 objc-method-decl:
10167 ( objc-type-name ) objc-selector
10168 objc-selector
10169 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10170 objc-keyword-selector objc-optparmlist
10171 attributes
10173 objc-keyword-selector:
10174 objc-keyword-decl
10175 objc-keyword-selector objc-keyword-decl
10177 objc-keyword-decl:
10178 objc-selector : ( objc-type-name ) identifier
10179 objc-selector : identifier
10180 : ( objc-type-name ) identifier
10181 : identifier
10183 objc-optparmlist:
10184 objc-optparms objc-optellipsis
10186 objc-optparms:
10187 empty
10188 objc-opt-parms , parameter-declaration
10190 objc-optellipsis:
10191 empty
10192 , ...
10195 static tree
10196 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10197 tree *attributes, tree *expr)
10199 tree type = NULL_TREE;
10200 tree sel;
10201 tree parms = NULL_TREE;
10202 bool ellipsis = false;
10203 bool attr_err = false;
10205 *attributes = NULL_TREE;
10206 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10208 matching_parens parens;
10209 parens.consume_open (parser);
10210 type = c_parser_objc_type_name (parser);
10211 parens.skip_until_found_close (parser);
10213 sel = c_parser_objc_selector (parser);
10214 /* If there is no selector, or a colon follows, we have an
10215 objc-keyword-selector. If there is a selector, and a colon does
10216 not follow, that selector ends the objc-method-decl. */
10217 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10219 tree tsel = sel;
10220 tree list = NULL_TREE;
10221 while (true)
10223 tree atype = NULL_TREE, id, keyworddecl;
10224 tree param_attr = NULL_TREE;
10225 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10226 break;
10227 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10229 c_parser_consume_token (parser);
10230 atype = c_parser_objc_type_name (parser);
10231 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10232 "expected %<)%>");
10234 /* New ObjC allows attributes on method parameters. */
10235 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10236 param_attr = c_parser_attributes (parser);
10237 if (c_parser_next_token_is_not (parser, CPP_NAME))
10239 c_parser_error (parser, "expected identifier");
10240 return error_mark_node;
10242 id = c_parser_peek_token (parser)->value;
10243 c_parser_consume_token (parser);
10244 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10245 list = chainon (list, keyworddecl);
10246 tsel = c_parser_objc_selector (parser);
10247 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10248 break;
10251 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10253 /* Parse the optional parameter list. Optional Objective-C
10254 method parameters follow the C syntax, and may include '...'
10255 to denote a variable number of arguments. */
10256 parms = make_node (TREE_LIST);
10257 while (c_parser_next_token_is (parser, CPP_COMMA))
10259 struct c_parm *parm;
10260 c_parser_consume_token (parser);
10261 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10263 ellipsis = true;
10264 c_parser_consume_token (parser);
10265 attr_err |= c_parser_objc_maybe_method_attributes
10266 (parser, attributes) ;
10267 break;
10269 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10270 if (parm == NULL)
10271 break;
10272 parms = chainon (parms,
10273 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10275 sel = list;
10277 else
10278 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10280 if (sel == NULL)
10282 c_parser_error (parser, "objective-c method declaration is expected");
10283 return error_mark_node;
10286 if (attr_err)
10287 return error_mark_node;
10289 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10292 /* Parse an objc-type-name.
10294 objc-type-name:
10295 objc-type-qualifiers[opt] type-name
10296 objc-type-qualifiers[opt]
10298 objc-type-qualifiers:
10299 objc-type-qualifier
10300 objc-type-qualifiers objc-type-qualifier
10302 objc-type-qualifier: one of
10303 in out inout bycopy byref oneway
10306 static tree
10307 c_parser_objc_type_name (c_parser *parser)
10309 tree quals = NULL_TREE;
10310 struct c_type_name *type_name = NULL;
10311 tree type = NULL_TREE;
10312 while (true)
10314 c_token *token = c_parser_peek_token (parser);
10315 if (token->type == CPP_KEYWORD
10316 && (token->keyword == RID_IN
10317 || token->keyword == RID_OUT
10318 || token->keyword == RID_INOUT
10319 || token->keyword == RID_BYCOPY
10320 || token->keyword == RID_BYREF
10321 || token->keyword == RID_ONEWAY))
10323 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10324 c_parser_consume_token (parser);
10326 else
10327 break;
10329 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10330 type_name = c_parser_type_name (parser);
10331 if (type_name)
10332 type = groktypename (type_name, NULL, NULL);
10334 /* If the type is unknown, and error has already been produced and
10335 we need to recover from the error. In that case, use NULL_TREE
10336 for the type, as if no type had been specified; this will use the
10337 default type ('id') which is good for error recovery. */
10338 if (type == error_mark_node)
10339 type = NULL_TREE;
10341 return build_tree_list (quals, type);
10344 /* Parse objc-protocol-refs.
10346 objc-protocol-refs:
10347 < identifier-list >
10350 static tree
10351 c_parser_objc_protocol_refs (c_parser *parser)
10353 tree list = NULL_TREE;
10354 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10355 c_parser_consume_token (parser);
10356 /* Any identifiers, including those declared as type names, are OK
10357 here. */
10358 while (true)
10360 tree id;
10361 if (c_parser_next_token_is_not (parser, CPP_NAME))
10363 c_parser_error (parser, "expected identifier");
10364 break;
10366 id = c_parser_peek_token (parser)->value;
10367 list = chainon (list, build_tree_list (NULL_TREE, id));
10368 c_parser_consume_token (parser);
10369 if (c_parser_next_token_is (parser, CPP_COMMA))
10370 c_parser_consume_token (parser);
10371 else
10372 break;
10374 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10375 return list;
10378 /* Parse an objc-try-catch-finally-statement.
10380 objc-try-catch-finally-statement:
10381 @try compound-statement objc-catch-list[opt]
10382 @try compound-statement objc-catch-list[opt] @finally compound-statement
10384 objc-catch-list:
10385 @catch ( objc-catch-parameter-declaration ) compound-statement
10386 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10388 objc-catch-parameter-declaration:
10389 parameter-declaration
10390 '...'
10392 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10394 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10395 for C++. Keep them in sync. */
10397 static void
10398 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10400 location_t location;
10401 tree stmt;
10403 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10404 c_parser_consume_token (parser);
10405 location = c_parser_peek_token (parser)->location;
10406 objc_maybe_warn_exceptions (location);
10407 stmt = c_parser_compound_statement (parser);
10408 objc_begin_try_stmt (location, stmt);
10410 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10412 struct c_parm *parm;
10413 tree parameter_declaration = error_mark_node;
10414 bool seen_open_paren = false;
10416 c_parser_consume_token (parser);
10417 matching_parens parens;
10418 if (!parens.require_open (parser))
10419 seen_open_paren = true;
10420 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10422 /* We have "@catch (...)" (where the '...' are literally
10423 what is in the code). Skip the '...'.
10424 parameter_declaration is set to NULL_TREE, and
10425 objc_being_catch_clauses() knows that that means
10426 '...'. */
10427 c_parser_consume_token (parser);
10428 parameter_declaration = NULL_TREE;
10430 else
10432 /* We have "@catch (NSException *exception)" or something
10433 like that. Parse the parameter declaration. */
10434 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10435 if (parm == NULL)
10436 parameter_declaration = error_mark_node;
10437 else
10438 parameter_declaration = grokparm (parm, NULL);
10440 if (seen_open_paren)
10441 parens.require_close (parser);
10442 else
10444 /* If there was no open parenthesis, we are recovering from
10445 an error, and we are trying to figure out what mistake
10446 the user has made. */
10448 /* If there is an immediate closing parenthesis, the user
10449 probably forgot the opening one (ie, they typed "@catch
10450 NSException *e)". Parse the closing parenthesis and keep
10451 going. */
10452 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10453 c_parser_consume_token (parser);
10455 /* If these is no immediate closing parenthesis, the user
10456 probably doesn't know that parenthesis are required at
10457 all (ie, they typed "@catch NSException *e"). So, just
10458 forget about the closing parenthesis and keep going. */
10460 objc_begin_catch_clause (parameter_declaration);
10461 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10462 c_parser_compound_statement_nostart (parser);
10463 objc_finish_catch_clause ();
10465 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10467 c_parser_consume_token (parser);
10468 location = c_parser_peek_token (parser)->location;
10469 stmt = c_parser_compound_statement (parser);
10470 objc_build_finally_clause (location, stmt);
10472 objc_finish_try_stmt ();
10475 /* Parse an objc-synchronized-statement.
10477 objc-synchronized-statement:
10478 @synchronized ( expression ) compound-statement
10481 static void
10482 c_parser_objc_synchronized_statement (c_parser *parser)
10484 location_t loc;
10485 tree expr, stmt;
10486 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10487 c_parser_consume_token (parser);
10488 loc = c_parser_peek_token (parser)->location;
10489 objc_maybe_warn_exceptions (loc);
10490 matching_parens parens;
10491 if (parens.require_open (parser))
10493 struct c_expr ce = c_parser_expression (parser);
10494 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10495 expr = ce.value;
10496 expr = c_fully_fold (expr, false, NULL);
10497 parens.skip_until_found_close (parser);
10499 else
10500 expr = error_mark_node;
10501 stmt = c_parser_compound_statement (parser);
10502 objc_build_synchronized (loc, expr, stmt);
10505 /* Parse an objc-selector; return NULL_TREE without an error if the
10506 next token is not an objc-selector.
10508 objc-selector:
10509 identifier
10510 one of
10511 enum struct union if else while do for switch case default
10512 break continue return goto asm sizeof typeof __alignof
10513 unsigned long const short volatile signed restrict _Complex
10514 in out inout bycopy byref oneway int char float double void _Bool
10515 _Atomic
10517 ??? Why this selection of keywords but not, for example, storage
10518 class specifiers? */
10520 static tree
10521 c_parser_objc_selector (c_parser *parser)
10523 c_token *token = c_parser_peek_token (parser);
10524 tree value = token->value;
10525 if (token->type == CPP_NAME)
10527 c_parser_consume_token (parser);
10528 return value;
10530 if (token->type != CPP_KEYWORD)
10531 return NULL_TREE;
10532 switch (token->keyword)
10534 case RID_ENUM:
10535 case RID_STRUCT:
10536 case RID_UNION:
10537 case RID_IF:
10538 case RID_ELSE:
10539 case RID_WHILE:
10540 case RID_DO:
10541 case RID_FOR:
10542 case RID_SWITCH:
10543 case RID_CASE:
10544 case RID_DEFAULT:
10545 case RID_BREAK:
10546 case RID_CONTINUE:
10547 case RID_RETURN:
10548 case RID_GOTO:
10549 case RID_ASM:
10550 case RID_SIZEOF:
10551 case RID_TYPEOF:
10552 case RID_ALIGNOF:
10553 case RID_UNSIGNED:
10554 case RID_LONG:
10555 case RID_CONST:
10556 case RID_SHORT:
10557 case RID_VOLATILE:
10558 case RID_SIGNED:
10559 case RID_RESTRICT:
10560 case RID_COMPLEX:
10561 case RID_IN:
10562 case RID_OUT:
10563 case RID_INOUT:
10564 case RID_BYCOPY:
10565 case RID_BYREF:
10566 case RID_ONEWAY:
10567 case RID_INT:
10568 case RID_CHAR:
10569 case RID_FLOAT:
10570 case RID_DOUBLE:
10571 CASE_RID_FLOATN_NX:
10572 case RID_VOID:
10573 case RID_BOOL:
10574 case RID_ATOMIC:
10575 case RID_AUTO_TYPE:
10576 case RID_INT_N_0:
10577 case RID_INT_N_1:
10578 case RID_INT_N_2:
10579 case RID_INT_N_3:
10580 c_parser_consume_token (parser);
10581 return value;
10582 default:
10583 return NULL_TREE;
10587 /* Parse an objc-selector-arg.
10589 objc-selector-arg:
10590 objc-selector
10591 objc-keywordname-list
10593 objc-keywordname-list:
10594 objc-keywordname
10595 objc-keywordname-list objc-keywordname
10597 objc-keywordname:
10598 objc-selector :
10602 static tree
10603 c_parser_objc_selector_arg (c_parser *parser)
10605 tree sel = c_parser_objc_selector (parser);
10606 tree list = NULL_TREE;
10607 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10608 return sel;
10609 while (true)
10611 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10612 return list;
10613 list = chainon (list, build_tree_list (sel, NULL_TREE));
10614 sel = c_parser_objc_selector (parser);
10615 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10616 break;
10618 return list;
10621 /* Parse an objc-receiver.
10623 objc-receiver:
10624 expression
10625 class-name
10626 type-name
10629 static tree
10630 c_parser_objc_receiver (c_parser *parser)
10632 location_t loc = c_parser_peek_token (parser)->location;
10634 if (c_parser_peek_token (parser)->type == CPP_NAME
10635 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10636 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10638 tree id = c_parser_peek_token (parser)->value;
10639 c_parser_consume_token (parser);
10640 return objc_get_class_reference (id);
10642 struct c_expr ce = c_parser_expression (parser);
10643 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10644 return c_fully_fold (ce.value, false, NULL);
10647 /* Parse objc-message-args.
10649 objc-message-args:
10650 objc-selector
10651 objc-keywordarg-list
10653 objc-keywordarg-list:
10654 objc-keywordarg
10655 objc-keywordarg-list objc-keywordarg
10657 objc-keywordarg:
10658 objc-selector : objc-keywordexpr
10659 : objc-keywordexpr
10662 static tree
10663 c_parser_objc_message_args (c_parser *parser)
10665 tree sel = c_parser_objc_selector (parser);
10666 tree list = NULL_TREE;
10667 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10668 return sel;
10669 while (true)
10671 tree keywordexpr;
10672 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10673 return error_mark_node;
10674 keywordexpr = c_parser_objc_keywordexpr (parser);
10675 list = chainon (list, build_tree_list (sel, keywordexpr));
10676 sel = c_parser_objc_selector (parser);
10677 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10678 break;
10680 return list;
10683 /* Parse an objc-keywordexpr.
10685 objc-keywordexpr:
10686 nonempty-expr-list
10689 static tree
10690 c_parser_objc_keywordexpr (c_parser *parser)
10692 tree ret;
10693 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10694 NULL, NULL, NULL, NULL);
10695 if (vec_safe_length (expr_list) == 1)
10697 /* Just return the expression, remove a level of
10698 indirection. */
10699 ret = (*expr_list)[0];
10701 else
10703 /* We have a comma expression, we will collapse later. */
10704 ret = build_tree_list_vec (expr_list);
10706 release_tree_vector (expr_list);
10707 return ret;
10710 /* A check, needed in several places, that ObjC interface, implementation or
10711 method definitions are not prefixed by incorrect items. */
10712 static bool
10713 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10714 struct c_declspecs *specs)
10716 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10717 || specs->typespec_kind != ctsk_none)
10719 c_parser_error (parser,
10720 "no type or storage class may be specified here,");
10721 c_parser_skip_to_end_of_block_or_statement (parser);
10722 return true;
10724 return false;
10727 /* Parse an Objective-C @property declaration. The syntax is:
10729 objc-property-declaration:
10730 '@property' objc-property-attributes[opt] struct-declaration ;
10732 objc-property-attributes:
10733 '(' objc-property-attribute-list ')'
10735 objc-property-attribute-list:
10736 objc-property-attribute
10737 objc-property-attribute-list, objc-property-attribute
10739 objc-property-attribute
10740 'getter' = identifier
10741 'setter' = identifier
10742 'readonly'
10743 'readwrite'
10744 'assign'
10745 'retain'
10746 'copy'
10747 'nonatomic'
10749 For example:
10750 @property NSString *name;
10751 @property (readonly) id object;
10752 @property (retain, nonatomic, getter=getTheName) id name;
10753 @property int a, b, c;
10755 PS: This function is identical to cp_parser_objc_at_propery_declaration
10756 for C++. Keep them in sync. */
10757 static void
10758 c_parser_objc_at_property_declaration (c_parser *parser)
10760 /* The following variables hold the attributes of the properties as
10761 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10762 seen. When we see an attribute, we set them to 'true' (if they
10763 are boolean properties) or to the identifier (if they have an
10764 argument, ie, for getter and setter). Note that here we only
10765 parse the list of attributes, check the syntax and accumulate the
10766 attributes that we find. objc_add_property_declaration() will
10767 then process the information. */
10768 bool property_assign = false;
10769 bool property_copy = false;
10770 tree property_getter_ident = NULL_TREE;
10771 bool property_nonatomic = false;
10772 bool property_readonly = false;
10773 bool property_readwrite = false;
10774 bool property_retain = false;
10775 tree property_setter_ident = NULL_TREE;
10777 /* 'properties' is the list of properties that we read. Usually a
10778 single one, but maybe more (eg, in "@property int a, b, c;" there
10779 are three). */
10780 tree properties;
10781 location_t loc;
10783 loc = c_parser_peek_token (parser)->location;
10784 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10786 c_parser_consume_token (parser); /* Eat '@property'. */
10788 /* Parse the optional attribute list... */
10789 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10791 matching_parens parens;
10793 /* Eat the '(' */
10794 parens.consume_open (parser);
10796 /* Property attribute keywords are valid now. */
10797 parser->objc_property_attr_context = true;
10799 while (true)
10801 bool syntax_error = false;
10802 c_token *token = c_parser_peek_token (parser);
10803 enum rid keyword;
10805 if (token->type != CPP_KEYWORD)
10807 if (token->type == CPP_CLOSE_PAREN)
10808 c_parser_error (parser, "expected identifier");
10809 else
10811 c_parser_consume_token (parser);
10812 c_parser_error (parser, "unknown property attribute");
10814 break;
10816 keyword = token->keyword;
10817 c_parser_consume_token (parser);
10818 switch (keyword)
10820 case RID_ASSIGN: property_assign = true; break;
10821 case RID_COPY: property_copy = true; break;
10822 case RID_NONATOMIC: property_nonatomic = true; break;
10823 case RID_READONLY: property_readonly = true; break;
10824 case RID_READWRITE: property_readwrite = true; break;
10825 case RID_RETAIN: property_retain = true; break;
10827 case RID_GETTER:
10828 case RID_SETTER:
10829 if (c_parser_next_token_is_not (parser, CPP_EQ))
10831 if (keyword == RID_GETTER)
10832 c_parser_error (parser,
10833 "missing %<=%> (after %<getter%> attribute)");
10834 else
10835 c_parser_error (parser,
10836 "missing %<=%> (after %<setter%> attribute)");
10837 syntax_error = true;
10838 break;
10840 c_parser_consume_token (parser); /* eat the = */
10841 if (c_parser_next_token_is_not (parser, CPP_NAME))
10843 c_parser_error (parser, "expected identifier");
10844 syntax_error = true;
10845 break;
10847 if (keyword == RID_SETTER)
10849 if (property_setter_ident != NULL_TREE)
10850 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10851 else
10852 property_setter_ident = c_parser_peek_token (parser)->value;
10853 c_parser_consume_token (parser);
10854 if (c_parser_next_token_is_not (parser, CPP_COLON))
10855 c_parser_error (parser, "setter name must terminate with %<:%>");
10856 else
10857 c_parser_consume_token (parser);
10859 else
10861 if (property_getter_ident != NULL_TREE)
10862 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10863 else
10864 property_getter_ident = c_parser_peek_token (parser)->value;
10865 c_parser_consume_token (parser);
10867 break;
10868 default:
10869 c_parser_error (parser, "unknown property attribute");
10870 syntax_error = true;
10871 break;
10874 if (syntax_error)
10875 break;
10877 if (c_parser_next_token_is (parser, CPP_COMMA))
10878 c_parser_consume_token (parser);
10879 else
10880 break;
10882 parser->objc_property_attr_context = false;
10883 parens.skip_until_found_close (parser);
10885 /* ... and the property declaration(s). */
10886 properties = c_parser_struct_declaration (parser);
10888 if (properties == error_mark_node)
10890 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10891 parser->error = false;
10892 return;
10895 if (properties == NULL_TREE)
10896 c_parser_error (parser, "expected identifier");
10897 else
10899 /* Comma-separated properties are chained together in
10900 reverse order; add them one by one. */
10901 properties = nreverse (properties);
10903 for (; properties; properties = TREE_CHAIN (properties))
10904 objc_add_property_declaration (loc, copy_node (properties),
10905 property_readonly, property_readwrite,
10906 property_assign, property_retain,
10907 property_copy, property_nonatomic,
10908 property_getter_ident, property_setter_ident);
10911 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10912 parser->error = false;
10915 /* Parse an Objective-C @synthesize declaration. The syntax is:
10917 objc-synthesize-declaration:
10918 @synthesize objc-synthesize-identifier-list ;
10920 objc-synthesize-identifier-list:
10921 objc-synthesize-identifier
10922 objc-synthesize-identifier-list, objc-synthesize-identifier
10924 objc-synthesize-identifier
10925 identifier
10926 identifier = identifier
10928 For example:
10929 @synthesize MyProperty;
10930 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10932 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10933 for C++. Keep them in sync.
10935 static void
10936 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10938 tree list = NULL_TREE;
10939 location_t loc;
10940 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10941 loc = c_parser_peek_token (parser)->location;
10943 c_parser_consume_token (parser);
10944 while (true)
10946 tree property, ivar;
10947 if (c_parser_next_token_is_not (parser, CPP_NAME))
10949 c_parser_error (parser, "expected identifier");
10950 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10951 /* Once we find the semicolon, we can resume normal parsing.
10952 We have to reset parser->error manually because
10953 c_parser_skip_until_found() won't reset it for us if the
10954 next token is precisely a semicolon. */
10955 parser->error = false;
10956 return;
10958 property = c_parser_peek_token (parser)->value;
10959 c_parser_consume_token (parser);
10960 if (c_parser_next_token_is (parser, CPP_EQ))
10962 c_parser_consume_token (parser);
10963 if (c_parser_next_token_is_not (parser, CPP_NAME))
10965 c_parser_error (parser, "expected identifier");
10966 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10967 parser->error = false;
10968 return;
10970 ivar = c_parser_peek_token (parser)->value;
10971 c_parser_consume_token (parser);
10973 else
10974 ivar = NULL_TREE;
10975 list = chainon (list, build_tree_list (ivar, property));
10976 if (c_parser_next_token_is (parser, CPP_COMMA))
10977 c_parser_consume_token (parser);
10978 else
10979 break;
10981 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10982 objc_add_synthesize_declaration (loc, list);
10985 /* Parse an Objective-C @dynamic declaration. The syntax is:
10987 objc-dynamic-declaration:
10988 @dynamic identifier-list ;
10990 For example:
10991 @dynamic MyProperty;
10992 @dynamic MyProperty, AnotherProperty;
10994 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10995 for C++. Keep them in sync.
10997 static void
10998 c_parser_objc_at_dynamic_declaration (c_parser *parser)
11000 tree list = NULL_TREE;
11001 location_t loc;
11002 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
11003 loc = c_parser_peek_token (parser)->location;
11005 c_parser_consume_token (parser);
11006 while (true)
11008 tree property;
11009 if (c_parser_next_token_is_not (parser, CPP_NAME))
11011 c_parser_error (parser, "expected identifier");
11012 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11013 parser->error = false;
11014 return;
11016 property = c_parser_peek_token (parser)->value;
11017 list = chainon (list, build_tree_list (NULL_TREE, property));
11018 c_parser_consume_token (parser);
11019 if (c_parser_next_token_is (parser, CPP_COMMA))
11020 c_parser_consume_token (parser);
11021 else
11022 break;
11024 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11025 objc_add_dynamic_declaration (loc, list);
11029 /* Parse a pragma GCC ivdep. */
11031 static bool
11032 c_parse_pragma_ivdep (c_parser *parser)
11034 c_parser_consume_pragma (parser);
11035 c_parser_skip_to_pragma_eol (parser);
11036 return true;
11039 /* Parse a pragma GCC unroll. */
11041 static unsigned short
11042 c_parser_pragma_unroll (c_parser *parser)
11044 unsigned short unroll;
11045 c_parser_consume_pragma (parser);
11046 location_t location = c_parser_peek_token (parser)->location;
11047 tree expr = c_parser_expr_no_commas (parser, NULL).value;
11048 mark_exp_read (expr);
11049 expr = c_fully_fold (expr, false, NULL);
11050 HOST_WIDE_INT lunroll = 0;
11051 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
11052 || TREE_CODE (expr) != INTEGER_CST
11053 || (lunroll = tree_to_shwi (expr)) < 0
11054 || lunroll >= USHRT_MAX)
11056 error_at (location, "%<#pragma GCC unroll%> requires an"
11057 " assignment-expression that evaluates to a non-negative"
11058 " integral constant less than %u", USHRT_MAX);
11059 unroll = 0;
11061 else
11063 unroll = (unsigned short)lunroll;
11064 if (unroll == 0)
11065 unroll = 1;
11068 c_parser_skip_to_pragma_eol (parser);
11069 return unroll;
11072 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
11073 should be considered, statements. ALLOW_STMT is true if we're within
11074 the context of a function and such pragmas are to be allowed. Returns
11075 true if we actually parsed such a pragma. */
11077 static bool
11078 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
11080 unsigned int id;
11081 const char *construct = NULL;
11083 id = c_parser_peek_token (parser)->pragma_kind;
11084 gcc_assert (id != PRAGMA_NONE);
11086 switch (id)
11088 case PRAGMA_OACC_DECLARE:
11089 c_parser_oacc_declare (parser);
11090 return false;
11092 case PRAGMA_OACC_ENTER_DATA:
11093 if (context != pragma_compound)
11095 construct = "acc enter data";
11096 in_compound:
11097 if (context == pragma_stmt)
11099 error_at (c_parser_peek_token (parser)->location,
11100 "%<#pragma %s%> may only be used in compound "
11101 "statements", construct);
11102 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11103 return false;
11105 goto bad_stmt;
11107 c_parser_oacc_enter_exit_data (parser, true);
11108 return false;
11110 case PRAGMA_OACC_EXIT_DATA:
11111 if (context != pragma_compound)
11113 construct = "acc exit data";
11114 goto in_compound;
11116 c_parser_oacc_enter_exit_data (parser, false);
11117 return false;
11119 case PRAGMA_OACC_ROUTINE:
11120 if (context != pragma_external)
11122 error_at (c_parser_peek_token (parser)->location,
11123 "%<#pragma acc routine%> must be at file scope");
11124 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11125 return false;
11127 c_parser_oacc_routine (parser, context);
11128 return false;
11130 case PRAGMA_OACC_UPDATE:
11131 if (context != pragma_compound)
11133 construct = "acc update";
11134 goto in_compound;
11136 c_parser_oacc_update (parser);
11137 return false;
11139 case PRAGMA_OMP_BARRIER:
11140 if (context != pragma_compound)
11142 construct = "omp barrier";
11143 goto in_compound;
11145 c_parser_omp_barrier (parser);
11146 return false;
11148 case PRAGMA_OMP_FLUSH:
11149 if (context != pragma_compound)
11151 construct = "omp flush";
11152 goto in_compound;
11154 c_parser_omp_flush (parser);
11155 return false;
11157 case PRAGMA_OMP_TASKWAIT:
11158 if (context != pragma_compound)
11160 construct = "omp taskwait";
11161 goto in_compound;
11163 c_parser_omp_taskwait (parser);
11164 return false;
11166 case PRAGMA_OMP_TASKYIELD:
11167 if (context != pragma_compound)
11169 construct = "omp taskyield";
11170 goto in_compound;
11172 c_parser_omp_taskyield (parser);
11173 return false;
11175 case PRAGMA_OMP_CANCEL:
11176 if (context != pragma_compound)
11178 construct = "omp cancel";
11179 goto in_compound;
11181 c_parser_omp_cancel (parser);
11182 return false;
11184 case PRAGMA_OMP_CANCELLATION_POINT:
11185 c_parser_omp_cancellation_point (parser, context);
11186 return false;
11188 case PRAGMA_OMP_THREADPRIVATE:
11189 c_parser_omp_threadprivate (parser);
11190 return false;
11192 case PRAGMA_OMP_TARGET:
11193 return c_parser_omp_target (parser, context, if_p);
11195 case PRAGMA_OMP_END_DECLARE_TARGET:
11196 c_parser_omp_end_declare_target (parser);
11197 return false;
11199 case PRAGMA_OMP_SECTION:
11200 error_at (c_parser_peek_token (parser)->location,
11201 "%<#pragma omp section%> may only be used in "
11202 "%<#pragma omp sections%> construct");
11203 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11204 return false;
11206 case PRAGMA_OMP_DECLARE:
11207 c_parser_omp_declare (parser, context);
11208 return false;
11210 case PRAGMA_OMP_ORDERED:
11211 return c_parser_omp_ordered (parser, context, if_p);
11213 case PRAGMA_IVDEP:
11215 const bool ivdep = c_parse_pragma_ivdep (parser);
11216 unsigned short unroll;
11217 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11218 unroll = c_parser_pragma_unroll (parser);
11219 else
11220 unroll = 0;
11221 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11222 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11223 && !c_parser_next_token_is_keyword (parser, RID_DO))
11225 c_parser_error (parser, "for, while or do statement expected");
11226 return false;
11228 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11229 c_parser_for_statement (parser, ivdep, unroll, if_p);
11230 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11231 c_parser_while_statement (parser, ivdep, unroll, if_p);
11232 else
11233 c_parser_do_statement (parser, ivdep, unroll);
11235 return false;
11237 case PRAGMA_UNROLL:
11239 unsigned short unroll = c_parser_pragma_unroll (parser);
11240 bool ivdep;
11241 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11242 ivdep = c_parse_pragma_ivdep (parser);
11243 else
11244 ivdep = false;
11245 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11246 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11247 && !c_parser_next_token_is_keyword (parser, RID_DO))
11249 c_parser_error (parser, "for, while or do statement expected");
11250 return false;
11252 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11253 c_parser_for_statement (parser, ivdep, unroll, if_p);
11254 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11255 c_parser_while_statement (parser, ivdep, unroll, if_p);
11256 else
11257 c_parser_do_statement (parser, ivdep, unroll);
11259 return false;
11261 case PRAGMA_GCC_PCH_PREPROCESS:
11262 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11263 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11264 return false;
11266 case PRAGMA_OACC_WAIT:
11267 if (context != pragma_compound)
11269 construct = "acc wait";
11270 goto in_compound;
11272 /* FALL THROUGH. */
11274 default:
11275 if (id < PRAGMA_FIRST_EXTERNAL)
11277 if (context != pragma_stmt && context != pragma_compound)
11279 bad_stmt:
11280 c_parser_error (parser, "expected declaration specifiers");
11281 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11282 return false;
11284 c_parser_omp_construct (parser, if_p);
11285 return true;
11287 break;
11290 c_parser_consume_pragma (parser);
11291 c_invoke_pragma_handler (id);
11293 /* Skip to EOL, but suppress any error message. Those will have been
11294 generated by the handler routine through calling error, as opposed
11295 to calling c_parser_error. */
11296 parser->error = true;
11297 c_parser_skip_to_pragma_eol (parser);
11299 return false;
11302 /* The interface the pragma parsers have to the lexer. */
11304 enum cpp_ttype
11305 pragma_lex (tree *value, location_t *loc)
11307 c_token *tok = c_parser_peek_token (the_parser);
11308 enum cpp_ttype ret = tok->type;
11310 *value = tok->value;
11311 if (loc)
11312 *loc = tok->location;
11314 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11315 ret = CPP_EOF;
11316 else
11318 if (ret == CPP_KEYWORD)
11319 ret = CPP_NAME;
11320 c_parser_consume_token (the_parser);
11323 return ret;
11326 static void
11327 c_parser_pragma_pch_preprocess (c_parser *parser)
11329 tree name = NULL;
11331 c_parser_consume_pragma (parser);
11332 if (c_parser_next_token_is (parser, CPP_STRING))
11334 name = c_parser_peek_token (parser)->value;
11335 c_parser_consume_token (parser);
11337 else
11338 c_parser_error (parser, "expected string literal");
11339 c_parser_skip_to_pragma_eol (parser);
11341 if (name)
11342 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11345 /* OpenACC and OpenMP parsing routines. */
11347 /* Returns name of the next clause.
11348 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11349 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11350 returned and the token is consumed. */
11352 static pragma_omp_clause
11353 c_parser_omp_clause_name (c_parser *parser)
11355 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11357 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11358 result = PRAGMA_OACC_CLAUSE_AUTO;
11359 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11360 result = PRAGMA_OMP_CLAUSE_IF;
11361 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11362 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11363 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11364 result = PRAGMA_OMP_CLAUSE_FOR;
11365 else if (c_parser_next_token_is (parser, CPP_NAME))
11367 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11369 switch (p[0])
11371 case 'a':
11372 if (!strcmp ("aligned", p))
11373 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11374 else if (!strcmp ("async", p))
11375 result = PRAGMA_OACC_CLAUSE_ASYNC;
11376 break;
11377 case 'c':
11378 if (!strcmp ("collapse", p))
11379 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11380 else if (!strcmp ("copy", p))
11381 result = PRAGMA_OACC_CLAUSE_COPY;
11382 else if (!strcmp ("copyin", p))
11383 result = PRAGMA_OMP_CLAUSE_COPYIN;
11384 else if (!strcmp ("copyout", p))
11385 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11386 else if (!strcmp ("copyprivate", p))
11387 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11388 else if (!strcmp ("create", p))
11389 result = PRAGMA_OACC_CLAUSE_CREATE;
11390 break;
11391 case 'd':
11392 if (!strcmp ("defaultmap", p))
11393 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11394 else if (!strcmp ("delete", p))
11395 result = PRAGMA_OACC_CLAUSE_DELETE;
11396 else if (!strcmp ("depend", p))
11397 result = PRAGMA_OMP_CLAUSE_DEPEND;
11398 else if (!strcmp ("device", p))
11399 result = PRAGMA_OMP_CLAUSE_DEVICE;
11400 else if (!strcmp ("deviceptr", p))
11401 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11402 else if (!strcmp ("device_resident", p))
11403 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11404 else if (!strcmp ("dist_schedule", p))
11405 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11406 break;
11407 case 'f':
11408 if (!strcmp ("final", p))
11409 result = PRAGMA_OMP_CLAUSE_FINAL;
11410 else if (!strcmp ("finalize", p))
11411 result = PRAGMA_OACC_CLAUSE_FINALIZE;
11412 else if (!strcmp ("firstprivate", p))
11413 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11414 else if (!strcmp ("from", p))
11415 result = PRAGMA_OMP_CLAUSE_FROM;
11416 break;
11417 case 'g':
11418 if (!strcmp ("gang", p))
11419 result = PRAGMA_OACC_CLAUSE_GANG;
11420 else if (!strcmp ("grainsize", p))
11421 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11422 break;
11423 case 'h':
11424 if (!strcmp ("hint", p))
11425 result = PRAGMA_OMP_CLAUSE_HINT;
11426 else if (!strcmp ("host", p))
11427 result = PRAGMA_OACC_CLAUSE_HOST;
11428 break;
11429 case 'i':
11430 if (!strcmp ("if_present", p))
11431 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
11432 else if (!strcmp ("inbranch", p))
11433 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11434 else if (!strcmp ("independent", p))
11435 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11436 else if (!strcmp ("is_device_ptr", p))
11437 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11438 break;
11439 case 'l':
11440 if (!strcmp ("lastprivate", p))
11441 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11442 else if (!strcmp ("linear", p))
11443 result = PRAGMA_OMP_CLAUSE_LINEAR;
11444 else if (!strcmp ("link", p))
11445 result = PRAGMA_OMP_CLAUSE_LINK;
11446 break;
11447 case 'm':
11448 if (!strcmp ("map", p))
11449 result = PRAGMA_OMP_CLAUSE_MAP;
11450 else if (!strcmp ("mergeable", p))
11451 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11452 break;
11453 case 'n':
11454 if (!strcmp ("nogroup", p))
11455 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11456 else if (!strcmp ("notinbranch", p))
11457 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11458 else if (!strcmp ("nowait", p))
11459 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11460 else if (!strcmp ("num_gangs", p))
11461 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11462 else if (!strcmp ("num_tasks", p))
11463 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11464 else if (!strcmp ("num_teams", p))
11465 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11466 else if (!strcmp ("num_threads", p))
11467 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11468 else if (!strcmp ("num_workers", p))
11469 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11470 break;
11471 case 'o':
11472 if (!strcmp ("ordered", p))
11473 result = PRAGMA_OMP_CLAUSE_ORDERED;
11474 break;
11475 case 'p':
11476 if (!strcmp ("parallel", p))
11477 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11478 else if (!strcmp ("present", p))
11479 result = PRAGMA_OACC_CLAUSE_PRESENT;
11480 /* As of OpenACC 2.5, these are now aliases of the non-present_or
11481 clauses. */
11482 else if (!strcmp ("present_or_copy", p)
11483 || !strcmp ("pcopy", p))
11484 result = PRAGMA_OACC_CLAUSE_COPY;
11485 else if (!strcmp ("present_or_copyin", p)
11486 || !strcmp ("pcopyin", p))
11487 result = PRAGMA_OACC_CLAUSE_COPYIN;
11488 else if (!strcmp ("present_or_copyout", p)
11489 || !strcmp ("pcopyout", p))
11490 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11491 else if (!strcmp ("present_or_create", p)
11492 || !strcmp ("pcreate", p))
11493 result = PRAGMA_OACC_CLAUSE_CREATE;
11494 else if (!strcmp ("priority", p))
11495 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11496 else if (!strcmp ("private", p))
11497 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11498 else if (!strcmp ("proc_bind", p))
11499 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11500 break;
11501 case 'r':
11502 if (!strcmp ("reduction", p))
11503 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11504 break;
11505 case 's':
11506 if (!strcmp ("safelen", p))
11507 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11508 else if (!strcmp ("schedule", p))
11509 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11510 else if (!strcmp ("sections", p))
11511 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11512 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
11513 result = PRAGMA_OACC_CLAUSE_HOST;
11514 else if (!strcmp ("seq", p))
11515 result = PRAGMA_OACC_CLAUSE_SEQ;
11516 else if (!strcmp ("shared", p))
11517 result = PRAGMA_OMP_CLAUSE_SHARED;
11518 else if (!strcmp ("simd", p))
11519 result = PRAGMA_OMP_CLAUSE_SIMD;
11520 else if (!strcmp ("simdlen", p))
11521 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11522 break;
11523 case 't':
11524 if (!strcmp ("taskgroup", p))
11525 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11526 else if (!strcmp ("thread_limit", p))
11527 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11528 else if (!strcmp ("threads", p))
11529 result = PRAGMA_OMP_CLAUSE_THREADS;
11530 else if (!strcmp ("tile", p))
11531 result = PRAGMA_OACC_CLAUSE_TILE;
11532 else if (!strcmp ("to", p))
11533 result = PRAGMA_OMP_CLAUSE_TO;
11534 break;
11535 case 'u':
11536 if (!strcmp ("uniform", p))
11537 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11538 else if (!strcmp ("untied", p))
11539 result = PRAGMA_OMP_CLAUSE_UNTIED;
11540 else if (!strcmp ("use_device", p))
11541 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11542 else if (!strcmp ("use_device_ptr", p))
11543 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11544 break;
11545 case 'v':
11546 if (!strcmp ("vector", p))
11547 result = PRAGMA_OACC_CLAUSE_VECTOR;
11548 else if (!strcmp ("vector_length", p))
11549 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11550 break;
11551 case 'w':
11552 if (!strcmp ("wait", p))
11553 result = PRAGMA_OACC_CLAUSE_WAIT;
11554 else if (!strcmp ("worker", p))
11555 result = PRAGMA_OACC_CLAUSE_WORKER;
11556 break;
11560 if (result != PRAGMA_OMP_CLAUSE_NONE)
11561 c_parser_consume_token (parser);
11563 return result;
11566 /* Validate that a clause of the given type does not already exist. */
11568 static void
11569 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11570 const char *name)
11572 tree c;
11574 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11575 if (OMP_CLAUSE_CODE (c) == code)
11577 location_t loc = OMP_CLAUSE_LOCATION (c);
11578 error_at (loc, "too many %qs clauses", name);
11579 break;
11583 /* OpenACC 2.0
11584 Parse wait clause or wait directive parameters. */
11586 static tree
11587 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11589 vec<tree, va_gc> *args;
11590 tree t, args_tree;
11592 matching_parens parens;
11593 if (!parens.require_open (parser))
11594 return list;
11596 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11598 if (args->length () == 0)
11600 c_parser_error (parser, "expected integer expression before ')'");
11601 release_tree_vector (args);
11602 return list;
11605 args_tree = build_tree_list_vec (args);
11607 for (t = args_tree; t; t = TREE_CHAIN (t))
11609 tree targ = TREE_VALUE (t);
11611 if (targ != error_mark_node)
11613 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11615 c_parser_error (parser, "expression must be integral");
11616 targ = error_mark_node;
11618 else
11620 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11622 OMP_CLAUSE_DECL (c) = targ;
11623 OMP_CLAUSE_CHAIN (c) = list;
11624 list = c;
11629 release_tree_vector (args);
11630 parens.require_close (parser);
11631 return list;
11634 /* OpenACC 2.0, OpenMP 2.5:
11635 variable-list:
11636 identifier
11637 variable-list , identifier
11639 If KIND is nonzero, create the appropriate node and install the
11640 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11641 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11643 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11644 return the list created. */
11646 static tree
11647 c_parser_omp_variable_list (c_parser *parser,
11648 location_t clause_loc,
11649 enum omp_clause_code kind, tree list)
11651 if (c_parser_next_token_is_not (parser, CPP_NAME)
11652 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11653 c_parser_error (parser, "expected identifier");
11655 while (c_parser_next_token_is (parser, CPP_NAME)
11656 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11658 tree t = lookup_name (c_parser_peek_token (parser)->value);
11660 if (t == NULL_TREE)
11662 undeclared_variable (c_parser_peek_token (parser)->location,
11663 c_parser_peek_token (parser)->value);
11664 t = error_mark_node;
11667 c_parser_consume_token (parser);
11669 if (t == error_mark_node)
11671 else if (kind != 0)
11673 switch (kind)
11675 case OMP_CLAUSE__CACHE_:
11676 /* The OpenACC cache directive explicitly only allows "array
11677 elements or subarrays". */
11678 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11680 c_parser_error (parser, "expected %<[%>");
11681 t = error_mark_node;
11682 break;
11684 /* FALLTHROUGH */
11685 case OMP_CLAUSE_MAP:
11686 case OMP_CLAUSE_FROM:
11687 case OMP_CLAUSE_TO:
11688 while (c_parser_next_token_is (parser, CPP_DOT))
11690 location_t op_loc = c_parser_peek_token (parser)->location;
11691 c_parser_consume_token (parser);
11692 if (!c_parser_next_token_is (parser, CPP_NAME))
11694 c_parser_error (parser, "expected identifier");
11695 t = error_mark_node;
11696 break;
11699 c_token *comp_tok = c_parser_peek_token (parser);
11700 tree ident = comp_tok->value;
11701 location_t comp_loc = comp_tok->location;
11702 c_parser_consume_token (parser);
11703 t = build_component_ref (op_loc, t, ident, comp_loc);
11705 /* FALLTHROUGH */
11706 case OMP_CLAUSE_DEPEND:
11707 case OMP_CLAUSE_REDUCTION:
11708 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11710 tree low_bound = NULL_TREE, length = NULL_TREE;
11712 c_parser_consume_token (parser);
11713 if (!c_parser_next_token_is (parser, CPP_COLON))
11715 location_t expr_loc
11716 = c_parser_peek_token (parser)->location;
11717 c_expr expr = c_parser_expression (parser);
11718 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11719 false, true);
11720 low_bound = expr.value;
11722 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11723 length = integer_one_node;
11724 else
11726 /* Look for `:'. */
11727 if (!c_parser_require (parser, CPP_COLON,
11728 "expected %<:%>"))
11730 t = error_mark_node;
11731 break;
11733 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11735 location_t expr_loc
11736 = c_parser_peek_token (parser)->location;
11737 c_expr expr = c_parser_expression (parser);
11738 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11739 false, true);
11740 length = expr.value;
11743 /* Look for the closing `]'. */
11744 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11745 "expected %<]%>"))
11747 t = error_mark_node;
11748 break;
11751 t = tree_cons (low_bound, length, t);
11753 break;
11754 default:
11755 break;
11758 if (t != error_mark_node)
11760 tree u = build_omp_clause (clause_loc, kind);
11761 OMP_CLAUSE_DECL (u) = t;
11762 OMP_CLAUSE_CHAIN (u) = list;
11763 list = u;
11766 else
11767 list = tree_cons (t, NULL_TREE, list);
11769 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11770 break;
11772 c_parser_consume_token (parser);
11775 return list;
11778 /* Similarly, but expect leading and trailing parenthesis. This is a very
11779 common case for OpenACC and OpenMP clauses. */
11781 static tree
11782 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11783 tree list)
11785 /* The clauses location. */
11786 location_t loc = c_parser_peek_token (parser)->location;
11788 matching_parens parens;
11789 if (parens.require_open (parser))
11791 list = c_parser_omp_variable_list (parser, loc, kind, list);
11792 parens.skip_until_found_close (parser);
11794 return list;
11797 /* OpenACC 2.0:
11798 copy ( variable-list )
11799 copyin ( variable-list )
11800 copyout ( variable-list )
11801 create ( variable-list )
11802 delete ( variable-list )
11803 present ( variable-list ) */
11805 static tree
11806 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11807 tree list)
11809 enum gomp_map_kind kind;
11810 switch (c_kind)
11812 case PRAGMA_OACC_CLAUSE_COPY:
11813 kind = GOMP_MAP_TOFROM;
11814 break;
11815 case PRAGMA_OACC_CLAUSE_COPYIN:
11816 kind = GOMP_MAP_TO;
11817 break;
11818 case PRAGMA_OACC_CLAUSE_COPYOUT:
11819 kind = GOMP_MAP_FROM;
11820 break;
11821 case PRAGMA_OACC_CLAUSE_CREATE:
11822 kind = GOMP_MAP_ALLOC;
11823 break;
11824 case PRAGMA_OACC_CLAUSE_DELETE:
11825 kind = GOMP_MAP_RELEASE;
11826 break;
11827 case PRAGMA_OACC_CLAUSE_DEVICE:
11828 kind = GOMP_MAP_FORCE_TO;
11829 break;
11830 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11831 kind = GOMP_MAP_DEVICE_RESIDENT;
11832 break;
11833 case PRAGMA_OACC_CLAUSE_HOST:
11834 kind = GOMP_MAP_FORCE_FROM;
11835 break;
11836 case PRAGMA_OACC_CLAUSE_LINK:
11837 kind = GOMP_MAP_LINK;
11838 break;
11839 case PRAGMA_OACC_CLAUSE_PRESENT:
11840 kind = GOMP_MAP_FORCE_PRESENT;
11841 break;
11842 default:
11843 gcc_unreachable ();
11845 tree nl, c;
11846 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11848 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11849 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11851 return nl;
11854 /* OpenACC 2.0:
11855 deviceptr ( variable-list ) */
11857 static tree
11858 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11860 location_t loc = c_parser_peek_token (parser)->location;
11861 tree vars, t;
11863 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11864 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11865 variable-list must only allow for pointer variables. */
11866 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11867 for (t = vars; t && t; t = TREE_CHAIN (t))
11869 tree v = TREE_PURPOSE (t);
11871 /* FIXME diagnostics: Ideally we should keep individual
11872 locations for all the variables in the var list to make the
11873 following errors more precise. Perhaps
11874 c_parser_omp_var_list_parens() should construct a list of
11875 locations to go along with the var list. */
11877 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11878 error_at (loc, "%qD is not a variable", v);
11879 else if (TREE_TYPE (v) == error_mark_node)
11881 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11882 error_at (loc, "%qD is not a pointer variable", v);
11884 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11885 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11886 OMP_CLAUSE_DECL (u) = v;
11887 OMP_CLAUSE_CHAIN (u) = list;
11888 list = u;
11891 return list;
11894 /* OpenACC 2.0, OpenMP 3.0:
11895 collapse ( constant-expression ) */
11897 static tree
11898 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11900 tree c, num = error_mark_node;
11901 HOST_WIDE_INT n;
11902 location_t loc;
11904 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11905 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11907 loc = c_parser_peek_token (parser)->location;
11908 matching_parens parens;
11909 if (parens.require_open (parser))
11911 num = c_parser_expr_no_commas (parser, NULL).value;
11912 parens.skip_until_found_close (parser);
11914 if (num == error_mark_node)
11915 return list;
11916 mark_exp_read (num);
11917 num = c_fully_fold (num, false, NULL);
11918 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11919 || !tree_fits_shwi_p (num)
11920 || (n = tree_to_shwi (num)) <= 0
11921 || (int) n != n)
11923 error_at (loc,
11924 "collapse argument needs positive constant integer expression");
11925 return list;
11927 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11928 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11929 OMP_CLAUSE_CHAIN (c) = list;
11930 return c;
11933 /* OpenMP 2.5:
11934 copyin ( variable-list ) */
11936 static tree
11937 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11939 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11942 /* OpenMP 2.5:
11943 copyprivate ( variable-list ) */
11945 static tree
11946 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11948 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11951 /* OpenMP 2.5:
11952 default ( none | shared )
11954 OpenACC:
11955 default ( none | present ) */
11957 static tree
11958 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11960 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11961 location_t loc = c_parser_peek_token (parser)->location;
11962 tree c;
11964 matching_parens parens;
11965 if (!parens.require_open (parser))
11966 return list;
11967 if (c_parser_next_token_is (parser, CPP_NAME))
11969 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11971 switch (p[0])
11973 case 'n':
11974 if (strcmp ("none", p) != 0)
11975 goto invalid_kind;
11976 kind = OMP_CLAUSE_DEFAULT_NONE;
11977 break;
11979 case 'p':
11980 if (strcmp ("present", p) != 0 || !is_oacc)
11981 goto invalid_kind;
11982 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11983 break;
11985 case 's':
11986 if (strcmp ("shared", p) != 0 || is_oacc)
11987 goto invalid_kind;
11988 kind = OMP_CLAUSE_DEFAULT_SHARED;
11989 break;
11991 default:
11992 goto invalid_kind;
11995 c_parser_consume_token (parser);
11997 else
11999 invalid_kind:
12000 if (is_oacc)
12001 c_parser_error (parser, "expected %<none%> or %<present%>");
12002 else
12003 c_parser_error (parser, "expected %<none%> or %<shared%>");
12005 parens.skip_until_found_close (parser);
12007 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
12008 return list;
12010 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
12011 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
12012 OMP_CLAUSE_CHAIN (c) = list;
12013 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
12015 return c;
12018 /* OpenMP 2.5:
12019 firstprivate ( variable-list ) */
12021 static tree
12022 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
12024 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
12027 /* OpenMP 3.1:
12028 final ( expression ) */
12030 static tree
12031 c_parser_omp_clause_final (c_parser *parser, tree list)
12033 location_t loc = c_parser_peek_token (parser)->location;
12034 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12036 tree t = c_parser_paren_condition (parser);
12037 tree c;
12039 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
12041 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
12042 OMP_CLAUSE_FINAL_EXPR (c) = t;
12043 OMP_CLAUSE_CHAIN (c) = list;
12044 list = c;
12046 else
12047 c_parser_error (parser, "expected %<(%>");
12049 return list;
12052 /* OpenACC, OpenMP 2.5:
12053 if ( expression )
12055 OpenMP 4.5:
12056 if ( directive-name-modifier : expression )
12058 directive-name-modifier:
12059 parallel | task | taskloop | target data | target | target update
12060 | target enter data | target exit data */
12062 static tree
12063 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
12065 location_t location = c_parser_peek_token (parser)->location;
12066 enum tree_code if_modifier = ERROR_MARK;
12068 matching_parens parens;
12069 if (!parens.require_open (parser))
12070 return list;
12072 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
12074 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12075 int n = 2;
12076 if (strcmp (p, "parallel") == 0)
12077 if_modifier = OMP_PARALLEL;
12078 else if (strcmp (p, "task") == 0)
12079 if_modifier = OMP_TASK;
12080 else if (strcmp (p, "taskloop") == 0)
12081 if_modifier = OMP_TASKLOOP;
12082 else if (strcmp (p, "target") == 0)
12084 if_modifier = OMP_TARGET;
12085 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12087 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
12088 if (strcmp ("data", p) == 0)
12089 if_modifier = OMP_TARGET_DATA;
12090 else if (strcmp ("update", p) == 0)
12091 if_modifier = OMP_TARGET_UPDATE;
12092 else if (strcmp ("enter", p) == 0)
12093 if_modifier = OMP_TARGET_ENTER_DATA;
12094 else if (strcmp ("exit", p) == 0)
12095 if_modifier = OMP_TARGET_EXIT_DATA;
12096 if (if_modifier != OMP_TARGET)
12098 n = 3;
12099 c_parser_consume_token (parser);
12101 else
12103 location_t loc = c_parser_peek_2nd_token (parser)->location;
12104 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
12105 "or %<exit%>");
12106 if_modifier = ERROR_MARK;
12108 if (if_modifier == OMP_TARGET_ENTER_DATA
12109 || if_modifier == OMP_TARGET_EXIT_DATA)
12111 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12113 p = IDENTIFIER_POINTER
12114 (c_parser_peek_2nd_token (parser)->value);
12115 if (strcmp ("data", p) == 0)
12116 n = 4;
12118 if (n == 4)
12119 c_parser_consume_token (parser);
12120 else
12122 location_t loc
12123 = c_parser_peek_2nd_token (parser)->location;
12124 error_at (loc, "expected %<data%>");
12125 if_modifier = ERROR_MARK;
12130 if (if_modifier != ERROR_MARK)
12132 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12134 c_parser_consume_token (parser);
12135 c_parser_consume_token (parser);
12137 else
12139 if (n > 2)
12141 location_t loc = c_parser_peek_2nd_token (parser)->location;
12142 error_at (loc, "expected %<:%>");
12144 if_modifier = ERROR_MARK;
12149 tree t = c_parser_condition (parser), c;
12150 parens.skip_until_found_close (parser);
12152 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12153 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12155 if (if_modifier != ERROR_MARK
12156 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12158 const char *p = NULL;
12159 switch (if_modifier)
12161 case OMP_PARALLEL: p = "parallel"; break;
12162 case OMP_TASK: p = "task"; break;
12163 case OMP_TASKLOOP: p = "taskloop"; break;
12164 case OMP_TARGET_DATA: p = "target data"; break;
12165 case OMP_TARGET: p = "target"; break;
12166 case OMP_TARGET_UPDATE: p = "target update"; break;
12167 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12168 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12169 default: gcc_unreachable ();
12171 error_at (location, "too many %<if%> clauses with %qs modifier",
12173 return list;
12175 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12177 if (!is_omp)
12178 error_at (location, "too many %<if%> clauses");
12179 else
12180 error_at (location, "too many %<if%> clauses without modifier");
12181 return list;
12183 else if (if_modifier == ERROR_MARK
12184 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12186 error_at (location, "if any %<if%> clause has modifier, then all "
12187 "%<if%> clauses have to use modifier");
12188 return list;
12192 c = build_omp_clause (location, OMP_CLAUSE_IF);
12193 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12194 OMP_CLAUSE_IF_EXPR (c) = t;
12195 OMP_CLAUSE_CHAIN (c) = list;
12196 return c;
12199 /* OpenMP 2.5:
12200 lastprivate ( variable-list ) */
12202 static tree
12203 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12205 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12208 /* OpenMP 3.1:
12209 mergeable */
12211 static tree
12212 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12214 tree c;
12216 /* FIXME: Should we allow duplicates? */
12217 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12219 c = build_omp_clause (c_parser_peek_token (parser)->location,
12220 OMP_CLAUSE_MERGEABLE);
12221 OMP_CLAUSE_CHAIN (c) = list;
12223 return c;
12226 /* OpenMP 2.5:
12227 nowait */
12229 static tree
12230 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12232 tree c;
12233 location_t loc = c_parser_peek_token (parser)->location;
12235 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12237 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12238 OMP_CLAUSE_CHAIN (c) = list;
12239 return c;
12242 /* OpenMP 2.5:
12243 num_threads ( expression ) */
12245 static tree
12246 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12248 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12249 matching_parens parens;
12250 if (parens.require_open (parser))
12252 location_t expr_loc = c_parser_peek_token (parser)->location;
12253 c_expr expr = c_parser_expression (parser);
12254 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12255 tree c, t = expr.value;
12256 t = c_fully_fold (t, false, NULL);
12258 parens.skip_until_found_close (parser);
12260 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12262 c_parser_error (parser, "expected integer expression");
12263 return list;
12266 /* Attempt to statically determine when the number isn't positive. */
12267 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12268 build_int_cst (TREE_TYPE (t), 0));
12269 protected_set_expr_location (c, expr_loc);
12270 if (c == boolean_true_node)
12272 warning_at (expr_loc, 0,
12273 "%<num_threads%> value must be positive");
12274 t = integer_one_node;
12277 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12279 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12280 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12281 OMP_CLAUSE_CHAIN (c) = list;
12282 list = c;
12285 return list;
12288 /* OpenMP 4.5:
12289 num_tasks ( expression ) */
12291 static tree
12292 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12294 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12295 matching_parens parens;
12296 if (parens.require_open (parser))
12298 location_t expr_loc = c_parser_peek_token (parser)->location;
12299 c_expr expr = c_parser_expression (parser);
12300 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12301 tree c, t = expr.value;
12302 t = c_fully_fold (t, false, NULL);
12304 parens.skip_until_found_close (parser);
12306 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12308 c_parser_error (parser, "expected integer expression");
12309 return list;
12312 /* Attempt to statically determine when the number isn't positive. */
12313 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12314 build_int_cst (TREE_TYPE (t), 0));
12315 if (CAN_HAVE_LOCATION_P (c))
12316 SET_EXPR_LOCATION (c, expr_loc);
12317 if (c == boolean_true_node)
12319 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12320 t = integer_one_node;
12323 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12325 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12326 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12327 OMP_CLAUSE_CHAIN (c) = list;
12328 list = c;
12331 return list;
12334 /* OpenMP 4.5:
12335 grainsize ( expression ) */
12337 static tree
12338 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12340 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12341 matching_parens parens;
12342 if (parens.require_open (parser))
12344 location_t expr_loc = c_parser_peek_token (parser)->location;
12345 c_expr expr = c_parser_expression (parser);
12346 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12347 tree c, t = expr.value;
12348 t = c_fully_fold (t, false, NULL);
12350 parens.skip_until_found_close (parser);
12352 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12354 c_parser_error (parser, "expected integer expression");
12355 return list;
12358 /* Attempt to statically determine when the number isn't positive. */
12359 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12360 build_int_cst (TREE_TYPE (t), 0));
12361 if (CAN_HAVE_LOCATION_P (c))
12362 SET_EXPR_LOCATION (c, expr_loc);
12363 if (c == boolean_true_node)
12365 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12366 t = integer_one_node;
12369 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12371 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12372 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12373 OMP_CLAUSE_CHAIN (c) = list;
12374 list = c;
12377 return list;
12380 /* OpenMP 4.5:
12381 priority ( expression ) */
12383 static tree
12384 c_parser_omp_clause_priority (c_parser *parser, tree list)
12386 location_t priority_loc = c_parser_peek_token (parser)->location;
12387 matching_parens parens;
12388 if (parens.require_open (parser))
12390 location_t expr_loc = c_parser_peek_token (parser)->location;
12391 c_expr expr = c_parser_expression (parser);
12392 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12393 tree c, t = expr.value;
12394 t = c_fully_fold (t, false, NULL);
12396 parens.skip_until_found_close (parser);
12398 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12400 c_parser_error (parser, "expected integer expression");
12401 return list;
12404 /* Attempt to statically determine when the number isn't
12405 non-negative. */
12406 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12407 build_int_cst (TREE_TYPE (t), 0));
12408 if (CAN_HAVE_LOCATION_P (c))
12409 SET_EXPR_LOCATION (c, expr_loc);
12410 if (c == boolean_true_node)
12412 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12413 t = integer_one_node;
12416 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12418 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12419 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12420 OMP_CLAUSE_CHAIN (c) = list;
12421 list = c;
12424 return list;
12427 /* OpenMP 4.5:
12428 hint ( expression ) */
12430 static tree
12431 c_parser_omp_clause_hint (c_parser *parser, tree list)
12433 location_t hint_loc = c_parser_peek_token (parser)->location;
12434 matching_parens parens;
12435 if (parens.require_open (parser))
12437 location_t expr_loc = c_parser_peek_token (parser)->location;
12438 c_expr expr = c_parser_expression (parser);
12439 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12440 tree c, t = expr.value;
12441 t = c_fully_fold (t, false, NULL);
12443 parens.skip_until_found_close (parser);
12445 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12447 c_parser_error (parser, "expected integer expression");
12448 return list;
12451 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12453 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12454 OMP_CLAUSE_HINT_EXPR (c) = t;
12455 OMP_CLAUSE_CHAIN (c) = list;
12456 list = c;
12459 return list;
12462 /* OpenMP 4.5:
12463 defaultmap ( tofrom : scalar ) */
12465 static tree
12466 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12468 location_t loc = c_parser_peek_token (parser)->location;
12469 tree c;
12470 const char *p;
12472 matching_parens parens;
12473 if (!parens.require_open (parser))
12474 return list;
12475 if (!c_parser_next_token_is (parser, CPP_NAME))
12477 c_parser_error (parser, "expected %<tofrom%>");
12478 goto out_err;
12480 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12481 if (strcmp (p, "tofrom") != 0)
12483 c_parser_error (parser, "expected %<tofrom%>");
12484 goto out_err;
12486 c_parser_consume_token (parser);
12487 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12488 goto out_err;
12489 if (!c_parser_next_token_is (parser, CPP_NAME))
12491 c_parser_error (parser, "expected %<scalar%>");
12492 goto out_err;
12494 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12495 if (strcmp (p, "scalar") != 0)
12497 c_parser_error (parser, "expected %<scalar%>");
12498 goto out_err;
12500 c_parser_consume_token (parser);
12501 parens.skip_until_found_close (parser);
12502 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12503 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12504 OMP_CLAUSE_CHAIN (c) = list;
12505 return c;
12507 out_err:
12508 parens.skip_until_found_close (parser);
12509 return list;
12512 /* OpenACC 2.0:
12513 use_device ( variable-list )
12515 OpenMP 4.5:
12516 use_device_ptr ( variable-list ) */
12518 static tree
12519 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12521 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12522 list);
12525 /* OpenMP 4.5:
12526 is_device_ptr ( variable-list ) */
12528 static tree
12529 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12531 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12534 /* OpenACC:
12535 num_gangs ( expression )
12536 num_workers ( expression )
12537 vector_length ( expression ) */
12539 static tree
12540 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12541 tree list)
12543 location_t loc = c_parser_peek_token (parser)->location;
12545 matching_parens parens;
12546 if (!parens.require_open (parser))
12547 return list;
12549 location_t expr_loc = c_parser_peek_token (parser)->location;
12550 c_expr expr = c_parser_expression (parser);
12551 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12552 tree c, t = expr.value;
12553 t = c_fully_fold (t, false, NULL);
12555 parens.skip_until_found_close (parser);
12557 if (t == error_mark_node)
12558 return list;
12559 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12561 error_at (expr_loc, "%qs expression must be integral",
12562 omp_clause_code_name[code]);
12563 return list;
12566 /* Attempt to statically determine when the number isn't positive. */
12567 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12568 build_int_cst (TREE_TYPE (t), 0));
12569 protected_set_expr_location (c, expr_loc);
12570 if (c == boolean_true_node)
12572 warning_at (expr_loc, 0,
12573 "%qs value must be positive",
12574 omp_clause_code_name[code]);
12575 t = integer_one_node;
12578 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12580 c = build_omp_clause (loc, code);
12581 OMP_CLAUSE_OPERAND (c, 0) = t;
12582 OMP_CLAUSE_CHAIN (c) = list;
12583 return c;
12586 /* OpenACC:
12588 gang [( gang-arg-list )]
12589 worker [( [num:] int-expr )]
12590 vector [( [length:] int-expr )]
12592 where gang-arg is one of:
12594 [num:] int-expr
12595 static: size-expr
12597 and size-expr may be:
12600 int-expr
12603 static tree
12604 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12605 const char *str, tree list)
12607 const char *id = "num";
12608 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12609 location_t loc = c_parser_peek_token (parser)->location;
12611 if (kind == OMP_CLAUSE_VECTOR)
12612 id = "length";
12614 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12616 c_parser_consume_token (parser);
12620 c_token *next = c_parser_peek_token (parser);
12621 int idx = 0;
12623 /* Gang static argument. */
12624 if (kind == OMP_CLAUSE_GANG
12625 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12627 c_parser_consume_token (parser);
12629 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12630 goto cleanup_error;
12632 idx = 1;
12633 if (ops[idx] != NULL_TREE)
12635 c_parser_error (parser, "too many %<static%> arguments");
12636 goto cleanup_error;
12639 /* Check for the '*' argument. */
12640 if (c_parser_next_token_is (parser, CPP_MULT)
12641 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12642 || c_parser_peek_2nd_token (parser)->type
12643 == CPP_CLOSE_PAREN))
12645 c_parser_consume_token (parser);
12646 ops[idx] = integer_minus_one_node;
12648 if (c_parser_next_token_is (parser, CPP_COMMA))
12650 c_parser_consume_token (parser);
12651 continue;
12653 else
12654 break;
12657 /* Worker num: argument and vector length: arguments. */
12658 else if (c_parser_next_token_is (parser, CPP_NAME)
12659 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12660 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12662 c_parser_consume_token (parser); /* id */
12663 c_parser_consume_token (parser); /* ':' */
12666 /* Now collect the actual argument. */
12667 if (ops[idx] != NULL_TREE)
12669 c_parser_error (parser, "unexpected argument");
12670 goto cleanup_error;
12673 location_t expr_loc = c_parser_peek_token (parser)->location;
12674 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12675 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12676 tree expr = cexpr.value;
12677 if (expr == error_mark_node)
12678 goto cleanup_error;
12680 expr = c_fully_fold (expr, false, NULL);
12682 /* Attempt to statically determine when the number isn't a
12683 positive integer. */
12685 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12687 c_parser_error (parser, "expected integer expression");
12688 return list;
12691 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12692 build_int_cst (TREE_TYPE (expr), 0));
12693 if (c == boolean_true_node)
12695 warning_at (loc, 0,
12696 "%qs value must be positive", str);
12697 expr = integer_one_node;
12700 ops[idx] = expr;
12702 if (kind == OMP_CLAUSE_GANG
12703 && c_parser_next_token_is (parser, CPP_COMMA))
12705 c_parser_consume_token (parser);
12706 continue;
12708 break;
12710 while (1);
12712 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12713 goto cleanup_error;
12716 check_no_duplicate_clause (list, kind, str);
12718 c = build_omp_clause (loc, kind);
12720 if (ops[1])
12721 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12723 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12724 OMP_CLAUSE_CHAIN (c) = list;
12726 return c;
12728 cleanup_error:
12729 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12730 return list;
12733 /* OpenACC 2.5:
12734 auto
12735 finalize
12736 independent
12737 nohost
12738 seq */
12740 static tree
12741 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12742 tree list)
12744 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12746 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12747 OMP_CLAUSE_CHAIN (c) = list;
12749 return c;
12752 /* OpenACC:
12753 async [( int-expr )] */
12755 static tree
12756 c_parser_oacc_clause_async (c_parser *parser, tree list)
12758 tree c, t;
12759 location_t loc = c_parser_peek_token (parser)->location;
12761 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12763 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12765 c_parser_consume_token (parser);
12767 t = c_parser_expression (parser).value;
12768 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12769 c_parser_error (parser, "expected integer expression");
12770 else if (t == error_mark_node
12771 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12772 return list;
12774 else
12775 t = c_fully_fold (t, false, NULL);
12777 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12779 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12780 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12781 OMP_CLAUSE_CHAIN (c) = list;
12782 list = c;
12784 return list;
12787 /* OpenACC 2.0:
12788 tile ( size-expr-list ) */
12790 static tree
12791 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12793 tree c, expr = error_mark_node;
12794 location_t loc;
12795 tree tile = NULL_TREE;
12797 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12798 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12800 loc = c_parser_peek_token (parser)->location;
12801 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12802 return list;
12806 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12807 return list;
12809 if (c_parser_next_token_is (parser, CPP_MULT)
12810 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12811 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12813 c_parser_consume_token (parser);
12814 expr = integer_zero_node;
12816 else
12818 location_t expr_loc = c_parser_peek_token (parser)->location;
12819 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12820 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12821 expr = cexpr.value;
12823 if (expr == error_mark_node)
12825 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12826 "expected %<)%>");
12827 return list;
12830 expr = c_fully_fold (expr, false, NULL);
12832 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12833 || !tree_fits_shwi_p (expr)
12834 || tree_to_shwi (expr) <= 0)
12836 error_at (expr_loc, "%<tile%> argument needs positive"
12837 " integral constant");
12838 expr = integer_zero_node;
12842 tile = tree_cons (NULL_TREE, expr, tile);
12844 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12846 /* Consume the trailing ')'. */
12847 c_parser_consume_token (parser);
12849 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12850 tile = nreverse (tile);
12851 OMP_CLAUSE_TILE_LIST (c) = tile;
12852 OMP_CLAUSE_CHAIN (c) = list;
12853 return c;
12856 /* OpenACC:
12857 wait ( int-expr-list ) */
12859 static tree
12860 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12862 location_t clause_loc = c_parser_peek_token (parser)->location;
12864 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12865 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12867 return list;
12870 /* OpenMP 2.5:
12871 ordered
12873 OpenMP 4.5:
12874 ordered ( constant-expression ) */
12876 static tree
12877 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12879 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12881 tree c, num = NULL_TREE;
12882 HOST_WIDE_INT n;
12883 location_t loc = c_parser_peek_token (parser)->location;
12884 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12886 matching_parens parens;
12887 parens.consume_open (parser);
12888 num = c_parser_expr_no_commas (parser, NULL).value;
12889 parens.skip_until_found_close (parser);
12891 if (num == error_mark_node)
12892 return list;
12893 if (num)
12895 mark_exp_read (num);
12896 num = c_fully_fold (num, false, NULL);
12897 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12898 || !tree_fits_shwi_p (num)
12899 || (n = tree_to_shwi (num)) <= 0
12900 || (int) n != n)
12902 error_at (loc, "ordered argument needs positive "
12903 "constant integer expression");
12904 return list;
12907 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12908 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12909 OMP_CLAUSE_CHAIN (c) = list;
12910 return c;
12913 /* OpenMP 2.5:
12914 private ( variable-list ) */
12916 static tree
12917 c_parser_omp_clause_private (c_parser *parser, tree list)
12919 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12922 /* OpenMP 2.5:
12923 reduction ( reduction-operator : variable-list )
12925 reduction-operator:
12926 One of: + * - & ^ | && ||
12928 OpenMP 3.1:
12930 reduction-operator:
12931 One of: + * - & ^ | && || max min
12933 OpenMP 4.0:
12935 reduction-operator:
12936 One of: + * - & ^ | && ||
12937 identifier */
12939 static tree
12940 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12942 location_t clause_loc = c_parser_peek_token (parser)->location;
12943 matching_parens parens;
12944 if (parens.require_open (parser))
12946 enum tree_code code = ERROR_MARK;
12947 tree reduc_id = NULL_TREE;
12949 switch (c_parser_peek_token (parser)->type)
12951 case CPP_PLUS:
12952 code = PLUS_EXPR;
12953 break;
12954 case CPP_MULT:
12955 code = MULT_EXPR;
12956 break;
12957 case CPP_MINUS:
12958 code = MINUS_EXPR;
12959 break;
12960 case CPP_AND:
12961 code = BIT_AND_EXPR;
12962 break;
12963 case CPP_XOR:
12964 code = BIT_XOR_EXPR;
12965 break;
12966 case CPP_OR:
12967 code = BIT_IOR_EXPR;
12968 break;
12969 case CPP_AND_AND:
12970 code = TRUTH_ANDIF_EXPR;
12971 break;
12972 case CPP_OR_OR:
12973 code = TRUTH_ORIF_EXPR;
12974 break;
12975 case CPP_NAME:
12977 const char *p
12978 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12979 if (strcmp (p, "min") == 0)
12981 code = MIN_EXPR;
12982 break;
12984 if (strcmp (p, "max") == 0)
12986 code = MAX_EXPR;
12987 break;
12989 reduc_id = c_parser_peek_token (parser)->value;
12990 break;
12992 default:
12993 c_parser_error (parser,
12994 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12995 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12996 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12997 return list;
12999 c_parser_consume_token (parser);
13000 reduc_id = c_omp_reduction_id (code, reduc_id);
13001 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13003 tree nl, c;
13005 nl = c_parser_omp_variable_list (parser, clause_loc,
13006 OMP_CLAUSE_REDUCTION, list);
13007 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13009 tree d = OMP_CLAUSE_DECL (c), type;
13010 if (TREE_CODE (d) != TREE_LIST)
13011 type = TREE_TYPE (d);
13012 else
13014 int cnt = 0;
13015 tree t;
13016 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
13017 cnt++;
13018 type = TREE_TYPE (t);
13019 while (cnt > 0)
13021 if (TREE_CODE (type) != POINTER_TYPE
13022 && TREE_CODE (type) != ARRAY_TYPE)
13023 break;
13024 type = TREE_TYPE (type);
13025 cnt--;
13028 while (TREE_CODE (type) == ARRAY_TYPE)
13029 type = TREE_TYPE (type);
13030 OMP_CLAUSE_REDUCTION_CODE (c) = code;
13031 if (code == ERROR_MARK
13032 || !(INTEGRAL_TYPE_P (type)
13033 || TREE_CODE (type) == REAL_TYPE
13034 || TREE_CODE (type) == COMPLEX_TYPE))
13035 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
13036 = c_omp_reduction_lookup (reduc_id,
13037 TYPE_MAIN_VARIANT (type));
13040 list = nl;
13042 parens.skip_until_found_close (parser);
13044 return list;
13047 /* OpenMP 2.5:
13048 schedule ( schedule-kind )
13049 schedule ( schedule-kind , expression )
13051 schedule-kind:
13052 static | dynamic | guided | runtime | auto
13054 OpenMP 4.5:
13055 schedule ( schedule-modifier : schedule-kind )
13056 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
13058 schedule-modifier:
13059 simd
13060 monotonic
13061 nonmonotonic */
13063 static tree
13064 c_parser_omp_clause_schedule (c_parser *parser, tree list)
13066 tree c, t;
13067 location_t loc = c_parser_peek_token (parser)->location;
13068 int modifiers = 0, nmodifiers = 0;
13070 matching_parens parens;
13071 if (!parens.require_open (parser))
13072 return list;
13074 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
13076 while (c_parser_next_token_is (parser, CPP_NAME))
13078 tree kind = c_parser_peek_token (parser)->value;
13079 const char *p = IDENTIFIER_POINTER (kind);
13080 if (strcmp ("simd", p) == 0)
13081 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
13082 else if (strcmp ("monotonic", p) == 0)
13083 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
13084 else if (strcmp ("nonmonotonic", p) == 0)
13085 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
13086 else
13087 break;
13088 c_parser_consume_token (parser);
13089 if (nmodifiers++ == 0
13090 && c_parser_next_token_is (parser, CPP_COMMA))
13091 c_parser_consume_token (parser);
13092 else
13094 c_parser_require (parser, CPP_COLON, "expected %<:%>");
13095 break;
13099 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
13100 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13101 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
13102 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13104 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
13105 "specified");
13106 modifiers = 0;
13109 if (c_parser_next_token_is (parser, CPP_NAME))
13111 tree kind = c_parser_peek_token (parser)->value;
13112 const char *p = IDENTIFIER_POINTER (kind);
13114 switch (p[0])
13116 case 'd':
13117 if (strcmp ("dynamic", p) != 0)
13118 goto invalid_kind;
13119 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
13120 break;
13122 case 'g':
13123 if (strcmp ("guided", p) != 0)
13124 goto invalid_kind;
13125 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
13126 break;
13128 case 'r':
13129 if (strcmp ("runtime", p) != 0)
13130 goto invalid_kind;
13131 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
13132 break;
13134 default:
13135 goto invalid_kind;
13138 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
13139 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
13140 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
13141 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
13142 else
13143 goto invalid_kind;
13145 c_parser_consume_token (parser);
13146 if (c_parser_next_token_is (parser, CPP_COMMA))
13148 location_t here;
13149 c_parser_consume_token (parser);
13151 here = c_parser_peek_token (parser)->location;
13152 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13153 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13154 t = expr.value;
13155 t = c_fully_fold (t, false, NULL);
13157 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13158 error_at (here, "schedule %<runtime%> does not take "
13159 "a %<chunk_size%> parameter");
13160 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13161 error_at (here,
13162 "schedule %<auto%> does not take "
13163 "a %<chunk_size%> parameter");
13164 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13166 /* Attempt to statically determine when the number isn't
13167 positive. */
13168 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13169 build_int_cst (TREE_TYPE (t), 0));
13170 protected_set_expr_location (s, loc);
13171 if (s == boolean_true_node)
13173 warning_at (loc, 0,
13174 "chunk size value must be positive");
13175 t = integer_one_node;
13177 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13179 else
13180 c_parser_error (parser, "expected integer expression");
13182 parens.skip_until_found_close (parser);
13184 else
13185 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13186 "expected %<,%> or %<)%>");
13188 OMP_CLAUSE_SCHEDULE_KIND (c)
13189 = (enum omp_clause_schedule_kind)
13190 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13192 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13193 OMP_CLAUSE_CHAIN (c) = list;
13194 return c;
13196 invalid_kind:
13197 c_parser_error (parser, "invalid schedule kind");
13198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13199 return list;
13202 /* OpenMP 2.5:
13203 shared ( variable-list ) */
13205 static tree
13206 c_parser_omp_clause_shared (c_parser *parser, tree list)
13208 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13211 /* OpenMP 3.0:
13212 untied */
13214 static tree
13215 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13217 tree c;
13219 /* FIXME: Should we allow duplicates? */
13220 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13222 c = build_omp_clause (c_parser_peek_token (parser)->location,
13223 OMP_CLAUSE_UNTIED);
13224 OMP_CLAUSE_CHAIN (c) = list;
13226 return c;
13229 /* OpenMP 4.0:
13230 inbranch
13231 notinbranch */
13233 static tree
13234 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13235 enum omp_clause_code code, tree list)
13237 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13239 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13240 OMP_CLAUSE_CHAIN (c) = list;
13242 return c;
13245 /* OpenMP 4.0:
13246 parallel
13248 sections
13249 taskgroup */
13251 static tree
13252 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13253 enum omp_clause_code code, tree list)
13255 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13256 OMP_CLAUSE_CHAIN (c) = list;
13258 return c;
13261 /* OpenMP 4.5:
13262 nogroup */
13264 static tree
13265 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13267 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13268 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13269 OMP_CLAUSE_NOGROUP);
13270 OMP_CLAUSE_CHAIN (c) = list;
13271 return c;
13274 /* OpenMP 4.5:
13275 simd
13276 threads */
13278 static tree
13279 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13280 enum omp_clause_code code, tree list)
13282 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13283 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13284 OMP_CLAUSE_CHAIN (c) = list;
13285 return c;
13288 /* OpenMP 4.0:
13289 num_teams ( expression ) */
13291 static tree
13292 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13294 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13295 matching_parens parens;
13296 if (parens.require_open (parser))
13298 location_t expr_loc = c_parser_peek_token (parser)->location;
13299 c_expr expr = c_parser_expression (parser);
13300 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13301 tree c, t = expr.value;
13302 t = c_fully_fold (t, false, NULL);
13304 parens.skip_until_found_close (parser);
13306 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13308 c_parser_error (parser, "expected integer expression");
13309 return list;
13312 /* Attempt to statically determine when the number isn't positive. */
13313 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13314 build_int_cst (TREE_TYPE (t), 0));
13315 protected_set_expr_location (c, expr_loc);
13316 if (c == boolean_true_node)
13318 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13319 t = integer_one_node;
13322 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13324 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13325 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13326 OMP_CLAUSE_CHAIN (c) = list;
13327 list = c;
13330 return list;
13333 /* OpenMP 4.0:
13334 thread_limit ( expression ) */
13336 static tree
13337 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13339 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13340 matching_parens parens;
13341 if (parens.require_open (parser))
13343 location_t expr_loc = c_parser_peek_token (parser)->location;
13344 c_expr expr = c_parser_expression (parser);
13345 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13346 tree c, t = expr.value;
13347 t = c_fully_fold (t, false, NULL);
13349 parens.skip_until_found_close (parser);
13351 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13353 c_parser_error (parser, "expected integer expression");
13354 return list;
13357 /* Attempt to statically determine when the number isn't positive. */
13358 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13359 build_int_cst (TREE_TYPE (t), 0));
13360 protected_set_expr_location (c, expr_loc);
13361 if (c == boolean_true_node)
13363 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13364 t = integer_one_node;
13367 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13368 "thread_limit");
13370 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13371 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13372 OMP_CLAUSE_CHAIN (c) = list;
13373 list = c;
13376 return list;
13379 /* OpenMP 4.0:
13380 aligned ( variable-list )
13381 aligned ( variable-list : constant-expression ) */
13383 static tree
13384 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13386 location_t clause_loc = c_parser_peek_token (parser)->location;
13387 tree nl, c;
13389 matching_parens parens;
13390 if (!parens.require_open (parser))
13391 return list;
13393 nl = c_parser_omp_variable_list (parser, clause_loc,
13394 OMP_CLAUSE_ALIGNED, list);
13396 if (c_parser_next_token_is (parser, CPP_COLON))
13398 c_parser_consume_token (parser);
13399 location_t expr_loc = c_parser_peek_token (parser)->location;
13400 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13401 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13402 tree alignment = expr.value;
13403 alignment = c_fully_fold (alignment, false, NULL);
13404 if (TREE_CODE (alignment) != INTEGER_CST
13405 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13406 || tree_int_cst_sgn (alignment) != 1)
13408 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13409 "be positive constant integer expression");
13410 alignment = NULL_TREE;
13413 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13414 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13417 parens.skip_until_found_close (parser);
13418 return nl;
13421 /* OpenMP 4.0:
13422 linear ( variable-list )
13423 linear ( variable-list : expression )
13425 OpenMP 4.5:
13426 linear ( modifier ( variable-list ) )
13427 linear ( modifier ( variable-list ) : expression ) */
13429 static tree
13430 c_parser_omp_clause_linear (c_parser *parser, tree list)
13432 location_t clause_loc = c_parser_peek_token (parser)->location;
13433 tree nl, c, step;
13434 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13436 matching_parens parens;
13437 if (!parens.require_open (parser))
13438 return list;
13440 if (c_parser_next_token_is (parser, CPP_NAME))
13442 c_token *tok = c_parser_peek_token (parser);
13443 const char *p = IDENTIFIER_POINTER (tok->value);
13444 if (strcmp ("val", p) == 0)
13445 kind = OMP_CLAUSE_LINEAR_VAL;
13446 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13447 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13448 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13450 c_parser_consume_token (parser);
13451 c_parser_consume_token (parser);
13455 nl = c_parser_omp_variable_list (parser, clause_loc,
13456 OMP_CLAUSE_LINEAR, list);
13458 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13459 parens.skip_until_found_close (parser);
13461 if (c_parser_next_token_is (parser, CPP_COLON))
13463 c_parser_consume_token (parser);
13464 location_t expr_loc = c_parser_peek_token (parser)->location;
13465 c_expr expr = c_parser_expression (parser);
13466 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13467 step = expr.value;
13468 step = c_fully_fold (step, false, NULL);
13469 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13471 error_at (clause_loc, "%<linear%> clause step expression must "
13472 "be integral");
13473 step = integer_one_node;
13477 else
13478 step = integer_one_node;
13480 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13482 OMP_CLAUSE_LINEAR_STEP (c) = step;
13483 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13486 parens.skip_until_found_close (parser);
13487 return nl;
13490 /* OpenMP 4.0:
13491 safelen ( constant-expression ) */
13493 static tree
13494 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13496 location_t clause_loc = c_parser_peek_token (parser)->location;
13497 tree c, t;
13499 matching_parens parens;
13500 if (!parens.require_open (parser))
13501 return list;
13503 location_t expr_loc = c_parser_peek_token (parser)->location;
13504 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13505 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13506 t = expr.value;
13507 t = c_fully_fold (t, false, NULL);
13508 if (TREE_CODE (t) != INTEGER_CST
13509 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13510 || tree_int_cst_sgn (t) != 1)
13512 error_at (clause_loc, "%<safelen%> clause expression must "
13513 "be positive constant integer expression");
13514 t = NULL_TREE;
13517 parens.skip_until_found_close (parser);
13518 if (t == NULL_TREE || t == error_mark_node)
13519 return list;
13521 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13523 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13524 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13525 OMP_CLAUSE_CHAIN (c) = list;
13526 return c;
13529 /* OpenMP 4.0:
13530 simdlen ( constant-expression ) */
13532 static tree
13533 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13535 location_t clause_loc = c_parser_peek_token (parser)->location;
13536 tree c, t;
13538 matching_parens parens;
13539 if (!parens.require_open (parser))
13540 return list;
13542 location_t expr_loc = c_parser_peek_token (parser)->location;
13543 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13544 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13545 t = expr.value;
13546 t = c_fully_fold (t, false, NULL);
13547 if (TREE_CODE (t) != INTEGER_CST
13548 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13549 || tree_int_cst_sgn (t) != 1)
13551 error_at (clause_loc, "%<simdlen%> clause expression must "
13552 "be positive constant integer expression");
13553 t = NULL_TREE;
13556 parens.skip_until_found_close (parser);
13557 if (t == NULL_TREE || t == error_mark_node)
13558 return list;
13560 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13562 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13563 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13564 OMP_CLAUSE_CHAIN (c) = list;
13565 return c;
13568 /* OpenMP 4.5:
13569 vec:
13570 identifier [+/- integer]
13571 vec , identifier [+/- integer]
13574 static tree
13575 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13576 tree list)
13578 tree vec = NULL;
13579 if (c_parser_next_token_is_not (parser, CPP_NAME)
13580 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13582 c_parser_error (parser, "expected identifier");
13583 return list;
13586 while (c_parser_next_token_is (parser, CPP_NAME)
13587 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13589 tree t = lookup_name (c_parser_peek_token (parser)->value);
13590 tree addend = NULL;
13592 if (t == NULL_TREE)
13594 undeclared_variable (c_parser_peek_token (parser)->location,
13595 c_parser_peek_token (parser)->value);
13596 t = error_mark_node;
13599 c_parser_consume_token (parser);
13601 bool neg = false;
13602 if (c_parser_next_token_is (parser, CPP_MINUS))
13603 neg = true;
13604 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13606 addend = integer_zero_node;
13607 neg = false;
13608 goto add_to_vector;
13610 c_parser_consume_token (parser);
13612 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13614 c_parser_error (parser, "expected integer");
13615 return list;
13618 addend = c_parser_peek_token (parser)->value;
13619 if (TREE_CODE (addend) != INTEGER_CST)
13621 c_parser_error (parser, "expected integer");
13622 return list;
13624 c_parser_consume_token (parser);
13626 add_to_vector:
13627 if (t != error_mark_node)
13629 vec = tree_cons (addend, t, vec);
13630 if (neg)
13631 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13634 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13635 break;
13637 c_parser_consume_token (parser);
13640 if (vec == NULL_TREE)
13641 return list;
13643 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13644 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13645 OMP_CLAUSE_DECL (u) = nreverse (vec);
13646 OMP_CLAUSE_CHAIN (u) = list;
13647 return u;
13650 /* OpenMP 4.0:
13651 depend ( depend-kind: variable-list )
13653 depend-kind:
13654 in | out | inout
13656 OpenMP 4.5:
13657 depend ( source )
13659 depend ( sink : vec ) */
13661 static tree
13662 c_parser_omp_clause_depend (c_parser *parser, tree list)
13664 location_t clause_loc = c_parser_peek_token (parser)->location;
13665 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13666 tree nl, c;
13668 matching_parens parens;
13669 if (!parens.require_open (parser))
13670 return list;
13672 if (c_parser_next_token_is (parser, CPP_NAME))
13674 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13675 if (strcmp ("in", p) == 0)
13676 kind = OMP_CLAUSE_DEPEND_IN;
13677 else if (strcmp ("inout", p) == 0)
13678 kind = OMP_CLAUSE_DEPEND_INOUT;
13679 else if (strcmp ("out", p) == 0)
13680 kind = OMP_CLAUSE_DEPEND_OUT;
13681 else if (strcmp ("source", p) == 0)
13682 kind = OMP_CLAUSE_DEPEND_SOURCE;
13683 else if (strcmp ("sink", p) == 0)
13684 kind = OMP_CLAUSE_DEPEND_SINK;
13685 else
13686 goto invalid_kind;
13688 else
13689 goto invalid_kind;
13691 c_parser_consume_token (parser);
13693 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13695 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13696 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13697 OMP_CLAUSE_DECL (c) = NULL_TREE;
13698 OMP_CLAUSE_CHAIN (c) = list;
13699 parens.skip_until_found_close (parser);
13700 return c;
13703 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13704 goto resync_fail;
13706 if (kind == OMP_CLAUSE_DEPEND_SINK)
13707 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13708 else
13710 nl = c_parser_omp_variable_list (parser, clause_loc,
13711 OMP_CLAUSE_DEPEND, list);
13713 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13714 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13717 parens.skip_until_found_close (parser);
13718 return nl;
13720 invalid_kind:
13721 c_parser_error (parser, "invalid depend kind");
13722 resync_fail:
13723 parens.skip_until_found_close (parser);
13724 return list;
13727 /* OpenMP 4.0:
13728 map ( map-kind: variable-list )
13729 map ( variable-list )
13731 map-kind:
13732 alloc | to | from | tofrom
13734 OpenMP 4.5:
13735 map-kind:
13736 alloc | to | from | tofrom | release | delete
13738 map ( always [,] map-kind: variable-list ) */
13740 static tree
13741 c_parser_omp_clause_map (c_parser *parser, tree list)
13743 location_t clause_loc = c_parser_peek_token (parser)->location;
13744 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13745 int always = 0;
13746 enum c_id_kind always_id_kind = C_ID_NONE;
13747 location_t always_loc = UNKNOWN_LOCATION;
13748 tree always_id = NULL_TREE;
13749 tree nl, c;
13751 matching_parens parens;
13752 if (!parens.require_open (parser))
13753 return list;
13755 if (c_parser_next_token_is (parser, CPP_NAME))
13757 c_token *tok = c_parser_peek_token (parser);
13758 const char *p = IDENTIFIER_POINTER (tok->value);
13759 always_id_kind = tok->id_kind;
13760 always_loc = tok->location;
13761 always_id = tok->value;
13762 if (strcmp ("always", p) == 0)
13764 c_token *sectok = c_parser_peek_2nd_token (parser);
13765 if (sectok->type == CPP_COMMA)
13767 c_parser_consume_token (parser);
13768 c_parser_consume_token (parser);
13769 always = 2;
13771 else if (sectok->type == CPP_NAME)
13773 p = IDENTIFIER_POINTER (sectok->value);
13774 if (strcmp ("alloc", p) == 0
13775 || strcmp ("to", p) == 0
13776 || strcmp ("from", p) == 0
13777 || strcmp ("tofrom", p) == 0
13778 || strcmp ("release", p) == 0
13779 || strcmp ("delete", p) == 0)
13781 c_parser_consume_token (parser);
13782 always = 1;
13788 if (c_parser_next_token_is (parser, CPP_NAME)
13789 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13791 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13792 if (strcmp ("alloc", p) == 0)
13793 kind = GOMP_MAP_ALLOC;
13794 else if (strcmp ("to", p) == 0)
13795 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13796 else if (strcmp ("from", p) == 0)
13797 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13798 else if (strcmp ("tofrom", p) == 0)
13799 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13800 else if (strcmp ("release", p) == 0)
13801 kind = GOMP_MAP_RELEASE;
13802 else if (strcmp ("delete", p) == 0)
13803 kind = GOMP_MAP_DELETE;
13804 else
13806 c_parser_error (parser, "invalid map kind");
13807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13808 "expected %<)%>");
13809 return list;
13811 c_parser_consume_token (parser);
13812 c_parser_consume_token (parser);
13814 else if (always)
13816 if (always_id_kind != C_ID_ID)
13818 c_parser_error (parser, "expected identifier");
13819 parens.skip_until_found_close (parser);
13820 return list;
13823 tree t = lookup_name (always_id);
13824 if (t == NULL_TREE)
13826 undeclared_variable (always_loc, always_id);
13827 t = error_mark_node;
13829 if (t != error_mark_node)
13831 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13832 OMP_CLAUSE_DECL (u) = t;
13833 OMP_CLAUSE_CHAIN (u) = list;
13834 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13835 list = u;
13837 if (always == 1)
13839 parens.skip_until_found_close (parser);
13840 return list;
13844 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13846 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13847 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13849 parens.skip_until_found_close (parser);
13850 return nl;
13853 /* OpenMP 4.0:
13854 device ( expression ) */
13856 static tree
13857 c_parser_omp_clause_device (c_parser *parser, tree list)
13859 location_t clause_loc = c_parser_peek_token (parser)->location;
13860 matching_parens parens;
13861 if (parens.require_open (parser))
13863 location_t expr_loc = c_parser_peek_token (parser)->location;
13864 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13865 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13866 tree c, t = expr.value;
13867 t = c_fully_fold (t, false, NULL);
13869 parens.skip_until_found_close (parser);
13871 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13873 c_parser_error (parser, "expected integer expression");
13874 return list;
13877 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13879 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13880 OMP_CLAUSE_DEVICE_ID (c) = t;
13881 OMP_CLAUSE_CHAIN (c) = list;
13882 list = c;
13885 return list;
13888 /* OpenMP 4.0:
13889 dist_schedule ( static )
13890 dist_schedule ( static , expression ) */
13892 static tree
13893 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13895 tree c, t = NULL_TREE;
13896 location_t loc = c_parser_peek_token (parser)->location;
13898 matching_parens parens;
13899 if (!parens.require_open (parser))
13900 return list;
13902 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13904 c_parser_error (parser, "invalid dist_schedule kind");
13905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13906 "expected %<)%>");
13907 return list;
13910 c_parser_consume_token (parser);
13911 if (c_parser_next_token_is (parser, CPP_COMMA))
13913 c_parser_consume_token (parser);
13915 location_t expr_loc = c_parser_peek_token (parser)->location;
13916 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13917 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13918 t = expr.value;
13919 t = c_fully_fold (t, false, NULL);
13920 parens.skip_until_found_close (parser);
13922 else
13923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13924 "expected %<,%> or %<)%>");
13926 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13927 if (t == error_mark_node)
13928 return list;
13930 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13931 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13932 OMP_CLAUSE_CHAIN (c) = list;
13933 return c;
13936 /* OpenMP 4.0:
13937 proc_bind ( proc-bind-kind )
13939 proc-bind-kind:
13940 master | close | spread */
13942 static tree
13943 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13945 location_t clause_loc = c_parser_peek_token (parser)->location;
13946 enum omp_clause_proc_bind_kind kind;
13947 tree c;
13949 matching_parens parens;
13950 if (!parens.require_open (parser))
13951 return list;
13953 if (c_parser_next_token_is (parser, CPP_NAME))
13955 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13956 if (strcmp ("master", p) == 0)
13957 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13958 else if (strcmp ("close", p) == 0)
13959 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13960 else if (strcmp ("spread", p) == 0)
13961 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13962 else
13963 goto invalid_kind;
13965 else
13966 goto invalid_kind;
13968 c_parser_consume_token (parser);
13969 parens.skip_until_found_close (parser);
13970 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13971 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13972 OMP_CLAUSE_CHAIN (c) = list;
13973 return c;
13975 invalid_kind:
13976 c_parser_error (parser, "invalid proc_bind kind");
13977 parens.skip_until_found_close (parser);
13978 return list;
13981 /* OpenMP 4.0:
13982 to ( variable-list ) */
13984 static tree
13985 c_parser_omp_clause_to (c_parser *parser, tree list)
13987 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13990 /* OpenMP 4.0:
13991 from ( variable-list ) */
13993 static tree
13994 c_parser_omp_clause_from (c_parser *parser, tree list)
13996 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13999 /* OpenMP 4.0:
14000 uniform ( variable-list ) */
14002 static tree
14003 c_parser_omp_clause_uniform (c_parser *parser, tree list)
14005 /* The clauses location. */
14006 location_t loc = c_parser_peek_token (parser)->location;
14008 matching_parens parens;
14009 if (parens.require_open (parser))
14011 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
14012 list);
14013 parens.skip_until_found_close (parser);
14015 return list;
14018 /* Parse all OpenACC clauses. The set clauses allowed by the directive
14019 is a bitmask in MASK. Return the list of clauses found. */
14021 static tree
14022 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
14023 const char *where, bool finish_p = true)
14025 tree clauses = NULL;
14026 bool first = true;
14028 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14030 location_t here;
14031 pragma_omp_clause c_kind;
14032 const char *c_name;
14033 tree prev = clauses;
14035 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14036 c_parser_consume_token (parser);
14038 here = c_parser_peek_token (parser)->location;
14039 c_kind = c_parser_omp_clause_name (parser);
14041 switch (c_kind)
14043 case PRAGMA_OACC_CLAUSE_ASYNC:
14044 clauses = c_parser_oacc_clause_async (parser, clauses);
14045 c_name = "async";
14046 break;
14047 case PRAGMA_OACC_CLAUSE_AUTO:
14048 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
14049 clauses);
14050 c_name = "auto";
14051 break;
14052 case PRAGMA_OACC_CLAUSE_COLLAPSE:
14053 clauses = c_parser_omp_clause_collapse (parser, clauses);
14054 c_name = "collapse";
14055 break;
14056 case PRAGMA_OACC_CLAUSE_COPY:
14057 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14058 c_name = "copy";
14059 break;
14060 case PRAGMA_OACC_CLAUSE_COPYIN:
14061 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14062 c_name = "copyin";
14063 break;
14064 case PRAGMA_OACC_CLAUSE_COPYOUT:
14065 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14066 c_name = "copyout";
14067 break;
14068 case PRAGMA_OACC_CLAUSE_CREATE:
14069 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14070 c_name = "create";
14071 break;
14072 case PRAGMA_OACC_CLAUSE_DELETE:
14073 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14074 c_name = "delete";
14075 break;
14076 case PRAGMA_OMP_CLAUSE_DEFAULT:
14077 clauses = c_parser_omp_clause_default (parser, clauses, true);
14078 c_name = "default";
14079 break;
14080 case PRAGMA_OACC_CLAUSE_DEVICE:
14081 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14082 c_name = "device";
14083 break;
14084 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
14085 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
14086 c_name = "deviceptr";
14087 break;
14088 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
14089 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14090 c_name = "device_resident";
14091 break;
14092 case PRAGMA_OACC_CLAUSE_FINALIZE:
14093 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
14094 clauses);
14095 c_name = "finalize";
14096 break;
14097 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
14098 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14099 c_name = "firstprivate";
14100 break;
14101 case PRAGMA_OACC_CLAUSE_GANG:
14102 c_name = "gang";
14103 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
14104 c_name, clauses);
14105 break;
14106 case PRAGMA_OACC_CLAUSE_HOST:
14107 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14108 c_name = "host";
14109 break;
14110 case PRAGMA_OACC_CLAUSE_IF:
14111 clauses = c_parser_omp_clause_if (parser, clauses, false);
14112 c_name = "if";
14113 break;
14114 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
14115 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_IF_PRESENT,
14116 clauses);
14117 c_name = "if_present";
14118 break;
14119 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
14120 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
14121 clauses);
14122 c_name = "independent";
14123 break;
14124 case PRAGMA_OACC_CLAUSE_LINK:
14125 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14126 c_name = "link";
14127 break;
14128 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
14129 clauses = c_parser_oacc_single_int_clause (parser,
14130 OMP_CLAUSE_NUM_GANGS,
14131 clauses);
14132 c_name = "num_gangs";
14133 break;
14134 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
14135 clauses = c_parser_oacc_single_int_clause (parser,
14136 OMP_CLAUSE_NUM_WORKERS,
14137 clauses);
14138 c_name = "num_workers";
14139 break;
14140 case PRAGMA_OACC_CLAUSE_PRESENT:
14141 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14142 c_name = "present";
14143 break;
14144 case PRAGMA_OACC_CLAUSE_PRIVATE:
14145 clauses = c_parser_omp_clause_private (parser, clauses);
14146 c_name = "private";
14147 break;
14148 case PRAGMA_OACC_CLAUSE_REDUCTION:
14149 clauses = c_parser_omp_clause_reduction (parser, clauses);
14150 c_name = "reduction";
14151 break;
14152 case PRAGMA_OACC_CLAUSE_SEQ:
14153 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14154 clauses);
14155 c_name = "seq";
14156 break;
14157 case PRAGMA_OACC_CLAUSE_TILE:
14158 clauses = c_parser_oacc_clause_tile (parser, clauses);
14159 c_name = "tile";
14160 break;
14161 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14162 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14163 c_name = "use_device";
14164 break;
14165 case PRAGMA_OACC_CLAUSE_VECTOR:
14166 c_name = "vector";
14167 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14168 c_name, clauses);
14169 break;
14170 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14171 clauses = c_parser_oacc_single_int_clause (parser,
14172 OMP_CLAUSE_VECTOR_LENGTH,
14173 clauses);
14174 c_name = "vector_length";
14175 break;
14176 case PRAGMA_OACC_CLAUSE_WAIT:
14177 clauses = c_parser_oacc_clause_wait (parser, clauses);
14178 c_name = "wait";
14179 break;
14180 case PRAGMA_OACC_CLAUSE_WORKER:
14181 c_name = "worker";
14182 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14183 c_name, clauses);
14184 break;
14185 default:
14186 c_parser_error (parser, "expected %<#pragma acc%> clause");
14187 goto saw_error;
14190 first = false;
14192 if (((mask >> c_kind) & 1) == 0)
14194 /* Remove the invalid clause(s) from the list to avoid
14195 confusing the rest of the compiler. */
14196 clauses = prev;
14197 error_at (here, "%qs is not valid for %qs", c_name, where);
14201 saw_error:
14202 c_parser_skip_to_pragma_eol (parser);
14204 if (finish_p)
14205 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14207 return clauses;
14210 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14211 is a bitmask in MASK. Return the list of clauses found. */
14213 static tree
14214 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14215 const char *where, bool finish_p = true)
14217 tree clauses = NULL;
14218 bool first = true;
14220 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14222 location_t here;
14223 pragma_omp_clause c_kind;
14224 const char *c_name;
14225 tree prev = clauses;
14227 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14228 c_parser_consume_token (parser);
14230 here = c_parser_peek_token (parser)->location;
14231 c_kind = c_parser_omp_clause_name (parser);
14233 switch (c_kind)
14235 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14236 clauses = c_parser_omp_clause_collapse (parser, clauses);
14237 c_name = "collapse";
14238 break;
14239 case PRAGMA_OMP_CLAUSE_COPYIN:
14240 clauses = c_parser_omp_clause_copyin (parser, clauses);
14241 c_name = "copyin";
14242 break;
14243 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14244 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14245 c_name = "copyprivate";
14246 break;
14247 case PRAGMA_OMP_CLAUSE_DEFAULT:
14248 clauses = c_parser_omp_clause_default (parser, clauses, false);
14249 c_name = "default";
14250 break;
14251 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14252 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14253 c_name = "firstprivate";
14254 break;
14255 case PRAGMA_OMP_CLAUSE_FINAL:
14256 clauses = c_parser_omp_clause_final (parser, clauses);
14257 c_name = "final";
14258 break;
14259 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14260 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14261 c_name = "grainsize";
14262 break;
14263 case PRAGMA_OMP_CLAUSE_HINT:
14264 clauses = c_parser_omp_clause_hint (parser, clauses);
14265 c_name = "hint";
14266 break;
14267 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14268 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14269 c_name = "defaultmap";
14270 break;
14271 case PRAGMA_OMP_CLAUSE_IF:
14272 clauses = c_parser_omp_clause_if (parser, clauses, true);
14273 c_name = "if";
14274 break;
14275 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14276 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14277 c_name = "lastprivate";
14278 break;
14279 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14280 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14281 c_name = "mergeable";
14282 break;
14283 case PRAGMA_OMP_CLAUSE_NOWAIT:
14284 clauses = c_parser_omp_clause_nowait (parser, clauses);
14285 c_name = "nowait";
14286 break;
14287 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14288 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14289 c_name = "num_tasks";
14290 break;
14291 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14292 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14293 c_name = "num_threads";
14294 break;
14295 case PRAGMA_OMP_CLAUSE_ORDERED:
14296 clauses = c_parser_omp_clause_ordered (parser, clauses);
14297 c_name = "ordered";
14298 break;
14299 case PRAGMA_OMP_CLAUSE_PRIORITY:
14300 clauses = c_parser_omp_clause_priority (parser, clauses);
14301 c_name = "priority";
14302 break;
14303 case PRAGMA_OMP_CLAUSE_PRIVATE:
14304 clauses = c_parser_omp_clause_private (parser, clauses);
14305 c_name = "private";
14306 break;
14307 case PRAGMA_OMP_CLAUSE_REDUCTION:
14308 clauses = c_parser_omp_clause_reduction (parser, clauses);
14309 c_name = "reduction";
14310 break;
14311 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14312 clauses = c_parser_omp_clause_schedule (parser, clauses);
14313 c_name = "schedule";
14314 break;
14315 case PRAGMA_OMP_CLAUSE_SHARED:
14316 clauses = c_parser_omp_clause_shared (parser, clauses);
14317 c_name = "shared";
14318 break;
14319 case PRAGMA_OMP_CLAUSE_UNTIED:
14320 clauses = c_parser_omp_clause_untied (parser, clauses);
14321 c_name = "untied";
14322 break;
14323 case PRAGMA_OMP_CLAUSE_INBRANCH:
14324 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14325 clauses);
14326 c_name = "inbranch";
14327 break;
14328 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14329 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14330 clauses);
14331 c_name = "notinbranch";
14332 break;
14333 case PRAGMA_OMP_CLAUSE_PARALLEL:
14334 clauses
14335 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14336 clauses);
14337 c_name = "parallel";
14338 if (!first)
14340 clause_not_first:
14341 error_at (here, "%qs must be the first clause of %qs",
14342 c_name, where);
14343 clauses = prev;
14345 break;
14346 case PRAGMA_OMP_CLAUSE_FOR:
14347 clauses
14348 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14349 clauses);
14350 c_name = "for";
14351 if (!first)
14352 goto clause_not_first;
14353 break;
14354 case PRAGMA_OMP_CLAUSE_SECTIONS:
14355 clauses
14356 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14357 clauses);
14358 c_name = "sections";
14359 if (!first)
14360 goto clause_not_first;
14361 break;
14362 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14363 clauses
14364 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14365 clauses);
14366 c_name = "taskgroup";
14367 if (!first)
14368 goto clause_not_first;
14369 break;
14370 case PRAGMA_OMP_CLAUSE_LINK:
14371 clauses
14372 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14373 c_name = "link";
14374 break;
14375 case PRAGMA_OMP_CLAUSE_TO:
14376 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14377 clauses
14378 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14379 clauses);
14380 else
14381 clauses = c_parser_omp_clause_to (parser, clauses);
14382 c_name = "to";
14383 break;
14384 case PRAGMA_OMP_CLAUSE_FROM:
14385 clauses = c_parser_omp_clause_from (parser, clauses);
14386 c_name = "from";
14387 break;
14388 case PRAGMA_OMP_CLAUSE_UNIFORM:
14389 clauses = c_parser_omp_clause_uniform (parser, clauses);
14390 c_name = "uniform";
14391 break;
14392 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14393 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14394 c_name = "num_teams";
14395 break;
14396 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14397 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14398 c_name = "thread_limit";
14399 break;
14400 case PRAGMA_OMP_CLAUSE_ALIGNED:
14401 clauses = c_parser_omp_clause_aligned (parser, clauses);
14402 c_name = "aligned";
14403 break;
14404 case PRAGMA_OMP_CLAUSE_LINEAR:
14405 clauses = c_parser_omp_clause_linear (parser, clauses);
14406 c_name = "linear";
14407 break;
14408 case PRAGMA_OMP_CLAUSE_DEPEND:
14409 clauses = c_parser_omp_clause_depend (parser, clauses);
14410 c_name = "depend";
14411 break;
14412 case PRAGMA_OMP_CLAUSE_MAP:
14413 clauses = c_parser_omp_clause_map (parser, clauses);
14414 c_name = "map";
14415 break;
14416 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14417 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14418 c_name = "use_device_ptr";
14419 break;
14420 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14421 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14422 c_name = "is_device_ptr";
14423 break;
14424 case PRAGMA_OMP_CLAUSE_DEVICE:
14425 clauses = c_parser_omp_clause_device (parser, clauses);
14426 c_name = "device";
14427 break;
14428 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14429 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14430 c_name = "dist_schedule";
14431 break;
14432 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14433 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14434 c_name = "proc_bind";
14435 break;
14436 case PRAGMA_OMP_CLAUSE_SAFELEN:
14437 clauses = c_parser_omp_clause_safelen (parser, clauses);
14438 c_name = "safelen";
14439 break;
14440 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14441 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14442 c_name = "simdlen";
14443 break;
14444 case PRAGMA_OMP_CLAUSE_NOGROUP:
14445 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14446 c_name = "nogroup";
14447 break;
14448 case PRAGMA_OMP_CLAUSE_THREADS:
14449 clauses
14450 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14451 clauses);
14452 c_name = "threads";
14453 break;
14454 case PRAGMA_OMP_CLAUSE_SIMD:
14455 clauses
14456 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14457 clauses);
14458 c_name = "simd";
14459 break;
14460 default:
14461 c_parser_error (parser, "expected %<#pragma omp%> clause");
14462 goto saw_error;
14465 first = false;
14467 if (((mask >> c_kind) & 1) == 0)
14469 /* Remove the invalid clause(s) from the list to avoid
14470 confusing the rest of the compiler. */
14471 clauses = prev;
14472 error_at (here, "%qs is not valid for %qs", c_name, where);
14476 saw_error:
14477 c_parser_skip_to_pragma_eol (parser);
14479 if (finish_p)
14481 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14482 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14483 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14486 return clauses;
14489 /* OpenACC 2.0, OpenMP 2.5:
14490 structured-block:
14491 statement
14493 In practice, we're also interested in adding the statement to an
14494 outer node. So it is convenient if we work around the fact that
14495 c_parser_statement calls add_stmt. */
14497 static tree
14498 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14500 tree stmt = push_stmt_list ();
14501 c_parser_statement (parser, if_p);
14502 return pop_stmt_list (stmt);
14505 /* OpenACC 2.0:
14506 # pragma acc cache (variable-list) new-line
14508 LOC is the location of the #pragma token.
14511 static tree
14512 c_parser_oacc_cache (location_t loc, c_parser *parser)
14514 tree stmt, clauses;
14516 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14517 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14519 c_parser_skip_to_pragma_eol (parser);
14521 stmt = make_node (OACC_CACHE);
14522 TREE_TYPE (stmt) = void_type_node;
14523 OACC_CACHE_CLAUSES (stmt) = clauses;
14524 SET_EXPR_LOCATION (stmt, loc);
14525 add_stmt (stmt);
14527 return stmt;
14530 /* OpenACC 2.0:
14531 # pragma acc data oacc-data-clause[optseq] new-line
14532 structured-block
14534 LOC is the location of the #pragma token.
14537 #define OACC_DATA_CLAUSE_MASK \
14538 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
14546 static tree
14547 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14549 tree stmt, clauses, block;
14551 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14552 "#pragma acc data");
14554 block = c_begin_omp_parallel ();
14555 add_stmt (c_parser_omp_structured_block (parser, if_p));
14557 stmt = c_finish_oacc_data (loc, clauses, block);
14559 return stmt;
14562 /* OpenACC 2.0:
14563 # pragma acc declare oacc-data-clause[optseq] new-line
14566 #define OACC_DECLARE_CLAUSE_MASK \
14567 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
14576 static void
14577 c_parser_oacc_declare (c_parser *parser)
14579 location_t pragma_loc = c_parser_peek_token (parser)->location;
14580 tree clauses, stmt, t, decl;
14582 bool error = false;
14584 c_parser_consume_pragma (parser);
14586 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14587 "#pragma acc declare");
14588 if (!clauses)
14590 error_at (pragma_loc,
14591 "no valid clauses specified in %<#pragma acc declare%>");
14592 return;
14595 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14597 location_t loc = OMP_CLAUSE_LOCATION (t);
14598 decl = OMP_CLAUSE_DECL (t);
14599 if (!DECL_P (decl))
14601 error_at (loc, "array section in %<#pragma acc declare%>");
14602 error = true;
14603 continue;
14606 switch (OMP_CLAUSE_MAP_KIND (t))
14608 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14609 case GOMP_MAP_ALLOC:
14610 case GOMP_MAP_TO:
14611 case GOMP_MAP_FORCE_DEVICEPTR:
14612 case GOMP_MAP_DEVICE_RESIDENT:
14613 break;
14615 case GOMP_MAP_LINK:
14616 if (!global_bindings_p ()
14617 && (TREE_STATIC (decl)
14618 || !DECL_EXTERNAL (decl)))
14620 error_at (loc,
14621 "%qD must be a global variable in "
14622 "%<#pragma acc declare link%>",
14623 decl);
14624 error = true;
14625 continue;
14627 break;
14629 default:
14630 if (global_bindings_p ())
14632 error_at (loc, "invalid OpenACC clause at file scope");
14633 error = true;
14634 continue;
14636 if (DECL_EXTERNAL (decl))
14638 error_at (loc,
14639 "invalid use of %<extern%> variable %qD "
14640 "in %<#pragma acc declare%>", decl);
14641 error = true;
14642 continue;
14644 else if (TREE_PUBLIC (decl))
14646 error_at (loc,
14647 "invalid use of %<global%> variable %qD "
14648 "in %<#pragma acc declare%>", decl);
14649 error = true;
14650 continue;
14652 break;
14655 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14656 || lookup_attribute ("omp declare target link",
14657 DECL_ATTRIBUTES (decl)))
14659 error_at (loc, "variable %qD used more than once with "
14660 "%<#pragma acc declare%>", decl);
14661 error = true;
14662 continue;
14665 if (!error)
14667 tree id;
14669 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14670 id = get_identifier ("omp declare target link");
14671 else
14672 id = get_identifier ("omp declare target");
14674 DECL_ATTRIBUTES (decl)
14675 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14677 if (global_bindings_p ())
14679 symtab_node *node = symtab_node::get (decl);
14680 if (node != NULL)
14682 node->offloadable = 1;
14683 if (ENABLE_OFFLOADING)
14685 g->have_offload = true;
14686 if (is_a <varpool_node *> (node))
14687 vec_safe_push (offload_vars, decl);
14694 if (error || global_bindings_p ())
14695 return;
14697 stmt = make_node (OACC_DECLARE);
14698 TREE_TYPE (stmt) = void_type_node;
14699 OACC_DECLARE_CLAUSES (stmt) = clauses;
14700 SET_EXPR_LOCATION (stmt, pragma_loc);
14702 add_stmt (stmt);
14704 return;
14707 /* OpenACC 2.0:
14708 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14712 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14715 LOC is the location of the #pragma token.
14718 #define OACC_ENTER_DATA_CLAUSE_MASK \
14719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14725 #define OACC_EXIT_DATA_CLAUSE_MASK \
14726 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
14731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14733 static void
14734 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14736 location_t loc = c_parser_peek_token (parser)->location;
14737 tree clauses, stmt;
14738 const char *p = "";
14740 c_parser_consume_pragma (parser);
14742 if (c_parser_next_token_is (parser, CPP_NAME))
14744 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14745 c_parser_consume_token (parser);
14748 if (strcmp (p, "data") != 0)
14750 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14751 enter ? "enter" : "exit");
14752 parser->error = true;
14753 c_parser_skip_to_pragma_eol (parser);
14754 return;
14757 if (enter)
14758 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14759 "#pragma acc enter data");
14760 else
14761 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14762 "#pragma acc exit data");
14764 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14766 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14767 enter ? "enter" : "exit");
14768 return;
14771 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14772 TREE_TYPE (stmt) = void_type_node;
14773 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14774 SET_EXPR_LOCATION (stmt, loc);
14775 add_stmt (stmt);
14779 /* OpenACC 2.0:
14780 # pragma acc host_data oacc-data-clause[optseq] new-line
14781 structured-block
14784 #define OACC_HOST_DATA_CLAUSE_MASK \
14785 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14787 static tree
14788 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14790 tree stmt, clauses, block;
14792 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14793 "#pragma acc host_data");
14795 block = c_begin_omp_parallel ();
14796 add_stmt (c_parser_omp_structured_block (parser, if_p));
14797 stmt = c_finish_oacc_host_data (loc, clauses, block);
14798 return stmt;
14802 /* OpenACC 2.0:
14804 # pragma acc loop oacc-loop-clause[optseq] new-line
14805 structured-block
14807 LOC is the location of the #pragma token.
14810 #define OACC_LOOP_CLAUSE_MASK \
14811 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14821 static tree
14822 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14823 omp_clause_mask mask, tree *cclauses, bool *if_p)
14825 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14827 strcat (p_name, " loop");
14828 mask |= OACC_LOOP_CLAUSE_MASK;
14830 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14831 cclauses == NULL);
14832 if (cclauses)
14834 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14835 if (*cclauses)
14836 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14837 if (clauses)
14838 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14841 tree block = c_begin_compound_stmt (true);
14842 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14843 if_p);
14844 block = c_end_compound_stmt (loc, block, true);
14845 add_stmt (block);
14847 return stmt;
14850 /* OpenACC 2.0:
14851 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14852 structured-block
14856 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14857 structured-block
14859 LOC is the location of the #pragma token.
14862 #define OACC_KERNELS_CLAUSE_MASK \
14863 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14877 #define OACC_PARALLEL_CLAUSE_MASK \
14878 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14895 static tree
14896 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14897 enum pragma_kind p_kind, char *p_name,
14898 bool *if_p)
14900 omp_clause_mask mask;
14901 enum tree_code code;
14902 switch (p_kind)
14904 case PRAGMA_OACC_KERNELS:
14905 strcat (p_name, " kernels");
14906 mask = OACC_KERNELS_CLAUSE_MASK;
14907 code = OACC_KERNELS;
14908 break;
14909 case PRAGMA_OACC_PARALLEL:
14910 strcat (p_name, " parallel");
14911 mask = OACC_PARALLEL_CLAUSE_MASK;
14912 code = OACC_PARALLEL;
14913 break;
14914 default:
14915 gcc_unreachable ();
14918 if (c_parser_next_token_is (parser, CPP_NAME))
14920 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14921 if (strcmp (p, "loop") == 0)
14923 c_parser_consume_token (parser);
14924 tree block = c_begin_omp_parallel ();
14925 tree clauses;
14926 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14927 return c_finish_omp_construct (loc, code, block, clauses);
14931 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14933 tree block = c_begin_omp_parallel ();
14934 add_stmt (c_parser_omp_structured_block (parser, if_p));
14936 return c_finish_omp_construct (loc, code, block, clauses);
14939 /* OpenACC 2.0:
14940 # pragma acc routine oacc-routine-clause[optseq] new-line
14941 function-definition
14943 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14946 #define OACC_ROUTINE_CLAUSE_MASK \
14947 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14952 /* Parse an OpenACC routine directive. For named directives, we apply
14953 immediately to the named function. For unnamed ones we then parse
14954 a declaration or definition, which must be for a function. */
14956 static void
14957 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14959 gcc_checking_assert (context == pragma_external);
14961 oacc_routine_data data;
14962 data.error_seen = false;
14963 data.fndecl_seen = false;
14964 data.clauses = NULL_TREE;
14965 data.loc = c_parser_peek_token (parser)->location;
14967 c_parser_consume_pragma (parser);
14969 /* Look for optional '( name )'. */
14970 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14972 c_parser_consume_token (parser); /* '(' */
14974 tree decl = NULL_TREE;
14975 c_token *name_token = c_parser_peek_token (parser);
14976 location_t name_loc = name_token->location;
14977 if (name_token->type == CPP_NAME
14978 && (name_token->id_kind == C_ID_ID
14979 || name_token->id_kind == C_ID_TYPENAME))
14981 decl = lookup_name (name_token->value);
14982 if (!decl)
14983 error_at (name_loc,
14984 "%qE has not been declared", name_token->value);
14985 c_parser_consume_token (parser);
14987 else
14988 c_parser_error (parser, "expected function name");
14990 if (!decl
14991 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14993 c_parser_skip_to_pragma_eol (parser, false);
14994 return;
14997 data.clauses
14998 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14999 "#pragma acc routine");
15001 if (TREE_CODE (decl) != FUNCTION_DECL)
15003 error_at (name_loc, "%qD does not refer to a function", decl);
15004 return;
15007 c_finish_oacc_routine (&data, decl, false);
15009 else /* No optional '( name )'. */
15011 data.clauses
15012 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15013 "#pragma acc routine");
15015 /* Emit a helpful diagnostic if there's another pragma following this
15016 one. Also don't allow a static assertion declaration, as in the
15017 following we'll just parse a *single* "declaration or function
15018 definition", and the static assertion counts an one. */
15019 if (c_parser_next_token_is (parser, CPP_PRAGMA)
15020 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
15022 error_at (data.loc,
15023 "%<#pragma acc routine%> not immediately followed by"
15024 " function declaration or definition");
15025 /* ..., and then just keep going. */
15026 return;
15029 /* We only have to consider the pragma_external case here. */
15030 if (c_parser_next_token_is (parser, CPP_KEYWORD)
15031 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
15033 int ext = disable_extension_diagnostics ();
15035 c_parser_consume_token (parser);
15036 while (c_parser_next_token_is (parser, CPP_KEYWORD)
15037 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
15038 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15039 NULL, vNULL, &data);
15040 restore_extension_diagnostics (ext);
15042 else
15043 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15044 NULL, vNULL, &data);
15048 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
15049 IS_DEFN is true if we're applying it to the definition. */
15051 static void
15052 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
15053 bool is_defn)
15055 /* Keep going if we're in error reporting mode. */
15056 if (data->error_seen
15057 || fndecl == error_mark_node)
15058 return;
15060 if (data->fndecl_seen)
15062 error_at (data->loc,
15063 "%<#pragma acc routine%> not immediately followed by"
15064 " a single function declaration or definition");
15065 data->error_seen = true;
15066 return;
15068 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
15070 error_at (data->loc,
15071 "%<#pragma acc routine%> not immediately followed by"
15072 " function declaration or definition");
15073 data->error_seen = true;
15074 return;
15077 if (oacc_get_fn_attrib (fndecl))
15079 error_at (data->loc,
15080 "%<#pragma acc routine%> already applied to %qD", fndecl);
15081 data->error_seen = true;
15082 return;
15085 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
15087 error_at (data->loc,
15088 TREE_USED (fndecl)
15089 ? G_("%<#pragma acc routine%> must be applied before use")
15090 : G_("%<#pragma acc routine%> must be applied before "
15091 "definition"));
15092 data->error_seen = true;
15093 return;
15096 /* Process the routine's dimension clauses. */
15097 tree dims = oacc_build_routine_dims (data->clauses);
15098 oacc_replace_fn_attrib (fndecl, dims);
15100 /* Add an "omp declare target" attribute. */
15101 DECL_ATTRIBUTES (fndecl)
15102 = tree_cons (get_identifier ("omp declare target"),
15103 NULL_TREE, DECL_ATTRIBUTES (fndecl));
15105 /* Remember that we've used this "#pragma acc routine". */
15106 data->fndecl_seen = true;
15109 /* OpenACC 2.0:
15110 # pragma acc update oacc-update-clause[optseq] new-line
15113 #define OACC_UPDATE_CLAUSE_MASK \
15114 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
15116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
15117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
15119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15121 static void
15122 c_parser_oacc_update (c_parser *parser)
15124 location_t loc = c_parser_peek_token (parser)->location;
15126 c_parser_consume_pragma (parser);
15128 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15129 "#pragma acc update");
15130 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15132 error_at (loc,
15133 "%<#pragma acc update%> must contain at least one "
15134 "%<device%> or %<host%> or %<self%> clause");
15135 return;
15138 if (parser->error)
15139 return;
15141 tree stmt = make_node (OACC_UPDATE);
15142 TREE_TYPE (stmt) = void_type_node;
15143 OACC_UPDATE_CLAUSES (stmt) = clauses;
15144 SET_EXPR_LOCATION (stmt, loc);
15145 add_stmt (stmt);
15148 /* OpenACC 2.0:
15149 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15151 LOC is the location of the #pragma token.
15154 #define OACC_WAIT_CLAUSE_MASK \
15155 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15157 static tree
15158 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15160 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15162 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15163 list = c_parser_oacc_wait_list (parser, loc, list);
15165 strcpy (p_name, " wait");
15166 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15167 stmt = c_finish_oacc_wait (loc, list, clauses);
15168 add_stmt (stmt);
15170 return stmt;
15173 /* OpenMP 2.5:
15174 # pragma omp atomic new-line
15175 expression-stmt
15177 expression-stmt:
15178 x binop= expr | x++ | ++x | x-- | --x
15179 binop:
15180 +, *, -, /, &, ^, |, <<, >>
15182 where x is an lvalue expression with scalar type.
15184 OpenMP 3.1:
15185 # pragma omp atomic new-line
15186 update-stmt
15188 # pragma omp atomic read new-line
15189 read-stmt
15191 # pragma omp atomic write new-line
15192 write-stmt
15194 # pragma omp atomic update new-line
15195 update-stmt
15197 # pragma omp atomic capture new-line
15198 capture-stmt
15200 # pragma omp atomic capture new-line
15201 capture-block
15203 read-stmt:
15204 v = x
15205 write-stmt:
15206 x = expr
15207 update-stmt:
15208 expression-stmt | x = x binop expr
15209 capture-stmt:
15210 v = expression-stmt
15211 capture-block:
15212 { v = x; update-stmt; } | { update-stmt; v = x; }
15214 OpenMP 4.0:
15215 update-stmt:
15216 expression-stmt | x = x binop expr | x = expr binop x
15217 capture-stmt:
15218 v = update-stmt
15219 capture-block:
15220 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15222 where x and v are lvalue expressions with scalar type.
15224 LOC is the location of the #pragma token. */
15226 static void
15227 c_parser_omp_atomic (location_t loc, c_parser *parser)
15229 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15230 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15231 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15232 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15233 struct c_expr expr;
15234 location_t eloc;
15235 bool structured_block = false;
15236 bool swapped = false;
15237 bool seq_cst = false;
15238 bool non_lvalue_p;
15240 if (c_parser_next_token_is (parser, CPP_NAME))
15242 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15243 if (!strcmp (p, "seq_cst"))
15245 seq_cst = true;
15246 c_parser_consume_token (parser);
15247 if (c_parser_next_token_is (parser, CPP_COMMA)
15248 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15249 c_parser_consume_token (parser);
15252 if (c_parser_next_token_is (parser, CPP_NAME))
15254 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15256 if (!strcmp (p, "read"))
15257 code = OMP_ATOMIC_READ;
15258 else if (!strcmp (p, "write"))
15259 code = NOP_EXPR;
15260 else if (!strcmp (p, "update"))
15261 code = OMP_ATOMIC;
15262 else if (!strcmp (p, "capture"))
15263 code = OMP_ATOMIC_CAPTURE_NEW;
15264 else
15265 p = NULL;
15266 if (p)
15267 c_parser_consume_token (parser);
15269 if (!seq_cst)
15271 if (c_parser_next_token_is (parser, CPP_COMMA)
15272 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15273 c_parser_consume_token (parser);
15275 if (c_parser_next_token_is (parser, CPP_NAME))
15277 const char *p
15278 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15279 if (!strcmp (p, "seq_cst"))
15281 seq_cst = true;
15282 c_parser_consume_token (parser);
15286 c_parser_skip_to_pragma_eol (parser);
15288 switch (code)
15290 case OMP_ATOMIC_READ:
15291 case NOP_EXPR: /* atomic write */
15292 v = c_parser_cast_expression (parser, NULL).value;
15293 non_lvalue_p = !lvalue_p (v);
15294 v = c_fully_fold (v, false, NULL, true);
15295 if (v == error_mark_node)
15296 goto saw_error;
15297 if (non_lvalue_p)
15298 v = non_lvalue (v);
15299 loc = c_parser_peek_token (parser)->location;
15300 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15301 goto saw_error;
15302 if (code == NOP_EXPR)
15304 lhs = c_parser_expression (parser).value;
15305 lhs = c_fully_fold (lhs, false, NULL);
15306 if (lhs == error_mark_node)
15307 goto saw_error;
15309 else
15311 lhs = c_parser_cast_expression (parser, NULL).value;
15312 non_lvalue_p = !lvalue_p (lhs);
15313 lhs = c_fully_fold (lhs, false, NULL, true);
15314 if (lhs == error_mark_node)
15315 goto saw_error;
15316 if (non_lvalue_p)
15317 lhs = non_lvalue (lhs);
15319 if (code == NOP_EXPR)
15321 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15322 opcode. */
15323 code = OMP_ATOMIC;
15324 rhs = lhs;
15325 lhs = v;
15326 v = NULL_TREE;
15328 goto done;
15329 case OMP_ATOMIC_CAPTURE_NEW:
15330 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15332 c_parser_consume_token (parser);
15333 structured_block = true;
15335 else
15337 v = c_parser_cast_expression (parser, NULL).value;
15338 non_lvalue_p = !lvalue_p (v);
15339 v = c_fully_fold (v, false, NULL, true);
15340 if (v == error_mark_node)
15341 goto saw_error;
15342 if (non_lvalue_p)
15343 v = non_lvalue (v);
15344 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15345 goto saw_error;
15347 break;
15348 default:
15349 break;
15352 /* For structured_block case we don't know yet whether
15353 old or new x should be captured. */
15354 restart:
15355 eloc = c_parser_peek_token (parser)->location;
15356 expr = c_parser_cast_expression (parser, NULL);
15357 lhs = expr.value;
15358 expr = default_function_array_conversion (eloc, expr);
15359 unfolded_lhs = expr.value;
15360 lhs = c_fully_fold (lhs, false, NULL, true);
15361 orig_lhs = lhs;
15362 switch (TREE_CODE (lhs))
15364 case ERROR_MARK:
15365 saw_error:
15366 c_parser_skip_to_end_of_block_or_statement (parser);
15367 if (structured_block)
15369 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15370 c_parser_consume_token (parser);
15371 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15373 c_parser_skip_to_end_of_block_or_statement (parser);
15374 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15375 c_parser_consume_token (parser);
15378 return;
15380 case POSTINCREMENT_EXPR:
15381 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15382 code = OMP_ATOMIC_CAPTURE_OLD;
15383 /* FALLTHROUGH */
15384 case PREINCREMENT_EXPR:
15385 lhs = TREE_OPERAND (lhs, 0);
15386 unfolded_lhs = NULL_TREE;
15387 opcode = PLUS_EXPR;
15388 rhs = integer_one_node;
15389 break;
15391 case POSTDECREMENT_EXPR:
15392 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15393 code = OMP_ATOMIC_CAPTURE_OLD;
15394 /* FALLTHROUGH */
15395 case PREDECREMENT_EXPR:
15396 lhs = TREE_OPERAND (lhs, 0);
15397 unfolded_lhs = NULL_TREE;
15398 opcode = MINUS_EXPR;
15399 rhs = integer_one_node;
15400 break;
15402 case COMPOUND_EXPR:
15403 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15404 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15405 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15406 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15407 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15408 (TREE_OPERAND (lhs, 1), 0), 0)))
15409 == BOOLEAN_TYPE)
15410 /* Undo effects of boolean_increment for post {in,de}crement. */
15411 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15412 /* FALLTHRU */
15413 case MODIFY_EXPR:
15414 if (TREE_CODE (lhs) == MODIFY_EXPR
15415 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15417 /* Undo effects of boolean_increment. */
15418 if (integer_onep (TREE_OPERAND (lhs, 1)))
15420 /* This is pre or post increment. */
15421 rhs = TREE_OPERAND (lhs, 1);
15422 lhs = TREE_OPERAND (lhs, 0);
15423 unfolded_lhs = NULL_TREE;
15424 opcode = NOP_EXPR;
15425 if (code == OMP_ATOMIC_CAPTURE_NEW
15426 && !structured_block
15427 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15428 code = OMP_ATOMIC_CAPTURE_OLD;
15429 break;
15431 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15432 && TREE_OPERAND (lhs, 0)
15433 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15435 /* This is pre or post decrement. */
15436 rhs = TREE_OPERAND (lhs, 1);
15437 lhs = TREE_OPERAND (lhs, 0);
15438 unfolded_lhs = NULL_TREE;
15439 opcode = NOP_EXPR;
15440 if (code == OMP_ATOMIC_CAPTURE_NEW
15441 && !structured_block
15442 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15443 code = OMP_ATOMIC_CAPTURE_OLD;
15444 break;
15447 /* FALLTHRU */
15448 default:
15449 if (!lvalue_p (unfolded_lhs))
15450 lhs = non_lvalue (lhs);
15451 switch (c_parser_peek_token (parser)->type)
15453 case CPP_MULT_EQ:
15454 opcode = MULT_EXPR;
15455 break;
15456 case CPP_DIV_EQ:
15457 opcode = TRUNC_DIV_EXPR;
15458 break;
15459 case CPP_PLUS_EQ:
15460 opcode = PLUS_EXPR;
15461 break;
15462 case CPP_MINUS_EQ:
15463 opcode = MINUS_EXPR;
15464 break;
15465 case CPP_LSHIFT_EQ:
15466 opcode = LSHIFT_EXPR;
15467 break;
15468 case CPP_RSHIFT_EQ:
15469 opcode = RSHIFT_EXPR;
15470 break;
15471 case CPP_AND_EQ:
15472 opcode = BIT_AND_EXPR;
15473 break;
15474 case CPP_OR_EQ:
15475 opcode = BIT_IOR_EXPR;
15476 break;
15477 case CPP_XOR_EQ:
15478 opcode = BIT_XOR_EXPR;
15479 break;
15480 case CPP_EQ:
15481 c_parser_consume_token (parser);
15482 eloc = c_parser_peek_token (parser)->location;
15483 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15484 rhs1 = expr.value;
15485 switch (TREE_CODE (rhs1))
15487 case MULT_EXPR:
15488 case TRUNC_DIV_EXPR:
15489 case RDIV_EXPR:
15490 case PLUS_EXPR:
15491 case MINUS_EXPR:
15492 case LSHIFT_EXPR:
15493 case RSHIFT_EXPR:
15494 case BIT_AND_EXPR:
15495 case BIT_IOR_EXPR:
15496 case BIT_XOR_EXPR:
15497 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15499 opcode = TREE_CODE (rhs1);
15500 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15501 true);
15502 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15503 true);
15504 goto stmt_done;
15506 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15508 opcode = TREE_CODE (rhs1);
15509 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15510 true);
15511 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15512 true);
15513 swapped = !commutative_tree_code (opcode);
15514 goto stmt_done;
15516 break;
15517 case ERROR_MARK:
15518 goto saw_error;
15519 default:
15520 break;
15522 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15524 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15526 code = OMP_ATOMIC_CAPTURE_OLD;
15527 v = lhs;
15528 lhs = NULL_TREE;
15529 expr = default_function_array_read_conversion (eloc, expr);
15530 unfolded_lhs1 = expr.value;
15531 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15532 rhs1 = NULL_TREE;
15533 c_parser_consume_token (parser);
15534 goto restart;
15536 if (structured_block)
15538 opcode = NOP_EXPR;
15539 expr = default_function_array_read_conversion (eloc, expr);
15540 rhs = c_fully_fold (expr.value, false, NULL, true);
15541 rhs1 = NULL_TREE;
15542 goto stmt_done;
15545 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15546 goto saw_error;
15547 default:
15548 c_parser_error (parser,
15549 "invalid operator for %<#pragma omp atomic%>");
15550 goto saw_error;
15553 /* Arrange to pass the location of the assignment operator to
15554 c_finish_omp_atomic. */
15555 loc = c_parser_peek_token (parser)->location;
15556 c_parser_consume_token (parser);
15557 eloc = c_parser_peek_token (parser)->location;
15558 expr = c_parser_expression (parser);
15559 expr = default_function_array_read_conversion (eloc, expr);
15560 rhs = expr.value;
15561 rhs = c_fully_fold (rhs, false, NULL, true);
15562 break;
15564 stmt_done:
15565 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15567 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15568 goto saw_error;
15569 v = c_parser_cast_expression (parser, NULL).value;
15570 non_lvalue_p = !lvalue_p (v);
15571 v = c_fully_fold (v, false, NULL, true);
15572 if (v == error_mark_node)
15573 goto saw_error;
15574 if (non_lvalue_p)
15575 v = non_lvalue (v);
15576 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15577 goto saw_error;
15578 eloc = c_parser_peek_token (parser)->location;
15579 expr = c_parser_cast_expression (parser, NULL);
15580 lhs1 = expr.value;
15581 expr = default_function_array_read_conversion (eloc, expr);
15582 unfolded_lhs1 = expr.value;
15583 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15584 if (lhs1 == error_mark_node)
15585 goto saw_error;
15586 if (!lvalue_p (unfolded_lhs1))
15587 lhs1 = non_lvalue (lhs1);
15589 if (structured_block)
15591 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15592 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15594 done:
15595 if (unfolded_lhs && unfolded_lhs1
15596 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15598 error ("%<#pragma omp atomic capture%> uses two different "
15599 "expressions for memory");
15600 stmt = error_mark_node;
15602 else
15603 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15604 swapped, seq_cst);
15605 if (stmt != error_mark_node)
15606 add_stmt (stmt);
15608 if (!structured_block)
15609 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15613 /* OpenMP 2.5:
15614 # pragma omp barrier new-line
15617 static void
15618 c_parser_omp_barrier (c_parser *parser)
15620 location_t loc = c_parser_peek_token (parser)->location;
15621 c_parser_consume_pragma (parser);
15622 c_parser_skip_to_pragma_eol (parser);
15624 c_finish_omp_barrier (loc);
15627 /* OpenMP 2.5:
15628 # pragma omp critical [(name)] new-line
15629 structured-block
15631 OpenMP 4.5:
15632 # pragma omp critical [(name) [hint(expression)]] new-line
15634 LOC is the location of the #pragma itself. */
15636 #define OMP_CRITICAL_CLAUSE_MASK \
15637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15639 static tree
15640 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15642 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15644 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15646 c_parser_consume_token (parser);
15647 if (c_parser_next_token_is (parser, CPP_NAME))
15649 name = c_parser_peek_token (parser)->value;
15650 c_parser_consume_token (parser);
15651 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15653 else
15654 c_parser_error (parser, "expected identifier");
15656 clauses = c_parser_omp_all_clauses (parser,
15657 OMP_CRITICAL_CLAUSE_MASK,
15658 "#pragma omp critical");
15660 else
15662 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15663 c_parser_error (parser, "expected %<(%> or end of line");
15664 c_parser_skip_to_pragma_eol (parser);
15667 stmt = c_parser_omp_structured_block (parser, if_p);
15668 return c_finish_omp_critical (loc, stmt, name, clauses);
15671 /* OpenMP 2.5:
15672 # pragma omp flush flush-vars[opt] new-line
15674 flush-vars:
15675 ( variable-list ) */
15677 static void
15678 c_parser_omp_flush (c_parser *parser)
15680 location_t loc = c_parser_peek_token (parser)->location;
15681 c_parser_consume_pragma (parser);
15682 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15683 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15684 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15685 c_parser_error (parser, "expected %<(%> or end of line");
15686 c_parser_skip_to_pragma_eol (parser);
15688 c_finish_omp_flush (loc);
15691 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15692 The real trick here is to determine the loop control variable early
15693 so that we can push a new decl if necessary to make it private.
15694 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15695 respectively. */
15697 static tree
15698 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15699 tree clauses, tree *cclauses, bool *if_p)
15701 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15702 tree declv, condv, incrv, initv, ret = NULL_TREE;
15703 tree pre_body = NULL_TREE, this_pre_body;
15704 tree ordered_cl = NULL_TREE;
15705 bool fail = false, open_brace_parsed = false;
15706 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15707 location_t for_loc;
15708 bool tiling = false;
15709 vec<tree, va_gc> *for_block = make_tree_vector ();
15711 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15712 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15713 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15714 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15716 tiling = true;
15717 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15719 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15720 && OMP_CLAUSE_ORDERED_EXPR (cl))
15722 ordered_cl = cl;
15723 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15726 if (ordered && ordered < collapse)
15728 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15729 "%<ordered%> clause parameter is less than %<collapse%>");
15730 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15731 = build_int_cst (NULL_TREE, collapse);
15732 ordered = collapse;
15734 if (ordered)
15736 for (tree *pc = &clauses; *pc; )
15737 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15739 error_at (OMP_CLAUSE_LOCATION (*pc),
15740 "%<linear%> clause may not be specified together "
15741 "with %<ordered%> clause with a parameter");
15742 *pc = OMP_CLAUSE_CHAIN (*pc);
15744 else
15745 pc = &OMP_CLAUSE_CHAIN (*pc);
15748 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15749 count = ordered ? ordered : collapse;
15751 declv = make_tree_vec (count);
15752 initv = make_tree_vec (count);
15753 condv = make_tree_vec (count);
15754 incrv = make_tree_vec (count);
15756 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15758 c_parser_error (parser, "for statement expected");
15759 return NULL;
15761 for_loc = c_parser_peek_token (parser)->location;
15762 c_parser_consume_token (parser);
15764 for (i = 0; i < count; i++)
15766 int bracecount = 0;
15768 matching_parens parens;
15769 if (!parens.require_open (parser))
15770 goto pop_scopes;
15772 /* Parse the initialization declaration or expression. */
15773 if (c_parser_next_tokens_start_declaration (parser))
15775 if (i > 0)
15776 vec_safe_push (for_block, c_begin_compound_stmt (true));
15777 this_pre_body = push_stmt_list ();
15778 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15779 NULL, vNULL);
15780 if (this_pre_body)
15782 this_pre_body = pop_stmt_list (this_pre_body);
15783 if (pre_body)
15785 tree t = pre_body;
15786 pre_body = push_stmt_list ();
15787 add_stmt (t);
15788 add_stmt (this_pre_body);
15789 pre_body = pop_stmt_list (pre_body);
15791 else
15792 pre_body = this_pre_body;
15794 decl = check_for_loop_decls (for_loc, flag_isoc99);
15795 if (decl == NULL)
15796 goto error_init;
15797 if (DECL_INITIAL (decl) == error_mark_node)
15798 decl = error_mark_node;
15799 init = decl;
15801 else if (c_parser_next_token_is (parser, CPP_NAME)
15802 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15804 struct c_expr decl_exp;
15805 struct c_expr init_exp;
15806 location_t init_loc;
15808 decl_exp = c_parser_postfix_expression (parser);
15809 decl = decl_exp.value;
15811 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15813 init_loc = c_parser_peek_token (parser)->location;
15814 init_exp = c_parser_expr_no_commas (parser, NULL);
15815 init_exp = default_function_array_read_conversion (init_loc,
15816 init_exp);
15817 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15818 NOP_EXPR, init_loc, init_exp.value,
15819 init_exp.original_type);
15820 init = c_process_expr_stmt (init_loc, init);
15822 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15824 else
15826 error_init:
15827 c_parser_error (parser,
15828 "expected iteration declaration or initialization");
15829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15830 "expected %<)%>");
15831 fail = true;
15832 goto parse_next;
15835 /* Parse the loop condition. */
15836 cond = NULL_TREE;
15837 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15839 location_t cond_loc = c_parser_peek_token (parser)->location;
15840 struct c_expr cond_expr
15841 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15843 cond = cond_expr.value;
15844 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15845 if (COMPARISON_CLASS_P (cond))
15847 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15848 op0 = c_fully_fold (op0, false, NULL);
15849 op1 = c_fully_fold (op1, false, NULL);
15850 TREE_OPERAND (cond, 0) = op0;
15851 TREE_OPERAND (cond, 1) = op1;
15853 switch (cond_expr.original_code)
15855 case GT_EXPR:
15856 case GE_EXPR:
15857 case LT_EXPR:
15858 case LE_EXPR:
15859 break;
15860 default:
15861 /* Can't be cond = error_mark_node, because we want to preserve
15862 the location until c_finish_omp_for. */
15863 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15864 break;
15866 protected_set_expr_location (cond, cond_loc);
15868 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15870 /* Parse the increment expression. */
15871 incr = NULL_TREE;
15872 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15874 location_t incr_loc = c_parser_peek_token (parser)->location;
15876 incr = c_process_expr_stmt (incr_loc,
15877 c_parser_expression (parser).value);
15879 parens.skip_until_found_close (parser);
15881 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15882 fail = true;
15883 else
15885 TREE_VEC_ELT (declv, i) = decl;
15886 TREE_VEC_ELT (initv, i) = init;
15887 TREE_VEC_ELT (condv, i) = cond;
15888 TREE_VEC_ELT (incrv, i) = incr;
15891 parse_next:
15892 if (i == count - 1)
15893 break;
15895 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15896 in between the collapsed for loops to be still considered perfectly
15897 nested. Hopefully the final version clarifies this.
15898 For now handle (multiple) {'s and empty statements. */
15901 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15903 c_parser_consume_token (parser);
15904 break;
15906 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15908 c_parser_consume_token (parser);
15909 bracecount++;
15911 else if (bracecount
15912 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15913 c_parser_consume_token (parser);
15914 else
15916 c_parser_error (parser, "not enough perfectly nested loops");
15917 if (bracecount)
15919 open_brace_parsed = true;
15920 bracecount--;
15922 fail = true;
15923 count = 0;
15924 break;
15927 while (1);
15929 nbraces += bracecount;
15932 if (nbraces)
15933 if_p = NULL;
15935 save_break = c_break_label;
15936 c_break_label = size_one_node;
15937 save_cont = c_cont_label;
15938 c_cont_label = NULL_TREE;
15939 body = push_stmt_list ();
15941 if (open_brace_parsed)
15943 location_t here = c_parser_peek_token (parser)->location;
15944 stmt = c_begin_compound_stmt (true);
15945 c_parser_compound_statement_nostart (parser);
15946 add_stmt (c_end_compound_stmt (here, stmt, true));
15948 else
15949 add_stmt (c_parser_c99_block_statement (parser, if_p));
15950 if (c_cont_label)
15952 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15953 SET_EXPR_LOCATION (t, loc);
15954 add_stmt (t);
15957 body = pop_stmt_list (body);
15958 c_break_label = save_break;
15959 c_cont_label = save_cont;
15961 while (nbraces)
15963 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15965 c_parser_consume_token (parser);
15966 nbraces--;
15968 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15969 c_parser_consume_token (parser);
15970 else
15972 c_parser_error (parser, "collapsed loops not perfectly nested");
15973 while (nbraces)
15975 location_t here = c_parser_peek_token (parser)->location;
15976 stmt = c_begin_compound_stmt (true);
15977 add_stmt (body);
15978 c_parser_compound_statement_nostart (parser);
15979 body = c_end_compound_stmt (here, stmt, true);
15980 nbraces--;
15982 goto pop_scopes;
15986 /* Only bother calling c_finish_omp_for if we haven't already generated
15987 an error from the initialization parsing. */
15988 if (!fail)
15990 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15991 incrv, body, pre_body);
15993 /* Check for iterators appearing in lb, b or incr expressions. */
15994 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15995 stmt = NULL_TREE;
15997 if (stmt)
15999 add_stmt (stmt);
16001 if (cclauses != NULL
16002 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
16004 tree *c;
16005 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
16006 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
16007 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
16008 c = &OMP_CLAUSE_CHAIN (*c);
16009 else
16011 for (i = 0; i < count; i++)
16012 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
16013 break;
16014 if (i == count)
16015 c = &OMP_CLAUSE_CHAIN (*c);
16016 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
16018 error_at (loc,
16019 "iteration variable %qD should not be firstprivate",
16020 OMP_CLAUSE_DECL (*c));
16021 *c = OMP_CLAUSE_CHAIN (*c);
16023 else
16025 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16026 tree l = *c;
16027 *c = OMP_CLAUSE_CHAIN (*c);
16028 if (code == OMP_SIMD)
16030 OMP_CLAUSE_CHAIN (l)
16031 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16032 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
16034 else
16036 OMP_CLAUSE_CHAIN (l) = clauses;
16037 clauses = l;
16042 OMP_FOR_CLAUSES (stmt) = clauses;
16044 ret = stmt;
16046 pop_scopes:
16047 while (!for_block->is_empty ())
16049 /* FIXME diagnostics: LOC below should be the actual location of
16050 this particular for block. We need to build a list of
16051 locations to go along with FOR_BLOCK. */
16052 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
16053 add_stmt (stmt);
16055 release_tree_vector (for_block);
16056 return ret;
16059 /* Helper function for OpenMP parsing, split clauses and call
16060 finish_omp_clauses on each of the set of clauses afterwards. */
16062 static void
16063 omp_split_clauses (location_t loc, enum tree_code code,
16064 omp_clause_mask mask, tree clauses, tree *cclauses)
16066 int i;
16067 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
16068 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
16069 if (cclauses[i])
16070 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
16073 /* OpenMP 4.0:
16074 #pragma omp simd simd-clause[optseq] new-line
16075 for-loop
16077 LOC is the location of the #pragma token.
16080 #define OMP_SIMD_CLAUSE_MASK \
16081 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
16082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16090 static tree
16091 c_parser_omp_simd (location_t loc, c_parser *parser,
16092 char *p_name, omp_clause_mask mask, tree *cclauses,
16093 bool *if_p)
16095 tree block, clauses, ret;
16097 strcat (p_name, " simd");
16098 mask |= OMP_SIMD_CLAUSE_MASK;
16100 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16101 if (cclauses)
16103 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
16104 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
16105 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
16106 OMP_CLAUSE_ORDERED);
16107 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
16109 error_at (OMP_CLAUSE_LOCATION (c),
16110 "%<ordered%> clause with parameter may not be specified "
16111 "on %qs construct", p_name);
16112 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
16116 block = c_begin_compound_stmt (true);
16117 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
16118 block = c_end_compound_stmt (loc, block, true);
16119 add_stmt (block);
16121 return ret;
16124 /* OpenMP 2.5:
16125 #pragma omp for for-clause[optseq] new-line
16126 for-loop
16128 OpenMP 4.0:
16129 #pragma omp for simd for-simd-clause[optseq] new-line
16130 for-loop
16132 LOC is the location of the #pragma token.
16135 #define OMP_FOR_CLAUSE_MASK \
16136 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
16142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
16143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16146 static tree
16147 c_parser_omp_for (location_t loc, c_parser *parser,
16148 char *p_name, omp_clause_mask mask, tree *cclauses,
16149 bool *if_p)
16151 tree block, clauses, ret;
16153 strcat (p_name, " for");
16154 mask |= OMP_FOR_CLAUSE_MASK;
16155 /* parallel for{, simd} disallows nowait clause, but for
16156 target {teams distribute ,}parallel for{, simd} it should be accepted. */
16157 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16158 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16159 /* Composite distribute parallel for{, simd} disallows ordered clause. */
16160 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16161 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16163 if (c_parser_next_token_is (parser, CPP_NAME))
16165 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16167 if (strcmp (p, "simd") == 0)
16169 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16170 if (cclauses == NULL)
16171 cclauses = cclauses_buf;
16173 c_parser_consume_token (parser);
16174 if (!flag_openmp) /* flag_openmp_simd */
16175 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16176 if_p);
16177 block = c_begin_compound_stmt (true);
16178 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16179 block = c_end_compound_stmt (loc, block, true);
16180 if (ret == NULL_TREE)
16181 return ret;
16182 ret = make_node (OMP_FOR);
16183 TREE_TYPE (ret) = void_type_node;
16184 OMP_FOR_BODY (ret) = block;
16185 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16186 SET_EXPR_LOCATION (ret, loc);
16187 add_stmt (ret);
16188 return ret;
16191 if (!flag_openmp) /* flag_openmp_simd */
16193 c_parser_skip_to_pragma_eol (parser, false);
16194 return NULL_TREE;
16197 /* Composite distribute parallel for disallows linear clause. */
16198 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16199 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16201 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16202 if (cclauses)
16204 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16205 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16208 block = c_begin_compound_stmt (true);
16209 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16210 block = c_end_compound_stmt (loc, block, true);
16211 add_stmt (block);
16213 return ret;
16216 /* OpenMP 2.5:
16217 # pragma omp master new-line
16218 structured-block
16220 LOC is the location of the #pragma token.
16223 static tree
16224 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16226 c_parser_skip_to_pragma_eol (parser);
16227 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16228 if_p));
16231 /* OpenMP 2.5:
16232 # pragma omp ordered new-line
16233 structured-block
16235 OpenMP 4.5:
16236 # pragma omp ordered ordered-clauses new-line
16237 structured-block
16239 # pragma omp ordered depend-clauses new-line */
16241 #define OMP_ORDERED_CLAUSE_MASK \
16242 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16245 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16246 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16248 static bool
16249 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16250 bool *if_p)
16252 location_t loc = c_parser_peek_token (parser)->location;
16253 c_parser_consume_pragma (parser);
16255 if (context != pragma_stmt && context != pragma_compound)
16257 c_parser_error (parser, "expected declaration specifiers");
16258 c_parser_skip_to_pragma_eol (parser, false);
16259 return false;
16262 if (c_parser_next_token_is (parser, CPP_NAME))
16264 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16266 if (!strcmp ("depend", p))
16268 if (!flag_openmp) /* flag_openmp_simd */
16270 c_parser_skip_to_pragma_eol (parser, false);
16271 return false;
16273 if (context == pragma_stmt)
16275 error_at (loc,
16276 "%<#pragma omp ordered%> with %<depend%> clause may "
16277 "only be used in compound statements");
16278 c_parser_skip_to_pragma_eol (parser, false);
16279 return false;
16282 tree clauses
16283 = c_parser_omp_all_clauses (parser,
16284 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16285 "#pragma omp ordered");
16286 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16287 return false;
16291 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16292 "#pragma omp ordered");
16294 if (!flag_openmp /* flag_openmp_simd */
16295 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16296 return false;
16298 c_finish_omp_ordered (loc, clauses,
16299 c_parser_omp_structured_block (parser, if_p));
16300 return true;
16303 /* OpenMP 2.5:
16305 section-scope:
16306 { section-sequence }
16308 section-sequence:
16309 section-directive[opt] structured-block
16310 section-sequence section-directive structured-block
16312 SECTIONS_LOC is the location of the #pragma omp sections. */
16314 static tree
16315 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16317 tree stmt, substmt;
16318 bool error_suppress = false;
16319 location_t loc;
16321 loc = c_parser_peek_token (parser)->location;
16322 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16324 /* Avoid skipping until the end of the block. */
16325 parser->error = false;
16326 return NULL_TREE;
16329 stmt = push_stmt_list ();
16331 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16333 substmt = c_parser_omp_structured_block (parser, NULL);
16334 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16335 SET_EXPR_LOCATION (substmt, loc);
16336 add_stmt (substmt);
16339 while (1)
16341 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16342 break;
16343 if (c_parser_next_token_is (parser, CPP_EOF))
16344 break;
16346 loc = c_parser_peek_token (parser)->location;
16347 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16349 c_parser_consume_pragma (parser);
16350 c_parser_skip_to_pragma_eol (parser);
16351 error_suppress = false;
16353 else if (!error_suppress)
16355 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16356 error_suppress = true;
16359 substmt = c_parser_omp_structured_block (parser, NULL);
16360 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16361 SET_EXPR_LOCATION (substmt, loc);
16362 add_stmt (substmt);
16364 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16365 "expected %<#pragma omp section%> or %<}%>");
16367 substmt = pop_stmt_list (stmt);
16369 stmt = make_node (OMP_SECTIONS);
16370 SET_EXPR_LOCATION (stmt, sections_loc);
16371 TREE_TYPE (stmt) = void_type_node;
16372 OMP_SECTIONS_BODY (stmt) = substmt;
16374 return add_stmt (stmt);
16377 /* OpenMP 2.5:
16378 # pragma omp sections sections-clause[optseq] newline
16379 sections-scope
16381 LOC is the location of the #pragma token.
16384 #define OMP_SECTIONS_CLAUSE_MASK \
16385 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16391 static tree
16392 c_parser_omp_sections (location_t loc, c_parser *parser,
16393 char *p_name, omp_clause_mask mask, tree *cclauses)
16395 tree block, clauses, ret;
16397 strcat (p_name, " sections");
16398 mask |= OMP_SECTIONS_CLAUSE_MASK;
16399 if (cclauses)
16400 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16402 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16403 if (cclauses)
16405 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16406 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16409 block = c_begin_compound_stmt (true);
16410 ret = c_parser_omp_sections_scope (loc, parser);
16411 if (ret)
16412 OMP_SECTIONS_CLAUSES (ret) = clauses;
16413 block = c_end_compound_stmt (loc, block, true);
16414 add_stmt (block);
16416 return ret;
16419 /* OpenMP 2.5:
16420 # pragma omp parallel parallel-clause[optseq] new-line
16421 structured-block
16422 # pragma omp parallel for parallel-for-clause[optseq] new-line
16423 structured-block
16424 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16425 structured-block
16427 OpenMP 4.0:
16428 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16429 structured-block
16431 LOC is the location of the #pragma token.
16434 #define OMP_PARALLEL_CLAUSE_MASK \
16435 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16445 static tree
16446 c_parser_omp_parallel (location_t loc, c_parser *parser,
16447 char *p_name, omp_clause_mask mask, tree *cclauses,
16448 bool *if_p)
16450 tree stmt, clauses, block;
16452 strcat (p_name, " parallel");
16453 mask |= OMP_PARALLEL_CLAUSE_MASK;
16454 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16455 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16456 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16457 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16459 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16461 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16462 if (cclauses == NULL)
16463 cclauses = cclauses_buf;
16465 c_parser_consume_token (parser);
16466 if (!flag_openmp) /* flag_openmp_simd */
16467 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16468 block = c_begin_omp_parallel ();
16469 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16470 stmt
16471 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16472 block);
16473 if (ret == NULL_TREE)
16474 return ret;
16475 OMP_PARALLEL_COMBINED (stmt) = 1;
16476 return stmt;
16478 /* When combined with distribute, parallel has to be followed by for.
16479 #pragma omp target parallel is allowed though. */
16480 else if (cclauses
16481 && (mask & (OMP_CLAUSE_MASK_1
16482 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16484 error_at (loc, "expected %<for%> after %qs", p_name);
16485 c_parser_skip_to_pragma_eol (parser);
16486 return NULL_TREE;
16488 else if (!flag_openmp) /* flag_openmp_simd */
16490 c_parser_skip_to_pragma_eol (parser, false);
16491 return NULL_TREE;
16493 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16495 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16496 if (strcmp (p, "sections") == 0)
16498 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16499 if (cclauses == NULL)
16500 cclauses = cclauses_buf;
16502 c_parser_consume_token (parser);
16503 block = c_begin_omp_parallel ();
16504 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16505 stmt = c_finish_omp_parallel (loc,
16506 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16507 block);
16508 OMP_PARALLEL_COMBINED (stmt) = 1;
16509 return stmt;
16513 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16514 if (cclauses)
16516 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16517 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16520 block = c_begin_omp_parallel ();
16521 c_parser_statement (parser, if_p);
16522 stmt = c_finish_omp_parallel (loc, clauses, block);
16524 return stmt;
16527 /* OpenMP 2.5:
16528 # pragma omp single single-clause[optseq] new-line
16529 structured-block
16531 LOC is the location of the #pragma.
16534 #define OMP_SINGLE_CLAUSE_MASK \
16535 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16540 static tree
16541 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16543 tree stmt = make_node (OMP_SINGLE);
16544 SET_EXPR_LOCATION (stmt, loc);
16545 TREE_TYPE (stmt) = void_type_node;
16547 OMP_SINGLE_CLAUSES (stmt)
16548 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16549 "#pragma omp single");
16550 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16552 return add_stmt (stmt);
16555 /* OpenMP 3.0:
16556 # pragma omp task task-clause[optseq] new-line
16558 LOC is the location of the #pragma.
16561 #define OMP_TASK_CLAUSE_MASK \
16562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16573 static tree
16574 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16576 tree clauses, block;
16578 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16579 "#pragma omp task");
16581 block = c_begin_omp_task ();
16582 c_parser_statement (parser, if_p);
16583 return c_finish_omp_task (loc, clauses, block);
16586 /* OpenMP 3.0:
16587 # pragma omp taskwait new-line
16590 static void
16591 c_parser_omp_taskwait (c_parser *parser)
16593 location_t loc = c_parser_peek_token (parser)->location;
16594 c_parser_consume_pragma (parser);
16595 c_parser_skip_to_pragma_eol (parser);
16597 c_finish_omp_taskwait (loc);
16600 /* OpenMP 3.1:
16601 # pragma omp taskyield new-line
16604 static void
16605 c_parser_omp_taskyield (c_parser *parser)
16607 location_t loc = c_parser_peek_token (parser)->location;
16608 c_parser_consume_pragma (parser);
16609 c_parser_skip_to_pragma_eol (parser);
16611 c_finish_omp_taskyield (loc);
16614 /* OpenMP 4.0:
16615 # pragma omp taskgroup new-line
16618 static tree
16619 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16621 location_t loc = c_parser_peek_token (parser)->location;
16622 c_parser_skip_to_pragma_eol (parser);
16623 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16624 if_p));
16627 /* OpenMP 4.0:
16628 # pragma omp cancel cancel-clause[optseq] new-line
16630 LOC is the location of the #pragma.
16633 #define OMP_CANCEL_CLAUSE_MASK \
16634 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16640 static void
16641 c_parser_omp_cancel (c_parser *parser)
16643 location_t loc = c_parser_peek_token (parser)->location;
16645 c_parser_consume_pragma (parser);
16646 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16647 "#pragma omp cancel");
16649 c_finish_omp_cancel (loc, clauses);
16652 /* OpenMP 4.0:
16653 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16655 LOC is the location of the #pragma.
16658 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16664 static void
16665 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16667 location_t loc = c_parser_peek_token (parser)->location;
16668 tree clauses;
16669 bool point_seen = false;
16671 c_parser_consume_pragma (parser);
16672 if (c_parser_next_token_is (parser, CPP_NAME))
16674 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16675 if (strcmp (p, "point") == 0)
16677 c_parser_consume_token (parser);
16678 point_seen = true;
16681 if (!point_seen)
16683 c_parser_error (parser, "expected %<point%>");
16684 c_parser_skip_to_pragma_eol (parser);
16685 return;
16688 if (context != pragma_compound)
16690 if (context == pragma_stmt)
16691 error_at (loc,
16692 "%<#pragma %s%> may only be used in compound statements",
16693 "omp cancellation point");
16694 else
16695 c_parser_error (parser, "expected declaration specifiers");
16696 c_parser_skip_to_pragma_eol (parser, false);
16697 return;
16700 clauses
16701 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16702 "#pragma omp cancellation point");
16704 c_finish_omp_cancellation_point (loc, clauses);
16707 /* OpenMP 4.0:
16708 #pragma omp distribute distribute-clause[optseq] new-line
16709 for-loop */
16711 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16712 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16718 static tree
16719 c_parser_omp_distribute (location_t loc, c_parser *parser,
16720 char *p_name, omp_clause_mask mask, tree *cclauses,
16721 bool *if_p)
16723 tree clauses, block, ret;
16725 strcat (p_name, " distribute");
16726 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16728 if (c_parser_next_token_is (parser, CPP_NAME))
16730 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16731 bool simd = false;
16732 bool parallel = false;
16734 if (strcmp (p, "simd") == 0)
16735 simd = true;
16736 else
16737 parallel = strcmp (p, "parallel") == 0;
16738 if (parallel || simd)
16740 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16741 if (cclauses == NULL)
16742 cclauses = cclauses_buf;
16743 c_parser_consume_token (parser);
16744 if (!flag_openmp) /* flag_openmp_simd */
16746 if (simd)
16747 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16748 if_p);
16749 else
16750 return c_parser_omp_parallel (loc, parser, p_name, mask,
16751 cclauses, if_p);
16753 block = c_begin_compound_stmt (true);
16754 if (simd)
16755 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16756 if_p);
16757 else
16758 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16759 if_p);
16760 block = c_end_compound_stmt (loc, block, true);
16761 if (ret == NULL)
16762 return ret;
16763 ret = make_node (OMP_DISTRIBUTE);
16764 TREE_TYPE (ret) = void_type_node;
16765 OMP_FOR_BODY (ret) = block;
16766 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16767 SET_EXPR_LOCATION (ret, loc);
16768 add_stmt (ret);
16769 return ret;
16772 if (!flag_openmp) /* flag_openmp_simd */
16774 c_parser_skip_to_pragma_eol (parser, false);
16775 return NULL_TREE;
16778 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16779 if (cclauses)
16781 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16782 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16785 block = c_begin_compound_stmt (true);
16786 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16787 if_p);
16788 block = c_end_compound_stmt (loc, block, true);
16789 add_stmt (block);
16791 return ret;
16794 /* OpenMP 4.0:
16795 # pragma omp teams teams-clause[optseq] new-line
16796 structured-block */
16798 #define OMP_TEAMS_CLAUSE_MASK \
16799 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16807 static tree
16808 c_parser_omp_teams (location_t loc, c_parser *parser,
16809 char *p_name, omp_clause_mask mask, tree *cclauses,
16810 bool *if_p)
16812 tree clauses, block, ret;
16814 strcat (p_name, " teams");
16815 mask |= OMP_TEAMS_CLAUSE_MASK;
16817 if (c_parser_next_token_is (parser, CPP_NAME))
16819 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16820 if (strcmp (p, "distribute") == 0)
16822 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16823 if (cclauses == NULL)
16824 cclauses = cclauses_buf;
16826 c_parser_consume_token (parser);
16827 if (!flag_openmp) /* flag_openmp_simd */
16828 return c_parser_omp_distribute (loc, parser, p_name, mask,
16829 cclauses, if_p);
16830 block = c_begin_compound_stmt (true);
16831 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16832 if_p);
16833 block = c_end_compound_stmt (loc, block, true);
16834 if (ret == NULL)
16835 return ret;
16836 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16837 ret = make_node (OMP_TEAMS);
16838 TREE_TYPE (ret) = void_type_node;
16839 OMP_TEAMS_CLAUSES (ret) = clauses;
16840 OMP_TEAMS_BODY (ret) = block;
16841 OMP_TEAMS_COMBINED (ret) = 1;
16842 return add_stmt (ret);
16845 if (!flag_openmp) /* flag_openmp_simd */
16847 c_parser_skip_to_pragma_eol (parser, false);
16848 return NULL_TREE;
16851 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16852 if (cclauses)
16854 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16855 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16858 tree stmt = make_node (OMP_TEAMS);
16859 TREE_TYPE (stmt) = void_type_node;
16860 OMP_TEAMS_CLAUSES (stmt) = clauses;
16861 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16863 return add_stmt (stmt);
16866 /* OpenMP 4.0:
16867 # pragma omp target data target-data-clause[optseq] new-line
16868 structured-block */
16870 #define OMP_TARGET_DATA_CLAUSE_MASK \
16871 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16876 static tree
16877 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16879 tree clauses
16880 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16881 "#pragma omp target data");
16882 int map_seen = 0;
16883 for (tree *pc = &clauses; *pc;)
16885 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16886 switch (OMP_CLAUSE_MAP_KIND (*pc))
16888 case GOMP_MAP_TO:
16889 case GOMP_MAP_ALWAYS_TO:
16890 case GOMP_MAP_FROM:
16891 case GOMP_MAP_ALWAYS_FROM:
16892 case GOMP_MAP_TOFROM:
16893 case GOMP_MAP_ALWAYS_TOFROM:
16894 case GOMP_MAP_ALLOC:
16895 map_seen = 3;
16896 break;
16897 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16898 case GOMP_MAP_ALWAYS_POINTER:
16899 break;
16900 default:
16901 map_seen |= 1;
16902 error_at (OMP_CLAUSE_LOCATION (*pc),
16903 "%<#pragma omp target data%> with map-type other "
16904 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16905 "on %<map%> clause");
16906 *pc = OMP_CLAUSE_CHAIN (*pc);
16907 continue;
16909 pc = &OMP_CLAUSE_CHAIN (*pc);
16912 if (map_seen != 3)
16914 if (map_seen == 0)
16915 error_at (loc,
16916 "%<#pragma omp target data%> must contain at least "
16917 "one %<map%> clause");
16918 return NULL_TREE;
16921 tree stmt = make_node (OMP_TARGET_DATA);
16922 TREE_TYPE (stmt) = void_type_node;
16923 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16924 keep_next_level ();
16925 tree block = c_begin_compound_stmt (true);
16926 add_stmt (c_parser_omp_structured_block (parser, if_p));
16927 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16929 SET_EXPR_LOCATION (stmt, loc);
16930 return add_stmt (stmt);
16933 /* OpenMP 4.0:
16934 # pragma omp target update target-update-clause[optseq] new-line */
16936 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16944 static bool
16945 c_parser_omp_target_update (location_t loc, c_parser *parser,
16946 enum pragma_context context)
16948 if (context == pragma_stmt)
16950 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16951 "omp target update");
16952 c_parser_skip_to_pragma_eol (parser, false);
16953 return false;
16956 tree clauses
16957 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16958 "#pragma omp target update");
16959 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16960 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16962 error_at (loc,
16963 "%<#pragma omp target update%> must contain at least one "
16964 "%<from%> or %<to%> clauses");
16965 return false;
16968 tree stmt = make_node (OMP_TARGET_UPDATE);
16969 TREE_TYPE (stmt) = void_type_node;
16970 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16971 SET_EXPR_LOCATION (stmt, loc);
16972 add_stmt (stmt);
16973 return false;
16976 /* OpenMP 4.5:
16977 # pragma omp target enter data target-data-clause[optseq] new-line */
16979 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16980 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16986 static tree
16987 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16988 enum pragma_context context)
16990 bool data_seen = false;
16991 if (c_parser_next_token_is (parser, CPP_NAME))
16993 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16994 if (strcmp (p, "data") == 0)
16996 c_parser_consume_token (parser);
16997 data_seen = true;
17000 if (!data_seen)
17002 c_parser_error (parser, "expected %<data%>");
17003 c_parser_skip_to_pragma_eol (parser);
17004 return NULL_TREE;
17007 if (context == pragma_stmt)
17009 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
17010 "omp target enter data");
17011 c_parser_skip_to_pragma_eol (parser, false);
17012 return NULL_TREE;
17015 tree clauses
17016 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
17017 "#pragma omp target enter data");
17018 int map_seen = 0;
17019 for (tree *pc = &clauses; *pc;)
17021 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17022 switch (OMP_CLAUSE_MAP_KIND (*pc))
17024 case GOMP_MAP_TO:
17025 case GOMP_MAP_ALWAYS_TO:
17026 case GOMP_MAP_ALLOC:
17027 map_seen = 3;
17028 break;
17029 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17030 case GOMP_MAP_ALWAYS_POINTER:
17031 break;
17032 default:
17033 map_seen |= 1;
17034 error_at (OMP_CLAUSE_LOCATION (*pc),
17035 "%<#pragma omp target enter data%> with map-type other "
17036 "than %<to%> or %<alloc%> on %<map%> clause");
17037 *pc = OMP_CLAUSE_CHAIN (*pc);
17038 continue;
17040 pc = &OMP_CLAUSE_CHAIN (*pc);
17043 if (map_seen != 3)
17045 if (map_seen == 0)
17046 error_at (loc,
17047 "%<#pragma omp target enter data%> must contain at least "
17048 "one %<map%> clause");
17049 return NULL_TREE;
17052 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
17053 TREE_TYPE (stmt) = void_type_node;
17054 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
17055 SET_EXPR_LOCATION (stmt, loc);
17056 add_stmt (stmt);
17057 return stmt;
17060 /* OpenMP 4.5:
17061 # pragma omp target exit data target-data-clause[optseq] new-line */
17063 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
17064 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17070 static tree
17071 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
17072 enum pragma_context context)
17074 bool data_seen = false;
17075 if (c_parser_next_token_is (parser, CPP_NAME))
17077 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17078 if (strcmp (p, "data") == 0)
17080 c_parser_consume_token (parser);
17081 data_seen = true;
17084 if (!data_seen)
17086 c_parser_error (parser, "expected %<data%>");
17087 c_parser_skip_to_pragma_eol (parser);
17088 return NULL_TREE;
17091 if (context == pragma_stmt)
17093 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
17094 "omp target exit data");
17095 c_parser_skip_to_pragma_eol (parser, false);
17096 return NULL_TREE;
17099 tree clauses
17100 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
17101 "#pragma omp target exit data");
17103 int map_seen = 0;
17104 for (tree *pc = &clauses; *pc;)
17106 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17107 switch (OMP_CLAUSE_MAP_KIND (*pc))
17109 case GOMP_MAP_FROM:
17110 case GOMP_MAP_ALWAYS_FROM:
17111 case GOMP_MAP_RELEASE:
17112 case GOMP_MAP_DELETE:
17113 map_seen = 3;
17114 break;
17115 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17116 case GOMP_MAP_ALWAYS_POINTER:
17117 break;
17118 default:
17119 map_seen |= 1;
17120 error_at (OMP_CLAUSE_LOCATION (*pc),
17121 "%<#pragma omp target exit data%> with map-type other "
17122 "than %<from%>, %<release%> or %<delete%> on %<map%>"
17123 " clause");
17124 *pc = OMP_CLAUSE_CHAIN (*pc);
17125 continue;
17127 pc = &OMP_CLAUSE_CHAIN (*pc);
17130 if (map_seen != 3)
17132 if (map_seen == 0)
17133 error_at (loc,
17134 "%<#pragma omp target exit data%> must contain at least one "
17135 "%<map%> clause");
17136 return NULL_TREE;
17139 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
17140 TREE_TYPE (stmt) = void_type_node;
17141 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
17142 SET_EXPR_LOCATION (stmt, loc);
17143 add_stmt (stmt);
17144 return stmt;
17147 /* OpenMP 4.0:
17148 # pragma omp target target-clause[optseq] new-line
17149 structured-block */
17151 #define OMP_TARGET_CLAUSE_MASK \
17152 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
17157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
17160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17162 static bool
17163 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17165 location_t loc = c_parser_peek_token (parser)->location;
17166 c_parser_consume_pragma (parser);
17167 tree *pc = NULL, stmt, block;
17169 if (context != pragma_stmt && context != pragma_compound)
17171 c_parser_error (parser, "expected declaration specifiers");
17172 c_parser_skip_to_pragma_eol (parser);
17173 return false;
17176 if (c_parser_next_token_is (parser, CPP_NAME))
17178 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17179 enum tree_code ccode = ERROR_MARK;
17181 if (strcmp (p, "teams") == 0)
17182 ccode = OMP_TEAMS;
17183 else if (strcmp (p, "parallel") == 0)
17184 ccode = OMP_PARALLEL;
17185 else if (strcmp (p, "simd") == 0)
17186 ccode = OMP_SIMD;
17187 if (ccode != ERROR_MARK)
17189 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17190 char p_name[sizeof ("#pragma omp target teams distribute "
17191 "parallel for simd")];
17193 c_parser_consume_token (parser);
17194 strcpy (p_name, "#pragma omp target");
17195 if (!flag_openmp) /* flag_openmp_simd */
17197 tree stmt;
17198 switch (ccode)
17200 case OMP_TEAMS:
17201 stmt = c_parser_omp_teams (loc, parser, p_name,
17202 OMP_TARGET_CLAUSE_MASK,
17203 cclauses, if_p);
17204 break;
17205 case OMP_PARALLEL:
17206 stmt = c_parser_omp_parallel (loc, parser, p_name,
17207 OMP_TARGET_CLAUSE_MASK,
17208 cclauses, if_p);
17209 break;
17210 case OMP_SIMD:
17211 stmt = c_parser_omp_simd (loc, parser, p_name,
17212 OMP_TARGET_CLAUSE_MASK,
17213 cclauses, if_p);
17214 break;
17215 default:
17216 gcc_unreachable ();
17218 return stmt != NULL_TREE;
17220 keep_next_level ();
17221 tree block = c_begin_compound_stmt (true), ret;
17222 switch (ccode)
17224 case OMP_TEAMS:
17225 ret = c_parser_omp_teams (loc, parser, p_name,
17226 OMP_TARGET_CLAUSE_MASK, cclauses,
17227 if_p);
17228 break;
17229 case OMP_PARALLEL:
17230 ret = c_parser_omp_parallel (loc, parser, p_name,
17231 OMP_TARGET_CLAUSE_MASK, cclauses,
17232 if_p);
17233 break;
17234 case OMP_SIMD:
17235 ret = c_parser_omp_simd (loc, parser, p_name,
17236 OMP_TARGET_CLAUSE_MASK, cclauses,
17237 if_p);
17238 break;
17239 default:
17240 gcc_unreachable ();
17242 block = c_end_compound_stmt (loc, block, true);
17243 if (ret == NULL_TREE)
17244 return false;
17245 if (ccode == OMP_TEAMS)
17247 /* For combined target teams, ensure the num_teams and
17248 thread_limit clause expressions are evaluated on the host,
17249 before entering the target construct. */
17250 tree c;
17251 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17252 c; c = OMP_CLAUSE_CHAIN (c))
17253 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17254 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17255 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17257 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17258 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17259 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17260 expr, NULL_TREE, NULL_TREE);
17261 add_stmt (expr);
17262 OMP_CLAUSE_OPERAND (c, 0) = expr;
17263 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17264 OMP_CLAUSE_FIRSTPRIVATE);
17265 OMP_CLAUSE_DECL (tc) = tmp;
17266 OMP_CLAUSE_CHAIN (tc)
17267 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17268 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17271 tree stmt = make_node (OMP_TARGET);
17272 TREE_TYPE (stmt) = void_type_node;
17273 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17274 OMP_TARGET_BODY (stmt) = block;
17275 OMP_TARGET_COMBINED (stmt) = 1;
17276 add_stmt (stmt);
17277 pc = &OMP_TARGET_CLAUSES (stmt);
17278 goto check_clauses;
17280 else if (!flag_openmp) /* flag_openmp_simd */
17282 c_parser_skip_to_pragma_eol (parser, false);
17283 return false;
17285 else if (strcmp (p, "data") == 0)
17287 c_parser_consume_token (parser);
17288 c_parser_omp_target_data (loc, parser, if_p);
17289 return true;
17291 else if (strcmp (p, "enter") == 0)
17293 c_parser_consume_token (parser);
17294 c_parser_omp_target_enter_data (loc, parser, context);
17295 return false;
17297 else if (strcmp (p, "exit") == 0)
17299 c_parser_consume_token (parser);
17300 c_parser_omp_target_exit_data (loc, parser, context);
17301 return false;
17303 else if (strcmp (p, "update") == 0)
17305 c_parser_consume_token (parser);
17306 return c_parser_omp_target_update (loc, parser, context);
17309 if (!flag_openmp) /* flag_openmp_simd */
17311 c_parser_skip_to_pragma_eol (parser, false);
17312 return false;
17315 stmt = make_node (OMP_TARGET);
17316 TREE_TYPE (stmt) = void_type_node;
17318 OMP_TARGET_CLAUSES (stmt)
17319 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17320 "#pragma omp target");
17321 pc = &OMP_TARGET_CLAUSES (stmt);
17322 keep_next_level ();
17323 block = c_begin_compound_stmt (true);
17324 add_stmt (c_parser_omp_structured_block (parser, if_p));
17325 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17327 SET_EXPR_LOCATION (stmt, loc);
17328 add_stmt (stmt);
17330 check_clauses:
17331 while (*pc)
17333 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17334 switch (OMP_CLAUSE_MAP_KIND (*pc))
17336 case GOMP_MAP_TO:
17337 case GOMP_MAP_ALWAYS_TO:
17338 case GOMP_MAP_FROM:
17339 case GOMP_MAP_ALWAYS_FROM:
17340 case GOMP_MAP_TOFROM:
17341 case GOMP_MAP_ALWAYS_TOFROM:
17342 case GOMP_MAP_ALLOC:
17343 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17344 case GOMP_MAP_ALWAYS_POINTER:
17345 break;
17346 default:
17347 error_at (OMP_CLAUSE_LOCATION (*pc),
17348 "%<#pragma omp target%> with map-type other "
17349 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17350 "on %<map%> clause");
17351 *pc = OMP_CLAUSE_CHAIN (*pc);
17352 continue;
17354 pc = &OMP_CLAUSE_CHAIN (*pc);
17356 return true;
17359 /* OpenMP 4.0:
17360 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17362 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17370 static void
17371 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17373 auto_vec<c_token> clauses;
17374 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17376 c_token *token = c_parser_peek_token (parser);
17377 if (token->type == CPP_EOF)
17379 c_parser_skip_to_pragma_eol (parser);
17380 return;
17382 clauses.safe_push (*token);
17383 c_parser_consume_token (parser);
17385 clauses.safe_push (*c_parser_peek_token (parser));
17386 c_parser_skip_to_pragma_eol (parser);
17388 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17390 if (c_parser_peek_token (parser)->pragma_kind
17391 != PRAGMA_OMP_DECLARE
17392 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17393 || strcmp (IDENTIFIER_POINTER
17394 (c_parser_peek_2nd_token (parser)->value),
17395 "simd") != 0)
17397 c_parser_error (parser,
17398 "%<#pragma omp declare simd%> must be followed by "
17399 "function declaration or definition or another "
17400 "%<#pragma omp declare simd%>");
17401 return;
17403 c_parser_consume_pragma (parser);
17404 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17406 c_token *token = c_parser_peek_token (parser);
17407 if (token->type == CPP_EOF)
17409 c_parser_skip_to_pragma_eol (parser);
17410 return;
17412 clauses.safe_push (*token);
17413 c_parser_consume_token (parser);
17415 clauses.safe_push (*c_parser_peek_token (parser));
17416 c_parser_skip_to_pragma_eol (parser);
17419 /* Make sure nothing tries to read past the end of the tokens. */
17420 c_token eof_token;
17421 memset (&eof_token, 0, sizeof (eof_token));
17422 eof_token.type = CPP_EOF;
17423 clauses.safe_push (eof_token);
17424 clauses.safe_push (eof_token);
17426 switch (context)
17428 case pragma_external:
17429 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17430 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17432 int ext = disable_extension_diagnostics ();
17434 c_parser_consume_token (parser);
17435 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17436 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17437 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17438 NULL, clauses);
17439 restore_extension_diagnostics (ext);
17441 else
17442 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17443 NULL, clauses);
17444 break;
17445 case pragma_struct:
17446 case pragma_param:
17447 case pragma_stmt:
17448 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17449 "function declaration or definition");
17450 break;
17451 case pragma_compound:
17452 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17453 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17455 int ext = disable_extension_diagnostics ();
17457 c_parser_consume_token (parser);
17458 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17459 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17460 if (c_parser_next_tokens_start_declaration (parser))
17462 c_parser_declaration_or_fndef (parser, true, true, true, true,
17463 true, NULL, clauses);
17464 restore_extension_diagnostics (ext);
17465 break;
17467 restore_extension_diagnostics (ext);
17469 else if (c_parser_next_tokens_start_declaration (parser))
17471 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17472 NULL, clauses);
17473 break;
17475 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17476 "function declaration or definition");
17477 break;
17478 default:
17479 gcc_unreachable ();
17483 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17484 and put that into "omp declare simd" attribute. */
17486 static void
17487 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17488 vec<c_token> clauses)
17490 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17491 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17492 has already processed the tokens. */
17493 if (clauses.exists () && clauses[0].type == CPP_EOF)
17494 return;
17495 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17497 error ("%<#pragma omp declare simd%> not immediately followed by "
17498 "a function declaration or definition");
17499 clauses[0].type = CPP_EOF;
17500 return;
17502 if (clauses.exists () && clauses[0].type != CPP_NAME)
17504 error_at (DECL_SOURCE_LOCATION (fndecl),
17505 "%<#pragma omp declare simd%> not immediately followed by "
17506 "a single function declaration or definition");
17507 clauses[0].type = CPP_EOF;
17508 return;
17511 if (parms == NULL_TREE)
17512 parms = DECL_ARGUMENTS (fndecl);
17514 unsigned int tokens_avail = parser->tokens_avail;
17515 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17518 parser->tokens = clauses.address ();
17519 parser->tokens_avail = clauses.length ();
17521 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17522 while (parser->tokens_avail > 3)
17524 c_token *token = c_parser_peek_token (parser);
17525 gcc_assert (token->type == CPP_NAME
17526 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17527 c_parser_consume_token (parser);
17528 parser->in_pragma = true;
17530 tree c = NULL_TREE;
17531 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17532 "#pragma omp declare simd");
17533 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17534 if (c != NULL_TREE)
17535 c = tree_cons (NULL_TREE, c, NULL_TREE);
17536 c = build_tree_list (get_identifier ("omp declare simd"), c);
17537 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17538 DECL_ATTRIBUTES (fndecl) = c;
17541 parser->tokens = &parser->tokens_buf[0];
17542 parser->tokens_avail = tokens_avail;
17543 if (clauses.exists ())
17544 clauses[0].type = CPP_PRAGMA;
17548 /* OpenMP 4.0:
17549 # pragma omp declare target new-line
17550 declarations and definitions
17551 # pragma omp end declare target new-line
17553 OpenMP 4.5:
17554 # pragma omp declare target ( extended-list ) new-line
17556 # pragma omp declare target declare-target-clauses[seq] new-line */
17558 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17559 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17562 static void
17563 c_parser_omp_declare_target (c_parser *parser)
17565 location_t loc = c_parser_peek_token (parser)->location;
17566 tree clauses = NULL_TREE;
17567 if (c_parser_next_token_is (parser, CPP_NAME))
17568 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17569 "#pragma omp declare target");
17570 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17572 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17573 clauses);
17574 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17575 c_parser_skip_to_pragma_eol (parser);
17577 else
17579 c_parser_skip_to_pragma_eol (parser);
17580 current_omp_declare_target_attribute++;
17581 return;
17583 if (current_omp_declare_target_attribute)
17584 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17585 "%<#pragma omp declare target%> without clauses and "
17586 "%<#pragma omp end declare target%>");
17587 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17589 tree t = OMP_CLAUSE_DECL (c), id;
17590 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17591 tree at2 = lookup_attribute ("omp declare target link",
17592 DECL_ATTRIBUTES (t));
17593 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17595 id = get_identifier ("omp declare target link");
17596 std::swap (at1, at2);
17598 else
17599 id = get_identifier ("omp declare target");
17600 if (at2)
17602 error_at (OMP_CLAUSE_LOCATION (c),
17603 "%qD specified both in declare target %<link%> and %<to%>"
17604 " clauses", t);
17605 continue;
17607 if (!at1)
17609 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17610 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17611 continue;
17613 symtab_node *node = symtab_node::get (t);
17614 if (node != NULL)
17616 node->offloadable = 1;
17617 if (ENABLE_OFFLOADING)
17619 g->have_offload = true;
17620 if (is_a <varpool_node *> (node))
17621 vec_safe_push (offload_vars, t);
17628 static void
17629 c_parser_omp_end_declare_target (c_parser *parser)
17631 location_t loc = c_parser_peek_token (parser)->location;
17632 c_parser_consume_pragma (parser);
17633 if (c_parser_next_token_is (parser, CPP_NAME)
17634 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17635 "declare") == 0)
17637 c_parser_consume_token (parser);
17638 if (c_parser_next_token_is (parser, CPP_NAME)
17639 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17640 "target") == 0)
17641 c_parser_consume_token (parser);
17642 else
17644 c_parser_error (parser, "expected %<target%>");
17645 c_parser_skip_to_pragma_eol (parser);
17646 return;
17649 else
17651 c_parser_error (parser, "expected %<declare%>");
17652 c_parser_skip_to_pragma_eol (parser);
17653 return;
17655 c_parser_skip_to_pragma_eol (parser);
17656 if (!current_omp_declare_target_attribute)
17657 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17658 "%<#pragma omp declare target%>");
17659 else
17660 current_omp_declare_target_attribute--;
17664 /* OpenMP 4.0
17665 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17666 initializer-clause[opt] new-line
17668 initializer-clause:
17669 initializer (omp_priv = initializer)
17670 initializer (function-name (argument-list)) */
17672 static void
17673 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17675 unsigned int tokens_avail = 0, i;
17676 vec<tree> types = vNULL;
17677 vec<c_token> clauses = vNULL;
17678 enum tree_code reduc_code = ERROR_MARK;
17679 tree reduc_id = NULL_TREE;
17680 tree type;
17681 location_t rloc = c_parser_peek_token (parser)->location;
17683 if (context == pragma_struct || context == pragma_param)
17685 error ("%<#pragma omp declare reduction%> not at file or block scope");
17686 goto fail;
17689 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17690 goto fail;
17692 switch (c_parser_peek_token (parser)->type)
17694 case CPP_PLUS:
17695 reduc_code = PLUS_EXPR;
17696 break;
17697 case CPP_MULT:
17698 reduc_code = MULT_EXPR;
17699 break;
17700 case CPP_MINUS:
17701 reduc_code = MINUS_EXPR;
17702 break;
17703 case CPP_AND:
17704 reduc_code = BIT_AND_EXPR;
17705 break;
17706 case CPP_XOR:
17707 reduc_code = BIT_XOR_EXPR;
17708 break;
17709 case CPP_OR:
17710 reduc_code = BIT_IOR_EXPR;
17711 break;
17712 case CPP_AND_AND:
17713 reduc_code = TRUTH_ANDIF_EXPR;
17714 break;
17715 case CPP_OR_OR:
17716 reduc_code = TRUTH_ORIF_EXPR;
17717 break;
17718 case CPP_NAME:
17719 const char *p;
17720 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17721 if (strcmp (p, "min") == 0)
17723 reduc_code = MIN_EXPR;
17724 break;
17726 if (strcmp (p, "max") == 0)
17728 reduc_code = MAX_EXPR;
17729 break;
17731 reduc_id = c_parser_peek_token (parser)->value;
17732 break;
17733 default:
17734 c_parser_error (parser,
17735 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17736 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17737 goto fail;
17740 tree orig_reduc_id, reduc_decl;
17741 orig_reduc_id = reduc_id;
17742 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17743 reduc_decl = c_omp_reduction_decl (reduc_id);
17744 c_parser_consume_token (parser);
17746 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17747 goto fail;
17749 while (true)
17751 location_t loc = c_parser_peek_token (parser)->location;
17752 struct c_type_name *ctype = c_parser_type_name (parser);
17753 if (ctype != NULL)
17755 type = groktypename (ctype, NULL, NULL);
17756 if (type == error_mark_node)
17758 else if ((INTEGRAL_TYPE_P (type)
17759 || TREE_CODE (type) == REAL_TYPE
17760 || TREE_CODE (type) == COMPLEX_TYPE)
17761 && orig_reduc_id == NULL_TREE)
17762 error_at (loc, "predeclared arithmetic type in "
17763 "%<#pragma omp declare reduction%>");
17764 else if (TREE_CODE (type) == FUNCTION_TYPE
17765 || TREE_CODE (type) == ARRAY_TYPE)
17766 error_at (loc, "function or array type in "
17767 "%<#pragma omp declare reduction%>");
17768 else if (TYPE_ATOMIC (type))
17769 error_at (loc, "%<_Atomic%> qualified type in "
17770 "%<#pragma omp declare reduction%>");
17771 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17772 error_at (loc, "const, volatile or restrict qualified type in "
17773 "%<#pragma omp declare reduction%>");
17774 else
17776 tree t;
17777 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17778 if (comptypes (TREE_PURPOSE (t), type))
17780 error_at (loc, "redeclaration of %qs "
17781 "%<#pragma omp declare reduction%> for "
17782 "type %qT",
17783 IDENTIFIER_POINTER (reduc_id)
17784 + sizeof ("omp declare reduction ") - 1,
17785 type);
17786 location_t ploc
17787 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17788 0));
17789 error_at (ploc, "previous %<#pragma omp declare "
17790 "reduction%>");
17791 break;
17793 if (t == NULL_TREE)
17794 types.safe_push (type);
17796 if (c_parser_next_token_is (parser, CPP_COMMA))
17797 c_parser_consume_token (parser);
17798 else
17799 break;
17801 else
17802 break;
17805 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17806 || types.is_empty ())
17808 fail:
17809 clauses.release ();
17810 types.release ();
17811 while (true)
17813 c_token *token = c_parser_peek_token (parser);
17814 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17815 break;
17816 c_parser_consume_token (parser);
17818 c_parser_skip_to_pragma_eol (parser);
17819 return;
17822 if (types.length () > 1)
17824 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17826 c_token *token = c_parser_peek_token (parser);
17827 if (token->type == CPP_EOF)
17828 goto fail;
17829 clauses.safe_push (*token);
17830 c_parser_consume_token (parser);
17832 clauses.safe_push (*c_parser_peek_token (parser));
17833 c_parser_skip_to_pragma_eol (parser);
17835 /* Make sure nothing tries to read past the end of the tokens. */
17836 c_token eof_token;
17837 memset (&eof_token, 0, sizeof (eof_token));
17838 eof_token.type = CPP_EOF;
17839 clauses.safe_push (eof_token);
17840 clauses.safe_push (eof_token);
17843 int errs = errorcount;
17844 FOR_EACH_VEC_ELT (types, i, type)
17846 tokens_avail = parser->tokens_avail;
17847 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17848 if (!clauses.is_empty ())
17850 parser->tokens = clauses.address ();
17851 parser->tokens_avail = clauses.length ();
17852 parser->in_pragma = true;
17855 bool nested = current_function_decl != NULL_TREE;
17856 if (nested)
17857 c_push_function_context ();
17858 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17859 reduc_id, default_function_type);
17860 current_function_decl = fndecl;
17861 allocate_struct_function (fndecl, true);
17862 push_scope ();
17863 tree stmt = push_stmt_list ();
17864 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17865 warn about these. */
17866 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17867 get_identifier ("omp_out"), type);
17868 DECL_ARTIFICIAL (omp_out) = 1;
17869 DECL_CONTEXT (omp_out) = fndecl;
17870 pushdecl (omp_out);
17871 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17872 get_identifier ("omp_in"), type);
17873 DECL_ARTIFICIAL (omp_in) = 1;
17874 DECL_CONTEXT (omp_in) = fndecl;
17875 pushdecl (omp_in);
17876 struct c_expr combiner = c_parser_expression (parser);
17877 struct c_expr initializer;
17878 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17879 bool bad = false;
17880 initializer.set_error ();
17881 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17882 bad = true;
17883 else if (c_parser_next_token_is (parser, CPP_NAME)
17884 && strcmp (IDENTIFIER_POINTER
17885 (c_parser_peek_token (parser)->value),
17886 "initializer") == 0)
17888 c_parser_consume_token (parser);
17889 pop_scope ();
17890 push_scope ();
17891 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17892 get_identifier ("omp_priv"), type);
17893 DECL_ARTIFICIAL (omp_priv) = 1;
17894 DECL_INITIAL (omp_priv) = error_mark_node;
17895 DECL_CONTEXT (omp_priv) = fndecl;
17896 pushdecl (omp_priv);
17897 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17898 get_identifier ("omp_orig"), type);
17899 DECL_ARTIFICIAL (omp_orig) = 1;
17900 DECL_CONTEXT (omp_orig) = fndecl;
17901 pushdecl (omp_orig);
17902 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17903 bad = true;
17904 else if (!c_parser_next_token_is (parser, CPP_NAME))
17906 c_parser_error (parser, "expected %<omp_priv%> or "
17907 "function-name");
17908 bad = true;
17910 else if (strcmp (IDENTIFIER_POINTER
17911 (c_parser_peek_token (parser)->value),
17912 "omp_priv") != 0)
17914 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17915 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17917 c_parser_error (parser, "expected function-name %<(%>");
17918 bad = true;
17920 else
17921 initializer = c_parser_postfix_expression (parser);
17922 if (initializer.value
17923 && TREE_CODE (initializer.value) == CALL_EXPR)
17925 int j;
17926 tree c = initializer.value;
17927 for (j = 0; j < call_expr_nargs (c); j++)
17929 tree a = CALL_EXPR_ARG (c, j);
17930 STRIP_NOPS (a);
17931 if (TREE_CODE (a) == ADDR_EXPR
17932 && TREE_OPERAND (a, 0) == omp_priv)
17933 break;
17935 if (j == call_expr_nargs (c))
17936 error ("one of the initializer call arguments should be "
17937 "%<&omp_priv%>");
17940 else
17942 c_parser_consume_token (parser);
17943 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17944 bad = true;
17945 else
17947 tree st = push_stmt_list ();
17948 location_t loc = c_parser_peek_token (parser)->location;
17949 rich_location richloc (line_table, loc);
17950 start_init (omp_priv, NULL_TREE, 0, &richloc);
17951 struct c_expr init = c_parser_initializer (parser);
17952 finish_init ();
17953 finish_decl (omp_priv, loc, init.value,
17954 init.original_type, NULL_TREE);
17955 pop_stmt_list (st);
17958 if (!bad
17959 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17960 bad = true;
17963 if (!bad)
17965 c_parser_skip_to_pragma_eol (parser);
17967 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17968 DECL_INITIAL (reduc_decl));
17969 DECL_INITIAL (reduc_decl) = t;
17970 DECL_SOURCE_LOCATION (omp_out) = rloc;
17971 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17972 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17973 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17974 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17975 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17976 if (omp_priv)
17978 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17979 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17980 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17981 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17982 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17983 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17984 walk_tree (&DECL_INITIAL (omp_priv),
17985 c_check_omp_declare_reduction_r,
17986 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17990 pop_stmt_list (stmt);
17991 pop_scope ();
17992 if (cfun->language != NULL)
17994 ggc_free (cfun->language);
17995 cfun->language = NULL;
17997 set_cfun (NULL);
17998 current_function_decl = NULL_TREE;
17999 if (nested)
18000 c_pop_function_context ();
18002 if (!clauses.is_empty ())
18004 parser->tokens = &parser->tokens_buf[0];
18005 parser->tokens_avail = tokens_avail;
18007 if (bad)
18008 goto fail;
18009 if (errs != errorcount)
18010 break;
18013 clauses.release ();
18014 types.release ();
18018 /* OpenMP 4.0
18019 #pragma omp declare simd declare-simd-clauses[optseq] new-line
18020 #pragma omp declare reduction (reduction-id : typename-list : expression) \
18021 initializer-clause[opt] new-line
18022 #pragma omp declare target new-line */
18024 static void
18025 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
18027 c_parser_consume_pragma (parser);
18028 if (c_parser_next_token_is (parser, CPP_NAME))
18030 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18031 if (strcmp (p, "simd") == 0)
18033 /* c_parser_consume_token (parser); done in
18034 c_parser_omp_declare_simd. */
18035 c_parser_omp_declare_simd (parser, context);
18036 return;
18038 if (strcmp (p, "reduction") == 0)
18040 c_parser_consume_token (parser);
18041 c_parser_omp_declare_reduction (parser, context);
18042 return;
18044 if (!flag_openmp) /* flag_openmp_simd */
18046 c_parser_skip_to_pragma_eol (parser, false);
18047 return;
18049 if (strcmp (p, "target") == 0)
18051 c_parser_consume_token (parser);
18052 c_parser_omp_declare_target (parser);
18053 return;
18057 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
18058 "or %<target%>");
18059 c_parser_skip_to_pragma_eol (parser);
18062 /* OpenMP 4.5:
18063 #pragma omp taskloop taskloop-clause[optseq] new-line
18064 for-loop
18066 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
18067 for-loop */
18069 #define OMP_TASKLOOP_CLAUSE_MASK \
18070 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
18071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
18075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
18076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
18077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
18079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
18081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
18082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
18083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
18085 static tree
18086 c_parser_omp_taskloop (location_t loc, c_parser *parser,
18087 char *p_name, omp_clause_mask mask, tree *cclauses,
18088 bool *if_p)
18090 tree clauses, block, ret;
18092 strcat (p_name, " taskloop");
18093 mask |= OMP_TASKLOOP_CLAUSE_MASK;
18095 if (c_parser_next_token_is (parser, CPP_NAME))
18097 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18099 if (strcmp (p, "simd") == 0)
18101 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18102 if (cclauses == NULL)
18103 cclauses = cclauses_buf;
18104 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
18105 c_parser_consume_token (parser);
18106 if (!flag_openmp) /* flag_openmp_simd */
18107 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
18108 if_p);
18109 block = c_begin_compound_stmt (true);
18110 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
18111 block = c_end_compound_stmt (loc, block, true);
18112 if (ret == NULL)
18113 return ret;
18114 ret = make_node (OMP_TASKLOOP);
18115 TREE_TYPE (ret) = void_type_node;
18116 OMP_FOR_BODY (ret) = block;
18117 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18118 SET_EXPR_LOCATION (ret, loc);
18119 add_stmt (ret);
18120 return ret;
18123 if (!flag_openmp) /* flag_openmp_simd */
18125 c_parser_skip_to_pragma_eol (parser, false);
18126 return NULL_TREE;
18129 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18130 if (cclauses)
18132 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
18133 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18136 block = c_begin_compound_stmt (true);
18137 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
18138 block = c_end_compound_stmt (loc, block, true);
18139 add_stmt (block);
18141 return ret;
18144 /* Main entry point to parsing most OpenMP pragmas. */
18146 static void
18147 c_parser_omp_construct (c_parser *parser, bool *if_p)
18149 enum pragma_kind p_kind;
18150 location_t loc;
18151 tree stmt;
18152 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18153 omp_clause_mask mask (0);
18155 loc = c_parser_peek_token (parser)->location;
18156 p_kind = c_parser_peek_token (parser)->pragma_kind;
18157 c_parser_consume_pragma (parser);
18159 switch (p_kind)
18161 case PRAGMA_OACC_ATOMIC:
18162 c_parser_omp_atomic (loc, parser);
18163 return;
18164 case PRAGMA_OACC_CACHE:
18165 strcpy (p_name, "#pragma acc");
18166 stmt = c_parser_oacc_cache (loc, parser);
18167 break;
18168 case PRAGMA_OACC_DATA:
18169 stmt = c_parser_oacc_data (loc, parser, if_p);
18170 break;
18171 case PRAGMA_OACC_HOST_DATA:
18172 stmt = c_parser_oacc_host_data (loc, parser, if_p);
18173 break;
18174 case PRAGMA_OACC_KERNELS:
18175 case PRAGMA_OACC_PARALLEL:
18176 strcpy (p_name, "#pragma acc");
18177 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18178 if_p);
18179 break;
18180 case PRAGMA_OACC_LOOP:
18181 strcpy (p_name, "#pragma acc");
18182 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18183 break;
18184 case PRAGMA_OACC_WAIT:
18185 strcpy (p_name, "#pragma wait");
18186 stmt = c_parser_oacc_wait (loc, parser, p_name);
18187 break;
18188 case PRAGMA_OMP_ATOMIC:
18189 c_parser_omp_atomic (loc, parser);
18190 return;
18191 case PRAGMA_OMP_CRITICAL:
18192 stmt = c_parser_omp_critical (loc, parser, if_p);
18193 break;
18194 case PRAGMA_OMP_DISTRIBUTE:
18195 strcpy (p_name, "#pragma omp");
18196 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18197 break;
18198 case PRAGMA_OMP_FOR:
18199 strcpy (p_name, "#pragma omp");
18200 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18201 break;
18202 case PRAGMA_OMP_MASTER:
18203 stmt = c_parser_omp_master (loc, parser, if_p);
18204 break;
18205 case PRAGMA_OMP_PARALLEL:
18206 strcpy (p_name, "#pragma omp");
18207 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18208 break;
18209 case PRAGMA_OMP_SECTIONS:
18210 strcpy (p_name, "#pragma omp");
18211 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18212 break;
18213 case PRAGMA_OMP_SIMD:
18214 strcpy (p_name, "#pragma omp");
18215 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18216 break;
18217 case PRAGMA_OMP_SINGLE:
18218 stmt = c_parser_omp_single (loc, parser, if_p);
18219 break;
18220 case PRAGMA_OMP_TASK:
18221 stmt = c_parser_omp_task (loc, parser, if_p);
18222 break;
18223 case PRAGMA_OMP_TASKGROUP:
18224 stmt = c_parser_omp_taskgroup (parser, if_p);
18225 break;
18226 case PRAGMA_OMP_TASKLOOP:
18227 strcpy (p_name, "#pragma omp");
18228 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18229 break;
18230 case PRAGMA_OMP_TEAMS:
18231 strcpy (p_name, "#pragma omp");
18232 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18233 break;
18234 default:
18235 gcc_unreachable ();
18238 if (stmt)
18239 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18243 /* OpenMP 2.5:
18244 # pragma omp threadprivate (variable-list) */
18246 static void
18247 c_parser_omp_threadprivate (c_parser *parser)
18249 tree vars, t;
18250 location_t loc;
18252 c_parser_consume_pragma (parser);
18253 loc = c_parser_peek_token (parser)->location;
18254 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18256 /* Mark every variable in VARS to be assigned thread local storage. */
18257 for (t = vars; t; t = TREE_CHAIN (t))
18259 tree v = TREE_PURPOSE (t);
18261 /* FIXME diagnostics: Ideally we should keep individual
18262 locations for all the variables in the var list to make the
18263 following errors more precise. Perhaps
18264 c_parser_omp_var_list_parens() should construct a list of
18265 locations to go along with the var list. */
18267 /* If V had already been marked threadprivate, it doesn't matter
18268 whether it had been used prior to this point. */
18269 if (!VAR_P (v))
18270 error_at (loc, "%qD is not a variable", v);
18271 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18272 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18273 else if (! is_global_var (v))
18274 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18275 else if (TREE_TYPE (v) == error_mark_node)
18277 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18278 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18279 else
18281 if (! DECL_THREAD_LOCAL_P (v))
18283 set_decl_tls_model (v, decl_default_tls_model (v));
18284 /* If rtl has been already set for this var, call
18285 make_decl_rtl once again, so that encode_section_info
18286 has a chance to look at the new decl flags. */
18287 if (DECL_RTL_SET_P (v))
18288 make_decl_rtl (v);
18290 C_DECL_THREADPRIVATE_P (v) = 1;
18294 c_parser_skip_to_pragma_eol (parser);
18297 /* Parse a transaction attribute (GCC Extension).
18299 transaction-attribute:
18300 attributes
18301 [ [ any-word ] ]
18303 The transactional memory language description is written for C++,
18304 and uses the C++0x attribute syntax. For compatibility, allow the
18305 bracket style for transactions in C as well. */
18307 static tree
18308 c_parser_transaction_attributes (c_parser *parser)
18310 tree attr_name, attr = NULL;
18312 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18313 return c_parser_attributes (parser);
18315 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18316 return NULL_TREE;
18317 c_parser_consume_token (parser);
18318 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18319 goto error1;
18321 attr_name = c_parser_attribute_any_word (parser);
18322 if (attr_name)
18324 c_parser_consume_token (parser);
18325 attr = build_tree_list (attr_name, NULL_TREE);
18327 else
18328 c_parser_error (parser, "expected identifier");
18330 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18331 error1:
18332 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18333 return attr;
18336 /* Parse a __transaction_atomic or __transaction_relaxed statement
18337 (GCC Extension).
18339 transaction-statement:
18340 __transaction_atomic transaction-attribute[opt] compound-statement
18341 __transaction_relaxed compound-statement
18343 Note that the only valid attribute is: "outer".
18346 static tree
18347 c_parser_transaction (c_parser *parser, enum rid keyword)
18349 unsigned int old_in = parser->in_transaction;
18350 unsigned int this_in = 1, new_in;
18351 location_t loc = c_parser_peek_token (parser)->location;
18352 tree stmt, attrs;
18354 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18355 || keyword == RID_TRANSACTION_RELAXED)
18356 && c_parser_next_token_is_keyword (parser, keyword));
18357 c_parser_consume_token (parser);
18359 if (keyword == RID_TRANSACTION_RELAXED)
18360 this_in |= TM_STMT_ATTR_RELAXED;
18361 else
18363 attrs = c_parser_transaction_attributes (parser);
18364 if (attrs)
18365 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18368 /* Keep track if we're in the lexical scope of an outer transaction. */
18369 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18371 parser->in_transaction = new_in;
18372 stmt = c_parser_compound_statement (parser);
18373 parser->in_transaction = old_in;
18375 if (flag_tm)
18376 stmt = c_finish_transaction (loc, stmt, this_in);
18377 else
18378 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18379 "%<__transaction_atomic%> without transactional memory support enabled"
18380 : "%<__transaction_relaxed %> "
18381 "without transactional memory support enabled"));
18383 return stmt;
18386 /* Parse a __transaction_atomic or __transaction_relaxed expression
18387 (GCC Extension).
18389 transaction-expression:
18390 __transaction_atomic ( expression )
18391 __transaction_relaxed ( expression )
18394 static struct c_expr
18395 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18397 struct c_expr ret;
18398 unsigned int old_in = parser->in_transaction;
18399 unsigned int this_in = 1;
18400 location_t loc = c_parser_peek_token (parser)->location;
18401 tree attrs;
18403 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18404 || keyword == RID_TRANSACTION_RELAXED)
18405 && c_parser_next_token_is_keyword (parser, keyword));
18406 c_parser_consume_token (parser);
18408 if (keyword == RID_TRANSACTION_RELAXED)
18409 this_in |= TM_STMT_ATTR_RELAXED;
18410 else
18412 attrs = c_parser_transaction_attributes (parser);
18413 if (attrs)
18414 this_in |= parse_tm_stmt_attr (attrs, 0);
18417 parser->in_transaction = this_in;
18418 matching_parens parens;
18419 if (parens.require_open (parser))
18421 tree expr = c_parser_expression (parser).value;
18422 ret.original_type = TREE_TYPE (expr);
18423 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18424 if (this_in & TM_STMT_ATTR_RELAXED)
18425 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18426 SET_EXPR_LOCATION (ret.value, loc);
18427 ret.original_code = TRANSACTION_EXPR;
18428 if (!parens.require_close (parser))
18430 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18431 goto error;
18434 else
18436 error:
18437 ret.set_error ();
18438 ret.original_code = ERROR_MARK;
18439 ret.original_type = NULL;
18441 parser->in_transaction = old_in;
18443 if (!flag_tm)
18444 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18445 "%<__transaction_atomic%> without transactional memory support enabled"
18446 : "%<__transaction_relaxed %> "
18447 "without transactional memory support enabled"));
18449 set_c_expr_source_range (&ret, loc, loc);
18451 return ret;
18454 /* Parse a __transaction_cancel statement (GCC Extension).
18456 transaction-cancel-statement:
18457 __transaction_cancel transaction-attribute[opt] ;
18459 Note that the only valid attribute is "outer".
18462 static tree
18463 c_parser_transaction_cancel (c_parser *parser)
18465 location_t loc = c_parser_peek_token (parser)->location;
18466 tree attrs;
18467 bool is_outer = false;
18469 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18470 c_parser_consume_token (parser);
18472 attrs = c_parser_transaction_attributes (parser);
18473 if (attrs)
18474 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18476 if (!flag_tm)
18478 error_at (loc, "%<__transaction_cancel%> without "
18479 "transactional memory support enabled");
18480 goto ret_error;
18482 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18484 error_at (loc, "%<__transaction_cancel%> within a "
18485 "%<__transaction_relaxed%>");
18486 goto ret_error;
18488 else if (is_outer)
18490 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18491 && !is_tm_may_cancel_outer (current_function_decl))
18493 error_at (loc, "outer %<__transaction_cancel%> not "
18494 "within outer %<__transaction_atomic%>");
18495 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18496 goto ret_error;
18499 else if (parser->in_transaction == 0)
18501 error_at (loc, "%<__transaction_cancel%> not within "
18502 "%<__transaction_atomic%>");
18503 goto ret_error;
18506 return add_stmt (build_tm_abort_call (loc, is_outer));
18508 ret_error:
18509 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18512 /* Parse a single source file. */
18514 void
18515 c_parse_file (void)
18517 /* Use local storage to begin. If the first token is a pragma, parse it.
18518 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18519 which will cause garbage collection. */
18520 c_parser tparser;
18522 memset (&tparser, 0, sizeof tparser);
18523 tparser.tokens = &tparser.tokens_buf[0];
18524 the_parser = &tparser;
18526 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18527 c_parser_pragma_pch_preprocess (&tparser);
18529 the_parser = ggc_alloc<c_parser> ();
18530 *the_parser = tparser;
18531 if (tparser.tokens == &tparser.tokens_buf[0])
18532 the_parser->tokens = &the_parser->tokens_buf[0];
18534 /* Initialize EH, if we've been told to do so. */
18535 if (flag_exceptions)
18536 using_eh_for_cleanups ();
18538 c_parser_translation_unit (the_parser);
18539 the_parser = NULL;
18542 /* Parse the body of a function declaration marked with "__RTL".
18544 The RTL parser works on the level of characters read from a
18545 FILE *, whereas c_parser works at the level of tokens.
18546 Square this circle by consuming all of the tokens up to and
18547 including the closing brace, recording the start/end of the RTL
18548 fragment, and reopening the file and re-reading the relevant
18549 lines within the RTL parser.
18551 This requires the opening and closing braces of the C function
18552 to be on separate lines from the RTL they wrap.
18554 Take ownership of START_WITH_PASS, if non-NULL. */
18556 void
18557 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18559 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18561 free (start_with_pass);
18562 return;
18565 location_t start_loc = c_parser_peek_token (parser)->location;
18567 /* Consume all tokens, up to the closing brace, handling
18568 matching pairs of braces in the rtl dump. */
18569 int num_open_braces = 1;
18570 while (1)
18572 switch (c_parser_peek_token (parser)->type)
18574 case CPP_OPEN_BRACE:
18575 num_open_braces++;
18576 break;
18577 case CPP_CLOSE_BRACE:
18578 if (--num_open_braces == 0)
18579 goto found_closing_brace;
18580 break;
18581 case CPP_EOF:
18582 error_at (start_loc, "no closing brace");
18583 free (start_with_pass);
18584 return;
18585 default:
18586 break;
18588 c_parser_consume_token (parser);
18591 found_closing_brace:
18592 /* At the closing brace; record its location. */
18593 location_t end_loc = c_parser_peek_token (parser)->location;
18595 /* Consume the closing brace. */
18596 c_parser_consume_token (parser);
18598 /* Invoke the RTL parser. */
18599 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18601 free (start_with_pass);
18602 return;
18605 /* If a pass name was provided for START_WITH_PASS, run the backend
18606 accordingly now, on the cfun created above, transferring
18607 ownership of START_WITH_PASS. */
18608 if (start_with_pass)
18609 run_rtl_passes (start_with_pass);
18612 #include "gt-c-c-parser.h"