Fix warning with -Wsign-compare -Wsystem-headers
[official-gcc.git] / gcc / c / c-parser.c
blob5ad4f57a4fe7883651cb46c44e60128c64ca16ed
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
72 /* We need to walk over decls with incomplete struct/union/enum types
73 after parsing the whole translation unit.
74 In finish_decl(), if the decl is static, has incomplete
75 struct/union/enum type, it is appeneded to incomplete_record_decls.
76 In c_parser_translation_unit(), we iterate over incomplete_record_decls
77 and report error if any of the decls are still incomplete. */
79 vec<tree> incomplete_record_decls;
81 void
82 set_c_expr_source_range (c_expr *expr,
83 location_t start, location_t finish)
85 expr->src_range.m_start = start;
86 expr->src_range.m_finish = finish;
87 if (expr->value)
88 set_source_range (expr->value, start, finish);
91 void
92 set_c_expr_source_range (c_expr *expr,
93 source_range src_range)
95 expr->src_range = src_range;
96 if (expr->value)
97 set_source_range (expr->value, src_range);
101 /* Initialization routine for this file. */
103 void
104 c_parse_init (void)
106 /* The only initialization required is of the reserved word
107 identifiers. */
108 unsigned int i;
109 tree id;
110 int mask = 0;
112 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
113 the c_token structure. */
114 gcc_assert (RID_MAX <= 255);
116 mask |= D_CXXONLY;
117 if (!flag_isoc99)
118 mask |= D_C99;
119 if (flag_no_asm)
121 mask |= D_ASM | D_EXT;
122 if (!flag_isoc99)
123 mask |= D_EXT89;
125 if (!c_dialect_objc ())
126 mask |= D_OBJC | D_CXX_OBJC;
128 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
129 for (i = 0; i < num_c_common_reswords; i++)
131 /* If a keyword is disabled, do not enter it into the table
132 and so create a canonical spelling that isn't a keyword. */
133 if (c_common_reswords[i].disable & mask)
135 if (warn_cxx_compat
136 && (c_common_reswords[i].disable & D_CXXWARN))
138 id = get_identifier (c_common_reswords[i].word);
139 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
140 C_IS_RESERVED_WORD (id) = 1;
142 continue;
145 id = get_identifier (c_common_reswords[i].word);
146 C_SET_RID_CODE (id, c_common_reswords[i].rid);
147 C_IS_RESERVED_WORD (id) = 1;
148 ridpointers [(int) c_common_reswords[i].rid] = id;
151 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 /* We always create the symbols but they aren't always supported. */
154 char name[50];
155 sprintf (name, "__int%d", int_n_data[i].bitsize);
156 id = get_identifier (name);
157 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
158 C_IS_RESERVED_WORD (id) = 1;
162 /* A parser structure recording information about the state and
163 context of parsing. Includes lexer information with up to two
164 tokens of look-ahead; more are not needed for C. */
165 struct GTY(()) c_parser {
166 /* The look-ahead tokens. */
167 c_token * GTY((skip)) tokens;
168 /* Buffer for look-ahead tokens. */
169 c_token tokens_buf[4];
170 /* How many look-ahead tokens are available (0 - 4, or
171 more if parsing from pre-lexed tokens). */
172 unsigned int tokens_avail;
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
185 /* Objective-C specific parser/lexer information. */
187 /* True if we are in a context where the Objective-C "PQ" keywords
188 are considered keywords. */
189 BOOL_BITFIELD objc_pq_context : 1;
190 /* True if we are parsing a (potential) Objective-C foreach
191 statement. This is set to true after we parsed 'for (' and while
192 we wait for 'in' or ';' to decide if it's a standard C for loop or an
193 Objective-C foreach loop. */
194 BOOL_BITFIELD objc_could_be_foreach_context : 1;
195 /* The following flag is needed to contextualize Objective-C lexical
196 analysis. In some cases (e.g., 'int NSObject;'), it is
197 undesirable to bind an identifier to an Objective-C class, even
198 if a class with that name exists. */
199 BOOL_BITFIELD objc_need_raw_identifier : 1;
200 /* Nonzero if we're processing a __transaction statement. The value
201 is 1 | TM_STMT_ATTR_*. */
202 unsigned int in_transaction : 4;
203 /* True if we are in a context where the Objective-C "Property attribute"
204 keywords are valid. */
205 BOOL_BITFIELD objc_property_attr_context : 1;
207 /* Location of the last consumed token. */
208 location_t last_token_location;
211 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
213 c_token *
214 c_parser_tokens_buf (c_parser *parser, unsigned n)
216 return &parser->tokens_buf[n];
219 /* Return the error state of PARSER. */
221 bool
222 c_parser_error (c_parser *parser)
224 return parser->error;
227 /* Set the error state of PARSER to ERR. */
229 void
230 c_parser_set_error (c_parser *parser, bool err)
232 parser->error = err;
236 /* The actual parser and external interface. ??? Does this need to be
237 garbage-collected? */
239 static GTY (()) c_parser *the_parser;
241 /* Read in and lex a single token, storing it in *TOKEN. */
243 static void
244 c_lex_one_token (c_parser *parser, c_token *token)
246 timevar_push (TV_LEX);
248 token->type = c_lex_with_flags (&token->value, &token->location,
249 &token->flags,
250 (parser->lex_untranslated_string
251 ? C_LEX_STRING_NO_TRANSLATE : 0));
252 token->id_kind = C_ID_NONE;
253 token->keyword = RID_MAX;
254 token->pragma_kind = PRAGMA_NONE;
256 switch (token->type)
258 case CPP_NAME:
260 tree decl;
262 bool objc_force_identifier = parser->objc_need_raw_identifier;
263 if (c_dialect_objc ())
264 parser->objc_need_raw_identifier = false;
266 if (C_IS_RESERVED_WORD (token->value))
268 enum rid rid_code = C_RID_CODE (token->value);
270 if (rid_code == RID_CXX_COMPAT_WARN)
272 warning_at (token->location,
273 OPT_Wc___compat,
274 "identifier %qE conflicts with C++ keyword",
275 token->value);
277 else if (rid_code >= RID_FIRST_ADDR_SPACE
278 && rid_code <= RID_LAST_ADDR_SPACE)
280 addr_space_t as;
281 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
282 targetm.addr_space.diagnose_usage (as, token->location);
283 token->id_kind = C_ID_ADDRSPACE;
284 token->keyword = rid_code;
285 break;
287 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
289 /* We found an Objective-C "pq" keyword (in, out,
290 inout, bycopy, byref, oneway). They need special
291 care because the interpretation depends on the
292 context. */
293 if (parser->objc_pq_context)
295 token->type = CPP_KEYWORD;
296 token->keyword = rid_code;
297 break;
299 else if (parser->objc_could_be_foreach_context
300 && rid_code == RID_IN)
302 /* We are in Objective-C, inside a (potential)
303 foreach context (which means after having
304 parsed 'for (', but before having parsed ';'),
305 and we found 'in'. We consider it the keyword
306 which terminates the declaration at the
307 beginning of a foreach-statement. Note that
308 this means you can't use 'in' for anything else
309 in that context; in particular, in Objective-C
310 you can't use 'in' as the name of the running
311 variable in a C for loop. We could potentially
312 try to add code here to disambiguate, but it
313 seems a reasonable limitation. */
314 token->type = CPP_KEYWORD;
315 token->keyword = rid_code;
316 break;
318 /* Else, "pq" keywords outside of the "pq" context are
319 not keywords, and we fall through to the code for
320 normal tokens. */
322 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
324 /* We found an Objective-C "property attribute"
325 keyword (getter, setter, readonly, etc). These are
326 only valid in the property context. */
327 if (parser->objc_property_attr_context)
329 token->type = CPP_KEYWORD;
330 token->keyword = rid_code;
331 break;
333 /* Else they are not special keywords.
336 else if (c_dialect_objc ()
337 && (OBJC_IS_AT_KEYWORD (rid_code)
338 || OBJC_IS_CXX_KEYWORD (rid_code)))
340 /* We found one of the Objective-C "@" keywords (defs,
341 selector, synchronized, etc) or one of the
342 Objective-C "cxx" keywords (class, private,
343 protected, public, try, catch, throw) without a
344 preceding '@' sign. Do nothing and fall through to
345 the code for normal tokens (in C++ we would still
346 consider the CXX ones keywords, but not in C). */
349 else
351 token->type = CPP_KEYWORD;
352 token->keyword = rid_code;
353 break;
357 decl = lookup_name (token->value);
358 if (decl)
360 if (TREE_CODE (decl) == TYPE_DECL)
362 token->id_kind = C_ID_TYPENAME;
363 break;
366 else if (c_dialect_objc ())
368 tree objc_interface_decl = objc_is_class_name (token->value);
369 /* Objective-C class names are in the same namespace as
370 variables and typedefs, and hence are shadowed by local
371 declarations. */
372 if (objc_interface_decl
373 && (!objc_force_identifier || global_bindings_p ()))
375 token->value = objc_interface_decl;
376 token->id_kind = C_ID_CLASSNAME;
377 break;
380 token->id_kind = C_ID_ID;
382 break;
383 case CPP_AT_NAME:
384 /* This only happens in Objective-C; it must be a keyword. */
385 token->type = CPP_KEYWORD;
386 switch (C_RID_CODE (token->value))
388 /* Replace 'class' with '@class', 'private' with '@private',
389 etc. This prevents confusion with the C++ keyword
390 'class', and makes the tokens consistent with other
391 Objective-C 'AT' keywords. For example '@class' is
392 reported as RID_AT_CLASS which is consistent with
393 '@synchronized', which is reported as
394 RID_AT_SYNCHRONIZED.
396 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
397 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
398 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
399 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
400 case RID_THROW: token->keyword = RID_AT_THROW; break;
401 case RID_TRY: token->keyword = RID_AT_TRY; break;
402 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
403 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
404 default: token->keyword = C_RID_CODE (token->value);
406 break;
407 case CPP_COLON:
408 case CPP_COMMA:
409 case CPP_CLOSE_PAREN:
410 case CPP_SEMICOLON:
411 /* These tokens may affect the interpretation of any identifiers
412 following, if doing Objective-C. */
413 if (c_dialect_objc ())
414 parser->objc_need_raw_identifier = false;
415 break;
416 case CPP_PRAGMA:
417 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
418 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
419 token->value = NULL;
420 break;
421 default:
422 break;
424 timevar_pop (TV_LEX);
427 /* Return a pointer to the next token from PARSER, reading it in if
428 necessary. */
430 c_token *
431 c_parser_peek_token (c_parser *parser)
433 if (parser->tokens_avail == 0)
435 c_lex_one_token (parser, &parser->tokens[0]);
436 parser->tokens_avail = 1;
438 return &parser->tokens[0];
441 /* Return a pointer to the next-but-one token from PARSER, reading it
442 in if necessary. The next token is already read in. */
444 c_token *
445 c_parser_peek_2nd_token (c_parser *parser)
447 if (parser->tokens_avail >= 2)
448 return &parser->tokens[1];
449 gcc_assert (parser->tokens_avail == 1);
450 gcc_assert (parser->tokens[0].type != CPP_EOF);
451 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
452 c_lex_one_token (parser, &parser->tokens[1]);
453 parser->tokens_avail = 2;
454 return &parser->tokens[1];
457 /* Return a pointer to the Nth token from PARSER, reading it
458 in if necessary. The N-1th token is already read in. */
460 c_token *
461 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
463 /* N is 1-based, not zero-based. */
464 gcc_assert (n > 0);
466 if (parser->tokens_avail >= n)
467 return &parser->tokens[n - 1];
468 gcc_assert (parser->tokens_avail == n - 1);
469 c_lex_one_token (parser, &parser->tokens[n - 1]);
470 parser->tokens_avail = n;
471 return &parser->tokens[n - 1];
474 bool
475 c_keyword_starts_typename (enum rid keyword)
477 switch (keyword)
479 case RID_UNSIGNED:
480 case RID_LONG:
481 case RID_SHORT:
482 case RID_SIGNED:
483 case RID_COMPLEX:
484 case RID_INT:
485 case RID_CHAR:
486 case RID_FLOAT:
487 case RID_DOUBLE:
488 case RID_VOID:
489 case RID_DFLOAT32:
490 case RID_DFLOAT64:
491 case RID_DFLOAT128:
492 CASE_RID_FLOATN_NX:
493 case RID_BOOL:
494 case RID_ENUM:
495 case RID_STRUCT:
496 case RID_UNION:
497 case RID_TYPEOF:
498 case RID_CONST:
499 case RID_ATOMIC:
500 case RID_VOLATILE:
501 case RID_RESTRICT:
502 case RID_ATTRIBUTE:
503 case RID_FRACT:
504 case RID_ACCUM:
505 case RID_SAT:
506 case RID_AUTO_TYPE:
507 case RID_ALIGNAS:
508 return true;
509 default:
510 if (keyword >= RID_FIRST_INT_N
511 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
512 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
513 return true;
514 return false;
518 /* Return true if TOKEN can start a type name,
519 false otherwise. */
520 bool
521 c_token_starts_typename (c_token *token)
523 switch (token->type)
525 case CPP_NAME:
526 switch (token->id_kind)
528 case C_ID_ID:
529 return false;
530 case C_ID_ADDRSPACE:
531 return true;
532 case C_ID_TYPENAME:
533 return true;
534 case C_ID_CLASSNAME:
535 gcc_assert (c_dialect_objc ());
536 return true;
537 default:
538 gcc_unreachable ();
540 case CPP_KEYWORD:
541 return c_keyword_starts_typename (token->keyword);
542 case CPP_LESS:
543 if (c_dialect_objc ())
544 return true;
545 return false;
546 default:
547 return false;
551 /* Return true if the next token from PARSER can start a type name,
552 false otherwise. LA specifies how to do lookahead in order to
553 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
555 static inline bool
556 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
558 c_token *token = c_parser_peek_token (parser);
559 if (c_token_starts_typename (token))
560 return true;
562 /* Try a bit harder to detect an unknown typename. */
563 if (la != cla_prefer_id
564 && token->type == CPP_NAME
565 && token->id_kind == C_ID_ID
567 /* Do not try too hard when we could have "object in array". */
568 && !parser->objc_could_be_foreach_context
570 && (la == cla_prefer_type
571 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
572 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
574 /* Only unknown identifiers. */
575 && !lookup_name (token->value))
576 return true;
578 return false;
581 /* Return true if TOKEN is a type qualifier, false otherwise. */
582 static bool
583 c_token_is_qualifier (c_token *token)
585 switch (token->type)
587 case CPP_NAME:
588 switch (token->id_kind)
590 case C_ID_ADDRSPACE:
591 return true;
592 default:
593 return false;
595 case CPP_KEYWORD:
596 switch (token->keyword)
598 case RID_CONST:
599 case RID_VOLATILE:
600 case RID_RESTRICT:
601 case RID_ATTRIBUTE:
602 case RID_ATOMIC:
603 return true;
604 default:
605 return false;
607 case CPP_LESS:
608 return false;
609 default:
610 gcc_unreachable ();
614 /* Return true if the next token from PARSER is a type qualifier,
615 false otherwise. */
616 static inline bool
617 c_parser_next_token_is_qualifier (c_parser *parser)
619 c_token *token = c_parser_peek_token (parser);
620 return c_token_is_qualifier (token);
623 /* Return true if TOKEN can start declaration specifiers, false
624 otherwise. */
625 static bool
626 c_token_starts_declspecs (c_token *token)
628 switch (token->type)
630 case CPP_NAME:
631 switch (token->id_kind)
633 case C_ID_ID:
634 return false;
635 case C_ID_ADDRSPACE:
636 return true;
637 case C_ID_TYPENAME:
638 return true;
639 case C_ID_CLASSNAME:
640 gcc_assert (c_dialect_objc ());
641 return true;
642 default:
643 gcc_unreachable ();
645 case CPP_KEYWORD:
646 switch (token->keyword)
648 case RID_STATIC:
649 case RID_EXTERN:
650 case RID_REGISTER:
651 case RID_TYPEDEF:
652 case RID_INLINE:
653 case RID_NORETURN:
654 case RID_AUTO:
655 case RID_THREAD:
656 case RID_UNSIGNED:
657 case RID_LONG:
658 case RID_SHORT:
659 case RID_SIGNED:
660 case RID_COMPLEX:
661 case RID_INT:
662 case RID_CHAR:
663 case RID_FLOAT:
664 case RID_DOUBLE:
665 case RID_VOID:
666 case RID_DFLOAT32:
667 case RID_DFLOAT64:
668 case RID_DFLOAT128:
669 CASE_RID_FLOATN_NX:
670 case RID_BOOL:
671 case RID_ENUM:
672 case RID_STRUCT:
673 case RID_UNION:
674 case RID_TYPEOF:
675 case RID_CONST:
676 case RID_VOLATILE:
677 case RID_RESTRICT:
678 case RID_ATTRIBUTE:
679 case RID_FRACT:
680 case RID_ACCUM:
681 case RID_SAT:
682 case RID_ALIGNAS:
683 case RID_ATOMIC:
684 case RID_AUTO_TYPE:
685 return true;
686 default:
687 if (token->keyword >= RID_FIRST_INT_N
688 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
689 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
690 return true;
691 return false;
693 case CPP_LESS:
694 if (c_dialect_objc ())
695 return true;
696 return false;
697 default:
698 return false;
703 /* Return true if TOKEN can start declaration specifiers or a static
704 assertion, false otherwise. */
705 static bool
706 c_token_starts_declaration (c_token *token)
708 if (c_token_starts_declspecs (token)
709 || token->keyword == RID_STATIC_ASSERT)
710 return true;
711 else
712 return false;
715 /* Return true if the next token from PARSER can start declaration
716 specifiers, false otherwise. */
717 bool
718 c_parser_next_token_starts_declspecs (c_parser *parser)
720 c_token *token = c_parser_peek_token (parser);
722 /* In Objective-C, a classname normally starts a declspecs unless it
723 is immediately followed by a dot. In that case, it is the
724 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
725 setter/getter on the class. c_token_starts_declspecs() can't
726 differentiate between the two cases because it only checks the
727 current token, so we have a special check here. */
728 if (c_dialect_objc ()
729 && token->type == CPP_NAME
730 && token->id_kind == C_ID_CLASSNAME
731 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
732 return false;
734 return c_token_starts_declspecs (token);
737 /* Return true if the next tokens from PARSER can start declaration
738 specifiers or a static assertion, false otherwise. */
739 bool
740 c_parser_next_tokens_start_declaration (c_parser *parser)
742 c_token *token = c_parser_peek_token (parser);
744 /* Same as above. */
745 if (c_dialect_objc ()
746 && token->type == CPP_NAME
747 && token->id_kind == C_ID_CLASSNAME
748 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
749 return false;
751 /* Labels do not start declarations. */
752 if (token->type == CPP_NAME
753 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
754 return false;
756 if (c_token_starts_declaration (token))
757 return true;
759 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
760 return true;
762 return false;
765 /* Consume the next token from PARSER. */
767 void
768 c_parser_consume_token (c_parser *parser)
770 gcc_assert (parser->tokens_avail >= 1);
771 gcc_assert (parser->tokens[0].type != CPP_EOF);
772 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
773 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
774 parser->last_token_location = parser->tokens[0].location;
775 if (parser->tokens != &parser->tokens_buf[0])
776 parser->tokens++;
777 else if (parser->tokens_avail == 2)
778 parser->tokens[0] = parser->tokens[1];
779 parser->tokens_avail--;
782 /* Expect the current token to be a #pragma. Consume it and remember
783 that we've begun parsing a pragma. */
785 static void
786 c_parser_consume_pragma (c_parser *parser)
788 gcc_assert (!parser->in_pragma);
789 gcc_assert (parser->tokens_avail >= 1);
790 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
791 if (parser->tokens != &parser->tokens_buf[0])
792 parser->tokens++;
793 else if (parser->tokens_avail == 2)
794 parser->tokens[0] = parser->tokens[1];
795 parser->tokens_avail--;
796 parser->in_pragma = true;
799 /* Update the global input_location from TOKEN. */
800 static inline void
801 c_parser_set_source_position_from_token (c_token *token)
803 if (token->type != CPP_EOF)
805 input_location = token->location;
809 /* Helper function for c_parser_error.
810 Having peeked a token of kind TOK1_KIND that might signify
811 a conflict marker, peek successor tokens to determine
812 if we actually do have a conflict marker.
813 Specifically, we consider a run of 7 '<', '=' or '>' characters
814 at the start of a line as a conflict marker.
815 These come through the lexer as three pairs and a single,
816 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
817 If it returns true, *OUT_LOC is written to with the location/range
818 of the marker. */
820 static bool
821 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
822 location_t *out_loc)
824 c_token *token2 = c_parser_peek_2nd_token (parser);
825 if (token2->type != tok1_kind)
826 return false;
827 c_token *token3 = c_parser_peek_nth_token (parser, 3);
828 if (token3->type != tok1_kind)
829 return false;
830 c_token *token4 = c_parser_peek_nth_token (parser, 4);
831 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
832 return false;
834 /* It must be at the start of the line. */
835 location_t start_loc = c_parser_peek_token (parser)->location;
836 if (LOCATION_COLUMN (start_loc) != 1)
837 return false;
839 /* We have a conflict marker. Construct a location of the form:
840 <<<<<<<
841 ^~~~~~~
842 with start == caret, finishing at the end of the marker. */
843 location_t finish_loc = get_finish (token4->location);
844 *out_loc = make_location (start_loc, start_loc, finish_loc);
846 return true;
849 /* Issue a diagnostic of the form
850 FILE:LINE: MESSAGE before TOKEN
851 where TOKEN is the next token in the input stream of PARSER.
852 MESSAGE (specified by the caller) is usually of the form "expected
853 OTHER-TOKEN".
855 Use RICHLOC as the location of the diagnostic.
857 Do not issue a diagnostic if still recovering from an error.
859 Return true iff an error was actually emitted.
861 ??? This is taken from the C++ parser, but building up messages in
862 this way is not i18n-friendly and some other approach should be
863 used. */
865 static bool
866 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
867 rich_location *richloc)
869 c_token *token = c_parser_peek_token (parser);
870 if (parser->error)
871 return false;
872 parser->error = true;
873 if (!gmsgid)
874 return false;
876 /* If this is actually a conflict marker, report it as such. */
877 if (token->type == CPP_LSHIFT
878 || token->type == CPP_RSHIFT
879 || token->type == CPP_EQ_EQ)
881 location_t loc;
882 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
884 error_at (loc, "version control conflict marker in file");
885 return true;
889 c_parse_error (gmsgid,
890 /* Because c_parse_error does not understand
891 CPP_KEYWORD, keywords are treated like
892 identifiers. */
893 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
894 /* ??? The C parser does not save the cpp flags of a
895 token, we need to pass 0 here and we will not get
896 the source spelling of some tokens but rather the
897 canonical spelling. */
898 token->value, /*flags=*/0, richloc);
899 return true;
902 /* As c_parser_error_richloc, but issue the message at the
903 location of PARSER's next token, or at input_location
904 if the next token is EOF. */
906 bool
907 c_parser_error (c_parser *parser, const char *gmsgid)
909 c_token *token = c_parser_peek_token (parser);
910 c_parser_set_source_position_from_token (token);
911 rich_location richloc (line_table, input_location);
912 return c_parser_error_richloc (parser, gmsgid, &richloc);
915 /* Some tokens naturally come in pairs e.g.'(' and ')'.
916 This class is for tracking such a matching pair of symbols.
917 In particular, it tracks the location of the first token,
918 so that if the second token is missing, we can highlight the
919 location of the first token when notifying the user about the
920 problem. */
922 template <typename traits_t>
923 class token_pair
925 public:
926 /* token_pair's ctor. */
927 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
929 /* If the next token is the opening symbol for this pair, consume it and
930 return true.
931 Otherwise, issue an error and return false.
932 In either case, record the location of the opening token. */
934 bool require_open (c_parser *parser)
936 c_token *token = c_parser_peek_token (parser);
937 if (token)
938 m_open_loc = token->location;
940 return c_parser_require (parser, traits_t::open_token_type,
941 traits_t::open_gmsgid);
944 /* Consume the next token from PARSER, recording its location as
945 that of the opening token within the pair. */
947 void consume_open (c_parser *parser)
949 c_token *token = c_parser_peek_token (parser);
950 gcc_assert (token->type == traits_t::open_token_type);
951 m_open_loc = token->location;
952 c_parser_consume_token (parser);
955 /* If the next token is the closing symbol for this pair, consume it
956 and return true.
957 Otherwise, issue an error, highlighting the location of the
958 corresponding opening token, and return false. */
960 bool require_close (c_parser *parser) const
962 return c_parser_require (parser, traits_t::close_token_type,
963 traits_t::close_gmsgid, m_open_loc);
966 /* Like token_pair::require_close, except that tokens will be skipped
967 until the desired token is found. An error message is still produced
968 if the next token is not as expected. */
970 void skip_until_found_close (c_parser *parser) const
972 c_parser_skip_until_found (parser, traits_t::close_token_type,
973 traits_t::close_gmsgid, m_open_loc);
976 private:
977 location_t m_open_loc;
980 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
982 struct matching_paren_traits
984 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
985 static const char * const open_gmsgid;
986 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
987 static const char * const close_gmsgid;
990 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
991 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
993 /* "matching_parens" is a token_pair<T> class for tracking matching
994 pairs of parentheses. */
996 typedef token_pair<matching_paren_traits> matching_parens;
998 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1000 struct matching_brace_traits
1002 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1003 static const char * const open_gmsgid;
1004 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1005 static const char * const close_gmsgid;
1008 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1009 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1011 /* "matching_braces" is a token_pair<T> class for tracking matching
1012 pairs of braces. */
1014 typedef token_pair<matching_brace_traits> matching_braces;
1016 /* Get a description of the matching symbol to TYPE e.g. "(" for
1017 CPP_CLOSE_PAREN. */
1019 static const char *
1020 get_matching_symbol (enum cpp_ttype type)
1022 switch (type)
1024 default:
1025 gcc_unreachable ();
1026 return "";
1027 case CPP_CLOSE_PAREN:
1028 return "(";
1029 case CPP_CLOSE_BRACE:
1030 return "{";
1034 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1035 issue the error MSGID. If MSGID is NULL then a message has already
1036 been produced and no message will be produced this time. Returns
1037 true if found, false otherwise.
1039 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1040 within any error as the location of an "opening" token matching
1041 the close token TYPE (e.g. the location of the '(' when TYPE is
1042 CPP_CLOSE_PAREN).
1044 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1045 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1046 attempt to generate a fix-it hint for the problem.
1047 Otherwise msgid describes multiple token types (e.g.
1048 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1049 generate a fix-it hint. */
1051 bool
1052 c_parser_require (c_parser *parser,
1053 enum cpp_ttype type,
1054 const char *msgid,
1055 location_t matching_location,
1056 bool type_is_unique)
1058 if (c_parser_next_token_is (parser, type))
1060 c_parser_consume_token (parser);
1061 return true;
1063 else
1065 location_t next_token_loc = c_parser_peek_token (parser)->location;
1066 gcc_rich_location richloc (next_token_loc);
1068 /* Potentially supply a fix-it hint, suggesting to add the
1069 missing token immediately after the *previous* token.
1070 This may move the primary location within richloc. */
1071 if (!parser->error && type_is_unique)
1072 maybe_suggest_missing_token_insertion (&richloc, type,
1073 parser->last_token_location);
1075 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1076 Attempt to consolidate diagnostics by printing it as a
1077 secondary range within the main diagnostic. */
1078 bool added_matching_location = false;
1079 if (matching_location != UNKNOWN_LOCATION)
1080 added_matching_location
1081 = richloc.add_location_if_nearby (matching_location);
1083 if (c_parser_error_richloc (parser, msgid, &richloc))
1084 /* If we weren't able to consolidate matching_location, then
1085 print it as a secondary diagnostic. */
1086 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1087 inform (matching_location, "to match this %qs",
1088 get_matching_symbol (type));
1090 return false;
1094 /* If the next token is the indicated keyword, consume it. Otherwise,
1095 issue the error MSGID. Returns true if found, false otherwise. */
1097 static bool
1098 c_parser_require_keyword (c_parser *parser,
1099 enum rid keyword,
1100 const char *msgid)
1102 if (c_parser_next_token_is_keyword (parser, keyword))
1104 c_parser_consume_token (parser);
1105 return true;
1107 else
1109 c_parser_error (parser, msgid);
1110 return false;
1114 /* Like c_parser_require, except that tokens will be skipped until the
1115 desired token is found. An error message is still produced if the
1116 next token is not as expected. If MSGID is NULL then a message has
1117 already been produced and no message will be produced this
1118 time.
1120 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1121 within any error as the location of an "opening" token matching
1122 the close token TYPE (e.g. the location of the '(' when TYPE is
1123 CPP_CLOSE_PAREN). */
1125 void
1126 c_parser_skip_until_found (c_parser *parser,
1127 enum cpp_ttype type,
1128 const char *msgid,
1129 location_t matching_location)
1131 unsigned nesting_depth = 0;
1133 if (c_parser_require (parser, type, msgid, matching_location))
1134 return;
1136 /* Skip tokens until the desired token is found. */
1137 while (true)
1139 /* Peek at the next token. */
1140 c_token *token = c_parser_peek_token (parser);
1141 /* If we've reached the token we want, consume it and stop. */
1142 if (token->type == type && !nesting_depth)
1144 c_parser_consume_token (parser);
1145 break;
1148 /* If we've run out of tokens, stop. */
1149 if (token->type == CPP_EOF)
1150 return;
1151 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1152 return;
1153 if (token->type == CPP_OPEN_BRACE
1154 || token->type == CPP_OPEN_PAREN
1155 || token->type == CPP_OPEN_SQUARE)
1156 ++nesting_depth;
1157 else if (token->type == CPP_CLOSE_BRACE
1158 || token->type == CPP_CLOSE_PAREN
1159 || token->type == CPP_CLOSE_SQUARE)
1161 if (nesting_depth-- == 0)
1162 break;
1164 /* Consume this token. */
1165 c_parser_consume_token (parser);
1167 parser->error = false;
1170 /* Skip tokens until the end of a parameter is found, but do not
1171 consume the comma, semicolon or closing delimiter. */
1173 static void
1174 c_parser_skip_to_end_of_parameter (c_parser *parser)
1176 unsigned nesting_depth = 0;
1178 while (true)
1180 c_token *token = c_parser_peek_token (parser);
1181 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1182 && !nesting_depth)
1183 break;
1184 /* If we've run out of tokens, stop. */
1185 if (token->type == CPP_EOF)
1186 return;
1187 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1188 return;
1189 if (token->type == CPP_OPEN_BRACE
1190 || token->type == CPP_OPEN_PAREN
1191 || token->type == CPP_OPEN_SQUARE)
1192 ++nesting_depth;
1193 else if (token->type == CPP_CLOSE_BRACE
1194 || token->type == CPP_CLOSE_PAREN
1195 || token->type == CPP_CLOSE_SQUARE)
1197 if (nesting_depth-- == 0)
1198 break;
1200 /* Consume this token. */
1201 c_parser_consume_token (parser);
1203 parser->error = false;
1206 /* Expect to be at the end of the pragma directive and consume an
1207 end of line marker. */
1209 static void
1210 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1212 gcc_assert (parser->in_pragma);
1213 parser->in_pragma = false;
1215 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1216 c_parser_error (parser, "expected end of line");
1218 cpp_ttype token_type;
1221 c_token *token = c_parser_peek_token (parser);
1222 token_type = token->type;
1223 if (token_type == CPP_EOF)
1224 break;
1225 c_parser_consume_token (parser);
1227 while (token_type != CPP_PRAGMA_EOL);
1229 parser->error = false;
1232 /* Skip tokens until we have consumed an entire block, or until we
1233 have consumed a non-nested ';'. */
1235 static void
1236 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1238 unsigned nesting_depth = 0;
1239 bool save_error = parser->error;
1241 while (true)
1243 c_token *token;
1245 /* Peek at the next token. */
1246 token = c_parser_peek_token (parser);
1248 switch (token->type)
1250 case CPP_EOF:
1251 return;
1253 case CPP_PRAGMA_EOL:
1254 if (parser->in_pragma)
1255 return;
1256 break;
1258 case CPP_SEMICOLON:
1259 /* If the next token is a ';', we have reached the
1260 end of the statement. */
1261 if (!nesting_depth)
1263 /* Consume the ';'. */
1264 c_parser_consume_token (parser);
1265 goto finished;
1267 break;
1269 case CPP_CLOSE_BRACE:
1270 /* If the next token is a non-nested '}', then we have
1271 reached the end of the current block. */
1272 if (nesting_depth == 0 || --nesting_depth == 0)
1274 c_parser_consume_token (parser);
1275 goto finished;
1277 break;
1279 case CPP_OPEN_BRACE:
1280 /* If it the next token is a '{', then we are entering a new
1281 block. Consume the entire block. */
1282 ++nesting_depth;
1283 break;
1285 case CPP_PRAGMA:
1286 /* If we see a pragma, consume the whole thing at once. We
1287 have some safeguards against consuming pragmas willy-nilly.
1288 Normally, we'd expect to be here with parser->error set,
1289 which disables these safeguards. But it's possible to get
1290 here for secondary error recovery, after parser->error has
1291 been cleared. */
1292 c_parser_consume_pragma (parser);
1293 c_parser_skip_to_pragma_eol (parser);
1294 parser->error = save_error;
1295 continue;
1297 default:
1298 break;
1301 c_parser_consume_token (parser);
1304 finished:
1305 parser->error = false;
1308 /* CPP's options (initialized by c-opts.c). */
1309 extern cpp_options *cpp_opts;
1311 /* Save the warning flags which are controlled by __extension__. */
1313 static inline int
1314 disable_extension_diagnostics (void)
1316 int ret = (pedantic
1317 | (warn_pointer_arith << 1)
1318 | (warn_traditional << 2)
1319 | (flag_iso << 3)
1320 | (warn_long_long << 4)
1321 | (warn_cxx_compat << 5)
1322 | (warn_overlength_strings << 6)
1323 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1324 play tricks to properly restore it. */
1325 | ((warn_c90_c99_compat == 1) << 7)
1326 | ((warn_c90_c99_compat == -1) << 8)
1327 /* Similarly for warn_c99_c11_compat. */
1328 | ((warn_c99_c11_compat == 1) << 9)
1329 | ((warn_c99_c11_compat == -1) << 10)
1331 cpp_opts->cpp_pedantic = pedantic = 0;
1332 warn_pointer_arith = 0;
1333 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1334 flag_iso = 0;
1335 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1336 warn_cxx_compat = 0;
1337 warn_overlength_strings = 0;
1338 warn_c90_c99_compat = 0;
1339 warn_c99_c11_compat = 0;
1340 return ret;
1343 /* Restore the warning flags which are controlled by __extension__.
1344 FLAGS is the return value from disable_extension_diagnostics. */
1346 static inline void
1347 restore_extension_diagnostics (int flags)
1349 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1350 warn_pointer_arith = (flags >> 1) & 1;
1351 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1352 flag_iso = (flags >> 3) & 1;
1353 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1354 warn_cxx_compat = (flags >> 5) & 1;
1355 warn_overlength_strings = (flags >> 6) & 1;
1356 /* See above for why is this needed. */
1357 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1358 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1361 /* Helper data structure for parsing #pragma acc routine. */
1362 struct oacc_routine_data {
1363 bool error_seen; /* Set if error has been reported. */
1364 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1365 tree clauses;
1366 location_t loc;
1369 static void c_parser_external_declaration (c_parser *);
1370 static void c_parser_asm_definition (c_parser *);
1371 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1372 bool, bool, tree *, vec<c_token>,
1373 struct oacc_routine_data * = NULL,
1374 bool * = NULL);
1375 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1376 static void c_parser_static_assert_declaration (c_parser *);
1377 static struct c_typespec c_parser_enum_specifier (c_parser *);
1378 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1379 static tree c_parser_struct_declaration (c_parser *);
1380 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1381 static tree c_parser_alignas_specifier (c_parser *);
1382 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1383 c_dtr_syn, bool *);
1384 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1385 bool,
1386 struct c_declarator *);
1387 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1388 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1389 tree);
1390 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1391 static tree c_parser_simple_asm_expr (c_parser *);
1392 static tree c_parser_attributes (c_parser *);
1393 static struct c_expr c_parser_initializer (c_parser *);
1394 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1395 struct obstack *);
1396 static void c_parser_initelt (c_parser *, struct obstack *);
1397 static void c_parser_initval (c_parser *, struct c_expr *,
1398 struct obstack *);
1399 static tree c_parser_compound_statement (c_parser *);
1400 static void c_parser_compound_statement_nostart (c_parser *);
1401 static void c_parser_label (c_parser *);
1402 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1403 static void c_parser_statement_after_labels (c_parser *, bool *,
1404 vec<tree> * = NULL);
1405 static tree c_parser_c99_block_statement (c_parser *, bool *,
1406 location_t * = NULL);
1407 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1408 static void c_parser_switch_statement (c_parser *, bool *);
1409 static void c_parser_while_statement (c_parser *, bool, unsigned short, bool *);
1410 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1411 static void c_parser_for_statement (c_parser *, bool, unsigned short, bool *);
1412 static tree c_parser_asm_statement (c_parser *);
1413 static tree c_parser_asm_operands (c_parser *);
1414 static tree c_parser_asm_goto_operands (c_parser *);
1415 static tree c_parser_asm_clobbers (c_parser *);
1416 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1417 tree = NULL_TREE);
1418 static struct c_expr c_parser_conditional_expression (c_parser *,
1419 struct c_expr *, tree);
1420 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1421 tree);
1422 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1423 static struct c_expr c_parser_unary_expression (c_parser *);
1424 static struct c_expr c_parser_sizeof_expression (c_parser *);
1425 static struct c_expr c_parser_alignof_expression (c_parser *);
1426 static struct c_expr c_parser_postfix_expression (c_parser *);
1427 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1428 struct c_type_name *,
1429 location_t);
1430 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1431 location_t loc,
1432 struct c_expr);
1433 static tree c_parser_transaction (c_parser *, enum rid);
1434 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1435 static tree c_parser_transaction_cancel (c_parser *);
1436 static struct c_expr c_parser_expression (c_parser *);
1437 static struct c_expr c_parser_expression_conv (c_parser *);
1438 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1439 vec<tree, va_gc> **, location_t *,
1440 tree *, vec<location_t> *,
1441 unsigned int * = NULL);
1442 static void c_parser_oacc_declare (c_parser *);
1443 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1444 static void c_parser_oacc_update (c_parser *);
1445 static void c_parser_omp_construct (c_parser *, bool *);
1446 static void c_parser_omp_threadprivate (c_parser *);
1447 static void c_parser_omp_barrier (c_parser *);
1448 static void c_parser_omp_flush (c_parser *);
1449 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1450 tree, tree *, bool *);
1451 static void c_parser_omp_taskwait (c_parser *);
1452 static void c_parser_omp_taskyield (c_parser *);
1453 static void c_parser_omp_cancel (c_parser *);
1455 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1456 pragma_stmt, pragma_compound };
1457 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1458 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1459 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1460 static void c_parser_omp_end_declare_target (c_parser *);
1461 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1462 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1463 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1465 /* These Objective-C parser functions are only ever called when
1466 compiling Objective-C. */
1467 static void c_parser_objc_class_definition (c_parser *, tree);
1468 static void c_parser_objc_class_instance_variables (c_parser *);
1469 static void c_parser_objc_class_declaration (c_parser *);
1470 static void c_parser_objc_alias_declaration (c_parser *);
1471 static void c_parser_objc_protocol_definition (c_parser *, tree);
1472 static bool c_parser_objc_method_type (c_parser *);
1473 static void c_parser_objc_method_definition (c_parser *);
1474 static void c_parser_objc_methodprotolist (c_parser *);
1475 static void c_parser_objc_methodproto (c_parser *);
1476 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1477 static tree c_parser_objc_type_name (c_parser *);
1478 static tree c_parser_objc_protocol_refs (c_parser *);
1479 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1480 static void c_parser_objc_synchronized_statement (c_parser *);
1481 static tree c_parser_objc_selector (c_parser *);
1482 static tree c_parser_objc_selector_arg (c_parser *);
1483 static tree c_parser_objc_receiver (c_parser *);
1484 static tree c_parser_objc_message_args (c_parser *);
1485 static tree c_parser_objc_keywordexpr (c_parser *);
1486 static void c_parser_objc_at_property_declaration (c_parser *);
1487 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1488 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1489 static bool c_parser_objc_diagnose_bad_element_prefix
1490 (c_parser *, struct c_declspecs *);
1492 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1494 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1496 translation-unit:
1497 external-declarations
1499 external-declarations:
1500 external-declaration
1501 external-declarations external-declaration
1503 GNU extensions:
1505 translation-unit:
1506 empty
1509 static void
1510 c_parser_translation_unit (c_parser *parser)
1512 if (c_parser_next_token_is (parser, CPP_EOF))
1514 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1515 "ISO C forbids an empty translation unit");
1517 else
1519 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1520 mark_valid_location_for_stdc_pragma (false);
1523 ggc_collect ();
1524 c_parser_external_declaration (parser);
1525 obstack_free (&parser_obstack, obstack_position);
1527 while (c_parser_next_token_is_not (parser, CPP_EOF));
1530 unsigned int i;
1531 tree decl;
1532 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1533 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1534 error ("storage size of %q+D isn%'t known", decl);
1537 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1539 external-declaration:
1540 function-definition
1541 declaration
1543 GNU extensions:
1545 external-declaration:
1546 asm-definition
1548 __extension__ external-declaration
1550 Objective-C:
1552 external-declaration:
1553 objc-class-definition
1554 objc-class-declaration
1555 objc-alias-declaration
1556 objc-protocol-definition
1557 objc-method-definition
1558 @end
1561 static void
1562 c_parser_external_declaration (c_parser *parser)
1564 int ext;
1565 switch (c_parser_peek_token (parser)->type)
1567 case CPP_KEYWORD:
1568 switch (c_parser_peek_token (parser)->keyword)
1570 case RID_EXTENSION:
1571 ext = disable_extension_diagnostics ();
1572 c_parser_consume_token (parser);
1573 c_parser_external_declaration (parser);
1574 restore_extension_diagnostics (ext);
1575 break;
1576 case RID_ASM:
1577 c_parser_asm_definition (parser);
1578 break;
1579 case RID_AT_INTERFACE:
1580 case RID_AT_IMPLEMENTATION:
1581 gcc_assert (c_dialect_objc ());
1582 c_parser_objc_class_definition (parser, NULL_TREE);
1583 break;
1584 case RID_AT_CLASS:
1585 gcc_assert (c_dialect_objc ());
1586 c_parser_objc_class_declaration (parser);
1587 break;
1588 case RID_AT_ALIAS:
1589 gcc_assert (c_dialect_objc ());
1590 c_parser_objc_alias_declaration (parser);
1591 break;
1592 case RID_AT_PROTOCOL:
1593 gcc_assert (c_dialect_objc ());
1594 c_parser_objc_protocol_definition (parser, NULL_TREE);
1595 break;
1596 case RID_AT_PROPERTY:
1597 gcc_assert (c_dialect_objc ());
1598 c_parser_objc_at_property_declaration (parser);
1599 break;
1600 case RID_AT_SYNTHESIZE:
1601 gcc_assert (c_dialect_objc ());
1602 c_parser_objc_at_synthesize_declaration (parser);
1603 break;
1604 case RID_AT_DYNAMIC:
1605 gcc_assert (c_dialect_objc ());
1606 c_parser_objc_at_dynamic_declaration (parser);
1607 break;
1608 case RID_AT_END:
1609 gcc_assert (c_dialect_objc ());
1610 c_parser_consume_token (parser);
1611 objc_finish_implementation ();
1612 break;
1613 default:
1614 goto decl_or_fndef;
1616 break;
1617 case CPP_SEMICOLON:
1618 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1619 "ISO C does not allow extra %<;%> outside of a function");
1620 c_parser_consume_token (parser);
1621 break;
1622 case CPP_PRAGMA:
1623 mark_valid_location_for_stdc_pragma (true);
1624 c_parser_pragma (parser, pragma_external, NULL);
1625 mark_valid_location_for_stdc_pragma (false);
1626 break;
1627 case CPP_PLUS:
1628 case CPP_MINUS:
1629 if (c_dialect_objc ())
1631 c_parser_objc_method_definition (parser);
1632 break;
1634 /* Else fall through, and yield a syntax error trying to parse
1635 as a declaration or function definition. */
1636 /* FALLTHRU */
1637 default:
1638 decl_or_fndef:
1639 /* A declaration or a function definition (or, in Objective-C,
1640 an @interface or @protocol with prefix attributes). We can
1641 only tell which after parsing the declaration specifiers, if
1642 any, and the first declarator. */
1643 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1644 NULL, vNULL);
1645 break;
1649 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1650 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1652 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
1654 static void
1655 add_debug_begin_stmt (location_t loc)
1657 /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721. */
1658 if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1659 return;
1661 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1662 SET_EXPR_LOCATION (stmt, loc);
1663 add_stmt (stmt);
1666 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1667 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1668 is accepted; otherwise (old-style parameter declarations) only other
1669 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1670 assertion is accepted; otherwise (old-style parameter declarations)
1671 it is not. If NESTED is true, we are inside a function or parsing
1672 old-style parameter declarations; any functions encountered are
1673 nested functions and declaration specifiers are required; otherwise
1674 we are at top level and functions are normal functions and
1675 declaration specifiers may be optional. If EMPTY_OK is true, empty
1676 declarations are OK (subject to all other constraints); otherwise
1677 (old-style parameter declarations) they are diagnosed. If
1678 START_ATTR_OK is true, the declaration specifiers may start with
1679 attributes; otherwise they may not.
1680 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1681 declaration when parsing an Objective-C foreach statement.
1682 FALLTHRU_ATTR_P is used to signal whether this function parsed
1683 "__attribute__((fallthrough));".
1685 declaration:
1686 declaration-specifiers init-declarator-list[opt] ;
1687 static_assert-declaration
1689 function-definition:
1690 declaration-specifiers[opt] declarator declaration-list[opt]
1691 compound-statement
1693 declaration-list:
1694 declaration
1695 declaration-list declaration
1697 init-declarator-list:
1698 init-declarator
1699 init-declarator-list , init-declarator
1701 init-declarator:
1702 declarator simple-asm-expr[opt] attributes[opt]
1703 declarator simple-asm-expr[opt] attributes[opt] = initializer
1705 GNU extensions:
1707 nested-function-definition:
1708 declaration-specifiers declarator declaration-list[opt]
1709 compound-statement
1711 attribute ;
1713 Objective-C:
1714 attributes objc-class-definition
1715 attributes objc-category-definition
1716 attributes objc-protocol-definition
1718 The simple-asm-expr and attributes are GNU extensions.
1720 This function does not handle __extension__; that is handled in its
1721 callers. ??? Following the old parser, __extension__ may start
1722 external declarations, declarations in functions and declarations
1723 at the start of "for" loops, but not old-style parameter
1724 declarations.
1726 C99 requires declaration specifiers in a function definition; the
1727 absence is diagnosed through the diagnosis of implicit int. In GNU
1728 C we also allow but diagnose declarations without declaration
1729 specifiers, but only at top level (elsewhere they conflict with
1730 other syntax).
1732 In Objective-C, declarations of the looping variable in a foreach
1733 statement are exceptionally terminated by 'in' (for example, 'for
1734 (NSObject *object in array) { ... }').
1736 OpenMP:
1738 declaration:
1739 threadprivate-directive
1741 GIMPLE:
1743 gimple-function-definition:
1744 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1745 declaration-list[opt] compound-statement
1747 rtl-function-definition:
1748 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1749 declaration-list[opt] compound-statement */
1751 static void
1752 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1753 bool static_assert_ok, bool empty_ok,
1754 bool nested, bool start_attr_ok,
1755 tree *objc_foreach_object_declaration,
1756 vec<c_token> omp_declare_simd_clauses,
1757 struct oacc_routine_data *oacc_routine_data,
1758 bool *fallthru_attr_p)
1760 struct c_declspecs *specs;
1761 tree prefix_attrs;
1762 tree all_prefix_attrs;
1763 bool diagnosed_no_specs = false;
1764 location_t here = c_parser_peek_token (parser)->location;
1766 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1768 if (static_assert_ok
1769 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1771 c_parser_static_assert_declaration (parser);
1772 return;
1774 specs = build_null_declspecs ();
1776 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1777 if (c_parser_peek_token (parser)->type == CPP_NAME
1778 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1779 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1780 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1781 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1783 tree name = c_parser_peek_token (parser)->value;
1785 /* Issue a warning about NAME being an unknown type name, perhaps
1786 with some kind of hint.
1787 If the user forgot a "struct" etc, suggest inserting
1788 it. Otherwise, attempt to look for misspellings. */
1789 gcc_rich_location richloc (here);
1790 if (tag_exists_p (RECORD_TYPE, name))
1792 /* This is not C++ with its implicit typedef. */
1793 richloc.add_fixit_insert_before ("struct ");
1794 error_at (&richloc,
1795 "unknown type name %qE;"
1796 " use %<struct%> keyword to refer to the type",
1797 name);
1799 else if (tag_exists_p (UNION_TYPE, name))
1801 richloc.add_fixit_insert_before ("union ");
1802 error_at (&richloc,
1803 "unknown type name %qE;"
1804 " use %<union%> keyword to refer to the type",
1805 name);
1807 else if (tag_exists_p (ENUMERAL_TYPE, name))
1809 richloc.add_fixit_insert_before ("enum ");
1810 error_at (&richloc,
1811 "unknown type name %qE;"
1812 " use %<enum%> keyword to refer to the type",
1813 name);
1815 else
1817 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1818 here);
1819 if (hint)
1821 richloc.add_fixit_replace (hint.suggestion ());
1822 error_at (&richloc,
1823 "unknown type name %qE; did you mean %qs?",
1824 name, hint.suggestion ());
1826 else
1827 error_at (here, "unknown type name %qE", name);
1830 /* Parse declspecs normally to get a correct pointer type, but avoid
1831 a further "fails to be a type name" error. Refuse nested functions
1832 since it is not how the user likely wants us to recover. */
1833 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1834 c_parser_peek_token (parser)->keyword = RID_VOID;
1835 c_parser_peek_token (parser)->value = error_mark_node;
1836 fndef_ok = !nested;
1839 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1840 true, true, cla_nonabstract_decl);
1841 if (parser->error)
1843 c_parser_skip_to_end_of_block_or_statement (parser);
1844 return;
1846 if (nested && !specs->declspecs_seen_p)
1848 c_parser_error (parser, "expected declaration specifiers");
1849 c_parser_skip_to_end_of_block_or_statement (parser);
1850 return;
1853 finish_declspecs (specs);
1854 bool auto_type_p = specs->typespec_word == cts_auto_type;
1855 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1857 if (auto_type_p)
1858 error_at (here, "%<__auto_type%> in empty declaration");
1859 else if (specs->typespec_kind == ctsk_none
1860 && attribute_fallthrough_p (specs->attrs))
1862 if (fallthru_attr_p != NULL)
1863 *fallthru_attr_p = true;
1864 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1865 void_type_node, 0);
1866 add_stmt (fn);
1868 else if (empty_ok)
1869 shadow_tag (specs);
1870 else
1872 shadow_tag_warned (specs, 1);
1873 pedwarn (here, 0, "empty declaration");
1875 c_parser_consume_token (parser);
1876 if (oacc_routine_data)
1877 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1878 return;
1881 /* Provide better error recovery. Note that a type name here is usually
1882 better diagnosed as a redeclaration. */
1883 if (empty_ok
1884 && specs->typespec_kind == ctsk_tagdef
1885 && c_parser_next_token_starts_declspecs (parser)
1886 && !c_parser_next_token_is (parser, CPP_NAME))
1888 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1889 parser->error = false;
1890 shadow_tag_warned (specs, 1);
1891 return;
1893 else if (c_dialect_objc () && !auto_type_p)
1895 /* Prefix attributes are an error on method decls. */
1896 switch (c_parser_peek_token (parser)->type)
1898 case CPP_PLUS:
1899 case CPP_MINUS:
1900 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1901 return;
1902 if (specs->attrs)
1904 warning_at (c_parser_peek_token (parser)->location,
1905 OPT_Wattributes,
1906 "prefix attributes are ignored for methods");
1907 specs->attrs = NULL_TREE;
1909 if (fndef_ok)
1910 c_parser_objc_method_definition (parser);
1911 else
1912 c_parser_objc_methodproto (parser);
1913 return;
1914 break;
1915 default:
1916 break;
1918 /* This is where we parse 'attributes @interface ...',
1919 'attributes @implementation ...', 'attributes @protocol ...'
1920 (where attributes could be, for example, __attribute__
1921 ((deprecated)).
1923 switch (c_parser_peek_token (parser)->keyword)
1925 case RID_AT_INTERFACE:
1927 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1928 return;
1929 c_parser_objc_class_definition (parser, specs->attrs);
1930 return;
1932 break;
1933 case RID_AT_IMPLEMENTATION:
1935 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1936 return;
1937 if (specs->attrs)
1939 warning_at (c_parser_peek_token (parser)->location,
1940 OPT_Wattributes,
1941 "prefix attributes are ignored for implementations");
1942 specs->attrs = NULL_TREE;
1944 c_parser_objc_class_definition (parser, NULL_TREE);
1945 return;
1947 break;
1948 case RID_AT_PROTOCOL:
1950 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1951 return;
1952 c_parser_objc_protocol_definition (parser, specs->attrs);
1953 return;
1955 break;
1956 case RID_AT_ALIAS:
1957 case RID_AT_CLASS:
1958 case RID_AT_END:
1959 case RID_AT_PROPERTY:
1960 if (specs->attrs)
1962 c_parser_error (parser, "unexpected attribute");
1963 specs->attrs = NULL;
1965 break;
1966 default:
1967 break;
1970 else if (attribute_fallthrough_p (specs->attrs))
1971 warning_at (here, OPT_Wattributes,
1972 "%<fallthrough%> attribute not followed by %<;%>");
1974 pending_xref_error ();
1975 prefix_attrs = specs->attrs;
1976 all_prefix_attrs = prefix_attrs;
1977 specs->attrs = NULL_TREE;
1978 while (true)
1980 struct c_declarator *declarator;
1981 bool dummy = false;
1982 timevar_id_t tv;
1983 tree fnbody = NULL_TREE;
1984 /* Declaring either one or more declarators (in which case we
1985 should diagnose if there were no declaration specifiers) or a
1986 function definition (in which case the diagnostic for
1987 implicit int suffices). */
1988 declarator = c_parser_declarator (parser,
1989 specs->typespec_kind != ctsk_none,
1990 C_DTR_NORMAL, &dummy);
1991 if (declarator == NULL)
1993 if (omp_declare_simd_clauses.exists ())
1994 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1995 omp_declare_simd_clauses);
1996 if (oacc_routine_data)
1997 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1998 c_parser_skip_to_end_of_block_or_statement (parser);
1999 return;
2001 if (auto_type_p && declarator->kind != cdk_id)
2003 error_at (here,
2004 "%<__auto_type%> requires a plain identifier"
2005 " as declarator");
2006 c_parser_skip_to_end_of_block_or_statement (parser);
2007 return;
2009 if (c_parser_next_token_is (parser, CPP_EQ)
2010 || c_parser_next_token_is (parser, CPP_COMMA)
2011 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2012 || c_parser_next_token_is_keyword (parser, RID_ASM)
2013 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2014 || c_parser_next_token_is_keyword (parser, RID_IN))
2016 tree asm_name = NULL_TREE;
2017 tree postfix_attrs = NULL_TREE;
2018 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2020 diagnosed_no_specs = true;
2021 pedwarn (here, 0, "data definition has no type or storage class");
2023 /* Having seen a data definition, there cannot now be a
2024 function definition. */
2025 fndef_ok = false;
2026 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2027 asm_name = c_parser_simple_asm_expr (parser);
2028 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2030 postfix_attrs = c_parser_attributes (parser);
2031 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2033 /* This means there is an attribute specifier after
2034 the declarator in a function definition. Provide
2035 some more information for the user. */
2036 error_at (here, "attributes should be specified before the "
2037 "declarator in a function definition");
2038 c_parser_skip_to_end_of_block_or_statement (parser);
2039 return;
2042 if (c_parser_next_token_is (parser, CPP_EQ))
2044 tree d;
2045 struct c_expr init;
2046 location_t init_loc;
2047 c_parser_consume_token (parser);
2048 if (auto_type_p)
2050 init_loc = c_parser_peek_token (parser)->location;
2051 rich_location richloc (line_table, init_loc);
2052 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2053 /* A parameter is initialized, which is invalid. Don't
2054 attempt to instrument the initializer. */
2055 int flag_sanitize_save = flag_sanitize;
2056 if (nested && !empty_ok)
2057 flag_sanitize = 0;
2058 init = c_parser_expr_no_commas (parser, NULL);
2059 flag_sanitize = flag_sanitize_save;
2060 if (TREE_CODE (init.value) == COMPONENT_REF
2061 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2062 error_at (here,
2063 "%<__auto_type%> used with a bit-field"
2064 " initializer");
2065 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2066 tree init_type = TREE_TYPE (init.value);
2067 /* As with typeof, remove all qualifiers from atomic types. */
2068 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2069 init_type
2070 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2071 bool vm_type = variably_modified_type_p (init_type,
2072 NULL_TREE);
2073 if (vm_type)
2074 init.value = save_expr (init.value);
2075 finish_init ();
2076 specs->typespec_kind = ctsk_typeof;
2077 specs->locations[cdw_typedef] = init_loc;
2078 specs->typedef_p = true;
2079 specs->type = init_type;
2080 if (vm_type)
2082 bool maybe_const = true;
2083 tree type_expr = c_fully_fold (init.value, false,
2084 &maybe_const);
2085 specs->expr_const_operands &= maybe_const;
2086 if (specs->expr)
2087 specs->expr = build2 (COMPOUND_EXPR,
2088 TREE_TYPE (type_expr),
2089 specs->expr, type_expr);
2090 else
2091 specs->expr = type_expr;
2093 d = start_decl (declarator, specs, true,
2094 chainon (postfix_attrs, all_prefix_attrs));
2095 if (!d)
2096 d = error_mark_node;
2097 if (omp_declare_simd_clauses.exists ())
2098 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2099 omp_declare_simd_clauses);
2101 else
2103 /* The declaration of the variable is in effect while
2104 its initializer is parsed. */
2105 d = start_decl (declarator, specs, true,
2106 chainon (postfix_attrs, all_prefix_attrs));
2107 if (!d)
2108 d = error_mark_node;
2109 if (omp_declare_simd_clauses.exists ())
2110 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2111 omp_declare_simd_clauses);
2112 init_loc = c_parser_peek_token (parser)->location;
2113 rich_location richloc (line_table, init_loc);
2114 start_init (d, asm_name, global_bindings_p (), &richloc);
2115 /* A parameter is initialized, which is invalid. Don't
2116 attempt to instrument the initializer. */
2117 int flag_sanitize_save = flag_sanitize;
2118 if (TREE_CODE (d) == PARM_DECL)
2119 flag_sanitize = 0;
2120 init = c_parser_initializer (parser);
2121 flag_sanitize = flag_sanitize_save;
2122 finish_init ();
2124 if (oacc_routine_data)
2125 c_finish_oacc_routine (oacc_routine_data, d, false);
2126 if (d != error_mark_node)
2128 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2130 /* Try to convert a string CONSTRUCTOR into a STRING_CST. */
2131 tree valtype = TREE_TYPE (init.value);
2132 if (TREE_CODE (init.value) == CONSTRUCTOR
2133 && TREE_CODE (valtype) == ARRAY_TYPE
2134 && TYPE_STRING_FLAG (TREE_TYPE (valtype)))
2135 if (tree str = braced_list_to_string (valtype, init.value))
2136 init.value = str;
2138 finish_decl (d, init_loc, init.value,
2139 init.original_type, asm_name);
2142 else
2144 if (auto_type_p)
2146 error_at (here,
2147 "%<__auto_type%> requires an initialized "
2148 "data declaration");
2149 c_parser_skip_to_end_of_block_or_statement (parser);
2150 return;
2152 tree d = start_decl (declarator, specs, false,
2153 chainon (postfix_attrs,
2154 all_prefix_attrs));
2155 if (d && TREE_CODE (d) == FUNCTION_DECL)
2156 if (declarator->kind == cdk_function)
2157 if (DECL_ARGUMENTS (d) == NULL_TREE)
2158 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2159 if (omp_declare_simd_clauses.exists ())
2161 tree parms = NULL_TREE;
2162 if (d && TREE_CODE (d) == FUNCTION_DECL)
2164 struct c_declarator *ce = declarator;
2165 while (ce != NULL)
2166 if (ce->kind == cdk_function)
2168 parms = ce->u.arg_info->parms;
2169 break;
2171 else
2172 ce = ce->declarator;
2174 if (parms)
2175 temp_store_parm_decls (d, parms);
2176 c_finish_omp_declare_simd (parser, d, parms,
2177 omp_declare_simd_clauses);
2178 if (parms)
2179 temp_pop_parm_decls ();
2181 if (oacc_routine_data)
2182 c_finish_oacc_routine (oacc_routine_data, d, false);
2183 if (d)
2184 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2185 NULL_TREE, asm_name);
2187 if (c_parser_next_token_is_keyword (parser, RID_IN))
2189 if (d)
2190 *objc_foreach_object_declaration = d;
2191 else
2192 *objc_foreach_object_declaration = error_mark_node;
2195 if (c_parser_next_token_is (parser, CPP_COMMA))
2197 if (auto_type_p)
2199 error_at (here,
2200 "%<__auto_type%> may only be used with"
2201 " a single declarator");
2202 c_parser_skip_to_end_of_block_or_statement (parser);
2203 return;
2205 c_parser_consume_token (parser);
2206 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2207 all_prefix_attrs = chainon (c_parser_attributes (parser),
2208 prefix_attrs);
2209 else
2210 all_prefix_attrs = prefix_attrs;
2211 continue;
2213 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2215 c_parser_consume_token (parser);
2216 return;
2218 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2220 /* This can only happen in Objective-C: we found the
2221 'in' that terminates the declaration inside an
2222 Objective-C foreach statement. Do not consume the
2223 token, so that the caller can use it to determine
2224 that this indeed is a foreach context. */
2225 return;
2227 else
2229 c_parser_error (parser, "expected %<,%> or %<;%>");
2230 c_parser_skip_to_end_of_block_or_statement (parser);
2231 return;
2234 else if (auto_type_p)
2236 error_at (here,
2237 "%<__auto_type%> requires an initialized data declaration");
2238 c_parser_skip_to_end_of_block_or_statement (parser);
2239 return;
2241 else if (!fndef_ok)
2243 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2244 "%<asm%> or %<__attribute__%>");
2245 c_parser_skip_to_end_of_block_or_statement (parser);
2246 return;
2248 /* Function definition (nested or otherwise). */
2249 if (nested)
2251 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2252 c_push_function_context ();
2254 if (!start_function (specs, declarator, all_prefix_attrs))
2256 /* At this point we've consumed:
2257 declaration-specifiers declarator
2258 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2259 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2260 but the
2261 declaration-specifiers declarator
2262 aren't grokkable as a function definition, so we have
2263 an error. */
2264 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2265 if (c_parser_next_token_starts_declspecs (parser))
2267 /* If we have
2268 declaration-specifiers declarator decl-specs
2269 then assume we have a missing semicolon, which would
2270 give us:
2271 declaration-specifiers declarator decl-specs
2274 <~~~~~~~~~ declaration ~~~~~~~~~~>
2275 Use c_parser_require to get an error with a fix-it hint. */
2276 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2277 parser->error = false;
2279 else
2281 /* This can appear in many cases looking nothing like a
2282 function definition, so we don't give a more specific
2283 error suggesting there was one. */
2284 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2285 "or %<__attribute__%>");
2287 if (nested)
2288 c_pop_function_context ();
2289 break;
2292 if (DECL_DECLARED_INLINE_P (current_function_decl))
2293 tv = TV_PARSE_INLINE;
2294 else
2295 tv = TV_PARSE_FUNC;
2296 auto_timevar at (g_timer, tv);
2298 /* Parse old-style parameter declarations. ??? Attributes are
2299 not allowed to start declaration specifiers here because of a
2300 syntax conflict between a function declaration with attribute
2301 suffix and a function definition with an attribute prefix on
2302 first old-style parameter declaration. Following the old
2303 parser, they are not accepted on subsequent old-style
2304 parameter declarations either. However, there is no
2305 ambiguity after the first declaration, nor indeed on the
2306 first as long as we don't allow postfix attributes after a
2307 declarator with a nonempty identifier list in a definition;
2308 and postfix attributes have never been accepted here in
2309 function definitions either. */
2310 while (c_parser_next_token_is_not (parser, CPP_EOF)
2311 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2312 c_parser_declaration_or_fndef (parser, false, false, false,
2313 true, false, NULL, vNULL);
2314 store_parm_decls ();
2315 if (omp_declare_simd_clauses.exists ())
2316 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2317 omp_declare_simd_clauses);
2318 if (oacc_routine_data)
2319 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2320 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2321 = c_parser_peek_token (parser)->location;
2323 /* If the definition was marked with __GIMPLE then parse the
2324 function body as GIMPLE. */
2325 if (specs->gimple_p)
2327 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2328 bool saved = in_late_binary_op;
2329 in_late_binary_op = true;
2330 c_parser_parse_gimple_body (parser);
2331 in_late_binary_op = saved;
2333 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2334 consuming the function body. */
2335 else if (specs->rtl_p)
2337 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2339 /* Normally, store_parm_decls sets next_is_function_body,
2340 anticipating a function body. We need a push_scope/pop_scope
2341 pair to flush out this state, or subsequent function parsing
2342 will go wrong. */
2343 push_scope ();
2344 pop_scope ();
2346 finish_function ();
2347 return;
2349 else
2350 fnbody = c_parser_compound_statement (parser);
2351 tree fndecl = current_function_decl;
2352 if (nested)
2354 tree decl = current_function_decl;
2355 /* Mark nested functions as needing static-chain initially.
2356 lower_nested_functions will recompute it but the
2357 DECL_STATIC_CHAIN flag is also used before that happens,
2358 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2359 DECL_STATIC_CHAIN (decl) = 1;
2360 add_stmt (fnbody);
2361 finish_function ();
2362 c_pop_function_context ();
2363 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2365 else
2367 if (fnbody)
2368 add_stmt (fnbody);
2369 finish_function ();
2371 /* Get rid of the empty stmt list for GIMPLE. */
2372 if (specs->gimple_p)
2373 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2375 break;
2379 /* Parse an asm-definition (asm() outside a function body). This is a
2380 GNU extension.
2382 asm-definition:
2383 simple-asm-expr ;
2386 static void
2387 c_parser_asm_definition (c_parser *parser)
2389 tree asm_str = c_parser_simple_asm_expr (parser);
2390 if (asm_str)
2391 symtab->finalize_toplevel_asm (asm_str);
2392 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2395 /* Parse a static assertion (C11 6.7.10).
2397 static_assert-declaration:
2398 static_assert-declaration-no-semi ;
2401 static void
2402 c_parser_static_assert_declaration (c_parser *parser)
2404 c_parser_static_assert_declaration_no_semi (parser);
2405 if (parser->error
2406 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2407 c_parser_skip_to_end_of_block_or_statement (parser);
2410 /* Parse a static assertion (C11 6.7.10), without the trailing
2411 semicolon.
2413 static_assert-declaration-no-semi:
2414 _Static_assert ( constant-expression , string-literal )
2417 static void
2418 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2420 location_t assert_loc, value_loc;
2421 tree value;
2422 tree string;
2424 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2425 assert_loc = c_parser_peek_token (parser)->location;
2426 if (flag_isoc99)
2427 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2428 "ISO C99 does not support %<_Static_assert%>");
2429 else
2430 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2431 "ISO C90 does not support %<_Static_assert%>");
2432 c_parser_consume_token (parser);
2433 matching_parens parens;
2434 if (!parens.require_open (parser))
2435 return;
2436 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2437 value = c_parser_expr_no_commas (parser, NULL).value;
2438 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2439 parser->lex_untranslated_string = true;
2440 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2442 parser->lex_untranslated_string = false;
2443 return;
2445 switch (c_parser_peek_token (parser)->type)
2447 case CPP_STRING:
2448 case CPP_STRING16:
2449 case CPP_STRING32:
2450 case CPP_WSTRING:
2451 case CPP_UTF8STRING:
2452 string = c_parser_peek_token (parser)->value;
2453 c_parser_consume_token (parser);
2454 parser->lex_untranslated_string = false;
2455 break;
2456 default:
2457 c_parser_error (parser, "expected string literal");
2458 parser->lex_untranslated_string = false;
2459 return;
2461 parens.require_close (parser);
2463 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2465 error_at (value_loc, "expression in static assertion is not an integer");
2466 return;
2468 if (TREE_CODE (value) != INTEGER_CST)
2470 value = c_fully_fold (value, false, NULL);
2471 /* Strip no-op conversions. */
2472 STRIP_TYPE_NOPS (value);
2473 if (TREE_CODE (value) == INTEGER_CST)
2474 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2475 "is not an integer constant expression");
2477 if (TREE_CODE (value) != INTEGER_CST)
2479 error_at (value_loc, "expression in static assertion is not constant");
2480 return;
2482 constant_expression_warning (value);
2483 if (integer_zerop (value))
2484 error_at (assert_loc, "static assertion failed: %E", string);
2487 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2488 6.7, C11 6.7), adding them to SPECS (which may already include some).
2489 Storage class specifiers are accepted iff SCSPEC_OK; type
2490 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2491 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2492 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2494 declaration-specifiers:
2495 storage-class-specifier declaration-specifiers[opt]
2496 type-specifier declaration-specifiers[opt]
2497 type-qualifier declaration-specifiers[opt]
2498 function-specifier declaration-specifiers[opt]
2499 alignment-specifier declaration-specifiers[opt]
2501 Function specifiers (inline) are from C99, and are currently
2502 handled as storage class specifiers, as is __thread. Alignment
2503 specifiers are from C11.
2505 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2506 storage-class-specifier:
2507 typedef
2508 extern
2509 static
2510 auto
2511 register
2512 _Thread_local
2514 (_Thread_local is new in C11.)
2516 C99 6.7.4, C11 6.7.4:
2517 function-specifier:
2518 inline
2519 _Noreturn
2521 (_Noreturn is new in C11.)
2523 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2524 type-specifier:
2525 void
2526 char
2527 short
2529 long
2530 float
2531 double
2532 signed
2533 unsigned
2534 _Bool
2535 _Complex
2536 [_Imaginary removed in C99 TC2]
2537 struct-or-union-specifier
2538 enum-specifier
2539 typedef-name
2540 atomic-type-specifier
2542 (_Bool and _Complex are new in C99.)
2543 (atomic-type-specifier is new in C11.)
2545 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2547 type-qualifier:
2548 const
2549 restrict
2550 volatile
2551 address-space-qualifier
2552 _Atomic
2554 (restrict is new in C99.)
2555 (_Atomic is new in C11.)
2557 GNU extensions:
2559 declaration-specifiers:
2560 attributes declaration-specifiers[opt]
2562 type-qualifier:
2563 address-space
2565 address-space:
2566 identifier recognized by the target
2568 storage-class-specifier:
2569 __thread
2571 type-specifier:
2572 typeof-specifier
2573 __auto_type
2574 __intN
2575 _Decimal32
2576 _Decimal64
2577 _Decimal128
2578 _Fract
2579 _Accum
2580 _Sat
2582 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2583 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2585 atomic-type-specifier
2586 _Atomic ( type-name )
2588 Objective-C:
2590 type-specifier:
2591 class-name objc-protocol-refs[opt]
2592 typedef-name objc-protocol-refs
2593 objc-protocol-refs
2596 void
2597 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2598 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2599 bool alignspec_ok, bool auto_type_ok,
2600 enum c_lookahead_kind la)
2602 bool attrs_ok = start_attr_ok;
2603 bool seen_type = specs->typespec_kind != ctsk_none;
2605 if (!typespec_ok)
2606 gcc_assert (la == cla_prefer_id);
2608 while (c_parser_next_token_is (parser, CPP_NAME)
2609 || c_parser_next_token_is (parser, CPP_KEYWORD)
2610 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2612 struct c_typespec t;
2613 tree attrs;
2614 tree align;
2615 location_t loc = c_parser_peek_token (parser)->location;
2617 /* If we cannot accept a type, exit if the next token must start
2618 one. Also, if we already have seen a tagged definition,
2619 a typename would be an error anyway and likely the user
2620 has simply forgotten a semicolon, so we exit. */
2621 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2622 && c_parser_next_tokens_start_typename (parser, la)
2623 && !c_parser_next_token_is_qualifier (parser)
2624 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2625 break;
2627 if (c_parser_next_token_is (parser, CPP_NAME))
2629 c_token *name_token = c_parser_peek_token (parser);
2630 tree value = name_token->value;
2631 c_id_kind kind = name_token->id_kind;
2633 if (kind == C_ID_ADDRSPACE)
2635 addr_space_t as
2636 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2637 declspecs_add_addrspace (name_token->location, specs, as);
2638 c_parser_consume_token (parser);
2639 attrs_ok = true;
2640 continue;
2643 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2645 /* If we cannot accept a type, and the next token must start one,
2646 exit. Do the same if we already have seen a tagged definition,
2647 since it would be an error anyway and likely the user has simply
2648 forgotten a semicolon. */
2649 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2650 break;
2652 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2653 a C_ID_CLASSNAME. */
2654 c_parser_consume_token (parser);
2655 seen_type = true;
2656 attrs_ok = true;
2657 if (kind == C_ID_ID)
2659 error_at (loc, "unknown type name %qE", value);
2660 t.kind = ctsk_typedef;
2661 t.spec = error_mark_node;
2663 else if (kind == C_ID_TYPENAME
2664 && (!c_dialect_objc ()
2665 || c_parser_next_token_is_not (parser, CPP_LESS)))
2667 t.kind = ctsk_typedef;
2668 /* For a typedef name, record the meaning, not the name.
2669 In case of 'foo foo, bar;'. */
2670 t.spec = lookup_name (value);
2672 else
2674 tree proto = NULL_TREE;
2675 gcc_assert (c_dialect_objc ());
2676 t.kind = ctsk_objc;
2677 if (c_parser_next_token_is (parser, CPP_LESS))
2678 proto = c_parser_objc_protocol_refs (parser);
2679 t.spec = objc_get_protocol_qualified_type (value, proto);
2681 t.expr = NULL_TREE;
2682 t.expr_const_operands = true;
2683 declspecs_add_type (name_token->location, specs, t);
2684 continue;
2686 if (c_parser_next_token_is (parser, CPP_LESS))
2688 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2689 nisse@lysator.liu.se. */
2690 tree proto;
2691 gcc_assert (c_dialect_objc ());
2692 if (!typespec_ok || seen_type)
2693 break;
2694 proto = c_parser_objc_protocol_refs (parser);
2695 t.kind = ctsk_objc;
2696 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2697 t.expr = NULL_TREE;
2698 t.expr_const_operands = true;
2699 declspecs_add_type (loc, specs, t);
2700 continue;
2702 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2703 switch (c_parser_peek_token (parser)->keyword)
2705 case RID_STATIC:
2706 case RID_EXTERN:
2707 case RID_REGISTER:
2708 case RID_TYPEDEF:
2709 case RID_INLINE:
2710 case RID_NORETURN:
2711 case RID_AUTO:
2712 case RID_THREAD:
2713 if (!scspec_ok)
2714 goto out;
2715 attrs_ok = true;
2716 /* TODO: Distinguish between function specifiers (inline, noreturn)
2717 and storage class specifiers, either here or in
2718 declspecs_add_scspec. */
2719 declspecs_add_scspec (loc, specs,
2720 c_parser_peek_token (parser)->value);
2721 c_parser_consume_token (parser);
2722 break;
2723 case RID_AUTO_TYPE:
2724 if (!auto_type_ok)
2725 goto out;
2726 /* Fall through. */
2727 case RID_UNSIGNED:
2728 case RID_LONG:
2729 case RID_SHORT:
2730 case RID_SIGNED:
2731 case RID_COMPLEX:
2732 case RID_INT:
2733 case RID_CHAR:
2734 case RID_FLOAT:
2735 case RID_DOUBLE:
2736 case RID_VOID:
2737 case RID_DFLOAT32:
2738 case RID_DFLOAT64:
2739 case RID_DFLOAT128:
2740 CASE_RID_FLOATN_NX:
2741 case RID_BOOL:
2742 case RID_FRACT:
2743 case RID_ACCUM:
2744 case RID_SAT:
2745 case RID_INT_N_0:
2746 case RID_INT_N_1:
2747 case RID_INT_N_2:
2748 case RID_INT_N_3:
2749 if (!typespec_ok)
2750 goto out;
2751 attrs_ok = true;
2752 seen_type = true;
2753 if (c_dialect_objc ())
2754 parser->objc_need_raw_identifier = true;
2755 t.kind = ctsk_resword;
2756 t.spec = c_parser_peek_token (parser)->value;
2757 t.expr = NULL_TREE;
2758 t.expr_const_operands = true;
2759 declspecs_add_type (loc, specs, t);
2760 c_parser_consume_token (parser);
2761 break;
2762 case RID_ENUM:
2763 if (!typespec_ok)
2764 goto out;
2765 attrs_ok = true;
2766 seen_type = true;
2767 t = c_parser_enum_specifier (parser);
2768 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2769 declspecs_add_type (loc, specs, t);
2770 break;
2771 case RID_STRUCT:
2772 case RID_UNION:
2773 if (!typespec_ok)
2774 goto out;
2775 attrs_ok = true;
2776 seen_type = true;
2777 t = c_parser_struct_or_union_specifier (parser);
2778 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2779 declspecs_add_type (loc, specs, t);
2780 break;
2781 case RID_TYPEOF:
2782 /* ??? The old parser rejected typeof after other type
2783 specifiers, but is a syntax error the best way of
2784 handling this? */
2785 if (!typespec_ok || seen_type)
2786 goto out;
2787 attrs_ok = true;
2788 seen_type = true;
2789 t = c_parser_typeof_specifier (parser);
2790 declspecs_add_type (loc, specs, t);
2791 break;
2792 case RID_ATOMIC:
2793 /* C parser handling of Objective-C constructs needs
2794 checking for correct lvalue-to-rvalue conversions, and
2795 the code in build_modify_expr handling various
2796 Objective-C cases, and that in build_unary_op handling
2797 Objective-C cases for increment / decrement, also needs
2798 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2799 and objc_types_are_equivalent may also need updates. */
2800 if (c_dialect_objc ())
2801 sorry ("%<_Atomic%> in Objective-C");
2802 if (flag_isoc99)
2803 pedwarn_c99 (loc, OPT_Wpedantic,
2804 "ISO C99 does not support the %<_Atomic%> qualifier");
2805 else
2806 pedwarn_c99 (loc, OPT_Wpedantic,
2807 "ISO C90 does not support the %<_Atomic%> qualifier");
2808 attrs_ok = true;
2809 tree value;
2810 value = c_parser_peek_token (parser)->value;
2811 c_parser_consume_token (parser);
2812 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2814 /* _Atomic ( type-name ). */
2815 seen_type = true;
2816 c_parser_consume_token (parser);
2817 struct c_type_name *type = c_parser_type_name (parser);
2818 t.kind = ctsk_typeof;
2819 t.spec = error_mark_node;
2820 t.expr = NULL_TREE;
2821 t.expr_const_operands = true;
2822 if (type != NULL)
2823 t.spec = groktypename (type, &t.expr,
2824 &t.expr_const_operands);
2825 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2826 "expected %<)%>");
2827 if (t.spec != error_mark_node)
2829 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2830 error_at (loc, "%<_Atomic%>-qualified array type");
2831 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2832 error_at (loc, "%<_Atomic%>-qualified function type");
2833 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2834 error_at (loc, "%<_Atomic%> applied to a qualified type");
2835 else
2836 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2838 declspecs_add_type (loc, specs, t);
2840 else
2841 declspecs_add_qual (loc, specs, value);
2842 break;
2843 case RID_CONST:
2844 case RID_VOLATILE:
2845 case RID_RESTRICT:
2846 attrs_ok = true;
2847 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2848 c_parser_consume_token (parser);
2849 break;
2850 case RID_ATTRIBUTE:
2851 if (!attrs_ok)
2852 goto out;
2853 attrs = c_parser_attributes (parser);
2854 declspecs_add_attrs (loc, specs, attrs);
2855 break;
2856 case RID_ALIGNAS:
2857 if (!alignspec_ok)
2858 goto out;
2859 align = c_parser_alignas_specifier (parser);
2860 declspecs_add_alignas (loc, specs, align);
2861 break;
2862 case RID_GIMPLE:
2863 if (! flag_gimple)
2864 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2865 c_parser_consume_token (parser);
2866 specs->gimple_p = true;
2867 specs->locations[cdw_gimple] = loc;
2868 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2869 break;
2870 case RID_RTL:
2871 c_parser_consume_token (parser);
2872 specs->rtl_p = true;
2873 specs->locations[cdw_rtl] = loc;
2874 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2875 break;
2876 default:
2877 goto out;
2880 out: ;
2883 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2885 enum-specifier:
2886 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2887 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2888 enum attributes[opt] identifier
2890 The form with trailing comma is new in C99. The forms with
2891 attributes are GNU extensions. In GNU C, we accept any expression
2892 without commas in the syntax (assignment expressions, not just
2893 conditional expressions); assignment expressions will be diagnosed
2894 as non-constant.
2896 enumerator-list:
2897 enumerator
2898 enumerator-list , enumerator
2900 enumerator:
2901 enumeration-constant
2902 enumeration-constant = constant-expression
2904 GNU Extensions:
2906 enumerator:
2907 enumeration-constant attributes[opt]
2908 enumeration-constant attributes[opt] = constant-expression
2912 static struct c_typespec
2913 c_parser_enum_specifier (c_parser *parser)
2915 struct c_typespec ret;
2916 tree attrs;
2917 tree ident = NULL_TREE;
2918 location_t enum_loc;
2919 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2920 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2921 c_parser_consume_token (parser);
2922 attrs = c_parser_attributes (parser);
2923 enum_loc = c_parser_peek_token (parser)->location;
2924 /* Set the location in case we create a decl now. */
2925 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2926 if (c_parser_next_token_is (parser, CPP_NAME))
2928 ident = c_parser_peek_token (parser)->value;
2929 ident_loc = c_parser_peek_token (parser)->location;
2930 enum_loc = ident_loc;
2931 c_parser_consume_token (parser);
2933 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2935 /* Parse an enum definition. */
2936 struct c_enum_contents the_enum;
2937 tree type;
2938 tree postfix_attrs;
2939 /* We chain the enumerators in reverse order, then put them in
2940 forward order at the end. */
2941 tree values;
2942 timevar_push (TV_PARSE_ENUM);
2943 type = start_enum (enum_loc, &the_enum, ident);
2944 values = NULL_TREE;
2945 c_parser_consume_token (parser);
2946 while (true)
2948 tree enum_id;
2949 tree enum_value;
2950 tree enum_decl;
2951 bool seen_comma;
2952 c_token *token;
2953 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2954 location_t decl_loc, value_loc;
2955 if (c_parser_next_token_is_not (parser, CPP_NAME))
2957 /* Give a nicer error for "enum {}". */
2958 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2959 && !parser->error)
2961 error_at (c_parser_peek_token (parser)->location,
2962 "empty enum is invalid");
2963 parser->error = true;
2965 else
2966 c_parser_error (parser, "expected identifier");
2967 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2968 values = error_mark_node;
2969 break;
2971 token = c_parser_peek_token (parser);
2972 enum_id = token->value;
2973 /* Set the location in case we create a decl now. */
2974 c_parser_set_source_position_from_token (token);
2975 decl_loc = value_loc = token->location;
2976 c_parser_consume_token (parser);
2977 /* Parse any specified attributes. */
2978 tree enum_attrs = c_parser_attributes (parser);
2979 if (c_parser_next_token_is (parser, CPP_EQ))
2981 c_parser_consume_token (parser);
2982 value_loc = c_parser_peek_token (parser)->location;
2983 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2985 else
2986 enum_value = NULL_TREE;
2987 enum_decl = build_enumerator (decl_loc, value_loc,
2988 &the_enum, enum_id, enum_value);
2989 if (enum_attrs)
2990 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2991 TREE_CHAIN (enum_decl) = values;
2992 values = enum_decl;
2993 seen_comma = false;
2994 if (c_parser_next_token_is (parser, CPP_COMMA))
2996 comma_loc = c_parser_peek_token (parser)->location;
2997 seen_comma = true;
2998 c_parser_consume_token (parser);
3000 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3002 if (seen_comma)
3003 pedwarn_c90 (comma_loc, OPT_Wpedantic,
3004 "comma at end of enumerator list");
3005 c_parser_consume_token (parser);
3006 break;
3008 if (!seen_comma)
3010 c_parser_error (parser, "expected %<,%> or %<}%>");
3011 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3012 values = error_mark_node;
3013 break;
3016 postfix_attrs = c_parser_attributes (parser);
3017 ret.spec = finish_enum (type, nreverse (values),
3018 chainon (attrs, postfix_attrs));
3019 ret.kind = ctsk_tagdef;
3020 ret.expr = NULL_TREE;
3021 ret.expr_const_operands = true;
3022 timevar_pop (TV_PARSE_ENUM);
3023 return ret;
3025 else if (!ident)
3027 c_parser_error (parser, "expected %<{%>");
3028 ret.spec = error_mark_node;
3029 ret.kind = ctsk_tagref;
3030 ret.expr = NULL_TREE;
3031 ret.expr_const_operands = true;
3032 return ret;
3034 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3035 /* In ISO C, enumerated types can be referred to only if already
3036 defined. */
3037 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3039 gcc_assert (ident);
3040 pedwarn (enum_loc, OPT_Wpedantic,
3041 "ISO C forbids forward references to %<enum%> types");
3043 return ret;
3046 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3048 struct-or-union-specifier:
3049 struct-or-union attributes[opt] identifier[opt]
3050 { struct-contents } attributes[opt]
3051 struct-or-union attributes[opt] identifier
3053 struct-contents:
3054 struct-declaration-list
3056 struct-declaration-list:
3057 struct-declaration ;
3058 struct-declaration-list struct-declaration ;
3060 GNU extensions:
3062 struct-contents:
3063 empty
3064 struct-declaration
3065 struct-declaration-list struct-declaration
3067 struct-declaration-list:
3068 struct-declaration-list ;
3071 (Note that in the syntax here, unlike that in ISO C, the semicolons
3072 are included here rather than in struct-declaration, in order to
3073 describe the syntax with extra semicolons and missing semicolon at
3074 end.)
3076 Objective-C:
3078 struct-declaration-list:
3079 @defs ( class-name )
3081 (Note this does not include a trailing semicolon, but can be
3082 followed by further declarations, and gets a pedwarn-if-pedantic
3083 when followed by a semicolon.) */
3085 static struct c_typespec
3086 c_parser_struct_or_union_specifier (c_parser *parser)
3088 struct c_typespec ret;
3089 tree attrs;
3090 tree ident = NULL_TREE;
3091 location_t struct_loc;
3092 location_t ident_loc = UNKNOWN_LOCATION;
3093 enum tree_code code;
3094 switch (c_parser_peek_token (parser)->keyword)
3096 case RID_STRUCT:
3097 code = RECORD_TYPE;
3098 break;
3099 case RID_UNION:
3100 code = UNION_TYPE;
3101 break;
3102 default:
3103 gcc_unreachable ();
3105 struct_loc = c_parser_peek_token (parser)->location;
3106 c_parser_consume_token (parser);
3107 attrs = c_parser_attributes (parser);
3109 /* Set the location in case we create a decl now. */
3110 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3112 if (c_parser_next_token_is (parser, CPP_NAME))
3114 ident = c_parser_peek_token (parser)->value;
3115 ident_loc = c_parser_peek_token (parser)->location;
3116 struct_loc = ident_loc;
3117 c_parser_consume_token (parser);
3119 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3121 /* Parse a struct or union definition. Start the scope of the
3122 tag before parsing components. */
3123 struct c_struct_parse_info *struct_info;
3124 tree type = start_struct (struct_loc, code, ident, &struct_info);
3125 tree postfix_attrs;
3126 /* We chain the components in reverse order, then put them in
3127 forward order at the end. Each struct-declaration may
3128 declare multiple components (comma-separated), so we must use
3129 chainon to join them, although when parsing each
3130 struct-declaration we can use TREE_CHAIN directly.
3132 The theory behind all this is that there will be more
3133 semicolon separated fields than comma separated fields, and
3134 so we'll be minimizing the number of node traversals required
3135 by chainon. */
3136 tree contents;
3137 timevar_push (TV_PARSE_STRUCT);
3138 contents = NULL_TREE;
3139 c_parser_consume_token (parser);
3140 /* Handle the Objective-C @defs construct,
3141 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3142 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3144 tree name;
3145 gcc_assert (c_dialect_objc ());
3146 c_parser_consume_token (parser);
3147 matching_parens parens;
3148 if (!parens.require_open (parser))
3149 goto end_at_defs;
3150 if (c_parser_next_token_is (parser, CPP_NAME)
3151 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3153 name = c_parser_peek_token (parser)->value;
3154 c_parser_consume_token (parser);
3156 else
3158 c_parser_error (parser, "expected class name");
3159 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3160 goto end_at_defs;
3162 parens.skip_until_found_close (parser);
3163 contents = nreverse (objc_get_class_ivars (name));
3165 end_at_defs:
3166 /* Parse the struct-declarations and semicolons. Problems with
3167 semicolons are diagnosed here; empty structures are diagnosed
3168 elsewhere. */
3169 while (true)
3171 tree decls;
3172 /* Parse any stray semicolon. */
3173 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3175 location_t semicolon_loc
3176 = c_parser_peek_token (parser)->location;
3177 gcc_rich_location richloc (semicolon_loc);
3178 richloc.add_fixit_remove ();
3179 pedwarn (&richloc, OPT_Wpedantic,
3180 "extra semicolon in struct or union specified");
3181 c_parser_consume_token (parser);
3182 continue;
3184 /* Stop if at the end of the struct or union contents. */
3185 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3187 c_parser_consume_token (parser);
3188 break;
3190 /* Accept #pragmas at struct scope. */
3191 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3193 c_parser_pragma (parser, pragma_struct, NULL);
3194 continue;
3196 /* Parse some comma-separated declarations, but not the
3197 trailing semicolon if any. */
3198 decls = c_parser_struct_declaration (parser);
3199 contents = chainon (decls, contents);
3200 /* If no semicolon follows, either we have a parse error or
3201 are at the end of the struct or union and should
3202 pedwarn. */
3203 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3204 c_parser_consume_token (parser);
3205 else
3207 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3208 pedwarn (c_parser_peek_token (parser)->location, 0,
3209 "no semicolon at end of struct or union");
3210 else if (parser->error
3211 || !c_parser_next_token_starts_declspecs (parser))
3213 c_parser_error (parser, "expected %<;%>");
3214 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3215 break;
3218 /* If we come here, we have already emitted an error
3219 for an expected `;', identifier or `(', and we also
3220 recovered already. Go on with the next field. */
3223 postfix_attrs = c_parser_attributes (parser);
3224 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3225 chainon (attrs, postfix_attrs), struct_info);
3226 ret.kind = ctsk_tagdef;
3227 ret.expr = NULL_TREE;
3228 ret.expr_const_operands = true;
3229 timevar_pop (TV_PARSE_STRUCT);
3230 return ret;
3232 else if (!ident)
3234 c_parser_error (parser, "expected %<{%>");
3235 ret.spec = error_mark_node;
3236 ret.kind = ctsk_tagref;
3237 ret.expr = NULL_TREE;
3238 ret.expr_const_operands = true;
3239 return ret;
3241 ret = parser_xref_tag (ident_loc, code, ident);
3242 return ret;
3245 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3246 *without* the trailing semicolon.
3248 struct-declaration:
3249 specifier-qualifier-list struct-declarator-list
3250 static_assert-declaration-no-semi
3252 specifier-qualifier-list:
3253 type-specifier specifier-qualifier-list[opt]
3254 type-qualifier specifier-qualifier-list[opt]
3255 alignment-specifier specifier-qualifier-list[opt]
3256 attributes specifier-qualifier-list[opt]
3258 struct-declarator-list:
3259 struct-declarator
3260 struct-declarator-list , attributes[opt] struct-declarator
3262 struct-declarator:
3263 declarator attributes[opt]
3264 declarator[opt] : constant-expression attributes[opt]
3266 GNU extensions:
3268 struct-declaration:
3269 __extension__ struct-declaration
3270 specifier-qualifier-list
3272 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3273 of attributes where shown is a GNU extension. In GNU C, we accept
3274 any expression without commas in the syntax (assignment
3275 expressions, not just conditional expressions); assignment
3276 expressions will be diagnosed as non-constant. */
3278 static tree
3279 c_parser_struct_declaration (c_parser *parser)
3281 struct c_declspecs *specs;
3282 tree prefix_attrs;
3283 tree all_prefix_attrs;
3284 tree decls;
3285 location_t decl_loc;
3286 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3288 int ext;
3289 tree decl;
3290 ext = disable_extension_diagnostics ();
3291 c_parser_consume_token (parser);
3292 decl = c_parser_struct_declaration (parser);
3293 restore_extension_diagnostics (ext);
3294 return decl;
3296 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3298 c_parser_static_assert_declaration_no_semi (parser);
3299 return NULL_TREE;
3301 specs = build_null_declspecs ();
3302 decl_loc = c_parser_peek_token (parser)->location;
3303 /* Strictly by the standard, we shouldn't allow _Alignas here,
3304 but it appears to have been intended to allow it there, so
3305 we're keeping it as it is until WG14 reaches a conclusion
3306 of N1731.
3307 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3308 c_parser_declspecs (parser, specs, false, true, true,
3309 true, false, cla_nonabstract_decl);
3310 if (parser->error)
3311 return NULL_TREE;
3312 if (!specs->declspecs_seen_p)
3314 c_parser_error (parser, "expected specifier-qualifier-list");
3315 return NULL_TREE;
3317 finish_declspecs (specs);
3318 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3319 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3321 tree ret;
3322 if (specs->typespec_kind == ctsk_none)
3324 pedwarn (decl_loc, OPT_Wpedantic,
3325 "ISO C forbids member declarations with no members");
3326 shadow_tag_warned (specs, pedantic);
3327 ret = NULL_TREE;
3329 else
3331 /* Support for unnamed structs or unions as members of
3332 structs or unions (which is [a] useful and [b] supports
3333 MS P-SDK). */
3334 tree attrs = NULL;
3336 ret = grokfield (c_parser_peek_token (parser)->location,
3337 build_id_declarator (NULL_TREE), specs,
3338 NULL_TREE, &attrs);
3339 if (ret)
3340 decl_attributes (&ret, attrs, 0);
3342 return ret;
3345 /* Provide better error recovery. Note that a type name here is valid,
3346 and will be treated as a field name. */
3347 if (specs->typespec_kind == ctsk_tagdef
3348 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3349 && c_parser_next_token_starts_declspecs (parser)
3350 && !c_parser_next_token_is (parser, CPP_NAME))
3352 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3353 parser->error = false;
3354 return NULL_TREE;
3357 pending_xref_error ();
3358 prefix_attrs = specs->attrs;
3359 all_prefix_attrs = prefix_attrs;
3360 specs->attrs = NULL_TREE;
3361 decls = NULL_TREE;
3362 while (true)
3364 /* Declaring one or more declarators or un-named bit-fields. */
3365 struct c_declarator *declarator;
3366 bool dummy = false;
3367 if (c_parser_next_token_is (parser, CPP_COLON))
3368 declarator = build_id_declarator (NULL_TREE);
3369 else
3370 declarator = c_parser_declarator (parser,
3371 specs->typespec_kind != ctsk_none,
3372 C_DTR_NORMAL, &dummy);
3373 if (declarator == NULL)
3375 c_parser_skip_to_end_of_block_or_statement (parser);
3376 break;
3378 if (c_parser_next_token_is (parser, CPP_COLON)
3379 || c_parser_next_token_is (parser, CPP_COMMA)
3380 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3381 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3382 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3384 tree postfix_attrs = NULL_TREE;
3385 tree width = NULL_TREE;
3386 tree d;
3387 if (c_parser_next_token_is (parser, CPP_COLON))
3389 c_parser_consume_token (parser);
3390 width = c_parser_expr_no_commas (parser, NULL).value;
3392 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3393 postfix_attrs = c_parser_attributes (parser);
3394 d = grokfield (c_parser_peek_token (parser)->location,
3395 declarator, specs, width, &all_prefix_attrs);
3396 decl_attributes (&d, chainon (postfix_attrs,
3397 all_prefix_attrs), 0);
3398 DECL_CHAIN (d) = decls;
3399 decls = d;
3400 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3401 all_prefix_attrs = chainon (c_parser_attributes (parser),
3402 prefix_attrs);
3403 else
3404 all_prefix_attrs = prefix_attrs;
3405 if (c_parser_next_token_is (parser, CPP_COMMA))
3406 c_parser_consume_token (parser);
3407 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3408 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3410 /* Semicolon consumed in caller. */
3411 break;
3413 else
3415 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3416 break;
3419 else
3421 c_parser_error (parser,
3422 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3423 "%<__attribute__%>");
3424 break;
3427 return decls;
3430 /* Parse a typeof specifier (a GNU extension).
3432 typeof-specifier:
3433 typeof ( expression )
3434 typeof ( type-name )
3437 static struct c_typespec
3438 c_parser_typeof_specifier (c_parser *parser)
3440 struct c_typespec ret;
3441 ret.kind = ctsk_typeof;
3442 ret.spec = error_mark_node;
3443 ret.expr = NULL_TREE;
3444 ret.expr_const_operands = true;
3445 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3446 c_parser_consume_token (parser);
3447 c_inhibit_evaluation_warnings++;
3448 in_typeof++;
3449 matching_parens parens;
3450 if (!parens.require_open (parser))
3452 c_inhibit_evaluation_warnings--;
3453 in_typeof--;
3454 return ret;
3456 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3458 struct c_type_name *type = c_parser_type_name (parser);
3459 c_inhibit_evaluation_warnings--;
3460 in_typeof--;
3461 if (type != NULL)
3463 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3464 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3467 else
3469 bool was_vm;
3470 location_t here = c_parser_peek_token (parser)->location;
3471 struct c_expr expr = c_parser_expression (parser);
3472 c_inhibit_evaluation_warnings--;
3473 in_typeof--;
3474 if (TREE_CODE (expr.value) == COMPONENT_REF
3475 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3476 error_at (here, "%<typeof%> applied to a bit-field");
3477 mark_exp_read (expr.value);
3478 ret.spec = TREE_TYPE (expr.value);
3479 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3480 /* This is returned with the type so that when the type is
3481 evaluated, this can be evaluated. */
3482 if (was_vm)
3483 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3484 pop_maybe_used (was_vm);
3485 /* For use in macros such as those in <stdatomic.h>, remove all
3486 qualifiers from atomic types. (const can be an issue for more macros
3487 using typeof than just the <stdatomic.h> ones.) */
3488 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3489 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3491 parens.skip_until_found_close (parser);
3492 return ret;
3495 /* Parse an alignment-specifier.
3497 C11 6.7.5:
3499 alignment-specifier:
3500 _Alignas ( type-name )
3501 _Alignas ( constant-expression )
3504 static tree
3505 c_parser_alignas_specifier (c_parser * parser)
3507 tree ret = error_mark_node;
3508 location_t loc = c_parser_peek_token (parser)->location;
3509 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3510 c_parser_consume_token (parser);
3511 if (flag_isoc99)
3512 pedwarn_c99 (loc, OPT_Wpedantic,
3513 "ISO C99 does not support %<_Alignas%>");
3514 else
3515 pedwarn_c99 (loc, OPT_Wpedantic,
3516 "ISO C90 does not support %<_Alignas%>");
3517 matching_parens parens;
3518 if (!parens.require_open (parser))
3519 return ret;
3520 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3522 struct c_type_name *type = c_parser_type_name (parser);
3523 if (type != NULL)
3524 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3525 false, true, 1);
3527 else
3528 ret = c_parser_expr_no_commas (parser, NULL).value;
3529 parens.skip_until_found_close (parser);
3530 return ret;
3533 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3534 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3535 a typedef name may be redeclared; otherwise it may not. KIND
3536 indicates which kind of declarator is wanted. Returns a valid
3537 declarator except in the case of a syntax error in which case NULL is
3538 returned. *SEEN_ID is set to true if an identifier being declared is
3539 seen; this is used to diagnose bad forms of abstract array declarators
3540 and to determine whether an identifier list is syntactically permitted.
3542 declarator:
3543 pointer[opt] direct-declarator
3545 direct-declarator:
3546 identifier
3547 ( attributes[opt] declarator )
3548 direct-declarator array-declarator
3549 direct-declarator ( parameter-type-list )
3550 direct-declarator ( identifier-list[opt] )
3552 pointer:
3553 * type-qualifier-list[opt]
3554 * type-qualifier-list[opt] pointer
3556 type-qualifier-list:
3557 type-qualifier
3558 attributes
3559 type-qualifier-list type-qualifier
3560 type-qualifier-list attributes
3562 array-declarator:
3563 [ type-qualifier-list[opt] assignment-expression[opt] ]
3564 [ static type-qualifier-list[opt] assignment-expression ]
3565 [ type-qualifier-list static assignment-expression ]
3566 [ type-qualifier-list[opt] * ]
3568 parameter-type-list:
3569 parameter-list
3570 parameter-list , ...
3572 parameter-list:
3573 parameter-declaration
3574 parameter-list , parameter-declaration
3576 parameter-declaration:
3577 declaration-specifiers declarator attributes[opt]
3578 declaration-specifiers abstract-declarator[opt] attributes[opt]
3580 identifier-list:
3581 identifier
3582 identifier-list , identifier
3584 abstract-declarator:
3585 pointer
3586 pointer[opt] direct-abstract-declarator
3588 direct-abstract-declarator:
3589 ( attributes[opt] abstract-declarator )
3590 direct-abstract-declarator[opt] array-declarator
3591 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3593 GNU extensions:
3595 direct-declarator:
3596 direct-declarator ( parameter-forward-declarations
3597 parameter-type-list[opt] )
3599 direct-abstract-declarator:
3600 direct-abstract-declarator[opt] ( parameter-forward-declarations
3601 parameter-type-list[opt] )
3603 parameter-forward-declarations:
3604 parameter-list ;
3605 parameter-forward-declarations parameter-list ;
3607 The uses of attributes shown above are GNU extensions.
3609 Some forms of array declarator are not included in C99 in the
3610 syntax for abstract declarators; these are disallowed elsewhere.
3611 This may be a defect (DR#289).
3613 This function also accepts an omitted abstract declarator as being
3614 an abstract declarator, although not part of the formal syntax. */
3616 struct c_declarator *
3617 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3618 bool *seen_id)
3620 /* Parse any initial pointer part. */
3621 if (c_parser_next_token_is (parser, CPP_MULT))
3623 struct c_declspecs *quals_attrs = build_null_declspecs ();
3624 struct c_declarator *inner;
3625 c_parser_consume_token (parser);
3626 c_parser_declspecs (parser, quals_attrs, false, false, true,
3627 false, false, cla_prefer_id);
3628 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3629 if (inner == NULL)
3630 return NULL;
3631 else
3632 return make_pointer_declarator (quals_attrs, inner);
3634 /* Now we have a direct declarator, direct abstract declarator or
3635 nothing (which counts as a direct abstract declarator here). */
3636 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3639 /* Parse a direct declarator or direct abstract declarator; arguments
3640 as c_parser_declarator. */
3642 static struct c_declarator *
3643 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3644 bool *seen_id)
3646 /* The direct declarator must start with an identifier (possibly
3647 omitted) or a parenthesized declarator (possibly abstract). In
3648 an ordinary declarator, initial parentheses must start a
3649 parenthesized declarator. In an abstract declarator or parameter
3650 declarator, they could start a parenthesized declarator or a
3651 parameter list. To tell which, the open parenthesis and any
3652 following attributes must be read. If a declaration specifier
3653 follows, then it is a parameter list; if the specifier is a
3654 typedef name, there might be an ambiguity about redeclaring it,
3655 which is resolved in the direction of treating it as a typedef
3656 name. If a close parenthesis follows, it is also an empty
3657 parameter list, as the syntax does not permit empty abstract
3658 declarators. Otherwise, it is a parenthesized declarator (in
3659 which case the analysis may be repeated inside it, recursively).
3661 ??? There is an ambiguity in a parameter declaration "int
3662 (__attribute__((foo)) x)", where x is not a typedef name: it
3663 could be an abstract declarator for a function, or declare x with
3664 parentheses. The proper resolution of this ambiguity needs
3665 documenting. At present we follow an accident of the old
3666 parser's implementation, whereby the first parameter must have
3667 some declaration specifiers other than just attributes. Thus as
3668 a parameter declaration it is treated as a parenthesized
3669 parameter named x, and as an abstract declarator it is
3670 rejected.
3672 ??? Also following the old parser, attributes inside an empty
3673 parameter list are ignored, making it a list not yielding a
3674 prototype, rather than giving an error or making it have one
3675 parameter with implicit type int.
3677 ??? Also following the old parser, typedef names may be
3678 redeclared in declarators, but not Objective-C class names. */
3680 if (kind != C_DTR_ABSTRACT
3681 && c_parser_next_token_is (parser, CPP_NAME)
3682 && ((type_seen_p
3683 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3684 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3685 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3687 struct c_declarator *inner
3688 = build_id_declarator (c_parser_peek_token (parser)->value);
3689 *seen_id = true;
3690 inner->id_loc = c_parser_peek_token (parser)->location;
3691 c_parser_consume_token (parser);
3692 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3695 if (kind != C_DTR_NORMAL
3696 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3698 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3699 inner->id_loc = c_parser_peek_token (parser)->location;
3700 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3703 /* Either we are at the end of an abstract declarator, or we have
3704 parentheses. */
3706 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3708 tree attrs;
3709 struct c_declarator *inner;
3710 c_parser_consume_token (parser);
3711 attrs = c_parser_attributes (parser);
3712 if (kind != C_DTR_NORMAL
3713 && (c_parser_next_token_starts_declspecs (parser)
3714 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3716 struct c_arg_info *args
3717 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3718 attrs);
3719 if (args == NULL)
3720 return NULL;
3721 else
3723 inner
3724 = build_function_declarator (args,
3725 build_id_declarator (NULL_TREE));
3726 return c_parser_direct_declarator_inner (parser, *seen_id,
3727 inner);
3730 /* A parenthesized declarator. */
3731 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3732 if (inner != NULL && attrs != NULL)
3733 inner = build_attrs_declarator (attrs, inner);
3734 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3736 c_parser_consume_token (parser);
3737 if (inner == NULL)
3738 return NULL;
3739 else
3740 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3742 else
3744 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3745 "expected %<)%>");
3746 return NULL;
3749 else
3751 if (kind == C_DTR_NORMAL)
3753 c_parser_error (parser, "expected identifier or %<(%>");
3754 return NULL;
3756 else
3757 return build_id_declarator (NULL_TREE);
3761 /* Parse part of a direct declarator or direct abstract declarator,
3762 given that some (in INNER) has already been parsed; ID_PRESENT is
3763 true if an identifier is present, false for an abstract
3764 declarator. */
3766 static struct c_declarator *
3767 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3768 struct c_declarator *inner)
3770 /* Parse a sequence of array declarators and parameter lists. */
3771 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3773 location_t brace_loc = c_parser_peek_token (parser)->location;
3774 struct c_declarator *declarator;
3775 struct c_declspecs *quals_attrs = build_null_declspecs ();
3776 bool static_seen;
3777 bool star_seen;
3778 struct c_expr dimen;
3779 dimen.value = NULL_TREE;
3780 dimen.original_code = ERROR_MARK;
3781 dimen.original_type = NULL_TREE;
3782 c_parser_consume_token (parser);
3783 c_parser_declspecs (parser, quals_attrs, false, false, true,
3784 false, false, cla_prefer_id);
3785 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3786 if (static_seen)
3787 c_parser_consume_token (parser);
3788 if (static_seen && !quals_attrs->declspecs_seen_p)
3789 c_parser_declspecs (parser, quals_attrs, false, false, true,
3790 false, false, cla_prefer_id);
3791 if (!quals_attrs->declspecs_seen_p)
3792 quals_attrs = NULL;
3793 /* If "static" is present, there must be an array dimension.
3794 Otherwise, there may be a dimension, "*", or no
3795 dimension. */
3796 if (static_seen)
3798 star_seen = false;
3799 dimen = c_parser_expr_no_commas (parser, NULL);
3801 else
3803 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3805 dimen.value = NULL_TREE;
3806 star_seen = false;
3808 else if (c_parser_next_token_is (parser, CPP_MULT))
3810 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3812 dimen.value = NULL_TREE;
3813 star_seen = true;
3814 c_parser_consume_token (parser);
3816 else
3818 star_seen = false;
3819 dimen = c_parser_expr_no_commas (parser, NULL);
3822 else
3824 star_seen = false;
3825 dimen = c_parser_expr_no_commas (parser, NULL);
3828 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3829 c_parser_consume_token (parser);
3830 else
3832 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3833 "expected %<]%>");
3834 return NULL;
3836 if (dimen.value)
3837 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3838 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3839 static_seen, star_seen);
3840 if (declarator == NULL)
3841 return NULL;
3842 inner = set_array_declarator_inner (declarator, inner);
3843 return c_parser_direct_declarator_inner (parser, id_present, inner);
3845 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3847 tree attrs;
3848 struct c_arg_info *args;
3849 c_parser_consume_token (parser);
3850 attrs = c_parser_attributes (parser);
3851 args = c_parser_parms_declarator (parser, id_present, attrs);
3852 if (args == NULL)
3853 return NULL;
3854 else
3856 inner = build_function_declarator (args, inner);
3857 return c_parser_direct_declarator_inner (parser, id_present, inner);
3860 return inner;
3863 /* Parse a parameter list or identifier list, including the closing
3864 parenthesis but not the opening one. ATTRS are the attributes at
3865 the start of the list. ID_LIST_OK is true if an identifier list is
3866 acceptable; such a list must not have attributes at the start. */
3868 static struct c_arg_info *
3869 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3871 push_scope ();
3872 declare_parm_level ();
3873 /* If the list starts with an identifier, it is an identifier list.
3874 Otherwise, it is either a prototype list or an empty list. */
3875 if (id_list_ok
3876 && !attrs
3877 && c_parser_next_token_is (parser, CPP_NAME)
3878 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3880 /* Look ahead to detect typos in type names. */
3881 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3882 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3883 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3884 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3885 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3887 tree list = NULL_TREE, *nextp = &list;
3888 while (c_parser_next_token_is (parser, CPP_NAME)
3889 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3891 *nextp = build_tree_list (NULL_TREE,
3892 c_parser_peek_token (parser)->value);
3893 nextp = & TREE_CHAIN (*nextp);
3894 c_parser_consume_token (parser);
3895 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3896 break;
3897 c_parser_consume_token (parser);
3898 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3900 c_parser_error (parser, "expected identifier");
3901 break;
3904 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3906 struct c_arg_info *ret = build_arg_info ();
3907 ret->types = list;
3908 c_parser_consume_token (parser);
3909 pop_scope ();
3910 return ret;
3912 else
3914 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3915 "expected %<)%>");
3916 pop_scope ();
3917 return NULL;
3920 else
3922 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3923 NULL);
3924 pop_scope ();
3925 return ret;
3929 /* Parse a parameter list (possibly empty), including the closing
3930 parenthesis but not the opening one. ATTRS are the attributes at
3931 the start of the list. EXPR is NULL or an expression that needs to
3932 be evaluated for the side effects of array size expressions in the
3933 parameters. */
3935 static struct c_arg_info *
3936 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3938 bool bad_parm = false;
3940 /* ??? Following the old parser, forward parameter declarations may
3941 use abstract declarators, and if no real parameter declarations
3942 follow the forward declarations then this is not diagnosed. Also
3943 note as above that attributes are ignored as the only contents of
3944 the parentheses, or as the only contents after forward
3945 declarations. */
3946 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3948 struct c_arg_info *ret = build_arg_info ();
3949 c_parser_consume_token (parser);
3950 return ret;
3952 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3954 struct c_arg_info *ret = build_arg_info ();
3956 if (flag_allow_parameterless_variadic_functions)
3958 /* F (...) is allowed. */
3959 ret->types = NULL_TREE;
3961 else
3963 /* Suppress -Wold-style-definition for this case. */
3964 ret->types = error_mark_node;
3965 error_at (c_parser_peek_token (parser)->location,
3966 "ISO C requires a named argument before %<...%>");
3968 c_parser_consume_token (parser);
3969 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3971 c_parser_consume_token (parser);
3972 return ret;
3974 else
3976 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3977 "expected %<)%>");
3978 return NULL;
3981 /* Nonempty list of parameters, either terminated with semicolon
3982 (forward declarations; recurse) or with close parenthesis (normal
3983 function) or with ", ... )" (variadic function). */
3984 while (true)
3986 /* Parse a parameter. */
3987 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3988 attrs = NULL_TREE;
3989 if (parm == NULL)
3990 bad_parm = true;
3991 else
3992 push_parm_decl (parm, &expr);
3993 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3995 tree new_attrs;
3996 c_parser_consume_token (parser);
3997 mark_forward_parm_decls ();
3998 new_attrs = c_parser_attributes (parser);
3999 return c_parser_parms_list_declarator (parser, new_attrs, expr);
4001 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4003 c_parser_consume_token (parser);
4004 if (bad_parm)
4005 return NULL;
4006 else
4007 return get_parm_info (false, expr);
4009 if (!c_parser_require (parser, CPP_COMMA,
4010 "expected %<;%>, %<,%> or %<)%>",
4011 UNKNOWN_LOCATION, false))
4013 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4014 return NULL;
4016 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4018 c_parser_consume_token (parser);
4019 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4021 c_parser_consume_token (parser);
4022 if (bad_parm)
4023 return NULL;
4024 else
4025 return get_parm_info (true, expr);
4027 else
4029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4030 "expected %<)%>");
4031 return NULL;
4037 /* Parse a parameter declaration. ATTRS are the attributes at the
4038 start of the declaration if it is the first parameter. */
4040 static struct c_parm *
4041 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4043 struct c_declspecs *specs;
4044 struct c_declarator *declarator;
4045 tree prefix_attrs;
4046 tree postfix_attrs = NULL_TREE;
4047 bool dummy = false;
4049 /* Accept #pragmas between parameter declarations. */
4050 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4051 c_parser_pragma (parser, pragma_param, NULL);
4053 if (!c_parser_next_token_starts_declspecs (parser))
4055 c_token *token = c_parser_peek_token (parser);
4056 if (parser->error)
4057 return NULL;
4058 c_parser_set_source_position_from_token (token);
4059 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4061 name_hint hint = lookup_name_fuzzy (token->value,
4062 FUZZY_LOOKUP_TYPENAME,
4063 token->location);
4064 if (hint)
4066 gcc_rich_location richloc (token->location);
4067 richloc.add_fixit_replace (hint.suggestion ());
4068 error_at (&richloc,
4069 "unknown type name %qE; did you mean %qs?",
4070 token->value, hint.suggestion ());
4072 else
4073 error_at (token->location, "unknown type name %qE", token->value);
4074 parser->error = true;
4076 /* ??? In some Objective-C cases '...' isn't applicable so there
4077 should be a different message. */
4078 else
4079 c_parser_error (parser,
4080 "expected declaration specifiers or %<...%>");
4081 c_parser_skip_to_end_of_parameter (parser);
4082 return NULL;
4085 location_t start_loc = c_parser_peek_token (parser)->location;
4087 specs = build_null_declspecs ();
4088 if (attrs)
4090 declspecs_add_attrs (input_location, specs, attrs);
4091 attrs = NULL_TREE;
4093 c_parser_declspecs (parser, specs, true, true, true, true, false,
4094 cla_nonabstract_decl);
4095 finish_declspecs (specs);
4096 pending_xref_error ();
4097 prefix_attrs = specs->attrs;
4098 specs->attrs = NULL_TREE;
4099 declarator = c_parser_declarator (parser,
4100 specs->typespec_kind != ctsk_none,
4101 C_DTR_PARM, &dummy);
4102 if (declarator == NULL)
4104 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4105 return NULL;
4107 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4108 postfix_attrs = c_parser_attributes (parser);
4110 /* Generate a location for the parameter, ranging from the start of the
4111 initial token to the end of the final token.
4113 If we have a identifier, then use it for the caret location, e.g.
4115 extern int callee (int one, int (*two)(int, int), float three);
4116 ~~~~~~^~~~~~~~~~~~~~
4118 otherwise, reuse the start location for the caret location e.g.:
4120 extern int callee (int one, int (*)(int, int), float three);
4121 ^~~~~~~~~~~~~~~~~
4123 location_t end_loc = parser->last_token_location;
4125 /* Find any cdk_id declarator; determine if we have an identifier. */
4126 c_declarator *id_declarator = declarator;
4127 while (id_declarator && id_declarator->kind != cdk_id)
4128 id_declarator = id_declarator->declarator;
4129 location_t caret_loc = (id_declarator->u.id
4130 ? id_declarator->id_loc
4131 : start_loc);
4132 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4134 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4135 declarator, param_loc);
4138 /* Parse a string literal in an asm expression. It should not be
4139 translated, and wide string literals are an error although
4140 permitted by the syntax. This is a GNU extension.
4142 asm-string-literal:
4143 string-literal
4145 ??? At present, following the old parser, the caller needs to have
4146 set lex_untranslated_string to 1. It would be better to follow the
4147 C++ parser rather than using this kludge. */
4149 static tree
4150 c_parser_asm_string_literal (c_parser *parser)
4152 tree str;
4153 int save_flag = warn_overlength_strings;
4154 warn_overlength_strings = 0;
4155 if (c_parser_next_token_is (parser, CPP_STRING))
4157 str = c_parser_peek_token (parser)->value;
4158 c_parser_consume_token (parser);
4160 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4162 error_at (c_parser_peek_token (parser)->location,
4163 "wide string literal in %<asm%>");
4164 str = build_string (1, "");
4165 c_parser_consume_token (parser);
4167 else
4169 c_parser_error (parser, "expected string literal");
4170 str = NULL_TREE;
4172 warn_overlength_strings = save_flag;
4173 return str;
4176 /* Parse a simple asm expression. This is used in restricted
4177 contexts, where a full expression with inputs and outputs does not
4178 make sense. This is a GNU extension.
4180 simple-asm-expr:
4181 asm ( asm-string-literal )
4184 static tree
4185 c_parser_simple_asm_expr (c_parser *parser)
4187 tree str;
4188 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4189 /* ??? Follow the C++ parser rather than using the
4190 lex_untranslated_string kludge. */
4191 parser->lex_untranslated_string = true;
4192 c_parser_consume_token (parser);
4193 matching_parens parens;
4194 if (!parens.require_open (parser))
4196 parser->lex_untranslated_string = false;
4197 return NULL_TREE;
4199 str = c_parser_asm_string_literal (parser);
4200 parser->lex_untranslated_string = false;
4201 if (!parens.require_close (parser))
4203 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4204 return NULL_TREE;
4206 return str;
4209 static tree
4210 c_parser_attribute_any_word (c_parser *parser)
4212 tree attr_name = NULL_TREE;
4214 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4216 /* ??? See comment above about what keywords are accepted here. */
4217 bool ok;
4218 switch (c_parser_peek_token (parser)->keyword)
4220 case RID_STATIC:
4221 case RID_UNSIGNED:
4222 case RID_LONG:
4223 case RID_CONST:
4224 case RID_EXTERN:
4225 case RID_REGISTER:
4226 case RID_TYPEDEF:
4227 case RID_SHORT:
4228 case RID_INLINE:
4229 case RID_NORETURN:
4230 case RID_VOLATILE:
4231 case RID_SIGNED:
4232 case RID_AUTO:
4233 case RID_RESTRICT:
4234 case RID_COMPLEX:
4235 case RID_THREAD:
4236 case RID_INT:
4237 case RID_CHAR:
4238 case RID_FLOAT:
4239 case RID_DOUBLE:
4240 case RID_VOID:
4241 case RID_DFLOAT32:
4242 case RID_DFLOAT64:
4243 case RID_DFLOAT128:
4244 CASE_RID_FLOATN_NX:
4245 case RID_BOOL:
4246 case RID_FRACT:
4247 case RID_ACCUM:
4248 case RID_SAT:
4249 case RID_TRANSACTION_ATOMIC:
4250 case RID_TRANSACTION_CANCEL:
4251 case RID_ATOMIC:
4252 case RID_AUTO_TYPE:
4253 case RID_INT_N_0:
4254 case RID_INT_N_1:
4255 case RID_INT_N_2:
4256 case RID_INT_N_3:
4257 ok = true;
4258 break;
4259 default:
4260 ok = false;
4261 break;
4263 if (!ok)
4264 return NULL_TREE;
4266 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4267 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4269 else if (c_parser_next_token_is (parser, CPP_NAME))
4270 attr_name = c_parser_peek_token (parser)->value;
4272 return attr_name;
4275 /* Parse (possibly empty) attributes. This is a GNU extension.
4277 attributes:
4278 empty
4279 attributes attribute
4281 attribute:
4282 __attribute__ ( ( attribute-list ) )
4284 attribute-list:
4285 attrib
4286 attribute_list , attrib
4288 attrib:
4289 empty
4290 any-word
4291 any-word ( identifier )
4292 any-word ( identifier , nonempty-expr-list )
4293 any-word ( expr-list )
4295 where the "identifier" must not be declared as a type, and
4296 "any-word" may be any identifier (including one declared as a
4297 type), a reserved word storage class specifier, type specifier or
4298 type qualifier. ??? This still leaves out most reserved keywords
4299 (following the old parser), shouldn't we include them, and why not
4300 allow identifiers declared as types to start the arguments? */
4302 static tree
4303 c_parser_attributes (c_parser *parser)
4305 tree attrs = NULL_TREE;
4306 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4308 /* ??? Follow the C++ parser rather than using the
4309 lex_untranslated_string kludge. */
4310 parser->lex_untranslated_string = true;
4311 /* Consume the `__attribute__' keyword. */
4312 c_parser_consume_token (parser);
4313 /* Look for the two `(' tokens. */
4314 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4316 parser->lex_untranslated_string = false;
4317 return attrs;
4319 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4321 parser->lex_untranslated_string = false;
4322 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4323 return attrs;
4325 /* Parse the attribute list. */
4326 while (c_parser_next_token_is (parser, CPP_COMMA)
4327 || c_parser_next_token_is (parser, CPP_NAME)
4328 || c_parser_next_token_is (parser, CPP_KEYWORD))
4330 tree attr, attr_name, attr_args;
4331 vec<tree, va_gc> *expr_list;
4332 if (c_parser_next_token_is (parser, CPP_COMMA))
4334 c_parser_consume_token (parser);
4335 continue;
4338 attr_name = c_parser_attribute_any_word (parser);
4339 if (attr_name == NULL)
4340 break;
4341 attr_name = canonicalize_attr_name (attr_name);
4342 c_parser_consume_token (parser);
4343 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4345 attr = build_tree_list (attr_name, NULL_TREE);
4346 /* Add this attribute to the list. */
4347 attrs = chainon (attrs, attr);
4348 /* If the next token isn't a comma, we're done. */
4349 if (!c_parser_next_token_is (parser, CPP_COMMA))
4350 break;
4351 continue;
4353 c_parser_consume_token (parser);
4354 /* Parse the attribute contents. If they start with an
4355 identifier which is followed by a comma or close
4356 parenthesis, then the arguments start with that
4357 identifier; otherwise they are an expression list.
4358 In objective-c the identifier may be a classname. */
4359 if (c_parser_next_token_is (parser, CPP_NAME)
4360 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4361 || (c_dialect_objc ()
4362 && c_parser_peek_token (parser)->id_kind
4363 == C_ID_CLASSNAME))
4364 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4365 || (c_parser_peek_2nd_token (parser)->type
4366 == CPP_CLOSE_PAREN))
4367 && (attribute_takes_identifier_p (attr_name)
4368 || (c_dialect_objc ()
4369 && c_parser_peek_token (parser)->id_kind
4370 == C_ID_CLASSNAME)))
4372 tree arg1 = c_parser_peek_token (parser)->value;
4373 c_parser_consume_token (parser);
4374 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4375 attr_args = build_tree_list (NULL_TREE, arg1);
4376 else
4378 tree tree_list;
4379 c_parser_consume_token (parser);
4380 expr_list = c_parser_expr_list (parser, false, true,
4381 NULL, NULL, NULL, NULL);
4382 tree_list = build_tree_list_vec (expr_list);
4383 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4384 release_tree_vector (expr_list);
4387 else
4389 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4390 attr_args = NULL_TREE;
4391 else
4393 expr_list = c_parser_expr_list (parser, false, true,
4394 NULL, NULL, NULL, NULL);
4395 attr_args = build_tree_list_vec (expr_list);
4396 release_tree_vector (expr_list);
4400 attr = build_tree_list (attr_name, attr_args);
4401 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4402 c_parser_consume_token (parser);
4403 else
4405 parser->lex_untranslated_string = false;
4406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4407 "expected %<)%>");
4408 return attrs;
4410 /* Add this attribute to the list. */
4411 attrs = chainon (attrs, attr);
4412 /* If the next token isn't a comma, we're done. */
4413 if (!c_parser_next_token_is (parser, CPP_COMMA))
4414 break;
4416 /* Look for the two `)' tokens. */
4417 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4418 c_parser_consume_token (parser);
4419 else
4421 parser->lex_untranslated_string = false;
4422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4423 "expected %<)%>");
4424 return attrs;
4426 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4427 c_parser_consume_token (parser);
4428 else
4430 parser->lex_untranslated_string = false;
4431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4432 "expected %<)%>");
4433 return attrs;
4435 parser->lex_untranslated_string = false;
4438 return attrs;
4441 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4442 says whether alignment specifiers are OK (only in cases that might
4443 be the type name of a compound literal).
4445 type-name:
4446 specifier-qualifier-list abstract-declarator[opt]
4449 struct c_type_name *
4450 c_parser_type_name (c_parser *parser, bool alignas_ok)
4452 struct c_declspecs *specs = build_null_declspecs ();
4453 struct c_declarator *declarator;
4454 struct c_type_name *ret;
4455 bool dummy = false;
4456 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4457 cla_prefer_type);
4458 if (!specs->declspecs_seen_p)
4460 c_parser_error (parser, "expected specifier-qualifier-list");
4461 return NULL;
4463 if (specs->type != error_mark_node)
4465 pending_xref_error ();
4466 finish_declspecs (specs);
4468 declarator = c_parser_declarator (parser,
4469 specs->typespec_kind != ctsk_none,
4470 C_DTR_ABSTRACT, &dummy);
4471 if (declarator == NULL)
4472 return NULL;
4473 ret = XOBNEW (&parser_obstack, struct c_type_name);
4474 ret->specs = specs;
4475 ret->declarator = declarator;
4476 return ret;
4479 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4481 initializer:
4482 assignment-expression
4483 { initializer-list }
4484 { initializer-list , }
4486 initializer-list:
4487 designation[opt] initializer
4488 initializer-list , designation[opt] initializer
4490 designation:
4491 designator-list =
4493 designator-list:
4494 designator
4495 designator-list designator
4497 designator:
4498 array-designator
4499 . identifier
4501 array-designator:
4502 [ constant-expression ]
4504 GNU extensions:
4506 initializer:
4509 designation:
4510 array-designator
4511 identifier :
4513 array-designator:
4514 [ constant-expression ... constant-expression ]
4516 Any expression without commas is accepted in the syntax for the
4517 constant-expressions, with non-constant expressions rejected later.
4519 This function is only used for top-level initializers; for nested
4520 ones, see c_parser_initval. */
4522 static struct c_expr
4523 c_parser_initializer (c_parser *parser)
4525 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4526 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4527 else
4529 struct c_expr ret;
4530 location_t loc = c_parser_peek_token (parser)->location;
4531 ret = c_parser_expr_no_commas (parser, NULL);
4532 if (TREE_CODE (ret.value) != STRING_CST
4533 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4534 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4535 return ret;
4539 /* The location of the last comma within the current initializer list,
4540 or UNKNOWN_LOCATION if not within one. */
4542 location_t last_init_list_comma;
4544 /* Parse a braced initializer list. TYPE is the type specified for a
4545 compound literal, and NULL_TREE for other initializers and for
4546 nested braced lists. NESTED_P is true for nested braced lists,
4547 false for the list of a compound literal or the list that is the
4548 top-level initializer in a declaration. */
4550 static struct c_expr
4551 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4552 struct obstack *outer_obstack)
4554 struct c_expr ret;
4555 struct obstack braced_init_obstack;
4556 location_t brace_loc = c_parser_peek_token (parser)->location;
4557 gcc_obstack_init (&braced_init_obstack);
4558 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4559 matching_braces braces;
4560 braces.consume_open (parser);
4561 if (nested_p)
4563 finish_implicit_inits (brace_loc, outer_obstack);
4564 push_init_level (brace_loc, 0, &braced_init_obstack);
4566 else
4567 really_start_incremental_init (type);
4568 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4570 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4572 else
4574 /* Parse a non-empty initializer list, possibly with a trailing
4575 comma. */
4576 while (true)
4578 c_parser_initelt (parser, &braced_init_obstack);
4579 if (parser->error)
4580 break;
4581 if (c_parser_next_token_is (parser, CPP_COMMA))
4583 last_init_list_comma = c_parser_peek_token (parser)->location;
4584 c_parser_consume_token (parser);
4586 else
4587 break;
4588 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4589 break;
4592 c_token *next_tok = c_parser_peek_token (parser);
4593 if (next_tok->type != CPP_CLOSE_BRACE)
4595 ret.set_error ();
4596 ret.original_code = ERROR_MARK;
4597 ret.original_type = NULL;
4598 braces.skip_until_found_close (parser);
4599 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4600 obstack_free (&braced_init_obstack, NULL);
4601 return ret;
4603 location_t close_loc = next_tok->location;
4604 c_parser_consume_token (parser);
4605 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4606 obstack_free (&braced_init_obstack, NULL);
4607 set_c_expr_source_range (&ret, brace_loc, close_loc);
4608 return ret;
4611 /* Parse a nested initializer, including designators. */
4613 static void
4614 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4616 /* Parse any designator or designator list. A single array
4617 designator may have the subsequent "=" omitted in GNU C, but a
4618 longer list or a structure member designator may not. */
4619 if (c_parser_next_token_is (parser, CPP_NAME)
4620 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4622 /* Old-style structure member designator. */
4623 set_init_label (c_parser_peek_token (parser)->location,
4624 c_parser_peek_token (parser)->value,
4625 c_parser_peek_token (parser)->location,
4626 braced_init_obstack);
4627 /* Use the colon as the error location. */
4628 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4629 "obsolete use of designated initializer with %<:%>");
4630 c_parser_consume_token (parser);
4631 c_parser_consume_token (parser);
4633 else
4635 /* des_seen is 0 if there have been no designators, 1 if there
4636 has been a single array designator and 2 otherwise. */
4637 int des_seen = 0;
4638 /* Location of a designator. */
4639 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4640 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4641 || c_parser_next_token_is (parser, CPP_DOT))
4643 int des_prev = des_seen;
4644 if (!des_seen)
4645 des_loc = c_parser_peek_token (parser)->location;
4646 if (des_seen < 2)
4647 des_seen++;
4648 if (c_parser_next_token_is (parser, CPP_DOT))
4650 des_seen = 2;
4651 c_parser_consume_token (parser);
4652 if (c_parser_next_token_is (parser, CPP_NAME))
4654 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4655 c_parser_peek_token (parser)->location,
4656 braced_init_obstack);
4657 c_parser_consume_token (parser);
4659 else
4661 struct c_expr init;
4662 init.set_error ();
4663 init.original_code = ERROR_MARK;
4664 init.original_type = NULL;
4665 c_parser_error (parser, "expected identifier");
4666 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4667 process_init_element (input_location, init, false,
4668 braced_init_obstack);
4669 return;
4672 else
4674 tree first, second;
4675 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4676 location_t array_index_loc = UNKNOWN_LOCATION;
4677 /* ??? Following the old parser, [ objc-receiver
4678 objc-message-args ] is accepted as an initializer,
4679 being distinguished from a designator by what follows
4680 the first assignment expression inside the square
4681 brackets, but after a first array designator a
4682 subsequent square bracket is for Objective-C taken to
4683 start an expression, using the obsolete form of
4684 designated initializer without '=', rather than
4685 possibly being a second level of designation: in LALR
4686 terms, the '[' is shifted rather than reducing
4687 designator to designator-list. */
4688 if (des_prev == 1 && c_dialect_objc ())
4690 des_seen = des_prev;
4691 break;
4693 if (des_prev == 0 && c_dialect_objc ())
4695 /* This might be an array designator or an
4696 Objective-C message expression. If the former,
4697 continue parsing here; if the latter, parse the
4698 remainder of the initializer given the starting
4699 primary-expression. ??? It might make sense to
4700 distinguish when des_prev == 1 as well; see
4701 previous comment. */
4702 tree rec, args;
4703 struct c_expr mexpr;
4704 c_parser_consume_token (parser);
4705 if (c_parser_peek_token (parser)->type == CPP_NAME
4706 && ((c_parser_peek_token (parser)->id_kind
4707 == C_ID_TYPENAME)
4708 || (c_parser_peek_token (parser)->id_kind
4709 == C_ID_CLASSNAME)))
4711 /* Type name receiver. */
4712 tree id = c_parser_peek_token (parser)->value;
4713 c_parser_consume_token (parser);
4714 rec = objc_get_class_reference (id);
4715 goto parse_message_args;
4717 first = c_parser_expr_no_commas (parser, NULL).value;
4718 mark_exp_read (first);
4719 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4720 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4721 goto array_desig_after_first;
4722 /* Expression receiver. So far only one part
4723 without commas has been parsed; there might be
4724 more of the expression. */
4725 rec = first;
4726 while (c_parser_next_token_is (parser, CPP_COMMA))
4728 struct c_expr next;
4729 location_t comma_loc, exp_loc;
4730 comma_loc = c_parser_peek_token (parser)->location;
4731 c_parser_consume_token (parser);
4732 exp_loc = c_parser_peek_token (parser)->location;
4733 next = c_parser_expr_no_commas (parser, NULL);
4734 next = convert_lvalue_to_rvalue (exp_loc, next,
4735 true, true);
4736 rec = build_compound_expr (comma_loc, rec, next.value);
4738 parse_message_args:
4739 /* Now parse the objc-message-args. */
4740 args = c_parser_objc_message_args (parser);
4741 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4742 "expected %<]%>");
4743 mexpr.value
4744 = objc_build_message_expr (rec, args);
4745 mexpr.original_code = ERROR_MARK;
4746 mexpr.original_type = NULL;
4747 /* Now parse and process the remainder of the
4748 initializer, starting with this message
4749 expression as a primary-expression. */
4750 c_parser_initval (parser, &mexpr, braced_init_obstack);
4751 return;
4753 c_parser_consume_token (parser);
4754 array_index_loc = c_parser_peek_token (parser)->location;
4755 first = c_parser_expr_no_commas (parser, NULL).value;
4756 mark_exp_read (first);
4757 array_desig_after_first:
4758 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4760 ellipsis_loc = c_parser_peek_token (parser)->location;
4761 c_parser_consume_token (parser);
4762 second = c_parser_expr_no_commas (parser, NULL).value;
4763 mark_exp_read (second);
4765 else
4766 second = NULL_TREE;
4767 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4769 c_parser_consume_token (parser);
4770 set_init_index (array_index_loc, first, second,
4771 braced_init_obstack);
4772 if (second)
4773 pedwarn (ellipsis_loc, OPT_Wpedantic,
4774 "ISO C forbids specifying range of elements to initialize");
4776 else
4777 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4778 "expected %<]%>");
4781 if (des_seen >= 1)
4783 if (c_parser_next_token_is (parser, CPP_EQ))
4785 pedwarn_c90 (des_loc, OPT_Wpedantic,
4786 "ISO C90 forbids specifying subobject "
4787 "to initialize");
4788 c_parser_consume_token (parser);
4790 else
4792 if (des_seen == 1)
4793 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4794 "obsolete use of designated initializer without %<=%>");
4795 else
4797 struct c_expr init;
4798 init.set_error ();
4799 init.original_code = ERROR_MARK;
4800 init.original_type = NULL;
4801 c_parser_error (parser, "expected %<=%>");
4802 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4803 process_init_element (input_location, init, false,
4804 braced_init_obstack);
4805 return;
4810 c_parser_initval (parser, NULL, braced_init_obstack);
4813 /* Parse a nested initializer; as c_parser_initializer but parses
4814 initializers within braced lists, after any designators have been
4815 applied. If AFTER is not NULL then it is an Objective-C message
4816 expression which is the primary-expression starting the
4817 initializer. */
4819 static void
4820 c_parser_initval (c_parser *parser, struct c_expr *after,
4821 struct obstack * braced_init_obstack)
4823 struct c_expr init;
4824 gcc_assert (!after || c_dialect_objc ());
4825 location_t loc = c_parser_peek_token (parser)->location;
4827 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4828 init = c_parser_braced_init (parser, NULL_TREE, true,
4829 braced_init_obstack);
4830 else
4832 init = c_parser_expr_no_commas (parser, after);
4833 if (init.value != NULL_TREE
4834 && TREE_CODE (init.value) != STRING_CST
4835 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4836 init = convert_lvalue_to_rvalue (loc, init, true, true);
4838 process_init_element (loc, init, false, braced_init_obstack);
4841 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4842 C99 6.8.2, C11 6.8.2).
4844 compound-statement:
4845 { block-item-list[opt] }
4846 { label-declarations block-item-list }
4848 block-item-list:
4849 block-item
4850 block-item-list block-item
4852 block-item:
4853 nested-declaration
4854 statement
4856 nested-declaration:
4857 declaration
4859 GNU extensions:
4861 compound-statement:
4862 { label-declarations block-item-list }
4864 nested-declaration:
4865 __extension__ nested-declaration
4866 nested-function-definition
4868 label-declarations:
4869 label-declaration
4870 label-declarations label-declaration
4872 label-declaration:
4873 __label__ identifier-list ;
4875 Allowing the mixing of declarations and code is new in C99. The
4876 GNU syntax also permits (not shown above) labels at the end of
4877 compound statements, which yield an error. We don't allow labels
4878 on declarations; this might seem like a natural extension, but
4879 there would be a conflict between attributes on the label and
4880 prefix attributes on the declaration. ??? The syntax follows the
4881 old parser in requiring something after label declarations.
4882 Although they are erroneous if the labels declared aren't defined,
4883 is it useful for the syntax to be this way?
4885 OpenACC:
4887 block-item:
4888 openacc-directive
4890 openacc-directive:
4891 update-directive
4893 OpenMP:
4895 block-item:
4896 openmp-directive
4898 openmp-directive:
4899 barrier-directive
4900 flush-directive
4901 taskwait-directive
4902 taskyield-directive
4903 cancel-directive
4904 cancellation-point-directive */
4906 static tree
4907 c_parser_compound_statement (c_parser *parser)
4909 tree stmt;
4910 location_t brace_loc;
4911 brace_loc = c_parser_peek_token (parser)->location;
4912 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4914 /* Ensure a scope is entered and left anyway to avoid confusion
4915 if we have just prepared to enter a function body. */
4916 stmt = c_begin_compound_stmt (true);
4917 c_end_compound_stmt (brace_loc, stmt, true);
4918 return error_mark_node;
4920 stmt = c_begin_compound_stmt (true);
4921 c_parser_compound_statement_nostart (parser);
4923 return c_end_compound_stmt (brace_loc, stmt, true);
4926 /* Parse a compound statement except for the opening brace. This is
4927 used for parsing both compound statements and statement expressions
4928 (which follow different paths to handling the opening). */
4930 static void
4931 c_parser_compound_statement_nostart (c_parser *parser)
4933 bool last_stmt = false;
4934 bool last_label = false;
4935 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4936 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4937 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4939 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4940 c_parser_consume_token (parser);
4941 return;
4943 mark_valid_location_for_stdc_pragma (true);
4944 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4946 /* Read zero or more forward-declarations for labels that nested
4947 functions can jump to. */
4948 mark_valid_location_for_stdc_pragma (false);
4949 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4951 label_loc = c_parser_peek_token (parser)->location;
4952 c_parser_consume_token (parser);
4953 /* Any identifiers, including those declared as type names,
4954 are OK here. */
4955 while (true)
4957 tree label;
4958 if (c_parser_next_token_is_not (parser, CPP_NAME))
4960 c_parser_error (parser, "expected identifier");
4961 break;
4963 label
4964 = declare_label (c_parser_peek_token (parser)->value);
4965 C_DECLARED_LABEL_FLAG (label) = 1;
4966 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4967 c_parser_consume_token (parser);
4968 if (c_parser_next_token_is (parser, CPP_COMMA))
4969 c_parser_consume_token (parser);
4970 else
4971 break;
4973 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4975 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4977 /* We must now have at least one statement, label or declaration. */
4978 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4980 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4981 c_parser_error (parser, "expected declaration or statement");
4982 c_parser_consume_token (parser);
4983 return;
4985 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4987 location_t loc = c_parser_peek_token (parser)->location;
4988 loc = expansion_point_location_if_in_system_header (loc);
4989 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4990 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4991 || (c_parser_next_token_is (parser, CPP_NAME)
4992 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4994 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4995 label_loc = c_parser_peek_2nd_token (parser)->location;
4996 else
4997 label_loc = c_parser_peek_token (parser)->location;
4998 last_label = true;
4999 last_stmt = false;
5000 mark_valid_location_for_stdc_pragma (false);
5001 c_parser_label (parser);
5003 else if (!last_label
5004 && c_parser_next_tokens_start_declaration (parser))
5006 last_label = false;
5007 mark_valid_location_for_stdc_pragma (false);
5008 bool fallthru_attr_p = false;
5009 c_parser_declaration_or_fndef (parser, true, true, true, true,
5010 true, NULL, vNULL, NULL,
5011 &fallthru_attr_p);
5012 if (last_stmt && !fallthru_attr_p)
5013 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5014 "ISO C90 forbids mixed declarations and code");
5015 last_stmt = fallthru_attr_p;
5017 else if (!last_label
5018 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5020 /* __extension__ can start a declaration, but is also an
5021 unary operator that can start an expression. Consume all
5022 but the last of a possible series of __extension__ to
5023 determine which. */
5024 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5025 && (c_parser_peek_2nd_token (parser)->keyword
5026 == RID_EXTENSION))
5027 c_parser_consume_token (parser);
5028 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5030 int ext;
5031 ext = disable_extension_diagnostics ();
5032 c_parser_consume_token (parser);
5033 last_label = false;
5034 mark_valid_location_for_stdc_pragma (false);
5035 c_parser_declaration_or_fndef (parser, true, true, true, true,
5036 true, NULL, vNULL);
5037 /* Following the old parser, __extension__ does not
5038 disable this diagnostic. */
5039 restore_extension_diagnostics (ext);
5040 if (last_stmt)
5041 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5042 "ISO C90 forbids mixed declarations and code");
5043 last_stmt = false;
5045 else
5046 goto statement;
5048 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5050 /* External pragmas, and some omp pragmas, are not associated
5051 with regular c code, and so are not to be considered statements
5052 syntactically. This ensures that the user doesn't put them
5053 places that would turn into syntax errors if the directive
5054 were ignored. */
5055 if (c_parser_pragma (parser,
5056 last_label ? pragma_stmt : pragma_compound,
5057 NULL))
5058 last_label = false, last_stmt = true;
5060 else if (c_parser_next_token_is (parser, CPP_EOF))
5062 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5063 c_parser_error (parser, "expected declaration or statement");
5064 return;
5066 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5068 if (parser->in_if_block)
5070 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5071 error_at (loc, "expected %<}%> before %<else%>");
5072 return;
5074 else
5076 error_at (loc, "%<else%> without a previous %<if%>");
5077 c_parser_consume_token (parser);
5078 continue;
5081 else
5083 statement:
5084 last_label = false;
5085 last_stmt = true;
5086 mark_valid_location_for_stdc_pragma (false);
5087 c_parser_statement_after_labels (parser, NULL);
5090 parser->error = false;
5092 if (last_label)
5093 error_at (label_loc, "label at end of compound statement");
5094 c_parser_consume_token (parser);
5095 /* Restore the value we started with. */
5096 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5099 /* Parse all consecutive labels. */
5101 static void
5102 c_parser_all_labels (c_parser *parser)
5104 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5105 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5106 || (c_parser_next_token_is (parser, CPP_NAME)
5107 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5108 c_parser_label (parser);
5111 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5113 label:
5114 identifier : attributes[opt]
5115 case constant-expression :
5116 default :
5118 GNU extensions:
5120 label:
5121 case constant-expression ... constant-expression :
5123 The use of attributes on labels is a GNU extension. The syntax in
5124 GNU C accepts any expressions without commas, non-constant
5125 expressions being rejected later. */
5127 static void
5128 c_parser_label (c_parser *parser)
5130 location_t loc1 = c_parser_peek_token (parser)->location;
5131 tree label = NULL_TREE;
5133 /* Remember whether this case or a user-defined label is allowed to fall
5134 through to. */
5135 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5137 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5139 tree exp1, exp2;
5140 c_parser_consume_token (parser);
5141 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5142 if (c_parser_next_token_is (parser, CPP_COLON))
5144 c_parser_consume_token (parser);
5145 label = do_case (loc1, exp1, NULL_TREE);
5147 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5149 c_parser_consume_token (parser);
5150 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5151 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5152 label = do_case (loc1, exp1, exp2);
5154 else
5155 c_parser_error (parser, "expected %<:%> or %<...%>");
5157 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5159 c_parser_consume_token (parser);
5160 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5161 label = do_case (loc1, NULL_TREE, NULL_TREE);
5163 else
5165 tree name = c_parser_peek_token (parser)->value;
5166 tree tlab;
5167 tree attrs;
5168 location_t loc2 = c_parser_peek_token (parser)->location;
5169 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5170 c_parser_consume_token (parser);
5171 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5172 c_parser_consume_token (parser);
5173 attrs = c_parser_attributes (parser);
5174 tlab = define_label (loc2, name);
5175 if (tlab)
5177 decl_attributes (&tlab, attrs, 0);
5178 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5181 if (label)
5183 if (TREE_CODE (label) == LABEL_EXPR)
5184 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5185 else
5186 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5188 /* Allow '__attribute__((fallthrough));'. */
5189 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5191 location_t loc = c_parser_peek_token (parser)->location;
5192 tree attrs = c_parser_attributes (parser);
5193 if (attribute_fallthrough_p (attrs))
5195 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5197 tree fn = build_call_expr_internal_loc (loc,
5198 IFN_FALLTHROUGH,
5199 void_type_node, 0);
5200 add_stmt (fn);
5202 else
5203 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5204 "not followed by %<;%>");
5206 else if (attrs != NULL_TREE)
5207 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5208 " can be applied to a null statement");
5210 if (c_parser_next_tokens_start_declaration (parser))
5212 error_at (c_parser_peek_token (parser)->location,
5213 "a label can only be part of a statement and "
5214 "a declaration is not a statement");
5215 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5216 /*static_assert_ok*/ true,
5217 /*empty_ok*/ true, /*nested*/ true,
5218 /*start_attr_ok*/ true, NULL,
5219 vNULL);
5224 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5226 statement:
5227 labeled-statement
5228 compound-statement
5229 expression-statement
5230 selection-statement
5231 iteration-statement
5232 jump-statement
5234 labeled-statement:
5235 label statement
5237 expression-statement:
5238 expression[opt] ;
5240 selection-statement:
5241 if-statement
5242 switch-statement
5244 iteration-statement:
5245 while-statement
5246 do-statement
5247 for-statement
5249 jump-statement:
5250 goto identifier ;
5251 continue ;
5252 break ;
5253 return expression[opt] ;
5255 GNU extensions:
5257 statement:
5258 asm-statement
5260 jump-statement:
5261 goto * expression ;
5263 expression-statement:
5264 attributes ;
5266 Objective-C:
5268 statement:
5269 objc-throw-statement
5270 objc-try-catch-statement
5271 objc-synchronized-statement
5273 objc-throw-statement:
5274 @throw expression ;
5275 @throw ;
5277 OpenACC:
5279 statement:
5280 openacc-construct
5282 openacc-construct:
5283 parallel-construct
5284 kernels-construct
5285 data-construct
5286 loop-construct
5288 parallel-construct:
5289 parallel-directive structured-block
5291 kernels-construct:
5292 kernels-directive structured-block
5294 data-construct:
5295 data-directive structured-block
5297 loop-construct:
5298 loop-directive structured-block
5300 OpenMP:
5302 statement:
5303 openmp-construct
5305 openmp-construct:
5306 parallel-construct
5307 for-construct
5308 simd-construct
5309 for-simd-construct
5310 sections-construct
5311 single-construct
5312 parallel-for-construct
5313 parallel-for-simd-construct
5314 parallel-sections-construct
5315 master-construct
5316 critical-construct
5317 atomic-construct
5318 ordered-construct
5320 parallel-construct:
5321 parallel-directive structured-block
5323 for-construct:
5324 for-directive iteration-statement
5326 simd-construct:
5327 simd-directive iteration-statements
5329 for-simd-construct:
5330 for-simd-directive iteration-statements
5332 sections-construct:
5333 sections-directive section-scope
5335 single-construct:
5336 single-directive structured-block
5338 parallel-for-construct:
5339 parallel-for-directive iteration-statement
5341 parallel-for-simd-construct:
5342 parallel-for-simd-directive iteration-statement
5344 parallel-sections-construct:
5345 parallel-sections-directive section-scope
5347 master-construct:
5348 master-directive structured-block
5350 critical-construct:
5351 critical-directive structured-block
5353 atomic-construct:
5354 atomic-directive expression-statement
5356 ordered-construct:
5357 ordered-directive structured-block
5359 Transactional Memory:
5361 statement:
5362 transaction-statement
5363 transaction-cancel-statement
5365 IF_P is used to track whether there's a (possibly labeled) if statement
5366 which is not enclosed in braces and has an else clause. This is used to
5367 implement -Wparentheses. */
5369 static void
5370 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5372 c_parser_all_labels (parser);
5373 if (loc_after_labels)
5374 *loc_after_labels = c_parser_peek_token (parser)->location;
5375 c_parser_statement_after_labels (parser, if_p, NULL);
5378 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5379 of if-else-if conditions.
5381 IF_P is used to track whether there's a (possibly labeled) if statement
5382 which is not enclosed in braces and has an else clause. This is used to
5383 implement -Wparentheses. */
5385 static void
5386 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5387 vec<tree> *chain)
5389 location_t loc = c_parser_peek_token (parser)->location;
5390 tree stmt = NULL_TREE;
5391 bool in_if_block = parser->in_if_block;
5392 parser->in_if_block = false;
5393 if (if_p != NULL)
5394 *if_p = false;
5396 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5397 add_debug_begin_stmt (loc);
5399 switch (c_parser_peek_token (parser)->type)
5401 case CPP_OPEN_BRACE:
5402 add_stmt (c_parser_compound_statement (parser));
5403 break;
5404 case CPP_KEYWORD:
5405 switch (c_parser_peek_token (parser)->keyword)
5407 case RID_IF:
5408 c_parser_if_statement (parser, if_p, chain);
5409 break;
5410 case RID_SWITCH:
5411 c_parser_switch_statement (parser, if_p);
5412 break;
5413 case RID_WHILE:
5414 c_parser_while_statement (parser, false, 0, if_p);
5415 break;
5416 case RID_DO:
5417 c_parser_do_statement (parser, 0, false);
5418 break;
5419 case RID_FOR:
5420 c_parser_for_statement (parser, false, 0, if_p);
5421 break;
5422 case RID_GOTO:
5423 c_parser_consume_token (parser);
5424 if (c_parser_next_token_is (parser, CPP_NAME))
5426 stmt = c_finish_goto_label (loc,
5427 c_parser_peek_token (parser)->value);
5428 c_parser_consume_token (parser);
5430 else if (c_parser_next_token_is (parser, CPP_MULT))
5432 struct c_expr val;
5434 c_parser_consume_token (parser);
5435 val = c_parser_expression (parser);
5436 val = convert_lvalue_to_rvalue (loc, val, false, true);
5437 stmt = c_finish_goto_ptr (loc, val.value);
5439 else
5440 c_parser_error (parser, "expected identifier or %<*%>");
5441 goto expect_semicolon;
5442 case RID_CONTINUE:
5443 c_parser_consume_token (parser);
5444 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5445 goto expect_semicolon;
5446 case RID_BREAK:
5447 c_parser_consume_token (parser);
5448 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5449 goto expect_semicolon;
5450 case RID_RETURN:
5451 c_parser_consume_token (parser);
5452 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5454 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5455 c_parser_consume_token (parser);
5457 else
5459 location_t xloc = c_parser_peek_token (parser)->location;
5460 struct c_expr expr = c_parser_expression_conv (parser);
5461 mark_exp_read (expr.value);
5462 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5463 expr.value, expr.original_type);
5464 goto expect_semicolon;
5466 break;
5467 case RID_ASM:
5468 stmt = c_parser_asm_statement (parser);
5469 break;
5470 case RID_TRANSACTION_ATOMIC:
5471 case RID_TRANSACTION_RELAXED:
5472 stmt = c_parser_transaction (parser,
5473 c_parser_peek_token (parser)->keyword);
5474 break;
5475 case RID_TRANSACTION_CANCEL:
5476 stmt = c_parser_transaction_cancel (parser);
5477 goto expect_semicolon;
5478 case RID_AT_THROW:
5479 gcc_assert (c_dialect_objc ());
5480 c_parser_consume_token (parser);
5481 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5483 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5484 c_parser_consume_token (parser);
5486 else
5488 struct c_expr expr = c_parser_expression (parser);
5489 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5490 expr.value = c_fully_fold (expr.value, false, NULL);
5491 stmt = objc_build_throw_stmt (loc, expr.value);
5492 goto expect_semicolon;
5494 break;
5495 case RID_AT_TRY:
5496 gcc_assert (c_dialect_objc ());
5497 c_parser_objc_try_catch_finally_statement (parser);
5498 break;
5499 case RID_AT_SYNCHRONIZED:
5500 gcc_assert (c_dialect_objc ());
5501 c_parser_objc_synchronized_statement (parser);
5502 break;
5503 case RID_ATTRIBUTE:
5505 /* Allow '__attribute__((fallthrough));'. */
5506 tree attrs = c_parser_attributes (parser);
5507 if (attribute_fallthrough_p (attrs))
5509 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5511 tree fn = build_call_expr_internal_loc (loc,
5512 IFN_FALLTHROUGH,
5513 void_type_node, 0);
5514 add_stmt (fn);
5515 /* Eat the ';'. */
5516 c_parser_consume_token (parser);
5518 else
5519 warning_at (loc, OPT_Wattributes,
5520 "%<fallthrough%> attribute not followed "
5521 "by %<;%>");
5523 else if (attrs != NULL_TREE)
5524 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5525 " can be applied to a null statement");
5526 break;
5528 default:
5529 goto expr_stmt;
5531 break;
5532 case CPP_SEMICOLON:
5533 c_parser_consume_token (parser);
5534 break;
5535 case CPP_CLOSE_PAREN:
5536 case CPP_CLOSE_SQUARE:
5537 /* Avoid infinite loop in error recovery:
5538 c_parser_skip_until_found stops at a closing nesting
5539 delimiter without consuming it, but here we need to consume
5540 it to proceed further. */
5541 c_parser_error (parser, "expected statement");
5542 c_parser_consume_token (parser);
5543 break;
5544 case CPP_PRAGMA:
5545 c_parser_pragma (parser, pragma_stmt, if_p);
5546 break;
5547 default:
5548 expr_stmt:
5549 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5550 expect_semicolon:
5551 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5552 break;
5554 /* Two cases cannot and do not have line numbers associated: If stmt
5555 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5556 cannot hold line numbers. But that's OK because the statement
5557 will either be changed to a MODIFY_EXPR during gimplification of
5558 the statement expr, or discarded. If stmt was compound, but
5559 without new variables, we will have skipped the creation of a
5560 BIND and will have a bare STATEMENT_LIST. But that's OK because
5561 (recursively) all of the component statements should already have
5562 line numbers assigned. ??? Can we discard no-op statements
5563 earlier? */
5564 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5565 protected_set_expr_location (stmt, loc);
5567 parser->in_if_block = in_if_block;
5570 /* Parse the condition from an if, do, while or for statements. */
5572 static tree
5573 c_parser_condition (c_parser *parser)
5575 location_t loc = c_parser_peek_token (parser)->location;
5576 tree cond;
5577 cond = c_parser_expression_conv (parser).value;
5578 cond = c_objc_common_truthvalue_conversion (loc, cond);
5579 cond = c_fully_fold (cond, false, NULL);
5580 if (warn_sequence_point)
5581 verify_sequence_points (cond);
5582 return cond;
5585 /* Parse a parenthesized condition from an if, do or while statement.
5587 condition:
5588 ( expression )
5590 static tree
5591 c_parser_paren_condition (c_parser *parser)
5593 tree cond;
5594 matching_parens parens;
5595 if (!parens.require_open (parser))
5596 return error_mark_node;
5597 cond = c_parser_condition (parser);
5598 parens.skip_until_found_close (parser);
5599 return cond;
5602 /* Parse a statement which is a block in C99.
5604 IF_P is used to track whether there's a (possibly labeled) if statement
5605 which is not enclosed in braces and has an else clause. This is used to
5606 implement -Wparentheses. */
5608 static tree
5609 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5610 location_t *loc_after_labels)
5612 tree block = c_begin_compound_stmt (flag_isoc99);
5613 location_t loc = c_parser_peek_token (parser)->location;
5614 c_parser_statement (parser, if_p, loc_after_labels);
5615 return c_end_compound_stmt (loc, block, flag_isoc99);
5618 /* Parse the body of an if statement. This is just parsing a
5619 statement but (a) it is a block in C99, (b) we track whether the
5620 body is an if statement for the sake of -Wparentheses warnings, (c)
5621 we handle an empty body specially for the sake of -Wempty-body
5622 warnings, and (d) we call parser_compound_statement directly
5623 because c_parser_statement_after_labels resets
5624 parser->in_if_block.
5626 IF_P is used to track whether there's a (possibly labeled) if statement
5627 which is not enclosed in braces and has an else clause. This is used to
5628 implement -Wparentheses. */
5630 static tree
5631 c_parser_if_body (c_parser *parser, bool *if_p,
5632 const token_indent_info &if_tinfo)
5634 tree block = c_begin_compound_stmt (flag_isoc99);
5635 location_t body_loc = c_parser_peek_token (parser)->location;
5636 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5637 token_indent_info body_tinfo
5638 = get_token_indent_info (c_parser_peek_token (parser));
5640 c_parser_all_labels (parser);
5641 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5643 location_t loc = c_parser_peek_token (parser)->location;
5644 add_stmt (build_empty_stmt (loc));
5645 c_parser_consume_token (parser);
5646 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5647 warning_at (loc, OPT_Wempty_body,
5648 "suggest braces around empty body in an %<if%> statement");
5650 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5651 add_stmt (c_parser_compound_statement (parser));
5652 else
5654 body_loc_after_labels = c_parser_peek_token (parser)->location;
5655 c_parser_statement_after_labels (parser, if_p);
5658 token_indent_info next_tinfo
5659 = get_token_indent_info (c_parser_peek_token (parser));
5660 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5661 if (body_loc_after_labels != UNKNOWN_LOCATION
5662 && next_tinfo.type != CPP_SEMICOLON)
5663 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5664 if_tinfo.location, RID_IF);
5666 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5669 /* Parse the else body of an if statement. This is just parsing a
5670 statement but (a) it is a block in C99, (b) we handle an empty body
5671 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5672 of if-else-if conditions. */
5674 static tree
5675 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5676 vec<tree> *chain)
5678 location_t body_loc = c_parser_peek_token (parser)->location;
5679 tree block = c_begin_compound_stmt (flag_isoc99);
5680 token_indent_info body_tinfo
5681 = get_token_indent_info (c_parser_peek_token (parser));
5682 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5684 c_parser_all_labels (parser);
5685 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5687 location_t loc = c_parser_peek_token (parser)->location;
5688 warning_at (loc,
5689 OPT_Wempty_body,
5690 "suggest braces around empty body in an %<else%> statement");
5691 add_stmt (build_empty_stmt (loc));
5692 c_parser_consume_token (parser);
5694 else
5696 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5697 body_loc_after_labels = c_parser_peek_token (parser)->location;
5698 c_parser_statement_after_labels (parser, NULL, chain);
5701 token_indent_info next_tinfo
5702 = get_token_indent_info (c_parser_peek_token (parser));
5703 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5704 if (body_loc_after_labels != UNKNOWN_LOCATION
5705 && next_tinfo.type != CPP_SEMICOLON)
5706 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5707 else_tinfo.location, RID_ELSE);
5709 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5712 /* We might need to reclassify any previously-lexed identifier, e.g.
5713 when we've left a for loop with an if-statement without else in the
5714 body - we might have used a wrong scope for the token. See PR67784. */
5716 static void
5717 c_parser_maybe_reclassify_token (c_parser *parser)
5719 if (c_parser_next_token_is (parser, CPP_NAME))
5721 c_token *token = c_parser_peek_token (parser);
5723 if (token->id_kind != C_ID_CLASSNAME)
5725 tree decl = lookup_name (token->value);
5727 token->id_kind = C_ID_ID;
5728 if (decl)
5730 if (TREE_CODE (decl) == TYPE_DECL)
5731 token->id_kind = C_ID_TYPENAME;
5733 else if (c_dialect_objc ())
5735 tree objc_interface_decl = objc_is_class_name (token->value);
5736 /* Objective-C class names are in the same namespace as
5737 variables and typedefs, and hence are shadowed by local
5738 declarations. */
5739 if (objc_interface_decl)
5741 token->value = objc_interface_decl;
5742 token->id_kind = C_ID_CLASSNAME;
5749 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5751 if-statement:
5752 if ( expression ) statement
5753 if ( expression ) statement else statement
5755 CHAIN is a vector of if-else-if conditions.
5756 IF_P is used to track whether there's a (possibly labeled) if statement
5757 which is not enclosed in braces and has an else clause. This is used to
5758 implement -Wparentheses. */
5760 static void
5761 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5763 tree block;
5764 location_t loc;
5765 tree cond;
5766 bool nested_if = false;
5767 tree first_body, second_body;
5768 bool in_if_block;
5770 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5771 token_indent_info if_tinfo
5772 = get_token_indent_info (c_parser_peek_token (parser));
5773 c_parser_consume_token (parser);
5774 block = c_begin_compound_stmt (flag_isoc99);
5775 loc = c_parser_peek_token (parser)->location;
5776 cond = c_parser_paren_condition (parser);
5777 in_if_block = parser->in_if_block;
5778 parser->in_if_block = true;
5779 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5780 parser->in_if_block = in_if_block;
5782 if (warn_duplicated_cond)
5783 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5785 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5787 token_indent_info else_tinfo
5788 = get_token_indent_info (c_parser_peek_token (parser));
5789 c_parser_consume_token (parser);
5790 if (warn_duplicated_cond)
5792 if (c_parser_next_token_is_keyword (parser, RID_IF)
5793 && chain == NULL)
5795 /* We've got "if (COND) else if (COND2)". Start the
5796 condition chain and add COND as the first element. */
5797 chain = new vec<tree> ();
5798 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5799 chain->safe_push (cond);
5801 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5803 /* This is if-else without subsequent if. Zap the condition
5804 chain; we would have already warned at this point. */
5805 delete chain;
5806 chain = NULL;
5809 second_body = c_parser_else_body (parser, else_tinfo, chain);
5810 /* Set IF_P to true to indicate that this if statement has an
5811 else clause. This may trigger the Wparentheses warning
5812 below when we get back up to the parent if statement. */
5813 if (if_p != NULL)
5814 *if_p = true;
5816 else
5818 second_body = NULL_TREE;
5820 /* Diagnose an ambiguous else if if-then-else is nested inside
5821 if-then. */
5822 if (nested_if)
5823 warning_at (loc, OPT_Wdangling_else,
5824 "suggest explicit braces to avoid ambiguous %<else%>");
5826 if (warn_duplicated_cond)
5828 /* This if statement does not have an else clause. We don't
5829 need the condition chain anymore. */
5830 delete chain;
5831 chain = NULL;
5834 c_finish_if_stmt (loc, cond, first_body, second_body);
5835 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5837 c_parser_maybe_reclassify_token (parser);
5840 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5842 switch-statement:
5843 switch (expression) statement
5846 static void
5847 c_parser_switch_statement (c_parser *parser, bool *if_p)
5849 struct c_expr ce;
5850 tree block, expr, body, save_break;
5851 location_t switch_loc = c_parser_peek_token (parser)->location;
5852 location_t switch_cond_loc;
5853 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5854 c_parser_consume_token (parser);
5855 block = c_begin_compound_stmt (flag_isoc99);
5856 bool explicit_cast_p = false;
5857 matching_parens parens;
5858 if (parens.require_open (parser))
5860 switch_cond_loc = c_parser_peek_token (parser)->location;
5861 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5862 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5863 explicit_cast_p = true;
5864 ce = c_parser_expression (parser);
5865 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5866 expr = ce.value;
5867 /* ??? expr has no valid location? */
5868 parens.skip_until_found_close (parser);
5870 else
5872 switch_cond_loc = UNKNOWN_LOCATION;
5873 expr = error_mark_node;
5874 ce.original_type = error_mark_node;
5876 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5877 save_break = c_break_label;
5878 c_break_label = NULL_TREE;
5879 location_t loc_after_labels;
5880 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5881 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5882 location_t next_loc = c_parser_peek_token (parser)->location;
5883 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5884 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5885 RID_SWITCH);
5886 if (c_break_label)
5888 location_t here = c_parser_peek_token (parser)->location;
5889 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5890 SET_EXPR_LOCATION (t, here);
5891 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5892 append_to_statement_list_force (t, &body);
5894 c_finish_case (body, ce.original_type);
5895 c_break_label = save_break;
5896 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5897 c_parser_maybe_reclassify_token (parser);
5900 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5902 while-statement:
5903 while (expression) statement
5905 IF_P is used to track whether there's a (possibly labeled) if statement
5906 which is not enclosed in braces and has an else clause. This is used to
5907 implement -Wparentheses. */
5909 static void
5910 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5911 bool *if_p)
5913 tree block, cond, body, save_break, save_cont;
5914 location_t loc;
5915 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5916 token_indent_info while_tinfo
5917 = get_token_indent_info (c_parser_peek_token (parser));
5918 c_parser_consume_token (parser);
5919 block = c_begin_compound_stmt (flag_isoc99);
5920 loc = c_parser_peek_token (parser)->location;
5921 cond = c_parser_paren_condition (parser);
5922 if (ivdep && cond != error_mark_node)
5923 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5924 build_int_cst (integer_type_node,
5925 annot_expr_ivdep_kind),
5926 integer_zero_node);
5927 if (unroll && cond != error_mark_node)
5928 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5929 build_int_cst (integer_type_node,
5930 annot_expr_unroll_kind),
5931 build_int_cst (integer_type_node, unroll));
5932 save_break = c_break_label;
5933 c_break_label = NULL_TREE;
5934 save_cont = c_cont_label;
5935 c_cont_label = NULL_TREE;
5937 token_indent_info body_tinfo
5938 = get_token_indent_info (c_parser_peek_token (parser));
5940 location_t loc_after_labels;
5941 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5942 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5943 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5944 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5945 c_parser_maybe_reclassify_token (parser);
5947 token_indent_info next_tinfo
5948 = get_token_indent_info (c_parser_peek_token (parser));
5949 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5951 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5952 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5953 while_tinfo.location, RID_WHILE);
5955 c_break_label = save_break;
5956 c_cont_label = save_cont;
5959 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5961 do-statement:
5962 do statement while ( expression ) ;
5965 static void
5966 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
5968 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5969 location_t loc;
5970 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5971 c_parser_consume_token (parser);
5972 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5973 warning_at (c_parser_peek_token (parser)->location,
5974 OPT_Wempty_body,
5975 "suggest braces around empty body in %<do%> statement");
5976 block = c_begin_compound_stmt (flag_isoc99);
5977 loc = c_parser_peek_token (parser)->location;
5978 save_break = c_break_label;
5979 c_break_label = NULL_TREE;
5980 save_cont = c_cont_label;
5981 c_cont_label = NULL_TREE;
5982 body = c_parser_c99_block_statement (parser, NULL);
5983 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5984 new_break = c_break_label;
5985 c_break_label = save_break;
5986 new_cont = c_cont_label;
5987 c_cont_label = save_cont;
5988 cond = c_parser_paren_condition (parser);
5989 if (ivdep && cond != error_mark_node)
5990 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5991 build_int_cst (integer_type_node,
5992 annot_expr_ivdep_kind),
5993 integer_zero_node);
5994 if (unroll && cond != error_mark_node)
5995 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5996 build_int_cst (integer_type_node,
5997 annot_expr_unroll_kind),
5998 build_int_cst (integer_type_node, unroll));
5999 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6000 c_parser_skip_to_end_of_block_or_statement (parser);
6001 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
6002 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6005 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6007 for-statement:
6008 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6009 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6011 The form with a declaration is new in C99.
6013 ??? In accordance with the old parser, the declaration may be a
6014 nested function, which is then rejected in check_for_loop_decls,
6015 but does it make any sense for this to be included in the grammar?
6016 Note in particular that the nested function does not include a
6017 trailing ';', whereas the "declaration" production includes one.
6018 Also, can we reject bad declarations earlier and cheaper than
6019 check_for_loop_decls?
6021 In Objective-C, there are two additional variants:
6023 foreach-statement:
6024 for ( expression in expresssion ) statement
6025 for ( declaration in expression ) statement
6027 This is inconsistent with C, because the second variant is allowed
6028 even if c99 is not enabled.
6030 The rest of the comment documents these Objective-C foreach-statement.
6032 Here is the canonical example of the first variant:
6033 for (object in array) { do something with object }
6034 we call the first expression ("object") the "object_expression" and
6035 the second expression ("array") the "collection_expression".
6036 object_expression must be an lvalue of type "id" (a generic Objective-C
6037 object) because the loop works by assigning to object_expression the
6038 various objects from the collection_expression. collection_expression
6039 must evaluate to something of type "id" which responds to the method
6040 countByEnumeratingWithState:objects:count:.
6042 The canonical example of the second variant is:
6043 for (id object in array) { do something with object }
6044 which is completely equivalent to
6046 id object;
6047 for (object in array) { do something with object }
6049 Note that initizializing 'object' in some way (eg, "for ((object =
6050 xxx) in array) { do something with object }") is possibly
6051 technically valid, but completely pointless as 'object' will be
6052 assigned to something else as soon as the loop starts. We should
6053 most likely reject it (TODO).
6055 The beginning of the Objective-C foreach-statement looks exactly
6056 like the beginning of the for-statement, and we can tell it is a
6057 foreach-statement only because the initial declaration or
6058 expression is terminated by 'in' instead of ';'.
6060 IF_P is used to track whether there's a (possibly labeled) if statement
6061 which is not enclosed in braces and has an else clause. This is used to
6062 implement -Wparentheses. */
6064 static void
6065 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6066 bool *if_p)
6068 tree block, cond, incr, save_break, save_cont, body;
6069 /* The following are only used when parsing an ObjC foreach statement. */
6070 tree object_expression;
6071 /* Silence the bogus uninitialized warning. */
6072 tree collection_expression = NULL;
6073 location_t loc = c_parser_peek_token (parser)->location;
6074 location_t for_loc = c_parser_peek_token (parser)->location;
6075 bool is_foreach_statement = false;
6076 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6077 token_indent_info for_tinfo
6078 = get_token_indent_info (c_parser_peek_token (parser));
6079 c_parser_consume_token (parser);
6080 /* Open a compound statement in Objective-C as well, just in case this is
6081 as foreach expression. */
6082 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6083 cond = error_mark_node;
6084 incr = error_mark_node;
6085 matching_parens parens;
6086 if (parens.require_open (parser))
6088 /* Parse the initialization declaration or expression. */
6089 object_expression = error_mark_node;
6090 parser->objc_could_be_foreach_context = c_dialect_objc ();
6091 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6093 parser->objc_could_be_foreach_context = false;
6094 c_parser_consume_token (parser);
6095 c_finish_expr_stmt (loc, NULL_TREE);
6097 else if (c_parser_next_tokens_start_declaration (parser))
6099 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6100 &object_expression, vNULL);
6101 parser->objc_could_be_foreach_context = false;
6103 if (c_parser_next_token_is_keyword (parser, RID_IN))
6105 c_parser_consume_token (parser);
6106 is_foreach_statement = true;
6107 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6108 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6110 else
6111 check_for_loop_decls (for_loc, flag_isoc99);
6113 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6115 /* __extension__ can start a declaration, but is also an
6116 unary operator that can start an expression. Consume all
6117 but the last of a possible series of __extension__ to
6118 determine which. */
6119 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6120 && (c_parser_peek_2nd_token (parser)->keyword
6121 == RID_EXTENSION))
6122 c_parser_consume_token (parser);
6123 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6125 int ext;
6126 ext = disable_extension_diagnostics ();
6127 c_parser_consume_token (parser);
6128 c_parser_declaration_or_fndef (parser, true, true, true, true,
6129 true, &object_expression, vNULL);
6130 parser->objc_could_be_foreach_context = false;
6132 restore_extension_diagnostics (ext);
6133 if (c_parser_next_token_is_keyword (parser, RID_IN))
6135 c_parser_consume_token (parser);
6136 is_foreach_statement = true;
6137 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6138 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6140 else
6141 check_for_loop_decls (for_loc, flag_isoc99);
6143 else
6144 goto init_expr;
6146 else
6148 init_expr:
6150 struct c_expr ce;
6151 tree init_expression;
6152 ce = c_parser_expression (parser);
6153 init_expression = ce.value;
6154 parser->objc_could_be_foreach_context = false;
6155 if (c_parser_next_token_is_keyword (parser, RID_IN))
6157 c_parser_consume_token (parser);
6158 is_foreach_statement = true;
6159 if (! lvalue_p (init_expression))
6160 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6161 object_expression = c_fully_fold (init_expression, false, NULL);
6163 else
6165 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6166 init_expression = ce.value;
6167 c_finish_expr_stmt (loc, init_expression);
6168 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6172 /* Parse the loop condition. In the case of a foreach
6173 statement, there is no loop condition. */
6174 gcc_assert (!parser->objc_could_be_foreach_context);
6175 if (!is_foreach_statement)
6177 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6179 if (ivdep)
6181 c_parser_error (parser, "missing loop condition in loop with "
6182 "%<GCC ivdep%> pragma");
6183 cond = error_mark_node;
6185 else if (unroll)
6187 c_parser_error (parser, "missing loop condition in loop with "
6188 "%<GCC unroll%> pragma");
6189 cond = error_mark_node;
6191 else
6193 c_parser_consume_token (parser);
6194 cond = NULL_TREE;
6197 else
6199 cond = c_parser_condition (parser);
6200 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6201 "expected %<;%>");
6203 if (ivdep && cond != error_mark_node)
6204 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6205 build_int_cst (integer_type_node,
6206 annot_expr_ivdep_kind),
6207 integer_zero_node);
6208 if (unroll && cond != error_mark_node)
6209 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6210 build_int_cst (integer_type_node,
6211 annot_expr_unroll_kind),
6212 build_int_cst (integer_type_node, unroll));
6214 /* Parse the increment expression (the third expression in a
6215 for-statement). In the case of a foreach-statement, this is
6216 the expression that follows the 'in'. */
6217 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6219 if (is_foreach_statement)
6221 c_parser_error (parser, "missing collection in fast enumeration");
6222 collection_expression = error_mark_node;
6224 else
6225 incr = c_process_expr_stmt (loc, NULL_TREE);
6227 else
6229 if (is_foreach_statement)
6230 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6231 false, NULL);
6232 else
6234 struct c_expr ce = c_parser_expression (parser);
6235 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6236 incr = c_process_expr_stmt (loc, ce.value);
6239 parens.skip_until_found_close (parser);
6241 save_break = c_break_label;
6242 c_break_label = NULL_TREE;
6243 save_cont = c_cont_label;
6244 c_cont_label = NULL_TREE;
6246 token_indent_info body_tinfo
6247 = get_token_indent_info (c_parser_peek_token (parser));
6249 location_t loc_after_labels;
6250 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6251 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6253 if (is_foreach_statement)
6254 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6255 else
6256 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6257 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6258 c_parser_maybe_reclassify_token (parser);
6260 token_indent_info next_tinfo
6261 = get_token_indent_info (c_parser_peek_token (parser));
6262 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6264 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6265 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6266 for_tinfo.location, RID_FOR);
6268 c_break_label = save_break;
6269 c_cont_label = save_cont;
6272 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6273 statement with inputs, outputs, clobbers, and volatile tag
6274 allowed.
6276 asm-statement:
6277 asm type-qualifier[opt] ( asm-argument ) ;
6278 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6280 asm-argument:
6281 asm-string-literal
6282 asm-string-literal : asm-operands[opt]
6283 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6284 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6286 asm-goto-argument:
6287 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6288 : asm-goto-operands
6290 Qualifiers other than volatile are accepted in the syntax but
6291 warned for. */
6293 static tree
6294 c_parser_asm_statement (c_parser *parser)
6296 tree quals, str, outputs, inputs, clobbers, labels, ret;
6297 bool simple, is_goto;
6298 location_t asm_loc = c_parser_peek_token (parser)->location;
6299 int section, nsections;
6301 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6302 c_parser_consume_token (parser);
6303 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6305 quals = c_parser_peek_token (parser)->value;
6306 c_parser_consume_token (parser);
6308 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6309 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6311 warning_at (c_parser_peek_token (parser)->location,
6313 "%E qualifier ignored on asm",
6314 c_parser_peek_token (parser)->value);
6315 quals = NULL_TREE;
6316 c_parser_consume_token (parser);
6318 else
6319 quals = NULL_TREE;
6321 is_goto = false;
6322 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6324 c_parser_consume_token (parser);
6325 is_goto = true;
6328 /* ??? Follow the C++ parser rather than using the
6329 lex_untranslated_string kludge. */
6330 parser->lex_untranslated_string = true;
6331 ret = NULL;
6333 matching_parens parens;
6334 if (!parens.require_open (parser))
6335 goto error;
6337 str = c_parser_asm_string_literal (parser);
6338 if (str == NULL_TREE)
6339 goto error_close_paren;
6341 simple = true;
6342 outputs = NULL_TREE;
6343 inputs = NULL_TREE;
6344 clobbers = NULL_TREE;
6345 labels = NULL_TREE;
6347 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6348 goto done_asm;
6350 /* Parse each colon-delimited section of operands. */
6351 nsections = 3 + is_goto;
6352 for (section = 0; section < nsections; ++section)
6354 if (!c_parser_require (parser, CPP_COLON,
6355 is_goto
6356 ? G_("expected %<:%>")
6357 : G_("expected %<:%> or %<)%>"),
6358 UNKNOWN_LOCATION, is_goto))
6359 goto error_close_paren;
6361 /* Once past any colon, we're no longer a simple asm. */
6362 simple = false;
6364 if ((!c_parser_next_token_is (parser, CPP_COLON)
6365 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6366 || section == 3)
6367 switch (section)
6369 case 0:
6370 /* For asm goto, we don't allow output operands, but reserve
6371 the slot for a future extension that does allow them. */
6372 if (!is_goto)
6373 outputs = c_parser_asm_operands (parser);
6374 break;
6375 case 1:
6376 inputs = c_parser_asm_operands (parser);
6377 break;
6378 case 2:
6379 clobbers = c_parser_asm_clobbers (parser);
6380 break;
6381 case 3:
6382 labels = c_parser_asm_goto_operands (parser);
6383 break;
6384 default:
6385 gcc_unreachable ();
6388 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6389 goto done_asm;
6392 done_asm:
6393 if (!parens.require_close (parser))
6395 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6396 goto error;
6399 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6400 c_parser_skip_to_end_of_block_or_statement (parser);
6402 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6403 clobbers, labels, simple));
6405 error:
6406 parser->lex_untranslated_string = false;
6407 return ret;
6409 error_close_paren:
6410 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6411 goto error;
6414 /* Parse asm operands, a GNU extension.
6416 asm-operands:
6417 asm-operand
6418 asm-operands , asm-operand
6420 asm-operand:
6421 asm-string-literal ( expression )
6422 [ identifier ] asm-string-literal ( expression )
6425 static tree
6426 c_parser_asm_operands (c_parser *parser)
6428 tree list = NULL_TREE;
6429 while (true)
6431 tree name, str;
6432 struct c_expr expr;
6433 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6435 c_parser_consume_token (parser);
6436 if (c_parser_next_token_is (parser, CPP_NAME))
6438 tree id = c_parser_peek_token (parser)->value;
6439 c_parser_consume_token (parser);
6440 name = build_string (IDENTIFIER_LENGTH (id),
6441 IDENTIFIER_POINTER (id));
6443 else
6445 c_parser_error (parser, "expected identifier");
6446 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6447 return NULL_TREE;
6449 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6450 "expected %<]%>");
6452 else
6453 name = NULL_TREE;
6454 str = c_parser_asm_string_literal (parser);
6455 if (str == NULL_TREE)
6456 return NULL_TREE;
6457 parser->lex_untranslated_string = false;
6458 matching_parens parens;
6459 if (!parens.require_open (parser))
6461 parser->lex_untranslated_string = true;
6462 return NULL_TREE;
6464 expr = c_parser_expression (parser);
6465 mark_exp_read (expr.value);
6466 parser->lex_untranslated_string = true;
6467 if (!parens.require_close (parser))
6469 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6470 return NULL_TREE;
6472 list = chainon (list, build_tree_list (build_tree_list (name, str),
6473 expr.value));
6474 if (c_parser_next_token_is (parser, CPP_COMMA))
6475 c_parser_consume_token (parser);
6476 else
6477 break;
6479 return list;
6482 /* Parse asm clobbers, a GNU extension.
6484 asm-clobbers:
6485 asm-string-literal
6486 asm-clobbers , asm-string-literal
6489 static tree
6490 c_parser_asm_clobbers (c_parser *parser)
6492 tree list = NULL_TREE;
6493 while (true)
6495 tree str = c_parser_asm_string_literal (parser);
6496 if (str)
6497 list = tree_cons (NULL_TREE, str, list);
6498 else
6499 return NULL_TREE;
6500 if (c_parser_next_token_is (parser, CPP_COMMA))
6501 c_parser_consume_token (parser);
6502 else
6503 break;
6505 return list;
6508 /* Parse asm goto labels, a GNU extension.
6510 asm-goto-operands:
6511 identifier
6512 asm-goto-operands , identifier
6515 static tree
6516 c_parser_asm_goto_operands (c_parser *parser)
6518 tree list = NULL_TREE;
6519 while (true)
6521 tree name, label;
6523 if (c_parser_next_token_is (parser, CPP_NAME))
6525 c_token *tok = c_parser_peek_token (parser);
6526 name = tok->value;
6527 label = lookup_label_for_goto (tok->location, name);
6528 c_parser_consume_token (parser);
6529 TREE_USED (label) = 1;
6531 else
6533 c_parser_error (parser, "expected identifier");
6534 return NULL_TREE;
6537 name = build_string (IDENTIFIER_LENGTH (name),
6538 IDENTIFIER_POINTER (name));
6539 list = tree_cons (name, label, list);
6540 if (c_parser_next_token_is (parser, CPP_COMMA))
6541 c_parser_consume_token (parser);
6542 else
6543 return nreverse (list);
6547 /* Parse an expression other than a compound expression; that is, an
6548 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6549 AFTER is not NULL then it is an Objective-C message expression which
6550 is the primary-expression starting the expression as an initializer.
6552 assignment-expression:
6553 conditional-expression
6554 unary-expression assignment-operator assignment-expression
6556 assignment-operator: one of
6557 = *= /= %= += -= <<= >>= &= ^= |=
6559 In GNU C we accept any conditional expression on the LHS and
6560 diagnose the invalid lvalue rather than producing a syntax
6561 error. */
6563 static struct c_expr
6564 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6565 tree omp_atomic_lhs)
6567 struct c_expr lhs, rhs, ret;
6568 enum tree_code code;
6569 location_t op_location, exp_location;
6570 gcc_assert (!after || c_dialect_objc ());
6571 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6572 op_location = c_parser_peek_token (parser)->location;
6573 switch (c_parser_peek_token (parser)->type)
6575 case CPP_EQ:
6576 code = NOP_EXPR;
6577 break;
6578 case CPP_MULT_EQ:
6579 code = MULT_EXPR;
6580 break;
6581 case CPP_DIV_EQ:
6582 code = TRUNC_DIV_EXPR;
6583 break;
6584 case CPP_MOD_EQ:
6585 code = TRUNC_MOD_EXPR;
6586 break;
6587 case CPP_PLUS_EQ:
6588 code = PLUS_EXPR;
6589 break;
6590 case CPP_MINUS_EQ:
6591 code = MINUS_EXPR;
6592 break;
6593 case CPP_LSHIFT_EQ:
6594 code = LSHIFT_EXPR;
6595 break;
6596 case CPP_RSHIFT_EQ:
6597 code = RSHIFT_EXPR;
6598 break;
6599 case CPP_AND_EQ:
6600 code = BIT_AND_EXPR;
6601 break;
6602 case CPP_XOR_EQ:
6603 code = BIT_XOR_EXPR;
6604 break;
6605 case CPP_OR_EQ:
6606 code = BIT_IOR_EXPR;
6607 break;
6608 default:
6609 return lhs;
6611 c_parser_consume_token (parser);
6612 exp_location = c_parser_peek_token (parser)->location;
6613 rhs = c_parser_expr_no_commas (parser, NULL);
6614 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6616 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6617 code, exp_location, rhs.value,
6618 rhs.original_type);
6619 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6620 if (code == NOP_EXPR)
6621 ret.original_code = MODIFY_EXPR;
6622 else
6624 TREE_NO_WARNING (ret.value) = 1;
6625 ret.original_code = ERROR_MARK;
6627 ret.original_type = NULL;
6628 return ret;
6631 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6632 AFTER is not NULL then it is an Objective-C message expression which is
6633 the primary-expression starting the expression as an initializer.
6635 conditional-expression:
6636 logical-OR-expression
6637 logical-OR-expression ? expression : conditional-expression
6639 GNU extensions:
6641 conditional-expression:
6642 logical-OR-expression ? : conditional-expression
6645 static struct c_expr
6646 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6647 tree omp_atomic_lhs)
6649 struct c_expr cond, exp1, exp2, ret;
6650 location_t start, cond_loc, colon_loc;
6652 gcc_assert (!after || c_dialect_objc ());
6654 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6656 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6657 return cond;
6658 if (cond.value != error_mark_node)
6659 start = cond.get_start ();
6660 else
6661 start = UNKNOWN_LOCATION;
6662 cond_loc = c_parser_peek_token (parser)->location;
6663 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6664 c_parser_consume_token (parser);
6665 if (c_parser_next_token_is (parser, CPP_COLON))
6667 tree eptype = NULL_TREE;
6669 location_t middle_loc = c_parser_peek_token (parser)->location;
6670 pedwarn (middle_loc, OPT_Wpedantic,
6671 "ISO C forbids omitting the middle term of a ?: expression");
6672 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6674 eptype = TREE_TYPE (cond.value);
6675 cond.value = TREE_OPERAND (cond.value, 0);
6677 tree e = cond.value;
6678 while (TREE_CODE (e) == COMPOUND_EXPR)
6679 e = TREE_OPERAND (e, 1);
6680 warn_for_omitted_condop (middle_loc, e);
6681 /* Make sure first operand is calculated only once. */
6682 exp1.value = save_expr (default_conversion (cond.value));
6683 if (eptype)
6684 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6685 exp1.original_type = NULL;
6686 exp1.src_range = cond.src_range;
6687 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6688 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6690 else
6692 cond.value
6693 = c_objc_common_truthvalue_conversion
6694 (cond_loc, default_conversion (cond.value));
6695 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6696 exp1 = c_parser_expression_conv (parser);
6697 mark_exp_read (exp1.value);
6698 c_inhibit_evaluation_warnings +=
6699 ((cond.value == truthvalue_true_node)
6700 - (cond.value == truthvalue_false_node));
6703 colon_loc = c_parser_peek_token (parser)->location;
6704 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6706 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6707 ret.set_error ();
6708 ret.original_code = ERROR_MARK;
6709 ret.original_type = NULL;
6710 return ret;
6713 location_t exp2_loc = c_parser_peek_token (parser)->location;
6714 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6715 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6717 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6718 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6719 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6720 ret.value = build_conditional_expr (colon_loc, cond.value,
6721 cond.original_code == C_MAYBE_CONST_EXPR,
6722 exp1.value, exp1.original_type, loc1,
6723 exp2.value, exp2.original_type, loc2);
6724 ret.original_code = ERROR_MARK;
6725 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6726 ret.original_type = NULL;
6727 else
6729 tree t1, t2;
6731 /* If both sides are enum type, the default conversion will have
6732 made the type of the result be an integer type. We want to
6733 remember the enum types we started with. */
6734 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6735 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6736 ret.original_type = ((t1 != error_mark_node
6737 && t2 != error_mark_node
6738 && (TYPE_MAIN_VARIANT (t1)
6739 == TYPE_MAIN_VARIANT (t2)))
6740 ? t1
6741 : NULL);
6743 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6744 return ret;
6747 /* Parse a binary expression; that is, a logical-OR-expression (C90
6748 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6749 NULL then it is an Objective-C message expression which is the
6750 primary-expression starting the expression as an initializer.
6752 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6753 when it should be the unfolded lhs. In a valid OpenMP source,
6754 one of the operands of the toplevel binary expression must be equal
6755 to it. In that case, just return a build2 created binary operation
6756 rather than result of parser_build_binary_op.
6758 multiplicative-expression:
6759 cast-expression
6760 multiplicative-expression * cast-expression
6761 multiplicative-expression / cast-expression
6762 multiplicative-expression % cast-expression
6764 additive-expression:
6765 multiplicative-expression
6766 additive-expression + multiplicative-expression
6767 additive-expression - multiplicative-expression
6769 shift-expression:
6770 additive-expression
6771 shift-expression << additive-expression
6772 shift-expression >> additive-expression
6774 relational-expression:
6775 shift-expression
6776 relational-expression < shift-expression
6777 relational-expression > shift-expression
6778 relational-expression <= shift-expression
6779 relational-expression >= shift-expression
6781 equality-expression:
6782 relational-expression
6783 equality-expression == relational-expression
6784 equality-expression != relational-expression
6786 AND-expression:
6787 equality-expression
6788 AND-expression & equality-expression
6790 exclusive-OR-expression:
6791 AND-expression
6792 exclusive-OR-expression ^ AND-expression
6794 inclusive-OR-expression:
6795 exclusive-OR-expression
6796 inclusive-OR-expression | exclusive-OR-expression
6798 logical-AND-expression:
6799 inclusive-OR-expression
6800 logical-AND-expression && inclusive-OR-expression
6802 logical-OR-expression:
6803 logical-AND-expression
6804 logical-OR-expression || logical-AND-expression
6807 static struct c_expr
6808 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6809 tree omp_atomic_lhs)
6811 /* A binary expression is parsed using operator-precedence parsing,
6812 with the operands being cast expressions. All the binary
6813 operators are left-associative. Thus a binary expression is of
6814 form:
6816 E0 op1 E1 op2 E2 ...
6818 which we represent on a stack. On the stack, the precedence
6819 levels are strictly increasing. When a new operator is
6820 encountered of higher precedence than that at the top of the
6821 stack, it is pushed; its LHS is the top expression, and its RHS
6822 is everything parsed until it is popped. When a new operator is
6823 encountered with precedence less than or equal to that at the top
6824 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6825 by the result of the operation until the operator at the top of
6826 the stack has lower precedence than the new operator or there is
6827 only one element on the stack; then the top expression is the LHS
6828 of the new operator. In the case of logical AND and OR
6829 expressions, we also need to adjust c_inhibit_evaluation_warnings
6830 as appropriate when the operators are pushed and popped. */
6832 struct {
6833 /* The expression at this stack level. */
6834 struct c_expr expr;
6835 /* The precedence of the operator on its left, PREC_NONE at the
6836 bottom of the stack. */
6837 enum c_parser_prec prec;
6838 /* The operation on its left. */
6839 enum tree_code op;
6840 /* The source location of this operation. */
6841 location_t loc;
6842 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6843 tree sizeof_arg;
6844 } stack[NUM_PRECS];
6845 int sp;
6846 /* Location of the binary operator. */
6847 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6848 #define POP \
6849 do { \
6850 switch (stack[sp].op) \
6852 case TRUTH_ANDIF_EXPR: \
6853 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6854 == truthvalue_false_node); \
6855 break; \
6856 case TRUTH_ORIF_EXPR: \
6857 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6858 == truthvalue_true_node); \
6859 break; \
6860 case TRUNC_DIV_EXPR: \
6861 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6862 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6864 tree type0 = stack[sp - 1].sizeof_arg; \
6865 tree type1 = stack[sp].sizeof_arg; \
6866 tree first_arg = type0; \
6867 if (!TYPE_P (type0)) \
6868 type0 = TREE_TYPE (type0); \
6869 if (!TYPE_P (type1)) \
6870 type1 = TREE_TYPE (type1); \
6871 if (POINTER_TYPE_P (type0) \
6872 && comptypes (TREE_TYPE (type0), type1) \
6873 && !(TREE_CODE (first_arg) == PARM_DECL \
6874 && C_ARRAY_PARAMETER (first_arg) \
6875 && warn_sizeof_array_argument)) \
6876 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6877 "division %<sizeof (%T) / sizeof (%T)%> does " \
6878 "not compute the number of array elements", \
6879 type0, type1)) \
6880 if (DECL_P (first_arg)) \
6881 inform (DECL_SOURCE_LOCATION (first_arg), \
6882 "first %<sizeof%> operand was declared here"); \
6884 break; \
6885 default: \
6886 break; \
6888 stack[sp - 1].expr \
6889 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6890 stack[sp - 1].expr, true, true); \
6891 stack[sp].expr \
6892 = convert_lvalue_to_rvalue (stack[sp].loc, \
6893 stack[sp].expr, true, true); \
6894 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6895 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6896 && ((1 << stack[sp].prec) \
6897 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6898 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6899 && stack[sp].op != TRUNC_MOD_EXPR \
6900 && stack[0].expr.value != error_mark_node \
6901 && stack[1].expr.value != error_mark_node \
6902 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6903 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6904 stack[0].expr.value \
6905 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6906 stack[0].expr.value, stack[1].expr.value); \
6907 else \
6908 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6909 stack[sp].op, \
6910 stack[sp - 1].expr, \
6911 stack[sp].expr); \
6912 sp--; \
6913 } while (0)
6914 gcc_assert (!after || c_dialect_objc ());
6915 stack[0].loc = c_parser_peek_token (parser)->location;
6916 stack[0].expr = c_parser_cast_expression (parser, after);
6917 stack[0].prec = PREC_NONE;
6918 stack[0].sizeof_arg = c_last_sizeof_arg;
6919 sp = 0;
6920 while (true)
6922 enum c_parser_prec oprec;
6923 enum tree_code ocode;
6924 source_range src_range;
6925 if (parser->error)
6926 goto out;
6927 switch (c_parser_peek_token (parser)->type)
6929 case CPP_MULT:
6930 oprec = PREC_MULT;
6931 ocode = MULT_EXPR;
6932 break;
6933 case CPP_DIV:
6934 oprec = PREC_MULT;
6935 ocode = TRUNC_DIV_EXPR;
6936 break;
6937 case CPP_MOD:
6938 oprec = PREC_MULT;
6939 ocode = TRUNC_MOD_EXPR;
6940 break;
6941 case CPP_PLUS:
6942 oprec = PREC_ADD;
6943 ocode = PLUS_EXPR;
6944 break;
6945 case CPP_MINUS:
6946 oprec = PREC_ADD;
6947 ocode = MINUS_EXPR;
6948 break;
6949 case CPP_LSHIFT:
6950 oprec = PREC_SHIFT;
6951 ocode = LSHIFT_EXPR;
6952 break;
6953 case CPP_RSHIFT:
6954 oprec = PREC_SHIFT;
6955 ocode = RSHIFT_EXPR;
6956 break;
6957 case CPP_LESS:
6958 oprec = PREC_REL;
6959 ocode = LT_EXPR;
6960 break;
6961 case CPP_GREATER:
6962 oprec = PREC_REL;
6963 ocode = GT_EXPR;
6964 break;
6965 case CPP_LESS_EQ:
6966 oprec = PREC_REL;
6967 ocode = LE_EXPR;
6968 break;
6969 case CPP_GREATER_EQ:
6970 oprec = PREC_REL;
6971 ocode = GE_EXPR;
6972 break;
6973 case CPP_EQ_EQ:
6974 oprec = PREC_EQ;
6975 ocode = EQ_EXPR;
6976 break;
6977 case CPP_NOT_EQ:
6978 oprec = PREC_EQ;
6979 ocode = NE_EXPR;
6980 break;
6981 case CPP_AND:
6982 oprec = PREC_BITAND;
6983 ocode = BIT_AND_EXPR;
6984 break;
6985 case CPP_XOR:
6986 oprec = PREC_BITXOR;
6987 ocode = BIT_XOR_EXPR;
6988 break;
6989 case CPP_OR:
6990 oprec = PREC_BITOR;
6991 ocode = BIT_IOR_EXPR;
6992 break;
6993 case CPP_AND_AND:
6994 oprec = PREC_LOGAND;
6995 ocode = TRUTH_ANDIF_EXPR;
6996 break;
6997 case CPP_OR_OR:
6998 oprec = PREC_LOGOR;
6999 ocode = TRUTH_ORIF_EXPR;
7000 break;
7001 default:
7002 /* Not a binary operator, so end of the binary
7003 expression. */
7004 goto out;
7006 binary_loc = c_parser_peek_token (parser)->location;
7007 while (oprec <= stack[sp].prec)
7008 POP;
7009 c_parser_consume_token (parser);
7010 switch (ocode)
7012 case TRUTH_ANDIF_EXPR:
7013 src_range = stack[sp].expr.src_range;
7014 stack[sp].expr
7015 = convert_lvalue_to_rvalue (stack[sp].loc,
7016 stack[sp].expr, true, true);
7017 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7018 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7019 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7020 == truthvalue_false_node);
7021 set_c_expr_source_range (&stack[sp].expr, src_range);
7022 break;
7023 case TRUTH_ORIF_EXPR:
7024 src_range = stack[sp].expr.src_range;
7025 stack[sp].expr
7026 = convert_lvalue_to_rvalue (stack[sp].loc,
7027 stack[sp].expr, true, true);
7028 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7029 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7030 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7031 == truthvalue_true_node);
7032 set_c_expr_source_range (&stack[sp].expr, src_range);
7033 break;
7034 default:
7035 break;
7037 sp++;
7038 stack[sp].loc = binary_loc;
7039 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7040 stack[sp].prec = oprec;
7041 stack[sp].op = ocode;
7042 stack[sp].sizeof_arg = c_last_sizeof_arg;
7044 out:
7045 while (sp > 0)
7046 POP;
7047 return stack[0].expr;
7048 #undef POP
7051 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7052 is not NULL then it is an Objective-C message expression which is the
7053 primary-expression starting the expression as an initializer.
7055 cast-expression:
7056 unary-expression
7057 ( type-name ) unary-expression
7060 static struct c_expr
7061 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7063 location_t cast_loc = c_parser_peek_token (parser)->location;
7064 gcc_assert (!after || c_dialect_objc ());
7065 if (after)
7066 return c_parser_postfix_expression_after_primary (parser,
7067 cast_loc, *after);
7068 /* If the expression begins with a parenthesized type name, it may
7069 be either a cast or a compound literal; we need to see whether
7070 the next character is '{' to tell the difference. If not, it is
7071 an unary expression. Full detection of unknown typenames here
7072 would require a 3-token lookahead. */
7073 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7074 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7076 struct c_type_name *type_name;
7077 struct c_expr ret;
7078 struct c_expr expr;
7079 matching_parens parens;
7080 parens.consume_open (parser);
7081 type_name = c_parser_type_name (parser, true);
7082 parens.skip_until_found_close (parser);
7083 if (type_name == NULL)
7085 ret.set_error ();
7086 ret.original_code = ERROR_MARK;
7087 ret.original_type = NULL;
7088 return ret;
7091 /* Save casted types in the function's used types hash table. */
7092 used_types_insert (type_name->specs->type);
7094 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7095 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7096 cast_loc);
7097 if (type_name->specs->alignas_p)
7098 error_at (type_name->specs->locations[cdw_alignas],
7099 "alignment specified for type name in cast");
7101 location_t expr_loc = c_parser_peek_token (parser)->location;
7102 expr = c_parser_cast_expression (parser, NULL);
7103 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7105 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7106 if (ret.value && expr.value)
7107 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7108 ret.original_code = ERROR_MARK;
7109 ret.original_type = NULL;
7110 return ret;
7112 else
7113 return c_parser_unary_expression (parser);
7116 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7118 unary-expression:
7119 postfix-expression
7120 ++ unary-expression
7121 -- unary-expression
7122 unary-operator cast-expression
7123 sizeof unary-expression
7124 sizeof ( type-name )
7126 unary-operator: one of
7127 & * + - ~ !
7129 GNU extensions:
7131 unary-expression:
7132 __alignof__ unary-expression
7133 __alignof__ ( type-name )
7134 && identifier
7136 (C11 permits _Alignof with type names only.)
7138 unary-operator: one of
7139 __extension__ __real__ __imag__
7141 Transactional Memory:
7143 unary-expression:
7144 transaction-expression
7146 In addition, the GNU syntax treats ++ and -- as unary operators, so
7147 they may be applied to cast expressions with errors for non-lvalues
7148 given later. */
7150 static struct c_expr
7151 c_parser_unary_expression (c_parser *parser)
7153 int ext;
7154 struct c_expr ret, op;
7155 location_t op_loc = c_parser_peek_token (parser)->location;
7156 location_t exp_loc;
7157 location_t finish;
7158 ret.original_code = ERROR_MARK;
7159 ret.original_type = NULL;
7160 switch (c_parser_peek_token (parser)->type)
7162 case CPP_PLUS_PLUS:
7163 c_parser_consume_token (parser);
7164 exp_loc = c_parser_peek_token (parser)->location;
7165 op = c_parser_cast_expression (parser, NULL);
7167 op = default_function_array_read_conversion (exp_loc, op);
7168 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7169 case CPP_MINUS_MINUS:
7170 c_parser_consume_token (parser);
7171 exp_loc = c_parser_peek_token (parser)->location;
7172 op = c_parser_cast_expression (parser, NULL);
7174 op = default_function_array_read_conversion (exp_loc, op);
7175 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7176 case CPP_AND:
7177 c_parser_consume_token (parser);
7178 op = c_parser_cast_expression (parser, NULL);
7179 mark_exp_read (op.value);
7180 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7181 case CPP_MULT:
7183 c_parser_consume_token (parser);
7184 exp_loc = c_parser_peek_token (parser)->location;
7185 op = c_parser_cast_expression (parser, NULL);
7186 finish = op.get_finish ();
7187 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7188 location_t combined_loc = make_location (op_loc, op_loc, finish);
7189 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7190 ret.src_range.m_start = op_loc;
7191 ret.src_range.m_finish = finish;
7192 return ret;
7194 case CPP_PLUS:
7195 if (!c_dialect_objc () && !in_system_header_at (input_location))
7196 warning_at (op_loc,
7197 OPT_Wtraditional,
7198 "traditional C rejects the unary plus operator");
7199 c_parser_consume_token (parser);
7200 exp_loc = c_parser_peek_token (parser)->location;
7201 op = c_parser_cast_expression (parser, NULL);
7202 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7203 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7204 case CPP_MINUS:
7205 c_parser_consume_token (parser);
7206 exp_loc = c_parser_peek_token (parser)->location;
7207 op = c_parser_cast_expression (parser, NULL);
7208 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7209 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7210 case CPP_COMPL:
7211 c_parser_consume_token (parser);
7212 exp_loc = c_parser_peek_token (parser)->location;
7213 op = c_parser_cast_expression (parser, NULL);
7214 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7215 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7216 case CPP_NOT:
7217 c_parser_consume_token (parser);
7218 exp_loc = c_parser_peek_token (parser)->location;
7219 op = c_parser_cast_expression (parser, NULL);
7220 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7221 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7222 case CPP_AND_AND:
7223 /* Refer to the address of a label as a pointer. */
7224 c_parser_consume_token (parser);
7225 if (c_parser_next_token_is (parser, CPP_NAME))
7227 ret.value = finish_label_address_expr
7228 (c_parser_peek_token (parser)->value, op_loc);
7229 set_c_expr_source_range (&ret, op_loc,
7230 c_parser_peek_token (parser)->get_finish ());
7231 c_parser_consume_token (parser);
7233 else
7235 c_parser_error (parser, "expected identifier");
7236 ret.set_error ();
7238 return ret;
7239 case CPP_KEYWORD:
7240 switch (c_parser_peek_token (parser)->keyword)
7242 case RID_SIZEOF:
7243 return c_parser_sizeof_expression (parser);
7244 case RID_ALIGNOF:
7245 return c_parser_alignof_expression (parser);
7246 case RID_EXTENSION:
7247 c_parser_consume_token (parser);
7248 ext = disable_extension_diagnostics ();
7249 ret = c_parser_cast_expression (parser, NULL);
7250 restore_extension_diagnostics (ext);
7251 return ret;
7252 case RID_REALPART:
7253 c_parser_consume_token (parser);
7254 exp_loc = c_parser_peek_token (parser)->location;
7255 op = c_parser_cast_expression (parser, NULL);
7256 op = default_function_array_conversion (exp_loc, op);
7257 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7258 case RID_IMAGPART:
7259 c_parser_consume_token (parser);
7260 exp_loc = c_parser_peek_token (parser)->location;
7261 op = c_parser_cast_expression (parser, NULL);
7262 op = default_function_array_conversion (exp_loc, op);
7263 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7264 case RID_TRANSACTION_ATOMIC:
7265 case RID_TRANSACTION_RELAXED:
7266 return c_parser_transaction_expression (parser,
7267 c_parser_peek_token (parser)->keyword);
7268 default:
7269 return c_parser_postfix_expression (parser);
7271 default:
7272 return c_parser_postfix_expression (parser);
7276 /* Parse a sizeof expression. */
7278 static struct c_expr
7279 c_parser_sizeof_expression (c_parser *parser)
7281 struct c_expr expr;
7282 struct c_expr result;
7283 location_t expr_loc;
7284 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7286 location_t start;
7287 location_t finish = UNKNOWN_LOCATION;
7289 start = c_parser_peek_token (parser)->location;
7291 c_parser_consume_token (parser);
7292 c_inhibit_evaluation_warnings++;
7293 in_sizeof++;
7294 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7295 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7297 /* Either sizeof ( type-name ) or sizeof unary-expression
7298 starting with a compound literal. */
7299 struct c_type_name *type_name;
7300 matching_parens parens;
7301 parens.consume_open (parser);
7302 expr_loc = c_parser_peek_token (parser)->location;
7303 type_name = c_parser_type_name (parser, true);
7304 parens.skip_until_found_close (parser);
7305 finish = parser->tokens_buf[0].location;
7306 if (type_name == NULL)
7308 struct c_expr ret;
7309 c_inhibit_evaluation_warnings--;
7310 in_sizeof--;
7311 ret.set_error ();
7312 ret.original_code = ERROR_MARK;
7313 ret.original_type = NULL;
7314 return ret;
7316 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7318 expr = c_parser_postfix_expression_after_paren_type (parser,
7319 type_name,
7320 expr_loc);
7321 finish = expr.get_finish ();
7322 goto sizeof_expr;
7324 /* sizeof ( type-name ). */
7325 if (type_name->specs->alignas_p)
7326 error_at (type_name->specs->locations[cdw_alignas],
7327 "alignment specified for type name in %<sizeof%>");
7328 c_inhibit_evaluation_warnings--;
7329 in_sizeof--;
7330 result = c_expr_sizeof_type (expr_loc, type_name);
7332 else
7334 expr_loc = c_parser_peek_token (parser)->location;
7335 expr = c_parser_unary_expression (parser);
7336 finish = expr.get_finish ();
7337 sizeof_expr:
7338 c_inhibit_evaluation_warnings--;
7339 in_sizeof--;
7340 mark_exp_read (expr.value);
7341 if (TREE_CODE (expr.value) == COMPONENT_REF
7342 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7343 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7344 result = c_expr_sizeof_expr (expr_loc, expr);
7346 if (finish != UNKNOWN_LOCATION)
7347 set_c_expr_source_range (&result, start, finish);
7348 return result;
7351 /* Parse an alignof expression. */
7353 static struct c_expr
7354 c_parser_alignof_expression (c_parser *parser)
7356 struct c_expr expr;
7357 location_t start_loc = c_parser_peek_token (parser)->location;
7358 location_t end_loc;
7359 tree alignof_spelling = c_parser_peek_token (parser)->value;
7360 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7361 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7362 "_Alignof") == 0;
7363 /* A diagnostic is not required for the use of this identifier in
7364 the implementation namespace; only diagnose it for the C11
7365 spelling because of existing code using the other spellings. */
7366 if (is_c11_alignof)
7368 if (flag_isoc99)
7369 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7370 alignof_spelling);
7371 else
7372 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7373 alignof_spelling);
7375 c_parser_consume_token (parser);
7376 c_inhibit_evaluation_warnings++;
7377 in_alignof++;
7378 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7379 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7381 /* Either __alignof__ ( type-name ) or __alignof__
7382 unary-expression starting with a compound literal. */
7383 location_t loc;
7384 struct c_type_name *type_name;
7385 struct c_expr ret;
7386 matching_parens parens;
7387 parens.consume_open (parser);
7388 loc = c_parser_peek_token (parser)->location;
7389 type_name = c_parser_type_name (parser, true);
7390 end_loc = c_parser_peek_token (parser)->location;
7391 parens.skip_until_found_close (parser);
7392 if (type_name == NULL)
7394 struct c_expr ret;
7395 c_inhibit_evaluation_warnings--;
7396 in_alignof--;
7397 ret.set_error ();
7398 ret.original_code = ERROR_MARK;
7399 ret.original_type = NULL;
7400 return ret;
7402 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7404 expr = c_parser_postfix_expression_after_paren_type (parser,
7405 type_name,
7406 loc);
7407 goto alignof_expr;
7409 /* alignof ( type-name ). */
7410 if (type_name->specs->alignas_p)
7411 error_at (type_name->specs->locations[cdw_alignas],
7412 "alignment specified for type name in %qE",
7413 alignof_spelling);
7414 c_inhibit_evaluation_warnings--;
7415 in_alignof--;
7416 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7417 NULL, NULL),
7418 false, is_c11_alignof, 1);
7419 ret.original_code = ERROR_MARK;
7420 ret.original_type = NULL;
7421 set_c_expr_source_range (&ret, start_loc, end_loc);
7422 return ret;
7424 else
7426 struct c_expr ret;
7427 expr = c_parser_unary_expression (parser);
7428 end_loc = expr.src_range.m_finish;
7429 alignof_expr:
7430 mark_exp_read (expr.value);
7431 c_inhibit_evaluation_warnings--;
7432 in_alignof--;
7433 if (is_c11_alignof)
7434 pedwarn (start_loc,
7435 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7436 alignof_spelling);
7437 ret.value = c_alignof_expr (start_loc, expr.value);
7438 ret.original_code = ERROR_MARK;
7439 ret.original_type = NULL;
7440 set_c_expr_source_range (&ret, start_loc, end_loc);
7441 return ret;
7445 /* Helper function to read arguments of builtins which are interfaces
7446 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7447 others. The name of the builtin is passed using BNAME parameter.
7448 Function returns true if there were no errors while parsing and
7449 stores the arguments in CEXPR_LIST. If it returns true,
7450 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7451 parenthesis. */
7452 static bool
7453 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7454 vec<c_expr_t, va_gc> **ret_cexpr_list,
7455 bool choose_expr_p,
7456 location_t *out_close_paren_loc)
7458 location_t loc = c_parser_peek_token (parser)->location;
7459 vec<c_expr_t, va_gc> *cexpr_list;
7460 c_expr_t expr;
7461 bool saved_force_folding_builtin_constant_p;
7463 *ret_cexpr_list = NULL;
7464 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7466 error_at (loc, "cannot take address of %qs", bname);
7467 return false;
7470 c_parser_consume_token (parser);
7472 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7474 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7475 c_parser_consume_token (parser);
7476 return true;
7479 saved_force_folding_builtin_constant_p
7480 = force_folding_builtin_constant_p;
7481 force_folding_builtin_constant_p |= choose_expr_p;
7482 expr = c_parser_expr_no_commas (parser, NULL);
7483 force_folding_builtin_constant_p
7484 = saved_force_folding_builtin_constant_p;
7485 vec_alloc (cexpr_list, 1);
7486 vec_safe_push (cexpr_list, expr);
7487 while (c_parser_next_token_is (parser, CPP_COMMA))
7489 c_parser_consume_token (parser);
7490 expr = c_parser_expr_no_commas (parser, NULL);
7491 vec_safe_push (cexpr_list, expr);
7494 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7495 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7496 return false;
7498 *ret_cexpr_list = cexpr_list;
7499 return true;
7502 /* This represents a single generic-association. */
7504 struct c_generic_association
7506 /* The location of the starting token of the type. */
7507 location_t type_location;
7508 /* The association's type, or NULL_TREE for 'default'. */
7509 tree type;
7510 /* The association's expression. */
7511 struct c_expr expression;
7514 /* Parse a generic-selection. (C11 6.5.1.1).
7516 generic-selection:
7517 _Generic ( assignment-expression , generic-assoc-list )
7519 generic-assoc-list:
7520 generic-association
7521 generic-assoc-list , generic-association
7523 generic-association:
7524 type-name : assignment-expression
7525 default : assignment-expression
7528 static struct c_expr
7529 c_parser_generic_selection (c_parser *parser)
7531 struct c_expr selector, error_expr;
7532 tree selector_type;
7533 struct c_generic_association matched_assoc;
7534 bool match_found = false;
7535 location_t generic_loc, selector_loc;
7537 error_expr.original_code = ERROR_MARK;
7538 error_expr.original_type = NULL;
7539 error_expr.set_error ();
7540 matched_assoc.type_location = UNKNOWN_LOCATION;
7541 matched_assoc.type = NULL_TREE;
7542 matched_assoc.expression = error_expr;
7544 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7545 generic_loc = c_parser_peek_token (parser)->location;
7546 c_parser_consume_token (parser);
7547 if (flag_isoc99)
7548 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7549 "ISO C99 does not support %<_Generic%>");
7550 else
7551 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7552 "ISO C90 does not support %<_Generic%>");
7554 matching_parens parens;
7555 if (!parens.require_open (parser))
7556 return error_expr;
7558 c_inhibit_evaluation_warnings++;
7559 selector_loc = c_parser_peek_token (parser)->location;
7560 selector = c_parser_expr_no_commas (parser, NULL);
7561 selector = default_function_array_conversion (selector_loc, selector);
7562 c_inhibit_evaluation_warnings--;
7564 if (selector.value == error_mark_node)
7566 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7567 return selector;
7569 selector_type = TREE_TYPE (selector.value);
7570 /* In ISO C terms, rvalues (including the controlling expression of
7571 _Generic) do not have qualified types. */
7572 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7573 selector_type = TYPE_MAIN_VARIANT (selector_type);
7574 /* In ISO C terms, _Noreturn is not part of the type of expressions
7575 such as &abort, but in GCC it is represented internally as a type
7576 qualifier. */
7577 if (FUNCTION_POINTER_TYPE_P (selector_type)
7578 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7579 selector_type
7580 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7582 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7584 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7585 return error_expr;
7588 auto_vec<c_generic_association> associations;
7589 while (1)
7591 struct c_generic_association assoc, *iter;
7592 unsigned int ix;
7593 c_token *token = c_parser_peek_token (parser);
7595 assoc.type_location = token->location;
7596 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7598 c_parser_consume_token (parser);
7599 assoc.type = NULL_TREE;
7601 else
7603 struct c_type_name *type_name;
7605 type_name = c_parser_type_name (parser);
7606 if (type_name == NULL)
7608 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7609 return error_expr;
7611 assoc.type = groktypename (type_name, NULL, NULL);
7612 if (assoc.type == error_mark_node)
7614 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7615 return error_expr;
7618 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7619 error_at (assoc.type_location,
7620 "%<_Generic%> association has function type");
7621 else if (!COMPLETE_TYPE_P (assoc.type))
7622 error_at (assoc.type_location,
7623 "%<_Generic%> association has incomplete type");
7625 if (variably_modified_type_p (assoc.type, NULL_TREE))
7626 error_at (assoc.type_location,
7627 "%<_Generic%> association has "
7628 "variable length type");
7631 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7633 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7634 return error_expr;
7637 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7638 if (assoc.expression.value == error_mark_node)
7640 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7641 return error_expr;
7644 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7646 if (assoc.type == NULL_TREE)
7648 if (iter->type == NULL_TREE)
7650 error_at (assoc.type_location,
7651 "duplicate %<default%> case in %<_Generic%>");
7652 inform (iter->type_location, "original %<default%> is here");
7655 else if (iter->type != NULL_TREE)
7657 if (comptypes (assoc.type, iter->type))
7659 error_at (assoc.type_location,
7660 "%<_Generic%> specifies two compatible types");
7661 inform (iter->type_location, "compatible type is here");
7666 if (assoc.type == NULL_TREE)
7668 if (!match_found)
7670 matched_assoc = assoc;
7671 match_found = true;
7674 else if (comptypes (assoc.type, selector_type))
7676 if (!match_found || matched_assoc.type == NULL_TREE)
7678 matched_assoc = assoc;
7679 match_found = true;
7681 else
7683 error_at (assoc.type_location,
7684 "%<_Generic%> selector matches multiple associations");
7685 inform (matched_assoc.type_location,
7686 "other match is here");
7690 associations.safe_push (assoc);
7692 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7693 break;
7694 c_parser_consume_token (parser);
7697 if (!parens.require_close (parser))
7699 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7700 return error_expr;
7703 if (!match_found)
7705 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7706 "compatible with any association",
7707 selector_type);
7708 return error_expr;
7711 return matched_assoc.expression;
7714 /* Check the validity of a function pointer argument *EXPR (argument
7715 position POS) to __builtin_tgmath. Return the number of function
7716 arguments if possibly valid; return 0 having reported an error if
7717 not valid. */
7719 static unsigned int
7720 check_tgmath_function (c_expr *expr, unsigned int pos)
7722 tree type = TREE_TYPE (expr->value);
7723 if (!FUNCTION_POINTER_TYPE_P (type))
7725 error_at (expr->get_location (),
7726 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7727 pos);
7728 return 0;
7730 type = TREE_TYPE (type);
7731 if (!prototype_p (type))
7733 error_at (expr->get_location (),
7734 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7735 return 0;
7737 if (stdarg_p (type))
7739 error_at (expr->get_location (),
7740 "argument %u of %<__builtin_tgmath%> has variable arguments",
7741 pos);
7742 return 0;
7744 unsigned int nargs = 0;
7745 function_args_iterator iter;
7746 tree t;
7747 FOREACH_FUNCTION_ARGS (type, t, iter)
7749 if (t == void_type_node)
7750 break;
7751 nargs++;
7753 if (nargs == 0)
7755 error_at (expr->get_location (),
7756 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7757 return 0;
7759 return nargs;
7762 /* Ways in which a parameter or return value of a type-generic macro
7763 may vary between the different functions the macro may call. */
7764 enum tgmath_parm_kind
7766 tgmath_fixed, tgmath_real, tgmath_complex
7769 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7770 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7771 call c_parser_postfix_expression_after_paren_type on encountering them.
7773 postfix-expression:
7774 primary-expression
7775 postfix-expression [ expression ]
7776 postfix-expression ( argument-expression-list[opt] )
7777 postfix-expression . identifier
7778 postfix-expression -> identifier
7779 postfix-expression ++
7780 postfix-expression --
7781 ( type-name ) { initializer-list }
7782 ( type-name ) { initializer-list , }
7784 argument-expression-list:
7785 argument-expression
7786 argument-expression-list , argument-expression
7788 primary-expression:
7789 identifier
7790 constant
7791 string-literal
7792 ( expression )
7793 generic-selection
7795 GNU extensions:
7797 primary-expression:
7798 __func__
7799 (treated as a keyword in GNU C)
7800 __FUNCTION__
7801 __PRETTY_FUNCTION__
7802 ( compound-statement )
7803 __builtin_va_arg ( assignment-expression , type-name )
7804 __builtin_offsetof ( type-name , offsetof-member-designator )
7805 __builtin_choose_expr ( assignment-expression ,
7806 assignment-expression ,
7807 assignment-expression )
7808 __builtin_types_compatible_p ( type-name , type-name )
7809 __builtin_tgmath ( expr-list )
7810 __builtin_complex ( assignment-expression , assignment-expression )
7811 __builtin_shuffle ( assignment-expression , assignment-expression )
7812 __builtin_shuffle ( assignment-expression ,
7813 assignment-expression ,
7814 assignment-expression, )
7816 offsetof-member-designator:
7817 identifier
7818 offsetof-member-designator . identifier
7819 offsetof-member-designator [ expression ]
7821 Objective-C:
7823 primary-expression:
7824 [ objc-receiver objc-message-args ]
7825 @selector ( objc-selector-arg )
7826 @protocol ( identifier )
7827 @encode ( type-name )
7828 objc-string-literal
7829 Classname . identifier
7832 static struct c_expr
7833 c_parser_postfix_expression (c_parser *parser)
7835 struct c_expr expr, e1;
7836 struct c_type_name *t1, *t2;
7837 location_t loc = c_parser_peek_token (parser)->location;
7838 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7839 expr.original_code = ERROR_MARK;
7840 expr.original_type = NULL;
7841 switch (c_parser_peek_token (parser)->type)
7843 case CPP_NUMBER:
7844 expr.value = c_parser_peek_token (parser)->value;
7845 set_c_expr_source_range (&expr, tok_range);
7846 loc = c_parser_peek_token (parser)->location;
7847 c_parser_consume_token (parser);
7848 if (TREE_CODE (expr.value) == FIXED_CST
7849 && !targetm.fixed_point_supported_p ())
7851 error_at (loc, "fixed-point types not supported for this target");
7852 expr.set_error ();
7854 break;
7855 case CPP_CHAR:
7856 case CPP_CHAR16:
7857 case CPP_CHAR32:
7858 case CPP_WCHAR:
7859 expr.value = c_parser_peek_token (parser)->value;
7860 /* For the purpose of warning when a pointer is compared with
7861 a zero character constant. */
7862 expr.original_type = char_type_node;
7863 set_c_expr_source_range (&expr, tok_range);
7864 c_parser_consume_token (parser);
7865 break;
7866 case CPP_STRING:
7867 case CPP_STRING16:
7868 case CPP_STRING32:
7869 case CPP_WSTRING:
7870 case CPP_UTF8STRING:
7871 expr.value = c_parser_peek_token (parser)->value;
7872 set_c_expr_source_range (&expr, tok_range);
7873 expr.original_code = STRING_CST;
7874 c_parser_consume_token (parser);
7875 break;
7876 case CPP_OBJC_STRING:
7877 gcc_assert (c_dialect_objc ());
7878 expr.value
7879 = objc_build_string_object (c_parser_peek_token (parser)->value);
7880 set_c_expr_source_range (&expr, tok_range);
7881 c_parser_consume_token (parser);
7882 break;
7883 case CPP_NAME:
7884 switch (c_parser_peek_token (parser)->id_kind)
7886 case C_ID_ID:
7888 tree id = c_parser_peek_token (parser)->value;
7889 c_parser_consume_token (parser);
7890 expr.value = build_external_ref (loc, id,
7891 (c_parser_peek_token (parser)->type
7892 == CPP_OPEN_PAREN),
7893 &expr.original_type);
7894 set_c_expr_source_range (&expr, tok_range);
7895 break;
7897 case C_ID_CLASSNAME:
7899 /* Here we parse the Objective-C 2.0 Class.name dot
7900 syntax. */
7901 tree class_name = c_parser_peek_token (parser)->value;
7902 tree component;
7903 c_parser_consume_token (parser);
7904 gcc_assert (c_dialect_objc ());
7905 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7907 expr.set_error ();
7908 break;
7910 if (c_parser_next_token_is_not (parser, CPP_NAME))
7912 c_parser_error (parser, "expected identifier");
7913 expr.set_error ();
7914 break;
7916 c_token *component_tok = c_parser_peek_token (parser);
7917 component = component_tok->value;
7918 location_t end_loc = component_tok->get_finish ();
7919 c_parser_consume_token (parser);
7920 expr.value = objc_build_class_component_ref (class_name,
7921 component);
7922 set_c_expr_source_range (&expr, loc, end_loc);
7923 break;
7925 default:
7926 c_parser_error (parser, "expected expression");
7927 expr.set_error ();
7928 break;
7930 break;
7931 case CPP_OPEN_PAREN:
7932 /* A parenthesized expression, statement expression or compound
7933 literal. */
7934 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7936 /* A statement expression. */
7937 tree stmt;
7938 location_t brace_loc;
7939 c_parser_consume_token (parser);
7940 brace_loc = c_parser_peek_token (parser)->location;
7941 c_parser_consume_token (parser);
7942 /* If we've not yet started the current function's statement list,
7943 or we're in the parameter scope of an old-style function
7944 declaration, statement expressions are not allowed. */
7945 if (!building_stmt_list_p () || old_style_parameter_scope ())
7947 error_at (loc, "braced-group within expression allowed "
7948 "only inside a function");
7949 parser->error = true;
7950 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7952 expr.set_error ();
7953 break;
7955 stmt = c_begin_stmt_expr ();
7956 c_parser_compound_statement_nostart (parser);
7957 location_t close_loc = c_parser_peek_token (parser)->location;
7958 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7959 "expected %<)%>");
7960 pedwarn (loc, OPT_Wpedantic,
7961 "ISO C forbids braced-groups within expressions");
7962 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7963 set_c_expr_source_range (&expr, loc, close_loc);
7964 mark_exp_read (expr.value);
7966 else
7968 /* A parenthesized expression. */
7969 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7970 c_parser_consume_token (parser);
7971 expr = c_parser_expression (parser);
7972 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7973 TREE_NO_WARNING (expr.value) = 1;
7974 if (expr.original_code != C_MAYBE_CONST_EXPR
7975 && expr.original_code != SIZEOF_EXPR)
7976 expr.original_code = ERROR_MARK;
7977 /* Don't change EXPR.ORIGINAL_TYPE. */
7978 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7979 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7980 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7981 "expected %<)%>", loc_open_paren);
7983 break;
7984 case CPP_KEYWORD:
7985 switch (c_parser_peek_token (parser)->keyword)
7987 case RID_FUNCTION_NAME:
7988 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7989 "%<__FUNCTION__%> predefined identifier");
7990 expr.value = fname_decl (loc,
7991 c_parser_peek_token (parser)->keyword,
7992 c_parser_peek_token (parser)->value);
7993 set_c_expr_source_range (&expr, loc, loc);
7994 c_parser_consume_token (parser);
7995 break;
7996 case RID_PRETTY_FUNCTION_NAME:
7997 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7998 "%<__PRETTY_FUNCTION__%> predefined identifier");
7999 expr.value = fname_decl (loc,
8000 c_parser_peek_token (parser)->keyword,
8001 c_parser_peek_token (parser)->value);
8002 set_c_expr_source_range (&expr, loc, loc);
8003 c_parser_consume_token (parser);
8004 break;
8005 case RID_C99_FUNCTION_NAME:
8006 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8007 "%<__func__%> predefined identifier");
8008 expr.value = fname_decl (loc,
8009 c_parser_peek_token (parser)->keyword,
8010 c_parser_peek_token (parser)->value);
8011 set_c_expr_source_range (&expr, loc, loc);
8012 c_parser_consume_token (parser);
8013 break;
8014 case RID_VA_ARG:
8016 location_t start_loc = loc;
8017 c_parser_consume_token (parser);
8018 matching_parens parens;
8019 if (!parens.require_open (parser))
8021 expr.set_error ();
8022 break;
8024 e1 = c_parser_expr_no_commas (parser, NULL);
8025 mark_exp_read (e1.value);
8026 e1.value = c_fully_fold (e1.value, false, NULL);
8027 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8030 expr.set_error ();
8031 break;
8033 loc = c_parser_peek_token (parser)->location;
8034 t1 = c_parser_type_name (parser);
8035 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8036 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8037 "expected %<)%>");
8038 if (t1 == NULL)
8040 expr.set_error ();
8042 else
8044 tree type_expr = NULL_TREE;
8045 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8046 groktypename (t1, &type_expr, NULL));
8047 if (type_expr)
8049 expr.value = build2 (C_MAYBE_CONST_EXPR,
8050 TREE_TYPE (expr.value), type_expr,
8051 expr.value);
8052 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8054 set_c_expr_source_range (&expr, start_loc, end_loc);
8057 break;
8058 case RID_OFFSETOF:
8060 c_parser_consume_token (parser);
8061 matching_parens parens;
8062 if (!parens.require_open (parser))
8064 expr.set_error ();
8065 break;
8067 t1 = c_parser_type_name (parser);
8068 if (t1 == NULL)
8069 parser->error = true;
8070 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8071 gcc_assert (parser->error);
8072 if (parser->error)
8074 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8075 expr.set_error ();
8076 break;
8078 tree type = groktypename (t1, NULL, NULL);
8079 tree offsetof_ref;
8080 if (type == error_mark_node)
8081 offsetof_ref = error_mark_node;
8082 else
8084 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8085 SET_EXPR_LOCATION (offsetof_ref, loc);
8087 /* Parse the second argument to __builtin_offsetof. We
8088 must have one identifier, and beyond that we want to
8089 accept sub structure and sub array references. */
8090 if (c_parser_next_token_is (parser, CPP_NAME))
8092 c_token *comp_tok = c_parser_peek_token (parser);
8093 offsetof_ref = build_component_ref
8094 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8095 c_parser_consume_token (parser);
8096 while (c_parser_next_token_is (parser, CPP_DOT)
8097 || c_parser_next_token_is (parser,
8098 CPP_OPEN_SQUARE)
8099 || c_parser_next_token_is (parser,
8100 CPP_DEREF))
8102 if (c_parser_next_token_is (parser, CPP_DEREF))
8104 loc = c_parser_peek_token (parser)->location;
8105 offsetof_ref = build_array_ref (loc,
8106 offsetof_ref,
8107 integer_zero_node);
8108 goto do_dot;
8110 else if (c_parser_next_token_is (parser, CPP_DOT))
8112 do_dot:
8113 c_parser_consume_token (parser);
8114 if (c_parser_next_token_is_not (parser,
8115 CPP_NAME))
8117 c_parser_error (parser, "expected identifier");
8118 break;
8120 c_token *comp_tok = c_parser_peek_token (parser);
8121 offsetof_ref = build_component_ref
8122 (loc, offsetof_ref, comp_tok->value,
8123 comp_tok->location);
8124 c_parser_consume_token (parser);
8126 else
8128 struct c_expr ce;
8129 tree idx;
8130 loc = c_parser_peek_token (parser)->location;
8131 c_parser_consume_token (parser);
8132 ce = c_parser_expression (parser);
8133 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8134 idx = ce.value;
8135 idx = c_fully_fold (idx, false, NULL);
8136 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8137 "expected %<]%>");
8138 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8142 else
8143 c_parser_error (parser, "expected identifier");
8144 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8145 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8146 "expected %<)%>");
8147 expr.value = fold_offsetof (offsetof_ref);
8148 set_c_expr_source_range (&expr, loc, end_loc);
8150 break;
8151 case RID_CHOOSE_EXPR:
8153 vec<c_expr_t, va_gc> *cexpr_list;
8154 c_expr_t *e1_p, *e2_p, *e3_p;
8155 tree c;
8156 location_t close_paren_loc;
8158 c_parser_consume_token (parser);
8159 if (!c_parser_get_builtin_args (parser,
8160 "__builtin_choose_expr",
8161 &cexpr_list, true,
8162 &close_paren_loc))
8164 expr.set_error ();
8165 break;
8168 if (vec_safe_length (cexpr_list) != 3)
8170 error_at (loc, "wrong number of arguments to "
8171 "%<__builtin_choose_expr%>");
8172 expr.set_error ();
8173 break;
8176 e1_p = &(*cexpr_list)[0];
8177 e2_p = &(*cexpr_list)[1];
8178 e3_p = &(*cexpr_list)[2];
8180 c = e1_p->value;
8181 mark_exp_read (e2_p->value);
8182 mark_exp_read (e3_p->value);
8183 if (TREE_CODE (c) != INTEGER_CST
8184 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8185 error_at (loc,
8186 "first argument to %<__builtin_choose_expr%> not"
8187 " a constant");
8188 constant_expression_warning (c);
8189 expr = integer_zerop (c) ? *e3_p : *e2_p;
8190 set_c_expr_source_range (&expr, loc, close_paren_loc);
8191 break;
8193 case RID_TYPES_COMPATIBLE_P:
8195 c_parser_consume_token (parser);
8196 matching_parens parens;
8197 if (!parens.require_open (parser))
8199 expr.set_error ();
8200 break;
8202 t1 = c_parser_type_name (parser);
8203 if (t1 == NULL)
8205 expr.set_error ();
8206 break;
8208 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8210 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8211 expr.set_error ();
8212 break;
8214 t2 = c_parser_type_name (parser);
8215 if (t2 == NULL)
8217 expr.set_error ();
8218 break;
8220 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8221 parens.skip_until_found_close (parser);
8222 tree e1, e2;
8223 e1 = groktypename (t1, NULL, NULL);
8224 e2 = groktypename (t2, NULL, NULL);
8225 if (e1 == error_mark_node || e2 == error_mark_node)
8227 expr.set_error ();
8228 break;
8231 e1 = TYPE_MAIN_VARIANT (e1);
8232 e2 = TYPE_MAIN_VARIANT (e2);
8234 expr.value
8235 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8236 set_c_expr_source_range (&expr, loc, close_paren_loc);
8238 break;
8239 case RID_BUILTIN_TGMATH:
8241 vec<c_expr_t, va_gc> *cexpr_list;
8242 location_t close_paren_loc;
8244 c_parser_consume_token (parser);
8245 if (!c_parser_get_builtin_args (parser,
8246 "__builtin_tgmath",
8247 &cexpr_list, false,
8248 &close_paren_loc))
8250 expr.set_error ();
8251 break;
8254 if (vec_safe_length (cexpr_list) < 3)
8256 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8257 expr.set_error ();
8258 break;
8261 unsigned int i;
8262 c_expr_t *p;
8263 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8264 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8265 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8266 if (nargs == 0)
8268 expr.set_error ();
8269 break;
8271 if (vec_safe_length (cexpr_list) < nargs)
8273 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8274 expr.set_error ();
8275 break;
8277 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8278 if (num_functions < 2)
8280 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8281 expr.set_error ();
8282 break;
8285 /* The first NUM_FUNCTIONS expressions are the function
8286 pointers. The remaining NARGS expressions are the
8287 arguments that are to be passed to one of those
8288 functions, chosen following <tgmath.h> rules. */
8289 for (unsigned int j = 1; j < num_functions; j++)
8291 unsigned int this_nargs
8292 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8293 if (this_nargs == 0)
8295 expr.set_error ();
8296 goto out;
8298 if (this_nargs != nargs)
8300 error_at ((*cexpr_list)[j].get_location (),
8301 "argument %u of %<__builtin_tgmath%> has "
8302 "wrong number of arguments", j + 1);
8303 expr.set_error ();
8304 goto out;
8308 /* The functions all have the same number of arguments.
8309 Determine whether arguments and return types vary in
8310 ways permitted for <tgmath.h> functions. */
8311 /* The first entry in each of these vectors is for the
8312 return type, subsequent entries for parameter
8313 types. */
8314 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8315 auto_vec<tree> parm_first (nargs + 1);
8316 auto_vec<bool> parm_complex (nargs + 1);
8317 auto_vec<bool> parm_varies (nargs + 1);
8318 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8319 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8320 parm_first.quick_push (first_ret);
8321 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8322 parm_varies.quick_push (false);
8323 function_args_iterator iter;
8324 tree t;
8325 unsigned int argpos;
8326 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8328 if (t == void_type_node)
8329 break;
8330 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8331 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8332 parm_varies.quick_push (false);
8334 for (unsigned int j = 1; j < num_functions; j++)
8336 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8337 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8338 if (ret != parm_first[0])
8340 parm_varies[0] = true;
8341 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8342 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8344 error_at ((*cexpr_list)[0].get_location (),
8345 "invalid type-generic return type for "
8346 "argument %u of %<__builtin_tgmath%>",
8348 expr.set_error ();
8349 goto out;
8351 if (!SCALAR_FLOAT_TYPE_P (ret)
8352 && !COMPLEX_FLOAT_TYPE_P (ret))
8354 error_at ((*cexpr_list)[j].get_location (),
8355 "invalid type-generic return type for "
8356 "argument %u of %<__builtin_tgmath%>",
8357 j + 1);
8358 expr.set_error ();
8359 goto out;
8362 if (TREE_CODE (ret) == COMPLEX_TYPE)
8363 parm_complex[0] = true;
8364 argpos = 1;
8365 FOREACH_FUNCTION_ARGS (type, t, iter)
8367 if (t == void_type_node)
8368 break;
8369 t = TYPE_MAIN_VARIANT (t);
8370 if (t != parm_first[argpos])
8372 parm_varies[argpos] = true;
8373 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8374 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8376 error_at ((*cexpr_list)[0].get_location (),
8377 "invalid type-generic type for "
8378 "argument %u of argument %u of "
8379 "%<__builtin_tgmath%>", argpos, 1);
8380 expr.set_error ();
8381 goto out;
8383 if (!SCALAR_FLOAT_TYPE_P (t)
8384 && !COMPLEX_FLOAT_TYPE_P (t))
8386 error_at ((*cexpr_list)[j].get_location (),
8387 "invalid type-generic type for "
8388 "argument %u of argument %u of "
8389 "%<__builtin_tgmath%>", argpos, j + 1);
8390 expr.set_error ();
8391 goto out;
8394 if (TREE_CODE (t) == COMPLEX_TYPE)
8395 parm_complex[argpos] = true;
8396 argpos++;
8399 enum tgmath_parm_kind max_variation = tgmath_fixed;
8400 for (unsigned int j = 0; j <= nargs; j++)
8402 enum tgmath_parm_kind this_kind;
8403 if (parm_varies[j])
8405 if (parm_complex[j])
8406 max_variation = this_kind = tgmath_complex;
8407 else
8409 this_kind = tgmath_real;
8410 if (max_variation != tgmath_complex)
8411 max_variation = tgmath_real;
8414 else
8415 this_kind = tgmath_fixed;
8416 parm_kind.quick_push (this_kind);
8418 if (max_variation == tgmath_fixed)
8420 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8421 "all have the same type");
8422 expr.set_error ();
8423 break;
8426 /* Identify a parameter (not the return type) that varies,
8427 including with complex types if any variation includes
8428 complex types; there must be at least one such
8429 parameter. */
8430 unsigned int tgarg = 0;
8431 for (unsigned int j = 1; j <= nargs; j++)
8432 if (parm_kind[j] == max_variation)
8434 tgarg = j;
8435 break;
8437 if (tgarg == 0)
8439 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8440 "lack type-generic parameter");
8441 expr.set_error ();
8442 break;
8445 /* Determine the type of the relevant parameter for each
8446 function. */
8447 auto_vec<tree> tg_type (num_functions);
8448 for (unsigned int j = 0; j < num_functions; j++)
8450 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8451 argpos = 1;
8452 FOREACH_FUNCTION_ARGS (type, t, iter)
8454 if (argpos == tgarg)
8456 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8457 break;
8459 argpos++;
8463 /* Verify that the corresponding types are different for
8464 all the listed functions. Also determine whether all
8465 the types are complex, whether all the types are
8466 standard or binary, and whether all the types are
8467 decimal. */
8468 bool all_complex = true;
8469 bool all_binary = true;
8470 bool all_decimal = true;
8471 hash_set<tree> tg_types;
8472 FOR_EACH_VEC_ELT (tg_type, i, t)
8474 if (TREE_CODE (t) == COMPLEX_TYPE)
8475 all_decimal = false;
8476 else
8478 all_complex = false;
8479 if (DECIMAL_FLOAT_TYPE_P (t))
8480 all_binary = false;
8481 else
8482 all_decimal = false;
8484 if (tg_types.add (t))
8486 error_at ((*cexpr_list)[i].get_location (),
8487 "duplicate type-generic parameter type for "
8488 "function argument %u of %<__builtin_tgmath%>",
8489 i + 1);
8490 expr.set_error ();
8491 goto out;
8495 /* Verify that other parameters and the return type whose
8496 types vary have their types varying in the correct
8497 way. */
8498 for (unsigned int j = 0; j < num_functions; j++)
8500 tree exp_type = tg_type[j];
8501 tree exp_real_type = exp_type;
8502 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8503 exp_real_type = TREE_TYPE (exp_type);
8504 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8505 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8506 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8507 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8509 error_at ((*cexpr_list)[j].get_location (),
8510 "bad return type for function argument %u "
8511 "of %<__builtin_tgmath%>", j + 1);
8512 expr.set_error ();
8513 goto out;
8515 argpos = 1;
8516 FOREACH_FUNCTION_ARGS (type, t, iter)
8518 if (t == void_type_node)
8519 break;
8520 t = TYPE_MAIN_VARIANT (t);
8521 if ((parm_kind[argpos] == tgmath_complex
8522 && t != exp_type)
8523 || (parm_kind[argpos] == tgmath_real
8524 && t != exp_real_type))
8526 error_at ((*cexpr_list)[j].get_location (),
8527 "bad type for argument %u of "
8528 "function argument %u of "
8529 "%<__builtin_tgmath%>", argpos, j + 1);
8530 expr.set_error ();
8531 goto out;
8533 argpos++;
8537 /* The functions listed are a valid set of functions for a
8538 <tgmath.h> macro to select between. Identify the
8539 matching function, if any. First, the argument types
8540 must be combined following <tgmath.h> rules. Integer
8541 types are treated as _Decimal64 if any type-generic
8542 argument is decimal, or if the only alternatives for
8543 type-generic arguments are of decimal types, and are
8544 otherwise treated as double (or _Complex double for
8545 complex integer types, or _Float64 or _Complex _Float64
8546 if all the return types are the same _FloatN or
8547 _FloatNx type). After that adjustment, types are
8548 combined following the usual arithmetic conversions.
8549 If the function only accepts complex arguments, a
8550 complex type is produced. */
8551 bool arg_complex = all_complex;
8552 bool arg_binary = all_binary;
8553 bool arg_int_decimal = all_decimal;
8554 for (unsigned int j = 1; j <= nargs; j++)
8556 if (parm_kind[j] == tgmath_fixed)
8557 continue;
8558 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8559 tree type = TREE_TYPE (ce->value);
8560 if (!INTEGRAL_TYPE_P (type)
8561 && !SCALAR_FLOAT_TYPE_P (type)
8562 && TREE_CODE (type) != COMPLEX_TYPE)
8564 error_at (ce->get_location (),
8565 "invalid type of argument %u of type-generic "
8566 "function", j);
8567 expr.set_error ();
8568 goto out;
8570 if (DECIMAL_FLOAT_TYPE_P (type))
8572 arg_int_decimal = true;
8573 if (all_complex)
8575 error_at (ce->get_location (),
8576 "decimal floating-point argument %u to "
8577 "complex-only type-generic function", j);
8578 expr.set_error ();
8579 goto out;
8581 else if (all_binary)
8583 error_at (ce->get_location (),
8584 "decimal floating-point argument %u to "
8585 "binary-only type-generic function", j);
8586 expr.set_error ();
8587 goto out;
8589 else if (arg_complex)
8591 error_at (ce->get_location (),
8592 "both complex and decimal floating-point "
8593 "arguments to type-generic function");
8594 expr.set_error ();
8595 goto out;
8597 else if (arg_binary)
8599 error_at (ce->get_location (),
8600 "both binary and decimal floating-point "
8601 "arguments to type-generic function");
8602 expr.set_error ();
8603 goto out;
8606 else if (TREE_CODE (type) == COMPLEX_TYPE)
8608 arg_complex = true;
8609 if (COMPLEX_FLOAT_TYPE_P (type))
8610 arg_binary = true;
8611 if (all_decimal)
8613 error_at (ce->get_location (),
8614 "complex argument %u to "
8615 "decimal-only type-generic function", j);
8616 expr.set_error ();
8617 goto out;
8619 else if (arg_int_decimal)
8621 error_at (ce->get_location (),
8622 "both complex and decimal floating-point "
8623 "arguments to type-generic function");
8624 expr.set_error ();
8625 goto out;
8628 else if (SCALAR_FLOAT_TYPE_P (type))
8630 arg_binary = true;
8631 if (all_decimal)
8633 error_at (ce->get_location (),
8634 "binary argument %u to "
8635 "decimal-only type-generic function", j);
8636 expr.set_error ();
8637 goto out;
8639 else if (arg_int_decimal)
8641 error_at (ce->get_location (),
8642 "both binary and decimal floating-point "
8643 "arguments to type-generic function");
8644 expr.set_error ();
8645 goto out;
8649 /* For a macro rounding its result to a narrower type, map
8650 integer types to _Float64 not double if the return type
8651 is a _FloatN or _FloatNx type. */
8652 bool arg_int_float64 = false;
8653 if (parm_kind[0] == tgmath_fixed
8654 && SCALAR_FLOAT_TYPE_P (parm_first[0])
8655 && float64_type_node != NULL_TREE)
8656 for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
8657 if (parm_first[0] == FLOATN_TYPE_NODE (j))
8659 arg_int_float64 = true;
8660 break;
8662 tree arg_real = NULL_TREE;
8663 for (unsigned int j = 1; j <= nargs; j++)
8665 if (parm_kind[j] == tgmath_fixed)
8666 continue;
8667 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8668 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8669 if (TREE_CODE (type) == COMPLEX_TYPE)
8670 type = TREE_TYPE (type);
8671 if (INTEGRAL_TYPE_P (type))
8672 type = (arg_int_decimal
8673 ? dfloat64_type_node
8674 : arg_int_float64
8675 ? float64_type_node
8676 : double_type_node);
8677 if (arg_real == NULL_TREE)
8678 arg_real = type;
8679 else
8680 arg_real = common_type (arg_real, type);
8681 if (arg_real == error_mark_node)
8683 expr.set_error ();
8684 goto out;
8687 tree arg_type = (arg_complex
8688 ? build_complex_type (arg_real)
8689 : arg_real);
8691 /* Look for a function to call with type-generic parameter
8692 type ARG_TYPE. */
8693 c_expr_t *fn = NULL;
8694 for (unsigned int j = 0; j < num_functions; j++)
8696 if (tg_type[j] == arg_type)
8698 fn = &(*cexpr_list)[j];
8699 break;
8702 if (fn == NULL
8703 && parm_kind[0] == tgmath_fixed
8704 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8706 /* Presume this is a macro that rounds its result to a
8707 narrower type, and look for the first function with
8708 at least the range and precision of the argument
8709 type. */
8710 for (unsigned int j = 0; j < num_functions; j++)
8712 if (arg_complex
8713 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8714 continue;
8715 tree real_tg_type = (arg_complex
8716 ? TREE_TYPE (tg_type[j])
8717 : tg_type[j]);
8718 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8719 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8720 continue;
8721 scalar_float_mode arg_mode
8722 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8723 scalar_float_mode tg_mode
8724 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8725 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8726 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8727 if (arg_fmt->b == tg_fmt->b
8728 && arg_fmt->p <= tg_fmt->p
8729 && arg_fmt->emax <= tg_fmt->emax
8730 && (arg_fmt->emin - arg_fmt->p
8731 >= tg_fmt->emin - tg_fmt->p))
8733 fn = &(*cexpr_list)[j];
8734 break;
8738 if (fn == NULL)
8740 error_at (loc, "no matching function for type-generic call");
8741 expr.set_error ();
8742 break;
8745 /* Construct a call to FN. */
8746 vec<tree, va_gc> *args;
8747 vec_alloc (args, nargs);
8748 vec<tree, va_gc> *origtypes;
8749 vec_alloc (origtypes, nargs);
8750 auto_vec<location_t> arg_loc (nargs);
8751 for (unsigned int j = 0; j < nargs; j++)
8753 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8754 args->quick_push (ce->value);
8755 arg_loc.quick_push (ce->get_location ());
8756 origtypes->quick_push (ce->original_type);
8758 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8759 args, origtypes);
8760 set_c_expr_source_range (&expr, loc, close_paren_loc);
8761 break;
8763 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8765 vec<c_expr_t, va_gc> *cexpr_list;
8766 c_expr_t *e2_p;
8767 tree chain_value;
8768 location_t close_paren_loc;
8770 c_parser_consume_token (parser);
8771 if (!c_parser_get_builtin_args (parser,
8772 "__builtin_call_with_static_chain",
8773 &cexpr_list, false,
8774 &close_paren_loc))
8776 expr.set_error ();
8777 break;
8779 if (vec_safe_length (cexpr_list) != 2)
8781 error_at (loc, "wrong number of arguments to "
8782 "%<__builtin_call_with_static_chain%>");
8783 expr.set_error ();
8784 break;
8787 expr = (*cexpr_list)[0];
8788 e2_p = &(*cexpr_list)[1];
8789 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8790 chain_value = e2_p->value;
8791 mark_exp_read (chain_value);
8793 if (TREE_CODE (expr.value) != CALL_EXPR)
8794 error_at (loc, "first argument to "
8795 "%<__builtin_call_with_static_chain%> "
8796 "must be a call expression");
8797 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8798 error_at (loc, "second argument to "
8799 "%<__builtin_call_with_static_chain%> "
8800 "must be a pointer type");
8801 else
8802 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8803 set_c_expr_source_range (&expr, loc, close_paren_loc);
8804 break;
8806 case RID_BUILTIN_COMPLEX:
8808 vec<c_expr_t, va_gc> *cexpr_list;
8809 c_expr_t *e1_p, *e2_p;
8810 location_t close_paren_loc;
8812 c_parser_consume_token (parser);
8813 if (!c_parser_get_builtin_args (parser,
8814 "__builtin_complex",
8815 &cexpr_list, false,
8816 &close_paren_loc))
8818 expr.set_error ();
8819 break;
8822 if (vec_safe_length (cexpr_list) != 2)
8824 error_at (loc, "wrong number of arguments to "
8825 "%<__builtin_complex%>");
8826 expr.set_error ();
8827 break;
8830 e1_p = &(*cexpr_list)[0];
8831 e2_p = &(*cexpr_list)[1];
8833 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8834 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8835 e1_p->value = convert (TREE_TYPE (e1_p->value),
8836 TREE_OPERAND (e1_p->value, 0));
8837 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8838 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8839 e2_p->value = convert (TREE_TYPE (e2_p->value),
8840 TREE_OPERAND (e2_p->value, 0));
8841 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8842 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8843 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8844 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8846 error_at (loc, "%<__builtin_complex%> operand "
8847 "not of real binary floating-point type");
8848 expr.set_error ();
8849 break;
8851 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8852 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8854 error_at (loc,
8855 "%<__builtin_complex%> operands of different types");
8856 expr.set_error ();
8857 break;
8859 pedwarn_c90 (loc, OPT_Wpedantic,
8860 "ISO C90 does not support complex types");
8861 expr.value = build2_loc (loc, COMPLEX_EXPR,
8862 build_complex_type
8863 (TYPE_MAIN_VARIANT
8864 (TREE_TYPE (e1_p->value))),
8865 e1_p->value, e2_p->value);
8866 set_c_expr_source_range (&expr, loc, close_paren_loc);
8867 break;
8869 case RID_BUILTIN_SHUFFLE:
8871 vec<c_expr_t, va_gc> *cexpr_list;
8872 unsigned int i;
8873 c_expr_t *p;
8874 location_t close_paren_loc;
8876 c_parser_consume_token (parser);
8877 if (!c_parser_get_builtin_args (parser,
8878 "__builtin_shuffle",
8879 &cexpr_list, false,
8880 &close_paren_loc))
8882 expr.set_error ();
8883 break;
8886 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8887 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8889 if (vec_safe_length (cexpr_list) == 2)
8890 expr.value =
8891 c_build_vec_perm_expr
8892 (loc, (*cexpr_list)[0].value,
8893 NULL_TREE, (*cexpr_list)[1].value);
8895 else if (vec_safe_length (cexpr_list) == 3)
8896 expr.value =
8897 c_build_vec_perm_expr
8898 (loc, (*cexpr_list)[0].value,
8899 (*cexpr_list)[1].value,
8900 (*cexpr_list)[2].value);
8901 else
8903 error_at (loc, "wrong number of arguments to "
8904 "%<__builtin_shuffle%>");
8905 expr.set_error ();
8907 set_c_expr_source_range (&expr, loc, close_paren_loc);
8908 break;
8910 case RID_AT_SELECTOR:
8912 gcc_assert (c_dialect_objc ());
8913 c_parser_consume_token (parser);
8914 matching_parens parens;
8915 if (!parens.require_open (parser))
8917 expr.set_error ();
8918 break;
8920 tree sel = c_parser_objc_selector_arg (parser);
8921 location_t close_loc = c_parser_peek_token (parser)->location;
8922 parens.skip_until_found_close (parser);
8923 expr.value = objc_build_selector_expr (loc, sel);
8924 set_c_expr_source_range (&expr, loc, close_loc);
8926 break;
8927 case RID_AT_PROTOCOL:
8929 gcc_assert (c_dialect_objc ());
8930 c_parser_consume_token (parser);
8931 matching_parens parens;
8932 if (!parens.require_open (parser))
8934 expr.set_error ();
8935 break;
8937 if (c_parser_next_token_is_not (parser, CPP_NAME))
8939 c_parser_error (parser, "expected identifier");
8940 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8941 expr.set_error ();
8942 break;
8944 tree id = c_parser_peek_token (parser)->value;
8945 c_parser_consume_token (parser);
8946 location_t close_loc = c_parser_peek_token (parser)->location;
8947 parens.skip_until_found_close (parser);
8948 expr.value = objc_build_protocol_expr (id);
8949 set_c_expr_source_range (&expr, loc, close_loc);
8951 break;
8952 case RID_AT_ENCODE:
8954 /* Extension to support C-structures in the archiver. */
8955 gcc_assert (c_dialect_objc ());
8956 c_parser_consume_token (parser);
8957 matching_parens parens;
8958 if (!parens.require_open (parser))
8960 expr.set_error ();
8961 break;
8963 t1 = c_parser_type_name (parser);
8964 if (t1 == NULL)
8966 expr.set_error ();
8967 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8968 break;
8970 location_t close_loc = c_parser_peek_token (parser)->location;
8971 parens.skip_until_found_close (parser);
8972 tree type = groktypename (t1, NULL, NULL);
8973 expr.value = objc_build_encode_expr (type);
8974 set_c_expr_source_range (&expr, loc, close_loc);
8976 break;
8977 case RID_GENERIC:
8978 expr = c_parser_generic_selection (parser);
8979 break;
8980 default:
8981 c_parser_error (parser, "expected expression");
8982 expr.set_error ();
8983 break;
8985 break;
8986 case CPP_OPEN_SQUARE:
8987 if (c_dialect_objc ())
8989 tree receiver, args;
8990 c_parser_consume_token (parser);
8991 receiver = c_parser_objc_receiver (parser);
8992 args = c_parser_objc_message_args (parser);
8993 location_t close_loc = c_parser_peek_token (parser)->location;
8994 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8995 "expected %<]%>");
8996 expr.value = objc_build_message_expr (receiver, args);
8997 set_c_expr_source_range (&expr, loc, close_loc);
8998 break;
9000 /* Else fall through to report error. */
9001 /* FALLTHRU */
9002 default:
9003 c_parser_error (parser, "expected expression");
9004 expr.set_error ();
9005 break;
9007 out:
9008 return c_parser_postfix_expression_after_primary
9009 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9012 /* Parse a postfix expression after a parenthesized type name: the
9013 brace-enclosed initializer of a compound literal, possibly followed
9014 by some postfix operators. This is separate because it is not
9015 possible to tell until after the type name whether a cast
9016 expression has a cast or a compound literal, or whether the operand
9017 of sizeof is a parenthesized type name or starts with a compound
9018 literal. TYPE_LOC is the location where TYPE_NAME starts--the
9019 location of the first token after the parentheses around the type
9020 name. */
9022 static struct c_expr
9023 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9024 struct c_type_name *type_name,
9025 location_t type_loc)
9027 tree type;
9028 struct c_expr init;
9029 bool non_const;
9030 struct c_expr expr;
9031 location_t start_loc;
9032 tree type_expr = NULL_TREE;
9033 bool type_expr_const = true;
9034 check_compound_literal_type (type_loc, type_name);
9035 rich_location richloc (line_table, type_loc);
9036 start_init (NULL_TREE, NULL, 0, &richloc);
9037 type = groktypename (type_name, &type_expr, &type_expr_const);
9038 start_loc = c_parser_peek_token (parser)->location;
9039 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9041 error_at (type_loc, "compound literal has variable size");
9042 type = error_mark_node;
9044 init = c_parser_braced_init (parser, type, false, NULL);
9045 finish_init ();
9046 maybe_warn_string_init (type_loc, type, init);
9048 if (type != error_mark_node
9049 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9050 && current_function_decl)
9052 error ("compound literal qualified by address-space qualifier");
9053 type = error_mark_node;
9056 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9057 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9058 ? CONSTRUCTOR_NON_CONST (init.value)
9059 : init.original_code == C_MAYBE_CONST_EXPR);
9060 non_const |= !type_expr_const;
9061 unsigned int alignas_align = 0;
9062 if (type != error_mark_node
9063 && type_name->specs->align_log != -1)
9065 alignas_align = 1U << type_name->specs->align_log;
9066 if (alignas_align < min_align_of_type (type))
9068 error_at (type_name->specs->locations[cdw_alignas],
9069 "%<_Alignas%> specifiers cannot reduce "
9070 "alignment of compound literal");
9071 alignas_align = 0;
9074 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9075 alignas_align);
9076 set_c_expr_source_range (&expr, init.src_range);
9077 expr.original_code = ERROR_MARK;
9078 expr.original_type = NULL;
9079 if (type != error_mark_node
9080 && expr.value != error_mark_node
9081 && type_expr)
9083 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9085 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9086 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9088 else
9090 gcc_assert (!non_const);
9091 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9092 type_expr, expr.value);
9095 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9098 /* Callback function for sizeof_pointer_memaccess_warning to compare
9099 types. */
9101 static bool
9102 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9104 return comptypes (type1, type2) == 1;
9107 /* Parse a postfix expression after the initial primary or compound
9108 literal; that is, parse a series of postfix operators.
9110 EXPR_LOC is the location of the primary expression. */
9112 static struct c_expr
9113 c_parser_postfix_expression_after_primary (c_parser *parser,
9114 location_t expr_loc,
9115 struct c_expr expr)
9117 struct c_expr orig_expr;
9118 tree ident, idx;
9119 location_t sizeof_arg_loc[3], comp_loc;
9120 tree sizeof_arg[3];
9121 unsigned int literal_zero_mask;
9122 unsigned int i;
9123 vec<tree, va_gc> *exprlist;
9124 vec<tree, va_gc> *origtypes = NULL;
9125 vec<location_t> arg_loc = vNULL;
9126 location_t start;
9127 location_t finish;
9129 while (true)
9131 location_t op_loc = c_parser_peek_token (parser)->location;
9132 switch (c_parser_peek_token (parser)->type)
9134 case CPP_OPEN_SQUARE:
9135 /* Array reference. */
9136 c_parser_consume_token (parser);
9137 idx = c_parser_expression (parser).value;
9138 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9139 "expected %<]%>");
9140 start = expr.get_start ();
9141 finish = parser->tokens_buf[0].location;
9142 expr.value = build_array_ref (op_loc, expr.value, idx);
9143 set_c_expr_source_range (&expr, start, finish);
9144 expr.original_code = ERROR_MARK;
9145 expr.original_type = NULL;
9146 break;
9147 case CPP_OPEN_PAREN:
9148 /* Function call. */
9149 c_parser_consume_token (parser);
9150 for (i = 0; i < 3; i++)
9152 sizeof_arg[i] = NULL_TREE;
9153 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9155 literal_zero_mask = 0;
9156 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9157 exprlist = NULL;
9158 else
9159 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9160 sizeof_arg_loc, sizeof_arg,
9161 &arg_loc, &literal_zero_mask);
9162 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9163 "expected %<)%>");
9164 orig_expr = expr;
9165 mark_exp_read (expr.value);
9166 if (warn_sizeof_pointer_memaccess)
9167 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9168 expr.value, exprlist,
9169 sizeof_arg,
9170 sizeof_ptr_memacc_comptypes);
9171 if (TREE_CODE (expr.value) == FUNCTION_DECL
9172 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9173 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9174 && vec_safe_length (exprlist) == 3)
9176 tree arg0 = (*exprlist)[0];
9177 tree arg2 = (*exprlist)[2];
9178 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9181 start = expr.get_start ();
9182 finish = parser->tokens_buf[0].get_finish ();
9183 expr.value
9184 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9185 exprlist, origtypes);
9186 set_c_expr_source_range (&expr, start, finish);
9188 expr.original_code = ERROR_MARK;
9189 if (TREE_CODE (expr.value) == INTEGER_CST
9190 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9191 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9192 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9193 expr.original_code = C_MAYBE_CONST_EXPR;
9194 expr.original_type = NULL;
9195 if (exprlist)
9197 release_tree_vector (exprlist);
9198 release_tree_vector (origtypes);
9200 arg_loc.release ();
9201 break;
9202 case CPP_DOT:
9203 /* Structure element reference. */
9204 c_parser_consume_token (parser);
9205 expr = default_function_array_conversion (expr_loc, expr);
9206 if (c_parser_next_token_is (parser, CPP_NAME))
9208 c_token *comp_tok = c_parser_peek_token (parser);
9209 ident = comp_tok->value;
9210 comp_loc = comp_tok->location;
9212 else
9214 c_parser_error (parser, "expected identifier");
9215 expr.set_error ();
9216 expr.original_code = ERROR_MARK;
9217 expr.original_type = NULL;
9218 return expr;
9220 start = expr.get_start ();
9221 finish = c_parser_peek_token (parser)->get_finish ();
9222 c_parser_consume_token (parser);
9223 expr.value = build_component_ref (op_loc, expr.value, ident,
9224 comp_loc);
9225 set_c_expr_source_range (&expr, start, finish);
9226 expr.original_code = ERROR_MARK;
9227 if (TREE_CODE (expr.value) != COMPONENT_REF)
9228 expr.original_type = NULL;
9229 else
9231 /* Remember the original type of a bitfield. */
9232 tree field = TREE_OPERAND (expr.value, 1);
9233 if (TREE_CODE (field) != FIELD_DECL)
9234 expr.original_type = NULL;
9235 else
9236 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9238 break;
9239 case CPP_DEREF:
9240 /* Structure element reference. */
9241 c_parser_consume_token (parser);
9242 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9243 if (c_parser_next_token_is (parser, CPP_NAME))
9245 c_token *comp_tok = c_parser_peek_token (parser);
9246 ident = comp_tok->value;
9247 comp_loc = comp_tok->location;
9249 else
9251 c_parser_error (parser, "expected identifier");
9252 expr.set_error ();
9253 expr.original_code = ERROR_MARK;
9254 expr.original_type = NULL;
9255 return expr;
9257 start = expr.get_start ();
9258 finish = c_parser_peek_token (parser)->get_finish ();
9259 c_parser_consume_token (parser);
9260 expr.value = build_component_ref (op_loc,
9261 build_indirect_ref (op_loc,
9262 expr.value,
9263 RO_ARROW),
9264 ident, comp_loc);
9265 set_c_expr_source_range (&expr, start, finish);
9266 expr.original_code = ERROR_MARK;
9267 if (TREE_CODE (expr.value) != COMPONENT_REF)
9268 expr.original_type = NULL;
9269 else
9271 /* Remember the original type of a bitfield. */
9272 tree field = TREE_OPERAND (expr.value, 1);
9273 if (TREE_CODE (field) != FIELD_DECL)
9274 expr.original_type = NULL;
9275 else
9276 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9278 break;
9279 case CPP_PLUS_PLUS:
9280 /* Postincrement. */
9281 start = expr.get_start ();
9282 finish = c_parser_peek_token (parser)->get_finish ();
9283 c_parser_consume_token (parser);
9284 expr = default_function_array_read_conversion (expr_loc, expr);
9285 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9286 expr.value, false);
9287 set_c_expr_source_range (&expr, start, finish);
9288 expr.original_code = ERROR_MARK;
9289 expr.original_type = NULL;
9290 break;
9291 case CPP_MINUS_MINUS:
9292 /* Postdecrement. */
9293 start = expr.get_start ();
9294 finish = c_parser_peek_token (parser)->get_finish ();
9295 c_parser_consume_token (parser);
9296 expr = default_function_array_read_conversion (expr_loc, expr);
9297 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9298 expr.value, false);
9299 set_c_expr_source_range (&expr, start, finish);
9300 expr.original_code = ERROR_MARK;
9301 expr.original_type = NULL;
9302 break;
9303 default:
9304 return expr;
9309 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9311 expression:
9312 assignment-expression
9313 expression , assignment-expression
9316 static struct c_expr
9317 c_parser_expression (c_parser *parser)
9319 location_t tloc = c_parser_peek_token (parser)->location;
9320 struct c_expr expr;
9321 expr = c_parser_expr_no_commas (parser, NULL);
9322 if (c_parser_next_token_is (parser, CPP_COMMA))
9323 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9324 while (c_parser_next_token_is (parser, CPP_COMMA))
9326 struct c_expr next;
9327 tree lhsval;
9328 location_t loc = c_parser_peek_token (parser)->location;
9329 location_t expr_loc;
9330 c_parser_consume_token (parser);
9331 expr_loc = c_parser_peek_token (parser)->location;
9332 lhsval = expr.value;
9333 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9334 lhsval = TREE_OPERAND (lhsval, 1);
9335 if (DECL_P (lhsval) || handled_component_p (lhsval))
9336 mark_exp_read (lhsval);
9337 next = c_parser_expr_no_commas (parser, NULL);
9338 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9339 expr.value = build_compound_expr (loc, expr.value, next.value);
9340 expr.original_code = COMPOUND_EXPR;
9341 expr.original_type = next.original_type;
9343 return expr;
9346 /* Parse an expression and convert functions or arrays to pointers and
9347 lvalues to rvalues. */
9349 static struct c_expr
9350 c_parser_expression_conv (c_parser *parser)
9352 struct c_expr expr;
9353 location_t loc = c_parser_peek_token (parser)->location;
9354 expr = c_parser_expression (parser);
9355 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9356 return expr;
9359 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9360 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9362 static inline void
9363 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9364 unsigned int idx)
9366 if (idx >= HOST_BITS_PER_INT)
9367 return;
9369 c_token *tok = c_parser_peek_token (parser);
9370 switch (tok->type)
9372 case CPP_NUMBER:
9373 case CPP_CHAR:
9374 case CPP_WCHAR:
9375 case CPP_CHAR16:
9376 case CPP_CHAR32:
9377 /* If a parameter is literal zero alone, remember it
9378 for -Wmemset-transposed-args warning. */
9379 if (integer_zerop (tok->value)
9380 && !TREE_OVERFLOW (tok->value)
9381 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9382 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9383 *literal_zero_mask |= 1U << idx;
9384 default:
9385 break;
9389 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9390 functions and arrays to pointers and lvalues to rvalues. If
9391 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9392 locations of function arguments into this vector.
9394 nonempty-expr-list:
9395 assignment-expression
9396 nonempty-expr-list , assignment-expression
9399 static vec<tree, va_gc> *
9400 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9401 vec<tree, va_gc> **p_orig_types,
9402 location_t *sizeof_arg_loc, tree *sizeof_arg,
9403 vec<location_t> *locations,
9404 unsigned int *literal_zero_mask)
9406 vec<tree, va_gc> *ret;
9407 vec<tree, va_gc> *orig_types;
9408 struct c_expr expr;
9409 unsigned int idx = 0;
9411 ret = make_tree_vector ();
9412 if (p_orig_types == NULL)
9413 orig_types = NULL;
9414 else
9415 orig_types = make_tree_vector ();
9417 if (literal_zero_mask)
9418 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9419 expr = c_parser_expr_no_commas (parser, NULL);
9420 if (convert_p)
9421 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9422 if (fold_p)
9423 expr.value = c_fully_fold (expr.value, false, NULL);
9424 ret->quick_push (expr.value);
9425 if (orig_types)
9426 orig_types->quick_push (expr.original_type);
9427 if (locations)
9428 locations->safe_push (expr.get_location ());
9429 if (sizeof_arg != NULL
9430 && expr.original_code == SIZEOF_EXPR)
9432 sizeof_arg[0] = c_last_sizeof_arg;
9433 sizeof_arg_loc[0] = c_last_sizeof_loc;
9435 while (c_parser_next_token_is (parser, CPP_COMMA))
9437 c_parser_consume_token (parser);
9438 if (literal_zero_mask)
9439 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9440 expr = c_parser_expr_no_commas (parser, NULL);
9441 if (convert_p)
9442 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9443 true);
9444 if (fold_p)
9445 expr.value = c_fully_fold (expr.value, false, NULL);
9446 vec_safe_push (ret, expr.value);
9447 if (orig_types)
9448 vec_safe_push (orig_types, expr.original_type);
9449 if (locations)
9450 locations->safe_push (expr.get_location ());
9451 if (++idx < 3
9452 && sizeof_arg != NULL
9453 && expr.original_code == SIZEOF_EXPR)
9455 sizeof_arg[idx] = c_last_sizeof_arg;
9456 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9459 if (orig_types)
9460 *p_orig_types = orig_types;
9461 return ret;
9464 /* Parse Objective-C-specific constructs. */
9466 /* Parse an objc-class-definition.
9468 objc-class-definition:
9469 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9470 objc-class-instance-variables[opt] objc-methodprotolist @end
9471 @implementation identifier objc-superclass[opt]
9472 objc-class-instance-variables[opt]
9473 @interface identifier ( identifier ) objc-protocol-refs[opt]
9474 objc-methodprotolist @end
9475 @interface identifier ( ) objc-protocol-refs[opt]
9476 objc-methodprotolist @end
9477 @implementation identifier ( identifier )
9479 objc-superclass:
9480 : identifier
9482 "@interface identifier (" must start "@interface identifier (
9483 identifier ) ...": objc-methodprotolist in the first production may
9484 not start with a parenthesized identifier as a declarator of a data
9485 definition with no declaration specifiers if the objc-superclass,
9486 objc-protocol-refs and objc-class-instance-variables are omitted. */
9488 static void
9489 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9491 bool iface_p;
9492 tree id1;
9493 tree superclass;
9494 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9495 iface_p = true;
9496 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9497 iface_p = false;
9498 else
9499 gcc_unreachable ();
9501 c_parser_consume_token (parser);
9502 if (c_parser_next_token_is_not (parser, CPP_NAME))
9504 c_parser_error (parser, "expected identifier");
9505 return;
9507 id1 = c_parser_peek_token (parser)->value;
9508 c_parser_consume_token (parser);
9509 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9511 /* We have a category or class extension. */
9512 tree id2;
9513 tree proto = NULL_TREE;
9514 matching_parens parens;
9515 parens.consume_open (parser);
9516 if (c_parser_next_token_is_not (parser, CPP_NAME))
9518 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9520 /* We have a class extension. */
9521 id2 = NULL_TREE;
9523 else
9525 c_parser_error (parser, "expected identifier or %<)%>");
9526 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9527 return;
9530 else
9532 id2 = c_parser_peek_token (parser)->value;
9533 c_parser_consume_token (parser);
9535 parens.skip_until_found_close (parser);
9536 if (!iface_p)
9538 objc_start_category_implementation (id1, id2);
9539 return;
9541 if (c_parser_next_token_is (parser, CPP_LESS))
9542 proto = c_parser_objc_protocol_refs (parser);
9543 objc_start_category_interface (id1, id2, proto, attributes);
9544 c_parser_objc_methodprotolist (parser);
9545 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9546 objc_finish_interface ();
9547 return;
9549 if (c_parser_next_token_is (parser, CPP_COLON))
9551 c_parser_consume_token (parser);
9552 if (c_parser_next_token_is_not (parser, CPP_NAME))
9554 c_parser_error (parser, "expected identifier");
9555 return;
9557 superclass = c_parser_peek_token (parser)->value;
9558 c_parser_consume_token (parser);
9560 else
9561 superclass = NULL_TREE;
9562 if (iface_p)
9564 tree proto = NULL_TREE;
9565 if (c_parser_next_token_is (parser, CPP_LESS))
9566 proto = c_parser_objc_protocol_refs (parser);
9567 objc_start_class_interface (id1, superclass, proto, attributes);
9569 else
9570 objc_start_class_implementation (id1, superclass);
9571 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9572 c_parser_objc_class_instance_variables (parser);
9573 if (iface_p)
9575 objc_continue_interface ();
9576 c_parser_objc_methodprotolist (parser);
9577 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9578 objc_finish_interface ();
9580 else
9582 objc_continue_implementation ();
9583 return;
9587 /* Parse objc-class-instance-variables.
9589 objc-class-instance-variables:
9590 { objc-instance-variable-decl-list[opt] }
9592 objc-instance-variable-decl-list:
9593 objc-visibility-spec
9594 objc-instance-variable-decl ;
9596 objc-instance-variable-decl-list objc-visibility-spec
9597 objc-instance-variable-decl-list objc-instance-variable-decl ;
9598 objc-instance-variable-decl-list ;
9600 objc-visibility-spec:
9601 @private
9602 @protected
9603 @public
9605 objc-instance-variable-decl:
9606 struct-declaration
9609 static void
9610 c_parser_objc_class_instance_variables (c_parser *parser)
9612 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9613 c_parser_consume_token (parser);
9614 while (c_parser_next_token_is_not (parser, CPP_EOF))
9616 tree decls;
9617 /* Parse any stray semicolon. */
9618 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9620 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9621 "extra semicolon");
9622 c_parser_consume_token (parser);
9623 continue;
9625 /* Stop if at the end of the instance variables. */
9626 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9628 c_parser_consume_token (parser);
9629 break;
9631 /* Parse any objc-visibility-spec. */
9632 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9634 c_parser_consume_token (parser);
9635 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9636 continue;
9638 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9640 c_parser_consume_token (parser);
9641 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9642 continue;
9644 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9646 c_parser_consume_token (parser);
9647 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9648 continue;
9650 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9652 c_parser_consume_token (parser);
9653 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9654 continue;
9656 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9658 c_parser_pragma (parser, pragma_external, NULL);
9659 continue;
9662 /* Parse some comma-separated declarations. */
9663 decls = c_parser_struct_declaration (parser);
9664 if (decls == NULL)
9666 /* There is a syntax error. We want to skip the offending
9667 tokens up to the next ';' (included) or '}'
9668 (excluded). */
9670 /* First, skip manually a ')' or ']'. This is because they
9671 reduce the nesting level, so c_parser_skip_until_found()
9672 wouldn't be able to skip past them. */
9673 c_token *token = c_parser_peek_token (parser);
9674 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9675 c_parser_consume_token (parser);
9677 /* Then, do the standard skipping. */
9678 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9680 /* We hopefully recovered. Start normal parsing again. */
9681 parser->error = false;
9682 continue;
9684 else
9686 /* Comma-separated instance variables are chained together
9687 in reverse order; add them one by one. */
9688 tree ivar = nreverse (decls);
9689 for (; ivar; ivar = DECL_CHAIN (ivar))
9690 objc_add_instance_variable (copy_node (ivar));
9692 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9696 /* Parse an objc-class-declaration.
9698 objc-class-declaration:
9699 @class identifier-list ;
9702 static void
9703 c_parser_objc_class_declaration (c_parser *parser)
9705 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9706 c_parser_consume_token (parser);
9707 /* Any identifiers, including those declared as type names, are OK
9708 here. */
9709 while (true)
9711 tree id;
9712 if (c_parser_next_token_is_not (parser, CPP_NAME))
9714 c_parser_error (parser, "expected identifier");
9715 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9716 parser->error = false;
9717 return;
9719 id = c_parser_peek_token (parser)->value;
9720 objc_declare_class (id);
9721 c_parser_consume_token (parser);
9722 if (c_parser_next_token_is (parser, CPP_COMMA))
9723 c_parser_consume_token (parser);
9724 else
9725 break;
9727 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9730 /* Parse an objc-alias-declaration.
9732 objc-alias-declaration:
9733 @compatibility_alias identifier identifier ;
9736 static void
9737 c_parser_objc_alias_declaration (c_parser *parser)
9739 tree id1, id2;
9740 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9741 c_parser_consume_token (parser);
9742 if (c_parser_next_token_is_not (parser, CPP_NAME))
9744 c_parser_error (parser, "expected identifier");
9745 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9746 return;
9748 id1 = c_parser_peek_token (parser)->value;
9749 c_parser_consume_token (parser);
9750 if (c_parser_next_token_is_not (parser, CPP_NAME))
9752 c_parser_error (parser, "expected identifier");
9753 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9754 return;
9756 id2 = c_parser_peek_token (parser)->value;
9757 c_parser_consume_token (parser);
9758 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9759 objc_declare_alias (id1, id2);
9762 /* Parse an objc-protocol-definition.
9764 objc-protocol-definition:
9765 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9766 @protocol identifier-list ;
9768 "@protocol identifier ;" should be resolved as "@protocol
9769 identifier-list ;": objc-methodprotolist may not start with a
9770 semicolon in the first alternative if objc-protocol-refs are
9771 omitted. */
9773 static void
9774 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9776 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9778 c_parser_consume_token (parser);
9779 if (c_parser_next_token_is_not (parser, CPP_NAME))
9781 c_parser_error (parser, "expected identifier");
9782 return;
9784 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9785 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9787 /* Any identifiers, including those declared as type names, are
9788 OK here. */
9789 while (true)
9791 tree id;
9792 if (c_parser_next_token_is_not (parser, CPP_NAME))
9794 c_parser_error (parser, "expected identifier");
9795 break;
9797 id = c_parser_peek_token (parser)->value;
9798 objc_declare_protocol (id, attributes);
9799 c_parser_consume_token (parser);
9800 if (c_parser_next_token_is (parser, CPP_COMMA))
9801 c_parser_consume_token (parser);
9802 else
9803 break;
9805 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9807 else
9809 tree id = c_parser_peek_token (parser)->value;
9810 tree proto = NULL_TREE;
9811 c_parser_consume_token (parser);
9812 if (c_parser_next_token_is (parser, CPP_LESS))
9813 proto = c_parser_objc_protocol_refs (parser);
9814 parser->objc_pq_context = true;
9815 objc_start_protocol (id, proto, attributes);
9816 c_parser_objc_methodprotolist (parser);
9817 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9818 parser->objc_pq_context = false;
9819 objc_finish_interface ();
9823 /* Parse an objc-method-type.
9825 objc-method-type:
9829 Return true if it is a class method (+) and false if it is
9830 an instance method (-).
9832 static inline bool
9833 c_parser_objc_method_type (c_parser *parser)
9835 switch (c_parser_peek_token (parser)->type)
9837 case CPP_PLUS:
9838 c_parser_consume_token (parser);
9839 return true;
9840 case CPP_MINUS:
9841 c_parser_consume_token (parser);
9842 return false;
9843 default:
9844 gcc_unreachable ();
9848 /* Parse an objc-method-definition.
9850 objc-method-definition:
9851 objc-method-type objc-method-decl ;[opt] compound-statement
9854 static void
9855 c_parser_objc_method_definition (c_parser *parser)
9857 bool is_class_method = c_parser_objc_method_type (parser);
9858 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9859 parser->objc_pq_context = true;
9860 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9861 &expr);
9862 if (decl == error_mark_node)
9863 return; /* Bail here. */
9865 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9867 c_parser_consume_token (parser);
9868 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9869 "extra semicolon in method definition specified");
9872 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9874 c_parser_error (parser, "expected %<{%>");
9875 return;
9878 parser->objc_pq_context = false;
9879 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9881 add_stmt (c_parser_compound_statement (parser));
9882 objc_finish_method_definition (current_function_decl);
9884 else
9886 /* This code is executed when we find a method definition
9887 outside of an @implementation context (or invalid for other
9888 reasons). Parse the method (to keep going) but do not emit
9889 any code.
9891 c_parser_compound_statement (parser);
9895 /* Parse an objc-methodprotolist.
9897 objc-methodprotolist:
9898 empty
9899 objc-methodprotolist objc-methodproto
9900 objc-methodprotolist declaration
9901 objc-methodprotolist ;
9902 @optional
9903 @required
9905 The declaration is a data definition, which may be missing
9906 declaration specifiers under the same rules and diagnostics as
9907 other data definitions outside functions, and the stray semicolon
9908 is diagnosed the same way as a stray semicolon outside a
9909 function. */
9911 static void
9912 c_parser_objc_methodprotolist (c_parser *parser)
9914 while (true)
9916 /* The list is terminated by @end. */
9917 switch (c_parser_peek_token (parser)->type)
9919 case CPP_SEMICOLON:
9920 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9921 "ISO C does not allow extra %<;%> outside of a function");
9922 c_parser_consume_token (parser);
9923 break;
9924 case CPP_PLUS:
9925 case CPP_MINUS:
9926 c_parser_objc_methodproto (parser);
9927 break;
9928 case CPP_PRAGMA:
9929 c_parser_pragma (parser, pragma_external, NULL);
9930 break;
9931 case CPP_EOF:
9932 return;
9933 default:
9934 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9935 return;
9936 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9937 c_parser_objc_at_property_declaration (parser);
9938 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9940 objc_set_method_opt (true);
9941 c_parser_consume_token (parser);
9943 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9945 objc_set_method_opt (false);
9946 c_parser_consume_token (parser);
9948 else
9949 c_parser_declaration_or_fndef (parser, false, false, true,
9950 false, true, NULL, vNULL);
9951 break;
9956 /* Parse an objc-methodproto.
9958 objc-methodproto:
9959 objc-method-type objc-method-decl ;
9962 static void
9963 c_parser_objc_methodproto (c_parser *parser)
9965 bool is_class_method = c_parser_objc_method_type (parser);
9966 tree decl, attributes = NULL_TREE;
9968 /* Remember protocol qualifiers in prototypes. */
9969 parser->objc_pq_context = true;
9970 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9971 NULL);
9972 /* Forget protocol qualifiers now. */
9973 parser->objc_pq_context = false;
9975 /* Do not allow the presence of attributes to hide an erroneous
9976 method implementation in the interface section. */
9977 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9979 c_parser_error (parser, "expected %<;%>");
9980 return;
9983 if (decl != error_mark_node)
9984 objc_add_method_declaration (is_class_method, decl, attributes);
9986 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9989 /* If we are at a position that method attributes may be present, check that
9990 there are not any parsed already (a syntax error) and then collect any
9991 specified at the current location. Finally, if new attributes were present,
9992 check that the next token is legal ( ';' for decls and '{' for defs). */
9994 static bool
9995 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9997 bool bad = false;
9998 if (*attributes)
10000 c_parser_error (parser,
10001 "method attributes must be specified at the end only");
10002 *attributes = NULL_TREE;
10003 bad = true;
10006 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10007 *attributes = c_parser_attributes (parser);
10009 /* If there were no attributes here, just report any earlier error. */
10010 if (*attributes == NULL_TREE || bad)
10011 return bad;
10013 /* If the attributes are followed by a ; or {, then just report any earlier
10014 error. */
10015 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10016 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10017 return bad;
10019 /* We've got attributes, but not at the end. */
10020 c_parser_error (parser,
10021 "expected %<;%> or %<{%> after method attribute definition");
10022 return true;
10025 /* Parse an objc-method-decl.
10027 objc-method-decl:
10028 ( objc-type-name ) objc-selector
10029 objc-selector
10030 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10031 objc-keyword-selector objc-optparmlist
10032 attributes
10034 objc-keyword-selector:
10035 objc-keyword-decl
10036 objc-keyword-selector objc-keyword-decl
10038 objc-keyword-decl:
10039 objc-selector : ( objc-type-name ) identifier
10040 objc-selector : identifier
10041 : ( objc-type-name ) identifier
10042 : identifier
10044 objc-optparmlist:
10045 objc-optparms objc-optellipsis
10047 objc-optparms:
10048 empty
10049 objc-opt-parms , parameter-declaration
10051 objc-optellipsis:
10052 empty
10053 , ...
10056 static tree
10057 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10058 tree *attributes, tree *expr)
10060 tree type = NULL_TREE;
10061 tree sel;
10062 tree parms = NULL_TREE;
10063 bool ellipsis = false;
10064 bool attr_err = false;
10066 *attributes = NULL_TREE;
10067 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10069 matching_parens parens;
10070 parens.consume_open (parser);
10071 type = c_parser_objc_type_name (parser);
10072 parens.skip_until_found_close (parser);
10074 sel = c_parser_objc_selector (parser);
10075 /* If there is no selector, or a colon follows, we have an
10076 objc-keyword-selector. If there is a selector, and a colon does
10077 not follow, that selector ends the objc-method-decl. */
10078 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10080 tree tsel = sel;
10081 tree list = NULL_TREE;
10082 while (true)
10084 tree atype = NULL_TREE, id, keyworddecl;
10085 tree param_attr = NULL_TREE;
10086 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10087 break;
10088 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10090 c_parser_consume_token (parser);
10091 atype = c_parser_objc_type_name (parser);
10092 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10093 "expected %<)%>");
10095 /* New ObjC allows attributes on method parameters. */
10096 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10097 param_attr = c_parser_attributes (parser);
10098 if (c_parser_next_token_is_not (parser, CPP_NAME))
10100 c_parser_error (parser, "expected identifier");
10101 return error_mark_node;
10103 id = c_parser_peek_token (parser)->value;
10104 c_parser_consume_token (parser);
10105 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10106 list = chainon (list, keyworddecl);
10107 tsel = c_parser_objc_selector (parser);
10108 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10109 break;
10112 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10114 /* Parse the optional parameter list. Optional Objective-C
10115 method parameters follow the C syntax, and may include '...'
10116 to denote a variable number of arguments. */
10117 parms = make_node (TREE_LIST);
10118 while (c_parser_next_token_is (parser, CPP_COMMA))
10120 struct c_parm *parm;
10121 c_parser_consume_token (parser);
10122 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10124 ellipsis = true;
10125 c_parser_consume_token (parser);
10126 attr_err |= c_parser_objc_maybe_method_attributes
10127 (parser, attributes) ;
10128 break;
10130 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10131 if (parm == NULL)
10132 break;
10133 parms = chainon (parms,
10134 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10136 sel = list;
10138 else
10139 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10141 if (sel == NULL)
10143 c_parser_error (parser, "objective-c method declaration is expected");
10144 return error_mark_node;
10147 if (attr_err)
10148 return error_mark_node;
10150 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10153 /* Parse an objc-type-name.
10155 objc-type-name:
10156 objc-type-qualifiers[opt] type-name
10157 objc-type-qualifiers[opt]
10159 objc-type-qualifiers:
10160 objc-type-qualifier
10161 objc-type-qualifiers objc-type-qualifier
10163 objc-type-qualifier: one of
10164 in out inout bycopy byref oneway
10167 static tree
10168 c_parser_objc_type_name (c_parser *parser)
10170 tree quals = NULL_TREE;
10171 struct c_type_name *type_name = NULL;
10172 tree type = NULL_TREE;
10173 while (true)
10175 c_token *token = c_parser_peek_token (parser);
10176 if (token->type == CPP_KEYWORD
10177 && (token->keyword == RID_IN
10178 || token->keyword == RID_OUT
10179 || token->keyword == RID_INOUT
10180 || token->keyword == RID_BYCOPY
10181 || token->keyword == RID_BYREF
10182 || token->keyword == RID_ONEWAY))
10184 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10185 c_parser_consume_token (parser);
10187 else
10188 break;
10190 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10191 type_name = c_parser_type_name (parser);
10192 if (type_name)
10193 type = groktypename (type_name, NULL, NULL);
10195 /* If the type is unknown, and error has already been produced and
10196 we need to recover from the error. In that case, use NULL_TREE
10197 for the type, as if no type had been specified; this will use the
10198 default type ('id') which is good for error recovery. */
10199 if (type == error_mark_node)
10200 type = NULL_TREE;
10202 return build_tree_list (quals, type);
10205 /* Parse objc-protocol-refs.
10207 objc-protocol-refs:
10208 < identifier-list >
10211 static tree
10212 c_parser_objc_protocol_refs (c_parser *parser)
10214 tree list = NULL_TREE;
10215 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10216 c_parser_consume_token (parser);
10217 /* Any identifiers, including those declared as type names, are OK
10218 here. */
10219 while (true)
10221 tree id;
10222 if (c_parser_next_token_is_not (parser, CPP_NAME))
10224 c_parser_error (parser, "expected identifier");
10225 break;
10227 id = c_parser_peek_token (parser)->value;
10228 list = chainon (list, build_tree_list (NULL_TREE, id));
10229 c_parser_consume_token (parser);
10230 if (c_parser_next_token_is (parser, CPP_COMMA))
10231 c_parser_consume_token (parser);
10232 else
10233 break;
10235 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10236 return list;
10239 /* Parse an objc-try-catch-finally-statement.
10241 objc-try-catch-finally-statement:
10242 @try compound-statement objc-catch-list[opt]
10243 @try compound-statement objc-catch-list[opt] @finally compound-statement
10245 objc-catch-list:
10246 @catch ( objc-catch-parameter-declaration ) compound-statement
10247 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10249 objc-catch-parameter-declaration:
10250 parameter-declaration
10251 '...'
10253 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10255 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10256 for C++. Keep them in sync. */
10258 static void
10259 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10261 location_t location;
10262 tree stmt;
10264 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10265 c_parser_consume_token (parser);
10266 location = c_parser_peek_token (parser)->location;
10267 objc_maybe_warn_exceptions (location);
10268 stmt = c_parser_compound_statement (parser);
10269 objc_begin_try_stmt (location, stmt);
10271 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10273 struct c_parm *parm;
10274 tree parameter_declaration = error_mark_node;
10275 bool seen_open_paren = false;
10277 c_parser_consume_token (parser);
10278 matching_parens parens;
10279 if (!parens.require_open (parser))
10280 seen_open_paren = true;
10281 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10283 /* We have "@catch (...)" (where the '...' are literally
10284 what is in the code). Skip the '...'.
10285 parameter_declaration is set to NULL_TREE, and
10286 objc_being_catch_clauses() knows that that means
10287 '...'. */
10288 c_parser_consume_token (parser);
10289 parameter_declaration = NULL_TREE;
10291 else
10293 /* We have "@catch (NSException *exception)" or something
10294 like that. Parse the parameter declaration. */
10295 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10296 if (parm == NULL)
10297 parameter_declaration = error_mark_node;
10298 else
10299 parameter_declaration = grokparm (parm, NULL);
10301 if (seen_open_paren)
10302 parens.require_close (parser);
10303 else
10305 /* If there was no open parenthesis, we are recovering from
10306 an error, and we are trying to figure out what mistake
10307 the user has made. */
10309 /* If there is an immediate closing parenthesis, the user
10310 probably forgot the opening one (ie, they typed "@catch
10311 NSException *e)". Parse the closing parenthesis and keep
10312 going. */
10313 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10314 c_parser_consume_token (parser);
10316 /* If these is no immediate closing parenthesis, the user
10317 probably doesn't know that parenthesis are required at
10318 all (ie, they typed "@catch NSException *e"). So, just
10319 forget about the closing parenthesis and keep going. */
10321 objc_begin_catch_clause (parameter_declaration);
10322 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10323 c_parser_compound_statement_nostart (parser);
10324 objc_finish_catch_clause ();
10326 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10328 c_parser_consume_token (parser);
10329 location = c_parser_peek_token (parser)->location;
10330 stmt = c_parser_compound_statement (parser);
10331 objc_build_finally_clause (location, stmt);
10333 objc_finish_try_stmt ();
10336 /* Parse an objc-synchronized-statement.
10338 objc-synchronized-statement:
10339 @synchronized ( expression ) compound-statement
10342 static void
10343 c_parser_objc_synchronized_statement (c_parser *parser)
10345 location_t loc;
10346 tree expr, stmt;
10347 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10348 c_parser_consume_token (parser);
10349 loc = c_parser_peek_token (parser)->location;
10350 objc_maybe_warn_exceptions (loc);
10351 matching_parens parens;
10352 if (parens.require_open (parser))
10354 struct c_expr ce = c_parser_expression (parser);
10355 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10356 expr = ce.value;
10357 expr = c_fully_fold (expr, false, NULL);
10358 parens.skip_until_found_close (parser);
10360 else
10361 expr = error_mark_node;
10362 stmt = c_parser_compound_statement (parser);
10363 objc_build_synchronized (loc, expr, stmt);
10366 /* Parse an objc-selector; return NULL_TREE without an error if the
10367 next token is not an objc-selector.
10369 objc-selector:
10370 identifier
10371 one of
10372 enum struct union if else while do for switch case default
10373 break continue return goto asm sizeof typeof __alignof
10374 unsigned long const short volatile signed restrict _Complex
10375 in out inout bycopy byref oneway int char float double void _Bool
10376 _Atomic
10378 ??? Why this selection of keywords but not, for example, storage
10379 class specifiers? */
10381 static tree
10382 c_parser_objc_selector (c_parser *parser)
10384 c_token *token = c_parser_peek_token (parser);
10385 tree value = token->value;
10386 if (token->type == CPP_NAME)
10388 c_parser_consume_token (parser);
10389 return value;
10391 if (token->type != CPP_KEYWORD)
10392 return NULL_TREE;
10393 switch (token->keyword)
10395 case RID_ENUM:
10396 case RID_STRUCT:
10397 case RID_UNION:
10398 case RID_IF:
10399 case RID_ELSE:
10400 case RID_WHILE:
10401 case RID_DO:
10402 case RID_FOR:
10403 case RID_SWITCH:
10404 case RID_CASE:
10405 case RID_DEFAULT:
10406 case RID_BREAK:
10407 case RID_CONTINUE:
10408 case RID_RETURN:
10409 case RID_GOTO:
10410 case RID_ASM:
10411 case RID_SIZEOF:
10412 case RID_TYPEOF:
10413 case RID_ALIGNOF:
10414 case RID_UNSIGNED:
10415 case RID_LONG:
10416 case RID_CONST:
10417 case RID_SHORT:
10418 case RID_VOLATILE:
10419 case RID_SIGNED:
10420 case RID_RESTRICT:
10421 case RID_COMPLEX:
10422 case RID_IN:
10423 case RID_OUT:
10424 case RID_INOUT:
10425 case RID_BYCOPY:
10426 case RID_BYREF:
10427 case RID_ONEWAY:
10428 case RID_INT:
10429 case RID_CHAR:
10430 case RID_FLOAT:
10431 case RID_DOUBLE:
10432 CASE_RID_FLOATN_NX:
10433 case RID_VOID:
10434 case RID_BOOL:
10435 case RID_ATOMIC:
10436 case RID_AUTO_TYPE:
10437 case RID_INT_N_0:
10438 case RID_INT_N_1:
10439 case RID_INT_N_2:
10440 case RID_INT_N_3:
10441 c_parser_consume_token (parser);
10442 return value;
10443 default:
10444 return NULL_TREE;
10448 /* Parse an objc-selector-arg.
10450 objc-selector-arg:
10451 objc-selector
10452 objc-keywordname-list
10454 objc-keywordname-list:
10455 objc-keywordname
10456 objc-keywordname-list objc-keywordname
10458 objc-keywordname:
10459 objc-selector :
10463 static tree
10464 c_parser_objc_selector_arg (c_parser *parser)
10466 tree sel = c_parser_objc_selector (parser);
10467 tree list = NULL_TREE;
10468 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10469 return sel;
10470 while (true)
10472 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10473 return list;
10474 list = chainon (list, build_tree_list (sel, NULL_TREE));
10475 sel = c_parser_objc_selector (parser);
10476 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10477 break;
10479 return list;
10482 /* Parse an objc-receiver.
10484 objc-receiver:
10485 expression
10486 class-name
10487 type-name
10490 static tree
10491 c_parser_objc_receiver (c_parser *parser)
10493 location_t loc = c_parser_peek_token (parser)->location;
10495 if (c_parser_peek_token (parser)->type == CPP_NAME
10496 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10497 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10499 tree id = c_parser_peek_token (parser)->value;
10500 c_parser_consume_token (parser);
10501 return objc_get_class_reference (id);
10503 struct c_expr ce = c_parser_expression (parser);
10504 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10505 return c_fully_fold (ce.value, false, NULL);
10508 /* Parse objc-message-args.
10510 objc-message-args:
10511 objc-selector
10512 objc-keywordarg-list
10514 objc-keywordarg-list:
10515 objc-keywordarg
10516 objc-keywordarg-list objc-keywordarg
10518 objc-keywordarg:
10519 objc-selector : objc-keywordexpr
10520 : objc-keywordexpr
10523 static tree
10524 c_parser_objc_message_args (c_parser *parser)
10526 tree sel = c_parser_objc_selector (parser);
10527 tree list = NULL_TREE;
10528 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10529 return sel;
10530 while (true)
10532 tree keywordexpr;
10533 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10534 return error_mark_node;
10535 keywordexpr = c_parser_objc_keywordexpr (parser);
10536 list = chainon (list, build_tree_list (sel, keywordexpr));
10537 sel = c_parser_objc_selector (parser);
10538 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10539 break;
10541 return list;
10544 /* Parse an objc-keywordexpr.
10546 objc-keywordexpr:
10547 nonempty-expr-list
10550 static tree
10551 c_parser_objc_keywordexpr (c_parser *parser)
10553 tree ret;
10554 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10555 NULL, NULL, NULL, NULL);
10556 if (vec_safe_length (expr_list) == 1)
10558 /* Just return the expression, remove a level of
10559 indirection. */
10560 ret = (*expr_list)[0];
10562 else
10564 /* We have a comma expression, we will collapse later. */
10565 ret = build_tree_list_vec (expr_list);
10567 release_tree_vector (expr_list);
10568 return ret;
10571 /* A check, needed in several places, that ObjC interface, implementation or
10572 method definitions are not prefixed by incorrect items. */
10573 static bool
10574 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10575 struct c_declspecs *specs)
10577 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10578 || specs->typespec_kind != ctsk_none)
10580 c_parser_error (parser,
10581 "no type or storage class may be specified here,");
10582 c_parser_skip_to_end_of_block_or_statement (parser);
10583 return true;
10585 return false;
10588 /* Parse an Objective-C @property declaration. The syntax is:
10590 objc-property-declaration:
10591 '@property' objc-property-attributes[opt] struct-declaration ;
10593 objc-property-attributes:
10594 '(' objc-property-attribute-list ')'
10596 objc-property-attribute-list:
10597 objc-property-attribute
10598 objc-property-attribute-list, objc-property-attribute
10600 objc-property-attribute
10601 'getter' = identifier
10602 'setter' = identifier
10603 'readonly'
10604 'readwrite'
10605 'assign'
10606 'retain'
10607 'copy'
10608 'nonatomic'
10610 For example:
10611 @property NSString *name;
10612 @property (readonly) id object;
10613 @property (retain, nonatomic, getter=getTheName) id name;
10614 @property int a, b, c;
10616 PS: This function is identical to cp_parser_objc_at_propery_declaration
10617 for C++. Keep them in sync. */
10618 static void
10619 c_parser_objc_at_property_declaration (c_parser *parser)
10621 /* The following variables hold the attributes of the properties as
10622 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10623 seen. When we see an attribute, we set them to 'true' (if they
10624 are boolean properties) or to the identifier (if they have an
10625 argument, ie, for getter and setter). Note that here we only
10626 parse the list of attributes, check the syntax and accumulate the
10627 attributes that we find. objc_add_property_declaration() will
10628 then process the information. */
10629 bool property_assign = false;
10630 bool property_copy = false;
10631 tree property_getter_ident = NULL_TREE;
10632 bool property_nonatomic = false;
10633 bool property_readonly = false;
10634 bool property_readwrite = false;
10635 bool property_retain = false;
10636 tree property_setter_ident = NULL_TREE;
10638 /* 'properties' is the list of properties that we read. Usually a
10639 single one, but maybe more (eg, in "@property int a, b, c;" there
10640 are three). */
10641 tree properties;
10642 location_t loc;
10644 loc = c_parser_peek_token (parser)->location;
10645 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10647 c_parser_consume_token (parser); /* Eat '@property'. */
10649 /* Parse the optional attribute list... */
10650 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10652 matching_parens parens;
10654 /* Eat the '(' */
10655 parens.consume_open (parser);
10657 /* Property attribute keywords are valid now. */
10658 parser->objc_property_attr_context = true;
10660 while (true)
10662 bool syntax_error = false;
10663 c_token *token = c_parser_peek_token (parser);
10664 enum rid keyword;
10666 if (token->type != CPP_KEYWORD)
10668 if (token->type == CPP_CLOSE_PAREN)
10669 c_parser_error (parser, "expected identifier");
10670 else
10672 c_parser_consume_token (parser);
10673 c_parser_error (parser, "unknown property attribute");
10675 break;
10677 keyword = token->keyword;
10678 c_parser_consume_token (parser);
10679 switch (keyword)
10681 case RID_ASSIGN: property_assign = true; break;
10682 case RID_COPY: property_copy = true; break;
10683 case RID_NONATOMIC: property_nonatomic = true; break;
10684 case RID_READONLY: property_readonly = true; break;
10685 case RID_READWRITE: property_readwrite = true; break;
10686 case RID_RETAIN: property_retain = true; break;
10688 case RID_GETTER:
10689 case RID_SETTER:
10690 if (c_parser_next_token_is_not (parser, CPP_EQ))
10692 if (keyword == RID_GETTER)
10693 c_parser_error (parser,
10694 "missing %<=%> (after %<getter%> attribute)");
10695 else
10696 c_parser_error (parser,
10697 "missing %<=%> (after %<setter%> attribute)");
10698 syntax_error = true;
10699 break;
10701 c_parser_consume_token (parser); /* eat the = */
10702 if (c_parser_next_token_is_not (parser, CPP_NAME))
10704 c_parser_error (parser, "expected identifier");
10705 syntax_error = true;
10706 break;
10708 if (keyword == RID_SETTER)
10710 if (property_setter_ident != NULL_TREE)
10711 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10712 else
10713 property_setter_ident = c_parser_peek_token (parser)->value;
10714 c_parser_consume_token (parser);
10715 if (c_parser_next_token_is_not (parser, CPP_COLON))
10716 c_parser_error (parser, "setter name must terminate with %<:%>");
10717 else
10718 c_parser_consume_token (parser);
10720 else
10722 if (property_getter_ident != NULL_TREE)
10723 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10724 else
10725 property_getter_ident = c_parser_peek_token (parser)->value;
10726 c_parser_consume_token (parser);
10728 break;
10729 default:
10730 c_parser_error (parser, "unknown property attribute");
10731 syntax_error = true;
10732 break;
10735 if (syntax_error)
10736 break;
10738 if (c_parser_next_token_is (parser, CPP_COMMA))
10739 c_parser_consume_token (parser);
10740 else
10741 break;
10743 parser->objc_property_attr_context = false;
10744 parens.skip_until_found_close (parser);
10746 /* ... and the property declaration(s). */
10747 properties = c_parser_struct_declaration (parser);
10749 if (properties == error_mark_node)
10751 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10752 parser->error = false;
10753 return;
10756 if (properties == NULL_TREE)
10757 c_parser_error (parser, "expected identifier");
10758 else
10760 /* Comma-separated properties are chained together in
10761 reverse order; add them one by one. */
10762 properties = nreverse (properties);
10764 for (; properties; properties = TREE_CHAIN (properties))
10765 objc_add_property_declaration (loc, copy_node (properties),
10766 property_readonly, property_readwrite,
10767 property_assign, property_retain,
10768 property_copy, property_nonatomic,
10769 property_getter_ident, property_setter_ident);
10772 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10773 parser->error = false;
10776 /* Parse an Objective-C @synthesize declaration. The syntax is:
10778 objc-synthesize-declaration:
10779 @synthesize objc-synthesize-identifier-list ;
10781 objc-synthesize-identifier-list:
10782 objc-synthesize-identifier
10783 objc-synthesize-identifier-list, objc-synthesize-identifier
10785 objc-synthesize-identifier
10786 identifier
10787 identifier = identifier
10789 For example:
10790 @synthesize MyProperty;
10791 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10793 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10794 for C++. Keep them in sync.
10796 static void
10797 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10799 tree list = NULL_TREE;
10800 location_t loc;
10801 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10802 loc = c_parser_peek_token (parser)->location;
10804 c_parser_consume_token (parser);
10805 while (true)
10807 tree property, ivar;
10808 if (c_parser_next_token_is_not (parser, CPP_NAME))
10810 c_parser_error (parser, "expected identifier");
10811 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10812 /* Once we find the semicolon, we can resume normal parsing.
10813 We have to reset parser->error manually because
10814 c_parser_skip_until_found() won't reset it for us if the
10815 next token is precisely a semicolon. */
10816 parser->error = false;
10817 return;
10819 property = c_parser_peek_token (parser)->value;
10820 c_parser_consume_token (parser);
10821 if (c_parser_next_token_is (parser, CPP_EQ))
10823 c_parser_consume_token (parser);
10824 if (c_parser_next_token_is_not (parser, CPP_NAME))
10826 c_parser_error (parser, "expected identifier");
10827 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10828 parser->error = false;
10829 return;
10831 ivar = c_parser_peek_token (parser)->value;
10832 c_parser_consume_token (parser);
10834 else
10835 ivar = NULL_TREE;
10836 list = chainon (list, build_tree_list (ivar, property));
10837 if (c_parser_next_token_is (parser, CPP_COMMA))
10838 c_parser_consume_token (parser);
10839 else
10840 break;
10842 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10843 objc_add_synthesize_declaration (loc, list);
10846 /* Parse an Objective-C @dynamic declaration. The syntax is:
10848 objc-dynamic-declaration:
10849 @dynamic identifier-list ;
10851 For example:
10852 @dynamic MyProperty;
10853 @dynamic MyProperty, AnotherProperty;
10855 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10856 for C++. Keep them in sync.
10858 static void
10859 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10861 tree list = NULL_TREE;
10862 location_t loc;
10863 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10864 loc = c_parser_peek_token (parser)->location;
10866 c_parser_consume_token (parser);
10867 while (true)
10869 tree property;
10870 if (c_parser_next_token_is_not (parser, CPP_NAME))
10872 c_parser_error (parser, "expected identifier");
10873 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10874 parser->error = false;
10875 return;
10877 property = c_parser_peek_token (parser)->value;
10878 list = chainon (list, build_tree_list (NULL_TREE, property));
10879 c_parser_consume_token (parser);
10880 if (c_parser_next_token_is (parser, CPP_COMMA))
10881 c_parser_consume_token (parser);
10882 else
10883 break;
10885 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10886 objc_add_dynamic_declaration (loc, list);
10890 /* Parse a pragma GCC ivdep. */
10892 static bool
10893 c_parse_pragma_ivdep (c_parser *parser)
10895 c_parser_consume_pragma (parser);
10896 c_parser_skip_to_pragma_eol (parser);
10897 return true;
10900 /* Parse a pragma GCC unroll. */
10902 static unsigned short
10903 c_parser_pragma_unroll (c_parser *parser)
10905 unsigned short unroll;
10906 c_parser_consume_pragma (parser);
10907 location_t location = c_parser_peek_token (parser)->location;
10908 tree expr = c_parser_expr_no_commas (parser, NULL).value;
10909 mark_exp_read (expr);
10910 expr = c_fully_fold (expr, false, NULL);
10911 HOST_WIDE_INT lunroll = 0;
10912 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
10913 || TREE_CODE (expr) != INTEGER_CST
10914 || (lunroll = tree_to_shwi (expr)) < 0
10915 || lunroll >= USHRT_MAX)
10917 error_at (location, "%<#pragma GCC unroll%> requires an"
10918 " assignment-expression that evaluates to a non-negative"
10919 " integral constant less than %u", USHRT_MAX);
10920 unroll = 0;
10922 else
10924 unroll = (unsigned short)lunroll;
10925 if (unroll == 0)
10926 unroll = 1;
10929 c_parser_skip_to_pragma_eol (parser);
10930 return unroll;
10933 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10934 should be considered, statements. ALLOW_STMT is true if we're within
10935 the context of a function and such pragmas are to be allowed. Returns
10936 true if we actually parsed such a pragma. */
10938 static bool
10939 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10941 unsigned int id;
10942 const char *construct = NULL;
10944 id = c_parser_peek_token (parser)->pragma_kind;
10945 gcc_assert (id != PRAGMA_NONE);
10947 switch (id)
10949 case PRAGMA_OACC_DECLARE:
10950 c_parser_oacc_declare (parser);
10951 return false;
10953 case PRAGMA_OACC_ENTER_DATA:
10954 if (context != pragma_compound)
10956 construct = "acc enter data";
10957 in_compound:
10958 if (context == pragma_stmt)
10960 error_at (c_parser_peek_token (parser)->location,
10961 "%<#pragma %s%> may only be used in compound "
10962 "statements", construct);
10963 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10964 return false;
10966 goto bad_stmt;
10968 c_parser_oacc_enter_exit_data (parser, true);
10969 return false;
10971 case PRAGMA_OACC_EXIT_DATA:
10972 if (context != pragma_compound)
10974 construct = "acc exit data";
10975 goto in_compound;
10977 c_parser_oacc_enter_exit_data (parser, false);
10978 return false;
10980 case PRAGMA_OACC_ROUTINE:
10981 if (context != pragma_external)
10983 error_at (c_parser_peek_token (parser)->location,
10984 "%<#pragma acc routine%> must be at file scope");
10985 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10986 return false;
10988 c_parser_oacc_routine (parser, context);
10989 return false;
10991 case PRAGMA_OACC_UPDATE:
10992 if (context != pragma_compound)
10994 construct = "acc update";
10995 goto in_compound;
10997 c_parser_oacc_update (parser);
10998 return false;
11000 case PRAGMA_OMP_BARRIER:
11001 if (context != pragma_compound)
11003 construct = "omp barrier";
11004 goto in_compound;
11006 c_parser_omp_barrier (parser);
11007 return false;
11009 case PRAGMA_OMP_FLUSH:
11010 if (context != pragma_compound)
11012 construct = "omp flush";
11013 goto in_compound;
11015 c_parser_omp_flush (parser);
11016 return false;
11018 case PRAGMA_OMP_TASKWAIT:
11019 if (context != pragma_compound)
11021 construct = "omp taskwait";
11022 goto in_compound;
11024 c_parser_omp_taskwait (parser);
11025 return false;
11027 case PRAGMA_OMP_TASKYIELD:
11028 if (context != pragma_compound)
11030 construct = "omp taskyield";
11031 goto in_compound;
11033 c_parser_omp_taskyield (parser);
11034 return false;
11036 case PRAGMA_OMP_CANCEL:
11037 if (context != pragma_compound)
11039 construct = "omp cancel";
11040 goto in_compound;
11042 c_parser_omp_cancel (parser);
11043 return false;
11045 case PRAGMA_OMP_CANCELLATION_POINT:
11046 c_parser_omp_cancellation_point (parser, context);
11047 return false;
11049 case PRAGMA_OMP_THREADPRIVATE:
11050 c_parser_omp_threadprivate (parser);
11051 return false;
11053 case PRAGMA_OMP_TARGET:
11054 return c_parser_omp_target (parser, context, if_p);
11056 case PRAGMA_OMP_END_DECLARE_TARGET:
11057 c_parser_omp_end_declare_target (parser);
11058 return false;
11060 case PRAGMA_OMP_SECTION:
11061 error_at (c_parser_peek_token (parser)->location,
11062 "%<#pragma omp section%> may only be used in "
11063 "%<#pragma omp sections%> construct");
11064 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11065 return false;
11067 case PRAGMA_OMP_DECLARE:
11068 c_parser_omp_declare (parser, context);
11069 return false;
11071 case PRAGMA_OMP_ORDERED:
11072 return c_parser_omp_ordered (parser, context, if_p);
11074 case PRAGMA_IVDEP:
11076 const bool ivdep = c_parse_pragma_ivdep (parser);
11077 unsigned short unroll;
11078 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11079 unroll = c_parser_pragma_unroll (parser);
11080 else
11081 unroll = 0;
11082 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11083 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11084 && !c_parser_next_token_is_keyword (parser, RID_DO))
11086 c_parser_error (parser, "for, while or do statement expected");
11087 return false;
11089 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11090 c_parser_for_statement (parser, ivdep, unroll, if_p);
11091 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11092 c_parser_while_statement (parser, ivdep, unroll, if_p);
11093 else
11094 c_parser_do_statement (parser, ivdep, unroll);
11096 return false;
11098 case PRAGMA_UNROLL:
11100 unsigned short unroll = c_parser_pragma_unroll (parser);
11101 bool ivdep;
11102 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11103 ivdep = c_parse_pragma_ivdep (parser);
11104 else
11105 ivdep = false;
11106 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11107 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11108 && !c_parser_next_token_is_keyword (parser, RID_DO))
11110 c_parser_error (parser, "for, while or do statement expected");
11111 return false;
11113 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11114 c_parser_for_statement (parser, ivdep, unroll, if_p);
11115 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11116 c_parser_while_statement (parser, ivdep, unroll, if_p);
11117 else
11118 c_parser_do_statement (parser, ivdep, unroll);
11120 return false;
11122 case PRAGMA_GCC_PCH_PREPROCESS:
11123 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11124 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11125 return false;
11127 case PRAGMA_OACC_WAIT:
11128 if (context != pragma_compound)
11130 construct = "acc wait";
11131 goto in_compound;
11133 /* FALL THROUGH. */
11135 default:
11136 if (id < PRAGMA_FIRST_EXTERNAL)
11138 if (context != pragma_stmt && context != pragma_compound)
11140 bad_stmt:
11141 c_parser_error (parser, "expected declaration specifiers");
11142 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11143 return false;
11145 c_parser_omp_construct (parser, if_p);
11146 return true;
11148 break;
11151 c_parser_consume_pragma (parser);
11152 c_invoke_pragma_handler (id);
11154 /* Skip to EOL, but suppress any error message. Those will have been
11155 generated by the handler routine through calling error, as opposed
11156 to calling c_parser_error. */
11157 parser->error = true;
11158 c_parser_skip_to_pragma_eol (parser);
11160 return false;
11163 /* The interface the pragma parsers have to the lexer. */
11165 enum cpp_ttype
11166 pragma_lex (tree *value, location_t *loc)
11168 c_token *tok = c_parser_peek_token (the_parser);
11169 enum cpp_ttype ret = tok->type;
11171 *value = tok->value;
11172 if (loc)
11173 *loc = tok->location;
11175 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11176 ret = CPP_EOF;
11177 else
11179 if (ret == CPP_KEYWORD)
11180 ret = CPP_NAME;
11181 c_parser_consume_token (the_parser);
11184 return ret;
11187 static void
11188 c_parser_pragma_pch_preprocess (c_parser *parser)
11190 tree name = NULL;
11192 c_parser_consume_pragma (parser);
11193 if (c_parser_next_token_is (parser, CPP_STRING))
11195 name = c_parser_peek_token (parser)->value;
11196 c_parser_consume_token (parser);
11198 else
11199 c_parser_error (parser, "expected string literal");
11200 c_parser_skip_to_pragma_eol (parser);
11202 if (name)
11203 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11206 /* OpenACC and OpenMP parsing routines. */
11208 /* Returns name of the next clause.
11209 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11210 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11211 returned and the token is consumed. */
11213 static pragma_omp_clause
11214 c_parser_omp_clause_name (c_parser *parser)
11216 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11218 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11219 result = PRAGMA_OACC_CLAUSE_AUTO;
11220 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11221 result = PRAGMA_OMP_CLAUSE_IF;
11222 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11223 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11224 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11225 result = PRAGMA_OMP_CLAUSE_FOR;
11226 else if (c_parser_next_token_is (parser, CPP_NAME))
11228 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11230 switch (p[0])
11232 case 'a':
11233 if (!strcmp ("aligned", p))
11234 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11235 else if (!strcmp ("async", p))
11236 result = PRAGMA_OACC_CLAUSE_ASYNC;
11237 break;
11238 case 'c':
11239 if (!strcmp ("collapse", p))
11240 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11241 else if (!strcmp ("copy", p))
11242 result = PRAGMA_OACC_CLAUSE_COPY;
11243 else if (!strcmp ("copyin", p))
11244 result = PRAGMA_OMP_CLAUSE_COPYIN;
11245 else if (!strcmp ("copyout", p))
11246 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11247 else if (!strcmp ("copyprivate", p))
11248 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11249 else if (!strcmp ("create", p))
11250 result = PRAGMA_OACC_CLAUSE_CREATE;
11251 break;
11252 case 'd':
11253 if (!strcmp ("defaultmap", p))
11254 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11255 else if (!strcmp ("delete", p))
11256 result = PRAGMA_OACC_CLAUSE_DELETE;
11257 else if (!strcmp ("depend", p))
11258 result = PRAGMA_OMP_CLAUSE_DEPEND;
11259 else if (!strcmp ("device", p))
11260 result = PRAGMA_OMP_CLAUSE_DEVICE;
11261 else if (!strcmp ("deviceptr", p))
11262 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11263 else if (!strcmp ("device_resident", p))
11264 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11265 else if (!strcmp ("dist_schedule", p))
11266 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11267 break;
11268 case 'f':
11269 if (!strcmp ("final", p))
11270 result = PRAGMA_OMP_CLAUSE_FINAL;
11271 else if (!strcmp ("finalize", p))
11272 result = PRAGMA_OACC_CLAUSE_FINALIZE;
11273 else if (!strcmp ("firstprivate", p))
11274 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11275 else if (!strcmp ("from", p))
11276 result = PRAGMA_OMP_CLAUSE_FROM;
11277 break;
11278 case 'g':
11279 if (!strcmp ("gang", p))
11280 result = PRAGMA_OACC_CLAUSE_GANG;
11281 else if (!strcmp ("grainsize", p))
11282 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11283 break;
11284 case 'h':
11285 if (!strcmp ("hint", p))
11286 result = PRAGMA_OMP_CLAUSE_HINT;
11287 else if (!strcmp ("host", p))
11288 result = PRAGMA_OACC_CLAUSE_HOST;
11289 break;
11290 case 'i':
11291 if (!strcmp ("if_present", p))
11292 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
11293 else if (!strcmp ("inbranch", p))
11294 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11295 else if (!strcmp ("independent", p))
11296 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11297 else if (!strcmp ("is_device_ptr", p))
11298 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11299 break;
11300 case 'l':
11301 if (!strcmp ("lastprivate", p))
11302 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11303 else if (!strcmp ("linear", p))
11304 result = PRAGMA_OMP_CLAUSE_LINEAR;
11305 else if (!strcmp ("link", p))
11306 result = PRAGMA_OMP_CLAUSE_LINK;
11307 break;
11308 case 'm':
11309 if (!strcmp ("map", p))
11310 result = PRAGMA_OMP_CLAUSE_MAP;
11311 else if (!strcmp ("mergeable", p))
11312 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11313 break;
11314 case 'n':
11315 if (!strcmp ("nogroup", p))
11316 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11317 else if (!strcmp ("notinbranch", p))
11318 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11319 else if (!strcmp ("nowait", p))
11320 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11321 else if (!strcmp ("num_gangs", p))
11322 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11323 else if (!strcmp ("num_tasks", p))
11324 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11325 else if (!strcmp ("num_teams", p))
11326 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11327 else if (!strcmp ("num_threads", p))
11328 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11329 else if (!strcmp ("num_workers", p))
11330 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11331 break;
11332 case 'o':
11333 if (!strcmp ("ordered", p))
11334 result = PRAGMA_OMP_CLAUSE_ORDERED;
11335 break;
11336 case 'p':
11337 if (!strcmp ("parallel", p))
11338 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11339 else if (!strcmp ("present", p))
11340 result = PRAGMA_OACC_CLAUSE_PRESENT;
11341 /* As of OpenACC 2.5, these are now aliases of the non-present_or
11342 clauses. */
11343 else if (!strcmp ("present_or_copy", p)
11344 || !strcmp ("pcopy", p))
11345 result = PRAGMA_OACC_CLAUSE_COPY;
11346 else if (!strcmp ("present_or_copyin", p)
11347 || !strcmp ("pcopyin", p))
11348 result = PRAGMA_OACC_CLAUSE_COPYIN;
11349 else if (!strcmp ("present_or_copyout", p)
11350 || !strcmp ("pcopyout", p))
11351 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11352 else if (!strcmp ("present_or_create", p)
11353 || !strcmp ("pcreate", p))
11354 result = PRAGMA_OACC_CLAUSE_CREATE;
11355 else if (!strcmp ("priority", p))
11356 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11357 else if (!strcmp ("private", p))
11358 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11359 else if (!strcmp ("proc_bind", p))
11360 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11361 break;
11362 case 'r':
11363 if (!strcmp ("reduction", p))
11364 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11365 break;
11366 case 's':
11367 if (!strcmp ("safelen", p))
11368 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11369 else if (!strcmp ("schedule", p))
11370 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11371 else if (!strcmp ("sections", p))
11372 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11373 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
11374 result = PRAGMA_OACC_CLAUSE_HOST;
11375 else if (!strcmp ("seq", p))
11376 result = PRAGMA_OACC_CLAUSE_SEQ;
11377 else if (!strcmp ("shared", p))
11378 result = PRAGMA_OMP_CLAUSE_SHARED;
11379 else if (!strcmp ("simd", p))
11380 result = PRAGMA_OMP_CLAUSE_SIMD;
11381 else if (!strcmp ("simdlen", p))
11382 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11383 break;
11384 case 't':
11385 if (!strcmp ("taskgroup", p))
11386 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11387 else if (!strcmp ("thread_limit", p))
11388 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11389 else if (!strcmp ("threads", p))
11390 result = PRAGMA_OMP_CLAUSE_THREADS;
11391 else if (!strcmp ("tile", p))
11392 result = PRAGMA_OACC_CLAUSE_TILE;
11393 else if (!strcmp ("to", p))
11394 result = PRAGMA_OMP_CLAUSE_TO;
11395 break;
11396 case 'u':
11397 if (!strcmp ("uniform", p))
11398 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11399 else if (!strcmp ("untied", p))
11400 result = PRAGMA_OMP_CLAUSE_UNTIED;
11401 else if (!strcmp ("use_device", p))
11402 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11403 else if (!strcmp ("use_device_ptr", p))
11404 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11405 break;
11406 case 'v':
11407 if (!strcmp ("vector", p))
11408 result = PRAGMA_OACC_CLAUSE_VECTOR;
11409 else if (!strcmp ("vector_length", p))
11410 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11411 break;
11412 case 'w':
11413 if (!strcmp ("wait", p))
11414 result = PRAGMA_OACC_CLAUSE_WAIT;
11415 else if (!strcmp ("worker", p))
11416 result = PRAGMA_OACC_CLAUSE_WORKER;
11417 break;
11421 if (result != PRAGMA_OMP_CLAUSE_NONE)
11422 c_parser_consume_token (parser);
11424 return result;
11427 /* Validate that a clause of the given type does not already exist. */
11429 static void
11430 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11431 const char *name)
11433 tree c;
11435 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11436 if (OMP_CLAUSE_CODE (c) == code)
11438 location_t loc = OMP_CLAUSE_LOCATION (c);
11439 error_at (loc, "too many %qs clauses", name);
11440 break;
11444 /* OpenACC 2.0
11445 Parse wait clause or wait directive parameters. */
11447 static tree
11448 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11450 vec<tree, va_gc> *args;
11451 tree t, args_tree;
11453 matching_parens parens;
11454 if (!parens.require_open (parser))
11455 return list;
11457 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11459 if (args->length () == 0)
11461 c_parser_error (parser, "expected integer expression before ')'");
11462 release_tree_vector (args);
11463 return list;
11466 args_tree = build_tree_list_vec (args);
11468 for (t = args_tree; t; t = TREE_CHAIN (t))
11470 tree targ = TREE_VALUE (t);
11472 if (targ != error_mark_node)
11474 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11476 c_parser_error (parser, "expression must be integral");
11477 targ = error_mark_node;
11479 else
11481 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11483 OMP_CLAUSE_DECL (c) = targ;
11484 OMP_CLAUSE_CHAIN (c) = list;
11485 list = c;
11490 release_tree_vector (args);
11491 parens.require_close (parser);
11492 return list;
11495 /* OpenACC 2.0, OpenMP 2.5:
11496 variable-list:
11497 identifier
11498 variable-list , identifier
11500 If KIND is nonzero, create the appropriate node and install the
11501 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11502 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11504 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11505 return the list created. */
11507 static tree
11508 c_parser_omp_variable_list (c_parser *parser,
11509 location_t clause_loc,
11510 enum omp_clause_code kind, tree list)
11512 if (c_parser_next_token_is_not (parser, CPP_NAME)
11513 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11514 c_parser_error (parser, "expected identifier");
11516 while (c_parser_next_token_is (parser, CPP_NAME)
11517 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11519 tree t = lookup_name (c_parser_peek_token (parser)->value);
11521 if (t == NULL_TREE)
11523 undeclared_variable (c_parser_peek_token (parser)->location,
11524 c_parser_peek_token (parser)->value);
11525 t = error_mark_node;
11528 c_parser_consume_token (parser);
11530 if (t == error_mark_node)
11532 else if (kind != 0)
11534 switch (kind)
11536 case OMP_CLAUSE__CACHE_:
11537 /* The OpenACC cache directive explicitly only allows "array
11538 elements or subarrays". */
11539 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11541 c_parser_error (parser, "expected %<[%>");
11542 t = error_mark_node;
11543 break;
11545 /* FALLTHROUGH */
11546 case OMP_CLAUSE_MAP:
11547 case OMP_CLAUSE_FROM:
11548 case OMP_CLAUSE_TO:
11549 while (c_parser_next_token_is (parser, CPP_DOT))
11551 location_t op_loc = c_parser_peek_token (parser)->location;
11552 c_parser_consume_token (parser);
11553 if (!c_parser_next_token_is (parser, CPP_NAME))
11555 c_parser_error (parser, "expected identifier");
11556 t = error_mark_node;
11557 break;
11560 c_token *comp_tok = c_parser_peek_token (parser);
11561 tree ident = comp_tok->value;
11562 location_t comp_loc = comp_tok->location;
11563 c_parser_consume_token (parser);
11564 t = build_component_ref (op_loc, t, ident, comp_loc);
11566 /* FALLTHROUGH */
11567 case OMP_CLAUSE_DEPEND:
11568 case OMP_CLAUSE_REDUCTION:
11569 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11571 tree low_bound = NULL_TREE, length = NULL_TREE;
11573 c_parser_consume_token (parser);
11574 if (!c_parser_next_token_is (parser, CPP_COLON))
11576 location_t expr_loc
11577 = c_parser_peek_token (parser)->location;
11578 c_expr expr = c_parser_expression (parser);
11579 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11580 false, true);
11581 low_bound = expr.value;
11583 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11584 length = integer_one_node;
11585 else
11587 /* Look for `:'. */
11588 if (!c_parser_require (parser, CPP_COLON,
11589 "expected %<:%>"))
11591 t = error_mark_node;
11592 break;
11594 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11596 location_t expr_loc
11597 = c_parser_peek_token (parser)->location;
11598 c_expr expr = c_parser_expression (parser);
11599 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11600 false, true);
11601 length = expr.value;
11604 /* Look for the closing `]'. */
11605 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11606 "expected %<]%>"))
11608 t = error_mark_node;
11609 break;
11612 t = tree_cons (low_bound, length, t);
11614 break;
11615 default:
11616 break;
11619 if (t != error_mark_node)
11621 tree u = build_omp_clause (clause_loc, kind);
11622 OMP_CLAUSE_DECL (u) = t;
11623 OMP_CLAUSE_CHAIN (u) = list;
11624 list = u;
11627 else
11628 list = tree_cons (t, NULL_TREE, list);
11630 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11631 break;
11633 c_parser_consume_token (parser);
11636 return list;
11639 /* Similarly, but expect leading and trailing parenthesis. This is a very
11640 common case for OpenACC and OpenMP clauses. */
11642 static tree
11643 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11644 tree list)
11646 /* The clauses location. */
11647 location_t loc = c_parser_peek_token (parser)->location;
11649 matching_parens parens;
11650 if (parens.require_open (parser))
11652 list = c_parser_omp_variable_list (parser, loc, kind, list);
11653 parens.skip_until_found_close (parser);
11655 return list;
11658 /* OpenACC 2.0:
11659 copy ( variable-list )
11660 copyin ( variable-list )
11661 copyout ( variable-list )
11662 create ( variable-list )
11663 delete ( variable-list )
11664 present ( variable-list ) */
11666 static tree
11667 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11668 tree list)
11670 enum gomp_map_kind kind;
11671 switch (c_kind)
11673 case PRAGMA_OACC_CLAUSE_COPY:
11674 kind = GOMP_MAP_TOFROM;
11675 break;
11676 case PRAGMA_OACC_CLAUSE_COPYIN:
11677 kind = GOMP_MAP_TO;
11678 break;
11679 case PRAGMA_OACC_CLAUSE_COPYOUT:
11680 kind = GOMP_MAP_FROM;
11681 break;
11682 case PRAGMA_OACC_CLAUSE_CREATE:
11683 kind = GOMP_MAP_ALLOC;
11684 break;
11685 case PRAGMA_OACC_CLAUSE_DELETE:
11686 kind = GOMP_MAP_RELEASE;
11687 break;
11688 case PRAGMA_OACC_CLAUSE_DEVICE:
11689 kind = GOMP_MAP_FORCE_TO;
11690 break;
11691 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11692 kind = GOMP_MAP_DEVICE_RESIDENT;
11693 break;
11694 case PRAGMA_OACC_CLAUSE_HOST:
11695 kind = GOMP_MAP_FORCE_FROM;
11696 break;
11697 case PRAGMA_OACC_CLAUSE_LINK:
11698 kind = GOMP_MAP_LINK;
11699 break;
11700 case PRAGMA_OACC_CLAUSE_PRESENT:
11701 kind = GOMP_MAP_FORCE_PRESENT;
11702 break;
11703 default:
11704 gcc_unreachable ();
11706 tree nl, c;
11707 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11709 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11710 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11712 return nl;
11715 /* OpenACC 2.0:
11716 deviceptr ( variable-list ) */
11718 static tree
11719 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11721 location_t loc = c_parser_peek_token (parser)->location;
11722 tree vars, t;
11724 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11725 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11726 variable-list must only allow for pointer variables. */
11727 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11728 for (t = vars; t && t; t = TREE_CHAIN (t))
11730 tree v = TREE_PURPOSE (t);
11732 /* FIXME diagnostics: Ideally we should keep individual
11733 locations for all the variables in the var list to make the
11734 following errors more precise. Perhaps
11735 c_parser_omp_var_list_parens() should construct a list of
11736 locations to go along with the var list. */
11738 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11739 error_at (loc, "%qD is not a variable", v);
11740 else if (TREE_TYPE (v) == error_mark_node)
11742 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11743 error_at (loc, "%qD is not a pointer variable", v);
11745 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11746 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11747 OMP_CLAUSE_DECL (u) = v;
11748 OMP_CLAUSE_CHAIN (u) = list;
11749 list = u;
11752 return list;
11755 /* OpenACC 2.0, OpenMP 3.0:
11756 collapse ( constant-expression ) */
11758 static tree
11759 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11761 tree c, num = error_mark_node;
11762 HOST_WIDE_INT n;
11763 location_t loc;
11765 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11766 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11768 loc = c_parser_peek_token (parser)->location;
11769 matching_parens parens;
11770 if (parens.require_open (parser))
11772 num = c_parser_expr_no_commas (parser, NULL).value;
11773 parens.skip_until_found_close (parser);
11775 if (num == error_mark_node)
11776 return list;
11777 mark_exp_read (num);
11778 num = c_fully_fold (num, false, NULL);
11779 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11780 || !tree_fits_shwi_p (num)
11781 || (n = tree_to_shwi (num)) <= 0
11782 || (int) n != n)
11784 error_at (loc,
11785 "collapse argument needs positive constant integer expression");
11786 return list;
11788 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11789 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11790 OMP_CLAUSE_CHAIN (c) = list;
11791 return c;
11794 /* OpenMP 2.5:
11795 copyin ( variable-list ) */
11797 static tree
11798 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11800 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11803 /* OpenMP 2.5:
11804 copyprivate ( variable-list ) */
11806 static tree
11807 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11809 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11812 /* OpenMP 2.5:
11813 default ( none | shared )
11815 OpenACC:
11816 default ( none | present ) */
11818 static tree
11819 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11821 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11822 location_t loc = c_parser_peek_token (parser)->location;
11823 tree c;
11825 matching_parens parens;
11826 if (!parens.require_open (parser))
11827 return list;
11828 if (c_parser_next_token_is (parser, CPP_NAME))
11830 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11832 switch (p[0])
11834 case 'n':
11835 if (strcmp ("none", p) != 0)
11836 goto invalid_kind;
11837 kind = OMP_CLAUSE_DEFAULT_NONE;
11838 break;
11840 case 'p':
11841 if (strcmp ("present", p) != 0 || !is_oacc)
11842 goto invalid_kind;
11843 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11844 break;
11846 case 's':
11847 if (strcmp ("shared", p) != 0 || is_oacc)
11848 goto invalid_kind;
11849 kind = OMP_CLAUSE_DEFAULT_SHARED;
11850 break;
11852 default:
11853 goto invalid_kind;
11856 c_parser_consume_token (parser);
11858 else
11860 invalid_kind:
11861 if (is_oacc)
11862 c_parser_error (parser, "expected %<none%> or %<present%>");
11863 else
11864 c_parser_error (parser, "expected %<none%> or %<shared%>");
11866 parens.skip_until_found_close (parser);
11868 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11869 return list;
11871 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11872 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11873 OMP_CLAUSE_CHAIN (c) = list;
11874 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11876 return c;
11879 /* OpenMP 2.5:
11880 firstprivate ( variable-list ) */
11882 static tree
11883 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11885 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11888 /* OpenMP 3.1:
11889 final ( expression ) */
11891 static tree
11892 c_parser_omp_clause_final (c_parser *parser, tree list)
11894 location_t loc = c_parser_peek_token (parser)->location;
11895 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11897 tree t = c_parser_paren_condition (parser);
11898 tree c;
11900 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11902 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11903 OMP_CLAUSE_FINAL_EXPR (c) = t;
11904 OMP_CLAUSE_CHAIN (c) = list;
11905 list = c;
11907 else
11908 c_parser_error (parser, "expected %<(%>");
11910 return list;
11913 /* OpenACC, OpenMP 2.5:
11914 if ( expression )
11916 OpenMP 4.5:
11917 if ( directive-name-modifier : expression )
11919 directive-name-modifier:
11920 parallel | task | taskloop | target data | target | target update
11921 | target enter data | target exit data */
11923 static tree
11924 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11926 location_t location = c_parser_peek_token (parser)->location;
11927 enum tree_code if_modifier = ERROR_MARK;
11929 matching_parens parens;
11930 if (!parens.require_open (parser))
11931 return list;
11933 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11935 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11936 int n = 2;
11937 if (strcmp (p, "parallel") == 0)
11938 if_modifier = OMP_PARALLEL;
11939 else if (strcmp (p, "task") == 0)
11940 if_modifier = OMP_TASK;
11941 else if (strcmp (p, "taskloop") == 0)
11942 if_modifier = OMP_TASKLOOP;
11943 else if (strcmp (p, "target") == 0)
11945 if_modifier = OMP_TARGET;
11946 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11948 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11949 if (strcmp ("data", p) == 0)
11950 if_modifier = OMP_TARGET_DATA;
11951 else if (strcmp ("update", p) == 0)
11952 if_modifier = OMP_TARGET_UPDATE;
11953 else if (strcmp ("enter", p) == 0)
11954 if_modifier = OMP_TARGET_ENTER_DATA;
11955 else if (strcmp ("exit", p) == 0)
11956 if_modifier = OMP_TARGET_EXIT_DATA;
11957 if (if_modifier != OMP_TARGET)
11959 n = 3;
11960 c_parser_consume_token (parser);
11962 else
11964 location_t loc = c_parser_peek_2nd_token (parser)->location;
11965 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11966 "or %<exit%>");
11967 if_modifier = ERROR_MARK;
11969 if (if_modifier == OMP_TARGET_ENTER_DATA
11970 || if_modifier == OMP_TARGET_EXIT_DATA)
11972 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11974 p = IDENTIFIER_POINTER
11975 (c_parser_peek_2nd_token (parser)->value);
11976 if (strcmp ("data", p) == 0)
11977 n = 4;
11979 if (n == 4)
11980 c_parser_consume_token (parser);
11981 else
11983 location_t loc
11984 = c_parser_peek_2nd_token (parser)->location;
11985 error_at (loc, "expected %<data%>");
11986 if_modifier = ERROR_MARK;
11991 if (if_modifier != ERROR_MARK)
11993 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11995 c_parser_consume_token (parser);
11996 c_parser_consume_token (parser);
11998 else
12000 if (n > 2)
12002 location_t loc = c_parser_peek_2nd_token (parser)->location;
12003 error_at (loc, "expected %<:%>");
12005 if_modifier = ERROR_MARK;
12010 tree t = c_parser_condition (parser), c;
12011 parens.skip_until_found_close (parser);
12013 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12014 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12016 if (if_modifier != ERROR_MARK
12017 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12019 const char *p = NULL;
12020 switch (if_modifier)
12022 case OMP_PARALLEL: p = "parallel"; break;
12023 case OMP_TASK: p = "task"; break;
12024 case OMP_TASKLOOP: p = "taskloop"; break;
12025 case OMP_TARGET_DATA: p = "target data"; break;
12026 case OMP_TARGET: p = "target"; break;
12027 case OMP_TARGET_UPDATE: p = "target update"; break;
12028 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12029 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12030 default: gcc_unreachable ();
12032 error_at (location, "too many %<if%> clauses with %qs modifier",
12034 return list;
12036 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12038 if (!is_omp)
12039 error_at (location, "too many %<if%> clauses");
12040 else
12041 error_at (location, "too many %<if%> clauses without modifier");
12042 return list;
12044 else if (if_modifier == ERROR_MARK
12045 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12047 error_at (location, "if any %<if%> clause has modifier, then all "
12048 "%<if%> clauses have to use modifier");
12049 return list;
12053 c = build_omp_clause (location, OMP_CLAUSE_IF);
12054 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12055 OMP_CLAUSE_IF_EXPR (c) = t;
12056 OMP_CLAUSE_CHAIN (c) = list;
12057 return c;
12060 /* OpenMP 2.5:
12061 lastprivate ( variable-list ) */
12063 static tree
12064 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12066 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12069 /* OpenMP 3.1:
12070 mergeable */
12072 static tree
12073 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12075 tree c;
12077 /* FIXME: Should we allow duplicates? */
12078 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12080 c = build_omp_clause (c_parser_peek_token (parser)->location,
12081 OMP_CLAUSE_MERGEABLE);
12082 OMP_CLAUSE_CHAIN (c) = list;
12084 return c;
12087 /* OpenMP 2.5:
12088 nowait */
12090 static tree
12091 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12093 tree c;
12094 location_t loc = c_parser_peek_token (parser)->location;
12096 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12098 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12099 OMP_CLAUSE_CHAIN (c) = list;
12100 return c;
12103 /* OpenMP 2.5:
12104 num_threads ( expression ) */
12106 static tree
12107 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12109 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12110 matching_parens parens;
12111 if (parens.require_open (parser))
12113 location_t expr_loc = c_parser_peek_token (parser)->location;
12114 c_expr expr = c_parser_expression (parser);
12115 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12116 tree c, t = expr.value;
12117 t = c_fully_fold (t, false, NULL);
12119 parens.skip_until_found_close (parser);
12121 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12123 c_parser_error (parser, "expected integer expression");
12124 return list;
12127 /* Attempt to statically determine when the number isn't positive. */
12128 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12129 build_int_cst (TREE_TYPE (t), 0));
12130 protected_set_expr_location (c, expr_loc);
12131 if (c == boolean_true_node)
12133 warning_at (expr_loc, 0,
12134 "%<num_threads%> value must be positive");
12135 t = integer_one_node;
12138 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12140 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12141 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12142 OMP_CLAUSE_CHAIN (c) = list;
12143 list = c;
12146 return list;
12149 /* OpenMP 4.5:
12150 num_tasks ( expression ) */
12152 static tree
12153 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12155 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12156 matching_parens parens;
12157 if (parens.require_open (parser))
12159 location_t expr_loc = c_parser_peek_token (parser)->location;
12160 c_expr expr = c_parser_expression (parser);
12161 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12162 tree c, t = expr.value;
12163 t = c_fully_fold (t, false, NULL);
12165 parens.skip_until_found_close (parser);
12167 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12169 c_parser_error (parser, "expected integer expression");
12170 return list;
12173 /* Attempt to statically determine when the number isn't positive. */
12174 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12175 build_int_cst (TREE_TYPE (t), 0));
12176 if (CAN_HAVE_LOCATION_P (c))
12177 SET_EXPR_LOCATION (c, expr_loc);
12178 if (c == boolean_true_node)
12180 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12181 t = integer_one_node;
12184 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12186 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12187 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12188 OMP_CLAUSE_CHAIN (c) = list;
12189 list = c;
12192 return list;
12195 /* OpenMP 4.5:
12196 grainsize ( expression ) */
12198 static tree
12199 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12201 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12202 matching_parens parens;
12203 if (parens.require_open (parser))
12205 location_t expr_loc = c_parser_peek_token (parser)->location;
12206 c_expr expr = c_parser_expression (parser);
12207 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12208 tree c, t = expr.value;
12209 t = c_fully_fold (t, false, NULL);
12211 parens.skip_until_found_close (parser);
12213 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12215 c_parser_error (parser, "expected integer expression");
12216 return list;
12219 /* Attempt to statically determine when the number isn't positive. */
12220 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12221 build_int_cst (TREE_TYPE (t), 0));
12222 if (CAN_HAVE_LOCATION_P (c))
12223 SET_EXPR_LOCATION (c, expr_loc);
12224 if (c == boolean_true_node)
12226 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12227 t = integer_one_node;
12230 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12232 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12233 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12234 OMP_CLAUSE_CHAIN (c) = list;
12235 list = c;
12238 return list;
12241 /* OpenMP 4.5:
12242 priority ( expression ) */
12244 static tree
12245 c_parser_omp_clause_priority (c_parser *parser, tree list)
12247 location_t priority_loc = c_parser_peek_token (parser)->location;
12248 matching_parens parens;
12249 if (parens.require_open (parser))
12251 location_t expr_loc = c_parser_peek_token (parser)->location;
12252 c_expr expr = c_parser_expression (parser);
12253 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12254 tree c, t = expr.value;
12255 t = c_fully_fold (t, false, NULL);
12257 parens.skip_until_found_close (parser);
12259 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12261 c_parser_error (parser, "expected integer expression");
12262 return list;
12265 /* Attempt to statically determine when the number isn't
12266 non-negative. */
12267 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12268 build_int_cst (TREE_TYPE (t), 0));
12269 if (CAN_HAVE_LOCATION_P (c))
12270 SET_EXPR_LOCATION (c, expr_loc);
12271 if (c == boolean_true_node)
12273 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12274 t = integer_one_node;
12277 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12279 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12280 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12281 OMP_CLAUSE_CHAIN (c) = list;
12282 list = c;
12285 return list;
12288 /* OpenMP 4.5:
12289 hint ( expression ) */
12291 static tree
12292 c_parser_omp_clause_hint (c_parser *parser, tree list)
12294 location_t hint_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 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12314 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12315 OMP_CLAUSE_HINT_EXPR (c) = t;
12316 OMP_CLAUSE_CHAIN (c) = list;
12317 list = c;
12320 return list;
12323 /* OpenMP 4.5:
12324 defaultmap ( tofrom : scalar ) */
12326 static tree
12327 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12329 location_t loc = c_parser_peek_token (parser)->location;
12330 tree c;
12331 const char *p;
12333 matching_parens parens;
12334 if (!parens.require_open (parser))
12335 return list;
12336 if (!c_parser_next_token_is (parser, CPP_NAME))
12338 c_parser_error (parser, "expected %<tofrom%>");
12339 goto out_err;
12341 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12342 if (strcmp (p, "tofrom") != 0)
12344 c_parser_error (parser, "expected %<tofrom%>");
12345 goto out_err;
12347 c_parser_consume_token (parser);
12348 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12349 goto out_err;
12350 if (!c_parser_next_token_is (parser, CPP_NAME))
12352 c_parser_error (parser, "expected %<scalar%>");
12353 goto out_err;
12355 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12356 if (strcmp (p, "scalar") != 0)
12358 c_parser_error (parser, "expected %<scalar%>");
12359 goto out_err;
12361 c_parser_consume_token (parser);
12362 parens.skip_until_found_close (parser);
12363 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12364 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12365 OMP_CLAUSE_CHAIN (c) = list;
12366 return c;
12368 out_err:
12369 parens.skip_until_found_close (parser);
12370 return list;
12373 /* OpenACC 2.0:
12374 use_device ( variable-list )
12376 OpenMP 4.5:
12377 use_device_ptr ( variable-list ) */
12379 static tree
12380 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12382 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12383 list);
12386 /* OpenMP 4.5:
12387 is_device_ptr ( variable-list ) */
12389 static tree
12390 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12392 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12395 /* OpenACC:
12396 num_gangs ( expression )
12397 num_workers ( expression )
12398 vector_length ( expression ) */
12400 static tree
12401 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12402 tree list)
12404 location_t loc = c_parser_peek_token (parser)->location;
12406 matching_parens parens;
12407 if (!parens.require_open (parser))
12408 return list;
12410 location_t expr_loc = c_parser_peek_token (parser)->location;
12411 c_expr expr = c_parser_expression (parser);
12412 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12413 tree c, t = expr.value;
12414 t = c_fully_fold (t, false, NULL);
12416 parens.skip_until_found_close (parser);
12418 if (t == error_mark_node)
12419 return list;
12420 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12422 error_at (expr_loc, "%qs expression must be integral",
12423 omp_clause_code_name[code]);
12424 return list;
12427 /* Attempt to statically determine when the number isn't positive. */
12428 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12429 build_int_cst (TREE_TYPE (t), 0));
12430 protected_set_expr_location (c, expr_loc);
12431 if (c == boolean_true_node)
12433 warning_at (expr_loc, 0,
12434 "%qs value must be positive",
12435 omp_clause_code_name[code]);
12436 t = integer_one_node;
12439 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12441 c = build_omp_clause (loc, code);
12442 OMP_CLAUSE_OPERAND (c, 0) = t;
12443 OMP_CLAUSE_CHAIN (c) = list;
12444 return c;
12447 /* OpenACC:
12449 gang [( gang-arg-list )]
12450 worker [( [num:] int-expr )]
12451 vector [( [length:] int-expr )]
12453 where gang-arg is one of:
12455 [num:] int-expr
12456 static: size-expr
12458 and size-expr may be:
12461 int-expr
12464 static tree
12465 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12466 const char *str, tree list)
12468 const char *id = "num";
12469 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12470 location_t loc = c_parser_peek_token (parser)->location;
12472 if (kind == OMP_CLAUSE_VECTOR)
12473 id = "length";
12475 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12477 c_parser_consume_token (parser);
12481 c_token *next = c_parser_peek_token (parser);
12482 int idx = 0;
12484 /* Gang static argument. */
12485 if (kind == OMP_CLAUSE_GANG
12486 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12488 c_parser_consume_token (parser);
12490 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12491 goto cleanup_error;
12493 idx = 1;
12494 if (ops[idx] != NULL_TREE)
12496 c_parser_error (parser, "too many %<static%> arguments");
12497 goto cleanup_error;
12500 /* Check for the '*' argument. */
12501 if (c_parser_next_token_is (parser, CPP_MULT)
12502 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12503 || c_parser_peek_2nd_token (parser)->type
12504 == CPP_CLOSE_PAREN))
12506 c_parser_consume_token (parser);
12507 ops[idx] = integer_minus_one_node;
12509 if (c_parser_next_token_is (parser, CPP_COMMA))
12511 c_parser_consume_token (parser);
12512 continue;
12514 else
12515 break;
12518 /* Worker num: argument and vector length: arguments. */
12519 else if (c_parser_next_token_is (parser, CPP_NAME)
12520 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12521 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12523 c_parser_consume_token (parser); /* id */
12524 c_parser_consume_token (parser); /* ':' */
12527 /* Now collect the actual argument. */
12528 if (ops[idx] != NULL_TREE)
12530 c_parser_error (parser, "unexpected argument");
12531 goto cleanup_error;
12534 location_t expr_loc = c_parser_peek_token (parser)->location;
12535 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12536 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12537 tree expr = cexpr.value;
12538 if (expr == error_mark_node)
12539 goto cleanup_error;
12541 expr = c_fully_fold (expr, false, NULL);
12543 /* Attempt to statically determine when the number isn't a
12544 positive integer. */
12546 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12548 c_parser_error (parser, "expected integer expression");
12549 return list;
12552 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12553 build_int_cst (TREE_TYPE (expr), 0));
12554 if (c == boolean_true_node)
12556 warning_at (loc, 0,
12557 "%qs value must be positive", str);
12558 expr = integer_one_node;
12561 ops[idx] = expr;
12563 if (kind == OMP_CLAUSE_GANG
12564 && c_parser_next_token_is (parser, CPP_COMMA))
12566 c_parser_consume_token (parser);
12567 continue;
12569 break;
12571 while (1);
12573 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12574 goto cleanup_error;
12577 check_no_duplicate_clause (list, kind, str);
12579 c = build_omp_clause (loc, kind);
12581 if (ops[1])
12582 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12584 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12585 OMP_CLAUSE_CHAIN (c) = list;
12587 return c;
12589 cleanup_error:
12590 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12591 return list;
12594 /* OpenACC 2.5:
12595 auto
12596 finalize
12597 independent
12598 nohost
12599 seq */
12601 static tree
12602 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12603 tree list)
12605 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12607 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12608 OMP_CLAUSE_CHAIN (c) = list;
12610 return c;
12613 /* OpenACC:
12614 async [( int-expr )] */
12616 static tree
12617 c_parser_oacc_clause_async (c_parser *parser, tree list)
12619 tree c, t;
12620 location_t loc = c_parser_peek_token (parser)->location;
12622 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12624 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12626 c_parser_consume_token (parser);
12628 t = c_parser_expression (parser).value;
12629 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12630 c_parser_error (parser, "expected integer expression");
12631 else if (t == error_mark_node
12632 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12633 return list;
12635 else
12636 t = c_fully_fold (t, false, NULL);
12638 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12640 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12641 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12642 OMP_CLAUSE_CHAIN (c) = list;
12643 list = c;
12645 return list;
12648 /* OpenACC 2.0:
12649 tile ( size-expr-list ) */
12651 static tree
12652 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12654 tree c, expr = error_mark_node;
12655 location_t loc;
12656 tree tile = NULL_TREE;
12658 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12659 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12661 loc = c_parser_peek_token (parser)->location;
12662 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12663 return list;
12667 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12668 return list;
12670 if (c_parser_next_token_is (parser, CPP_MULT)
12671 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12672 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12674 c_parser_consume_token (parser);
12675 expr = integer_zero_node;
12677 else
12679 location_t expr_loc = c_parser_peek_token (parser)->location;
12680 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12681 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12682 expr = cexpr.value;
12684 if (expr == error_mark_node)
12686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12687 "expected %<)%>");
12688 return list;
12691 expr = c_fully_fold (expr, false, NULL);
12693 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12694 || !tree_fits_shwi_p (expr)
12695 || tree_to_shwi (expr) <= 0)
12697 error_at (expr_loc, "%<tile%> argument needs positive"
12698 " integral constant");
12699 expr = integer_zero_node;
12703 tile = tree_cons (NULL_TREE, expr, tile);
12705 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12707 /* Consume the trailing ')'. */
12708 c_parser_consume_token (parser);
12710 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12711 tile = nreverse (tile);
12712 OMP_CLAUSE_TILE_LIST (c) = tile;
12713 OMP_CLAUSE_CHAIN (c) = list;
12714 return c;
12717 /* OpenACC:
12718 wait ( int-expr-list ) */
12720 static tree
12721 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12723 location_t clause_loc = c_parser_peek_token (parser)->location;
12725 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12726 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12728 return list;
12731 /* OpenMP 2.5:
12732 ordered
12734 OpenMP 4.5:
12735 ordered ( constant-expression ) */
12737 static tree
12738 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12740 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12742 tree c, num = NULL_TREE;
12743 HOST_WIDE_INT n;
12744 location_t loc = c_parser_peek_token (parser)->location;
12745 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12747 matching_parens parens;
12748 parens.consume_open (parser);
12749 num = c_parser_expr_no_commas (parser, NULL).value;
12750 parens.skip_until_found_close (parser);
12752 if (num == error_mark_node)
12753 return list;
12754 if (num)
12756 mark_exp_read (num);
12757 num = c_fully_fold (num, false, NULL);
12758 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12759 || !tree_fits_shwi_p (num)
12760 || (n = tree_to_shwi (num)) <= 0
12761 || (int) n != n)
12763 error_at (loc, "ordered argument needs positive "
12764 "constant integer expression");
12765 return list;
12768 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12769 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12770 OMP_CLAUSE_CHAIN (c) = list;
12771 return c;
12774 /* OpenMP 2.5:
12775 private ( variable-list ) */
12777 static tree
12778 c_parser_omp_clause_private (c_parser *parser, tree list)
12780 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12783 /* OpenMP 2.5:
12784 reduction ( reduction-operator : variable-list )
12786 reduction-operator:
12787 One of: + * - & ^ | && ||
12789 OpenMP 3.1:
12791 reduction-operator:
12792 One of: + * - & ^ | && || max min
12794 OpenMP 4.0:
12796 reduction-operator:
12797 One of: + * - & ^ | && ||
12798 identifier */
12800 static tree
12801 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12803 location_t clause_loc = c_parser_peek_token (parser)->location;
12804 matching_parens parens;
12805 if (parens.require_open (parser))
12807 enum tree_code code = ERROR_MARK;
12808 tree reduc_id = NULL_TREE;
12810 switch (c_parser_peek_token (parser)->type)
12812 case CPP_PLUS:
12813 code = PLUS_EXPR;
12814 break;
12815 case CPP_MULT:
12816 code = MULT_EXPR;
12817 break;
12818 case CPP_MINUS:
12819 code = MINUS_EXPR;
12820 break;
12821 case CPP_AND:
12822 code = BIT_AND_EXPR;
12823 break;
12824 case CPP_XOR:
12825 code = BIT_XOR_EXPR;
12826 break;
12827 case CPP_OR:
12828 code = BIT_IOR_EXPR;
12829 break;
12830 case CPP_AND_AND:
12831 code = TRUTH_ANDIF_EXPR;
12832 break;
12833 case CPP_OR_OR:
12834 code = TRUTH_ORIF_EXPR;
12835 break;
12836 case CPP_NAME:
12838 const char *p
12839 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12840 if (strcmp (p, "min") == 0)
12842 code = MIN_EXPR;
12843 break;
12845 if (strcmp (p, "max") == 0)
12847 code = MAX_EXPR;
12848 break;
12850 reduc_id = c_parser_peek_token (parser)->value;
12851 break;
12853 default:
12854 c_parser_error (parser,
12855 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12856 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12857 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12858 return list;
12860 c_parser_consume_token (parser);
12861 reduc_id = c_omp_reduction_id (code, reduc_id);
12862 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12864 tree nl, c;
12866 nl = c_parser_omp_variable_list (parser, clause_loc,
12867 OMP_CLAUSE_REDUCTION, list);
12868 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12870 tree d = OMP_CLAUSE_DECL (c), type;
12871 if (TREE_CODE (d) != TREE_LIST)
12872 type = TREE_TYPE (d);
12873 else
12875 int cnt = 0;
12876 tree t;
12877 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12878 cnt++;
12879 type = TREE_TYPE (t);
12880 while (cnt > 0)
12882 if (TREE_CODE (type) != POINTER_TYPE
12883 && TREE_CODE (type) != ARRAY_TYPE)
12884 break;
12885 type = TREE_TYPE (type);
12886 cnt--;
12889 while (TREE_CODE (type) == ARRAY_TYPE)
12890 type = TREE_TYPE (type);
12891 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12892 if (code == ERROR_MARK
12893 || !(INTEGRAL_TYPE_P (type)
12894 || TREE_CODE (type) == REAL_TYPE
12895 || TREE_CODE (type) == COMPLEX_TYPE))
12896 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12897 = c_omp_reduction_lookup (reduc_id,
12898 TYPE_MAIN_VARIANT (type));
12901 list = nl;
12903 parens.skip_until_found_close (parser);
12905 return list;
12908 /* OpenMP 2.5:
12909 schedule ( schedule-kind )
12910 schedule ( schedule-kind , expression )
12912 schedule-kind:
12913 static | dynamic | guided | runtime | auto
12915 OpenMP 4.5:
12916 schedule ( schedule-modifier : schedule-kind )
12917 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12919 schedule-modifier:
12920 simd
12921 monotonic
12922 nonmonotonic */
12924 static tree
12925 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12927 tree c, t;
12928 location_t loc = c_parser_peek_token (parser)->location;
12929 int modifiers = 0, nmodifiers = 0;
12931 matching_parens parens;
12932 if (!parens.require_open (parser))
12933 return list;
12935 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12937 while (c_parser_next_token_is (parser, CPP_NAME))
12939 tree kind = c_parser_peek_token (parser)->value;
12940 const char *p = IDENTIFIER_POINTER (kind);
12941 if (strcmp ("simd", p) == 0)
12942 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12943 else if (strcmp ("monotonic", p) == 0)
12944 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12945 else if (strcmp ("nonmonotonic", p) == 0)
12946 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12947 else
12948 break;
12949 c_parser_consume_token (parser);
12950 if (nmodifiers++ == 0
12951 && c_parser_next_token_is (parser, CPP_COMMA))
12952 c_parser_consume_token (parser);
12953 else
12955 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12956 break;
12960 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12961 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12962 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12963 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12965 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12966 "specified");
12967 modifiers = 0;
12970 if (c_parser_next_token_is (parser, CPP_NAME))
12972 tree kind = c_parser_peek_token (parser)->value;
12973 const char *p = IDENTIFIER_POINTER (kind);
12975 switch (p[0])
12977 case 'd':
12978 if (strcmp ("dynamic", p) != 0)
12979 goto invalid_kind;
12980 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12981 break;
12983 case 'g':
12984 if (strcmp ("guided", p) != 0)
12985 goto invalid_kind;
12986 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12987 break;
12989 case 'r':
12990 if (strcmp ("runtime", p) != 0)
12991 goto invalid_kind;
12992 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12993 break;
12995 default:
12996 goto invalid_kind;
12999 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
13000 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
13001 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
13002 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
13003 else
13004 goto invalid_kind;
13006 c_parser_consume_token (parser);
13007 if (c_parser_next_token_is (parser, CPP_COMMA))
13009 location_t here;
13010 c_parser_consume_token (parser);
13012 here = c_parser_peek_token (parser)->location;
13013 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13014 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13015 t = expr.value;
13016 t = c_fully_fold (t, false, NULL);
13018 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13019 error_at (here, "schedule %<runtime%> does not take "
13020 "a %<chunk_size%> parameter");
13021 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13022 error_at (here,
13023 "schedule %<auto%> does not take "
13024 "a %<chunk_size%> parameter");
13025 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13027 /* Attempt to statically determine when the number isn't
13028 positive. */
13029 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13030 build_int_cst (TREE_TYPE (t), 0));
13031 protected_set_expr_location (s, loc);
13032 if (s == boolean_true_node)
13034 warning_at (loc, 0,
13035 "chunk size value must be positive");
13036 t = integer_one_node;
13038 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13040 else
13041 c_parser_error (parser, "expected integer expression");
13043 parens.skip_until_found_close (parser);
13045 else
13046 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13047 "expected %<,%> or %<)%>");
13049 OMP_CLAUSE_SCHEDULE_KIND (c)
13050 = (enum omp_clause_schedule_kind)
13051 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13053 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13054 OMP_CLAUSE_CHAIN (c) = list;
13055 return c;
13057 invalid_kind:
13058 c_parser_error (parser, "invalid schedule kind");
13059 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13060 return list;
13063 /* OpenMP 2.5:
13064 shared ( variable-list ) */
13066 static tree
13067 c_parser_omp_clause_shared (c_parser *parser, tree list)
13069 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13072 /* OpenMP 3.0:
13073 untied */
13075 static tree
13076 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13078 tree c;
13080 /* FIXME: Should we allow duplicates? */
13081 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13083 c = build_omp_clause (c_parser_peek_token (parser)->location,
13084 OMP_CLAUSE_UNTIED);
13085 OMP_CLAUSE_CHAIN (c) = list;
13087 return c;
13090 /* OpenMP 4.0:
13091 inbranch
13092 notinbranch */
13094 static tree
13095 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13096 enum omp_clause_code code, tree list)
13098 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13100 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13101 OMP_CLAUSE_CHAIN (c) = list;
13103 return c;
13106 /* OpenMP 4.0:
13107 parallel
13109 sections
13110 taskgroup */
13112 static tree
13113 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13114 enum omp_clause_code code, tree list)
13116 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13117 OMP_CLAUSE_CHAIN (c) = list;
13119 return c;
13122 /* OpenMP 4.5:
13123 nogroup */
13125 static tree
13126 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13128 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13129 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13130 OMP_CLAUSE_NOGROUP);
13131 OMP_CLAUSE_CHAIN (c) = list;
13132 return c;
13135 /* OpenMP 4.5:
13136 simd
13137 threads */
13139 static tree
13140 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13141 enum omp_clause_code code, tree list)
13143 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13144 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13145 OMP_CLAUSE_CHAIN (c) = list;
13146 return c;
13149 /* OpenMP 4.0:
13150 num_teams ( expression ) */
13152 static tree
13153 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13155 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13156 matching_parens parens;
13157 if (parens.require_open (parser))
13159 location_t expr_loc = c_parser_peek_token (parser)->location;
13160 c_expr expr = c_parser_expression (parser);
13161 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13162 tree c, t = expr.value;
13163 t = c_fully_fold (t, false, NULL);
13165 parens.skip_until_found_close (parser);
13167 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13169 c_parser_error (parser, "expected integer expression");
13170 return list;
13173 /* Attempt to statically determine when the number isn't positive. */
13174 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13175 build_int_cst (TREE_TYPE (t), 0));
13176 protected_set_expr_location (c, expr_loc);
13177 if (c == boolean_true_node)
13179 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13180 t = integer_one_node;
13183 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13185 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13186 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13187 OMP_CLAUSE_CHAIN (c) = list;
13188 list = c;
13191 return list;
13194 /* OpenMP 4.0:
13195 thread_limit ( expression ) */
13197 static tree
13198 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13200 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13201 matching_parens parens;
13202 if (parens.require_open (parser))
13204 location_t expr_loc = c_parser_peek_token (parser)->location;
13205 c_expr expr = c_parser_expression (parser);
13206 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13207 tree c, t = expr.value;
13208 t = c_fully_fold (t, false, NULL);
13210 parens.skip_until_found_close (parser);
13212 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13214 c_parser_error (parser, "expected integer expression");
13215 return list;
13218 /* Attempt to statically determine when the number isn't positive. */
13219 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13220 build_int_cst (TREE_TYPE (t), 0));
13221 protected_set_expr_location (c, expr_loc);
13222 if (c == boolean_true_node)
13224 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13225 t = integer_one_node;
13228 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13229 "thread_limit");
13231 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13232 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13233 OMP_CLAUSE_CHAIN (c) = list;
13234 list = c;
13237 return list;
13240 /* OpenMP 4.0:
13241 aligned ( variable-list )
13242 aligned ( variable-list : constant-expression ) */
13244 static tree
13245 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13247 location_t clause_loc = c_parser_peek_token (parser)->location;
13248 tree nl, c;
13250 matching_parens parens;
13251 if (!parens.require_open (parser))
13252 return list;
13254 nl = c_parser_omp_variable_list (parser, clause_loc,
13255 OMP_CLAUSE_ALIGNED, list);
13257 if (c_parser_next_token_is (parser, CPP_COLON))
13259 c_parser_consume_token (parser);
13260 location_t expr_loc = c_parser_peek_token (parser)->location;
13261 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13262 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13263 tree alignment = expr.value;
13264 alignment = c_fully_fold (alignment, false, NULL);
13265 if (TREE_CODE (alignment) != INTEGER_CST
13266 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13267 || tree_int_cst_sgn (alignment) != 1)
13269 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13270 "be positive constant integer expression");
13271 alignment = NULL_TREE;
13274 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13275 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13278 parens.skip_until_found_close (parser);
13279 return nl;
13282 /* OpenMP 4.0:
13283 linear ( variable-list )
13284 linear ( variable-list : expression )
13286 OpenMP 4.5:
13287 linear ( modifier ( variable-list ) )
13288 linear ( modifier ( variable-list ) : expression ) */
13290 static tree
13291 c_parser_omp_clause_linear (c_parser *parser, tree list)
13293 location_t clause_loc = c_parser_peek_token (parser)->location;
13294 tree nl, c, step;
13295 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13297 matching_parens parens;
13298 if (!parens.require_open (parser))
13299 return list;
13301 if (c_parser_next_token_is (parser, CPP_NAME))
13303 c_token *tok = c_parser_peek_token (parser);
13304 const char *p = IDENTIFIER_POINTER (tok->value);
13305 if (strcmp ("val", p) == 0)
13306 kind = OMP_CLAUSE_LINEAR_VAL;
13307 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13308 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13309 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13311 c_parser_consume_token (parser);
13312 c_parser_consume_token (parser);
13316 nl = c_parser_omp_variable_list (parser, clause_loc,
13317 OMP_CLAUSE_LINEAR, list);
13319 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13320 parens.skip_until_found_close (parser);
13322 if (c_parser_next_token_is (parser, CPP_COLON))
13324 c_parser_consume_token (parser);
13325 location_t expr_loc = c_parser_peek_token (parser)->location;
13326 c_expr expr = c_parser_expression (parser);
13327 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13328 step = expr.value;
13329 step = c_fully_fold (step, false, NULL);
13330 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13332 error_at (clause_loc, "%<linear%> clause step expression must "
13333 "be integral");
13334 step = integer_one_node;
13338 else
13339 step = integer_one_node;
13341 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13343 OMP_CLAUSE_LINEAR_STEP (c) = step;
13344 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13347 parens.skip_until_found_close (parser);
13348 return nl;
13351 /* OpenMP 4.0:
13352 safelen ( constant-expression ) */
13354 static tree
13355 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13357 location_t clause_loc = c_parser_peek_token (parser)->location;
13358 tree c, t;
13360 matching_parens parens;
13361 if (!parens.require_open (parser))
13362 return list;
13364 location_t expr_loc = c_parser_peek_token (parser)->location;
13365 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13366 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13367 t = expr.value;
13368 t = c_fully_fold (t, false, NULL);
13369 if (TREE_CODE (t) != INTEGER_CST
13370 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13371 || tree_int_cst_sgn (t) != 1)
13373 error_at (clause_loc, "%<safelen%> clause expression must "
13374 "be positive constant integer expression");
13375 t = NULL_TREE;
13378 parens.skip_until_found_close (parser);
13379 if (t == NULL_TREE || t == error_mark_node)
13380 return list;
13382 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13384 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13385 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13386 OMP_CLAUSE_CHAIN (c) = list;
13387 return c;
13390 /* OpenMP 4.0:
13391 simdlen ( constant-expression ) */
13393 static tree
13394 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13396 location_t clause_loc = c_parser_peek_token (parser)->location;
13397 tree c, t;
13399 matching_parens parens;
13400 if (!parens.require_open (parser))
13401 return list;
13403 location_t expr_loc = c_parser_peek_token (parser)->location;
13404 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13405 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13406 t = expr.value;
13407 t = c_fully_fold (t, false, NULL);
13408 if (TREE_CODE (t) != INTEGER_CST
13409 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13410 || tree_int_cst_sgn (t) != 1)
13412 error_at (clause_loc, "%<simdlen%> clause expression must "
13413 "be positive constant integer expression");
13414 t = NULL_TREE;
13417 parens.skip_until_found_close (parser);
13418 if (t == NULL_TREE || t == error_mark_node)
13419 return list;
13421 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13423 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13424 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13425 OMP_CLAUSE_CHAIN (c) = list;
13426 return c;
13429 /* OpenMP 4.5:
13430 vec:
13431 identifier [+/- integer]
13432 vec , identifier [+/- integer]
13435 static tree
13436 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13437 tree list)
13439 tree vec = NULL;
13440 if (c_parser_next_token_is_not (parser, CPP_NAME)
13441 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13443 c_parser_error (parser, "expected identifier");
13444 return list;
13447 while (c_parser_next_token_is (parser, CPP_NAME)
13448 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13450 tree t = lookup_name (c_parser_peek_token (parser)->value);
13451 tree addend = NULL;
13453 if (t == NULL_TREE)
13455 undeclared_variable (c_parser_peek_token (parser)->location,
13456 c_parser_peek_token (parser)->value);
13457 t = error_mark_node;
13460 c_parser_consume_token (parser);
13462 bool neg = false;
13463 if (c_parser_next_token_is (parser, CPP_MINUS))
13464 neg = true;
13465 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13467 addend = integer_zero_node;
13468 neg = false;
13469 goto add_to_vector;
13471 c_parser_consume_token (parser);
13473 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13475 c_parser_error (parser, "expected integer");
13476 return list;
13479 addend = c_parser_peek_token (parser)->value;
13480 if (TREE_CODE (addend) != INTEGER_CST)
13482 c_parser_error (parser, "expected integer");
13483 return list;
13485 c_parser_consume_token (parser);
13487 add_to_vector:
13488 if (t != error_mark_node)
13490 vec = tree_cons (addend, t, vec);
13491 if (neg)
13492 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13495 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13496 break;
13498 c_parser_consume_token (parser);
13501 if (vec == NULL_TREE)
13502 return list;
13504 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13505 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13506 OMP_CLAUSE_DECL (u) = nreverse (vec);
13507 OMP_CLAUSE_CHAIN (u) = list;
13508 return u;
13511 /* OpenMP 4.0:
13512 depend ( depend-kind: variable-list )
13514 depend-kind:
13515 in | out | inout
13517 OpenMP 4.5:
13518 depend ( source )
13520 depend ( sink : vec ) */
13522 static tree
13523 c_parser_omp_clause_depend (c_parser *parser, tree list)
13525 location_t clause_loc = c_parser_peek_token (parser)->location;
13526 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13527 tree nl, c;
13529 matching_parens parens;
13530 if (!parens.require_open (parser))
13531 return list;
13533 if (c_parser_next_token_is (parser, CPP_NAME))
13535 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13536 if (strcmp ("in", p) == 0)
13537 kind = OMP_CLAUSE_DEPEND_IN;
13538 else if (strcmp ("inout", p) == 0)
13539 kind = OMP_CLAUSE_DEPEND_INOUT;
13540 else if (strcmp ("out", p) == 0)
13541 kind = OMP_CLAUSE_DEPEND_OUT;
13542 else if (strcmp ("source", p) == 0)
13543 kind = OMP_CLAUSE_DEPEND_SOURCE;
13544 else if (strcmp ("sink", p) == 0)
13545 kind = OMP_CLAUSE_DEPEND_SINK;
13546 else
13547 goto invalid_kind;
13549 else
13550 goto invalid_kind;
13552 c_parser_consume_token (parser);
13554 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13556 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13557 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13558 OMP_CLAUSE_DECL (c) = NULL_TREE;
13559 OMP_CLAUSE_CHAIN (c) = list;
13560 parens.skip_until_found_close (parser);
13561 return c;
13564 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13565 goto resync_fail;
13567 if (kind == OMP_CLAUSE_DEPEND_SINK)
13568 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13569 else
13571 nl = c_parser_omp_variable_list (parser, clause_loc,
13572 OMP_CLAUSE_DEPEND, list);
13574 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13575 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13578 parens.skip_until_found_close (parser);
13579 return nl;
13581 invalid_kind:
13582 c_parser_error (parser, "invalid depend kind");
13583 resync_fail:
13584 parens.skip_until_found_close (parser);
13585 return list;
13588 /* OpenMP 4.0:
13589 map ( map-kind: variable-list )
13590 map ( variable-list )
13592 map-kind:
13593 alloc | to | from | tofrom
13595 OpenMP 4.5:
13596 map-kind:
13597 alloc | to | from | tofrom | release | delete
13599 map ( always [,] map-kind: variable-list ) */
13601 static tree
13602 c_parser_omp_clause_map (c_parser *parser, tree list)
13604 location_t clause_loc = c_parser_peek_token (parser)->location;
13605 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13606 int always = 0;
13607 enum c_id_kind always_id_kind = C_ID_NONE;
13608 location_t always_loc = UNKNOWN_LOCATION;
13609 tree always_id = NULL_TREE;
13610 tree nl, c;
13612 matching_parens parens;
13613 if (!parens.require_open (parser))
13614 return list;
13616 if (c_parser_next_token_is (parser, CPP_NAME))
13618 c_token *tok = c_parser_peek_token (parser);
13619 const char *p = IDENTIFIER_POINTER (tok->value);
13620 always_id_kind = tok->id_kind;
13621 always_loc = tok->location;
13622 always_id = tok->value;
13623 if (strcmp ("always", p) == 0)
13625 c_token *sectok = c_parser_peek_2nd_token (parser);
13626 if (sectok->type == CPP_COMMA)
13628 c_parser_consume_token (parser);
13629 c_parser_consume_token (parser);
13630 always = 2;
13632 else if (sectok->type == CPP_NAME)
13634 p = IDENTIFIER_POINTER (sectok->value);
13635 if (strcmp ("alloc", p) == 0
13636 || strcmp ("to", p) == 0
13637 || strcmp ("from", p) == 0
13638 || strcmp ("tofrom", p) == 0
13639 || strcmp ("release", p) == 0
13640 || strcmp ("delete", p) == 0)
13642 c_parser_consume_token (parser);
13643 always = 1;
13649 if (c_parser_next_token_is (parser, CPP_NAME)
13650 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13652 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13653 if (strcmp ("alloc", p) == 0)
13654 kind = GOMP_MAP_ALLOC;
13655 else if (strcmp ("to", p) == 0)
13656 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13657 else if (strcmp ("from", p) == 0)
13658 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13659 else if (strcmp ("tofrom", p) == 0)
13660 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13661 else if (strcmp ("release", p) == 0)
13662 kind = GOMP_MAP_RELEASE;
13663 else if (strcmp ("delete", p) == 0)
13664 kind = GOMP_MAP_DELETE;
13665 else
13667 c_parser_error (parser, "invalid map kind");
13668 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13669 "expected %<)%>");
13670 return list;
13672 c_parser_consume_token (parser);
13673 c_parser_consume_token (parser);
13675 else if (always)
13677 if (always_id_kind != C_ID_ID)
13679 c_parser_error (parser, "expected identifier");
13680 parens.skip_until_found_close (parser);
13681 return list;
13684 tree t = lookup_name (always_id);
13685 if (t == NULL_TREE)
13687 undeclared_variable (always_loc, always_id);
13688 t = error_mark_node;
13690 if (t != error_mark_node)
13692 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13693 OMP_CLAUSE_DECL (u) = t;
13694 OMP_CLAUSE_CHAIN (u) = list;
13695 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13696 list = u;
13698 if (always == 1)
13700 parens.skip_until_found_close (parser);
13701 return list;
13705 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13707 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13708 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13710 parens.skip_until_found_close (parser);
13711 return nl;
13714 /* OpenMP 4.0:
13715 device ( expression ) */
13717 static tree
13718 c_parser_omp_clause_device (c_parser *parser, tree list)
13720 location_t clause_loc = c_parser_peek_token (parser)->location;
13721 matching_parens parens;
13722 if (parens.require_open (parser))
13724 location_t expr_loc = c_parser_peek_token (parser)->location;
13725 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13726 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13727 tree c, t = expr.value;
13728 t = c_fully_fold (t, false, NULL);
13730 parens.skip_until_found_close (parser);
13732 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13734 c_parser_error (parser, "expected integer expression");
13735 return list;
13738 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13740 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13741 OMP_CLAUSE_DEVICE_ID (c) = t;
13742 OMP_CLAUSE_CHAIN (c) = list;
13743 list = c;
13746 return list;
13749 /* OpenMP 4.0:
13750 dist_schedule ( static )
13751 dist_schedule ( static , expression ) */
13753 static tree
13754 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13756 tree c, t = NULL_TREE;
13757 location_t loc = c_parser_peek_token (parser)->location;
13759 matching_parens parens;
13760 if (!parens.require_open (parser))
13761 return list;
13763 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13765 c_parser_error (parser, "invalid dist_schedule kind");
13766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13767 "expected %<)%>");
13768 return list;
13771 c_parser_consume_token (parser);
13772 if (c_parser_next_token_is (parser, CPP_COMMA))
13774 c_parser_consume_token (parser);
13776 location_t expr_loc = c_parser_peek_token (parser)->location;
13777 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13778 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13779 t = expr.value;
13780 t = c_fully_fold (t, false, NULL);
13781 parens.skip_until_found_close (parser);
13783 else
13784 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13785 "expected %<,%> or %<)%>");
13787 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13788 if (t == error_mark_node)
13789 return list;
13791 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13792 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13793 OMP_CLAUSE_CHAIN (c) = list;
13794 return c;
13797 /* OpenMP 4.0:
13798 proc_bind ( proc-bind-kind )
13800 proc-bind-kind:
13801 master | close | spread */
13803 static tree
13804 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13806 location_t clause_loc = c_parser_peek_token (parser)->location;
13807 enum omp_clause_proc_bind_kind kind;
13808 tree c;
13810 matching_parens parens;
13811 if (!parens.require_open (parser))
13812 return list;
13814 if (c_parser_next_token_is (parser, CPP_NAME))
13816 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13817 if (strcmp ("master", p) == 0)
13818 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13819 else if (strcmp ("close", p) == 0)
13820 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13821 else if (strcmp ("spread", p) == 0)
13822 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13823 else
13824 goto invalid_kind;
13826 else
13827 goto invalid_kind;
13829 c_parser_consume_token (parser);
13830 parens.skip_until_found_close (parser);
13831 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13832 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13833 OMP_CLAUSE_CHAIN (c) = list;
13834 return c;
13836 invalid_kind:
13837 c_parser_error (parser, "invalid proc_bind kind");
13838 parens.skip_until_found_close (parser);
13839 return list;
13842 /* OpenMP 4.0:
13843 to ( variable-list ) */
13845 static tree
13846 c_parser_omp_clause_to (c_parser *parser, tree list)
13848 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13851 /* OpenMP 4.0:
13852 from ( variable-list ) */
13854 static tree
13855 c_parser_omp_clause_from (c_parser *parser, tree list)
13857 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13860 /* OpenMP 4.0:
13861 uniform ( variable-list ) */
13863 static tree
13864 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13866 /* The clauses location. */
13867 location_t loc = c_parser_peek_token (parser)->location;
13869 matching_parens parens;
13870 if (parens.require_open (parser))
13872 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13873 list);
13874 parens.skip_until_found_close (parser);
13876 return list;
13879 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13880 is a bitmask in MASK. Return the list of clauses found. */
13882 static tree
13883 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13884 const char *where, bool finish_p = true)
13886 tree clauses = NULL;
13887 bool first = true;
13889 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13891 location_t here;
13892 pragma_omp_clause c_kind;
13893 const char *c_name;
13894 tree prev = clauses;
13896 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13897 c_parser_consume_token (parser);
13899 here = c_parser_peek_token (parser)->location;
13900 c_kind = c_parser_omp_clause_name (parser);
13902 switch (c_kind)
13904 case PRAGMA_OACC_CLAUSE_ASYNC:
13905 clauses = c_parser_oacc_clause_async (parser, clauses);
13906 c_name = "async";
13907 break;
13908 case PRAGMA_OACC_CLAUSE_AUTO:
13909 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13910 clauses);
13911 c_name = "auto";
13912 break;
13913 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13914 clauses = c_parser_omp_clause_collapse (parser, clauses);
13915 c_name = "collapse";
13916 break;
13917 case PRAGMA_OACC_CLAUSE_COPY:
13918 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13919 c_name = "copy";
13920 break;
13921 case PRAGMA_OACC_CLAUSE_COPYIN:
13922 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13923 c_name = "copyin";
13924 break;
13925 case PRAGMA_OACC_CLAUSE_COPYOUT:
13926 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13927 c_name = "copyout";
13928 break;
13929 case PRAGMA_OACC_CLAUSE_CREATE:
13930 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13931 c_name = "create";
13932 break;
13933 case PRAGMA_OACC_CLAUSE_DELETE:
13934 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13935 c_name = "delete";
13936 break;
13937 case PRAGMA_OMP_CLAUSE_DEFAULT:
13938 clauses = c_parser_omp_clause_default (parser, clauses, true);
13939 c_name = "default";
13940 break;
13941 case PRAGMA_OACC_CLAUSE_DEVICE:
13942 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13943 c_name = "device";
13944 break;
13945 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13946 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13947 c_name = "deviceptr";
13948 break;
13949 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13950 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13951 c_name = "device_resident";
13952 break;
13953 case PRAGMA_OACC_CLAUSE_FINALIZE:
13954 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
13955 clauses);
13956 c_name = "finalize";
13957 break;
13958 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13959 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13960 c_name = "firstprivate";
13961 break;
13962 case PRAGMA_OACC_CLAUSE_GANG:
13963 c_name = "gang";
13964 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13965 c_name, clauses);
13966 break;
13967 case PRAGMA_OACC_CLAUSE_HOST:
13968 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13969 c_name = "host";
13970 break;
13971 case PRAGMA_OACC_CLAUSE_IF:
13972 clauses = c_parser_omp_clause_if (parser, clauses, false);
13973 c_name = "if";
13974 break;
13975 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
13976 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_IF_PRESENT,
13977 clauses);
13978 c_name = "if_present";
13979 break;
13980 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13981 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13982 clauses);
13983 c_name = "independent";
13984 break;
13985 case PRAGMA_OACC_CLAUSE_LINK:
13986 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13987 c_name = "link";
13988 break;
13989 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13990 clauses = c_parser_oacc_single_int_clause (parser,
13991 OMP_CLAUSE_NUM_GANGS,
13992 clauses);
13993 c_name = "num_gangs";
13994 break;
13995 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13996 clauses = c_parser_oacc_single_int_clause (parser,
13997 OMP_CLAUSE_NUM_WORKERS,
13998 clauses);
13999 c_name = "num_workers";
14000 break;
14001 case PRAGMA_OACC_CLAUSE_PRESENT:
14002 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14003 c_name = "present";
14004 break;
14005 case PRAGMA_OACC_CLAUSE_PRIVATE:
14006 clauses = c_parser_omp_clause_private (parser, clauses);
14007 c_name = "private";
14008 break;
14009 case PRAGMA_OACC_CLAUSE_REDUCTION:
14010 clauses = c_parser_omp_clause_reduction (parser, clauses);
14011 c_name = "reduction";
14012 break;
14013 case PRAGMA_OACC_CLAUSE_SEQ:
14014 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14015 clauses);
14016 c_name = "seq";
14017 break;
14018 case PRAGMA_OACC_CLAUSE_TILE:
14019 clauses = c_parser_oacc_clause_tile (parser, clauses);
14020 c_name = "tile";
14021 break;
14022 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14023 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14024 c_name = "use_device";
14025 break;
14026 case PRAGMA_OACC_CLAUSE_VECTOR:
14027 c_name = "vector";
14028 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14029 c_name, clauses);
14030 break;
14031 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14032 clauses = c_parser_oacc_single_int_clause (parser,
14033 OMP_CLAUSE_VECTOR_LENGTH,
14034 clauses);
14035 c_name = "vector_length";
14036 break;
14037 case PRAGMA_OACC_CLAUSE_WAIT:
14038 clauses = c_parser_oacc_clause_wait (parser, clauses);
14039 c_name = "wait";
14040 break;
14041 case PRAGMA_OACC_CLAUSE_WORKER:
14042 c_name = "worker";
14043 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14044 c_name, clauses);
14045 break;
14046 default:
14047 c_parser_error (parser, "expected %<#pragma acc%> clause");
14048 goto saw_error;
14051 first = false;
14053 if (((mask >> c_kind) & 1) == 0)
14055 /* Remove the invalid clause(s) from the list to avoid
14056 confusing the rest of the compiler. */
14057 clauses = prev;
14058 error_at (here, "%qs is not valid for %qs", c_name, where);
14062 saw_error:
14063 c_parser_skip_to_pragma_eol (parser);
14065 if (finish_p)
14066 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14068 return clauses;
14071 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14072 is a bitmask in MASK. Return the list of clauses found. */
14074 static tree
14075 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14076 const char *where, bool finish_p = true)
14078 tree clauses = NULL;
14079 bool first = true;
14081 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14083 location_t here;
14084 pragma_omp_clause c_kind;
14085 const char *c_name;
14086 tree prev = clauses;
14088 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14089 c_parser_consume_token (parser);
14091 here = c_parser_peek_token (parser)->location;
14092 c_kind = c_parser_omp_clause_name (parser);
14094 switch (c_kind)
14096 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14097 clauses = c_parser_omp_clause_collapse (parser, clauses);
14098 c_name = "collapse";
14099 break;
14100 case PRAGMA_OMP_CLAUSE_COPYIN:
14101 clauses = c_parser_omp_clause_copyin (parser, clauses);
14102 c_name = "copyin";
14103 break;
14104 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14105 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14106 c_name = "copyprivate";
14107 break;
14108 case PRAGMA_OMP_CLAUSE_DEFAULT:
14109 clauses = c_parser_omp_clause_default (parser, clauses, false);
14110 c_name = "default";
14111 break;
14112 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14113 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14114 c_name = "firstprivate";
14115 break;
14116 case PRAGMA_OMP_CLAUSE_FINAL:
14117 clauses = c_parser_omp_clause_final (parser, clauses);
14118 c_name = "final";
14119 break;
14120 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14121 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14122 c_name = "grainsize";
14123 break;
14124 case PRAGMA_OMP_CLAUSE_HINT:
14125 clauses = c_parser_omp_clause_hint (parser, clauses);
14126 c_name = "hint";
14127 break;
14128 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14129 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14130 c_name = "defaultmap";
14131 break;
14132 case PRAGMA_OMP_CLAUSE_IF:
14133 clauses = c_parser_omp_clause_if (parser, clauses, true);
14134 c_name = "if";
14135 break;
14136 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14137 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14138 c_name = "lastprivate";
14139 break;
14140 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14141 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14142 c_name = "mergeable";
14143 break;
14144 case PRAGMA_OMP_CLAUSE_NOWAIT:
14145 clauses = c_parser_omp_clause_nowait (parser, clauses);
14146 c_name = "nowait";
14147 break;
14148 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14149 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14150 c_name = "num_tasks";
14151 break;
14152 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14153 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14154 c_name = "num_threads";
14155 break;
14156 case PRAGMA_OMP_CLAUSE_ORDERED:
14157 clauses = c_parser_omp_clause_ordered (parser, clauses);
14158 c_name = "ordered";
14159 break;
14160 case PRAGMA_OMP_CLAUSE_PRIORITY:
14161 clauses = c_parser_omp_clause_priority (parser, clauses);
14162 c_name = "priority";
14163 break;
14164 case PRAGMA_OMP_CLAUSE_PRIVATE:
14165 clauses = c_parser_omp_clause_private (parser, clauses);
14166 c_name = "private";
14167 break;
14168 case PRAGMA_OMP_CLAUSE_REDUCTION:
14169 clauses = c_parser_omp_clause_reduction (parser, clauses);
14170 c_name = "reduction";
14171 break;
14172 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14173 clauses = c_parser_omp_clause_schedule (parser, clauses);
14174 c_name = "schedule";
14175 break;
14176 case PRAGMA_OMP_CLAUSE_SHARED:
14177 clauses = c_parser_omp_clause_shared (parser, clauses);
14178 c_name = "shared";
14179 break;
14180 case PRAGMA_OMP_CLAUSE_UNTIED:
14181 clauses = c_parser_omp_clause_untied (parser, clauses);
14182 c_name = "untied";
14183 break;
14184 case PRAGMA_OMP_CLAUSE_INBRANCH:
14185 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14186 clauses);
14187 c_name = "inbranch";
14188 break;
14189 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14190 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14191 clauses);
14192 c_name = "notinbranch";
14193 break;
14194 case PRAGMA_OMP_CLAUSE_PARALLEL:
14195 clauses
14196 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14197 clauses);
14198 c_name = "parallel";
14199 if (!first)
14201 clause_not_first:
14202 error_at (here, "%qs must be the first clause of %qs",
14203 c_name, where);
14204 clauses = prev;
14206 break;
14207 case PRAGMA_OMP_CLAUSE_FOR:
14208 clauses
14209 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14210 clauses);
14211 c_name = "for";
14212 if (!first)
14213 goto clause_not_first;
14214 break;
14215 case PRAGMA_OMP_CLAUSE_SECTIONS:
14216 clauses
14217 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14218 clauses);
14219 c_name = "sections";
14220 if (!first)
14221 goto clause_not_first;
14222 break;
14223 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14224 clauses
14225 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14226 clauses);
14227 c_name = "taskgroup";
14228 if (!first)
14229 goto clause_not_first;
14230 break;
14231 case PRAGMA_OMP_CLAUSE_LINK:
14232 clauses
14233 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14234 c_name = "link";
14235 break;
14236 case PRAGMA_OMP_CLAUSE_TO:
14237 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14238 clauses
14239 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14240 clauses);
14241 else
14242 clauses = c_parser_omp_clause_to (parser, clauses);
14243 c_name = "to";
14244 break;
14245 case PRAGMA_OMP_CLAUSE_FROM:
14246 clauses = c_parser_omp_clause_from (parser, clauses);
14247 c_name = "from";
14248 break;
14249 case PRAGMA_OMP_CLAUSE_UNIFORM:
14250 clauses = c_parser_omp_clause_uniform (parser, clauses);
14251 c_name = "uniform";
14252 break;
14253 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14254 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14255 c_name = "num_teams";
14256 break;
14257 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14258 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14259 c_name = "thread_limit";
14260 break;
14261 case PRAGMA_OMP_CLAUSE_ALIGNED:
14262 clauses = c_parser_omp_clause_aligned (parser, clauses);
14263 c_name = "aligned";
14264 break;
14265 case PRAGMA_OMP_CLAUSE_LINEAR:
14266 clauses = c_parser_omp_clause_linear (parser, clauses);
14267 c_name = "linear";
14268 break;
14269 case PRAGMA_OMP_CLAUSE_DEPEND:
14270 clauses = c_parser_omp_clause_depend (parser, clauses);
14271 c_name = "depend";
14272 break;
14273 case PRAGMA_OMP_CLAUSE_MAP:
14274 clauses = c_parser_omp_clause_map (parser, clauses);
14275 c_name = "map";
14276 break;
14277 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14278 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14279 c_name = "use_device_ptr";
14280 break;
14281 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14282 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14283 c_name = "is_device_ptr";
14284 break;
14285 case PRAGMA_OMP_CLAUSE_DEVICE:
14286 clauses = c_parser_omp_clause_device (parser, clauses);
14287 c_name = "device";
14288 break;
14289 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14290 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14291 c_name = "dist_schedule";
14292 break;
14293 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14294 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14295 c_name = "proc_bind";
14296 break;
14297 case PRAGMA_OMP_CLAUSE_SAFELEN:
14298 clauses = c_parser_omp_clause_safelen (parser, clauses);
14299 c_name = "safelen";
14300 break;
14301 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14302 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14303 c_name = "simdlen";
14304 break;
14305 case PRAGMA_OMP_CLAUSE_NOGROUP:
14306 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14307 c_name = "nogroup";
14308 break;
14309 case PRAGMA_OMP_CLAUSE_THREADS:
14310 clauses
14311 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14312 clauses);
14313 c_name = "threads";
14314 break;
14315 case PRAGMA_OMP_CLAUSE_SIMD:
14316 clauses
14317 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14318 clauses);
14319 c_name = "simd";
14320 break;
14321 default:
14322 c_parser_error (parser, "expected %<#pragma omp%> clause");
14323 goto saw_error;
14326 first = false;
14328 if (((mask >> c_kind) & 1) == 0)
14330 /* Remove the invalid clause(s) from the list to avoid
14331 confusing the rest of the compiler. */
14332 clauses = prev;
14333 error_at (here, "%qs is not valid for %qs", c_name, where);
14337 saw_error:
14338 c_parser_skip_to_pragma_eol (parser);
14340 if (finish_p)
14342 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14343 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14344 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14347 return clauses;
14350 /* OpenACC 2.0, OpenMP 2.5:
14351 structured-block:
14352 statement
14354 In practice, we're also interested in adding the statement to an
14355 outer node. So it is convenient if we work around the fact that
14356 c_parser_statement calls add_stmt. */
14358 static tree
14359 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14361 tree stmt = push_stmt_list ();
14362 c_parser_statement (parser, if_p);
14363 return pop_stmt_list (stmt);
14366 /* OpenACC 2.0:
14367 # pragma acc cache (variable-list) new-line
14369 LOC is the location of the #pragma token.
14372 static tree
14373 c_parser_oacc_cache (location_t loc, c_parser *parser)
14375 tree stmt, clauses;
14377 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14378 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14380 c_parser_skip_to_pragma_eol (parser);
14382 stmt = make_node (OACC_CACHE);
14383 TREE_TYPE (stmt) = void_type_node;
14384 OACC_CACHE_CLAUSES (stmt) = clauses;
14385 SET_EXPR_LOCATION (stmt, loc);
14386 add_stmt (stmt);
14388 return stmt;
14391 /* OpenACC 2.0:
14392 # pragma acc data oacc-data-clause[optseq] new-line
14393 structured-block
14395 LOC is the location of the #pragma token.
14398 #define OACC_DATA_CLAUSE_MASK \
14399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
14407 static tree
14408 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14410 tree stmt, clauses, block;
14412 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14413 "#pragma acc data");
14415 block = c_begin_omp_parallel ();
14416 add_stmt (c_parser_omp_structured_block (parser, if_p));
14418 stmt = c_finish_oacc_data (loc, clauses, block);
14420 return stmt;
14423 /* OpenACC 2.0:
14424 # pragma acc declare oacc-data-clause[optseq] new-line
14427 #define OACC_DECLARE_CLAUSE_MASK \
14428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
14437 static void
14438 c_parser_oacc_declare (c_parser *parser)
14440 location_t pragma_loc = c_parser_peek_token (parser)->location;
14441 tree clauses, stmt, t, decl;
14443 bool error = false;
14445 c_parser_consume_pragma (parser);
14447 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14448 "#pragma acc declare");
14449 if (!clauses)
14451 error_at (pragma_loc,
14452 "no valid clauses specified in %<#pragma acc declare%>");
14453 return;
14456 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14458 location_t loc = OMP_CLAUSE_LOCATION (t);
14459 decl = OMP_CLAUSE_DECL (t);
14460 if (!DECL_P (decl))
14462 error_at (loc, "array section in %<#pragma acc declare%>");
14463 error = true;
14464 continue;
14467 switch (OMP_CLAUSE_MAP_KIND (t))
14469 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14470 case GOMP_MAP_ALLOC:
14471 case GOMP_MAP_TO:
14472 case GOMP_MAP_FORCE_DEVICEPTR:
14473 case GOMP_MAP_DEVICE_RESIDENT:
14474 break;
14476 case GOMP_MAP_LINK:
14477 if (!global_bindings_p ()
14478 && (TREE_STATIC (decl)
14479 || !DECL_EXTERNAL (decl)))
14481 error_at (loc,
14482 "%qD must be a global variable in "
14483 "%<#pragma acc declare link%>",
14484 decl);
14485 error = true;
14486 continue;
14488 break;
14490 default:
14491 if (global_bindings_p ())
14493 error_at (loc, "invalid OpenACC clause at file scope");
14494 error = true;
14495 continue;
14497 if (DECL_EXTERNAL (decl))
14499 error_at (loc,
14500 "invalid use of %<extern%> variable %qD "
14501 "in %<#pragma acc declare%>", decl);
14502 error = true;
14503 continue;
14505 else if (TREE_PUBLIC (decl))
14507 error_at (loc,
14508 "invalid use of %<global%> variable %qD "
14509 "in %<#pragma acc declare%>", decl);
14510 error = true;
14511 continue;
14513 break;
14516 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14517 || lookup_attribute ("omp declare target link",
14518 DECL_ATTRIBUTES (decl)))
14520 error_at (loc, "variable %qD used more than once with "
14521 "%<#pragma acc declare%>", decl);
14522 error = true;
14523 continue;
14526 if (!error)
14528 tree id;
14530 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14531 id = get_identifier ("omp declare target link");
14532 else
14533 id = get_identifier ("omp declare target");
14535 DECL_ATTRIBUTES (decl)
14536 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14538 if (global_bindings_p ())
14540 symtab_node *node = symtab_node::get (decl);
14541 if (node != NULL)
14543 node->offloadable = 1;
14544 if (ENABLE_OFFLOADING)
14546 g->have_offload = true;
14547 if (is_a <varpool_node *> (node))
14548 vec_safe_push (offload_vars, decl);
14555 if (error || global_bindings_p ())
14556 return;
14558 stmt = make_node (OACC_DECLARE);
14559 TREE_TYPE (stmt) = void_type_node;
14560 OACC_DECLARE_CLAUSES (stmt) = clauses;
14561 SET_EXPR_LOCATION (stmt, pragma_loc);
14563 add_stmt (stmt);
14565 return;
14568 /* OpenACC 2.0:
14569 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14573 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14576 LOC is the location of the #pragma token.
14579 #define OACC_ENTER_DATA_CLAUSE_MASK \
14580 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14586 #define OACC_EXIT_DATA_CLAUSE_MASK \
14587 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
14592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14594 static void
14595 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14597 location_t loc = c_parser_peek_token (parser)->location;
14598 tree clauses, stmt;
14599 const char *p = "";
14601 c_parser_consume_pragma (parser);
14603 if (c_parser_next_token_is (parser, CPP_NAME))
14605 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14606 c_parser_consume_token (parser);
14609 if (strcmp (p, "data") != 0)
14611 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14612 enter ? "enter" : "exit");
14613 parser->error = true;
14614 c_parser_skip_to_pragma_eol (parser);
14615 return;
14618 if (enter)
14619 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14620 "#pragma acc enter data");
14621 else
14622 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14623 "#pragma acc exit data");
14625 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14627 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14628 enter ? "enter" : "exit");
14629 return;
14632 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14633 TREE_TYPE (stmt) = void_type_node;
14634 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14635 SET_EXPR_LOCATION (stmt, loc);
14636 add_stmt (stmt);
14640 /* OpenACC 2.0:
14641 # pragma acc host_data oacc-data-clause[optseq] new-line
14642 structured-block
14645 #define OACC_HOST_DATA_CLAUSE_MASK \
14646 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14648 static tree
14649 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14651 tree stmt, clauses, block;
14653 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14654 "#pragma acc host_data");
14656 block = c_begin_omp_parallel ();
14657 add_stmt (c_parser_omp_structured_block (parser, if_p));
14658 stmt = c_finish_oacc_host_data (loc, clauses, block);
14659 return stmt;
14663 /* OpenACC 2.0:
14665 # pragma acc loop oacc-loop-clause[optseq] new-line
14666 structured-block
14668 LOC is the location of the #pragma token.
14671 #define OACC_LOOP_CLAUSE_MASK \
14672 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14682 static tree
14683 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14684 omp_clause_mask mask, tree *cclauses, bool *if_p)
14686 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14688 strcat (p_name, " loop");
14689 mask |= OACC_LOOP_CLAUSE_MASK;
14691 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14692 cclauses == NULL);
14693 if (cclauses)
14695 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14696 if (*cclauses)
14697 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14698 if (clauses)
14699 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14702 tree block = c_begin_compound_stmt (true);
14703 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14704 if_p);
14705 block = c_end_compound_stmt (loc, block, true);
14706 add_stmt (block);
14708 return stmt;
14711 /* OpenACC 2.0:
14712 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14713 structured-block
14717 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14718 structured-block
14720 LOC is the location of the #pragma token.
14723 #define OACC_KERNELS_CLAUSE_MASK \
14724 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14738 #define OACC_PARALLEL_CLAUSE_MASK \
14739 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14756 static tree
14757 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14758 enum pragma_kind p_kind, char *p_name,
14759 bool *if_p)
14761 omp_clause_mask mask;
14762 enum tree_code code;
14763 switch (p_kind)
14765 case PRAGMA_OACC_KERNELS:
14766 strcat (p_name, " kernels");
14767 mask = OACC_KERNELS_CLAUSE_MASK;
14768 code = OACC_KERNELS;
14769 break;
14770 case PRAGMA_OACC_PARALLEL:
14771 strcat (p_name, " parallel");
14772 mask = OACC_PARALLEL_CLAUSE_MASK;
14773 code = OACC_PARALLEL;
14774 break;
14775 default:
14776 gcc_unreachable ();
14779 if (c_parser_next_token_is (parser, CPP_NAME))
14781 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14782 if (strcmp (p, "loop") == 0)
14784 c_parser_consume_token (parser);
14785 tree block = c_begin_omp_parallel ();
14786 tree clauses;
14787 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14788 return c_finish_omp_construct (loc, code, block, clauses);
14792 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14794 tree block = c_begin_omp_parallel ();
14795 add_stmt (c_parser_omp_structured_block (parser, if_p));
14797 return c_finish_omp_construct (loc, code, block, clauses);
14800 /* OpenACC 2.0:
14801 # pragma acc routine oacc-routine-clause[optseq] new-line
14802 function-definition
14804 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14807 #define OACC_ROUTINE_CLAUSE_MASK \
14808 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14813 /* Parse an OpenACC routine directive. For named directives, we apply
14814 immediately to the named function. For unnamed ones we then parse
14815 a declaration or definition, which must be for a function. */
14817 static void
14818 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14820 gcc_checking_assert (context == pragma_external);
14822 oacc_routine_data data;
14823 data.error_seen = false;
14824 data.fndecl_seen = false;
14825 data.clauses = NULL_TREE;
14826 data.loc = c_parser_peek_token (parser)->location;
14828 c_parser_consume_pragma (parser);
14830 /* Look for optional '( name )'. */
14831 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14833 c_parser_consume_token (parser); /* '(' */
14835 tree decl = NULL_TREE;
14836 c_token *name_token = c_parser_peek_token (parser);
14837 location_t name_loc = name_token->location;
14838 if (name_token->type == CPP_NAME
14839 && (name_token->id_kind == C_ID_ID
14840 || name_token->id_kind == C_ID_TYPENAME))
14842 decl = lookup_name (name_token->value);
14843 if (!decl)
14844 error_at (name_loc,
14845 "%qE has not been declared", name_token->value);
14846 c_parser_consume_token (parser);
14848 else
14849 c_parser_error (parser, "expected function name");
14851 if (!decl
14852 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14854 c_parser_skip_to_pragma_eol (parser, false);
14855 return;
14858 data.clauses
14859 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14860 "#pragma acc routine");
14862 if (TREE_CODE (decl) != FUNCTION_DECL)
14864 error_at (name_loc, "%qD does not refer to a function", decl);
14865 return;
14868 c_finish_oacc_routine (&data, decl, false);
14870 else /* No optional '( name )'. */
14872 data.clauses
14873 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14874 "#pragma acc routine");
14876 /* Emit a helpful diagnostic if there's another pragma following this
14877 one. Also don't allow a static assertion declaration, as in the
14878 following we'll just parse a *single* "declaration or function
14879 definition", and the static assertion counts an one. */
14880 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14881 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14883 error_at (data.loc,
14884 "%<#pragma acc routine%> not immediately followed by"
14885 " function declaration or definition");
14886 /* ..., and then just keep going. */
14887 return;
14890 /* We only have to consider the pragma_external case here. */
14891 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14892 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14894 int ext = disable_extension_diagnostics ();
14896 c_parser_consume_token (parser);
14897 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14898 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14899 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14900 NULL, vNULL, &data);
14901 restore_extension_diagnostics (ext);
14903 else
14904 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14905 NULL, vNULL, &data);
14909 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14910 IS_DEFN is true if we're applying it to the definition. */
14912 static void
14913 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14914 bool is_defn)
14916 /* Keep going if we're in error reporting mode. */
14917 if (data->error_seen
14918 || fndecl == error_mark_node)
14919 return;
14921 if (data->fndecl_seen)
14923 error_at (data->loc,
14924 "%<#pragma acc routine%> not immediately followed by"
14925 " a single function declaration or definition");
14926 data->error_seen = true;
14927 return;
14929 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14931 error_at (data->loc,
14932 "%<#pragma acc routine%> not immediately followed by"
14933 " function declaration or definition");
14934 data->error_seen = true;
14935 return;
14938 if (oacc_get_fn_attrib (fndecl))
14940 error_at (data->loc,
14941 "%<#pragma acc routine%> already applied to %qD", fndecl);
14942 data->error_seen = true;
14943 return;
14946 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14948 error_at (data->loc,
14949 TREE_USED (fndecl)
14950 ? G_("%<#pragma acc routine%> must be applied before use")
14951 : G_("%<#pragma acc routine%> must be applied before "
14952 "definition"));
14953 data->error_seen = true;
14954 return;
14957 /* Process the routine's dimension clauses. */
14958 tree dims = oacc_build_routine_dims (data->clauses);
14959 oacc_replace_fn_attrib (fndecl, dims);
14961 /* Add an "omp declare target" attribute. */
14962 DECL_ATTRIBUTES (fndecl)
14963 = tree_cons (get_identifier ("omp declare target"),
14964 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14966 /* Remember that we've used this "#pragma acc routine". */
14967 data->fndecl_seen = true;
14970 /* OpenACC 2.0:
14971 # pragma acc update oacc-update-clause[optseq] new-line
14974 #define OACC_UPDATE_CLAUSE_MASK \
14975 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
14980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14982 static void
14983 c_parser_oacc_update (c_parser *parser)
14985 location_t loc = c_parser_peek_token (parser)->location;
14987 c_parser_consume_pragma (parser);
14989 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14990 "#pragma acc update");
14991 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14993 error_at (loc,
14994 "%<#pragma acc update%> must contain at least one "
14995 "%<device%> or %<host%> or %<self%> clause");
14996 return;
14999 if (parser->error)
15000 return;
15002 tree stmt = make_node (OACC_UPDATE);
15003 TREE_TYPE (stmt) = void_type_node;
15004 OACC_UPDATE_CLAUSES (stmt) = clauses;
15005 SET_EXPR_LOCATION (stmt, loc);
15006 add_stmt (stmt);
15009 /* OpenACC 2.0:
15010 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15012 LOC is the location of the #pragma token.
15015 #define OACC_WAIT_CLAUSE_MASK \
15016 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15018 static tree
15019 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15021 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15023 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15024 list = c_parser_oacc_wait_list (parser, loc, list);
15026 strcpy (p_name, " wait");
15027 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15028 stmt = c_finish_oacc_wait (loc, list, clauses);
15029 add_stmt (stmt);
15031 return stmt;
15034 /* OpenMP 2.5:
15035 # pragma omp atomic new-line
15036 expression-stmt
15038 expression-stmt:
15039 x binop= expr | x++ | ++x | x-- | --x
15040 binop:
15041 +, *, -, /, &, ^, |, <<, >>
15043 where x is an lvalue expression with scalar type.
15045 OpenMP 3.1:
15046 # pragma omp atomic new-line
15047 update-stmt
15049 # pragma omp atomic read new-line
15050 read-stmt
15052 # pragma omp atomic write new-line
15053 write-stmt
15055 # pragma omp atomic update new-line
15056 update-stmt
15058 # pragma omp atomic capture new-line
15059 capture-stmt
15061 # pragma omp atomic capture new-line
15062 capture-block
15064 read-stmt:
15065 v = x
15066 write-stmt:
15067 x = expr
15068 update-stmt:
15069 expression-stmt | x = x binop expr
15070 capture-stmt:
15071 v = expression-stmt
15072 capture-block:
15073 { v = x; update-stmt; } | { update-stmt; v = x; }
15075 OpenMP 4.0:
15076 update-stmt:
15077 expression-stmt | x = x binop expr | x = expr binop x
15078 capture-stmt:
15079 v = update-stmt
15080 capture-block:
15081 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15083 where x and v are lvalue expressions with scalar type.
15085 LOC is the location of the #pragma token. */
15087 static void
15088 c_parser_omp_atomic (location_t loc, c_parser *parser)
15090 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15091 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15092 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15093 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15094 struct c_expr expr;
15095 location_t eloc;
15096 bool structured_block = false;
15097 bool swapped = false;
15098 bool seq_cst = false;
15099 bool non_lvalue_p;
15101 if (c_parser_next_token_is (parser, CPP_NAME))
15103 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15104 if (!strcmp (p, "seq_cst"))
15106 seq_cst = true;
15107 c_parser_consume_token (parser);
15108 if (c_parser_next_token_is (parser, CPP_COMMA)
15109 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15110 c_parser_consume_token (parser);
15113 if (c_parser_next_token_is (parser, CPP_NAME))
15115 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15117 if (!strcmp (p, "read"))
15118 code = OMP_ATOMIC_READ;
15119 else if (!strcmp (p, "write"))
15120 code = NOP_EXPR;
15121 else if (!strcmp (p, "update"))
15122 code = OMP_ATOMIC;
15123 else if (!strcmp (p, "capture"))
15124 code = OMP_ATOMIC_CAPTURE_NEW;
15125 else
15126 p = NULL;
15127 if (p)
15128 c_parser_consume_token (parser);
15130 if (!seq_cst)
15132 if (c_parser_next_token_is (parser, CPP_COMMA)
15133 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15134 c_parser_consume_token (parser);
15136 if (c_parser_next_token_is (parser, CPP_NAME))
15138 const char *p
15139 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15140 if (!strcmp (p, "seq_cst"))
15142 seq_cst = true;
15143 c_parser_consume_token (parser);
15147 c_parser_skip_to_pragma_eol (parser);
15149 switch (code)
15151 case OMP_ATOMIC_READ:
15152 case NOP_EXPR: /* atomic write */
15153 v = c_parser_cast_expression (parser, NULL).value;
15154 non_lvalue_p = !lvalue_p (v);
15155 v = c_fully_fold (v, false, NULL, true);
15156 if (v == error_mark_node)
15157 goto saw_error;
15158 if (non_lvalue_p)
15159 v = non_lvalue (v);
15160 loc = c_parser_peek_token (parser)->location;
15161 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15162 goto saw_error;
15163 if (code == NOP_EXPR)
15165 lhs = c_parser_expression (parser).value;
15166 lhs = c_fully_fold (lhs, false, NULL);
15167 if (lhs == error_mark_node)
15168 goto saw_error;
15170 else
15172 lhs = c_parser_cast_expression (parser, NULL).value;
15173 non_lvalue_p = !lvalue_p (lhs);
15174 lhs = c_fully_fold (lhs, false, NULL, true);
15175 if (lhs == error_mark_node)
15176 goto saw_error;
15177 if (non_lvalue_p)
15178 lhs = non_lvalue (lhs);
15180 if (code == NOP_EXPR)
15182 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15183 opcode. */
15184 code = OMP_ATOMIC;
15185 rhs = lhs;
15186 lhs = v;
15187 v = NULL_TREE;
15189 goto done;
15190 case OMP_ATOMIC_CAPTURE_NEW:
15191 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15193 c_parser_consume_token (parser);
15194 structured_block = true;
15196 else
15198 v = c_parser_cast_expression (parser, NULL).value;
15199 non_lvalue_p = !lvalue_p (v);
15200 v = c_fully_fold (v, false, NULL, true);
15201 if (v == error_mark_node)
15202 goto saw_error;
15203 if (non_lvalue_p)
15204 v = non_lvalue (v);
15205 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15206 goto saw_error;
15208 break;
15209 default:
15210 break;
15213 /* For structured_block case we don't know yet whether
15214 old or new x should be captured. */
15215 restart:
15216 eloc = c_parser_peek_token (parser)->location;
15217 expr = c_parser_cast_expression (parser, NULL);
15218 lhs = expr.value;
15219 expr = default_function_array_conversion (eloc, expr);
15220 unfolded_lhs = expr.value;
15221 lhs = c_fully_fold (lhs, false, NULL, true);
15222 orig_lhs = lhs;
15223 switch (TREE_CODE (lhs))
15225 case ERROR_MARK:
15226 saw_error:
15227 c_parser_skip_to_end_of_block_or_statement (parser);
15228 if (structured_block)
15230 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15231 c_parser_consume_token (parser);
15232 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15234 c_parser_skip_to_end_of_block_or_statement (parser);
15235 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15236 c_parser_consume_token (parser);
15239 return;
15241 case POSTINCREMENT_EXPR:
15242 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15243 code = OMP_ATOMIC_CAPTURE_OLD;
15244 /* FALLTHROUGH */
15245 case PREINCREMENT_EXPR:
15246 lhs = TREE_OPERAND (lhs, 0);
15247 unfolded_lhs = NULL_TREE;
15248 opcode = PLUS_EXPR;
15249 rhs = integer_one_node;
15250 break;
15252 case POSTDECREMENT_EXPR:
15253 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15254 code = OMP_ATOMIC_CAPTURE_OLD;
15255 /* FALLTHROUGH */
15256 case PREDECREMENT_EXPR:
15257 lhs = TREE_OPERAND (lhs, 0);
15258 unfolded_lhs = NULL_TREE;
15259 opcode = MINUS_EXPR;
15260 rhs = integer_one_node;
15261 break;
15263 case COMPOUND_EXPR:
15264 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15265 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15266 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15267 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15268 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15269 (TREE_OPERAND (lhs, 1), 0), 0)))
15270 == BOOLEAN_TYPE)
15271 /* Undo effects of boolean_increment for post {in,de}crement. */
15272 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15273 /* FALLTHRU */
15274 case MODIFY_EXPR:
15275 if (TREE_CODE (lhs) == MODIFY_EXPR
15276 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15278 /* Undo effects of boolean_increment. */
15279 if (integer_onep (TREE_OPERAND (lhs, 1)))
15281 /* This is pre or post increment. */
15282 rhs = TREE_OPERAND (lhs, 1);
15283 lhs = TREE_OPERAND (lhs, 0);
15284 unfolded_lhs = NULL_TREE;
15285 opcode = NOP_EXPR;
15286 if (code == OMP_ATOMIC_CAPTURE_NEW
15287 && !structured_block
15288 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15289 code = OMP_ATOMIC_CAPTURE_OLD;
15290 break;
15292 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15293 && TREE_OPERAND (lhs, 0)
15294 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15296 /* This is pre or post decrement. */
15297 rhs = TREE_OPERAND (lhs, 1);
15298 lhs = TREE_OPERAND (lhs, 0);
15299 unfolded_lhs = NULL_TREE;
15300 opcode = NOP_EXPR;
15301 if (code == OMP_ATOMIC_CAPTURE_NEW
15302 && !structured_block
15303 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15304 code = OMP_ATOMIC_CAPTURE_OLD;
15305 break;
15308 /* FALLTHRU */
15309 default:
15310 if (!lvalue_p (unfolded_lhs))
15311 lhs = non_lvalue (lhs);
15312 switch (c_parser_peek_token (parser)->type)
15314 case CPP_MULT_EQ:
15315 opcode = MULT_EXPR;
15316 break;
15317 case CPP_DIV_EQ:
15318 opcode = TRUNC_DIV_EXPR;
15319 break;
15320 case CPP_PLUS_EQ:
15321 opcode = PLUS_EXPR;
15322 break;
15323 case CPP_MINUS_EQ:
15324 opcode = MINUS_EXPR;
15325 break;
15326 case CPP_LSHIFT_EQ:
15327 opcode = LSHIFT_EXPR;
15328 break;
15329 case CPP_RSHIFT_EQ:
15330 opcode = RSHIFT_EXPR;
15331 break;
15332 case CPP_AND_EQ:
15333 opcode = BIT_AND_EXPR;
15334 break;
15335 case CPP_OR_EQ:
15336 opcode = BIT_IOR_EXPR;
15337 break;
15338 case CPP_XOR_EQ:
15339 opcode = BIT_XOR_EXPR;
15340 break;
15341 case CPP_EQ:
15342 c_parser_consume_token (parser);
15343 eloc = c_parser_peek_token (parser)->location;
15344 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15345 rhs1 = expr.value;
15346 switch (TREE_CODE (rhs1))
15348 case MULT_EXPR:
15349 case TRUNC_DIV_EXPR:
15350 case RDIV_EXPR:
15351 case PLUS_EXPR:
15352 case MINUS_EXPR:
15353 case LSHIFT_EXPR:
15354 case RSHIFT_EXPR:
15355 case BIT_AND_EXPR:
15356 case BIT_IOR_EXPR:
15357 case BIT_XOR_EXPR:
15358 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15360 opcode = TREE_CODE (rhs1);
15361 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15362 true);
15363 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15364 true);
15365 goto stmt_done;
15367 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15369 opcode = TREE_CODE (rhs1);
15370 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15371 true);
15372 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15373 true);
15374 swapped = !commutative_tree_code (opcode);
15375 goto stmt_done;
15377 break;
15378 case ERROR_MARK:
15379 goto saw_error;
15380 default:
15381 break;
15383 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15385 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15387 code = OMP_ATOMIC_CAPTURE_OLD;
15388 v = lhs;
15389 lhs = NULL_TREE;
15390 expr = default_function_array_read_conversion (eloc, expr);
15391 unfolded_lhs1 = expr.value;
15392 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15393 rhs1 = NULL_TREE;
15394 c_parser_consume_token (parser);
15395 goto restart;
15397 if (structured_block)
15399 opcode = NOP_EXPR;
15400 expr = default_function_array_read_conversion (eloc, expr);
15401 rhs = c_fully_fold (expr.value, false, NULL, true);
15402 rhs1 = NULL_TREE;
15403 goto stmt_done;
15406 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15407 goto saw_error;
15408 default:
15409 c_parser_error (parser,
15410 "invalid operator for %<#pragma omp atomic%>");
15411 goto saw_error;
15414 /* Arrange to pass the location of the assignment operator to
15415 c_finish_omp_atomic. */
15416 loc = c_parser_peek_token (parser)->location;
15417 c_parser_consume_token (parser);
15418 eloc = c_parser_peek_token (parser)->location;
15419 expr = c_parser_expression (parser);
15420 expr = default_function_array_read_conversion (eloc, expr);
15421 rhs = expr.value;
15422 rhs = c_fully_fold (rhs, false, NULL, true);
15423 break;
15425 stmt_done:
15426 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15428 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15429 goto saw_error;
15430 v = c_parser_cast_expression (parser, NULL).value;
15431 non_lvalue_p = !lvalue_p (v);
15432 v = c_fully_fold (v, false, NULL, true);
15433 if (v == error_mark_node)
15434 goto saw_error;
15435 if (non_lvalue_p)
15436 v = non_lvalue (v);
15437 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15438 goto saw_error;
15439 eloc = c_parser_peek_token (parser)->location;
15440 expr = c_parser_cast_expression (parser, NULL);
15441 lhs1 = expr.value;
15442 expr = default_function_array_read_conversion (eloc, expr);
15443 unfolded_lhs1 = expr.value;
15444 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15445 if (lhs1 == error_mark_node)
15446 goto saw_error;
15447 if (!lvalue_p (unfolded_lhs1))
15448 lhs1 = non_lvalue (lhs1);
15450 if (structured_block)
15452 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15453 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15455 done:
15456 if (unfolded_lhs && unfolded_lhs1
15457 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15459 error ("%<#pragma omp atomic capture%> uses two different "
15460 "expressions for memory");
15461 stmt = error_mark_node;
15463 else
15464 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15465 swapped, seq_cst);
15466 if (stmt != error_mark_node)
15467 add_stmt (stmt);
15469 if (!structured_block)
15470 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15474 /* OpenMP 2.5:
15475 # pragma omp barrier new-line
15478 static void
15479 c_parser_omp_barrier (c_parser *parser)
15481 location_t loc = c_parser_peek_token (parser)->location;
15482 c_parser_consume_pragma (parser);
15483 c_parser_skip_to_pragma_eol (parser);
15485 c_finish_omp_barrier (loc);
15488 /* OpenMP 2.5:
15489 # pragma omp critical [(name)] new-line
15490 structured-block
15492 OpenMP 4.5:
15493 # pragma omp critical [(name) [hint(expression)]] new-line
15495 LOC is the location of the #pragma itself. */
15497 #define OMP_CRITICAL_CLAUSE_MASK \
15498 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15500 static tree
15501 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15503 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15505 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15507 c_parser_consume_token (parser);
15508 if (c_parser_next_token_is (parser, CPP_NAME))
15510 name = c_parser_peek_token (parser)->value;
15511 c_parser_consume_token (parser);
15512 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15514 else
15515 c_parser_error (parser, "expected identifier");
15517 clauses = c_parser_omp_all_clauses (parser,
15518 OMP_CRITICAL_CLAUSE_MASK,
15519 "#pragma omp critical");
15521 else
15523 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15524 c_parser_error (parser, "expected %<(%> or end of line");
15525 c_parser_skip_to_pragma_eol (parser);
15528 stmt = c_parser_omp_structured_block (parser, if_p);
15529 return c_finish_omp_critical (loc, stmt, name, clauses);
15532 /* OpenMP 2.5:
15533 # pragma omp flush flush-vars[opt] new-line
15535 flush-vars:
15536 ( variable-list ) */
15538 static void
15539 c_parser_omp_flush (c_parser *parser)
15541 location_t loc = c_parser_peek_token (parser)->location;
15542 c_parser_consume_pragma (parser);
15543 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15544 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15545 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15546 c_parser_error (parser, "expected %<(%> or end of line");
15547 c_parser_skip_to_pragma_eol (parser);
15549 c_finish_omp_flush (loc);
15552 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15553 The real trick here is to determine the loop control variable early
15554 so that we can push a new decl if necessary to make it private.
15555 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15556 respectively. */
15558 static tree
15559 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15560 tree clauses, tree *cclauses, bool *if_p)
15562 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15563 tree declv, condv, incrv, initv, ret = NULL_TREE;
15564 tree pre_body = NULL_TREE, this_pre_body;
15565 tree ordered_cl = NULL_TREE;
15566 bool fail = false, open_brace_parsed = false;
15567 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15568 location_t for_loc;
15569 bool tiling = false;
15570 vec<tree, va_gc> *for_block = make_tree_vector ();
15572 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15573 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15574 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15575 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15577 tiling = true;
15578 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15580 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15581 && OMP_CLAUSE_ORDERED_EXPR (cl))
15583 ordered_cl = cl;
15584 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15587 if (ordered && ordered < collapse)
15589 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15590 "%<ordered%> clause parameter is less than %<collapse%>");
15591 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15592 = build_int_cst (NULL_TREE, collapse);
15593 ordered = collapse;
15595 if (ordered)
15597 for (tree *pc = &clauses; *pc; )
15598 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15600 error_at (OMP_CLAUSE_LOCATION (*pc),
15601 "%<linear%> clause may not be specified together "
15602 "with %<ordered%> clause with a parameter");
15603 *pc = OMP_CLAUSE_CHAIN (*pc);
15605 else
15606 pc = &OMP_CLAUSE_CHAIN (*pc);
15609 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15610 count = ordered ? ordered : collapse;
15612 declv = make_tree_vec (count);
15613 initv = make_tree_vec (count);
15614 condv = make_tree_vec (count);
15615 incrv = make_tree_vec (count);
15617 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15619 c_parser_error (parser, "for statement expected");
15620 return NULL;
15622 for_loc = c_parser_peek_token (parser)->location;
15623 c_parser_consume_token (parser);
15625 for (i = 0; i < count; i++)
15627 int bracecount = 0;
15629 matching_parens parens;
15630 if (!parens.require_open (parser))
15631 goto pop_scopes;
15633 /* Parse the initialization declaration or expression. */
15634 if (c_parser_next_tokens_start_declaration (parser))
15636 if (i > 0)
15637 vec_safe_push (for_block, c_begin_compound_stmt (true));
15638 this_pre_body = push_stmt_list ();
15639 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15640 NULL, vNULL);
15641 if (this_pre_body)
15643 this_pre_body = pop_stmt_list (this_pre_body);
15644 if (pre_body)
15646 tree t = pre_body;
15647 pre_body = push_stmt_list ();
15648 add_stmt (t);
15649 add_stmt (this_pre_body);
15650 pre_body = pop_stmt_list (pre_body);
15652 else
15653 pre_body = this_pre_body;
15655 decl = check_for_loop_decls (for_loc, flag_isoc99);
15656 if (decl == NULL)
15657 goto error_init;
15658 if (DECL_INITIAL (decl) == error_mark_node)
15659 decl = error_mark_node;
15660 init = decl;
15662 else if (c_parser_next_token_is (parser, CPP_NAME)
15663 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15665 struct c_expr decl_exp;
15666 struct c_expr init_exp;
15667 location_t init_loc;
15669 decl_exp = c_parser_postfix_expression (parser);
15670 decl = decl_exp.value;
15672 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15674 init_loc = c_parser_peek_token (parser)->location;
15675 init_exp = c_parser_expr_no_commas (parser, NULL);
15676 init_exp = default_function_array_read_conversion (init_loc,
15677 init_exp);
15678 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15679 NOP_EXPR, init_loc, init_exp.value,
15680 init_exp.original_type);
15681 init = c_process_expr_stmt (init_loc, init);
15683 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15685 else
15687 error_init:
15688 c_parser_error (parser,
15689 "expected iteration declaration or initialization");
15690 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15691 "expected %<)%>");
15692 fail = true;
15693 goto parse_next;
15696 /* Parse the loop condition. */
15697 cond = NULL_TREE;
15698 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15700 location_t cond_loc = c_parser_peek_token (parser)->location;
15701 struct c_expr cond_expr
15702 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15704 cond = cond_expr.value;
15705 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15706 if (COMPARISON_CLASS_P (cond))
15708 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15709 op0 = c_fully_fold (op0, false, NULL);
15710 op1 = c_fully_fold (op1, false, NULL);
15711 TREE_OPERAND (cond, 0) = op0;
15712 TREE_OPERAND (cond, 1) = op1;
15714 switch (cond_expr.original_code)
15716 case GT_EXPR:
15717 case GE_EXPR:
15718 case LT_EXPR:
15719 case LE_EXPR:
15720 break;
15721 default:
15722 /* Can't be cond = error_mark_node, because we want to preserve
15723 the location until c_finish_omp_for. */
15724 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15725 break;
15727 protected_set_expr_location (cond, cond_loc);
15729 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15731 /* Parse the increment expression. */
15732 incr = NULL_TREE;
15733 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15735 location_t incr_loc = c_parser_peek_token (parser)->location;
15737 incr = c_process_expr_stmt (incr_loc,
15738 c_parser_expression (parser).value);
15740 parens.skip_until_found_close (parser);
15742 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15743 fail = true;
15744 else
15746 TREE_VEC_ELT (declv, i) = decl;
15747 TREE_VEC_ELT (initv, i) = init;
15748 TREE_VEC_ELT (condv, i) = cond;
15749 TREE_VEC_ELT (incrv, i) = incr;
15752 parse_next:
15753 if (i == count - 1)
15754 break;
15756 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15757 in between the collapsed for loops to be still considered perfectly
15758 nested. Hopefully the final version clarifies this.
15759 For now handle (multiple) {'s and empty statements. */
15762 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15764 c_parser_consume_token (parser);
15765 break;
15767 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15769 c_parser_consume_token (parser);
15770 bracecount++;
15772 else if (bracecount
15773 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15774 c_parser_consume_token (parser);
15775 else
15777 c_parser_error (parser, "not enough perfectly nested loops");
15778 if (bracecount)
15780 open_brace_parsed = true;
15781 bracecount--;
15783 fail = true;
15784 count = 0;
15785 break;
15788 while (1);
15790 nbraces += bracecount;
15793 if (nbraces)
15794 if_p = NULL;
15796 save_break = c_break_label;
15797 c_break_label = size_one_node;
15798 save_cont = c_cont_label;
15799 c_cont_label = NULL_TREE;
15800 body = push_stmt_list ();
15802 if (open_brace_parsed)
15804 location_t here = c_parser_peek_token (parser)->location;
15805 stmt = c_begin_compound_stmt (true);
15806 c_parser_compound_statement_nostart (parser);
15807 add_stmt (c_end_compound_stmt (here, stmt, true));
15809 else
15810 add_stmt (c_parser_c99_block_statement (parser, if_p));
15811 if (c_cont_label)
15813 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15814 SET_EXPR_LOCATION (t, loc);
15815 add_stmt (t);
15818 body = pop_stmt_list (body);
15819 c_break_label = save_break;
15820 c_cont_label = save_cont;
15822 while (nbraces)
15824 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15826 c_parser_consume_token (parser);
15827 nbraces--;
15829 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15830 c_parser_consume_token (parser);
15831 else
15833 c_parser_error (parser, "collapsed loops not perfectly nested");
15834 while (nbraces)
15836 location_t here = c_parser_peek_token (parser)->location;
15837 stmt = c_begin_compound_stmt (true);
15838 add_stmt (body);
15839 c_parser_compound_statement_nostart (parser);
15840 body = c_end_compound_stmt (here, stmt, true);
15841 nbraces--;
15843 goto pop_scopes;
15847 /* Only bother calling c_finish_omp_for if we haven't already generated
15848 an error from the initialization parsing. */
15849 if (!fail)
15851 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15852 incrv, body, pre_body);
15854 /* Check for iterators appearing in lb, b or incr expressions. */
15855 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15856 stmt = NULL_TREE;
15858 if (stmt)
15860 add_stmt (stmt);
15862 if (cclauses != NULL
15863 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15865 tree *c;
15866 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15867 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15868 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15869 c = &OMP_CLAUSE_CHAIN (*c);
15870 else
15872 for (i = 0; i < count; i++)
15873 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15874 break;
15875 if (i == count)
15876 c = &OMP_CLAUSE_CHAIN (*c);
15877 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15879 error_at (loc,
15880 "iteration variable %qD should not be firstprivate",
15881 OMP_CLAUSE_DECL (*c));
15882 *c = OMP_CLAUSE_CHAIN (*c);
15884 else
15886 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15887 tree l = *c;
15888 *c = OMP_CLAUSE_CHAIN (*c);
15889 if (code == OMP_SIMD)
15891 OMP_CLAUSE_CHAIN (l)
15892 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15893 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15895 else
15897 OMP_CLAUSE_CHAIN (l) = clauses;
15898 clauses = l;
15903 OMP_FOR_CLAUSES (stmt) = clauses;
15905 ret = stmt;
15907 pop_scopes:
15908 while (!for_block->is_empty ())
15910 /* FIXME diagnostics: LOC below should be the actual location of
15911 this particular for block. We need to build a list of
15912 locations to go along with FOR_BLOCK. */
15913 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15914 add_stmt (stmt);
15916 release_tree_vector (for_block);
15917 return ret;
15920 /* Helper function for OpenMP parsing, split clauses and call
15921 finish_omp_clauses on each of the set of clauses afterwards. */
15923 static void
15924 omp_split_clauses (location_t loc, enum tree_code code,
15925 omp_clause_mask mask, tree clauses, tree *cclauses)
15927 int i;
15928 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15929 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15930 if (cclauses[i])
15931 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15934 /* OpenMP 4.0:
15935 #pragma omp simd simd-clause[optseq] new-line
15936 for-loop
15938 LOC is the location of the #pragma token.
15941 #define OMP_SIMD_CLAUSE_MASK \
15942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15951 static tree
15952 c_parser_omp_simd (location_t loc, c_parser *parser,
15953 char *p_name, omp_clause_mask mask, tree *cclauses,
15954 bool *if_p)
15956 tree block, clauses, ret;
15958 strcat (p_name, " simd");
15959 mask |= OMP_SIMD_CLAUSE_MASK;
15961 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15962 if (cclauses)
15964 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15965 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15966 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15967 OMP_CLAUSE_ORDERED);
15968 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15970 error_at (OMP_CLAUSE_LOCATION (c),
15971 "%<ordered%> clause with parameter may not be specified "
15972 "on %qs construct", p_name);
15973 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15977 block = c_begin_compound_stmt (true);
15978 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15979 block = c_end_compound_stmt (loc, block, true);
15980 add_stmt (block);
15982 return ret;
15985 /* OpenMP 2.5:
15986 #pragma omp for for-clause[optseq] new-line
15987 for-loop
15989 OpenMP 4.0:
15990 #pragma omp for simd for-simd-clause[optseq] new-line
15991 for-loop
15993 LOC is the location of the #pragma token.
15996 #define OMP_FOR_CLAUSE_MASK \
15997 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
16003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
16004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16007 static tree
16008 c_parser_omp_for (location_t loc, c_parser *parser,
16009 char *p_name, omp_clause_mask mask, tree *cclauses,
16010 bool *if_p)
16012 tree block, clauses, ret;
16014 strcat (p_name, " for");
16015 mask |= OMP_FOR_CLAUSE_MASK;
16016 /* parallel for{, simd} disallows nowait clause, but for
16017 target {teams distribute ,}parallel for{, simd} it should be accepted. */
16018 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16019 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16020 /* Composite distribute parallel for{, simd} disallows ordered clause. */
16021 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16022 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16024 if (c_parser_next_token_is (parser, CPP_NAME))
16026 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16028 if (strcmp (p, "simd") == 0)
16030 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16031 if (cclauses == NULL)
16032 cclauses = cclauses_buf;
16034 c_parser_consume_token (parser);
16035 if (!flag_openmp) /* flag_openmp_simd */
16036 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16037 if_p);
16038 block = c_begin_compound_stmt (true);
16039 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16040 block = c_end_compound_stmt (loc, block, true);
16041 if (ret == NULL_TREE)
16042 return ret;
16043 ret = make_node (OMP_FOR);
16044 TREE_TYPE (ret) = void_type_node;
16045 OMP_FOR_BODY (ret) = block;
16046 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16047 SET_EXPR_LOCATION (ret, loc);
16048 add_stmt (ret);
16049 return ret;
16052 if (!flag_openmp) /* flag_openmp_simd */
16054 c_parser_skip_to_pragma_eol (parser, false);
16055 return NULL_TREE;
16058 /* Composite distribute parallel for disallows linear clause. */
16059 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16060 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16062 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16063 if (cclauses)
16065 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16066 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16069 block = c_begin_compound_stmt (true);
16070 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16071 block = c_end_compound_stmt (loc, block, true);
16072 add_stmt (block);
16074 return ret;
16077 /* OpenMP 2.5:
16078 # pragma omp master new-line
16079 structured-block
16081 LOC is the location of the #pragma token.
16084 static tree
16085 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16087 c_parser_skip_to_pragma_eol (parser);
16088 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16089 if_p));
16092 /* OpenMP 2.5:
16093 # pragma omp ordered new-line
16094 structured-block
16096 OpenMP 4.5:
16097 # pragma omp ordered ordered-clauses new-line
16098 structured-block
16100 # pragma omp ordered depend-clauses new-line */
16102 #define OMP_ORDERED_CLAUSE_MASK \
16103 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16106 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16107 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16109 static bool
16110 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16111 bool *if_p)
16113 location_t loc = c_parser_peek_token (parser)->location;
16114 c_parser_consume_pragma (parser);
16116 if (context != pragma_stmt && context != pragma_compound)
16118 c_parser_error (parser, "expected declaration specifiers");
16119 c_parser_skip_to_pragma_eol (parser, false);
16120 return false;
16123 if (c_parser_next_token_is (parser, CPP_NAME))
16125 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16127 if (!strcmp ("depend", p))
16129 if (!flag_openmp) /* flag_openmp_simd */
16131 c_parser_skip_to_pragma_eol (parser, false);
16132 return false;
16134 if (context == pragma_stmt)
16136 error_at (loc,
16137 "%<#pragma omp ordered%> with %<depend%> clause may "
16138 "only be used in compound statements");
16139 c_parser_skip_to_pragma_eol (parser, false);
16140 return false;
16143 tree clauses
16144 = c_parser_omp_all_clauses (parser,
16145 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16146 "#pragma omp ordered");
16147 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16148 return false;
16152 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16153 "#pragma omp ordered");
16155 if (!flag_openmp /* flag_openmp_simd */
16156 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16157 return false;
16159 c_finish_omp_ordered (loc, clauses,
16160 c_parser_omp_structured_block (parser, if_p));
16161 return true;
16164 /* OpenMP 2.5:
16166 section-scope:
16167 { section-sequence }
16169 section-sequence:
16170 section-directive[opt] structured-block
16171 section-sequence section-directive structured-block
16173 SECTIONS_LOC is the location of the #pragma omp sections. */
16175 static tree
16176 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16178 tree stmt, substmt;
16179 bool error_suppress = false;
16180 location_t loc;
16182 loc = c_parser_peek_token (parser)->location;
16183 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16185 /* Avoid skipping until the end of the block. */
16186 parser->error = false;
16187 return NULL_TREE;
16190 stmt = push_stmt_list ();
16192 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16194 substmt = c_parser_omp_structured_block (parser, NULL);
16195 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16196 SET_EXPR_LOCATION (substmt, loc);
16197 add_stmt (substmt);
16200 while (1)
16202 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16203 break;
16204 if (c_parser_next_token_is (parser, CPP_EOF))
16205 break;
16207 loc = c_parser_peek_token (parser)->location;
16208 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16210 c_parser_consume_pragma (parser);
16211 c_parser_skip_to_pragma_eol (parser);
16212 error_suppress = false;
16214 else if (!error_suppress)
16216 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16217 error_suppress = true;
16220 substmt = c_parser_omp_structured_block (parser, NULL);
16221 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16222 SET_EXPR_LOCATION (substmt, loc);
16223 add_stmt (substmt);
16225 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16226 "expected %<#pragma omp section%> or %<}%>");
16228 substmt = pop_stmt_list (stmt);
16230 stmt = make_node (OMP_SECTIONS);
16231 SET_EXPR_LOCATION (stmt, sections_loc);
16232 TREE_TYPE (stmt) = void_type_node;
16233 OMP_SECTIONS_BODY (stmt) = substmt;
16235 return add_stmt (stmt);
16238 /* OpenMP 2.5:
16239 # pragma omp sections sections-clause[optseq] newline
16240 sections-scope
16242 LOC is the location of the #pragma token.
16245 #define OMP_SECTIONS_CLAUSE_MASK \
16246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16252 static tree
16253 c_parser_omp_sections (location_t loc, c_parser *parser,
16254 char *p_name, omp_clause_mask mask, tree *cclauses)
16256 tree block, clauses, ret;
16258 strcat (p_name, " sections");
16259 mask |= OMP_SECTIONS_CLAUSE_MASK;
16260 if (cclauses)
16261 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16263 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16264 if (cclauses)
16266 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16267 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16270 block = c_begin_compound_stmt (true);
16271 ret = c_parser_omp_sections_scope (loc, parser);
16272 if (ret)
16273 OMP_SECTIONS_CLAUSES (ret) = clauses;
16274 block = c_end_compound_stmt (loc, block, true);
16275 add_stmt (block);
16277 return ret;
16280 /* OpenMP 2.5:
16281 # pragma omp parallel parallel-clause[optseq] new-line
16282 structured-block
16283 # pragma omp parallel for parallel-for-clause[optseq] new-line
16284 structured-block
16285 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16286 structured-block
16288 OpenMP 4.0:
16289 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16290 structured-block
16292 LOC is the location of the #pragma token.
16295 #define OMP_PARALLEL_CLAUSE_MASK \
16296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16306 static tree
16307 c_parser_omp_parallel (location_t loc, c_parser *parser,
16308 char *p_name, omp_clause_mask mask, tree *cclauses,
16309 bool *if_p)
16311 tree stmt, clauses, block;
16313 strcat (p_name, " parallel");
16314 mask |= OMP_PARALLEL_CLAUSE_MASK;
16315 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16316 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16317 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16318 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16320 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16322 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16323 if (cclauses == NULL)
16324 cclauses = cclauses_buf;
16326 c_parser_consume_token (parser);
16327 if (!flag_openmp) /* flag_openmp_simd */
16328 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16329 block = c_begin_omp_parallel ();
16330 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16331 stmt
16332 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16333 block);
16334 if (ret == NULL_TREE)
16335 return ret;
16336 OMP_PARALLEL_COMBINED (stmt) = 1;
16337 return stmt;
16339 /* When combined with distribute, parallel has to be followed by for.
16340 #pragma omp target parallel is allowed though. */
16341 else if (cclauses
16342 && (mask & (OMP_CLAUSE_MASK_1
16343 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16345 error_at (loc, "expected %<for%> after %qs", p_name);
16346 c_parser_skip_to_pragma_eol (parser);
16347 return NULL_TREE;
16349 else if (!flag_openmp) /* flag_openmp_simd */
16351 c_parser_skip_to_pragma_eol (parser, false);
16352 return NULL_TREE;
16354 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16356 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16357 if (strcmp (p, "sections") == 0)
16359 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16360 if (cclauses == NULL)
16361 cclauses = cclauses_buf;
16363 c_parser_consume_token (parser);
16364 block = c_begin_omp_parallel ();
16365 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16366 stmt = c_finish_omp_parallel (loc,
16367 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16368 block);
16369 OMP_PARALLEL_COMBINED (stmt) = 1;
16370 return stmt;
16374 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16375 if (cclauses)
16377 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16378 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16381 block = c_begin_omp_parallel ();
16382 c_parser_statement (parser, if_p);
16383 stmt = c_finish_omp_parallel (loc, clauses, block);
16385 return stmt;
16388 /* OpenMP 2.5:
16389 # pragma omp single single-clause[optseq] new-line
16390 structured-block
16392 LOC is the location of the #pragma.
16395 #define OMP_SINGLE_CLAUSE_MASK \
16396 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16401 static tree
16402 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16404 tree stmt = make_node (OMP_SINGLE);
16405 SET_EXPR_LOCATION (stmt, loc);
16406 TREE_TYPE (stmt) = void_type_node;
16408 OMP_SINGLE_CLAUSES (stmt)
16409 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16410 "#pragma omp single");
16411 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16413 return add_stmt (stmt);
16416 /* OpenMP 3.0:
16417 # pragma omp task task-clause[optseq] new-line
16419 LOC is the location of the #pragma.
16422 #define OMP_TASK_CLAUSE_MASK \
16423 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16434 static tree
16435 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16437 tree clauses, block;
16439 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16440 "#pragma omp task");
16442 block = c_begin_omp_task ();
16443 c_parser_statement (parser, if_p);
16444 return c_finish_omp_task (loc, clauses, block);
16447 /* OpenMP 3.0:
16448 # pragma omp taskwait new-line
16451 static void
16452 c_parser_omp_taskwait (c_parser *parser)
16454 location_t loc = c_parser_peek_token (parser)->location;
16455 c_parser_consume_pragma (parser);
16456 c_parser_skip_to_pragma_eol (parser);
16458 c_finish_omp_taskwait (loc);
16461 /* OpenMP 3.1:
16462 # pragma omp taskyield new-line
16465 static void
16466 c_parser_omp_taskyield (c_parser *parser)
16468 location_t loc = c_parser_peek_token (parser)->location;
16469 c_parser_consume_pragma (parser);
16470 c_parser_skip_to_pragma_eol (parser);
16472 c_finish_omp_taskyield (loc);
16475 /* OpenMP 4.0:
16476 # pragma omp taskgroup new-line
16479 static tree
16480 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16482 location_t loc = c_parser_peek_token (parser)->location;
16483 c_parser_skip_to_pragma_eol (parser);
16484 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16485 if_p));
16488 /* OpenMP 4.0:
16489 # pragma omp cancel cancel-clause[optseq] new-line
16491 LOC is the location of the #pragma.
16494 #define OMP_CANCEL_CLAUSE_MASK \
16495 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16501 static void
16502 c_parser_omp_cancel (c_parser *parser)
16504 location_t loc = c_parser_peek_token (parser)->location;
16506 c_parser_consume_pragma (parser);
16507 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16508 "#pragma omp cancel");
16510 c_finish_omp_cancel (loc, clauses);
16513 /* OpenMP 4.0:
16514 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16516 LOC is the location of the #pragma.
16519 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16520 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16525 static void
16526 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16528 location_t loc = c_parser_peek_token (parser)->location;
16529 tree clauses;
16530 bool point_seen = false;
16532 c_parser_consume_pragma (parser);
16533 if (c_parser_next_token_is (parser, CPP_NAME))
16535 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16536 if (strcmp (p, "point") == 0)
16538 c_parser_consume_token (parser);
16539 point_seen = true;
16542 if (!point_seen)
16544 c_parser_error (parser, "expected %<point%>");
16545 c_parser_skip_to_pragma_eol (parser);
16546 return;
16549 if (context != pragma_compound)
16551 if (context == pragma_stmt)
16552 error_at (loc,
16553 "%<#pragma %s%> may only be used in compound statements",
16554 "omp cancellation point");
16555 else
16556 c_parser_error (parser, "expected declaration specifiers");
16557 c_parser_skip_to_pragma_eol (parser, false);
16558 return;
16561 clauses
16562 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16563 "#pragma omp cancellation point");
16565 c_finish_omp_cancellation_point (loc, clauses);
16568 /* OpenMP 4.0:
16569 #pragma omp distribute distribute-clause[optseq] new-line
16570 for-loop */
16572 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16573 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16579 static tree
16580 c_parser_omp_distribute (location_t loc, c_parser *parser,
16581 char *p_name, omp_clause_mask mask, tree *cclauses,
16582 bool *if_p)
16584 tree clauses, block, ret;
16586 strcat (p_name, " distribute");
16587 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16589 if (c_parser_next_token_is (parser, CPP_NAME))
16591 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16592 bool simd = false;
16593 bool parallel = false;
16595 if (strcmp (p, "simd") == 0)
16596 simd = true;
16597 else
16598 parallel = strcmp (p, "parallel") == 0;
16599 if (parallel || simd)
16601 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16602 if (cclauses == NULL)
16603 cclauses = cclauses_buf;
16604 c_parser_consume_token (parser);
16605 if (!flag_openmp) /* flag_openmp_simd */
16607 if (simd)
16608 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16609 if_p);
16610 else
16611 return c_parser_omp_parallel (loc, parser, p_name, mask,
16612 cclauses, if_p);
16614 block = c_begin_compound_stmt (true);
16615 if (simd)
16616 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16617 if_p);
16618 else
16619 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16620 if_p);
16621 block = c_end_compound_stmt (loc, block, true);
16622 if (ret == NULL)
16623 return ret;
16624 ret = make_node (OMP_DISTRIBUTE);
16625 TREE_TYPE (ret) = void_type_node;
16626 OMP_FOR_BODY (ret) = block;
16627 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16628 SET_EXPR_LOCATION (ret, loc);
16629 add_stmt (ret);
16630 return ret;
16633 if (!flag_openmp) /* flag_openmp_simd */
16635 c_parser_skip_to_pragma_eol (parser, false);
16636 return NULL_TREE;
16639 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16640 if (cclauses)
16642 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16643 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16646 block = c_begin_compound_stmt (true);
16647 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16648 if_p);
16649 block = c_end_compound_stmt (loc, block, true);
16650 add_stmt (block);
16652 return ret;
16655 /* OpenMP 4.0:
16656 # pragma omp teams teams-clause[optseq] new-line
16657 structured-block */
16659 #define OMP_TEAMS_CLAUSE_MASK \
16660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16668 static tree
16669 c_parser_omp_teams (location_t loc, c_parser *parser,
16670 char *p_name, omp_clause_mask mask, tree *cclauses,
16671 bool *if_p)
16673 tree clauses, block, ret;
16675 strcat (p_name, " teams");
16676 mask |= OMP_TEAMS_CLAUSE_MASK;
16678 if (c_parser_next_token_is (parser, CPP_NAME))
16680 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16681 if (strcmp (p, "distribute") == 0)
16683 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16684 if (cclauses == NULL)
16685 cclauses = cclauses_buf;
16687 c_parser_consume_token (parser);
16688 if (!flag_openmp) /* flag_openmp_simd */
16689 return c_parser_omp_distribute (loc, parser, p_name, mask,
16690 cclauses, if_p);
16691 block = c_begin_compound_stmt (true);
16692 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16693 if_p);
16694 block = c_end_compound_stmt (loc, block, true);
16695 if (ret == NULL)
16696 return ret;
16697 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16698 ret = make_node (OMP_TEAMS);
16699 TREE_TYPE (ret) = void_type_node;
16700 OMP_TEAMS_CLAUSES (ret) = clauses;
16701 OMP_TEAMS_BODY (ret) = block;
16702 OMP_TEAMS_COMBINED (ret) = 1;
16703 return add_stmt (ret);
16706 if (!flag_openmp) /* flag_openmp_simd */
16708 c_parser_skip_to_pragma_eol (parser, false);
16709 return NULL_TREE;
16712 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16713 if (cclauses)
16715 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16716 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16719 tree stmt = make_node (OMP_TEAMS);
16720 TREE_TYPE (stmt) = void_type_node;
16721 OMP_TEAMS_CLAUSES (stmt) = clauses;
16722 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16724 return add_stmt (stmt);
16727 /* OpenMP 4.0:
16728 # pragma omp target data target-data-clause[optseq] new-line
16729 structured-block */
16731 #define OMP_TARGET_DATA_CLAUSE_MASK \
16732 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16737 static tree
16738 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16740 tree clauses
16741 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16742 "#pragma omp target data");
16743 int map_seen = 0;
16744 for (tree *pc = &clauses; *pc;)
16746 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16747 switch (OMP_CLAUSE_MAP_KIND (*pc))
16749 case GOMP_MAP_TO:
16750 case GOMP_MAP_ALWAYS_TO:
16751 case GOMP_MAP_FROM:
16752 case GOMP_MAP_ALWAYS_FROM:
16753 case GOMP_MAP_TOFROM:
16754 case GOMP_MAP_ALWAYS_TOFROM:
16755 case GOMP_MAP_ALLOC:
16756 map_seen = 3;
16757 break;
16758 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16759 case GOMP_MAP_ALWAYS_POINTER:
16760 break;
16761 default:
16762 map_seen |= 1;
16763 error_at (OMP_CLAUSE_LOCATION (*pc),
16764 "%<#pragma omp target data%> with map-type other "
16765 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16766 "on %<map%> clause");
16767 *pc = OMP_CLAUSE_CHAIN (*pc);
16768 continue;
16770 pc = &OMP_CLAUSE_CHAIN (*pc);
16773 if (map_seen != 3)
16775 if (map_seen == 0)
16776 error_at (loc,
16777 "%<#pragma omp target data%> must contain at least "
16778 "one %<map%> clause");
16779 return NULL_TREE;
16782 tree stmt = make_node (OMP_TARGET_DATA);
16783 TREE_TYPE (stmt) = void_type_node;
16784 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16785 keep_next_level ();
16786 tree block = c_begin_compound_stmt (true);
16787 add_stmt (c_parser_omp_structured_block (parser, if_p));
16788 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16790 SET_EXPR_LOCATION (stmt, loc);
16791 return add_stmt (stmt);
16794 /* OpenMP 4.0:
16795 # pragma omp target update target-update-clause[optseq] new-line */
16797 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16798 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16805 static bool
16806 c_parser_omp_target_update (location_t loc, c_parser *parser,
16807 enum pragma_context context)
16809 if (context == pragma_stmt)
16811 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16812 "omp target update");
16813 c_parser_skip_to_pragma_eol (parser, false);
16814 return false;
16817 tree clauses
16818 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16819 "#pragma omp target update");
16820 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16821 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16823 error_at (loc,
16824 "%<#pragma omp target update%> must contain at least one "
16825 "%<from%> or %<to%> clauses");
16826 return false;
16829 tree stmt = make_node (OMP_TARGET_UPDATE);
16830 TREE_TYPE (stmt) = void_type_node;
16831 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16832 SET_EXPR_LOCATION (stmt, loc);
16833 add_stmt (stmt);
16834 return false;
16837 /* OpenMP 4.5:
16838 # pragma omp target enter data target-data-clause[optseq] new-line */
16840 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16841 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16847 static tree
16848 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16849 enum pragma_context context)
16851 bool data_seen = false;
16852 if (c_parser_next_token_is (parser, CPP_NAME))
16854 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16855 if (strcmp (p, "data") == 0)
16857 c_parser_consume_token (parser);
16858 data_seen = true;
16861 if (!data_seen)
16863 c_parser_error (parser, "expected %<data%>");
16864 c_parser_skip_to_pragma_eol (parser);
16865 return NULL_TREE;
16868 if (context == pragma_stmt)
16870 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16871 "omp target enter data");
16872 c_parser_skip_to_pragma_eol (parser, false);
16873 return NULL_TREE;
16876 tree clauses
16877 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16878 "#pragma omp target enter data");
16879 int map_seen = 0;
16880 for (tree *pc = &clauses; *pc;)
16882 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16883 switch (OMP_CLAUSE_MAP_KIND (*pc))
16885 case GOMP_MAP_TO:
16886 case GOMP_MAP_ALWAYS_TO:
16887 case GOMP_MAP_ALLOC:
16888 map_seen = 3;
16889 break;
16890 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16891 case GOMP_MAP_ALWAYS_POINTER:
16892 break;
16893 default:
16894 map_seen |= 1;
16895 error_at (OMP_CLAUSE_LOCATION (*pc),
16896 "%<#pragma omp target enter data%> with map-type other "
16897 "than %<to%> or %<alloc%> on %<map%> clause");
16898 *pc = OMP_CLAUSE_CHAIN (*pc);
16899 continue;
16901 pc = &OMP_CLAUSE_CHAIN (*pc);
16904 if (map_seen != 3)
16906 if (map_seen == 0)
16907 error_at (loc,
16908 "%<#pragma omp target enter data%> must contain at least "
16909 "one %<map%> clause");
16910 return NULL_TREE;
16913 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16914 TREE_TYPE (stmt) = void_type_node;
16915 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16916 SET_EXPR_LOCATION (stmt, loc);
16917 add_stmt (stmt);
16918 return stmt;
16921 /* OpenMP 4.5:
16922 # pragma omp target exit data target-data-clause[optseq] new-line */
16924 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16931 static tree
16932 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16933 enum pragma_context context)
16935 bool data_seen = false;
16936 if (c_parser_next_token_is (parser, CPP_NAME))
16938 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16939 if (strcmp (p, "data") == 0)
16941 c_parser_consume_token (parser);
16942 data_seen = true;
16945 if (!data_seen)
16947 c_parser_error (parser, "expected %<data%>");
16948 c_parser_skip_to_pragma_eol (parser);
16949 return NULL_TREE;
16952 if (context == pragma_stmt)
16954 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16955 "omp target exit data");
16956 c_parser_skip_to_pragma_eol (parser, false);
16957 return NULL_TREE;
16960 tree clauses
16961 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16962 "#pragma omp target exit data");
16964 int map_seen = 0;
16965 for (tree *pc = &clauses; *pc;)
16967 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16968 switch (OMP_CLAUSE_MAP_KIND (*pc))
16970 case GOMP_MAP_FROM:
16971 case GOMP_MAP_ALWAYS_FROM:
16972 case GOMP_MAP_RELEASE:
16973 case GOMP_MAP_DELETE:
16974 map_seen = 3;
16975 break;
16976 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16977 case GOMP_MAP_ALWAYS_POINTER:
16978 break;
16979 default:
16980 map_seen |= 1;
16981 error_at (OMP_CLAUSE_LOCATION (*pc),
16982 "%<#pragma omp target exit data%> with map-type other "
16983 "than %<from%>, %<release%> or %<delete%> on %<map%>"
16984 " clause");
16985 *pc = OMP_CLAUSE_CHAIN (*pc);
16986 continue;
16988 pc = &OMP_CLAUSE_CHAIN (*pc);
16991 if (map_seen != 3)
16993 if (map_seen == 0)
16994 error_at (loc,
16995 "%<#pragma omp target exit data%> must contain at least one "
16996 "%<map%> clause");
16997 return NULL_TREE;
17000 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
17001 TREE_TYPE (stmt) = void_type_node;
17002 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
17003 SET_EXPR_LOCATION (stmt, loc);
17004 add_stmt (stmt);
17005 return stmt;
17008 /* OpenMP 4.0:
17009 # pragma omp target target-clause[optseq] new-line
17010 structured-block */
17012 #define OMP_TARGET_CLAUSE_MASK \
17013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
17018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
17021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17023 static bool
17024 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17026 location_t loc = c_parser_peek_token (parser)->location;
17027 c_parser_consume_pragma (parser);
17028 tree *pc = NULL, stmt, block;
17030 if (context != pragma_stmt && context != pragma_compound)
17032 c_parser_error (parser, "expected declaration specifiers");
17033 c_parser_skip_to_pragma_eol (parser);
17034 return false;
17037 if (c_parser_next_token_is (parser, CPP_NAME))
17039 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17040 enum tree_code ccode = ERROR_MARK;
17042 if (strcmp (p, "teams") == 0)
17043 ccode = OMP_TEAMS;
17044 else if (strcmp (p, "parallel") == 0)
17045 ccode = OMP_PARALLEL;
17046 else if (strcmp (p, "simd") == 0)
17047 ccode = OMP_SIMD;
17048 if (ccode != ERROR_MARK)
17050 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17051 char p_name[sizeof ("#pragma omp target teams distribute "
17052 "parallel for simd")];
17054 c_parser_consume_token (parser);
17055 strcpy (p_name, "#pragma omp target");
17056 if (!flag_openmp) /* flag_openmp_simd */
17058 tree stmt;
17059 switch (ccode)
17061 case OMP_TEAMS:
17062 stmt = c_parser_omp_teams (loc, parser, p_name,
17063 OMP_TARGET_CLAUSE_MASK,
17064 cclauses, if_p);
17065 break;
17066 case OMP_PARALLEL:
17067 stmt = c_parser_omp_parallel (loc, parser, p_name,
17068 OMP_TARGET_CLAUSE_MASK,
17069 cclauses, if_p);
17070 break;
17071 case OMP_SIMD:
17072 stmt = c_parser_omp_simd (loc, parser, p_name,
17073 OMP_TARGET_CLAUSE_MASK,
17074 cclauses, if_p);
17075 break;
17076 default:
17077 gcc_unreachable ();
17079 return stmt != NULL_TREE;
17081 keep_next_level ();
17082 tree block = c_begin_compound_stmt (true), ret;
17083 switch (ccode)
17085 case OMP_TEAMS:
17086 ret = c_parser_omp_teams (loc, parser, p_name,
17087 OMP_TARGET_CLAUSE_MASK, cclauses,
17088 if_p);
17089 break;
17090 case OMP_PARALLEL:
17091 ret = c_parser_omp_parallel (loc, parser, p_name,
17092 OMP_TARGET_CLAUSE_MASK, cclauses,
17093 if_p);
17094 break;
17095 case OMP_SIMD:
17096 ret = c_parser_omp_simd (loc, parser, p_name,
17097 OMP_TARGET_CLAUSE_MASK, cclauses,
17098 if_p);
17099 break;
17100 default:
17101 gcc_unreachable ();
17103 block = c_end_compound_stmt (loc, block, true);
17104 if (ret == NULL_TREE)
17105 return false;
17106 if (ccode == OMP_TEAMS)
17108 /* For combined target teams, ensure the num_teams and
17109 thread_limit clause expressions are evaluated on the host,
17110 before entering the target construct. */
17111 tree c;
17112 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17113 c; c = OMP_CLAUSE_CHAIN (c))
17114 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17115 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17116 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17118 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17119 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17120 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17121 expr, NULL_TREE, NULL_TREE);
17122 add_stmt (expr);
17123 OMP_CLAUSE_OPERAND (c, 0) = expr;
17124 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17125 OMP_CLAUSE_FIRSTPRIVATE);
17126 OMP_CLAUSE_DECL (tc) = tmp;
17127 OMP_CLAUSE_CHAIN (tc)
17128 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17129 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17132 tree stmt = make_node (OMP_TARGET);
17133 TREE_TYPE (stmt) = void_type_node;
17134 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17135 OMP_TARGET_BODY (stmt) = block;
17136 OMP_TARGET_COMBINED (stmt) = 1;
17137 add_stmt (stmt);
17138 pc = &OMP_TARGET_CLAUSES (stmt);
17139 goto check_clauses;
17141 else if (!flag_openmp) /* flag_openmp_simd */
17143 c_parser_skip_to_pragma_eol (parser, false);
17144 return false;
17146 else if (strcmp (p, "data") == 0)
17148 c_parser_consume_token (parser);
17149 c_parser_omp_target_data (loc, parser, if_p);
17150 return true;
17152 else if (strcmp (p, "enter") == 0)
17154 c_parser_consume_token (parser);
17155 c_parser_omp_target_enter_data (loc, parser, context);
17156 return false;
17158 else if (strcmp (p, "exit") == 0)
17160 c_parser_consume_token (parser);
17161 c_parser_omp_target_exit_data (loc, parser, context);
17162 return false;
17164 else if (strcmp (p, "update") == 0)
17166 c_parser_consume_token (parser);
17167 return c_parser_omp_target_update (loc, parser, context);
17170 if (!flag_openmp) /* flag_openmp_simd */
17172 c_parser_skip_to_pragma_eol (parser, false);
17173 return false;
17176 stmt = make_node (OMP_TARGET);
17177 TREE_TYPE (stmt) = void_type_node;
17179 OMP_TARGET_CLAUSES (stmt)
17180 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17181 "#pragma omp target");
17182 pc = &OMP_TARGET_CLAUSES (stmt);
17183 keep_next_level ();
17184 block = c_begin_compound_stmt (true);
17185 add_stmt (c_parser_omp_structured_block (parser, if_p));
17186 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17188 SET_EXPR_LOCATION (stmt, loc);
17189 add_stmt (stmt);
17191 check_clauses:
17192 while (*pc)
17194 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17195 switch (OMP_CLAUSE_MAP_KIND (*pc))
17197 case GOMP_MAP_TO:
17198 case GOMP_MAP_ALWAYS_TO:
17199 case GOMP_MAP_FROM:
17200 case GOMP_MAP_ALWAYS_FROM:
17201 case GOMP_MAP_TOFROM:
17202 case GOMP_MAP_ALWAYS_TOFROM:
17203 case GOMP_MAP_ALLOC:
17204 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17205 case GOMP_MAP_ALWAYS_POINTER:
17206 break;
17207 default:
17208 error_at (OMP_CLAUSE_LOCATION (*pc),
17209 "%<#pragma omp target%> with map-type other "
17210 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17211 "on %<map%> clause");
17212 *pc = OMP_CLAUSE_CHAIN (*pc);
17213 continue;
17215 pc = &OMP_CLAUSE_CHAIN (*pc);
17217 return true;
17220 /* OpenMP 4.0:
17221 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17223 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17224 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17231 static void
17232 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17234 auto_vec<c_token> clauses;
17235 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17237 c_token *token = c_parser_peek_token (parser);
17238 if (token->type == CPP_EOF)
17240 c_parser_skip_to_pragma_eol (parser);
17241 return;
17243 clauses.safe_push (*token);
17244 c_parser_consume_token (parser);
17246 clauses.safe_push (*c_parser_peek_token (parser));
17247 c_parser_skip_to_pragma_eol (parser);
17249 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17251 if (c_parser_peek_token (parser)->pragma_kind
17252 != PRAGMA_OMP_DECLARE
17253 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17254 || strcmp (IDENTIFIER_POINTER
17255 (c_parser_peek_2nd_token (parser)->value),
17256 "simd") != 0)
17258 c_parser_error (parser,
17259 "%<#pragma omp declare simd%> must be followed by "
17260 "function declaration or definition or another "
17261 "%<#pragma omp declare simd%>");
17262 return;
17264 c_parser_consume_pragma (parser);
17265 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17267 c_token *token = c_parser_peek_token (parser);
17268 if (token->type == CPP_EOF)
17270 c_parser_skip_to_pragma_eol (parser);
17271 return;
17273 clauses.safe_push (*token);
17274 c_parser_consume_token (parser);
17276 clauses.safe_push (*c_parser_peek_token (parser));
17277 c_parser_skip_to_pragma_eol (parser);
17280 /* Make sure nothing tries to read past the end of the tokens. */
17281 c_token eof_token;
17282 memset (&eof_token, 0, sizeof (eof_token));
17283 eof_token.type = CPP_EOF;
17284 clauses.safe_push (eof_token);
17285 clauses.safe_push (eof_token);
17287 switch (context)
17289 case pragma_external:
17290 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17291 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17293 int ext = disable_extension_diagnostics ();
17295 c_parser_consume_token (parser);
17296 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17297 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17298 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17299 NULL, clauses);
17300 restore_extension_diagnostics (ext);
17302 else
17303 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17304 NULL, clauses);
17305 break;
17306 case pragma_struct:
17307 case pragma_param:
17308 case pragma_stmt:
17309 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17310 "function declaration or definition");
17311 break;
17312 case pragma_compound:
17313 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17314 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17316 int ext = disable_extension_diagnostics ();
17318 c_parser_consume_token (parser);
17319 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17320 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17321 if (c_parser_next_tokens_start_declaration (parser))
17323 c_parser_declaration_or_fndef (parser, true, true, true, true,
17324 true, NULL, clauses);
17325 restore_extension_diagnostics (ext);
17326 break;
17328 restore_extension_diagnostics (ext);
17330 else if (c_parser_next_tokens_start_declaration (parser))
17332 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17333 NULL, clauses);
17334 break;
17336 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17337 "function declaration or definition");
17338 break;
17339 default:
17340 gcc_unreachable ();
17344 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17345 and put that into "omp declare simd" attribute. */
17347 static void
17348 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17349 vec<c_token> clauses)
17351 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17352 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17353 has already processed the tokens. */
17354 if (clauses.exists () && clauses[0].type == CPP_EOF)
17355 return;
17356 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17358 error ("%<#pragma omp declare simd%> not immediately followed by "
17359 "a function declaration or definition");
17360 clauses[0].type = CPP_EOF;
17361 return;
17363 if (clauses.exists () && clauses[0].type != CPP_NAME)
17365 error_at (DECL_SOURCE_LOCATION (fndecl),
17366 "%<#pragma omp declare simd%> not immediately followed by "
17367 "a single function declaration or definition");
17368 clauses[0].type = CPP_EOF;
17369 return;
17372 if (parms == NULL_TREE)
17373 parms = DECL_ARGUMENTS (fndecl);
17375 unsigned int tokens_avail = parser->tokens_avail;
17376 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17379 parser->tokens = clauses.address ();
17380 parser->tokens_avail = clauses.length ();
17382 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17383 while (parser->tokens_avail > 3)
17385 c_token *token = c_parser_peek_token (parser);
17386 gcc_assert (token->type == CPP_NAME
17387 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17388 c_parser_consume_token (parser);
17389 parser->in_pragma = true;
17391 tree c = NULL_TREE;
17392 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17393 "#pragma omp declare simd");
17394 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17395 if (c != NULL_TREE)
17396 c = tree_cons (NULL_TREE, c, NULL_TREE);
17397 c = build_tree_list (get_identifier ("omp declare simd"), c);
17398 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17399 DECL_ATTRIBUTES (fndecl) = c;
17402 parser->tokens = &parser->tokens_buf[0];
17403 parser->tokens_avail = tokens_avail;
17404 if (clauses.exists ())
17405 clauses[0].type = CPP_PRAGMA;
17409 /* OpenMP 4.0:
17410 # pragma omp declare target new-line
17411 declarations and definitions
17412 # pragma omp end declare target new-line
17414 OpenMP 4.5:
17415 # pragma omp declare target ( extended-list ) new-line
17417 # pragma omp declare target declare-target-clauses[seq] new-line */
17419 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17420 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17423 static void
17424 c_parser_omp_declare_target (c_parser *parser)
17426 location_t loc = c_parser_peek_token (parser)->location;
17427 tree clauses = NULL_TREE;
17428 if (c_parser_next_token_is (parser, CPP_NAME))
17429 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17430 "#pragma omp declare target");
17431 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17433 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17434 clauses);
17435 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17436 c_parser_skip_to_pragma_eol (parser);
17438 else
17440 c_parser_skip_to_pragma_eol (parser);
17441 current_omp_declare_target_attribute++;
17442 return;
17444 if (current_omp_declare_target_attribute)
17445 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17446 "%<#pragma omp declare target%> without clauses and "
17447 "%<#pragma omp end declare target%>");
17448 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17450 tree t = OMP_CLAUSE_DECL (c), id;
17451 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17452 tree at2 = lookup_attribute ("omp declare target link",
17453 DECL_ATTRIBUTES (t));
17454 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17456 id = get_identifier ("omp declare target link");
17457 std::swap (at1, at2);
17459 else
17460 id = get_identifier ("omp declare target");
17461 if (at2)
17463 error_at (OMP_CLAUSE_LOCATION (c),
17464 "%qD specified both in declare target %<link%> and %<to%>"
17465 " clauses", t);
17466 continue;
17468 if (!at1)
17470 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17471 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17472 continue;
17474 symtab_node *node = symtab_node::get (t);
17475 if (node != NULL)
17477 node->offloadable = 1;
17478 if (ENABLE_OFFLOADING)
17480 g->have_offload = true;
17481 if (is_a <varpool_node *> (node))
17482 vec_safe_push (offload_vars, t);
17489 static void
17490 c_parser_omp_end_declare_target (c_parser *parser)
17492 location_t loc = c_parser_peek_token (parser)->location;
17493 c_parser_consume_pragma (parser);
17494 if (c_parser_next_token_is (parser, CPP_NAME)
17495 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17496 "declare") == 0)
17498 c_parser_consume_token (parser);
17499 if (c_parser_next_token_is (parser, CPP_NAME)
17500 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17501 "target") == 0)
17502 c_parser_consume_token (parser);
17503 else
17505 c_parser_error (parser, "expected %<target%>");
17506 c_parser_skip_to_pragma_eol (parser);
17507 return;
17510 else
17512 c_parser_error (parser, "expected %<declare%>");
17513 c_parser_skip_to_pragma_eol (parser);
17514 return;
17516 c_parser_skip_to_pragma_eol (parser);
17517 if (!current_omp_declare_target_attribute)
17518 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17519 "%<#pragma omp declare target%>");
17520 else
17521 current_omp_declare_target_attribute--;
17525 /* OpenMP 4.0
17526 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17527 initializer-clause[opt] new-line
17529 initializer-clause:
17530 initializer (omp_priv = initializer)
17531 initializer (function-name (argument-list)) */
17533 static void
17534 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17536 unsigned int tokens_avail = 0, i;
17537 vec<tree> types = vNULL;
17538 vec<c_token> clauses = vNULL;
17539 enum tree_code reduc_code = ERROR_MARK;
17540 tree reduc_id = NULL_TREE;
17541 tree type;
17542 location_t rloc = c_parser_peek_token (parser)->location;
17544 if (context == pragma_struct || context == pragma_param)
17546 error ("%<#pragma omp declare reduction%> not at file or block scope");
17547 goto fail;
17550 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17551 goto fail;
17553 switch (c_parser_peek_token (parser)->type)
17555 case CPP_PLUS:
17556 reduc_code = PLUS_EXPR;
17557 break;
17558 case CPP_MULT:
17559 reduc_code = MULT_EXPR;
17560 break;
17561 case CPP_MINUS:
17562 reduc_code = MINUS_EXPR;
17563 break;
17564 case CPP_AND:
17565 reduc_code = BIT_AND_EXPR;
17566 break;
17567 case CPP_XOR:
17568 reduc_code = BIT_XOR_EXPR;
17569 break;
17570 case CPP_OR:
17571 reduc_code = BIT_IOR_EXPR;
17572 break;
17573 case CPP_AND_AND:
17574 reduc_code = TRUTH_ANDIF_EXPR;
17575 break;
17576 case CPP_OR_OR:
17577 reduc_code = TRUTH_ORIF_EXPR;
17578 break;
17579 case CPP_NAME:
17580 const char *p;
17581 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17582 if (strcmp (p, "min") == 0)
17584 reduc_code = MIN_EXPR;
17585 break;
17587 if (strcmp (p, "max") == 0)
17589 reduc_code = MAX_EXPR;
17590 break;
17592 reduc_id = c_parser_peek_token (parser)->value;
17593 break;
17594 default:
17595 c_parser_error (parser,
17596 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17597 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17598 goto fail;
17601 tree orig_reduc_id, reduc_decl;
17602 orig_reduc_id = reduc_id;
17603 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17604 reduc_decl = c_omp_reduction_decl (reduc_id);
17605 c_parser_consume_token (parser);
17607 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17608 goto fail;
17610 while (true)
17612 location_t loc = c_parser_peek_token (parser)->location;
17613 struct c_type_name *ctype = c_parser_type_name (parser);
17614 if (ctype != NULL)
17616 type = groktypename (ctype, NULL, NULL);
17617 if (type == error_mark_node)
17619 else if ((INTEGRAL_TYPE_P (type)
17620 || TREE_CODE (type) == REAL_TYPE
17621 || TREE_CODE (type) == COMPLEX_TYPE)
17622 && orig_reduc_id == NULL_TREE)
17623 error_at (loc, "predeclared arithmetic type in "
17624 "%<#pragma omp declare reduction%>");
17625 else if (TREE_CODE (type) == FUNCTION_TYPE
17626 || TREE_CODE (type) == ARRAY_TYPE)
17627 error_at (loc, "function or array type in "
17628 "%<#pragma omp declare reduction%>");
17629 else if (TYPE_ATOMIC (type))
17630 error_at (loc, "%<_Atomic%> qualified type in "
17631 "%<#pragma omp declare reduction%>");
17632 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17633 error_at (loc, "const, volatile or restrict qualified type in "
17634 "%<#pragma omp declare reduction%>");
17635 else
17637 tree t;
17638 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17639 if (comptypes (TREE_PURPOSE (t), type))
17641 error_at (loc, "redeclaration of %qs "
17642 "%<#pragma omp declare reduction%> for "
17643 "type %qT",
17644 IDENTIFIER_POINTER (reduc_id)
17645 + sizeof ("omp declare reduction ") - 1,
17646 type);
17647 location_t ploc
17648 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17649 0));
17650 error_at (ploc, "previous %<#pragma omp declare "
17651 "reduction%>");
17652 break;
17654 if (t == NULL_TREE)
17655 types.safe_push (type);
17657 if (c_parser_next_token_is (parser, CPP_COMMA))
17658 c_parser_consume_token (parser);
17659 else
17660 break;
17662 else
17663 break;
17666 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17667 || types.is_empty ())
17669 fail:
17670 clauses.release ();
17671 types.release ();
17672 while (true)
17674 c_token *token = c_parser_peek_token (parser);
17675 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17676 break;
17677 c_parser_consume_token (parser);
17679 c_parser_skip_to_pragma_eol (parser);
17680 return;
17683 if (types.length () > 1)
17685 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17687 c_token *token = c_parser_peek_token (parser);
17688 if (token->type == CPP_EOF)
17689 goto fail;
17690 clauses.safe_push (*token);
17691 c_parser_consume_token (parser);
17693 clauses.safe_push (*c_parser_peek_token (parser));
17694 c_parser_skip_to_pragma_eol (parser);
17696 /* Make sure nothing tries to read past the end of the tokens. */
17697 c_token eof_token;
17698 memset (&eof_token, 0, sizeof (eof_token));
17699 eof_token.type = CPP_EOF;
17700 clauses.safe_push (eof_token);
17701 clauses.safe_push (eof_token);
17704 int errs = errorcount;
17705 FOR_EACH_VEC_ELT (types, i, type)
17707 tokens_avail = parser->tokens_avail;
17708 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17709 if (!clauses.is_empty ())
17711 parser->tokens = clauses.address ();
17712 parser->tokens_avail = clauses.length ();
17713 parser->in_pragma = true;
17716 bool nested = current_function_decl != NULL_TREE;
17717 if (nested)
17718 c_push_function_context ();
17719 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17720 reduc_id, default_function_type);
17721 current_function_decl = fndecl;
17722 allocate_struct_function (fndecl, true);
17723 push_scope ();
17724 tree stmt = push_stmt_list ();
17725 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17726 warn about these. */
17727 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17728 get_identifier ("omp_out"), type);
17729 DECL_ARTIFICIAL (omp_out) = 1;
17730 DECL_CONTEXT (omp_out) = fndecl;
17731 pushdecl (omp_out);
17732 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17733 get_identifier ("omp_in"), type);
17734 DECL_ARTIFICIAL (omp_in) = 1;
17735 DECL_CONTEXT (omp_in) = fndecl;
17736 pushdecl (omp_in);
17737 struct c_expr combiner = c_parser_expression (parser);
17738 struct c_expr initializer;
17739 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17740 bool bad = false;
17741 initializer.set_error ();
17742 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17743 bad = true;
17744 else if (c_parser_next_token_is (parser, CPP_NAME)
17745 && strcmp (IDENTIFIER_POINTER
17746 (c_parser_peek_token (parser)->value),
17747 "initializer") == 0)
17749 c_parser_consume_token (parser);
17750 pop_scope ();
17751 push_scope ();
17752 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17753 get_identifier ("omp_priv"), type);
17754 DECL_ARTIFICIAL (omp_priv) = 1;
17755 DECL_INITIAL (omp_priv) = error_mark_node;
17756 DECL_CONTEXT (omp_priv) = fndecl;
17757 pushdecl (omp_priv);
17758 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17759 get_identifier ("omp_orig"), type);
17760 DECL_ARTIFICIAL (omp_orig) = 1;
17761 DECL_CONTEXT (omp_orig) = fndecl;
17762 pushdecl (omp_orig);
17763 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17764 bad = true;
17765 else if (!c_parser_next_token_is (parser, CPP_NAME))
17767 c_parser_error (parser, "expected %<omp_priv%> or "
17768 "function-name");
17769 bad = true;
17771 else if (strcmp (IDENTIFIER_POINTER
17772 (c_parser_peek_token (parser)->value),
17773 "omp_priv") != 0)
17775 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17776 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17778 c_parser_error (parser, "expected function-name %<(%>");
17779 bad = true;
17781 else
17782 initializer = c_parser_postfix_expression (parser);
17783 if (initializer.value
17784 && TREE_CODE (initializer.value) == CALL_EXPR)
17786 int j;
17787 tree c = initializer.value;
17788 for (j = 0; j < call_expr_nargs (c); j++)
17790 tree a = CALL_EXPR_ARG (c, j);
17791 STRIP_NOPS (a);
17792 if (TREE_CODE (a) == ADDR_EXPR
17793 && TREE_OPERAND (a, 0) == omp_priv)
17794 break;
17796 if (j == call_expr_nargs (c))
17797 error ("one of the initializer call arguments should be "
17798 "%<&omp_priv%>");
17801 else
17803 c_parser_consume_token (parser);
17804 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17805 bad = true;
17806 else
17808 tree st = push_stmt_list ();
17809 location_t loc = c_parser_peek_token (parser)->location;
17810 rich_location richloc (line_table, loc);
17811 start_init (omp_priv, NULL_TREE, 0, &richloc);
17812 struct c_expr init = c_parser_initializer (parser);
17813 finish_init ();
17814 finish_decl (omp_priv, loc, init.value,
17815 init.original_type, NULL_TREE);
17816 pop_stmt_list (st);
17819 if (!bad
17820 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17821 bad = true;
17824 if (!bad)
17826 c_parser_skip_to_pragma_eol (parser);
17828 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17829 DECL_INITIAL (reduc_decl));
17830 DECL_INITIAL (reduc_decl) = t;
17831 DECL_SOURCE_LOCATION (omp_out) = rloc;
17832 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17833 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17834 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17835 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17836 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17837 if (omp_priv)
17839 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17840 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17841 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17842 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17843 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17844 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17845 walk_tree (&DECL_INITIAL (omp_priv),
17846 c_check_omp_declare_reduction_r,
17847 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17851 pop_stmt_list (stmt);
17852 pop_scope ();
17853 if (cfun->language != NULL)
17855 ggc_free (cfun->language);
17856 cfun->language = NULL;
17858 set_cfun (NULL);
17859 current_function_decl = NULL_TREE;
17860 if (nested)
17861 c_pop_function_context ();
17863 if (!clauses.is_empty ())
17865 parser->tokens = &parser->tokens_buf[0];
17866 parser->tokens_avail = tokens_avail;
17868 if (bad)
17869 goto fail;
17870 if (errs != errorcount)
17871 break;
17874 clauses.release ();
17875 types.release ();
17879 /* OpenMP 4.0
17880 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17881 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17882 initializer-clause[opt] new-line
17883 #pragma omp declare target new-line */
17885 static void
17886 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17888 c_parser_consume_pragma (parser);
17889 if (c_parser_next_token_is (parser, CPP_NAME))
17891 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17892 if (strcmp (p, "simd") == 0)
17894 /* c_parser_consume_token (parser); done in
17895 c_parser_omp_declare_simd. */
17896 c_parser_omp_declare_simd (parser, context);
17897 return;
17899 if (strcmp (p, "reduction") == 0)
17901 c_parser_consume_token (parser);
17902 c_parser_omp_declare_reduction (parser, context);
17903 return;
17905 if (!flag_openmp) /* flag_openmp_simd */
17907 c_parser_skip_to_pragma_eol (parser, false);
17908 return;
17910 if (strcmp (p, "target") == 0)
17912 c_parser_consume_token (parser);
17913 c_parser_omp_declare_target (parser);
17914 return;
17918 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17919 "or %<target%>");
17920 c_parser_skip_to_pragma_eol (parser);
17923 /* OpenMP 4.5:
17924 #pragma omp taskloop taskloop-clause[optseq] new-line
17925 for-loop
17927 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17928 for-loop */
17930 #define OMP_TASKLOOP_CLAUSE_MASK \
17931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17946 static tree
17947 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17948 char *p_name, omp_clause_mask mask, tree *cclauses,
17949 bool *if_p)
17951 tree clauses, block, ret;
17953 strcat (p_name, " taskloop");
17954 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17956 if (c_parser_next_token_is (parser, CPP_NAME))
17958 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17960 if (strcmp (p, "simd") == 0)
17962 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17963 if (cclauses == NULL)
17964 cclauses = cclauses_buf;
17965 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17966 c_parser_consume_token (parser);
17967 if (!flag_openmp) /* flag_openmp_simd */
17968 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17969 if_p);
17970 block = c_begin_compound_stmt (true);
17971 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17972 block = c_end_compound_stmt (loc, block, true);
17973 if (ret == NULL)
17974 return ret;
17975 ret = make_node (OMP_TASKLOOP);
17976 TREE_TYPE (ret) = void_type_node;
17977 OMP_FOR_BODY (ret) = block;
17978 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17979 SET_EXPR_LOCATION (ret, loc);
17980 add_stmt (ret);
17981 return ret;
17984 if (!flag_openmp) /* flag_openmp_simd */
17986 c_parser_skip_to_pragma_eol (parser, false);
17987 return NULL_TREE;
17990 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17991 if (cclauses)
17993 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17994 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17997 block = c_begin_compound_stmt (true);
17998 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17999 block = c_end_compound_stmt (loc, block, true);
18000 add_stmt (block);
18002 return ret;
18005 /* Main entry point to parsing most OpenMP pragmas. */
18007 static void
18008 c_parser_omp_construct (c_parser *parser, bool *if_p)
18010 enum pragma_kind p_kind;
18011 location_t loc;
18012 tree stmt;
18013 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18014 omp_clause_mask mask (0);
18016 loc = c_parser_peek_token (parser)->location;
18017 p_kind = c_parser_peek_token (parser)->pragma_kind;
18018 c_parser_consume_pragma (parser);
18020 switch (p_kind)
18022 case PRAGMA_OACC_ATOMIC:
18023 c_parser_omp_atomic (loc, parser);
18024 return;
18025 case PRAGMA_OACC_CACHE:
18026 strcpy (p_name, "#pragma acc");
18027 stmt = c_parser_oacc_cache (loc, parser);
18028 break;
18029 case PRAGMA_OACC_DATA:
18030 stmt = c_parser_oacc_data (loc, parser, if_p);
18031 break;
18032 case PRAGMA_OACC_HOST_DATA:
18033 stmt = c_parser_oacc_host_data (loc, parser, if_p);
18034 break;
18035 case PRAGMA_OACC_KERNELS:
18036 case PRAGMA_OACC_PARALLEL:
18037 strcpy (p_name, "#pragma acc");
18038 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18039 if_p);
18040 break;
18041 case PRAGMA_OACC_LOOP:
18042 strcpy (p_name, "#pragma acc");
18043 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18044 break;
18045 case PRAGMA_OACC_WAIT:
18046 strcpy (p_name, "#pragma wait");
18047 stmt = c_parser_oacc_wait (loc, parser, p_name);
18048 break;
18049 case PRAGMA_OMP_ATOMIC:
18050 c_parser_omp_atomic (loc, parser);
18051 return;
18052 case PRAGMA_OMP_CRITICAL:
18053 stmt = c_parser_omp_critical (loc, parser, if_p);
18054 break;
18055 case PRAGMA_OMP_DISTRIBUTE:
18056 strcpy (p_name, "#pragma omp");
18057 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18058 break;
18059 case PRAGMA_OMP_FOR:
18060 strcpy (p_name, "#pragma omp");
18061 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18062 break;
18063 case PRAGMA_OMP_MASTER:
18064 stmt = c_parser_omp_master (loc, parser, if_p);
18065 break;
18066 case PRAGMA_OMP_PARALLEL:
18067 strcpy (p_name, "#pragma omp");
18068 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18069 break;
18070 case PRAGMA_OMP_SECTIONS:
18071 strcpy (p_name, "#pragma omp");
18072 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18073 break;
18074 case PRAGMA_OMP_SIMD:
18075 strcpy (p_name, "#pragma omp");
18076 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18077 break;
18078 case PRAGMA_OMP_SINGLE:
18079 stmt = c_parser_omp_single (loc, parser, if_p);
18080 break;
18081 case PRAGMA_OMP_TASK:
18082 stmt = c_parser_omp_task (loc, parser, if_p);
18083 break;
18084 case PRAGMA_OMP_TASKGROUP:
18085 stmt = c_parser_omp_taskgroup (parser, if_p);
18086 break;
18087 case PRAGMA_OMP_TASKLOOP:
18088 strcpy (p_name, "#pragma omp");
18089 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18090 break;
18091 case PRAGMA_OMP_TEAMS:
18092 strcpy (p_name, "#pragma omp");
18093 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18094 break;
18095 default:
18096 gcc_unreachable ();
18099 if (stmt)
18100 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18104 /* OpenMP 2.5:
18105 # pragma omp threadprivate (variable-list) */
18107 static void
18108 c_parser_omp_threadprivate (c_parser *parser)
18110 tree vars, t;
18111 location_t loc;
18113 c_parser_consume_pragma (parser);
18114 loc = c_parser_peek_token (parser)->location;
18115 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18117 /* Mark every variable in VARS to be assigned thread local storage. */
18118 for (t = vars; t; t = TREE_CHAIN (t))
18120 tree v = TREE_PURPOSE (t);
18122 /* FIXME diagnostics: Ideally we should keep individual
18123 locations for all the variables in the var list to make the
18124 following errors more precise. Perhaps
18125 c_parser_omp_var_list_parens() should construct a list of
18126 locations to go along with the var list. */
18128 /* If V had already been marked threadprivate, it doesn't matter
18129 whether it had been used prior to this point. */
18130 if (!VAR_P (v))
18131 error_at (loc, "%qD is not a variable", v);
18132 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18133 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18134 else if (! is_global_var (v))
18135 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18136 else if (TREE_TYPE (v) == error_mark_node)
18138 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18139 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18140 else
18142 if (! DECL_THREAD_LOCAL_P (v))
18144 set_decl_tls_model (v, decl_default_tls_model (v));
18145 /* If rtl has been already set for this var, call
18146 make_decl_rtl once again, so that encode_section_info
18147 has a chance to look at the new decl flags. */
18148 if (DECL_RTL_SET_P (v))
18149 make_decl_rtl (v);
18151 C_DECL_THREADPRIVATE_P (v) = 1;
18155 c_parser_skip_to_pragma_eol (parser);
18158 /* Parse a transaction attribute (GCC Extension).
18160 transaction-attribute:
18161 attributes
18162 [ [ any-word ] ]
18164 The transactional memory language description is written for C++,
18165 and uses the C++0x attribute syntax. For compatibility, allow the
18166 bracket style for transactions in C as well. */
18168 static tree
18169 c_parser_transaction_attributes (c_parser *parser)
18171 tree attr_name, attr = NULL;
18173 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18174 return c_parser_attributes (parser);
18176 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18177 return NULL_TREE;
18178 c_parser_consume_token (parser);
18179 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18180 goto error1;
18182 attr_name = c_parser_attribute_any_word (parser);
18183 if (attr_name)
18185 c_parser_consume_token (parser);
18186 attr = build_tree_list (attr_name, NULL_TREE);
18188 else
18189 c_parser_error (parser, "expected identifier");
18191 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18192 error1:
18193 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18194 return attr;
18197 /* Parse a __transaction_atomic or __transaction_relaxed statement
18198 (GCC Extension).
18200 transaction-statement:
18201 __transaction_atomic transaction-attribute[opt] compound-statement
18202 __transaction_relaxed compound-statement
18204 Note that the only valid attribute is: "outer".
18207 static tree
18208 c_parser_transaction (c_parser *parser, enum rid keyword)
18210 unsigned int old_in = parser->in_transaction;
18211 unsigned int this_in = 1, new_in;
18212 location_t loc = c_parser_peek_token (parser)->location;
18213 tree stmt, attrs;
18215 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18216 || keyword == RID_TRANSACTION_RELAXED)
18217 && c_parser_next_token_is_keyword (parser, keyword));
18218 c_parser_consume_token (parser);
18220 if (keyword == RID_TRANSACTION_RELAXED)
18221 this_in |= TM_STMT_ATTR_RELAXED;
18222 else
18224 attrs = c_parser_transaction_attributes (parser);
18225 if (attrs)
18226 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18229 /* Keep track if we're in the lexical scope of an outer transaction. */
18230 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18232 parser->in_transaction = new_in;
18233 stmt = c_parser_compound_statement (parser);
18234 parser->in_transaction = old_in;
18236 if (flag_tm)
18237 stmt = c_finish_transaction (loc, stmt, this_in);
18238 else
18239 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18240 "%<__transaction_atomic%> without transactional memory support enabled"
18241 : "%<__transaction_relaxed %> "
18242 "without transactional memory support enabled"));
18244 return stmt;
18247 /* Parse a __transaction_atomic or __transaction_relaxed expression
18248 (GCC Extension).
18250 transaction-expression:
18251 __transaction_atomic ( expression )
18252 __transaction_relaxed ( expression )
18255 static struct c_expr
18256 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18258 struct c_expr ret;
18259 unsigned int old_in = parser->in_transaction;
18260 unsigned int this_in = 1;
18261 location_t loc = c_parser_peek_token (parser)->location;
18262 tree attrs;
18264 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18265 || keyword == RID_TRANSACTION_RELAXED)
18266 && c_parser_next_token_is_keyword (parser, keyword));
18267 c_parser_consume_token (parser);
18269 if (keyword == RID_TRANSACTION_RELAXED)
18270 this_in |= TM_STMT_ATTR_RELAXED;
18271 else
18273 attrs = c_parser_transaction_attributes (parser);
18274 if (attrs)
18275 this_in |= parse_tm_stmt_attr (attrs, 0);
18278 parser->in_transaction = this_in;
18279 matching_parens parens;
18280 if (parens.require_open (parser))
18282 tree expr = c_parser_expression (parser).value;
18283 ret.original_type = TREE_TYPE (expr);
18284 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18285 if (this_in & TM_STMT_ATTR_RELAXED)
18286 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18287 SET_EXPR_LOCATION (ret.value, loc);
18288 ret.original_code = TRANSACTION_EXPR;
18289 if (!parens.require_close (parser))
18291 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18292 goto error;
18295 else
18297 error:
18298 ret.set_error ();
18299 ret.original_code = ERROR_MARK;
18300 ret.original_type = NULL;
18302 parser->in_transaction = old_in;
18304 if (!flag_tm)
18305 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18306 "%<__transaction_atomic%> without transactional memory support enabled"
18307 : "%<__transaction_relaxed %> "
18308 "without transactional memory support enabled"));
18310 set_c_expr_source_range (&ret, loc, loc);
18312 return ret;
18315 /* Parse a __transaction_cancel statement (GCC Extension).
18317 transaction-cancel-statement:
18318 __transaction_cancel transaction-attribute[opt] ;
18320 Note that the only valid attribute is "outer".
18323 static tree
18324 c_parser_transaction_cancel (c_parser *parser)
18326 location_t loc = c_parser_peek_token (parser)->location;
18327 tree attrs;
18328 bool is_outer = false;
18330 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18331 c_parser_consume_token (parser);
18333 attrs = c_parser_transaction_attributes (parser);
18334 if (attrs)
18335 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18337 if (!flag_tm)
18339 error_at (loc, "%<__transaction_cancel%> without "
18340 "transactional memory support enabled");
18341 goto ret_error;
18343 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18345 error_at (loc, "%<__transaction_cancel%> within a "
18346 "%<__transaction_relaxed%>");
18347 goto ret_error;
18349 else if (is_outer)
18351 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18352 && !is_tm_may_cancel_outer (current_function_decl))
18354 error_at (loc, "outer %<__transaction_cancel%> not "
18355 "within outer %<__transaction_atomic%>");
18356 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18357 goto ret_error;
18360 else if (parser->in_transaction == 0)
18362 error_at (loc, "%<__transaction_cancel%> not within "
18363 "%<__transaction_atomic%>");
18364 goto ret_error;
18367 return add_stmt (build_tm_abort_call (loc, is_outer));
18369 ret_error:
18370 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18373 /* Parse a single source file. */
18375 void
18376 c_parse_file (void)
18378 /* Use local storage to begin. If the first token is a pragma, parse it.
18379 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18380 which will cause garbage collection. */
18381 c_parser tparser;
18383 memset (&tparser, 0, sizeof tparser);
18384 tparser.tokens = &tparser.tokens_buf[0];
18385 the_parser = &tparser;
18387 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18388 c_parser_pragma_pch_preprocess (&tparser);
18390 the_parser = ggc_alloc<c_parser> ();
18391 *the_parser = tparser;
18392 if (tparser.tokens == &tparser.tokens_buf[0])
18393 the_parser->tokens = &the_parser->tokens_buf[0];
18395 /* Initialize EH, if we've been told to do so. */
18396 if (flag_exceptions)
18397 using_eh_for_cleanups ();
18399 c_parser_translation_unit (the_parser);
18400 the_parser = NULL;
18403 /* Parse the body of a function declaration marked with "__RTL".
18405 The RTL parser works on the level of characters read from a
18406 FILE *, whereas c_parser works at the level of tokens.
18407 Square this circle by consuming all of the tokens up to and
18408 including the closing brace, recording the start/end of the RTL
18409 fragment, and reopening the file and re-reading the relevant
18410 lines within the RTL parser.
18412 This requires the opening and closing braces of the C function
18413 to be on separate lines from the RTL they wrap.
18415 Take ownership of START_WITH_PASS, if non-NULL. */
18417 void
18418 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18420 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18422 free (start_with_pass);
18423 return;
18426 location_t start_loc = c_parser_peek_token (parser)->location;
18428 /* Consume all tokens, up to the closing brace, handling
18429 matching pairs of braces in the rtl dump. */
18430 int num_open_braces = 1;
18431 while (1)
18433 switch (c_parser_peek_token (parser)->type)
18435 case CPP_OPEN_BRACE:
18436 num_open_braces++;
18437 break;
18438 case CPP_CLOSE_BRACE:
18439 if (--num_open_braces == 0)
18440 goto found_closing_brace;
18441 break;
18442 case CPP_EOF:
18443 error_at (start_loc, "no closing brace");
18444 free (start_with_pass);
18445 return;
18446 default:
18447 break;
18449 c_parser_consume_token (parser);
18452 found_closing_brace:
18453 /* At the closing brace; record its location. */
18454 location_t end_loc = c_parser_peek_token (parser)->location;
18456 /* Consume the closing brace. */
18457 c_parser_consume_token (parser);
18459 /* Invoke the RTL parser. */
18460 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18462 free (start_with_pass);
18463 return;
18466 /* If a pass name was provided for START_WITH_PASS, run the backend
18467 accordingly now, on the cfun created above, transferring
18468 ownership of START_WITH_PASS. */
18469 if (start_with_pass)
18470 run_rtl_passes (start_with_pass);
18473 #include "gt-c-c-parser.h"