2017-12-01 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c / c-parser.c
blobe9267fe9cc107e84990235ed16a004cd022f0997
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
72 /* We need to walk over decls with incomplete struct/union/enum types
73 after parsing the whole translation unit.
74 In finish_decl(), if the decl is static, has incomplete
75 struct/union/enum type, it is appeneded to incomplete_record_decls.
76 In c_parser_translation_unit(), we iterate over incomplete_record_decls
77 and report error if any of the decls are still incomplete. */
79 vec<tree> incomplete_record_decls;
81 void
82 set_c_expr_source_range (c_expr *expr,
83 location_t start, location_t finish)
85 expr->src_range.m_start = start;
86 expr->src_range.m_finish = finish;
87 if (expr->value)
88 set_source_range (expr->value, start, finish);
91 void
92 set_c_expr_source_range (c_expr *expr,
93 source_range src_range)
95 expr->src_range = src_range;
96 if (expr->value)
97 set_source_range (expr->value, src_range);
101 /* Initialization routine for this file. */
103 void
104 c_parse_init (void)
106 /* The only initialization required is of the reserved word
107 identifiers. */
108 unsigned int i;
109 tree id;
110 int mask = 0;
112 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
113 the c_token structure. */
114 gcc_assert (RID_MAX <= 255);
116 mask |= D_CXXONLY;
117 if (!flag_isoc99)
118 mask |= D_C99;
119 if (flag_no_asm)
121 mask |= D_ASM | D_EXT;
122 if (!flag_isoc99)
123 mask |= D_EXT89;
125 if (!c_dialect_objc ())
126 mask |= D_OBJC | D_CXX_OBJC;
128 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
129 for (i = 0; i < num_c_common_reswords; i++)
131 /* If a keyword is disabled, do not enter it into the table
132 and so create a canonical spelling that isn't a keyword. */
133 if (c_common_reswords[i].disable & mask)
135 if (warn_cxx_compat
136 && (c_common_reswords[i].disable & D_CXXWARN))
138 id = get_identifier (c_common_reswords[i].word);
139 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
140 C_IS_RESERVED_WORD (id) = 1;
142 continue;
145 id = get_identifier (c_common_reswords[i].word);
146 C_SET_RID_CODE (id, c_common_reswords[i].rid);
147 C_IS_RESERVED_WORD (id) = 1;
148 ridpointers [(int) c_common_reswords[i].rid] = id;
151 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 /* We always create the symbols but they aren't always supported. */
154 char name[50];
155 sprintf (name, "__int%d", int_n_data[i].bitsize);
156 id = get_identifier (name);
157 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
158 C_IS_RESERVED_WORD (id) = 1;
162 /* A parser structure recording information about the state and
163 context of parsing. Includes lexer information with up to two
164 tokens of look-ahead; more are not needed for C. */
165 struct GTY(()) c_parser {
166 /* The look-ahead tokens. */
167 c_token * GTY((skip)) tokens;
168 /* Buffer for look-ahead tokens. */
169 c_token tokens_buf[4];
170 /* How many look-ahead tokens are available (0 - 4, or
171 more if parsing from pre-lexed tokens). */
172 unsigned int tokens_avail;
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
185 /* Objective-C specific parser/lexer information. */
187 /* True if we are in a context where the Objective-C "PQ" keywords
188 are considered keywords. */
189 BOOL_BITFIELD objc_pq_context : 1;
190 /* True if we are parsing a (potential) Objective-C foreach
191 statement. This is set to true after we parsed 'for (' and while
192 we wait for 'in' or ';' to decide if it's a standard C for loop or an
193 Objective-C foreach loop. */
194 BOOL_BITFIELD objc_could_be_foreach_context : 1;
195 /* The following flag is needed to contextualize Objective-C lexical
196 analysis. In some cases (e.g., 'int NSObject;'), it is
197 undesirable to bind an identifier to an Objective-C class, even
198 if a class with that name exists. */
199 BOOL_BITFIELD objc_need_raw_identifier : 1;
200 /* Nonzero if we're processing a __transaction statement. The value
201 is 1 | TM_STMT_ATTR_*. */
202 unsigned int in_transaction : 4;
203 /* True if we are in a context where the Objective-C "Property attribute"
204 keywords are valid. */
205 BOOL_BITFIELD objc_property_attr_context : 1;
207 /* Location of the last consumed token. */
208 location_t last_token_location;
211 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
213 c_token *
214 c_parser_tokens_buf (c_parser *parser, unsigned n)
216 return &parser->tokens_buf[n];
219 /* Return the error state of PARSER. */
221 bool
222 c_parser_error (c_parser *parser)
224 return parser->error;
227 /* Set the error state of PARSER to ERR. */
229 void
230 c_parser_set_error (c_parser *parser, bool err)
232 parser->error = err;
236 /* The actual parser and external interface. ??? Does this need to be
237 garbage-collected? */
239 static GTY (()) c_parser *the_parser;
241 /* Read in and lex a single token, storing it in *TOKEN. */
243 static void
244 c_lex_one_token (c_parser *parser, c_token *token)
246 timevar_push (TV_LEX);
248 token->type = c_lex_with_flags (&token->value, &token->location,
249 &token->flags,
250 (parser->lex_untranslated_string
251 ? C_LEX_STRING_NO_TRANSLATE : 0));
252 token->id_kind = C_ID_NONE;
253 token->keyword = RID_MAX;
254 token->pragma_kind = PRAGMA_NONE;
256 switch (token->type)
258 case CPP_NAME:
260 tree decl;
262 bool objc_force_identifier = parser->objc_need_raw_identifier;
263 if (c_dialect_objc ())
264 parser->objc_need_raw_identifier = false;
266 if (C_IS_RESERVED_WORD (token->value))
268 enum rid rid_code = C_RID_CODE (token->value);
270 if (rid_code == RID_CXX_COMPAT_WARN)
272 warning_at (token->location,
273 OPT_Wc___compat,
274 "identifier %qE conflicts with C++ keyword",
275 token->value);
277 else if (rid_code >= RID_FIRST_ADDR_SPACE
278 && rid_code <= RID_LAST_ADDR_SPACE)
280 addr_space_t as;
281 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
282 targetm.addr_space.diagnose_usage (as, token->location);
283 token->id_kind = C_ID_ADDRSPACE;
284 token->keyword = rid_code;
285 break;
287 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
289 /* We found an Objective-C "pq" keyword (in, out,
290 inout, bycopy, byref, oneway). They need special
291 care because the interpretation depends on the
292 context. */
293 if (parser->objc_pq_context)
295 token->type = CPP_KEYWORD;
296 token->keyword = rid_code;
297 break;
299 else if (parser->objc_could_be_foreach_context
300 && rid_code == RID_IN)
302 /* We are in Objective-C, inside a (potential)
303 foreach context (which means after having
304 parsed 'for (', but before having parsed ';'),
305 and we found 'in'. We consider it the keyword
306 which terminates the declaration at the
307 beginning of a foreach-statement. Note that
308 this means you can't use 'in' for anything else
309 in that context; in particular, in Objective-C
310 you can't use 'in' as the name of the running
311 variable in a C for loop. We could potentially
312 try to add code here to disambiguate, but it
313 seems a reasonable limitation. */
314 token->type = CPP_KEYWORD;
315 token->keyword = rid_code;
316 break;
318 /* Else, "pq" keywords outside of the "pq" context are
319 not keywords, and we fall through to the code for
320 normal tokens. */
322 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
324 /* We found an Objective-C "property attribute"
325 keyword (getter, setter, readonly, etc). These are
326 only valid in the property context. */
327 if (parser->objc_property_attr_context)
329 token->type = CPP_KEYWORD;
330 token->keyword = rid_code;
331 break;
333 /* Else they are not special keywords.
336 else if (c_dialect_objc ()
337 && (OBJC_IS_AT_KEYWORD (rid_code)
338 || OBJC_IS_CXX_KEYWORD (rid_code)))
340 /* We found one of the Objective-C "@" keywords (defs,
341 selector, synchronized, etc) or one of the
342 Objective-C "cxx" keywords (class, private,
343 protected, public, try, catch, throw) without a
344 preceding '@' sign. Do nothing and fall through to
345 the code for normal tokens (in C++ we would still
346 consider the CXX ones keywords, but not in C). */
349 else
351 token->type = CPP_KEYWORD;
352 token->keyword = rid_code;
353 break;
357 decl = lookup_name (token->value);
358 if (decl)
360 if (TREE_CODE (decl) == TYPE_DECL)
362 token->id_kind = C_ID_TYPENAME;
363 break;
366 else if (c_dialect_objc ())
368 tree objc_interface_decl = objc_is_class_name (token->value);
369 /* Objective-C class names are in the same namespace as
370 variables and typedefs, and hence are shadowed by local
371 declarations. */
372 if (objc_interface_decl
373 && (!objc_force_identifier || global_bindings_p ()))
375 token->value = objc_interface_decl;
376 token->id_kind = C_ID_CLASSNAME;
377 break;
380 token->id_kind = C_ID_ID;
382 break;
383 case CPP_AT_NAME:
384 /* This only happens in Objective-C; it must be a keyword. */
385 token->type = CPP_KEYWORD;
386 switch (C_RID_CODE (token->value))
388 /* Replace 'class' with '@class', 'private' with '@private',
389 etc. This prevents confusion with the C++ keyword
390 'class', and makes the tokens consistent with other
391 Objective-C 'AT' keywords. For example '@class' is
392 reported as RID_AT_CLASS which is consistent with
393 '@synchronized', which is reported as
394 RID_AT_SYNCHRONIZED.
396 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
397 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
398 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
399 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
400 case RID_THROW: token->keyword = RID_AT_THROW; break;
401 case RID_TRY: token->keyword = RID_AT_TRY; break;
402 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
403 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
404 default: token->keyword = C_RID_CODE (token->value);
406 break;
407 case CPP_COLON:
408 case CPP_COMMA:
409 case CPP_CLOSE_PAREN:
410 case CPP_SEMICOLON:
411 /* These tokens may affect the interpretation of any identifiers
412 following, if doing Objective-C. */
413 if (c_dialect_objc ())
414 parser->objc_need_raw_identifier = false;
415 break;
416 case CPP_PRAGMA:
417 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
418 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
419 token->value = NULL;
420 break;
421 default:
422 break;
424 timevar_pop (TV_LEX);
427 /* Return a pointer to the next token from PARSER, reading it in if
428 necessary. */
430 c_token *
431 c_parser_peek_token (c_parser *parser)
433 if (parser->tokens_avail == 0)
435 c_lex_one_token (parser, &parser->tokens[0]);
436 parser->tokens_avail = 1;
438 return &parser->tokens[0];
441 /* Return a pointer to the next-but-one token from PARSER, reading it
442 in if necessary. The next token is already read in. */
444 c_token *
445 c_parser_peek_2nd_token (c_parser *parser)
447 if (parser->tokens_avail >= 2)
448 return &parser->tokens[1];
449 gcc_assert (parser->tokens_avail == 1);
450 gcc_assert (parser->tokens[0].type != CPP_EOF);
451 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
452 c_lex_one_token (parser, &parser->tokens[1]);
453 parser->tokens_avail = 2;
454 return &parser->tokens[1];
457 /* Return a pointer to the Nth token from PARSER, reading it
458 in if necessary. The N-1th token is already read in. */
460 c_token *
461 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
463 /* N is 1-based, not zero-based. */
464 gcc_assert (n > 0);
466 if (parser->tokens_avail >= n)
467 return &parser->tokens[n - 1];
468 gcc_assert (parser->tokens_avail == n - 1);
469 c_lex_one_token (parser, &parser->tokens[n - 1]);
470 parser->tokens_avail = n;
471 return &parser->tokens[n - 1];
474 bool
475 c_keyword_starts_typename (enum rid keyword)
477 switch (keyword)
479 case RID_UNSIGNED:
480 case RID_LONG:
481 case RID_SHORT:
482 case RID_SIGNED:
483 case RID_COMPLEX:
484 case RID_INT:
485 case RID_CHAR:
486 case RID_FLOAT:
487 case RID_DOUBLE:
488 case RID_VOID:
489 case RID_DFLOAT32:
490 case RID_DFLOAT64:
491 case RID_DFLOAT128:
492 CASE_RID_FLOATN_NX:
493 case RID_BOOL:
494 case RID_ENUM:
495 case RID_STRUCT:
496 case RID_UNION:
497 case RID_TYPEOF:
498 case RID_CONST:
499 case RID_ATOMIC:
500 case RID_VOLATILE:
501 case RID_RESTRICT:
502 case RID_ATTRIBUTE:
503 case RID_FRACT:
504 case RID_ACCUM:
505 case RID_SAT:
506 case RID_AUTO_TYPE:
507 return true;
508 default:
509 if (keyword >= RID_FIRST_INT_N
510 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
511 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
512 return true;
513 return false;
517 /* Return true if TOKEN can start a type name,
518 false otherwise. */
519 bool
520 c_token_starts_typename (c_token *token)
522 switch (token->type)
524 case CPP_NAME:
525 switch (token->id_kind)
527 case C_ID_ID:
528 return false;
529 case C_ID_ADDRSPACE:
530 return true;
531 case C_ID_TYPENAME:
532 return true;
533 case C_ID_CLASSNAME:
534 gcc_assert (c_dialect_objc ());
535 return true;
536 default:
537 gcc_unreachable ();
539 case CPP_KEYWORD:
540 return c_keyword_starts_typename (token->keyword);
541 case CPP_LESS:
542 if (c_dialect_objc ())
543 return true;
544 return false;
545 default:
546 return false;
550 /* Return true if the next token from PARSER can start a type name,
551 false otherwise. LA specifies how to do lookahead in order to
552 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
554 static inline bool
555 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
557 c_token *token = c_parser_peek_token (parser);
558 if (c_token_starts_typename (token))
559 return true;
561 /* Try a bit harder to detect an unknown typename. */
562 if (la != cla_prefer_id
563 && token->type == CPP_NAME
564 && token->id_kind == C_ID_ID
566 /* Do not try too hard when we could have "object in array". */
567 && !parser->objc_could_be_foreach_context
569 && (la == cla_prefer_type
570 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
571 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
573 /* Only unknown identifiers. */
574 && !lookup_name (token->value))
575 return true;
577 return false;
580 /* Return true if TOKEN is a type qualifier, false otherwise. */
581 static bool
582 c_token_is_qualifier (c_token *token)
584 switch (token->type)
586 case CPP_NAME:
587 switch (token->id_kind)
589 case C_ID_ADDRSPACE:
590 return true;
591 default:
592 return false;
594 case CPP_KEYWORD:
595 switch (token->keyword)
597 case RID_CONST:
598 case RID_VOLATILE:
599 case RID_RESTRICT:
600 case RID_ATTRIBUTE:
601 case RID_ATOMIC:
602 return true;
603 default:
604 return false;
606 case CPP_LESS:
607 return false;
608 default:
609 gcc_unreachable ();
613 /* Return true if the next token from PARSER is a type qualifier,
614 false otherwise. */
615 static inline bool
616 c_parser_next_token_is_qualifier (c_parser *parser)
618 c_token *token = c_parser_peek_token (parser);
619 return c_token_is_qualifier (token);
622 /* Return true if TOKEN can start declaration specifiers, false
623 otherwise. */
624 static bool
625 c_token_starts_declspecs (c_token *token)
627 switch (token->type)
629 case CPP_NAME:
630 switch (token->id_kind)
632 case C_ID_ID:
633 return false;
634 case C_ID_ADDRSPACE:
635 return true;
636 case C_ID_TYPENAME:
637 return true;
638 case C_ID_CLASSNAME:
639 gcc_assert (c_dialect_objc ());
640 return true;
641 default:
642 gcc_unreachable ();
644 case CPP_KEYWORD:
645 switch (token->keyword)
647 case RID_STATIC:
648 case RID_EXTERN:
649 case RID_REGISTER:
650 case RID_TYPEDEF:
651 case RID_INLINE:
652 case RID_NORETURN:
653 case RID_AUTO:
654 case RID_THREAD:
655 case RID_UNSIGNED:
656 case RID_LONG:
657 case RID_SHORT:
658 case RID_SIGNED:
659 case RID_COMPLEX:
660 case RID_INT:
661 case RID_CHAR:
662 case RID_FLOAT:
663 case RID_DOUBLE:
664 case RID_VOID:
665 case RID_DFLOAT32:
666 case RID_DFLOAT64:
667 case RID_DFLOAT128:
668 CASE_RID_FLOATN_NX:
669 case RID_BOOL:
670 case RID_ENUM:
671 case RID_STRUCT:
672 case RID_UNION:
673 case RID_TYPEOF:
674 case RID_CONST:
675 case RID_VOLATILE:
676 case RID_RESTRICT:
677 case RID_ATTRIBUTE:
678 case RID_FRACT:
679 case RID_ACCUM:
680 case RID_SAT:
681 case RID_ALIGNAS:
682 case RID_ATOMIC:
683 case RID_AUTO_TYPE:
684 return true;
685 default:
686 if (token->keyword >= RID_FIRST_INT_N
687 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
688 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
689 return true;
690 return false;
692 case CPP_LESS:
693 if (c_dialect_objc ())
694 return true;
695 return false;
696 default:
697 return false;
702 /* Return true if TOKEN can start declaration specifiers or a static
703 assertion, false otherwise. */
704 static bool
705 c_token_starts_declaration (c_token *token)
707 if (c_token_starts_declspecs (token)
708 || token->keyword == RID_STATIC_ASSERT)
709 return true;
710 else
711 return false;
714 /* Return true if the next token from PARSER can start declaration
715 specifiers, false otherwise. */
716 bool
717 c_parser_next_token_starts_declspecs (c_parser *parser)
719 c_token *token = c_parser_peek_token (parser);
721 /* In Objective-C, a classname normally starts a declspecs unless it
722 is immediately followed by a dot. In that case, it is the
723 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
724 setter/getter on the class. c_token_starts_declspecs() can't
725 differentiate between the two cases because it only checks the
726 current token, so we have a special check here. */
727 if (c_dialect_objc ()
728 && token->type == CPP_NAME
729 && token->id_kind == C_ID_CLASSNAME
730 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
731 return false;
733 return c_token_starts_declspecs (token);
736 /* Return true if the next tokens from PARSER can start declaration
737 specifiers or a static assertion, false otherwise. */
738 bool
739 c_parser_next_tokens_start_declaration (c_parser *parser)
741 c_token *token = c_parser_peek_token (parser);
743 /* Same as above. */
744 if (c_dialect_objc ()
745 && token->type == CPP_NAME
746 && token->id_kind == C_ID_CLASSNAME
747 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
748 return false;
750 /* Labels do not start declarations. */
751 if (token->type == CPP_NAME
752 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
753 return false;
755 if (c_token_starts_declaration (token))
756 return true;
758 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
759 return true;
761 return false;
764 /* Consume the next token from PARSER. */
766 void
767 c_parser_consume_token (c_parser *parser)
769 gcc_assert (parser->tokens_avail >= 1);
770 gcc_assert (parser->tokens[0].type != CPP_EOF);
771 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
772 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
773 parser->last_token_location = parser->tokens[0].location;
774 if (parser->tokens != &parser->tokens_buf[0])
775 parser->tokens++;
776 else if (parser->tokens_avail == 2)
777 parser->tokens[0] = parser->tokens[1];
778 parser->tokens_avail--;
781 /* Expect the current token to be a #pragma. Consume it and remember
782 that we've begun parsing a pragma. */
784 static void
785 c_parser_consume_pragma (c_parser *parser)
787 gcc_assert (!parser->in_pragma);
788 gcc_assert (parser->tokens_avail >= 1);
789 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
790 if (parser->tokens != &parser->tokens_buf[0])
791 parser->tokens++;
792 else if (parser->tokens_avail == 2)
793 parser->tokens[0] = parser->tokens[1];
794 parser->tokens_avail--;
795 parser->in_pragma = true;
798 /* Update the global input_location from TOKEN. */
799 static inline void
800 c_parser_set_source_position_from_token (c_token *token)
802 if (token->type != CPP_EOF)
804 input_location = token->location;
808 /* Helper function for c_parser_error.
809 Having peeked a token of kind TOK1_KIND that might signify
810 a conflict marker, peek successor tokens to determine
811 if we actually do have a conflict marker.
812 Specifically, we consider a run of 7 '<', '=' or '>' characters
813 at the start of a line as a conflict marker.
814 These come through the lexer as three pairs and a single,
815 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
816 If it returns true, *OUT_LOC is written to with the location/range
817 of the marker. */
819 static bool
820 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
821 location_t *out_loc)
823 c_token *token2 = c_parser_peek_2nd_token (parser);
824 if (token2->type != tok1_kind)
825 return false;
826 c_token *token3 = c_parser_peek_nth_token (parser, 3);
827 if (token3->type != tok1_kind)
828 return false;
829 c_token *token4 = c_parser_peek_nth_token (parser, 4);
830 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
831 return false;
833 /* It must be at the start of the line. */
834 location_t start_loc = c_parser_peek_token (parser)->location;
835 if (LOCATION_COLUMN (start_loc) != 1)
836 return false;
838 /* We have a conflict marker. Construct a location of the form:
839 <<<<<<<
840 ^~~~~~~
841 with start == caret, finishing at the end of the marker. */
842 location_t finish_loc = get_finish (token4->location);
843 *out_loc = make_location (start_loc, start_loc, finish_loc);
845 return true;
848 /* Issue a diagnostic of the form
849 FILE:LINE: MESSAGE before TOKEN
850 where TOKEN is the next token in the input stream of PARSER.
851 MESSAGE (specified by the caller) is usually of the form "expected
852 OTHER-TOKEN".
854 Use RICHLOC as the location of the diagnostic.
856 Do not issue a diagnostic if still recovering from an error.
858 Return true iff an error was actually emitted.
860 ??? This is taken from the C++ parser, but building up messages in
861 this way is not i18n-friendly and some other approach should be
862 used. */
864 static bool
865 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
866 rich_location *richloc)
868 c_token *token = c_parser_peek_token (parser);
869 if (parser->error)
870 return false;
871 parser->error = true;
872 if (!gmsgid)
873 return false;
875 /* If this is actually a conflict marker, report it as such. */
876 if (token->type == CPP_LSHIFT
877 || token->type == CPP_RSHIFT
878 || token->type == CPP_EQ_EQ)
880 location_t loc;
881 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
883 error_at (loc, "version control conflict marker in file");
884 return true;
888 c_parse_error (gmsgid,
889 /* Because c_parse_error does not understand
890 CPP_KEYWORD, keywords are treated like
891 identifiers. */
892 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
893 /* ??? The C parser does not save the cpp flags of a
894 token, we need to pass 0 here and we will not get
895 the source spelling of some tokens but rather the
896 canonical spelling. */
897 token->value, /*flags=*/0, richloc);
898 return true;
901 /* As c_parser_error_richloc, but issue the message at the
902 location of PARSER's next token, or at input_location
903 if the next token is EOF. */
905 bool
906 c_parser_error (c_parser *parser, const char *gmsgid)
908 c_token *token = c_parser_peek_token (parser);
909 c_parser_set_source_position_from_token (token);
910 rich_location richloc (line_table, input_location);
911 return c_parser_error_richloc (parser, gmsgid, &richloc);
914 /* Some tokens naturally come in pairs e.g.'(' and ')'.
915 This class is for tracking such a matching pair of symbols.
916 In particular, it tracks the location of the first token,
917 so that if the second token is missing, we can highlight the
918 location of the first token when notifying the user about the
919 problem. */
921 template <typename traits_t>
922 class token_pair
924 public:
925 /* token_pair's ctor. */
926 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
928 /* If the next token is the opening symbol for this pair, consume it and
929 return true.
930 Otherwise, issue an error and return false.
931 In either case, record the location of the opening token. */
933 bool require_open (c_parser *parser)
935 c_token *token = c_parser_peek_token (parser);
936 if (token)
937 m_open_loc = token->location;
939 return c_parser_require (parser, traits_t::open_token_type,
940 traits_t::open_gmsgid);
943 /* Consume the next token from PARSER, recording its location as
944 that of the opening token within the pair. */
946 void consume_open (c_parser *parser)
948 c_token *token = c_parser_peek_token (parser);
949 gcc_assert (token->type == traits_t::open_token_type);
950 m_open_loc = token->location;
951 c_parser_consume_token (parser);
954 /* If the next token is the closing symbol for this pair, consume it
955 and return true.
956 Otherwise, issue an error, highlighting the location of the
957 corresponding opening token, and return false. */
959 bool require_close (c_parser *parser) const
961 return c_parser_require (parser, traits_t::close_token_type,
962 traits_t::close_gmsgid, m_open_loc);
965 /* Like token_pair::require_close, except that tokens will be skipped
966 until the desired token is found. An error message is still produced
967 if the next token is not as expected. */
969 void skip_until_found_close (c_parser *parser) const
971 c_parser_skip_until_found (parser, traits_t::close_token_type,
972 traits_t::close_gmsgid, m_open_loc);
975 private:
976 location_t m_open_loc;
979 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
981 struct matching_paren_traits
983 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
984 static const char * const open_gmsgid;
985 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
986 static const char * const close_gmsgid;
989 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
990 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
992 /* "matching_parens" is a token_pair<T> class for tracking matching
993 pairs of parentheses. */
995 typedef token_pair<matching_paren_traits> matching_parens;
997 /* Traits for token_pair<T> for tracking matching pairs of braces. */
999 struct matching_brace_traits
1001 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1002 static const char * const open_gmsgid;
1003 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1004 static const char * const close_gmsgid;
1007 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1008 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1010 /* "matching_braces" is a token_pair<T> class for tracking matching
1011 pairs of braces. */
1013 typedef token_pair<matching_brace_traits> matching_braces;
1015 /* Get a description of the matching symbol to TYPE e.g. "(" for
1016 CPP_CLOSE_PAREN. */
1018 static const char *
1019 get_matching_symbol (enum cpp_ttype type)
1021 switch (type)
1023 default:
1024 gcc_unreachable ();
1025 return "";
1026 case CPP_CLOSE_PAREN:
1027 return "(";
1028 case CPP_CLOSE_BRACE:
1029 return "{";
1033 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1034 issue the error MSGID. If MSGID is NULL then a message has already
1035 been produced and no message will be produced this time. Returns
1036 true if found, false otherwise.
1038 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1039 within any error as the location of an "opening" token matching
1040 the close token TYPE (e.g. the location of the '(' when TYPE is
1041 CPP_CLOSE_PAREN).
1043 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1044 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1045 attempt to generate a fix-it hint for the problem.
1046 Otherwise msgid describes multiple token types (e.g.
1047 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1048 generate a fix-it hint. */
1050 bool
1051 c_parser_require (c_parser *parser,
1052 enum cpp_ttype type,
1053 const char *msgid,
1054 location_t matching_location,
1055 bool type_is_unique)
1057 if (c_parser_next_token_is (parser, type))
1059 c_parser_consume_token (parser);
1060 return true;
1062 else
1064 location_t next_token_loc = c_parser_peek_token (parser)->location;
1065 gcc_rich_location richloc (next_token_loc);
1067 /* Potentially supply a fix-it hint, suggesting to add the
1068 missing token immediately after the *previous* token.
1069 This may move the primary location within richloc. */
1070 if (!parser->error && type_is_unique)
1071 maybe_suggest_missing_token_insertion (&richloc, type,
1072 parser->last_token_location);
1074 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1075 Attempt to consolidate diagnostics by printing it as a
1076 secondary range within the main diagnostic. */
1077 bool added_matching_location = false;
1078 if (matching_location != UNKNOWN_LOCATION)
1079 added_matching_location
1080 = richloc.add_location_if_nearby (matching_location);
1082 if (c_parser_error_richloc (parser, msgid, &richloc))
1083 /* If we weren't able to consolidate matching_location, then
1084 print it as a secondary diagnostic. */
1085 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1086 inform (matching_location, "to match this %qs",
1087 get_matching_symbol (type));
1089 return false;
1093 /* If the next token is the indicated keyword, consume it. Otherwise,
1094 issue the error MSGID. Returns true if found, false otherwise. */
1096 static bool
1097 c_parser_require_keyword (c_parser *parser,
1098 enum rid keyword,
1099 const char *msgid)
1101 if (c_parser_next_token_is_keyword (parser, keyword))
1103 c_parser_consume_token (parser);
1104 return true;
1106 else
1108 c_parser_error (parser, msgid);
1109 return false;
1113 /* Like c_parser_require, except that tokens will be skipped until the
1114 desired token is found. An error message is still produced if the
1115 next token is not as expected. If MSGID is NULL then a message has
1116 already been produced and no message will be produced this
1117 time.
1119 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1120 within any error as the location of an "opening" token matching
1121 the close token TYPE (e.g. the location of the '(' when TYPE is
1122 CPP_CLOSE_PAREN). */
1124 void
1125 c_parser_skip_until_found (c_parser *parser,
1126 enum cpp_ttype type,
1127 const char *msgid,
1128 location_t matching_location)
1130 unsigned nesting_depth = 0;
1132 if (c_parser_require (parser, type, msgid, matching_location))
1133 return;
1135 /* Skip tokens until the desired token is found. */
1136 while (true)
1138 /* Peek at the next token. */
1139 c_token *token = c_parser_peek_token (parser);
1140 /* If we've reached the token we want, consume it and stop. */
1141 if (token->type == type && !nesting_depth)
1143 c_parser_consume_token (parser);
1144 break;
1147 /* If we've run out of tokens, stop. */
1148 if (token->type == CPP_EOF)
1149 return;
1150 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1151 return;
1152 if (token->type == CPP_OPEN_BRACE
1153 || token->type == CPP_OPEN_PAREN
1154 || token->type == CPP_OPEN_SQUARE)
1155 ++nesting_depth;
1156 else if (token->type == CPP_CLOSE_BRACE
1157 || token->type == CPP_CLOSE_PAREN
1158 || token->type == CPP_CLOSE_SQUARE)
1160 if (nesting_depth-- == 0)
1161 break;
1163 /* Consume this token. */
1164 c_parser_consume_token (parser);
1166 parser->error = false;
1169 /* Skip tokens until the end of a parameter is found, but do not
1170 consume the comma, semicolon or closing delimiter. */
1172 static void
1173 c_parser_skip_to_end_of_parameter (c_parser *parser)
1175 unsigned nesting_depth = 0;
1177 while (true)
1179 c_token *token = c_parser_peek_token (parser);
1180 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1181 && !nesting_depth)
1182 break;
1183 /* If we've run out of tokens, stop. */
1184 if (token->type == CPP_EOF)
1185 return;
1186 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1187 return;
1188 if (token->type == CPP_OPEN_BRACE
1189 || token->type == CPP_OPEN_PAREN
1190 || token->type == CPP_OPEN_SQUARE)
1191 ++nesting_depth;
1192 else if (token->type == CPP_CLOSE_BRACE
1193 || token->type == CPP_CLOSE_PAREN
1194 || token->type == CPP_CLOSE_SQUARE)
1196 if (nesting_depth-- == 0)
1197 break;
1199 /* Consume this token. */
1200 c_parser_consume_token (parser);
1202 parser->error = false;
1205 /* Expect to be at the end of the pragma directive and consume an
1206 end of line marker. */
1208 static void
1209 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1211 gcc_assert (parser->in_pragma);
1212 parser->in_pragma = false;
1214 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1215 c_parser_error (parser, "expected end of line");
1217 cpp_ttype token_type;
1220 c_token *token = c_parser_peek_token (parser);
1221 token_type = token->type;
1222 if (token_type == CPP_EOF)
1223 break;
1224 c_parser_consume_token (parser);
1226 while (token_type != CPP_PRAGMA_EOL);
1228 parser->error = false;
1231 /* Skip tokens until we have consumed an entire block, or until we
1232 have consumed a non-nested ';'. */
1234 static void
1235 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1237 unsigned nesting_depth = 0;
1238 bool save_error = parser->error;
1240 while (true)
1242 c_token *token;
1244 /* Peek at the next token. */
1245 token = c_parser_peek_token (parser);
1247 switch (token->type)
1249 case CPP_EOF:
1250 return;
1252 case CPP_PRAGMA_EOL:
1253 if (parser->in_pragma)
1254 return;
1255 break;
1257 case CPP_SEMICOLON:
1258 /* If the next token is a ';', we have reached the
1259 end of the statement. */
1260 if (!nesting_depth)
1262 /* Consume the ';'. */
1263 c_parser_consume_token (parser);
1264 goto finished;
1266 break;
1268 case CPP_CLOSE_BRACE:
1269 /* If the next token is a non-nested '}', then we have
1270 reached the end of the current block. */
1271 if (nesting_depth == 0 || --nesting_depth == 0)
1273 c_parser_consume_token (parser);
1274 goto finished;
1276 break;
1278 case CPP_OPEN_BRACE:
1279 /* If it the next token is a '{', then we are entering a new
1280 block. Consume the entire block. */
1281 ++nesting_depth;
1282 break;
1284 case CPP_PRAGMA:
1285 /* If we see a pragma, consume the whole thing at once. We
1286 have some safeguards against consuming pragmas willy-nilly.
1287 Normally, we'd expect to be here with parser->error set,
1288 which disables these safeguards. But it's possible to get
1289 here for secondary error recovery, after parser->error has
1290 been cleared. */
1291 c_parser_consume_pragma (parser);
1292 c_parser_skip_to_pragma_eol (parser);
1293 parser->error = save_error;
1294 continue;
1296 default:
1297 break;
1300 c_parser_consume_token (parser);
1303 finished:
1304 parser->error = false;
1307 /* CPP's options (initialized by c-opts.c). */
1308 extern cpp_options *cpp_opts;
1310 /* Save the warning flags which are controlled by __extension__. */
1312 static inline int
1313 disable_extension_diagnostics (void)
1315 int ret = (pedantic
1316 | (warn_pointer_arith << 1)
1317 | (warn_traditional << 2)
1318 | (flag_iso << 3)
1319 | (warn_long_long << 4)
1320 | (warn_cxx_compat << 5)
1321 | (warn_overlength_strings << 6)
1322 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1323 play tricks to properly restore it. */
1324 | ((warn_c90_c99_compat == 1) << 7)
1325 | ((warn_c90_c99_compat == -1) << 8)
1326 /* Similarly for warn_c99_c11_compat. */
1327 | ((warn_c99_c11_compat == 1) << 9)
1328 | ((warn_c99_c11_compat == -1) << 10)
1330 cpp_opts->cpp_pedantic = pedantic = 0;
1331 warn_pointer_arith = 0;
1332 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1333 flag_iso = 0;
1334 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1335 warn_cxx_compat = 0;
1336 warn_overlength_strings = 0;
1337 warn_c90_c99_compat = 0;
1338 warn_c99_c11_compat = 0;
1339 return ret;
1342 /* Restore the warning flags which are controlled by __extension__.
1343 FLAGS is the return value from disable_extension_diagnostics. */
1345 static inline void
1346 restore_extension_diagnostics (int flags)
1348 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1349 warn_pointer_arith = (flags >> 1) & 1;
1350 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1351 flag_iso = (flags >> 3) & 1;
1352 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1353 warn_cxx_compat = (flags >> 5) & 1;
1354 warn_overlength_strings = (flags >> 6) & 1;
1355 /* See above for why is this needed. */
1356 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1357 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1360 /* Helper data structure for parsing #pragma acc routine. */
1361 struct oacc_routine_data {
1362 bool error_seen; /* Set if error has been reported. */
1363 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1364 tree clauses;
1365 location_t loc;
1368 static void c_parser_external_declaration (c_parser *);
1369 static void c_parser_asm_definition (c_parser *);
1370 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1371 bool, bool, tree *, vec<c_token>,
1372 struct oacc_routine_data * = NULL,
1373 bool * = NULL);
1374 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1375 static void c_parser_static_assert_declaration (c_parser *);
1376 static struct c_typespec c_parser_enum_specifier (c_parser *);
1377 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1378 static tree c_parser_struct_declaration (c_parser *);
1379 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1380 static tree c_parser_alignas_specifier (c_parser *);
1381 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1382 c_dtr_syn, bool *);
1383 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1384 bool,
1385 struct c_declarator *);
1386 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1387 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1388 tree);
1389 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1390 static tree c_parser_simple_asm_expr (c_parser *);
1391 static tree c_parser_attributes (c_parser *);
1392 static struct c_expr c_parser_initializer (c_parser *);
1393 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1394 struct obstack *);
1395 static void c_parser_initelt (c_parser *, struct obstack *);
1396 static void c_parser_initval (c_parser *, struct c_expr *,
1397 struct obstack *);
1398 static tree c_parser_compound_statement (c_parser *);
1399 static void c_parser_compound_statement_nostart (c_parser *);
1400 static void c_parser_label (c_parser *);
1401 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1402 static void c_parser_statement_after_labels (c_parser *, bool *,
1403 vec<tree> * = NULL);
1404 static tree c_parser_c99_block_statement (c_parser *, bool *,
1405 location_t * = NULL);
1406 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1407 static void c_parser_switch_statement (c_parser *, bool *);
1408 static void c_parser_while_statement (c_parser *, bool, bool *);
1409 static void c_parser_do_statement (c_parser *, bool);
1410 static void c_parser_for_statement (c_parser *, bool, bool *);
1411 static tree c_parser_asm_statement (c_parser *);
1412 static tree c_parser_asm_operands (c_parser *);
1413 static tree c_parser_asm_goto_operands (c_parser *);
1414 static tree c_parser_asm_clobbers (c_parser *);
1415 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1416 tree = NULL_TREE);
1417 static struct c_expr c_parser_conditional_expression (c_parser *,
1418 struct c_expr *, tree);
1419 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1420 tree);
1421 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1422 static struct c_expr c_parser_unary_expression (c_parser *);
1423 static struct c_expr c_parser_sizeof_expression (c_parser *);
1424 static struct c_expr c_parser_alignof_expression (c_parser *);
1425 static struct c_expr c_parser_postfix_expression (c_parser *);
1426 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1427 struct c_type_name *,
1428 location_t);
1429 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1430 location_t loc,
1431 struct c_expr);
1432 static tree c_parser_transaction (c_parser *, enum rid);
1433 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1434 static tree c_parser_transaction_cancel (c_parser *);
1435 static struct c_expr c_parser_expression (c_parser *);
1436 static struct c_expr c_parser_expression_conv (c_parser *);
1437 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1438 vec<tree, va_gc> **, location_t *,
1439 tree *, vec<location_t> *,
1440 unsigned int * = NULL);
1441 static void c_parser_oacc_declare (c_parser *);
1442 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1443 static void c_parser_oacc_update (c_parser *);
1444 static void c_parser_omp_construct (c_parser *, bool *);
1445 static void c_parser_omp_threadprivate (c_parser *);
1446 static void c_parser_omp_barrier (c_parser *);
1447 static void c_parser_omp_flush (c_parser *);
1448 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1449 tree, tree *, bool *);
1450 static void c_parser_omp_taskwait (c_parser *);
1451 static void c_parser_omp_taskyield (c_parser *);
1452 static void c_parser_omp_cancel (c_parser *);
1454 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1455 pragma_stmt, pragma_compound };
1456 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1457 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1458 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1459 static void c_parser_omp_end_declare_target (c_parser *);
1460 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1461 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1462 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1464 /* These Objective-C parser functions are only ever called when
1465 compiling Objective-C. */
1466 static void c_parser_objc_class_definition (c_parser *, tree);
1467 static void c_parser_objc_class_instance_variables (c_parser *);
1468 static void c_parser_objc_class_declaration (c_parser *);
1469 static void c_parser_objc_alias_declaration (c_parser *);
1470 static void c_parser_objc_protocol_definition (c_parser *, tree);
1471 static bool c_parser_objc_method_type (c_parser *);
1472 static void c_parser_objc_method_definition (c_parser *);
1473 static void c_parser_objc_methodprotolist (c_parser *);
1474 static void c_parser_objc_methodproto (c_parser *);
1475 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1476 static tree c_parser_objc_type_name (c_parser *);
1477 static tree c_parser_objc_protocol_refs (c_parser *);
1478 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1479 static void c_parser_objc_synchronized_statement (c_parser *);
1480 static tree c_parser_objc_selector (c_parser *);
1481 static tree c_parser_objc_selector_arg (c_parser *);
1482 static tree c_parser_objc_receiver (c_parser *);
1483 static tree c_parser_objc_message_args (c_parser *);
1484 static tree c_parser_objc_keywordexpr (c_parser *);
1485 static void c_parser_objc_at_property_declaration (c_parser *);
1486 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1487 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1488 static bool c_parser_objc_diagnose_bad_element_prefix
1489 (c_parser *, struct c_declspecs *);
1491 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1493 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1495 translation-unit:
1496 external-declarations
1498 external-declarations:
1499 external-declaration
1500 external-declarations external-declaration
1502 GNU extensions:
1504 translation-unit:
1505 empty
1508 static void
1509 c_parser_translation_unit (c_parser *parser)
1511 if (c_parser_next_token_is (parser, CPP_EOF))
1513 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1514 "ISO C forbids an empty translation unit");
1516 else
1518 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1519 mark_valid_location_for_stdc_pragma (false);
1522 ggc_collect ();
1523 c_parser_external_declaration (parser);
1524 obstack_free (&parser_obstack, obstack_position);
1526 while (c_parser_next_token_is_not (parser, CPP_EOF));
1529 unsigned int i;
1530 tree decl;
1531 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1532 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1533 error ("storage size of %q+D isn%'t known", decl);
1536 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1538 external-declaration:
1539 function-definition
1540 declaration
1542 GNU extensions:
1544 external-declaration:
1545 asm-definition
1547 __extension__ external-declaration
1549 Objective-C:
1551 external-declaration:
1552 objc-class-definition
1553 objc-class-declaration
1554 objc-alias-declaration
1555 objc-protocol-definition
1556 objc-method-definition
1557 @end
1560 static void
1561 c_parser_external_declaration (c_parser *parser)
1563 int ext;
1564 switch (c_parser_peek_token (parser)->type)
1566 case CPP_KEYWORD:
1567 switch (c_parser_peek_token (parser)->keyword)
1569 case RID_EXTENSION:
1570 ext = disable_extension_diagnostics ();
1571 c_parser_consume_token (parser);
1572 c_parser_external_declaration (parser);
1573 restore_extension_diagnostics (ext);
1574 break;
1575 case RID_ASM:
1576 c_parser_asm_definition (parser);
1577 break;
1578 case RID_AT_INTERFACE:
1579 case RID_AT_IMPLEMENTATION:
1580 gcc_assert (c_dialect_objc ());
1581 c_parser_objc_class_definition (parser, NULL_TREE);
1582 break;
1583 case RID_AT_CLASS:
1584 gcc_assert (c_dialect_objc ());
1585 c_parser_objc_class_declaration (parser);
1586 break;
1587 case RID_AT_ALIAS:
1588 gcc_assert (c_dialect_objc ());
1589 c_parser_objc_alias_declaration (parser);
1590 break;
1591 case RID_AT_PROTOCOL:
1592 gcc_assert (c_dialect_objc ());
1593 c_parser_objc_protocol_definition (parser, NULL_TREE);
1594 break;
1595 case RID_AT_PROPERTY:
1596 gcc_assert (c_dialect_objc ());
1597 c_parser_objc_at_property_declaration (parser);
1598 break;
1599 case RID_AT_SYNTHESIZE:
1600 gcc_assert (c_dialect_objc ());
1601 c_parser_objc_at_synthesize_declaration (parser);
1602 break;
1603 case RID_AT_DYNAMIC:
1604 gcc_assert (c_dialect_objc ());
1605 c_parser_objc_at_dynamic_declaration (parser);
1606 break;
1607 case RID_AT_END:
1608 gcc_assert (c_dialect_objc ());
1609 c_parser_consume_token (parser);
1610 objc_finish_implementation ();
1611 break;
1612 default:
1613 goto decl_or_fndef;
1615 break;
1616 case CPP_SEMICOLON:
1617 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1618 "ISO C does not allow extra %<;%> outside of a function");
1619 c_parser_consume_token (parser);
1620 break;
1621 case CPP_PRAGMA:
1622 mark_valid_location_for_stdc_pragma (true);
1623 c_parser_pragma (parser, pragma_external, NULL);
1624 mark_valid_location_for_stdc_pragma (false);
1625 break;
1626 case CPP_PLUS:
1627 case CPP_MINUS:
1628 if (c_dialect_objc ())
1630 c_parser_objc_method_definition (parser);
1631 break;
1633 /* Else fall through, and yield a syntax error trying to parse
1634 as a declaration or function definition. */
1635 /* FALLTHRU */
1636 default:
1637 decl_or_fndef:
1638 /* A declaration or a function definition (or, in Objective-C,
1639 an @interface or @protocol with prefix attributes). We can
1640 only tell which after parsing the declaration specifiers, if
1641 any, and the first declarator. */
1642 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1643 NULL, vNULL);
1644 break;
1648 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1649 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1651 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1652 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1653 is accepted; otherwise (old-style parameter declarations) only other
1654 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1655 assertion is accepted; otherwise (old-style parameter declarations)
1656 it is not. If NESTED is true, we are inside a function or parsing
1657 old-style parameter declarations; any functions encountered are
1658 nested functions and declaration specifiers are required; otherwise
1659 we are at top level and functions are normal functions and
1660 declaration specifiers may be optional. If EMPTY_OK is true, empty
1661 declarations are OK (subject to all other constraints); otherwise
1662 (old-style parameter declarations) they are diagnosed. If
1663 START_ATTR_OK is true, the declaration specifiers may start with
1664 attributes; otherwise they may not.
1665 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1666 declaration when parsing an Objective-C foreach statement.
1667 FALLTHRU_ATTR_P is used to signal whether this function parsed
1668 "__attribute__((fallthrough));".
1670 declaration:
1671 declaration-specifiers init-declarator-list[opt] ;
1672 static_assert-declaration
1674 function-definition:
1675 declaration-specifiers[opt] declarator declaration-list[opt]
1676 compound-statement
1678 declaration-list:
1679 declaration
1680 declaration-list declaration
1682 init-declarator-list:
1683 init-declarator
1684 init-declarator-list , init-declarator
1686 init-declarator:
1687 declarator simple-asm-expr[opt] attributes[opt]
1688 declarator simple-asm-expr[opt] attributes[opt] = initializer
1690 GNU extensions:
1692 nested-function-definition:
1693 declaration-specifiers declarator declaration-list[opt]
1694 compound-statement
1696 attribute ;
1698 Objective-C:
1699 attributes objc-class-definition
1700 attributes objc-category-definition
1701 attributes objc-protocol-definition
1703 The simple-asm-expr and attributes are GNU extensions.
1705 This function does not handle __extension__; that is handled in its
1706 callers. ??? Following the old parser, __extension__ may start
1707 external declarations, declarations in functions and declarations
1708 at the start of "for" loops, but not old-style parameter
1709 declarations.
1711 C99 requires declaration specifiers in a function definition; the
1712 absence is diagnosed through the diagnosis of implicit int. In GNU
1713 C we also allow but diagnose declarations without declaration
1714 specifiers, but only at top level (elsewhere they conflict with
1715 other syntax).
1717 In Objective-C, declarations of the looping variable in a foreach
1718 statement are exceptionally terminated by 'in' (for example, 'for
1719 (NSObject *object in array) { ... }').
1721 OpenMP:
1723 declaration:
1724 threadprivate-directive
1726 GIMPLE:
1728 gimple-function-definition:
1729 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1730 declaration-list[opt] compound-statement
1732 rtl-function-definition:
1733 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1734 declaration-list[opt] compound-statement */
1736 static void
1737 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1738 bool static_assert_ok, bool empty_ok,
1739 bool nested, bool start_attr_ok,
1740 tree *objc_foreach_object_declaration,
1741 vec<c_token> omp_declare_simd_clauses,
1742 struct oacc_routine_data *oacc_routine_data,
1743 bool *fallthru_attr_p)
1745 struct c_declspecs *specs;
1746 tree prefix_attrs;
1747 tree all_prefix_attrs;
1748 bool diagnosed_no_specs = false;
1749 location_t here = c_parser_peek_token (parser)->location;
1751 if (static_assert_ok
1752 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1754 c_parser_static_assert_declaration (parser);
1755 return;
1757 specs = build_null_declspecs ();
1759 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1760 if (c_parser_peek_token (parser)->type == CPP_NAME
1761 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1762 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1763 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1764 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1766 tree name = c_parser_peek_token (parser)->value;
1768 /* Issue a warning about NAME being an unknown type name, perhaps
1769 with some kind of hint.
1770 If the user forgot a "struct" etc, suggest inserting
1771 it. Otherwise, attempt to look for misspellings. */
1772 gcc_rich_location richloc (here);
1773 if (tag_exists_p (RECORD_TYPE, name))
1775 /* This is not C++ with its implicit typedef. */
1776 richloc.add_fixit_insert_before ("struct ");
1777 error_at (&richloc,
1778 "unknown type name %qE;"
1779 " use %<struct%> keyword to refer to the type",
1780 name);
1782 else if (tag_exists_p (UNION_TYPE, name))
1784 richloc.add_fixit_insert_before ("union ");
1785 error_at (&richloc,
1786 "unknown type name %qE;"
1787 " use %<union%> keyword to refer to the type",
1788 name);
1790 else if (tag_exists_p (ENUMERAL_TYPE, name))
1792 richloc.add_fixit_insert_before ("enum ");
1793 error_at (&richloc,
1794 "unknown type name %qE;"
1795 " use %<enum%> keyword to refer to the type",
1796 name);
1798 else
1800 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1801 here);
1802 if (hint)
1804 richloc.add_fixit_replace (hint.suggestion ());
1805 error_at (&richloc,
1806 "unknown type name %qE; did you mean %qs?",
1807 name, hint.suggestion ());
1809 else
1810 error_at (here, "unknown type name %qE", name);
1813 /* Parse declspecs normally to get a correct pointer type, but avoid
1814 a further "fails to be a type name" error. Refuse nested functions
1815 since it is not how the user likely wants us to recover. */
1816 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1817 c_parser_peek_token (parser)->keyword = RID_VOID;
1818 c_parser_peek_token (parser)->value = error_mark_node;
1819 fndef_ok = !nested;
1822 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1823 true, true, cla_nonabstract_decl);
1824 if (parser->error)
1826 c_parser_skip_to_end_of_block_or_statement (parser);
1827 return;
1829 if (nested && !specs->declspecs_seen_p)
1831 c_parser_error (parser, "expected declaration specifiers");
1832 c_parser_skip_to_end_of_block_or_statement (parser);
1833 return;
1836 finish_declspecs (specs);
1837 bool auto_type_p = specs->typespec_word == cts_auto_type;
1838 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1840 if (auto_type_p)
1841 error_at (here, "%<__auto_type%> in empty declaration");
1842 else if (specs->typespec_kind == ctsk_none
1843 && attribute_fallthrough_p (specs->attrs))
1845 if (fallthru_attr_p != NULL)
1846 *fallthru_attr_p = true;
1847 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1848 void_type_node, 0);
1849 add_stmt (fn);
1851 else if (empty_ok)
1852 shadow_tag (specs);
1853 else
1855 shadow_tag_warned (specs, 1);
1856 pedwarn (here, 0, "empty declaration");
1858 c_parser_consume_token (parser);
1859 if (oacc_routine_data)
1860 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1861 return;
1864 /* Provide better error recovery. Note that a type name here is usually
1865 better diagnosed as a redeclaration. */
1866 if (empty_ok
1867 && specs->typespec_kind == ctsk_tagdef
1868 && c_parser_next_token_starts_declspecs (parser)
1869 && !c_parser_next_token_is (parser, CPP_NAME))
1871 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1872 parser->error = false;
1873 shadow_tag_warned (specs, 1);
1874 return;
1876 else if (c_dialect_objc () && !auto_type_p)
1878 /* Prefix attributes are an error on method decls. */
1879 switch (c_parser_peek_token (parser)->type)
1881 case CPP_PLUS:
1882 case CPP_MINUS:
1883 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1884 return;
1885 if (specs->attrs)
1887 warning_at (c_parser_peek_token (parser)->location,
1888 OPT_Wattributes,
1889 "prefix attributes are ignored for methods");
1890 specs->attrs = NULL_TREE;
1892 if (fndef_ok)
1893 c_parser_objc_method_definition (parser);
1894 else
1895 c_parser_objc_methodproto (parser);
1896 return;
1897 break;
1898 default:
1899 break;
1901 /* This is where we parse 'attributes @interface ...',
1902 'attributes @implementation ...', 'attributes @protocol ...'
1903 (where attributes could be, for example, __attribute__
1904 ((deprecated)).
1906 switch (c_parser_peek_token (parser)->keyword)
1908 case RID_AT_INTERFACE:
1910 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1911 return;
1912 c_parser_objc_class_definition (parser, specs->attrs);
1913 return;
1915 break;
1916 case RID_AT_IMPLEMENTATION:
1918 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1919 return;
1920 if (specs->attrs)
1922 warning_at (c_parser_peek_token (parser)->location,
1923 OPT_Wattributes,
1924 "prefix attributes are ignored for implementations");
1925 specs->attrs = NULL_TREE;
1927 c_parser_objc_class_definition (parser, NULL_TREE);
1928 return;
1930 break;
1931 case RID_AT_PROTOCOL:
1933 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1934 return;
1935 c_parser_objc_protocol_definition (parser, specs->attrs);
1936 return;
1938 break;
1939 case RID_AT_ALIAS:
1940 case RID_AT_CLASS:
1941 case RID_AT_END:
1942 case RID_AT_PROPERTY:
1943 if (specs->attrs)
1945 c_parser_error (parser, "unexpected attribute");
1946 specs->attrs = NULL;
1948 break;
1949 default:
1950 break;
1953 else if (attribute_fallthrough_p (specs->attrs))
1954 warning_at (here, OPT_Wattributes,
1955 "%<fallthrough%> attribute not followed by %<;%>");
1957 pending_xref_error ();
1958 prefix_attrs = specs->attrs;
1959 all_prefix_attrs = prefix_attrs;
1960 specs->attrs = NULL_TREE;
1961 while (true)
1963 struct c_declarator *declarator;
1964 bool dummy = false;
1965 timevar_id_t tv;
1966 tree fnbody = NULL_TREE;
1967 /* Declaring either one or more declarators (in which case we
1968 should diagnose if there were no declaration specifiers) or a
1969 function definition (in which case the diagnostic for
1970 implicit int suffices). */
1971 declarator = c_parser_declarator (parser,
1972 specs->typespec_kind != ctsk_none,
1973 C_DTR_NORMAL, &dummy);
1974 if (declarator == NULL)
1976 if (omp_declare_simd_clauses.exists ())
1977 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1978 omp_declare_simd_clauses);
1979 if (oacc_routine_data)
1980 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1981 c_parser_skip_to_end_of_block_or_statement (parser);
1982 return;
1984 if (auto_type_p && declarator->kind != cdk_id)
1986 error_at (here,
1987 "%<__auto_type%> requires a plain identifier"
1988 " as declarator");
1989 c_parser_skip_to_end_of_block_or_statement (parser);
1990 return;
1992 if (c_parser_next_token_is (parser, CPP_EQ)
1993 || c_parser_next_token_is (parser, CPP_COMMA)
1994 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1995 || c_parser_next_token_is_keyword (parser, RID_ASM)
1996 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1997 || c_parser_next_token_is_keyword (parser, RID_IN))
1999 tree asm_name = NULL_TREE;
2000 tree postfix_attrs = NULL_TREE;
2001 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2003 diagnosed_no_specs = true;
2004 pedwarn (here, 0, "data definition has no type or storage class");
2006 /* Having seen a data definition, there cannot now be a
2007 function definition. */
2008 fndef_ok = false;
2009 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2010 asm_name = c_parser_simple_asm_expr (parser);
2011 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2013 postfix_attrs = c_parser_attributes (parser);
2014 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2016 /* This means there is an attribute specifier after
2017 the declarator in a function definition. Provide
2018 some more information for the user. */
2019 error_at (here, "attributes should be specified before the "
2020 "declarator in a function definition");
2021 c_parser_skip_to_end_of_block_or_statement (parser);
2022 return;
2025 if (c_parser_next_token_is (parser, CPP_EQ))
2027 tree d;
2028 struct c_expr init;
2029 location_t init_loc;
2030 c_parser_consume_token (parser);
2031 if (auto_type_p)
2033 init_loc = c_parser_peek_token (parser)->location;
2034 rich_location richloc (line_table, init_loc);
2035 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2036 /* A parameter is initialized, which is invalid. Don't
2037 attempt to instrument the initializer. */
2038 int flag_sanitize_save = flag_sanitize;
2039 if (nested && !empty_ok)
2040 flag_sanitize = 0;
2041 init = c_parser_expr_no_commas (parser, NULL);
2042 flag_sanitize = flag_sanitize_save;
2043 if (TREE_CODE (init.value) == COMPONENT_REF
2044 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2045 error_at (here,
2046 "%<__auto_type%> used with a bit-field"
2047 " initializer");
2048 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2049 tree init_type = TREE_TYPE (init.value);
2050 /* As with typeof, remove all qualifiers from atomic types. */
2051 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2052 init_type
2053 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2054 bool vm_type = variably_modified_type_p (init_type,
2055 NULL_TREE);
2056 if (vm_type)
2057 init.value = save_expr (init.value);
2058 finish_init ();
2059 specs->typespec_kind = ctsk_typeof;
2060 specs->locations[cdw_typedef] = init_loc;
2061 specs->typedef_p = true;
2062 specs->type = init_type;
2063 if (vm_type)
2065 bool maybe_const = true;
2066 tree type_expr = c_fully_fold (init.value, false,
2067 &maybe_const);
2068 specs->expr_const_operands &= maybe_const;
2069 if (specs->expr)
2070 specs->expr = build2 (COMPOUND_EXPR,
2071 TREE_TYPE (type_expr),
2072 specs->expr, type_expr);
2073 else
2074 specs->expr = type_expr;
2076 d = start_decl (declarator, specs, true,
2077 chainon (postfix_attrs, all_prefix_attrs));
2078 if (!d)
2079 d = error_mark_node;
2080 if (omp_declare_simd_clauses.exists ())
2081 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2082 omp_declare_simd_clauses);
2084 else
2086 /* The declaration of the variable is in effect while
2087 its initializer is parsed. */
2088 d = start_decl (declarator, specs, true,
2089 chainon (postfix_attrs, all_prefix_attrs));
2090 if (!d)
2091 d = error_mark_node;
2092 if (omp_declare_simd_clauses.exists ())
2093 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2094 omp_declare_simd_clauses);
2095 init_loc = c_parser_peek_token (parser)->location;
2096 rich_location richloc (line_table, init_loc);
2097 start_init (d, asm_name, global_bindings_p (), &richloc);
2098 /* A parameter is initialized, which is invalid. Don't
2099 attempt to instrument the initializer. */
2100 int flag_sanitize_save = flag_sanitize;
2101 if (TREE_CODE (d) == PARM_DECL)
2102 flag_sanitize = 0;
2103 init = c_parser_initializer (parser);
2104 flag_sanitize = flag_sanitize_save;
2105 finish_init ();
2107 if (oacc_routine_data)
2108 c_finish_oacc_routine (oacc_routine_data, d, false);
2109 if (d != error_mark_node)
2111 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2112 finish_decl (d, init_loc, init.value,
2113 init.original_type, asm_name);
2116 else
2118 if (auto_type_p)
2120 error_at (here,
2121 "%<__auto_type%> requires an initialized "
2122 "data declaration");
2123 c_parser_skip_to_end_of_block_or_statement (parser);
2124 return;
2126 tree d = start_decl (declarator, specs, false,
2127 chainon (postfix_attrs,
2128 all_prefix_attrs));
2129 if (d && TREE_CODE (d) == FUNCTION_DECL)
2130 if (declarator->kind == cdk_function)
2131 if (DECL_ARGUMENTS (d) == NULL_TREE)
2132 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2133 if (omp_declare_simd_clauses.exists ())
2135 tree parms = NULL_TREE;
2136 if (d && TREE_CODE (d) == FUNCTION_DECL)
2138 struct c_declarator *ce = declarator;
2139 while (ce != NULL)
2140 if (ce->kind == cdk_function)
2142 parms = ce->u.arg_info->parms;
2143 break;
2145 else
2146 ce = ce->declarator;
2148 if (parms)
2149 temp_store_parm_decls (d, parms);
2150 c_finish_omp_declare_simd (parser, d, parms,
2151 omp_declare_simd_clauses);
2152 if (parms)
2153 temp_pop_parm_decls ();
2155 if (oacc_routine_data)
2156 c_finish_oacc_routine (oacc_routine_data, d, false);
2157 if (d)
2158 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2159 NULL_TREE, asm_name);
2161 if (c_parser_next_token_is_keyword (parser, RID_IN))
2163 if (d)
2164 *objc_foreach_object_declaration = d;
2165 else
2166 *objc_foreach_object_declaration = error_mark_node;
2169 if (c_parser_next_token_is (parser, CPP_COMMA))
2171 if (auto_type_p)
2173 error_at (here,
2174 "%<__auto_type%> may only be used with"
2175 " a single declarator");
2176 c_parser_skip_to_end_of_block_or_statement (parser);
2177 return;
2179 c_parser_consume_token (parser);
2180 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2181 all_prefix_attrs = chainon (c_parser_attributes (parser),
2182 prefix_attrs);
2183 else
2184 all_prefix_attrs = prefix_attrs;
2185 continue;
2187 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2189 c_parser_consume_token (parser);
2190 return;
2192 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2194 /* This can only happen in Objective-C: we found the
2195 'in' that terminates the declaration inside an
2196 Objective-C foreach statement. Do not consume the
2197 token, so that the caller can use it to determine
2198 that this indeed is a foreach context. */
2199 return;
2201 else
2203 c_parser_error (parser, "expected %<,%> or %<;%>");
2204 c_parser_skip_to_end_of_block_or_statement (parser);
2205 return;
2208 else if (auto_type_p)
2210 error_at (here,
2211 "%<__auto_type%> requires an initialized data declaration");
2212 c_parser_skip_to_end_of_block_or_statement (parser);
2213 return;
2215 else if (!fndef_ok)
2217 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2218 "%<asm%> or %<__attribute__%>");
2219 c_parser_skip_to_end_of_block_or_statement (parser);
2220 return;
2222 /* Function definition (nested or otherwise). */
2223 if (nested)
2225 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2226 c_push_function_context ();
2228 if (!start_function (specs, declarator, all_prefix_attrs))
2230 /* At this point we've consumed:
2231 declaration-specifiers declarator
2232 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2233 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2234 but the
2235 declaration-specifiers declarator
2236 aren't grokkable as a function definition, so we have
2237 an error. */
2238 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2239 if (c_parser_next_token_starts_declspecs (parser))
2241 /* If we have
2242 declaration-specifiers declarator decl-specs
2243 then assume we have a missing semicolon, which would
2244 give us:
2245 declaration-specifiers declarator decl-specs
2248 <~~~~~~~~~ declaration ~~~~~~~~~~>
2249 Use c_parser_require to get an error with a fix-it hint. */
2250 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2251 parser->error = false;
2253 else
2255 /* This can appear in many cases looking nothing like a
2256 function definition, so we don't give a more specific
2257 error suggesting there was one. */
2258 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2259 "or %<__attribute__%>");
2261 if (nested)
2262 c_pop_function_context ();
2263 break;
2266 if (DECL_DECLARED_INLINE_P (current_function_decl))
2267 tv = TV_PARSE_INLINE;
2268 else
2269 tv = TV_PARSE_FUNC;
2270 auto_timevar at (g_timer, tv);
2272 /* Parse old-style parameter declarations. ??? Attributes are
2273 not allowed to start declaration specifiers here because of a
2274 syntax conflict between a function declaration with attribute
2275 suffix and a function definition with an attribute prefix on
2276 first old-style parameter declaration. Following the old
2277 parser, they are not accepted on subsequent old-style
2278 parameter declarations either. However, there is no
2279 ambiguity after the first declaration, nor indeed on the
2280 first as long as we don't allow postfix attributes after a
2281 declarator with a nonempty identifier list in a definition;
2282 and postfix attributes have never been accepted here in
2283 function definitions either. */
2284 while (c_parser_next_token_is_not (parser, CPP_EOF)
2285 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2286 c_parser_declaration_or_fndef (parser, false, false, false,
2287 true, false, NULL, vNULL);
2288 store_parm_decls ();
2289 if (omp_declare_simd_clauses.exists ())
2290 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2291 omp_declare_simd_clauses);
2292 if (oacc_routine_data)
2293 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2294 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2295 = c_parser_peek_token (parser)->location;
2297 /* If the definition was marked with __GIMPLE then parse the
2298 function body as GIMPLE. */
2299 if (specs->gimple_p)
2301 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2302 bool saved = in_late_binary_op;
2303 in_late_binary_op = true;
2304 c_parser_parse_gimple_body (parser);
2305 in_late_binary_op = saved;
2307 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2308 consuming the function body. */
2309 else if (specs->rtl_p)
2311 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2313 /* Normally, store_parm_decls sets next_is_function_body,
2314 anticipating a function body. We need a push_scope/pop_scope
2315 pair to flush out this state, or subsequent function parsing
2316 will go wrong. */
2317 push_scope ();
2318 pop_scope ();
2320 finish_function ();
2321 return;
2323 else
2324 fnbody = c_parser_compound_statement (parser);
2325 tree fndecl = current_function_decl;
2326 if (nested)
2328 tree decl = current_function_decl;
2329 /* Mark nested functions as needing static-chain initially.
2330 lower_nested_functions will recompute it but the
2331 DECL_STATIC_CHAIN flag is also used before that happens,
2332 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2333 DECL_STATIC_CHAIN (decl) = 1;
2334 add_stmt (fnbody);
2335 finish_function ();
2336 c_pop_function_context ();
2337 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2339 else
2341 if (fnbody)
2342 add_stmt (fnbody);
2343 finish_function ();
2345 /* Get rid of the empty stmt list for GIMPLE. */
2346 if (specs->gimple_p)
2347 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2349 break;
2353 /* Parse an asm-definition (asm() outside a function body). This is a
2354 GNU extension.
2356 asm-definition:
2357 simple-asm-expr ;
2360 static void
2361 c_parser_asm_definition (c_parser *parser)
2363 tree asm_str = c_parser_simple_asm_expr (parser);
2364 if (asm_str)
2365 symtab->finalize_toplevel_asm (asm_str);
2366 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2369 /* Parse a static assertion (C11 6.7.10).
2371 static_assert-declaration:
2372 static_assert-declaration-no-semi ;
2375 static void
2376 c_parser_static_assert_declaration (c_parser *parser)
2378 c_parser_static_assert_declaration_no_semi (parser);
2379 if (parser->error
2380 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2381 c_parser_skip_to_end_of_block_or_statement (parser);
2384 /* Parse a static assertion (C11 6.7.10), without the trailing
2385 semicolon.
2387 static_assert-declaration-no-semi:
2388 _Static_assert ( constant-expression , string-literal )
2391 static void
2392 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2394 location_t assert_loc, value_loc;
2395 tree value;
2396 tree string;
2398 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2399 assert_loc = c_parser_peek_token (parser)->location;
2400 if (flag_isoc99)
2401 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2402 "ISO C99 does not support %<_Static_assert%>");
2403 else
2404 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2405 "ISO C90 does not support %<_Static_assert%>");
2406 c_parser_consume_token (parser);
2407 matching_parens parens;
2408 if (!parens.require_open (parser))
2409 return;
2410 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2411 value = c_parser_expr_no_commas (parser, NULL).value;
2412 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2413 parser->lex_untranslated_string = true;
2414 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2416 parser->lex_untranslated_string = false;
2417 return;
2419 switch (c_parser_peek_token (parser)->type)
2421 case CPP_STRING:
2422 case CPP_STRING16:
2423 case CPP_STRING32:
2424 case CPP_WSTRING:
2425 case CPP_UTF8STRING:
2426 string = c_parser_peek_token (parser)->value;
2427 c_parser_consume_token (parser);
2428 parser->lex_untranslated_string = false;
2429 break;
2430 default:
2431 c_parser_error (parser, "expected string literal");
2432 parser->lex_untranslated_string = false;
2433 return;
2435 parens.require_close (parser);
2437 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2439 error_at (value_loc, "expression in static assertion is not an integer");
2440 return;
2442 if (TREE_CODE (value) != INTEGER_CST)
2444 value = c_fully_fold (value, false, NULL);
2445 /* Strip no-op conversions. */
2446 STRIP_TYPE_NOPS (value);
2447 if (TREE_CODE (value) == INTEGER_CST)
2448 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2449 "is not an integer constant expression");
2451 if (TREE_CODE (value) != INTEGER_CST)
2453 error_at (value_loc, "expression in static assertion is not constant");
2454 return;
2456 constant_expression_warning (value);
2457 if (integer_zerop (value))
2458 error_at (assert_loc, "static assertion failed: %E", string);
2461 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2462 6.7, C11 6.7), adding them to SPECS (which may already include some).
2463 Storage class specifiers are accepted iff SCSPEC_OK; type
2464 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2465 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2466 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2468 declaration-specifiers:
2469 storage-class-specifier declaration-specifiers[opt]
2470 type-specifier declaration-specifiers[opt]
2471 type-qualifier declaration-specifiers[opt]
2472 function-specifier declaration-specifiers[opt]
2473 alignment-specifier declaration-specifiers[opt]
2475 Function specifiers (inline) are from C99, and are currently
2476 handled as storage class specifiers, as is __thread. Alignment
2477 specifiers are from C11.
2479 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2480 storage-class-specifier:
2481 typedef
2482 extern
2483 static
2484 auto
2485 register
2486 _Thread_local
2488 (_Thread_local is new in C11.)
2490 C99 6.7.4, C11 6.7.4:
2491 function-specifier:
2492 inline
2493 _Noreturn
2495 (_Noreturn is new in C11.)
2497 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2498 type-specifier:
2499 void
2500 char
2501 short
2503 long
2504 float
2505 double
2506 signed
2507 unsigned
2508 _Bool
2509 _Complex
2510 [_Imaginary removed in C99 TC2]
2511 struct-or-union-specifier
2512 enum-specifier
2513 typedef-name
2514 atomic-type-specifier
2516 (_Bool and _Complex are new in C99.)
2517 (atomic-type-specifier is new in C11.)
2519 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2521 type-qualifier:
2522 const
2523 restrict
2524 volatile
2525 address-space-qualifier
2526 _Atomic
2528 (restrict is new in C99.)
2529 (_Atomic is new in C11.)
2531 GNU extensions:
2533 declaration-specifiers:
2534 attributes declaration-specifiers[opt]
2536 type-qualifier:
2537 address-space
2539 address-space:
2540 identifier recognized by the target
2542 storage-class-specifier:
2543 __thread
2545 type-specifier:
2546 typeof-specifier
2547 __auto_type
2548 __intN
2549 _Decimal32
2550 _Decimal64
2551 _Decimal128
2552 _Fract
2553 _Accum
2554 _Sat
2556 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2557 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2559 atomic-type-specifier
2560 _Atomic ( type-name )
2562 Objective-C:
2564 type-specifier:
2565 class-name objc-protocol-refs[opt]
2566 typedef-name objc-protocol-refs
2567 objc-protocol-refs
2570 void
2571 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2572 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2573 bool alignspec_ok, bool auto_type_ok,
2574 enum c_lookahead_kind la)
2576 bool attrs_ok = start_attr_ok;
2577 bool seen_type = specs->typespec_kind != ctsk_none;
2579 if (!typespec_ok)
2580 gcc_assert (la == cla_prefer_id);
2582 while (c_parser_next_token_is (parser, CPP_NAME)
2583 || c_parser_next_token_is (parser, CPP_KEYWORD)
2584 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2586 struct c_typespec t;
2587 tree attrs;
2588 tree align;
2589 location_t loc = c_parser_peek_token (parser)->location;
2591 /* If we cannot accept a type, exit if the next token must start
2592 one. Also, if we already have seen a tagged definition,
2593 a typename would be an error anyway and likely the user
2594 has simply forgotten a semicolon, so we exit. */
2595 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2596 && c_parser_next_tokens_start_typename (parser, la)
2597 && !c_parser_next_token_is_qualifier (parser))
2598 break;
2600 if (c_parser_next_token_is (parser, CPP_NAME))
2602 c_token *name_token = c_parser_peek_token (parser);
2603 tree value = name_token->value;
2604 c_id_kind kind = name_token->id_kind;
2606 if (kind == C_ID_ADDRSPACE)
2608 addr_space_t as
2609 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2610 declspecs_add_addrspace (name_token->location, specs, as);
2611 c_parser_consume_token (parser);
2612 attrs_ok = true;
2613 continue;
2616 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2618 /* If we cannot accept a type, and the next token must start one,
2619 exit. Do the same if we already have seen a tagged definition,
2620 since it would be an error anyway and likely the user has simply
2621 forgotten a semicolon. */
2622 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2623 break;
2625 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2626 a C_ID_CLASSNAME. */
2627 c_parser_consume_token (parser);
2628 seen_type = true;
2629 attrs_ok = true;
2630 if (kind == C_ID_ID)
2632 error_at (loc, "unknown type name %qE", value);
2633 t.kind = ctsk_typedef;
2634 t.spec = error_mark_node;
2636 else if (kind == C_ID_TYPENAME
2637 && (!c_dialect_objc ()
2638 || c_parser_next_token_is_not (parser, CPP_LESS)))
2640 t.kind = ctsk_typedef;
2641 /* For a typedef name, record the meaning, not the name.
2642 In case of 'foo foo, bar;'. */
2643 t.spec = lookup_name (value);
2645 else
2647 tree proto = NULL_TREE;
2648 gcc_assert (c_dialect_objc ());
2649 t.kind = ctsk_objc;
2650 if (c_parser_next_token_is (parser, CPP_LESS))
2651 proto = c_parser_objc_protocol_refs (parser);
2652 t.spec = objc_get_protocol_qualified_type (value, proto);
2654 t.expr = NULL_TREE;
2655 t.expr_const_operands = true;
2656 declspecs_add_type (name_token->location, specs, t);
2657 continue;
2659 if (c_parser_next_token_is (parser, CPP_LESS))
2661 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2662 nisse@lysator.liu.se. */
2663 tree proto;
2664 gcc_assert (c_dialect_objc ());
2665 if (!typespec_ok || seen_type)
2666 break;
2667 proto = c_parser_objc_protocol_refs (parser);
2668 t.kind = ctsk_objc;
2669 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2670 t.expr = NULL_TREE;
2671 t.expr_const_operands = true;
2672 declspecs_add_type (loc, specs, t);
2673 continue;
2675 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2676 switch (c_parser_peek_token (parser)->keyword)
2678 case RID_STATIC:
2679 case RID_EXTERN:
2680 case RID_REGISTER:
2681 case RID_TYPEDEF:
2682 case RID_INLINE:
2683 case RID_NORETURN:
2684 case RID_AUTO:
2685 case RID_THREAD:
2686 if (!scspec_ok)
2687 goto out;
2688 attrs_ok = true;
2689 /* TODO: Distinguish between function specifiers (inline, noreturn)
2690 and storage class specifiers, either here or in
2691 declspecs_add_scspec. */
2692 declspecs_add_scspec (loc, specs,
2693 c_parser_peek_token (parser)->value);
2694 c_parser_consume_token (parser);
2695 break;
2696 case RID_AUTO_TYPE:
2697 if (!auto_type_ok)
2698 goto out;
2699 /* Fall through. */
2700 case RID_UNSIGNED:
2701 case RID_LONG:
2702 case RID_SHORT:
2703 case RID_SIGNED:
2704 case RID_COMPLEX:
2705 case RID_INT:
2706 case RID_CHAR:
2707 case RID_FLOAT:
2708 case RID_DOUBLE:
2709 case RID_VOID:
2710 case RID_DFLOAT32:
2711 case RID_DFLOAT64:
2712 case RID_DFLOAT128:
2713 CASE_RID_FLOATN_NX:
2714 case RID_BOOL:
2715 case RID_FRACT:
2716 case RID_ACCUM:
2717 case RID_SAT:
2718 case RID_INT_N_0:
2719 case RID_INT_N_1:
2720 case RID_INT_N_2:
2721 case RID_INT_N_3:
2722 if (!typespec_ok)
2723 goto out;
2724 attrs_ok = true;
2725 seen_type = true;
2726 if (c_dialect_objc ())
2727 parser->objc_need_raw_identifier = true;
2728 t.kind = ctsk_resword;
2729 t.spec = c_parser_peek_token (parser)->value;
2730 t.expr = NULL_TREE;
2731 t.expr_const_operands = true;
2732 declspecs_add_type (loc, specs, t);
2733 c_parser_consume_token (parser);
2734 break;
2735 case RID_ENUM:
2736 if (!typespec_ok)
2737 goto out;
2738 attrs_ok = true;
2739 seen_type = true;
2740 t = c_parser_enum_specifier (parser);
2741 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2742 declspecs_add_type (loc, specs, t);
2743 break;
2744 case RID_STRUCT:
2745 case RID_UNION:
2746 if (!typespec_ok)
2747 goto out;
2748 attrs_ok = true;
2749 seen_type = true;
2750 t = c_parser_struct_or_union_specifier (parser);
2751 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2752 declspecs_add_type (loc, specs, t);
2753 break;
2754 case RID_TYPEOF:
2755 /* ??? The old parser rejected typeof after other type
2756 specifiers, but is a syntax error the best way of
2757 handling this? */
2758 if (!typespec_ok || seen_type)
2759 goto out;
2760 attrs_ok = true;
2761 seen_type = true;
2762 t = c_parser_typeof_specifier (parser);
2763 declspecs_add_type (loc, specs, t);
2764 break;
2765 case RID_ATOMIC:
2766 /* C parser handling of Objective-C constructs needs
2767 checking for correct lvalue-to-rvalue conversions, and
2768 the code in build_modify_expr handling various
2769 Objective-C cases, and that in build_unary_op handling
2770 Objective-C cases for increment / decrement, also needs
2771 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2772 and objc_types_are_equivalent may also need updates. */
2773 if (c_dialect_objc ())
2774 sorry ("%<_Atomic%> in Objective-C");
2775 if (flag_isoc99)
2776 pedwarn_c99 (loc, OPT_Wpedantic,
2777 "ISO C99 does not support the %<_Atomic%> qualifier");
2778 else
2779 pedwarn_c99 (loc, OPT_Wpedantic,
2780 "ISO C90 does not support the %<_Atomic%> qualifier");
2781 attrs_ok = true;
2782 tree value;
2783 value = c_parser_peek_token (parser)->value;
2784 c_parser_consume_token (parser);
2785 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2787 /* _Atomic ( type-name ). */
2788 seen_type = true;
2789 c_parser_consume_token (parser);
2790 struct c_type_name *type = c_parser_type_name (parser);
2791 t.kind = ctsk_typeof;
2792 t.spec = error_mark_node;
2793 t.expr = NULL_TREE;
2794 t.expr_const_operands = true;
2795 if (type != NULL)
2796 t.spec = groktypename (type, &t.expr,
2797 &t.expr_const_operands);
2798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2799 "expected %<)%>");
2800 if (t.spec != error_mark_node)
2802 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2803 error_at (loc, "%<_Atomic%>-qualified array type");
2804 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2805 error_at (loc, "%<_Atomic%>-qualified function type");
2806 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2807 error_at (loc, "%<_Atomic%> applied to a qualified type");
2808 else
2809 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2811 declspecs_add_type (loc, specs, t);
2813 else
2814 declspecs_add_qual (loc, specs, value);
2815 break;
2816 case RID_CONST:
2817 case RID_VOLATILE:
2818 case RID_RESTRICT:
2819 attrs_ok = true;
2820 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2821 c_parser_consume_token (parser);
2822 break;
2823 case RID_ATTRIBUTE:
2824 if (!attrs_ok)
2825 goto out;
2826 attrs = c_parser_attributes (parser);
2827 declspecs_add_attrs (loc, specs, attrs);
2828 break;
2829 case RID_ALIGNAS:
2830 if (!alignspec_ok)
2831 goto out;
2832 align = c_parser_alignas_specifier (parser);
2833 declspecs_add_alignas (loc, specs, align);
2834 break;
2835 case RID_GIMPLE:
2836 if (! flag_gimple)
2837 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2838 c_parser_consume_token (parser);
2839 specs->gimple_p = true;
2840 specs->locations[cdw_gimple] = loc;
2841 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2842 break;
2843 case RID_RTL:
2844 c_parser_consume_token (parser);
2845 specs->rtl_p = true;
2846 specs->locations[cdw_rtl] = loc;
2847 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2848 break;
2849 default:
2850 goto out;
2853 out: ;
2856 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2858 enum-specifier:
2859 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2860 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2861 enum attributes[opt] identifier
2863 The form with trailing comma is new in C99. The forms with
2864 attributes are GNU extensions. In GNU C, we accept any expression
2865 without commas in the syntax (assignment expressions, not just
2866 conditional expressions); assignment expressions will be diagnosed
2867 as non-constant.
2869 enumerator-list:
2870 enumerator
2871 enumerator-list , enumerator
2873 enumerator:
2874 enumeration-constant
2875 enumeration-constant = constant-expression
2877 GNU Extensions:
2879 enumerator:
2880 enumeration-constant attributes[opt]
2881 enumeration-constant attributes[opt] = constant-expression
2885 static struct c_typespec
2886 c_parser_enum_specifier (c_parser *parser)
2888 struct c_typespec ret;
2889 tree attrs;
2890 tree ident = NULL_TREE;
2891 location_t enum_loc;
2892 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2893 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2894 c_parser_consume_token (parser);
2895 attrs = c_parser_attributes (parser);
2896 enum_loc = c_parser_peek_token (parser)->location;
2897 /* Set the location in case we create a decl now. */
2898 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2899 if (c_parser_next_token_is (parser, CPP_NAME))
2901 ident = c_parser_peek_token (parser)->value;
2902 ident_loc = c_parser_peek_token (parser)->location;
2903 enum_loc = ident_loc;
2904 c_parser_consume_token (parser);
2906 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2908 /* Parse an enum definition. */
2909 struct c_enum_contents the_enum;
2910 tree type;
2911 tree postfix_attrs;
2912 /* We chain the enumerators in reverse order, then put them in
2913 forward order at the end. */
2914 tree values;
2915 timevar_push (TV_PARSE_ENUM);
2916 type = start_enum (enum_loc, &the_enum, ident);
2917 values = NULL_TREE;
2918 c_parser_consume_token (parser);
2919 while (true)
2921 tree enum_id;
2922 tree enum_value;
2923 tree enum_decl;
2924 bool seen_comma;
2925 c_token *token;
2926 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2927 location_t decl_loc, value_loc;
2928 if (c_parser_next_token_is_not (parser, CPP_NAME))
2930 /* Give a nicer error for "enum {}". */
2931 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2932 && !parser->error)
2934 error_at (c_parser_peek_token (parser)->location,
2935 "empty enum is invalid");
2936 parser->error = true;
2938 else
2939 c_parser_error (parser, "expected identifier");
2940 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2941 values = error_mark_node;
2942 break;
2944 token = c_parser_peek_token (parser);
2945 enum_id = token->value;
2946 /* Set the location in case we create a decl now. */
2947 c_parser_set_source_position_from_token (token);
2948 decl_loc = value_loc = token->location;
2949 c_parser_consume_token (parser);
2950 /* Parse any specified attributes. */
2951 tree enum_attrs = c_parser_attributes (parser);
2952 if (c_parser_next_token_is (parser, CPP_EQ))
2954 c_parser_consume_token (parser);
2955 value_loc = c_parser_peek_token (parser)->location;
2956 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2958 else
2959 enum_value = NULL_TREE;
2960 enum_decl = build_enumerator (decl_loc, value_loc,
2961 &the_enum, enum_id, enum_value);
2962 if (enum_attrs)
2963 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2964 TREE_CHAIN (enum_decl) = values;
2965 values = enum_decl;
2966 seen_comma = false;
2967 if (c_parser_next_token_is (parser, CPP_COMMA))
2969 comma_loc = c_parser_peek_token (parser)->location;
2970 seen_comma = true;
2971 c_parser_consume_token (parser);
2973 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2975 if (seen_comma)
2976 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2977 "comma at end of enumerator list");
2978 c_parser_consume_token (parser);
2979 break;
2981 if (!seen_comma)
2983 c_parser_error (parser, "expected %<,%> or %<}%>");
2984 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2985 values = error_mark_node;
2986 break;
2989 postfix_attrs = c_parser_attributes (parser);
2990 ret.spec = finish_enum (type, nreverse (values),
2991 chainon (attrs, postfix_attrs));
2992 ret.kind = ctsk_tagdef;
2993 ret.expr = NULL_TREE;
2994 ret.expr_const_operands = true;
2995 timevar_pop (TV_PARSE_ENUM);
2996 return ret;
2998 else if (!ident)
3000 c_parser_error (parser, "expected %<{%>");
3001 ret.spec = error_mark_node;
3002 ret.kind = ctsk_tagref;
3003 ret.expr = NULL_TREE;
3004 ret.expr_const_operands = true;
3005 return ret;
3007 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3008 /* In ISO C, enumerated types can be referred to only if already
3009 defined. */
3010 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3012 gcc_assert (ident);
3013 pedwarn (enum_loc, OPT_Wpedantic,
3014 "ISO C forbids forward references to %<enum%> types");
3016 return ret;
3019 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3021 struct-or-union-specifier:
3022 struct-or-union attributes[opt] identifier[opt]
3023 { struct-contents } attributes[opt]
3024 struct-or-union attributes[opt] identifier
3026 struct-contents:
3027 struct-declaration-list
3029 struct-declaration-list:
3030 struct-declaration ;
3031 struct-declaration-list struct-declaration ;
3033 GNU extensions:
3035 struct-contents:
3036 empty
3037 struct-declaration
3038 struct-declaration-list struct-declaration
3040 struct-declaration-list:
3041 struct-declaration-list ;
3044 (Note that in the syntax here, unlike that in ISO C, the semicolons
3045 are included here rather than in struct-declaration, in order to
3046 describe the syntax with extra semicolons and missing semicolon at
3047 end.)
3049 Objective-C:
3051 struct-declaration-list:
3052 @defs ( class-name )
3054 (Note this does not include a trailing semicolon, but can be
3055 followed by further declarations, and gets a pedwarn-if-pedantic
3056 when followed by a semicolon.) */
3058 static struct c_typespec
3059 c_parser_struct_or_union_specifier (c_parser *parser)
3061 struct c_typespec ret;
3062 tree attrs;
3063 tree ident = NULL_TREE;
3064 location_t struct_loc;
3065 location_t ident_loc = UNKNOWN_LOCATION;
3066 enum tree_code code;
3067 switch (c_parser_peek_token (parser)->keyword)
3069 case RID_STRUCT:
3070 code = RECORD_TYPE;
3071 break;
3072 case RID_UNION:
3073 code = UNION_TYPE;
3074 break;
3075 default:
3076 gcc_unreachable ();
3078 struct_loc = c_parser_peek_token (parser)->location;
3079 c_parser_consume_token (parser);
3080 attrs = c_parser_attributes (parser);
3082 /* Set the location in case we create a decl now. */
3083 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3085 if (c_parser_next_token_is (parser, CPP_NAME))
3087 ident = c_parser_peek_token (parser)->value;
3088 ident_loc = c_parser_peek_token (parser)->location;
3089 struct_loc = ident_loc;
3090 c_parser_consume_token (parser);
3092 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3094 /* Parse a struct or union definition. Start the scope of the
3095 tag before parsing components. */
3096 struct c_struct_parse_info *struct_info;
3097 tree type = start_struct (struct_loc, code, ident, &struct_info);
3098 tree postfix_attrs;
3099 /* We chain the components in reverse order, then put them in
3100 forward order at the end. Each struct-declaration may
3101 declare multiple components (comma-separated), so we must use
3102 chainon to join them, although when parsing each
3103 struct-declaration we can use TREE_CHAIN directly.
3105 The theory behind all this is that there will be more
3106 semicolon separated fields than comma separated fields, and
3107 so we'll be minimizing the number of node traversals required
3108 by chainon. */
3109 tree contents;
3110 timevar_push (TV_PARSE_STRUCT);
3111 contents = NULL_TREE;
3112 c_parser_consume_token (parser);
3113 /* Handle the Objective-C @defs construct,
3114 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3115 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3117 tree name;
3118 gcc_assert (c_dialect_objc ());
3119 c_parser_consume_token (parser);
3120 matching_parens parens;
3121 if (!parens.require_open (parser))
3122 goto end_at_defs;
3123 if (c_parser_next_token_is (parser, CPP_NAME)
3124 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3126 name = c_parser_peek_token (parser)->value;
3127 c_parser_consume_token (parser);
3129 else
3131 c_parser_error (parser, "expected class name");
3132 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3133 goto end_at_defs;
3135 parens.skip_until_found_close (parser);
3136 contents = nreverse (objc_get_class_ivars (name));
3138 end_at_defs:
3139 /* Parse the struct-declarations and semicolons. Problems with
3140 semicolons are diagnosed here; empty structures are diagnosed
3141 elsewhere. */
3142 while (true)
3144 tree decls;
3145 /* Parse any stray semicolon. */
3146 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3148 location_t semicolon_loc
3149 = c_parser_peek_token (parser)->location;
3150 gcc_rich_location richloc (semicolon_loc);
3151 richloc.add_fixit_remove ();
3152 pedwarn (&richloc, OPT_Wpedantic,
3153 "extra semicolon in struct or union specified");
3154 c_parser_consume_token (parser);
3155 continue;
3157 /* Stop if at the end of the struct or union contents. */
3158 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3160 c_parser_consume_token (parser);
3161 break;
3163 /* Accept #pragmas at struct scope. */
3164 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3166 c_parser_pragma (parser, pragma_struct, NULL);
3167 continue;
3169 /* Parse some comma-separated declarations, but not the
3170 trailing semicolon if any. */
3171 decls = c_parser_struct_declaration (parser);
3172 contents = chainon (decls, contents);
3173 /* If no semicolon follows, either we have a parse error or
3174 are at the end of the struct or union and should
3175 pedwarn. */
3176 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3177 c_parser_consume_token (parser);
3178 else
3180 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3181 pedwarn (c_parser_peek_token (parser)->location, 0,
3182 "no semicolon at end of struct or union");
3183 else if (parser->error
3184 || !c_parser_next_token_starts_declspecs (parser))
3186 c_parser_error (parser, "expected %<;%>");
3187 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3188 break;
3191 /* If we come here, we have already emitted an error
3192 for an expected `;', identifier or `(', and we also
3193 recovered already. Go on with the next field. */
3196 postfix_attrs = c_parser_attributes (parser);
3197 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3198 chainon (attrs, postfix_attrs), struct_info);
3199 ret.kind = ctsk_tagdef;
3200 ret.expr = NULL_TREE;
3201 ret.expr_const_operands = true;
3202 timevar_pop (TV_PARSE_STRUCT);
3203 return ret;
3205 else if (!ident)
3207 c_parser_error (parser, "expected %<{%>");
3208 ret.spec = error_mark_node;
3209 ret.kind = ctsk_tagref;
3210 ret.expr = NULL_TREE;
3211 ret.expr_const_operands = true;
3212 return ret;
3214 ret = parser_xref_tag (ident_loc, code, ident);
3215 return ret;
3218 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3219 *without* the trailing semicolon.
3221 struct-declaration:
3222 specifier-qualifier-list struct-declarator-list
3223 static_assert-declaration-no-semi
3225 specifier-qualifier-list:
3226 type-specifier specifier-qualifier-list[opt]
3227 type-qualifier specifier-qualifier-list[opt]
3228 attributes specifier-qualifier-list[opt]
3230 struct-declarator-list:
3231 struct-declarator
3232 struct-declarator-list , attributes[opt] struct-declarator
3234 struct-declarator:
3235 declarator attributes[opt]
3236 declarator[opt] : constant-expression attributes[opt]
3238 GNU extensions:
3240 struct-declaration:
3241 __extension__ struct-declaration
3242 specifier-qualifier-list
3244 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3245 of attributes where shown is a GNU extension. In GNU C, we accept
3246 any expression without commas in the syntax (assignment
3247 expressions, not just conditional expressions); assignment
3248 expressions will be diagnosed as non-constant. */
3250 static tree
3251 c_parser_struct_declaration (c_parser *parser)
3253 struct c_declspecs *specs;
3254 tree prefix_attrs;
3255 tree all_prefix_attrs;
3256 tree decls;
3257 location_t decl_loc;
3258 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3260 int ext;
3261 tree decl;
3262 ext = disable_extension_diagnostics ();
3263 c_parser_consume_token (parser);
3264 decl = c_parser_struct_declaration (parser);
3265 restore_extension_diagnostics (ext);
3266 return decl;
3268 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3270 c_parser_static_assert_declaration_no_semi (parser);
3271 return NULL_TREE;
3273 specs = build_null_declspecs ();
3274 decl_loc = c_parser_peek_token (parser)->location;
3275 /* Strictly by the standard, we shouldn't allow _Alignas here,
3276 but it appears to have been intended to allow it there, so
3277 we're keeping it as it is until WG14 reaches a conclusion
3278 of N1731.
3279 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3280 c_parser_declspecs (parser, specs, false, true, true,
3281 true, false, cla_nonabstract_decl);
3282 if (parser->error)
3283 return NULL_TREE;
3284 if (!specs->declspecs_seen_p)
3286 c_parser_error (parser, "expected specifier-qualifier-list");
3287 return NULL_TREE;
3289 finish_declspecs (specs);
3290 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3291 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3293 tree ret;
3294 if (specs->typespec_kind == ctsk_none)
3296 pedwarn (decl_loc, OPT_Wpedantic,
3297 "ISO C forbids member declarations with no members");
3298 shadow_tag_warned (specs, pedantic);
3299 ret = NULL_TREE;
3301 else
3303 /* Support for unnamed structs or unions as members of
3304 structs or unions (which is [a] useful and [b] supports
3305 MS P-SDK). */
3306 tree attrs = NULL;
3308 ret = grokfield (c_parser_peek_token (parser)->location,
3309 build_id_declarator (NULL_TREE), specs,
3310 NULL_TREE, &attrs);
3311 if (ret)
3312 decl_attributes (&ret, attrs, 0);
3314 return ret;
3317 /* Provide better error recovery. Note that a type name here is valid,
3318 and will be treated as a field name. */
3319 if (specs->typespec_kind == ctsk_tagdef
3320 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3321 && c_parser_next_token_starts_declspecs (parser)
3322 && !c_parser_next_token_is (parser, CPP_NAME))
3324 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3325 parser->error = false;
3326 return NULL_TREE;
3329 pending_xref_error ();
3330 prefix_attrs = specs->attrs;
3331 all_prefix_attrs = prefix_attrs;
3332 specs->attrs = NULL_TREE;
3333 decls = NULL_TREE;
3334 while (true)
3336 /* Declaring one or more declarators or un-named bit-fields. */
3337 struct c_declarator *declarator;
3338 bool dummy = false;
3339 if (c_parser_next_token_is (parser, CPP_COLON))
3340 declarator = build_id_declarator (NULL_TREE);
3341 else
3342 declarator = c_parser_declarator (parser,
3343 specs->typespec_kind != ctsk_none,
3344 C_DTR_NORMAL, &dummy);
3345 if (declarator == NULL)
3347 c_parser_skip_to_end_of_block_or_statement (parser);
3348 break;
3350 if (c_parser_next_token_is (parser, CPP_COLON)
3351 || c_parser_next_token_is (parser, CPP_COMMA)
3352 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3353 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3354 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3356 tree postfix_attrs = NULL_TREE;
3357 tree width = NULL_TREE;
3358 tree d;
3359 if (c_parser_next_token_is (parser, CPP_COLON))
3361 c_parser_consume_token (parser);
3362 width = c_parser_expr_no_commas (parser, NULL).value;
3364 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3365 postfix_attrs = c_parser_attributes (parser);
3366 d = grokfield (c_parser_peek_token (parser)->location,
3367 declarator, specs, width, &all_prefix_attrs);
3368 decl_attributes (&d, chainon (postfix_attrs,
3369 all_prefix_attrs), 0);
3370 DECL_CHAIN (d) = decls;
3371 decls = d;
3372 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3373 all_prefix_attrs = chainon (c_parser_attributes (parser),
3374 prefix_attrs);
3375 else
3376 all_prefix_attrs = prefix_attrs;
3377 if (c_parser_next_token_is (parser, CPP_COMMA))
3378 c_parser_consume_token (parser);
3379 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3380 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3382 /* Semicolon consumed in caller. */
3383 break;
3385 else
3387 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3388 break;
3391 else
3393 c_parser_error (parser,
3394 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3395 "%<__attribute__%>");
3396 break;
3399 return decls;
3402 /* Parse a typeof specifier (a GNU extension).
3404 typeof-specifier:
3405 typeof ( expression )
3406 typeof ( type-name )
3409 static struct c_typespec
3410 c_parser_typeof_specifier (c_parser *parser)
3412 struct c_typespec ret;
3413 ret.kind = ctsk_typeof;
3414 ret.spec = error_mark_node;
3415 ret.expr = NULL_TREE;
3416 ret.expr_const_operands = true;
3417 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3418 c_parser_consume_token (parser);
3419 c_inhibit_evaluation_warnings++;
3420 in_typeof++;
3421 matching_parens parens;
3422 if (!parens.require_open (parser))
3424 c_inhibit_evaluation_warnings--;
3425 in_typeof--;
3426 return ret;
3428 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3430 struct c_type_name *type = c_parser_type_name (parser);
3431 c_inhibit_evaluation_warnings--;
3432 in_typeof--;
3433 if (type != NULL)
3435 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3436 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3439 else
3441 bool was_vm;
3442 location_t here = c_parser_peek_token (parser)->location;
3443 struct c_expr expr = c_parser_expression (parser);
3444 c_inhibit_evaluation_warnings--;
3445 in_typeof--;
3446 if (TREE_CODE (expr.value) == COMPONENT_REF
3447 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3448 error_at (here, "%<typeof%> applied to a bit-field");
3449 mark_exp_read (expr.value);
3450 ret.spec = TREE_TYPE (expr.value);
3451 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3452 /* This is returned with the type so that when the type is
3453 evaluated, this can be evaluated. */
3454 if (was_vm)
3455 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3456 pop_maybe_used (was_vm);
3457 /* For use in macros such as those in <stdatomic.h>, remove all
3458 qualifiers from atomic types. (const can be an issue for more macros
3459 using typeof than just the <stdatomic.h> ones.) */
3460 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3461 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3463 parens.skip_until_found_close (parser);
3464 return ret;
3467 /* Parse an alignment-specifier.
3469 C11 6.7.5:
3471 alignment-specifier:
3472 _Alignas ( type-name )
3473 _Alignas ( constant-expression )
3476 static tree
3477 c_parser_alignas_specifier (c_parser * parser)
3479 tree ret = error_mark_node;
3480 location_t loc = c_parser_peek_token (parser)->location;
3481 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3482 c_parser_consume_token (parser);
3483 if (flag_isoc99)
3484 pedwarn_c99 (loc, OPT_Wpedantic,
3485 "ISO C99 does not support %<_Alignas%>");
3486 else
3487 pedwarn_c99 (loc, OPT_Wpedantic,
3488 "ISO C90 does not support %<_Alignas%>");
3489 matching_parens parens;
3490 if (!parens.require_open (parser))
3491 return ret;
3492 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3494 struct c_type_name *type = c_parser_type_name (parser);
3495 if (type != NULL)
3496 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3497 false, true, 1);
3499 else
3500 ret = c_parser_expr_no_commas (parser, NULL).value;
3501 parens.skip_until_found_close (parser);
3502 return ret;
3505 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3506 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3507 a typedef name may be redeclared; otherwise it may not. KIND
3508 indicates which kind of declarator is wanted. Returns a valid
3509 declarator except in the case of a syntax error in which case NULL is
3510 returned. *SEEN_ID is set to true if an identifier being declared is
3511 seen; this is used to diagnose bad forms of abstract array declarators
3512 and to determine whether an identifier list is syntactically permitted.
3514 declarator:
3515 pointer[opt] direct-declarator
3517 direct-declarator:
3518 identifier
3519 ( attributes[opt] declarator )
3520 direct-declarator array-declarator
3521 direct-declarator ( parameter-type-list )
3522 direct-declarator ( identifier-list[opt] )
3524 pointer:
3525 * type-qualifier-list[opt]
3526 * type-qualifier-list[opt] pointer
3528 type-qualifier-list:
3529 type-qualifier
3530 attributes
3531 type-qualifier-list type-qualifier
3532 type-qualifier-list attributes
3534 array-declarator:
3535 [ type-qualifier-list[opt] assignment-expression[opt] ]
3536 [ static type-qualifier-list[opt] assignment-expression ]
3537 [ type-qualifier-list static assignment-expression ]
3538 [ type-qualifier-list[opt] * ]
3540 parameter-type-list:
3541 parameter-list
3542 parameter-list , ...
3544 parameter-list:
3545 parameter-declaration
3546 parameter-list , parameter-declaration
3548 parameter-declaration:
3549 declaration-specifiers declarator attributes[opt]
3550 declaration-specifiers abstract-declarator[opt] attributes[opt]
3552 identifier-list:
3553 identifier
3554 identifier-list , identifier
3556 abstract-declarator:
3557 pointer
3558 pointer[opt] direct-abstract-declarator
3560 direct-abstract-declarator:
3561 ( attributes[opt] abstract-declarator )
3562 direct-abstract-declarator[opt] array-declarator
3563 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3565 GNU extensions:
3567 direct-declarator:
3568 direct-declarator ( parameter-forward-declarations
3569 parameter-type-list[opt] )
3571 direct-abstract-declarator:
3572 direct-abstract-declarator[opt] ( parameter-forward-declarations
3573 parameter-type-list[opt] )
3575 parameter-forward-declarations:
3576 parameter-list ;
3577 parameter-forward-declarations parameter-list ;
3579 The uses of attributes shown above are GNU extensions.
3581 Some forms of array declarator are not included in C99 in the
3582 syntax for abstract declarators; these are disallowed elsewhere.
3583 This may be a defect (DR#289).
3585 This function also accepts an omitted abstract declarator as being
3586 an abstract declarator, although not part of the formal syntax. */
3588 struct c_declarator *
3589 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3590 bool *seen_id)
3592 /* Parse any initial pointer part. */
3593 if (c_parser_next_token_is (parser, CPP_MULT))
3595 struct c_declspecs *quals_attrs = build_null_declspecs ();
3596 struct c_declarator *inner;
3597 c_parser_consume_token (parser);
3598 c_parser_declspecs (parser, quals_attrs, false, false, true,
3599 false, false, cla_prefer_id);
3600 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3601 if (inner == NULL)
3602 return NULL;
3603 else
3604 return make_pointer_declarator (quals_attrs, inner);
3606 /* Now we have a direct declarator, direct abstract declarator or
3607 nothing (which counts as a direct abstract declarator here). */
3608 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3611 /* Parse a direct declarator or direct abstract declarator; arguments
3612 as c_parser_declarator. */
3614 static struct c_declarator *
3615 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3616 bool *seen_id)
3618 /* The direct declarator must start with an identifier (possibly
3619 omitted) or a parenthesized declarator (possibly abstract). In
3620 an ordinary declarator, initial parentheses must start a
3621 parenthesized declarator. In an abstract declarator or parameter
3622 declarator, they could start a parenthesized declarator or a
3623 parameter list. To tell which, the open parenthesis and any
3624 following attributes must be read. If a declaration specifier
3625 follows, then it is a parameter list; if the specifier is a
3626 typedef name, there might be an ambiguity about redeclaring it,
3627 which is resolved in the direction of treating it as a typedef
3628 name. If a close parenthesis follows, it is also an empty
3629 parameter list, as the syntax does not permit empty abstract
3630 declarators. Otherwise, it is a parenthesized declarator (in
3631 which case the analysis may be repeated inside it, recursively).
3633 ??? There is an ambiguity in a parameter declaration "int
3634 (__attribute__((foo)) x)", where x is not a typedef name: it
3635 could be an abstract declarator for a function, or declare x with
3636 parentheses. The proper resolution of this ambiguity needs
3637 documenting. At present we follow an accident of the old
3638 parser's implementation, whereby the first parameter must have
3639 some declaration specifiers other than just attributes. Thus as
3640 a parameter declaration it is treated as a parenthesized
3641 parameter named x, and as an abstract declarator it is
3642 rejected.
3644 ??? Also following the old parser, attributes inside an empty
3645 parameter list are ignored, making it a list not yielding a
3646 prototype, rather than giving an error or making it have one
3647 parameter with implicit type int.
3649 ??? Also following the old parser, typedef names may be
3650 redeclared in declarators, but not Objective-C class names. */
3652 if (kind != C_DTR_ABSTRACT
3653 && c_parser_next_token_is (parser, CPP_NAME)
3654 && ((type_seen_p
3655 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3656 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3657 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3659 struct c_declarator *inner
3660 = build_id_declarator (c_parser_peek_token (parser)->value);
3661 *seen_id = true;
3662 inner->id_loc = c_parser_peek_token (parser)->location;
3663 c_parser_consume_token (parser);
3664 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3667 if (kind != C_DTR_NORMAL
3668 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3670 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3671 inner->id_loc = c_parser_peek_token (parser)->location;
3672 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3675 /* Either we are at the end of an abstract declarator, or we have
3676 parentheses. */
3678 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3680 tree attrs;
3681 struct c_declarator *inner;
3682 c_parser_consume_token (parser);
3683 attrs = c_parser_attributes (parser);
3684 if (kind != C_DTR_NORMAL
3685 && (c_parser_next_token_starts_declspecs (parser)
3686 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3688 struct c_arg_info *args
3689 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3690 attrs);
3691 if (args == NULL)
3692 return NULL;
3693 else
3695 inner
3696 = build_function_declarator (args,
3697 build_id_declarator (NULL_TREE));
3698 return c_parser_direct_declarator_inner (parser, *seen_id,
3699 inner);
3702 /* A parenthesized declarator. */
3703 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3704 if (inner != NULL && attrs != NULL)
3705 inner = build_attrs_declarator (attrs, inner);
3706 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3708 c_parser_consume_token (parser);
3709 if (inner == NULL)
3710 return NULL;
3711 else
3712 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3714 else
3716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3717 "expected %<)%>");
3718 return NULL;
3721 else
3723 if (kind == C_DTR_NORMAL)
3725 c_parser_error (parser, "expected identifier or %<(%>");
3726 return NULL;
3728 else
3729 return build_id_declarator (NULL_TREE);
3733 /* Parse part of a direct declarator or direct abstract declarator,
3734 given that some (in INNER) has already been parsed; ID_PRESENT is
3735 true if an identifier is present, false for an abstract
3736 declarator. */
3738 static struct c_declarator *
3739 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3740 struct c_declarator *inner)
3742 /* Parse a sequence of array declarators and parameter lists. */
3743 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3745 location_t brace_loc = c_parser_peek_token (parser)->location;
3746 struct c_declarator *declarator;
3747 struct c_declspecs *quals_attrs = build_null_declspecs ();
3748 bool static_seen;
3749 bool star_seen;
3750 struct c_expr dimen;
3751 dimen.value = NULL_TREE;
3752 dimen.original_code = ERROR_MARK;
3753 dimen.original_type = NULL_TREE;
3754 c_parser_consume_token (parser);
3755 c_parser_declspecs (parser, quals_attrs, false, false, true,
3756 false, false, cla_prefer_id);
3757 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3758 if (static_seen)
3759 c_parser_consume_token (parser);
3760 if (static_seen && !quals_attrs->declspecs_seen_p)
3761 c_parser_declspecs (parser, quals_attrs, false, false, true,
3762 false, false, cla_prefer_id);
3763 if (!quals_attrs->declspecs_seen_p)
3764 quals_attrs = NULL;
3765 /* If "static" is present, there must be an array dimension.
3766 Otherwise, there may be a dimension, "*", or no
3767 dimension. */
3768 if (static_seen)
3770 star_seen = false;
3771 dimen = c_parser_expr_no_commas (parser, NULL);
3773 else
3775 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3777 dimen.value = NULL_TREE;
3778 star_seen = false;
3780 else if (c_parser_next_token_is (parser, CPP_MULT))
3782 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3784 dimen.value = NULL_TREE;
3785 star_seen = true;
3786 c_parser_consume_token (parser);
3788 else
3790 star_seen = false;
3791 dimen = c_parser_expr_no_commas (parser, NULL);
3794 else
3796 star_seen = false;
3797 dimen = c_parser_expr_no_commas (parser, NULL);
3800 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3801 c_parser_consume_token (parser);
3802 else
3804 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3805 "expected %<]%>");
3806 return NULL;
3808 if (dimen.value)
3809 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3810 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3811 static_seen, star_seen);
3812 if (declarator == NULL)
3813 return NULL;
3814 inner = set_array_declarator_inner (declarator, inner);
3815 return c_parser_direct_declarator_inner (parser, id_present, inner);
3817 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3819 tree attrs;
3820 struct c_arg_info *args;
3821 c_parser_consume_token (parser);
3822 attrs = c_parser_attributes (parser);
3823 args = c_parser_parms_declarator (parser, id_present, attrs);
3824 if (args == NULL)
3825 return NULL;
3826 else
3828 inner = build_function_declarator (args, inner);
3829 return c_parser_direct_declarator_inner (parser, id_present, inner);
3832 return inner;
3835 /* Parse a parameter list or identifier list, including the closing
3836 parenthesis but not the opening one. ATTRS are the attributes at
3837 the start of the list. ID_LIST_OK is true if an identifier list is
3838 acceptable; such a list must not have attributes at the start. */
3840 static struct c_arg_info *
3841 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3843 push_scope ();
3844 declare_parm_level ();
3845 /* If the list starts with an identifier, it is an identifier list.
3846 Otherwise, it is either a prototype list or an empty list. */
3847 if (id_list_ok
3848 && !attrs
3849 && c_parser_next_token_is (parser, CPP_NAME)
3850 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3852 /* Look ahead to detect typos in type names. */
3853 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3854 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3855 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3856 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3857 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3859 tree list = NULL_TREE, *nextp = &list;
3860 while (c_parser_next_token_is (parser, CPP_NAME)
3861 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3863 *nextp = build_tree_list (NULL_TREE,
3864 c_parser_peek_token (parser)->value);
3865 nextp = & TREE_CHAIN (*nextp);
3866 c_parser_consume_token (parser);
3867 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3868 break;
3869 c_parser_consume_token (parser);
3870 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3872 c_parser_error (parser, "expected identifier");
3873 break;
3876 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3878 struct c_arg_info *ret = build_arg_info ();
3879 ret->types = list;
3880 c_parser_consume_token (parser);
3881 pop_scope ();
3882 return ret;
3884 else
3886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3887 "expected %<)%>");
3888 pop_scope ();
3889 return NULL;
3892 else
3894 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3895 NULL);
3896 pop_scope ();
3897 return ret;
3901 /* Parse a parameter list (possibly empty), including the closing
3902 parenthesis but not the opening one. ATTRS are the attributes at
3903 the start of the list. EXPR is NULL or an expression that needs to
3904 be evaluated for the side effects of array size expressions in the
3905 parameters. */
3907 static struct c_arg_info *
3908 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3910 bool bad_parm = false;
3912 /* ??? Following the old parser, forward parameter declarations may
3913 use abstract declarators, and if no real parameter declarations
3914 follow the forward declarations then this is not diagnosed. Also
3915 note as above that attributes are ignored as the only contents of
3916 the parentheses, or as the only contents after forward
3917 declarations. */
3918 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3920 struct c_arg_info *ret = build_arg_info ();
3921 c_parser_consume_token (parser);
3922 return ret;
3924 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3926 struct c_arg_info *ret = build_arg_info ();
3928 if (flag_allow_parameterless_variadic_functions)
3930 /* F (...) is allowed. */
3931 ret->types = NULL_TREE;
3933 else
3935 /* Suppress -Wold-style-definition for this case. */
3936 ret->types = error_mark_node;
3937 error_at (c_parser_peek_token (parser)->location,
3938 "ISO C requires a named argument before %<...%>");
3940 c_parser_consume_token (parser);
3941 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3943 c_parser_consume_token (parser);
3944 return ret;
3946 else
3948 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3949 "expected %<)%>");
3950 return NULL;
3953 /* Nonempty list of parameters, either terminated with semicolon
3954 (forward declarations; recurse) or with close parenthesis (normal
3955 function) or with ", ... )" (variadic function). */
3956 while (true)
3958 /* Parse a parameter. */
3959 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3960 attrs = NULL_TREE;
3961 if (parm == NULL)
3962 bad_parm = true;
3963 else
3964 push_parm_decl (parm, &expr);
3965 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3967 tree new_attrs;
3968 c_parser_consume_token (parser);
3969 mark_forward_parm_decls ();
3970 new_attrs = c_parser_attributes (parser);
3971 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3973 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3975 c_parser_consume_token (parser);
3976 if (bad_parm)
3977 return NULL;
3978 else
3979 return get_parm_info (false, expr);
3981 if (!c_parser_require (parser, CPP_COMMA,
3982 "expected %<;%>, %<,%> or %<)%>",
3983 UNKNOWN_LOCATION, false))
3985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3986 return NULL;
3988 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3990 c_parser_consume_token (parser);
3991 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3993 c_parser_consume_token (parser);
3994 if (bad_parm)
3995 return NULL;
3996 else
3997 return get_parm_info (true, expr);
3999 else
4001 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4002 "expected %<)%>");
4003 return NULL;
4009 /* Parse a parameter declaration. ATTRS are the attributes at the
4010 start of the declaration if it is the first parameter. */
4012 static struct c_parm *
4013 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4015 struct c_declspecs *specs;
4016 struct c_declarator *declarator;
4017 tree prefix_attrs;
4018 tree postfix_attrs = NULL_TREE;
4019 bool dummy = false;
4021 /* Accept #pragmas between parameter declarations. */
4022 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4023 c_parser_pragma (parser, pragma_param, NULL);
4025 if (!c_parser_next_token_starts_declspecs (parser))
4027 c_token *token = c_parser_peek_token (parser);
4028 if (parser->error)
4029 return NULL;
4030 c_parser_set_source_position_from_token (token);
4031 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4033 name_hint hint = lookup_name_fuzzy (token->value,
4034 FUZZY_LOOKUP_TYPENAME,
4035 token->location);
4036 if (hint)
4038 gcc_rich_location richloc (token->location);
4039 richloc.add_fixit_replace (hint.suggestion ());
4040 error_at (&richloc,
4041 "unknown type name %qE; did you mean %qs?",
4042 token->value, hint.suggestion ());
4044 else
4045 error_at (token->location, "unknown type name %qE", token->value);
4046 parser->error = true;
4048 /* ??? In some Objective-C cases '...' isn't applicable so there
4049 should be a different message. */
4050 else
4051 c_parser_error (parser,
4052 "expected declaration specifiers or %<...%>");
4053 c_parser_skip_to_end_of_parameter (parser);
4054 return NULL;
4057 location_t start_loc = c_parser_peek_token (parser)->location;
4059 specs = build_null_declspecs ();
4060 if (attrs)
4062 declspecs_add_attrs (input_location, specs, attrs);
4063 attrs = NULL_TREE;
4065 c_parser_declspecs (parser, specs, true, true, true, true, false,
4066 cla_nonabstract_decl);
4067 finish_declspecs (specs);
4068 pending_xref_error ();
4069 prefix_attrs = specs->attrs;
4070 specs->attrs = NULL_TREE;
4071 declarator = c_parser_declarator (parser,
4072 specs->typespec_kind != ctsk_none,
4073 C_DTR_PARM, &dummy);
4074 if (declarator == NULL)
4076 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4077 return NULL;
4079 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4080 postfix_attrs = c_parser_attributes (parser);
4082 /* Generate a location for the parameter, ranging from the start of the
4083 initial token to the end of the final token.
4085 If we have a identifier, then use it for the caret location, e.g.
4087 extern int callee (int one, int (*two)(int, int), float three);
4088 ~~~~~~^~~~~~~~~~~~~~
4090 otherwise, reuse the start location for the caret location e.g.:
4092 extern int callee (int one, int (*)(int, int), float three);
4093 ^~~~~~~~~~~~~~~~~
4095 location_t end_loc = parser->last_token_location;
4097 /* Find any cdk_id declarator; determine if we have an identifier. */
4098 c_declarator *id_declarator = declarator;
4099 while (id_declarator && id_declarator->kind != cdk_id)
4100 id_declarator = id_declarator->declarator;
4101 location_t caret_loc = (id_declarator->u.id
4102 ? id_declarator->id_loc
4103 : start_loc);
4104 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4106 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4107 declarator, param_loc);
4110 /* Parse a string literal in an asm expression. It should not be
4111 translated, and wide string literals are an error although
4112 permitted by the syntax. This is a GNU extension.
4114 asm-string-literal:
4115 string-literal
4117 ??? At present, following the old parser, the caller needs to have
4118 set lex_untranslated_string to 1. It would be better to follow the
4119 C++ parser rather than using this kludge. */
4121 static tree
4122 c_parser_asm_string_literal (c_parser *parser)
4124 tree str;
4125 int save_flag = warn_overlength_strings;
4126 warn_overlength_strings = 0;
4127 if (c_parser_next_token_is (parser, CPP_STRING))
4129 str = c_parser_peek_token (parser)->value;
4130 c_parser_consume_token (parser);
4132 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4134 error_at (c_parser_peek_token (parser)->location,
4135 "wide string literal in %<asm%>");
4136 str = build_string (1, "");
4137 c_parser_consume_token (parser);
4139 else
4141 c_parser_error (parser, "expected string literal");
4142 str = NULL_TREE;
4144 warn_overlength_strings = save_flag;
4145 return str;
4148 /* Parse a simple asm expression. This is used in restricted
4149 contexts, where a full expression with inputs and outputs does not
4150 make sense. This is a GNU extension.
4152 simple-asm-expr:
4153 asm ( asm-string-literal )
4156 static tree
4157 c_parser_simple_asm_expr (c_parser *parser)
4159 tree str;
4160 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4161 /* ??? Follow the C++ parser rather than using the
4162 lex_untranslated_string kludge. */
4163 parser->lex_untranslated_string = true;
4164 c_parser_consume_token (parser);
4165 matching_parens parens;
4166 if (!parens.require_open (parser))
4168 parser->lex_untranslated_string = false;
4169 return NULL_TREE;
4171 str = c_parser_asm_string_literal (parser);
4172 parser->lex_untranslated_string = false;
4173 if (!parens.require_close (parser))
4175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4176 return NULL_TREE;
4178 return str;
4181 static tree
4182 c_parser_attribute_any_word (c_parser *parser)
4184 tree attr_name = NULL_TREE;
4186 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4188 /* ??? See comment above about what keywords are accepted here. */
4189 bool ok;
4190 switch (c_parser_peek_token (parser)->keyword)
4192 case RID_STATIC:
4193 case RID_UNSIGNED:
4194 case RID_LONG:
4195 case RID_CONST:
4196 case RID_EXTERN:
4197 case RID_REGISTER:
4198 case RID_TYPEDEF:
4199 case RID_SHORT:
4200 case RID_INLINE:
4201 case RID_NORETURN:
4202 case RID_VOLATILE:
4203 case RID_SIGNED:
4204 case RID_AUTO:
4205 case RID_RESTRICT:
4206 case RID_COMPLEX:
4207 case RID_THREAD:
4208 case RID_INT:
4209 case RID_CHAR:
4210 case RID_FLOAT:
4211 case RID_DOUBLE:
4212 case RID_VOID:
4213 case RID_DFLOAT32:
4214 case RID_DFLOAT64:
4215 case RID_DFLOAT128:
4216 CASE_RID_FLOATN_NX:
4217 case RID_BOOL:
4218 case RID_FRACT:
4219 case RID_ACCUM:
4220 case RID_SAT:
4221 case RID_TRANSACTION_ATOMIC:
4222 case RID_TRANSACTION_CANCEL:
4223 case RID_ATOMIC:
4224 case RID_AUTO_TYPE:
4225 case RID_INT_N_0:
4226 case RID_INT_N_1:
4227 case RID_INT_N_2:
4228 case RID_INT_N_3:
4229 ok = true;
4230 break;
4231 default:
4232 ok = false;
4233 break;
4235 if (!ok)
4236 return NULL_TREE;
4238 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4239 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4241 else if (c_parser_next_token_is (parser, CPP_NAME))
4242 attr_name = c_parser_peek_token (parser)->value;
4244 return attr_name;
4247 /* Parse (possibly empty) attributes. This is a GNU extension.
4249 attributes:
4250 empty
4251 attributes attribute
4253 attribute:
4254 __attribute__ ( ( attribute-list ) )
4256 attribute-list:
4257 attrib
4258 attribute_list , attrib
4260 attrib:
4261 empty
4262 any-word
4263 any-word ( identifier )
4264 any-word ( identifier , nonempty-expr-list )
4265 any-word ( expr-list )
4267 where the "identifier" must not be declared as a type, and
4268 "any-word" may be any identifier (including one declared as a
4269 type), a reserved word storage class specifier, type specifier or
4270 type qualifier. ??? This still leaves out most reserved keywords
4271 (following the old parser), shouldn't we include them, and why not
4272 allow identifiers declared as types to start the arguments? */
4274 static tree
4275 c_parser_attributes (c_parser *parser)
4277 tree attrs = NULL_TREE;
4278 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4280 /* ??? Follow the C++ parser rather than using the
4281 lex_untranslated_string kludge. */
4282 parser->lex_untranslated_string = true;
4283 /* Consume the `__attribute__' keyword. */
4284 c_parser_consume_token (parser);
4285 /* Look for the two `(' tokens. */
4286 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4288 parser->lex_untranslated_string = false;
4289 return attrs;
4291 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4293 parser->lex_untranslated_string = false;
4294 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4295 return attrs;
4297 /* Parse the attribute list. */
4298 while (c_parser_next_token_is (parser, CPP_COMMA)
4299 || c_parser_next_token_is (parser, CPP_NAME)
4300 || c_parser_next_token_is (parser, CPP_KEYWORD))
4302 tree attr, attr_name, attr_args;
4303 vec<tree, va_gc> *expr_list;
4304 if (c_parser_next_token_is (parser, CPP_COMMA))
4306 c_parser_consume_token (parser);
4307 continue;
4310 attr_name = c_parser_attribute_any_word (parser);
4311 if (attr_name == NULL)
4312 break;
4313 attr_name = canonicalize_attr_name (attr_name);
4314 c_parser_consume_token (parser);
4315 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4317 attr = build_tree_list (attr_name, NULL_TREE);
4318 /* Add this attribute to the list. */
4319 attrs = chainon (attrs, attr);
4320 /* If the next token isn't a comma, we're done. */
4321 if (!c_parser_next_token_is (parser, CPP_COMMA))
4322 break;
4323 continue;
4325 c_parser_consume_token (parser);
4326 /* Parse the attribute contents. If they start with an
4327 identifier which is followed by a comma or close
4328 parenthesis, then the arguments start with that
4329 identifier; otherwise they are an expression list.
4330 In objective-c the identifier may be a classname. */
4331 if (c_parser_next_token_is (parser, CPP_NAME)
4332 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4333 || (c_dialect_objc ()
4334 && c_parser_peek_token (parser)->id_kind
4335 == C_ID_CLASSNAME))
4336 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4337 || (c_parser_peek_2nd_token (parser)->type
4338 == CPP_CLOSE_PAREN))
4339 && (attribute_takes_identifier_p (attr_name)
4340 || (c_dialect_objc ()
4341 && c_parser_peek_token (parser)->id_kind
4342 == C_ID_CLASSNAME)))
4344 tree arg1 = c_parser_peek_token (parser)->value;
4345 c_parser_consume_token (parser);
4346 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4347 attr_args = build_tree_list (NULL_TREE, arg1);
4348 else
4350 tree tree_list;
4351 c_parser_consume_token (parser);
4352 expr_list = c_parser_expr_list (parser, false, true,
4353 NULL, NULL, NULL, NULL);
4354 tree_list = build_tree_list_vec (expr_list);
4355 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4356 release_tree_vector (expr_list);
4359 else
4361 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4362 attr_args = NULL_TREE;
4363 else
4365 expr_list = c_parser_expr_list (parser, false, true,
4366 NULL, NULL, NULL, NULL);
4367 attr_args = build_tree_list_vec (expr_list);
4368 release_tree_vector (expr_list);
4372 attr = build_tree_list (attr_name, attr_args);
4373 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4374 c_parser_consume_token (parser);
4375 else
4377 parser->lex_untranslated_string = false;
4378 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4379 "expected %<)%>");
4380 return attrs;
4382 /* Add this attribute to the list. */
4383 attrs = chainon (attrs, attr);
4384 /* If the next token isn't a comma, we're done. */
4385 if (!c_parser_next_token_is (parser, CPP_COMMA))
4386 break;
4388 /* Look for the two `)' tokens. */
4389 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4390 c_parser_consume_token (parser);
4391 else
4393 parser->lex_untranslated_string = false;
4394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4395 "expected %<)%>");
4396 return attrs;
4398 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4399 c_parser_consume_token (parser);
4400 else
4402 parser->lex_untranslated_string = false;
4403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4404 "expected %<)%>");
4405 return attrs;
4407 parser->lex_untranslated_string = false;
4410 return attrs;
4413 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7).
4415 type-name:
4416 specifier-qualifier-list abstract-declarator[opt]
4419 struct c_type_name *
4420 c_parser_type_name (c_parser *parser)
4422 struct c_declspecs *specs = build_null_declspecs ();
4423 struct c_declarator *declarator;
4424 struct c_type_name *ret;
4425 bool dummy = false;
4426 c_parser_declspecs (parser, specs, false, true, true, false, false,
4427 cla_prefer_type);
4428 if (!specs->declspecs_seen_p)
4430 c_parser_error (parser, "expected specifier-qualifier-list");
4431 return NULL;
4433 if (specs->type != error_mark_node)
4435 pending_xref_error ();
4436 finish_declspecs (specs);
4438 declarator = c_parser_declarator (parser,
4439 specs->typespec_kind != ctsk_none,
4440 C_DTR_ABSTRACT, &dummy);
4441 if (declarator == NULL)
4442 return NULL;
4443 ret = XOBNEW (&parser_obstack, struct c_type_name);
4444 ret->specs = specs;
4445 ret->declarator = declarator;
4446 return ret;
4449 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4451 initializer:
4452 assignment-expression
4453 { initializer-list }
4454 { initializer-list , }
4456 initializer-list:
4457 designation[opt] initializer
4458 initializer-list , designation[opt] initializer
4460 designation:
4461 designator-list =
4463 designator-list:
4464 designator
4465 designator-list designator
4467 designator:
4468 array-designator
4469 . identifier
4471 array-designator:
4472 [ constant-expression ]
4474 GNU extensions:
4476 initializer:
4479 designation:
4480 array-designator
4481 identifier :
4483 array-designator:
4484 [ constant-expression ... constant-expression ]
4486 Any expression without commas is accepted in the syntax for the
4487 constant-expressions, with non-constant expressions rejected later.
4489 This function is only used for top-level initializers; for nested
4490 ones, see c_parser_initval. */
4492 static struct c_expr
4493 c_parser_initializer (c_parser *parser)
4495 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4496 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4497 else
4499 struct c_expr ret;
4500 location_t loc = c_parser_peek_token (parser)->location;
4501 ret = c_parser_expr_no_commas (parser, NULL);
4502 if (TREE_CODE (ret.value) != STRING_CST
4503 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4504 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4505 return ret;
4509 /* The location of the last comma within the current initializer list,
4510 or UNKNOWN_LOCATION if not within one. */
4512 location_t last_init_list_comma;
4514 /* Parse a braced initializer list. TYPE is the type specified for a
4515 compound literal, and NULL_TREE for other initializers and for
4516 nested braced lists. NESTED_P is true for nested braced lists,
4517 false for the list of a compound literal or the list that is the
4518 top-level initializer in a declaration. */
4520 static struct c_expr
4521 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4522 struct obstack *outer_obstack)
4524 struct c_expr ret;
4525 struct obstack braced_init_obstack;
4526 location_t brace_loc = c_parser_peek_token (parser)->location;
4527 gcc_obstack_init (&braced_init_obstack);
4528 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4529 matching_braces braces;
4530 braces.consume_open (parser);
4531 if (nested_p)
4533 finish_implicit_inits (brace_loc, outer_obstack);
4534 push_init_level (brace_loc, 0, &braced_init_obstack);
4536 else
4537 really_start_incremental_init (type);
4538 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4540 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4542 else
4544 /* Parse a non-empty initializer list, possibly with a trailing
4545 comma. */
4546 while (true)
4548 c_parser_initelt (parser, &braced_init_obstack);
4549 if (parser->error)
4550 break;
4551 if (c_parser_next_token_is (parser, CPP_COMMA))
4553 last_init_list_comma = c_parser_peek_token (parser)->location;
4554 c_parser_consume_token (parser);
4556 else
4557 break;
4558 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4559 break;
4562 c_token *next_tok = c_parser_peek_token (parser);
4563 if (next_tok->type != CPP_CLOSE_BRACE)
4565 ret.value = error_mark_node;
4566 ret.original_code = ERROR_MARK;
4567 ret.original_type = NULL;
4568 braces.skip_until_found_close (parser);
4569 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4570 obstack_free (&braced_init_obstack, NULL);
4571 return ret;
4573 location_t close_loc = next_tok->location;
4574 c_parser_consume_token (parser);
4575 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4576 obstack_free (&braced_init_obstack, NULL);
4577 set_c_expr_source_range (&ret, brace_loc, close_loc);
4578 return ret;
4581 /* Parse a nested initializer, including designators. */
4583 static void
4584 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4586 /* Parse any designator or designator list. A single array
4587 designator may have the subsequent "=" omitted in GNU C, but a
4588 longer list or a structure member designator may not. */
4589 if (c_parser_next_token_is (parser, CPP_NAME)
4590 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4592 /* Old-style structure member designator. */
4593 set_init_label (c_parser_peek_token (parser)->location,
4594 c_parser_peek_token (parser)->value,
4595 c_parser_peek_token (parser)->location,
4596 braced_init_obstack);
4597 /* Use the colon as the error location. */
4598 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4599 "obsolete use of designated initializer with %<:%>");
4600 c_parser_consume_token (parser);
4601 c_parser_consume_token (parser);
4603 else
4605 /* des_seen is 0 if there have been no designators, 1 if there
4606 has been a single array designator and 2 otherwise. */
4607 int des_seen = 0;
4608 /* Location of a designator. */
4609 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4610 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4611 || c_parser_next_token_is (parser, CPP_DOT))
4613 int des_prev = des_seen;
4614 if (!des_seen)
4615 des_loc = c_parser_peek_token (parser)->location;
4616 if (des_seen < 2)
4617 des_seen++;
4618 if (c_parser_next_token_is (parser, CPP_DOT))
4620 des_seen = 2;
4621 c_parser_consume_token (parser);
4622 if (c_parser_next_token_is (parser, CPP_NAME))
4624 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4625 c_parser_peek_token (parser)->location,
4626 braced_init_obstack);
4627 c_parser_consume_token (parser);
4629 else
4631 struct c_expr init;
4632 init.value = error_mark_node;
4633 init.original_code = ERROR_MARK;
4634 init.original_type = NULL;
4635 c_parser_error (parser, "expected identifier");
4636 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4637 process_init_element (input_location, init, false,
4638 braced_init_obstack);
4639 return;
4642 else
4644 tree first, second;
4645 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4646 location_t array_index_loc = UNKNOWN_LOCATION;
4647 /* ??? Following the old parser, [ objc-receiver
4648 objc-message-args ] is accepted as an initializer,
4649 being distinguished from a designator by what follows
4650 the first assignment expression inside the square
4651 brackets, but after a first array designator a
4652 subsequent square bracket is for Objective-C taken to
4653 start an expression, using the obsolete form of
4654 designated initializer without '=', rather than
4655 possibly being a second level of designation: in LALR
4656 terms, the '[' is shifted rather than reducing
4657 designator to designator-list. */
4658 if (des_prev == 1 && c_dialect_objc ())
4660 des_seen = des_prev;
4661 break;
4663 if (des_prev == 0 && c_dialect_objc ())
4665 /* This might be an array designator or an
4666 Objective-C message expression. If the former,
4667 continue parsing here; if the latter, parse the
4668 remainder of the initializer given the starting
4669 primary-expression. ??? It might make sense to
4670 distinguish when des_prev == 1 as well; see
4671 previous comment. */
4672 tree rec, args;
4673 struct c_expr mexpr;
4674 c_parser_consume_token (parser);
4675 if (c_parser_peek_token (parser)->type == CPP_NAME
4676 && ((c_parser_peek_token (parser)->id_kind
4677 == C_ID_TYPENAME)
4678 || (c_parser_peek_token (parser)->id_kind
4679 == C_ID_CLASSNAME)))
4681 /* Type name receiver. */
4682 tree id = c_parser_peek_token (parser)->value;
4683 c_parser_consume_token (parser);
4684 rec = objc_get_class_reference (id);
4685 goto parse_message_args;
4687 first = c_parser_expr_no_commas (parser, NULL).value;
4688 mark_exp_read (first);
4689 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4690 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4691 goto array_desig_after_first;
4692 /* Expression receiver. So far only one part
4693 without commas has been parsed; there might be
4694 more of the expression. */
4695 rec = first;
4696 while (c_parser_next_token_is (parser, CPP_COMMA))
4698 struct c_expr next;
4699 location_t comma_loc, exp_loc;
4700 comma_loc = c_parser_peek_token (parser)->location;
4701 c_parser_consume_token (parser);
4702 exp_loc = c_parser_peek_token (parser)->location;
4703 next = c_parser_expr_no_commas (parser, NULL);
4704 next = convert_lvalue_to_rvalue (exp_loc, next,
4705 true, true);
4706 rec = build_compound_expr (comma_loc, rec, next.value);
4708 parse_message_args:
4709 /* Now parse the objc-message-args. */
4710 args = c_parser_objc_message_args (parser);
4711 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4712 "expected %<]%>");
4713 mexpr.value
4714 = objc_build_message_expr (rec, args);
4715 mexpr.original_code = ERROR_MARK;
4716 mexpr.original_type = NULL;
4717 /* Now parse and process the remainder of the
4718 initializer, starting with this message
4719 expression as a primary-expression. */
4720 c_parser_initval (parser, &mexpr, braced_init_obstack);
4721 return;
4723 c_parser_consume_token (parser);
4724 array_index_loc = c_parser_peek_token (parser)->location;
4725 first = c_parser_expr_no_commas (parser, NULL).value;
4726 mark_exp_read (first);
4727 array_desig_after_first:
4728 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4730 ellipsis_loc = c_parser_peek_token (parser)->location;
4731 c_parser_consume_token (parser);
4732 second = c_parser_expr_no_commas (parser, NULL).value;
4733 mark_exp_read (second);
4735 else
4736 second = NULL_TREE;
4737 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4739 c_parser_consume_token (parser);
4740 set_init_index (array_index_loc, first, second,
4741 braced_init_obstack);
4742 if (second)
4743 pedwarn (ellipsis_loc, OPT_Wpedantic,
4744 "ISO C forbids specifying range of elements to initialize");
4746 else
4747 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4748 "expected %<]%>");
4751 if (des_seen >= 1)
4753 if (c_parser_next_token_is (parser, CPP_EQ))
4755 pedwarn_c90 (des_loc, OPT_Wpedantic,
4756 "ISO C90 forbids specifying subobject "
4757 "to initialize");
4758 c_parser_consume_token (parser);
4760 else
4762 if (des_seen == 1)
4763 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4764 "obsolete use of designated initializer without %<=%>");
4765 else
4767 struct c_expr init;
4768 init.value = error_mark_node;
4769 init.original_code = ERROR_MARK;
4770 init.original_type = NULL;
4771 c_parser_error (parser, "expected %<=%>");
4772 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4773 process_init_element (input_location, init, false,
4774 braced_init_obstack);
4775 return;
4780 c_parser_initval (parser, NULL, braced_init_obstack);
4783 /* Parse a nested initializer; as c_parser_initializer but parses
4784 initializers within braced lists, after any designators have been
4785 applied. If AFTER is not NULL then it is an Objective-C message
4786 expression which is the primary-expression starting the
4787 initializer. */
4789 static void
4790 c_parser_initval (c_parser *parser, struct c_expr *after,
4791 struct obstack * braced_init_obstack)
4793 struct c_expr init;
4794 gcc_assert (!after || c_dialect_objc ());
4795 location_t loc = c_parser_peek_token (parser)->location;
4797 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4798 init = c_parser_braced_init (parser, NULL_TREE, true,
4799 braced_init_obstack);
4800 else
4802 init = c_parser_expr_no_commas (parser, after);
4803 if (init.value != NULL_TREE
4804 && TREE_CODE (init.value) != STRING_CST
4805 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4806 init = convert_lvalue_to_rvalue (loc, init, true, true);
4808 process_init_element (loc, init, false, braced_init_obstack);
4811 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4812 C99 6.8.2, C11 6.8.2).
4814 compound-statement:
4815 { block-item-list[opt] }
4816 { label-declarations block-item-list }
4818 block-item-list:
4819 block-item
4820 block-item-list block-item
4822 block-item:
4823 nested-declaration
4824 statement
4826 nested-declaration:
4827 declaration
4829 GNU extensions:
4831 compound-statement:
4832 { label-declarations block-item-list }
4834 nested-declaration:
4835 __extension__ nested-declaration
4836 nested-function-definition
4838 label-declarations:
4839 label-declaration
4840 label-declarations label-declaration
4842 label-declaration:
4843 __label__ identifier-list ;
4845 Allowing the mixing of declarations and code is new in C99. The
4846 GNU syntax also permits (not shown above) labels at the end of
4847 compound statements, which yield an error. We don't allow labels
4848 on declarations; this might seem like a natural extension, but
4849 there would be a conflict between attributes on the label and
4850 prefix attributes on the declaration. ??? The syntax follows the
4851 old parser in requiring something after label declarations.
4852 Although they are erroneous if the labels declared aren't defined,
4853 is it useful for the syntax to be this way?
4855 OpenACC:
4857 block-item:
4858 openacc-directive
4860 openacc-directive:
4861 update-directive
4863 OpenMP:
4865 block-item:
4866 openmp-directive
4868 openmp-directive:
4869 barrier-directive
4870 flush-directive
4871 taskwait-directive
4872 taskyield-directive
4873 cancel-directive
4874 cancellation-point-directive */
4876 static tree
4877 c_parser_compound_statement (c_parser *parser)
4879 tree stmt;
4880 location_t brace_loc;
4881 brace_loc = c_parser_peek_token (parser)->location;
4882 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4884 /* Ensure a scope is entered and left anyway to avoid confusion
4885 if we have just prepared to enter a function body. */
4886 stmt = c_begin_compound_stmt (true);
4887 c_end_compound_stmt (brace_loc, stmt, true);
4888 return error_mark_node;
4890 stmt = c_begin_compound_stmt (true);
4891 c_parser_compound_statement_nostart (parser);
4893 return c_end_compound_stmt (brace_loc, stmt, true);
4896 /* Parse a compound statement except for the opening brace. This is
4897 used for parsing both compound statements and statement expressions
4898 (which follow different paths to handling the opening). */
4900 static void
4901 c_parser_compound_statement_nostart (c_parser *parser)
4903 bool last_stmt = false;
4904 bool last_label = false;
4905 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4906 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4907 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4909 c_parser_consume_token (parser);
4910 return;
4912 mark_valid_location_for_stdc_pragma (true);
4913 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4915 /* Read zero or more forward-declarations for labels that nested
4916 functions can jump to. */
4917 mark_valid_location_for_stdc_pragma (false);
4918 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4920 label_loc = c_parser_peek_token (parser)->location;
4921 c_parser_consume_token (parser);
4922 /* Any identifiers, including those declared as type names,
4923 are OK here. */
4924 while (true)
4926 tree label;
4927 if (c_parser_next_token_is_not (parser, CPP_NAME))
4929 c_parser_error (parser, "expected identifier");
4930 break;
4932 label
4933 = declare_label (c_parser_peek_token (parser)->value);
4934 C_DECLARED_LABEL_FLAG (label) = 1;
4935 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4936 c_parser_consume_token (parser);
4937 if (c_parser_next_token_is (parser, CPP_COMMA))
4938 c_parser_consume_token (parser);
4939 else
4940 break;
4942 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4944 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4946 /* We must now have at least one statement, label or declaration. */
4947 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4949 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4950 c_parser_error (parser, "expected declaration or statement");
4951 c_parser_consume_token (parser);
4952 return;
4954 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4956 location_t loc = c_parser_peek_token (parser)->location;
4957 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4958 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4959 || (c_parser_next_token_is (parser, CPP_NAME)
4960 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4962 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4963 label_loc = c_parser_peek_2nd_token (parser)->location;
4964 else
4965 label_loc = c_parser_peek_token (parser)->location;
4966 last_label = true;
4967 last_stmt = false;
4968 mark_valid_location_for_stdc_pragma (false);
4969 c_parser_label (parser);
4971 else if (!last_label
4972 && c_parser_next_tokens_start_declaration (parser))
4974 last_label = false;
4975 mark_valid_location_for_stdc_pragma (false);
4976 bool fallthru_attr_p = false;
4977 c_parser_declaration_or_fndef (parser, true, true, true, true,
4978 true, NULL, vNULL, NULL,
4979 &fallthru_attr_p);
4980 if (last_stmt && !fallthru_attr_p)
4981 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4982 "ISO C90 forbids mixed declarations and code");
4983 last_stmt = fallthru_attr_p;
4985 else if (!last_label
4986 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4988 /* __extension__ can start a declaration, but is also an
4989 unary operator that can start an expression. Consume all
4990 but the last of a possible series of __extension__ to
4991 determine which. */
4992 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4993 && (c_parser_peek_2nd_token (parser)->keyword
4994 == RID_EXTENSION))
4995 c_parser_consume_token (parser);
4996 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4998 int ext;
4999 ext = disable_extension_diagnostics ();
5000 c_parser_consume_token (parser);
5001 last_label = false;
5002 mark_valid_location_for_stdc_pragma (false);
5003 c_parser_declaration_or_fndef (parser, true, true, true, true,
5004 true, NULL, vNULL);
5005 /* Following the old parser, __extension__ does not
5006 disable this diagnostic. */
5007 restore_extension_diagnostics (ext);
5008 if (last_stmt)
5009 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5010 "ISO C90 forbids mixed declarations and code");
5011 last_stmt = false;
5013 else
5014 goto statement;
5016 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5018 /* External pragmas, and some omp pragmas, are not associated
5019 with regular c code, and so are not to be considered statements
5020 syntactically. This ensures that the user doesn't put them
5021 places that would turn into syntax errors if the directive
5022 were ignored. */
5023 if (c_parser_pragma (parser,
5024 last_label ? pragma_stmt : pragma_compound,
5025 NULL))
5026 last_label = false, last_stmt = true;
5028 else if (c_parser_next_token_is (parser, CPP_EOF))
5030 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5031 c_parser_error (parser, "expected declaration or statement");
5032 return;
5034 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5036 if (parser->in_if_block)
5038 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5039 error_at (loc, "expected %<}%> before %<else%>");
5040 return;
5042 else
5044 error_at (loc, "%<else%> without a previous %<if%>");
5045 c_parser_consume_token (parser);
5046 continue;
5049 else
5051 statement:
5052 last_label = false;
5053 last_stmt = true;
5054 mark_valid_location_for_stdc_pragma (false);
5055 c_parser_statement_after_labels (parser, NULL);
5058 parser->error = false;
5060 if (last_label)
5061 error_at (label_loc, "label at end of compound statement");
5062 c_parser_consume_token (parser);
5063 /* Restore the value we started with. */
5064 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5067 /* Parse all consecutive labels. */
5069 static void
5070 c_parser_all_labels (c_parser *parser)
5072 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5073 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5074 || (c_parser_next_token_is (parser, CPP_NAME)
5075 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5076 c_parser_label (parser);
5079 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5081 label:
5082 identifier : attributes[opt]
5083 case constant-expression :
5084 default :
5086 GNU extensions:
5088 label:
5089 case constant-expression ... constant-expression :
5091 The use of attributes on labels is a GNU extension. The syntax in
5092 GNU C accepts any expressions without commas, non-constant
5093 expressions being rejected later. */
5095 static void
5096 c_parser_label (c_parser *parser)
5098 location_t loc1 = c_parser_peek_token (parser)->location;
5099 tree label = NULL_TREE;
5101 /* Remember whether this case or a user-defined label is allowed to fall
5102 through to. */
5103 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5105 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5107 tree exp1, exp2;
5108 c_parser_consume_token (parser);
5109 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5110 if (c_parser_next_token_is (parser, CPP_COLON))
5112 c_parser_consume_token (parser);
5113 label = do_case (loc1, exp1, NULL_TREE);
5115 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5117 c_parser_consume_token (parser);
5118 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5119 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5120 label = do_case (loc1, exp1, exp2);
5122 else
5123 c_parser_error (parser, "expected %<:%> or %<...%>");
5125 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5127 c_parser_consume_token (parser);
5128 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5129 label = do_case (loc1, NULL_TREE, NULL_TREE);
5131 else
5133 tree name = c_parser_peek_token (parser)->value;
5134 tree tlab;
5135 tree attrs;
5136 location_t loc2 = c_parser_peek_token (parser)->location;
5137 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5138 c_parser_consume_token (parser);
5139 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5140 c_parser_consume_token (parser);
5141 attrs = c_parser_attributes (parser);
5142 tlab = define_label (loc2, name);
5143 if (tlab)
5145 decl_attributes (&tlab, attrs, 0);
5146 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5149 if (label)
5151 if (TREE_CODE (label) == LABEL_EXPR)
5152 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5153 else
5154 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5156 /* Allow '__attribute__((fallthrough));'. */
5157 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5159 location_t loc = c_parser_peek_token (parser)->location;
5160 tree attrs = c_parser_attributes (parser);
5161 if (attribute_fallthrough_p (attrs))
5163 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5165 tree fn = build_call_expr_internal_loc (loc,
5166 IFN_FALLTHROUGH,
5167 void_type_node, 0);
5168 add_stmt (fn);
5170 else
5171 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5172 "not followed by %<;%>");
5174 else if (attrs != NULL_TREE)
5175 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5176 " can be applied to a null statement");
5178 if (c_parser_next_tokens_start_declaration (parser))
5180 error_at (c_parser_peek_token (parser)->location,
5181 "a label can only be part of a statement and "
5182 "a declaration is not a statement");
5183 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5184 /*static_assert_ok*/ true,
5185 /*empty_ok*/ true, /*nested*/ true,
5186 /*start_attr_ok*/ true, NULL,
5187 vNULL);
5192 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5194 statement:
5195 labeled-statement
5196 compound-statement
5197 expression-statement
5198 selection-statement
5199 iteration-statement
5200 jump-statement
5202 labeled-statement:
5203 label statement
5205 expression-statement:
5206 expression[opt] ;
5208 selection-statement:
5209 if-statement
5210 switch-statement
5212 iteration-statement:
5213 while-statement
5214 do-statement
5215 for-statement
5217 jump-statement:
5218 goto identifier ;
5219 continue ;
5220 break ;
5221 return expression[opt] ;
5223 GNU extensions:
5225 statement:
5226 asm-statement
5228 jump-statement:
5229 goto * expression ;
5231 expression-statement:
5232 attributes ;
5234 Objective-C:
5236 statement:
5237 objc-throw-statement
5238 objc-try-catch-statement
5239 objc-synchronized-statement
5241 objc-throw-statement:
5242 @throw expression ;
5243 @throw ;
5245 OpenACC:
5247 statement:
5248 openacc-construct
5250 openacc-construct:
5251 parallel-construct
5252 kernels-construct
5253 data-construct
5254 loop-construct
5256 parallel-construct:
5257 parallel-directive structured-block
5259 kernels-construct:
5260 kernels-directive structured-block
5262 data-construct:
5263 data-directive structured-block
5265 loop-construct:
5266 loop-directive structured-block
5268 OpenMP:
5270 statement:
5271 openmp-construct
5273 openmp-construct:
5274 parallel-construct
5275 for-construct
5276 simd-construct
5277 for-simd-construct
5278 sections-construct
5279 single-construct
5280 parallel-for-construct
5281 parallel-for-simd-construct
5282 parallel-sections-construct
5283 master-construct
5284 critical-construct
5285 atomic-construct
5286 ordered-construct
5288 parallel-construct:
5289 parallel-directive structured-block
5291 for-construct:
5292 for-directive iteration-statement
5294 simd-construct:
5295 simd-directive iteration-statements
5297 for-simd-construct:
5298 for-simd-directive iteration-statements
5300 sections-construct:
5301 sections-directive section-scope
5303 single-construct:
5304 single-directive structured-block
5306 parallel-for-construct:
5307 parallel-for-directive iteration-statement
5309 parallel-for-simd-construct:
5310 parallel-for-simd-directive iteration-statement
5312 parallel-sections-construct:
5313 parallel-sections-directive section-scope
5315 master-construct:
5316 master-directive structured-block
5318 critical-construct:
5319 critical-directive structured-block
5321 atomic-construct:
5322 atomic-directive expression-statement
5324 ordered-construct:
5325 ordered-directive structured-block
5327 Transactional Memory:
5329 statement:
5330 transaction-statement
5331 transaction-cancel-statement
5333 IF_P is used to track whether there's a (possibly labeled) if statement
5334 which is not enclosed in braces and has an else clause. This is used to
5335 implement -Wparentheses. */
5337 static void
5338 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5340 c_parser_all_labels (parser);
5341 if (loc_after_labels)
5342 *loc_after_labels = c_parser_peek_token (parser)->location;
5343 c_parser_statement_after_labels (parser, if_p, NULL);
5346 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5347 of if-else-if conditions.
5349 IF_P is used to track whether there's a (possibly labeled) if statement
5350 which is not enclosed in braces and has an else clause. This is used to
5351 implement -Wparentheses. */
5353 static void
5354 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5355 vec<tree> *chain)
5357 location_t loc = c_parser_peek_token (parser)->location;
5358 tree stmt = NULL_TREE;
5359 bool in_if_block = parser->in_if_block;
5360 parser->in_if_block = false;
5361 if (if_p != NULL)
5362 *if_p = false;
5363 switch (c_parser_peek_token (parser)->type)
5365 case CPP_OPEN_BRACE:
5366 add_stmt (c_parser_compound_statement (parser));
5367 break;
5368 case CPP_KEYWORD:
5369 switch (c_parser_peek_token (parser)->keyword)
5371 case RID_IF:
5372 c_parser_if_statement (parser, if_p, chain);
5373 break;
5374 case RID_SWITCH:
5375 c_parser_switch_statement (parser, if_p);
5376 break;
5377 case RID_WHILE:
5378 c_parser_while_statement (parser, false, if_p);
5379 break;
5380 case RID_DO:
5381 c_parser_do_statement (parser, false);
5382 break;
5383 case RID_FOR:
5384 c_parser_for_statement (parser, false, if_p);
5385 break;
5386 case RID_GOTO:
5387 c_parser_consume_token (parser);
5388 if (c_parser_next_token_is (parser, CPP_NAME))
5390 stmt = c_finish_goto_label (loc,
5391 c_parser_peek_token (parser)->value);
5392 c_parser_consume_token (parser);
5394 else if (c_parser_next_token_is (parser, CPP_MULT))
5396 struct c_expr val;
5398 c_parser_consume_token (parser);
5399 val = c_parser_expression (parser);
5400 val = convert_lvalue_to_rvalue (loc, val, false, true);
5401 stmt = c_finish_goto_ptr (loc, val.value);
5403 else
5404 c_parser_error (parser, "expected identifier or %<*%>");
5405 goto expect_semicolon;
5406 case RID_CONTINUE:
5407 c_parser_consume_token (parser);
5408 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5409 goto expect_semicolon;
5410 case RID_BREAK:
5411 c_parser_consume_token (parser);
5412 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5413 goto expect_semicolon;
5414 case RID_RETURN:
5415 c_parser_consume_token (parser);
5416 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5418 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5419 c_parser_consume_token (parser);
5421 else
5423 location_t xloc = c_parser_peek_token (parser)->location;
5424 struct c_expr expr = c_parser_expression_conv (parser);
5425 mark_exp_read (expr.value);
5426 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5427 expr.value, expr.original_type);
5428 goto expect_semicolon;
5430 break;
5431 case RID_ASM:
5432 stmt = c_parser_asm_statement (parser);
5433 break;
5434 case RID_TRANSACTION_ATOMIC:
5435 case RID_TRANSACTION_RELAXED:
5436 stmt = c_parser_transaction (parser,
5437 c_parser_peek_token (parser)->keyword);
5438 break;
5439 case RID_TRANSACTION_CANCEL:
5440 stmt = c_parser_transaction_cancel (parser);
5441 goto expect_semicolon;
5442 case RID_AT_THROW:
5443 gcc_assert (c_dialect_objc ());
5444 c_parser_consume_token (parser);
5445 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5447 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5448 c_parser_consume_token (parser);
5450 else
5452 struct c_expr expr = c_parser_expression (parser);
5453 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5454 expr.value = c_fully_fold (expr.value, false, NULL);
5455 stmt = objc_build_throw_stmt (loc, expr.value);
5456 goto expect_semicolon;
5458 break;
5459 case RID_AT_TRY:
5460 gcc_assert (c_dialect_objc ());
5461 c_parser_objc_try_catch_finally_statement (parser);
5462 break;
5463 case RID_AT_SYNCHRONIZED:
5464 gcc_assert (c_dialect_objc ());
5465 c_parser_objc_synchronized_statement (parser);
5466 break;
5467 case RID_ATTRIBUTE:
5469 /* Allow '__attribute__((fallthrough));'. */
5470 tree attrs = c_parser_attributes (parser);
5471 if (attribute_fallthrough_p (attrs))
5473 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5475 tree fn = build_call_expr_internal_loc (loc,
5476 IFN_FALLTHROUGH,
5477 void_type_node, 0);
5478 add_stmt (fn);
5479 /* Eat the ';'. */
5480 c_parser_consume_token (parser);
5482 else
5483 warning_at (loc, OPT_Wattributes,
5484 "%<fallthrough%> attribute not followed "
5485 "by %<;%>");
5487 else if (attrs != NULL_TREE)
5488 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5489 " can be applied to a null statement");
5490 break;
5492 default:
5493 goto expr_stmt;
5495 break;
5496 case CPP_SEMICOLON:
5497 c_parser_consume_token (parser);
5498 break;
5499 case CPP_CLOSE_PAREN:
5500 case CPP_CLOSE_SQUARE:
5501 /* Avoid infinite loop in error recovery:
5502 c_parser_skip_until_found stops at a closing nesting
5503 delimiter without consuming it, but here we need to consume
5504 it to proceed further. */
5505 c_parser_error (parser, "expected statement");
5506 c_parser_consume_token (parser);
5507 break;
5508 case CPP_PRAGMA:
5509 c_parser_pragma (parser, pragma_stmt, if_p);
5510 break;
5511 default:
5512 expr_stmt:
5513 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5514 expect_semicolon:
5515 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5516 break;
5518 /* Two cases cannot and do not have line numbers associated: If stmt
5519 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5520 cannot hold line numbers. But that's OK because the statement
5521 will either be changed to a MODIFY_EXPR during gimplification of
5522 the statement expr, or discarded. If stmt was compound, but
5523 without new variables, we will have skipped the creation of a
5524 BIND and will have a bare STATEMENT_LIST. But that's OK because
5525 (recursively) all of the component statements should already have
5526 line numbers assigned. ??? Can we discard no-op statements
5527 earlier? */
5528 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5529 protected_set_expr_location (stmt, loc);
5531 parser->in_if_block = in_if_block;
5534 /* Parse the condition from an if, do, while or for statements. */
5536 static tree
5537 c_parser_condition (c_parser *parser)
5539 location_t loc = c_parser_peek_token (parser)->location;
5540 tree cond;
5541 cond = c_parser_expression_conv (parser).value;
5542 cond = c_objc_common_truthvalue_conversion (loc, cond);
5543 cond = c_fully_fold (cond, false, NULL);
5544 if (warn_sequence_point)
5545 verify_sequence_points (cond);
5546 return cond;
5549 /* Parse a parenthesized condition from an if, do or while statement.
5551 condition:
5552 ( expression )
5554 static tree
5555 c_parser_paren_condition (c_parser *parser)
5557 tree cond;
5558 matching_parens parens;
5559 if (!parens.require_open (parser))
5560 return error_mark_node;
5561 cond = c_parser_condition (parser);
5562 parens.skip_until_found_close (parser);
5563 return cond;
5566 /* Parse a statement which is a block in C99.
5568 IF_P is used to track whether there's a (possibly labeled) if statement
5569 which is not enclosed in braces and has an else clause. This is used to
5570 implement -Wparentheses. */
5572 static tree
5573 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5574 location_t *loc_after_labels)
5576 tree block = c_begin_compound_stmt (flag_isoc99);
5577 location_t loc = c_parser_peek_token (parser)->location;
5578 c_parser_statement (parser, if_p, loc_after_labels);
5579 return c_end_compound_stmt (loc, block, flag_isoc99);
5582 /* Parse the body of an if statement. This is just parsing a
5583 statement but (a) it is a block in C99, (b) we track whether the
5584 body is an if statement for the sake of -Wparentheses warnings, (c)
5585 we handle an empty body specially for the sake of -Wempty-body
5586 warnings, and (d) we call parser_compound_statement directly
5587 because c_parser_statement_after_labels resets
5588 parser->in_if_block.
5590 IF_P is used to track whether there's a (possibly labeled) if statement
5591 which is not enclosed in braces and has an else clause. This is used to
5592 implement -Wparentheses. */
5594 static tree
5595 c_parser_if_body (c_parser *parser, bool *if_p,
5596 const token_indent_info &if_tinfo)
5598 tree block = c_begin_compound_stmt (flag_isoc99);
5599 location_t body_loc = c_parser_peek_token (parser)->location;
5600 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5601 token_indent_info body_tinfo
5602 = get_token_indent_info (c_parser_peek_token (parser));
5604 c_parser_all_labels (parser);
5605 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5607 location_t loc = c_parser_peek_token (parser)->location;
5608 add_stmt (build_empty_stmt (loc));
5609 c_parser_consume_token (parser);
5610 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5611 warning_at (loc, OPT_Wempty_body,
5612 "suggest braces around empty body in an %<if%> statement");
5614 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5615 add_stmt (c_parser_compound_statement (parser));
5616 else
5618 body_loc_after_labels = c_parser_peek_token (parser)->location;
5619 c_parser_statement_after_labels (parser, if_p);
5622 token_indent_info next_tinfo
5623 = get_token_indent_info (c_parser_peek_token (parser));
5624 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5625 if (body_loc_after_labels != UNKNOWN_LOCATION
5626 && next_tinfo.type != CPP_SEMICOLON)
5627 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5628 if_tinfo.location, RID_IF);
5630 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5633 /* Parse the else body of an if statement. This is just parsing a
5634 statement but (a) it is a block in C99, (b) we handle an empty body
5635 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5636 of if-else-if conditions. */
5638 static tree
5639 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5640 vec<tree> *chain)
5642 location_t body_loc = c_parser_peek_token (parser)->location;
5643 tree block = c_begin_compound_stmt (flag_isoc99);
5644 token_indent_info body_tinfo
5645 = get_token_indent_info (c_parser_peek_token (parser));
5646 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5648 c_parser_all_labels (parser);
5649 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5651 location_t loc = c_parser_peek_token (parser)->location;
5652 warning_at (loc,
5653 OPT_Wempty_body,
5654 "suggest braces around empty body in an %<else%> statement");
5655 add_stmt (build_empty_stmt (loc));
5656 c_parser_consume_token (parser);
5658 else
5660 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5661 body_loc_after_labels = c_parser_peek_token (parser)->location;
5662 c_parser_statement_after_labels (parser, NULL, chain);
5665 token_indent_info next_tinfo
5666 = get_token_indent_info (c_parser_peek_token (parser));
5667 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5668 if (body_loc_after_labels != UNKNOWN_LOCATION
5669 && next_tinfo.type != CPP_SEMICOLON)
5670 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5671 else_tinfo.location, RID_ELSE);
5673 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5676 /* We might need to reclassify any previously-lexed identifier, e.g.
5677 when we've left a for loop with an if-statement without else in the
5678 body - we might have used a wrong scope for the token. See PR67784. */
5680 static void
5681 c_parser_maybe_reclassify_token (c_parser *parser)
5683 if (c_parser_next_token_is (parser, CPP_NAME))
5685 c_token *token = c_parser_peek_token (parser);
5687 if (token->id_kind != C_ID_CLASSNAME)
5689 tree decl = lookup_name (token->value);
5691 token->id_kind = C_ID_ID;
5692 if (decl)
5694 if (TREE_CODE (decl) == TYPE_DECL)
5695 token->id_kind = C_ID_TYPENAME;
5697 else if (c_dialect_objc ())
5699 tree objc_interface_decl = objc_is_class_name (token->value);
5700 /* Objective-C class names are in the same namespace as
5701 variables and typedefs, and hence are shadowed by local
5702 declarations. */
5703 if (objc_interface_decl)
5705 token->value = objc_interface_decl;
5706 token->id_kind = C_ID_CLASSNAME;
5713 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5715 if-statement:
5716 if ( expression ) statement
5717 if ( expression ) statement else statement
5719 CHAIN is a vector of if-else-if conditions.
5720 IF_P is used to track whether there's a (possibly labeled) if statement
5721 which is not enclosed in braces and has an else clause. This is used to
5722 implement -Wparentheses. */
5724 static void
5725 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5727 tree block;
5728 location_t loc;
5729 tree cond;
5730 bool nested_if = false;
5731 tree first_body, second_body;
5732 bool in_if_block;
5734 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5735 token_indent_info if_tinfo
5736 = get_token_indent_info (c_parser_peek_token (parser));
5737 c_parser_consume_token (parser);
5738 block = c_begin_compound_stmt (flag_isoc99);
5739 loc = c_parser_peek_token (parser)->location;
5740 cond = c_parser_paren_condition (parser);
5741 in_if_block = parser->in_if_block;
5742 parser->in_if_block = true;
5743 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5744 parser->in_if_block = in_if_block;
5746 if (warn_duplicated_cond)
5747 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5749 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5751 token_indent_info else_tinfo
5752 = get_token_indent_info (c_parser_peek_token (parser));
5753 c_parser_consume_token (parser);
5754 if (warn_duplicated_cond)
5756 if (c_parser_next_token_is_keyword (parser, RID_IF)
5757 && chain == NULL)
5759 /* We've got "if (COND) else if (COND2)". Start the
5760 condition chain and add COND as the first element. */
5761 chain = new vec<tree> ();
5762 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5763 chain->safe_push (cond);
5765 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5767 /* This is if-else without subsequent if. Zap the condition
5768 chain; we would have already warned at this point. */
5769 delete chain;
5770 chain = NULL;
5773 second_body = c_parser_else_body (parser, else_tinfo, chain);
5774 /* Set IF_P to true to indicate that this if statement has an
5775 else clause. This may trigger the Wparentheses warning
5776 below when we get back up to the parent if statement. */
5777 if (if_p != NULL)
5778 *if_p = true;
5780 else
5782 second_body = NULL_TREE;
5784 /* Diagnose an ambiguous else if if-then-else is nested inside
5785 if-then. */
5786 if (nested_if)
5787 warning_at (loc, OPT_Wdangling_else,
5788 "suggest explicit braces to avoid ambiguous %<else%>");
5790 if (warn_duplicated_cond)
5792 /* This if statement does not have an else clause. We don't
5793 need the condition chain anymore. */
5794 delete chain;
5795 chain = NULL;
5798 c_finish_if_stmt (loc, cond, first_body, second_body);
5799 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5801 c_parser_maybe_reclassify_token (parser);
5804 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5806 switch-statement:
5807 switch (expression) statement
5810 static void
5811 c_parser_switch_statement (c_parser *parser, bool *if_p)
5813 struct c_expr ce;
5814 tree block, expr, body, save_break;
5815 location_t switch_loc = c_parser_peek_token (parser)->location;
5816 location_t switch_cond_loc;
5817 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5818 c_parser_consume_token (parser);
5819 block = c_begin_compound_stmt (flag_isoc99);
5820 bool explicit_cast_p = false;
5821 matching_parens parens;
5822 if (parens.require_open (parser))
5824 switch_cond_loc = c_parser_peek_token (parser)->location;
5825 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5826 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5827 explicit_cast_p = true;
5828 ce = c_parser_expression (parser);
5829 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5830 expr = ce.value;
5831 /* ??? expr has no valid location? */
5832 parens.skip_until_found_close (parser);
5834 else
5836 switch_cond_loc = UNKNOWN_LOCATION;
5837 expr = error_mark_node;
5838 ce.original_type = error_mark_node;
5840 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5841 save_break = c_break_label;
5842 c_break_label = NULL_TREE;
5843 location_t loc_after_labels;
5844 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5845 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5846 location_t next_loc = c_parser_peek_token (parser)->location;
5847 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5848 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5849 RID_SWITCH);
5850 if (c_break_label)
5852 location_t here = c_parser_peek_token (parser)->location;
5853 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5854 SET_EXPR_LOCATION (t, here);
5855 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5856 append_to_statement_list_force (t, &body);
5858 c_finish_case (body, ce.original_type);
5859 c_break_label = save_break;
5860 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5861 c_parser_maybe_reclassify_token (parser);
5864 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5866 while-statement:
5867 while (expression) statement
5869 IF_P is used to track whether there's a (possibly labeled) if statement
5870 which is not enclosed in braces and has an else clause. This is used to
5871 implement -Wparentheses. */
5873 static void
5874 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5876 tree block, cond, body, save_break, save_cont;
5877 location_t loc;
5878 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5879 token_indent_info while_tinfo
5880 = get_token_indent_info (c_parser_peek_token (parser));
5881 c_parser_consume_token (parser);
5882 block = c_begin_compound_stmt (flag_isoc99);
5883 loc = c_parser_peek_token (parser)->location;
5884 cond = c_parser_paren_condition (parser);
5885 if (ivdep && cond != error_mark_node)
5886 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5887 build_int_cst (integer_type_node,
5888 annot_expr_ivdep_kind),
5889 integer_zero_node);
5890 save_break = c_break_label;
5891 c_break_label = NULL_TREE;
5892 save_cont = c_cont_label;
5893 c_cont_label = NULL_TREE;
5895 token_indent_info body_tinfo
5896 = get_token_indent_info (c_parser_peek_token (parser));
5898 location_t loc_after_labels;
5899 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5900 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5901 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5902 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5903 c_parser_maybe_reclassify_token (parser);
5905 token_indent_info next_tinfo
5906 = get_token_indent_info (c_parser_peek_token (parser));
5907 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5909 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5910 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5911 while_tinfo.location, RID_WHILE);
5913 c_break_label = save_break;
5914 c_cont_label = save_cont;
5917 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5919 do-statement:
5920 do statement while ( expression ) ;
5923 static void
5924 c_parser_do_statement (c_parser *parser, bool ivdep)
5926 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5927 location_t loc;
5928 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5929 c_parser_consume_token (parser);
5930 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5931 warning_at (c_parser_peek_token (parser)->location,
5932 OPT_Wempty_body,
5933 "suggest braces around empty body in %<do%> statement");
5934 block = c_begin_compound_stmt (flag_isoc99);
5935 loc = c_parser_peek_token (parser)->location;
5936 save_break = c_break_label;
5937 c_break_label = NULL_TREE;
5938 save_cont = c_cont_label;
5939 c_cont_label = NULL_TREE;
5940 body = c_parser_c99_block_statement (parser, NULL);
5941 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5942 new_break = c_break_label;
5943 c_break_label = save_break;
5944 new_cont = c_cont_label;
5945 c_cont_label = save_cont;
5946 cond = c_parser_paren_condition (parser);
5947 if (ivdep && cond != error_mark_node)
5948 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5949 build_int_cst (integer_type_node,
5950 annot_expr_ivdep_kind),
5951 integer_zero_node);
5952 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5953 c_parser_skip_to_end_of_block_or_statement (parser);
5954 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5955 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5958 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5960 for-statement:
5961 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5962 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5964 The form with a declaration is new in C99.
5966 ??? In accordance with the old parser, the declaration may be a
5967 nested function, which is then rejected in check_for_loop_decls,
5968 but does it make any sense for this to be included in the grammar?
5969 Note in particular that the nested function does not include a
5970 trailing ';', whereas the "declaration" production includes one.
5971 Also, can we reject bad declarations earlier and cheaper than
5972 check_for_loop_decls?
5974 In Objective-C, there are two additional variants:
5976 foreach-statement:
5977 for ( expression in expresssion ) statement
5978 for ( declaration in expression ) statement
5980 This is inconsistent with C, because the second variant is allowed
5981 even if c99 is not enabled.
5983 The rest of the comment documents these Objective-C foreach-statement.
5985 Here is the canonical example of the first variant:
5986 for (object in array) { do something with object }
5987 we call the first expression ("object") the "object_expression" and
5988 the second expression ("array") the "collection_expression".
5989 object_expression must be an lvalue of type "id" (a generic Objective-C
5990 object) because the loop works by assigning to object_expression the
5991 various objects from the collection_expression. collection_expression
5992 must evaluate to something of type "id" which responds to the method
5993 countByEnumeratingWithState:objects:count:.
5995 The canonical example of the second variant is:
5996 for (id object in array) { do something with object }
5997 which is completely equivalent to
5999 id object;
6000 for (object in array) { do something with object }
6002 Note that initizializing 'object' in some way (eg, "for ((object =
6003 xxx) in array) { do something with object }") is possibly
6004 technically valid, but completely pointless as 'object' will be
6005 assigned to something else as soon as the loop starts. We should
6006 most likely reject it (TODO).
6008 The beginning of the Objective-C foreach-statement looks exactly
6009 like the beginning of the for-statement, and we can tell it is a
6010 foreach-statement only because the initial declaration or
6011 expression is terminated by 'in' instead of ';'.
6013 IF_P is used to track whether there's a (possibly labeled) if statement
6014 which is not enclosed in braces and has an else clause. This is used to
6015 implement -Wparentheses. */
6017 static void
6018 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
6020 tree block, cond, incr, save_break, save_cont, body;
6021 /* The following are only used when parsing an ObjC foreach statement. */
6022 tree object_expression;
6023 /* Silence the bogus uninitialized warning. */
6024 tree collection_expression = NULL;
6025 location_t loc = c_parser_peek_token (parser)->location;
6026 location_t for_loc = c_parser_peek_token (parser)->location;
6027 bool is_foreach_statement = false;
6028 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6029 token_indent_info for_tinfo
6030 = get_token_indent_info (c_parser_peek_token (parser));
6031 c_parser_consume_token (parser);
6032 /* Open a compound statement in Objective-C as well, just in case this is
6033 as foreach expression. */
6034 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6035 cond = error_mark_node;
6036 incr = error_mark_node;
6037 matching_parens parens;
6038 if (parens.require_open (parser))
6040 /* Parse the initialization declaration or expression. */
6041 object_expression = error_mark_node;
6042 parser->objc_could_be_foreach_context = c_dialect_objc ();
6043 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6045 parser->objc_could_be_foreach_context = false;
6046 c_parser_consume_token (parser);
6047 c_finish_expr_stmt (loc, NULL_TREE);
6049 else if (c_parser_next_tokens_start_declaration (parser))
6051 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6052 &object_expression, vNULL);
6053 parser->objc_could_be_foreach_context = false;
6055 if (c_parser_next_token_is_keyword (parser, RID_IN))
6057 c_parser_consume_token (parser);
6058 is_foreach_statement = true;
6059 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6060 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6062 else
6063 check_for_loop_decls (for_loc, flag_isoc99);
6065 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6067 /* __extension__ can start a declaration, but is also an
6068 unary operator that can start an expression. Consume all
6069 but the last of a possible series of __extension__ to
6070 determine which. */
6071 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6072 && (c_parser_peek_2nd_token (parser)->keyword
6073 == RID_EXTENSION))
6074 c_parser_consume_token (parser);
6075 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6077 int ext;
6078 ext = disable_extension_diagnostics ();
6079 c_parser_consume_token (parser);
6080 c_parser_declaration_or_fndef (parser, true, true, true, true,
6081 true, &object_expression, vNULL);
6082 parser->objc_could_be_foreach_context = false;
6084 restore_extension_diagnostics (ext);
6085 if (c_parser_next_token_is_keyword (parser, RID_IN))
6087 c_parser_consume_token (parser);
6088 is_foreach_statement = true;
6089 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6090 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6092 else
6093 check_for_loop_decls (for_loc, flag_isoc99);
6095 else
6096 goto init_expr;
6098 else
6100 init_expr:
6102 struct c_expr ce;
6103 tree init_expression;
6104 ce = c_parser_expression (parser);
6105 init_expression = ce.value;
6106 parser->objc_could_be_foreach_context = false;
6107 if (c_parser_next_token_is_keyword (parser, RID_IN))
6109 c_parser_consume_token (parser);
6110 is_foreach_statement = true;
6111 if (! lvalue_p (init_expression))
6112 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6113 object_expression = c_fully_fold (init_expression, false, NULL);
6115 else
6117 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6118 init_expression = ce.value;
6119 c_finish_expr_stmt (loc, init_expression);
6120 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6124 /* Parse the loop condition. In the case of a foreach
6125 statement, there is no loop condition. */
6126 gcc_assert (!parser->objc_could_be_foreach_context);
6127 if (!is_foreach_statement)
6129 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6131 if (ivdep)
6133 c_parser_error (parser, "missing loop condition in loop with "
6134 "%<GCC ivdep%> pragma");
6135 cond = error_mark_node;
6137 else
6139 c_parser_consume_token (parser);
6140 cond = NULL_TREE;
6143 else
6145 cond = c_parser_condition (parser);
6146 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6147 "expected %<;%>");
6149 if (ivdep && cond != error_mark_node)
6150 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6151 build_int_cst (integer_type_node,
6152 annot_expr_ivdep_kind),
6153 integer_zero_node);
6155 /* Parse the increment expression (the third expression in a
6156 for-statement). In the case of a foreach-statement, this is
6157 the expression that follows the 'in'. */
6158 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6160 if (is_foreach_statement)
6162 c_parser_error (parser, "missing collection in fast enumeration");
6163 collection_expression = error_mark_node;
6165 else
6166 incr = c_process_expr_stmt (loc, NULL_TREE);
6168 else
6170 if (is_foreach_statement)
6171 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6172 false, NULL);
6173 else
6175 struct c_expr ce = c_parser_expression (parser);
6176 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6177 incr = c_process_expr_stmt (loc, ce.value);
6180 parens.skip_until_found_close (parser);
6182 save_break = c_break_label;
6183 c_break_label = NULL_TREE;
6184 save_cont = c_cont_label;
6185 c_cont_label = NULL_TREE;
6187 token_indent_info body_tinfo
6188 = get_token_indent_info (c_parser_peek_token (parser));
6190 location_t loc_after_labels;
6191 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6192 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6194 if (is_foreach_statement)
6195 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6196 else
6197 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6198 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6199 c_parser_maybe_reclassify_token (parser);
6201 token_indent_info next_tinfo
6202 = get_token_indent_info (c_parser_peek_token (parser));
6203 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6205 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6206 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6207 for_tinfo.location, RID_FOR);
6209 c_break_label = save_break;
6210 c_cont_label = save_cont;
6213 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6214 statement with inputs, outputs, clobbers, and volatile tag
6215 allowed.
6217 asm-statement:
6218 asm type-qualifier[opt] ( asm-argument ) ;
6219 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6221 asm-argument:
6222 asm-string-literal
6223 asm-string-literal : asm-operands[opt]
6224 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6225 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6227 asm-goto-argument:
6228 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6229 : asm-goto-operands
6231 Qualifiers other than volatile are accepted in the syntax but
6232 warned for. */
6234 static tree
6235 c_parser_asm_statement (c_parser *parser)
6237 tree quals, str, outputs, inputs, clobbers, labels, ret;
6238 bool simple, is_goto;
6239 location_t asm_loc = c_parser_peek_token (parser)->location;
6240 int section, nsections;
6242 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6243 c_parser_consume_token (parser);
6244 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6246 quals = c_parser_peek_token (parser)->value;
6247 c_parser_consume_token (parser);
6249 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6250 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6252 warning_at (c_parser_peek_token (parser)->location,
6254 "%E qualifier ignored on asm",
6255 c_parser_peek_token (parser)->value);
6256 quals = NULL_TREE;
6257 c_parser_consume_token (parser);
6259 else
6260 quals = NULL_TREE;
6262 is_goto = false;
6263 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6265 c_parser_consume_token (parser);
6266 is_goto = true;
6269 /* ??? Follow the C++ parser rather than using the
6270 lex_untranslated_string kludge. */
6271 parser->lex_untranslated_string = true;
6272 ret = NULL;
6274 matching_parens parens;
6275 if (!parens.require_open (parser))
6276 goto error;
6278 str = c_parser_asm_string_literal (parser);
6279 if (str == NULL_TREE)
6280 goto error_close_paren;
6282 simple = true;
6283 outputs = NULL_TREE;
6284 inputs = NULL_TREE;
6285 clobbers = NULL_TREE;
6286 labels = NULL_TREE;
6288 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6289 goto done_asm;
6291 /* Parse each colon-delimited section of operands. */
6292 nsections = 3 + is_goto;
6293 for (section = 0; section < nsections; ++section)
6295 if (!c_parser_require (parser, CPP_COLON,
6296 is_goto
6297 ? G_("expected %<:%>")
6298 : G_("expected %<:%> or %<)%>"),
6299 UNKNOWN_LOCATION, is_goto))
6300 goto error_close_paren;
6302 /* Once past any colon, we're no longer a simple asm. */
6303 simple = false;
6305 if ((!c_parser_next_token_is (parser, CPP_COLON)
6306 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6307 || section == 3)
6308 switch (section)
6310 case 0:
6311 /* For asm goto, we don't allow output operands, but reserve
6312 the slot for a future extension that does allow them. */
6313 if (!is_goto)
6314 outputs = c_parser_asm_operands (parser);
6315 break;
6316 case 1:
6317 inputs = c_parser_asm_operands (parser);
6318 break;
6319 case 2:
6320 clobbers = c_parser_asm_clobbers (parser);
6321 break;
6322 case 3:
6323 labels = c_parser_asm_goto_operands (parser);
6324 break;
6325 default:
6326 gcc_unreachable ();
6329 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6330 goto done_asm;
6333 done_asm:
6334 if (!parens.require_close (parser))
6336 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6337 goto error;
6340 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6341 c_parser_skip_to_end_of_block_or_statement (parser);
6343 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6344 clobbers, labels, simple));
6346 error:
6347 parser->lex_untranslated_string = false;
6348 return ret;
6350 error_close_paren:
6351 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6352 goto error;
6355 /* Parse asm operands, a GNU extension.
6357 asm-operands:
6358 asm-operand
6359 asm-operands , asm-operand
6361 asm-operand:
6362 asm-string-literal ( expression )
6363 [ identifier ] asm-string-literal ( expression )
6366 static tree
6367 c_parser_asm_operands (c_parser *parser)
6369 tree list = NULL_TREE;
6370 while (true)
6372 tree name, str;
6373 struct c_expr expr;
6374 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6376 c_parser_consume_token (parser);
6377 if (c_parser_next_token_is (parser, CPP_NAME))
6379 tree id = c_parser_peek_token (parser)->value;
6380 c_parser_consume_token (parser);
6381 name = build_string (IDENTIFIER_LENGTH (id),
6382 IDENTIFIER_POINTER (id));
6384 else
6386 c_parser_error (parser, "expected identifier");
6387 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6388 return NULL_TREE;
6390 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6391 "expected %<]%>");
6393 else
6394 name = NULL_TREE;
6395 str = c_parser_asm_string_literal (parser);
6396 if (str == NULL_TREE)
6397 return NULL_TREE;
6398 parser->lex_untranslated_string = false;
6399 matching_parens parens;
6400 if (!parens.require_open (parser))
6402 parser->lex_untranslated_string = true;
6403 return NULL_TREE;
6405 expr = c_parser_expression (parser);
6406 mark_exp_read (expr.value);
6407 parser->lex_untranslated_string = true;
6408 if (!parens.require_close (parser))
6410 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6411 return NULL_TREE;
6413 list = chainon (list, build_tree_list (build_tree_list (name, str),
6414 expr.value));
6415 if (c_parser_next_token_is (parser, CPP_COMMA))
6416 c_parser_consume_token (parser);
6417 else
6418 break;
6420 return list;
6423 /* Parse asm clobbers, a GNU extension.
6425 asm-clobbers:
6426 asm-string-literal
6427 asm-clobbers , asm-string-literal
6430 static tree
6431 c_parser_asm_clobbers (c_parser *parser)
6433 tree list = NULL_TREE;
6434 while (true)
6436 tree str = c_parser_asm_string_literal (parser);
6437 if (str)
6438 list = tree_cons (NULL_TREE, str, list);
6439 else
6440 return NULL_TREE;
6441 if (c_parser_next_token_is (parser, CPP_COMMA))
6442 c_parser_consume_token (parser);
6443 else
6444 break;
6446 return list;
6449 /* Parse asm goto labels, a GNU extension.
6451 asm-goto-operands:
6452 identifier
6453 asm-goto-operands , identifier
6456 static tree
6457 c_parser_asm_goto_operands (c_parser *parser)
6459 tree list = NULL_TREE;
6460 while (true)
6462 tree name, label;
6464 if (c_parser_next_token_is (parser, CPP_NAME))
6466 c_token *tok = c_parser_peek_token (parser);
6467 name = tok->value;
6468 label = lookup_label_for_goto (tok->location, name);
6469 c_parser_consume_token (parser);
6470 TREE_USED (label) = 1;
6472 else
6474 c_parser_error (parser, "expected identifier");
6475 return NULL_TREE;
6478 name = build_string (IDENTIFIER_LENGTH (name),
6479 IDENTIFIER_POINTER (name));
6480 list = tree_cons (name, label, list);
6481 if (c_parser_next_token_is (parser, CPP_COMMA))
6482 c_parser_consume_token (parser);
6483 else
6484 return nreverse (list);
6488 /* Parse an expression other than a compound expression; that is, an
6489 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6490 AFTER is not NULL then it is an Objective-C message expression which
6491 is the primary-expression starting the expression as an initializer.
6493 assignment-expression:
6494 conditional-expression
6495 unary-expression assignment-operator assignment-expression
6497 assignment-operator: one of
6498 = *= /= %= += -= <<= >>= &= ^= |=
6500 In GNU C we accept any conditional expression on the LHS and
6501 diagnose the invalid lvalue rather than producing a syntax
6502 error. */
6504 static struct c_expr
6505 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6506 tree omp_atomic_lhs)
6508 struct c_expr lhs, rhs, ret;
6509 enum tree_code code;
6510 location_t op_location, exp_location;
6511 gcc_assert (!after || c_dialect_objc ());
6512 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6513 op_location = c_parser_peek_token (parser)->location;
6514 switch (c_parser_peek_token (parser)->type)
6516 case CPP_EQ:
6517 code = NOP_EXPR;
6518 break;
6519 case CPP_MULT_EQ:
6520 code = MULT_EXPR;
6521 break;
6522 case CPP_DIV_EQ:
6523 code = TRUNC_DIV_EXPR;
6524 break;
6525 case CPP_MOD_EQ:
6526 code = TRUNC_MOD_EXPR;
6527 break;
6528 case CPP_PLUS_EQ:
6529 code = PLUS_EXPR;
6530 break;
6531 case CPP_MINUS_EQ:
6532 code = MINUS_EXPR;
6533 break;
6534 case CPP_LSHIFT_EQ:
6535 code = LSHIFT_EXPR;
6536 break;
6537 case CPP_RSHIFT_EQ:
6538 code = RSHIFT_EXPR;
6539 break;
6540 case CPP_AND_EQ:
6541 code = BIT_AND_EXPR;
6542 break;
6543 case CPP_XOR_EQ:
6544 code = BIT_XOR_EXPR;
6545 break;
6546 case CPP_OR_EQ:
6547 code = BIT_IOR_EXPR;
6548 break;
6549 default:
6550 return lhs;
6552 c_parser_consume_token (parser);
6553 exp_location = c_parser_peek_token (parser)->location;
6554 rhs = c_parser_expr_no_commas (parser, NULL);
6555 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6557 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6558 code, exp_location, rhs.value,
6559 rhs.original_type);
6560 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6561 if (code == NOP_EXPR)
6562 ret.original_code = MODIFY_EXPR;
6563 else
6565 TREE_NO_WARNING (ret.value) = 1;
6566 ret.original_code = ERROR_MARK;
6568 ret.original_type = NULL;
6569 return ret;
6572 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6573 AFTER is not NULL then it is an Objective-C message expression which is
6574 the primary-expression starting the expression as an initializer.
6576 conditional-expression:
6577 logical-OR-expression
6578 logical-OR-expression ? expression : conditional-expression
6580 GNU extensions:
6582 conditional-expression:
6583 logical-OR-expression ? : conditional-expression
6586 static struct c_expr
6587 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6588 tree omp_atomic_lhs)
6590 struct c_expr cond, exp1, exp2, ret;
6591 location_t start, cond_loc, colon_loc;
6593 gcc_assert (!after || c_dialect_objc ());
6595 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6597 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6598 return cond;
6599 if (cond.value != error_mark_node)
6600 start = cond.get_start ();
6601 else
6602 start = UNKNOWN_LOCATION;
6603 cond_loc = c_parser_peek_token (parser)->location;
6604 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6605 c_parser_consume_token (parser);
6606 if (c_parser_next_token_is (parser, CPP_COLON))
6608 tree eptype = NULL_TREE;
6610 location_t middle_loc = c_parser_peek_token (parser)->location;
6611 pedwarn (middle_loc, OPT_Wpedantic,
6612 "ISO C forbids omitting the middle term of a ?: expression");
6613 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6615 eptype = TREE_TYPE (cond.value);
6616 cond.value = TREE_OPERAND (cond.value, 0);
6618 tree e = cond.value;
6619 while (TREE_CODE (e) == COMPOUND_EXPR)
6620 e = TREE_OPERAND (e, 1);
6621 warn_for_omitted_condop (middle_loc, e);
6622 /* Make sure first operand is calculated only once. */
6623 exp1.value = save_expr (default_conversion (cond.value));
6624 if (eptype)
6625 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6626 exp1.original_type = NULL;
6627 exp1.src_range = cond.src_range;
6628 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6629 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6631 else
6633 cond.value
6634 = c_objc_common_truthvalue_conversion
6635 (cond_loc, default_conversion (cond.value));
6636 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6637 exp1 = c_parser_expression_conv (parser);
6638 mark_exp_read (exp1.value);
6639 c_inhibit_evaluation_warnings +=
6640 ((cond.value == truthvalue_true_node)
6641 - (cond.value == truthvalue_false_node));
6644 colon_loc = c_parser_peek_token (parser)->location;
6645 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6647 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6648 ret.value = error_mark_node;
6649 ret.original_code = ERROR_MARK;
6650 ret.original_type = NULL;
6651 return ret;
6654 location_t exp2_loc = c_parser_peek_token (parser)->location;
6655 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6656 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6658 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6659 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6660 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6661 ret.value = build_conditional_expr (colon_loc, cond.value,
6662 cond.original_code == C_MAYBE_CONST_EXPR,
6663 exp1.value, exp1.original_type, loc1,
6664 exp2.value, exp2.original_type, loc2);
6665 ret.original_code = ERROR_MARK;
6666 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6667 ret.original_type = NULL;
6668 else
6670 tree t1, t2;
6672 /* If both sides are enum type, the default conversion will have
6673 made the type of the result be an integer type. We want to
6674 remember the enum types we started with. */
6675 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6676 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6677 ret.original_type = ((t1 != error_mark_node
6678 && t2 != error_mark_node
6679 && (TYPE_MAIN_VARIANT (t1)
6680 == TYPE_MAIN_VARIANT (t2)))
6681 ? t1
6682 : NULL);
6684 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6685 return ret;
6688 /* Parse a binary expression; that is, a logical-OR-expression (C90
6689 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6690 NULL then it is an Objective-C message expression which is the
6691 primary-expression starting the expression as an initializer.
6693 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6694 when it should be the unfolded lhs. In a valid OpenMP source,
6695 one of the operands of the toplevel binary expression must be equal
6696 to it. In that case, just return a build2 created binary operation
6697 rather than result of parser_build_binary_op.
6699 multiplicative-expression:
6700 cast-expression
6701 multiplicative-expression * cast-expression
6702 multiplicative-expression / cast-expression
6703 multiplicative-expression % cast-expression
6705 additive-expression:
6706 multiplicative-expression
6707 additive-expression + multiplicative-expression
6708 additive-expression - multiplicative-expression
6710 shift-expression:
6711 additive-expression
6712 shift-expression << additive-expression
6713 shift-expression >> additive-expression
6715 relational-expression:
6716 shift-expression
6717 relational-expression < shift-expression
6718 relational-expression > shift-expression
6719 relational-expression <= shift-expression
6720 relational-expression >= shift-expression
6722 equality-expression:
6723 relational-expression
6724 equality-expression == relational-expression
6725 equality-expression != relational-expression
6727 AND-expression:
6728 equality-expression
6729 AND-expression & equality-expression
6731 exclusive-OR-expression:
6732 AND-expression
6733 exclusive-OR-expression ^ AND-expression
6735 inclusive-OR-expression:
6736 exclusive-OR-expression
6737 inclusive-OR-expression | exclusive-OR-expression
6739 logical-AND-expression:
6740 inclusive-OR-expression
6741 logical-AND-expression && inclusive-OR-expression
6743 logical-OR-expression:
6744 logical-AND-expression
6745 logical-OR-expression || logical-AND-expression
6748 static struct c_expr
6749 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6750 tree omp_atomic_lhs)
6752 /* A binary expression is parsed using operator-precedence parsing,
6753 with the operands being cast expressions. All the binary
6754 operators are left-associative. Thus a binary expression is of
6755 form:
6757 E0 op1 E1 op2 E2 ...
6759 which we represent on a stack. On the stack, the precedence
6760 levels are strictly increasing. When a new operator is
6761 encountered of higher precedence than that at the top of the
6762 stack, it is pushed; its LHS is the top expression, and its RHS
6763 is everything parsed until it is popped. When a new operator is
6764 encountered with precedence less than or equal to that at the top
6765 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6766 by the result of the operation until the operator at the top of
6767 the stack has lower precedence than the new operator or there is
6768 only one element on the stack; then the top expression is the LHS
6769 of the new operator. In the case of logical AND and OR
6770 expressions, we also need to adjust c_inhibit_evaluation_warnings
6771 as appropriate when the operators are pushed and popped. */
6773 struct {
6774 /* The expression at this stack level. */
6775 struct c_expr expr;
6776 /* The precedence of the operator on its left, PREC_NONE at the
6777 bottom of the stack. */
6778 enum c_parser_prec prec;
6779 /* The operation on its left. */
6780 enum tree_code op;
6781 /* The source location of this operation. */
6782 location_t loc;
6783 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6784 tree sizeof_arg;
6785 } stack[NUM_PRECS];
6786 int sp;
6787 /* Location of the binary operator. */
6788 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6789 #define POP \
6790 do { \
6791 switch (stack[sp].op) \
6793 case TRUTH_ANDIF_EXPR: \
6794 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6795 == truthvalue_false_node); \
6796 break; \
6797 case TRUTH_ORIF_EXPR: \
6798 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6799 == truthvalue_true_node); \
6800 break; \
6801 case TRUNC_DIV_EXPR: \
6802 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6803 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6805 tree type0 = stack[sp - 1].sizeof_arg; \
6806 tree type1 = stack[sp].sizeof_arg; \
6807 tree first_arg = type0; \
6808 if (!TYPE_P (type0)) \
6809 type0 = TREE_TYPE (type0); \
6810 if (!TYPE_P (type1)) \
6811 type1 = TREE_TYPE (type1); \
6812 if (POINTER_TYPE_P (type0) \
6813 && comptypes (TREE_TYPE (type0), type1) \
6814 && !(TREE_CODE (first_arg) == PARM_DECL \
6815 && C_ARRAY_PARAMETER (first_arg) \
6816 && warn_sizeof_array_argument)) \
6817 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6818 "division %<sizeof (%T) / sizeof (%T)%> does " \
6819 "not compute the number of array elements", \
6820 type0, type1)) \
6821 if (DECL_P (first_arg)) \
6822 inform (DECL_SOURCE_LOCATION (first_arg), \
6823 "first %<sizeof%> operand was declared here"); \
6825 break; \
6826 default: \
6827 break; \
6829 stack[sp - 1].expr \
6830 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6831 stack[sp - 1].expr, true, true); \
6832 stack[sp].expr \
6833 = convert_lvalue_to_rvalue (stack[sp].loc, \
6834 stack[sp].expr, true, true); \
6835 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6836 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6837 && ((1 << stack[sp].prec) \
6838 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6839 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6840 && stack[sp].op != TRUNC_MOD_EXPR \
6841 && stack[0].expr.value != error_mark_node \
6842 && stack[1].expr.value != error_mark_node \
6843 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6844 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6845 stack[0].expr.value \
6846 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6847 stack[0].expr.value, stack[1].expr.value); \
6848 else \
6849 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6850 stack[sp].op, \
6851 stack[sp - 1].expr, \
6852 stack[sp].expr); \
6853 sp--; \
6854 } while (0)
6855 gcc_assert (!after || c_dialect_objc ());
6856 stack[0].loc = c_parser_peek_token (parser)->location;
6857 stack[0].expr = c_parser_cast_expression (parser, after);
6858 stack[0].prec = PREC_NONE;
6859 stack[0].sizeof_arg = c_last_sizeof_arg;
6860 sp = 0;
6861 while (true)
6863 enum c_parser_prec oprec;
6864 enum tree_code ocode;
6865 source_range src_range;
6866 if (parser->error)
6867 goto out;
6868 switch (c_parser_peek_token (parser)->type)
6870 case CPP_MULT:
6871 oprec = PREC_MULT;
6872 ocode = MULT_EXPR;
6873 break;
6874 case CPP_DIV:
6875 oprec = PREC_MULT;
6876 ocode = TRUNC_DIV_EXPR;
6877 break;
6878 case CPP_MOD:
6879 oprec = PREC_MULT;
6880 ocode = TRUNC_MOD_EXPR;
6881 break;
6882 case CPP_PLUS:
6883 oprec = PREC_ADD;
6884 ocode = PLUS_EXPR;
6885 break;
6886 case CPP_MINUS:
6887 oprec = PREC_ADD;
6888 ocode = MINUS_EXPR;
6889 break;
6890 case CPP_LSHIFT:
6891 oprec = PREC_SHIFT;
6892 ocode = LSHIFT_EXPR;
6893 break;
6894 case CPP_RSHIFT:
6895 oprec = PREC_SHIFT;
6896 ocode = RSHIFT_EXPR;
6897 break;
6898 case CPP_LESS:
6899 oprec = PREC_REL;
6900 ocode = LT_EXPR;
6901 break;
6902 case CPP_GREATER:
6903 oprec = PREC_REL;
6904 ocode = GT_EXPR;
6905 break;
6906 case CPP_LESS_EQ:
6907 oprec = PREC_REL;
6908 ocode = LE_EXPR;
6909 break;
6910 case CPP_GREATER_EQ:
6911 oprec = PREC_REL;
6912 ocode = GE_EXPR;
6913 break;
6914 case CPP_EQ_EQ:
6915 oprec = PREC_EQ;
6916 ocode = EQ_EXPR;
6917 break;
6918 case CPP_NOT_EQ:
6919 oprec = PREC_EQ;
6920 ocode = NE_EXPR;
6921 break;
6922 case CPP_AND:
6923 oprec = PREC_BITAND;
6924 ocode = BIT_AND_EXPR;
6925 break;
6926 case CPP_XOR:
6927 oprec = PREC_BITXOR;
6928 ocode = BIT_XOR_EXPR;
6929 break;
6930 case CPP_OR:
6931 oprec = PREC_BITOR;
6932 ocode = BIT_IOR_EXPR;
6933 break;
6934 case CPP_AND_AND:
6935 oprec = PREC_LOGAND;
6936 ocode = TRUTH_ANDIF_EXPR;
6937 break;
6938 case CPP_OR_OR:
6939 oprec = PREC_LOGOR;
6940 ocode = TRUTH_ORIF_EXPR;
6941 break;
6942 default:
6943 /* Not a binary operator, so end of the binary
6944 expression. */
6945 goto out;
6947 binary_loc = c_parser_peek_token (parser)->location;
6948 while (oprec <= stack[sp].prec)
6949 POP;
6950 c_parser_consume_token (parser);
6951 switch (ocode)
6953 case TRUTH_ANDIF_EXPR:
6954 src_range = stack[sp].expr.src_range;
6955 stack[sp].expr
6956 = convert_lvalue_to_rvalue (stack[sp].loc,
6957 stack[sp].expr, true, true);
6958 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6959 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6960 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6961 == truthvalue_false_node);
6962 set_c_expr_source_range (&stack[sp].expr, src_range);
6963 break;
6964 case TRUTH_ORIF_EXPR:
6965 src_range = stack[sp].expr.src_range;
6966 stack[sp].expr
6967 = convert_lvalue_to_rvalue (stack[sp].loc,
6968 stack[sp].expr, true, true);
6969 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6970 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6971 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6972 == truthvalue_true_node);
6973 set_c_expr_source_range (&stack[sp].expr, src_range);
6974 break;
6975 default:
6976 break;
6978 sp++;
6979 stack[sp].loc = binary_loc;
6980 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6981 stack[sp].prec = oprec;
6982 stack[sp].op = ocode;
6983 stack[sp].sizeof_arg = c_last_sizeof_arg;
6985 out:
6986 while (sp > 0)
6987 POP;
6988 return stack[0].expr;
6989 #undef POP
6992 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
6993 is not NULL then it is an Objective-C message expression which is the
6994 primary-expression starting the expression as an initializer.
6996 cast-expression:
6997 unary-expression
6998 ( type-name ) unary-expression
7001 static struct c_expr
7002 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7004 location_t cast_loc = c_parser_peek_token (parser)->location;
7005 gcc_assert (!after || c_dialect_objc ());
7006 if (after)
7007 return c_parser_postfix_expression_after_primary (parser,
7008 cast_loc, *after);
7009 /* If the expression begins with a parenthesized type name, it may
7010 be either a cast or a compound literal; we need to see whether
7011 the next character is '{' to tell the difference. If not, it is
7012 an unary expression. Full detection of unknown typenames here
7013 would require a 3-token lookahead. */
7014 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7015 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7017 struct c_type_name *type_name;
7018 struct c_expr ret;
7019 struct c_expr expr;
7020 matching_parens parens;
7021 parens.consume_open (parser);
7022 type_name = c_parser_type_name (parser);
7023 parens.skip_until_found_close (parser);
7024 if (type_name == NULL)
7026 ret.value = error_mark_node;
7027 ret.original_code = ERROR_MARK;
7028 ret.original_type = NULL;
7029 return ret;
7032 /* Save casted types in the function's used types hash table. */
7033 used_types_insert (type_name->specs->type);
7035 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7036 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7037 cast_loc);
7039 location_t expr_loc = c_parser_peek_token (parser)->location;
7040 expr = c_parser_cast_expression (parser, NULL);
7041 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7043 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7044 if (ret.value && expr.value)
7045 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7046 ret.original_code = ERROR_MARK;
7047 ret.original_type = NULL;
7048 return ret;
7050 else
7051 return c_parser_unary_expression (parser);
7054 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7056 unary-expression:
7057 postfix-expression
7058 ++ unary-expression
7059 -- unary-expression
7060 unary-operator cast-expression
7061 sizeof unary-expression
7062 sizeof ( type-name )
7064 unary-operator: one of
7065 & * + - ~ !
7067 GNU extensions:
7069 unary-expression:
7070 __alignof__ unary-expression
7071 __alignof__ ( type-name )
7072 && identifier
7074 (C11 permits _Alignof with type names only.)
7076 unary-operator: one of
7077 __extension__ __real__ __imag__
7079 Transactional Memory:
7081 unary-expression:
7082 transaction-expression
7084 In addition, the GNU syntax treats ++ and -- as unary operators, so
7085 they may be applied to cast expressions with errors for non-lvalues
7086 given later. */
7088 static struct c_expr
7089 c_parser_unary_expression (c_parser *parser)
7091 int ext;
7092 struct c_expr ret, op;
7093 location_t op_loc = c_parser_peek_token (parser)->location;
7094 location_t exp_loc;
7095 location_t finish;
7096 ret.original_code = ERROR_MARK;
7097 ret.original_type = NULL;
7098 switch (c_parser_peek_token (parser)->type)
7100 case CPP_PLUS_PLUS:
7101 c_parser_consume_token (parser);
7102 exp_loc = c_parser_peek_token (parser)->location;
7103 op = c_parser_cast_expression (parser, NULL);
7105 op = default_function_array_read_conversion (exp_loc, op);
7106 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7107 case CPP_MINUS_MINUS:
7108 c_parser_consume_token (parser);
7109 exp_loc = c_parser_peek_token (parser)->location;
7110 op = c_parser_cast_expression (parser, NULL);
7112 op = default_function_array_read_conversion (exp_loc, op);
7113 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7114 case CPP_AND:
7115 c_parser_consume_token (parser);
7116 op = c_parser_cast_expression (parser, NULL);
7117 mark_exp_read (op.value);
7118 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7119 case CPP_MULT:
7121 c_parser_consume_token (parser);
7122 exp_loc = c_parser_peek_token (parser)->location;
7123 op = c_parser_cast_expression (parser, NULL);
7124 finish = op.get_finish ();
7125 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7126 location_t combined_loc = make_location (op_loc, op_loc, finish);
7127 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7128 ret.src_range.m_start = op_loc;
7129 ret.src_range.m_finish = finish;
7130 return ret;
7132 case CPP_PLUS:
7133 if (!c_dialect_objc () && !in_system_header_at (input_location))
7134 warning_at (op_loc,
7135 OPT_Wtraditional,
7136 "traditional C rejects the unary plus operator");
7137 c_parser_consume_token (parser);
7138 exp_loc = c_parser_peek_token (parser)->location;
7139 op = c_parser_cast_expression (parser, NULL);
7140 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7141 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7142 case CPP_MINUS:
7143 c_parser_consume_token (parser);
7144 exp_loc = c_parser_peek_token (parser)->location;
7145 op = c_parser_cast_expression (parser, NULL);
7146 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7147 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7148 case CPP_COMPL:
7149 c_parser_consume_token (parser);
7150 exp_loc = c_parser_peek_token (parser)->location;
7151 op = c_parser_cast_expression (parser, NULL);
7152 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7153 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7154 case CPP_NOT:
7155 c_parser_consume_token (parser);
7156 exp_loc = c_parser_peek_token (parser)->location;
7157 op = c_parser_cast_expression (parser, NULL);
7158 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7159 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7160 case CPP_AND_AND:
7161 /* Refer to the address of a label as a pointer. */
7162 c_parser_consume_token (parser);
7163 if (c_parser_next_token_is (parser, CPP_NAME))
7165 ret.value = finish_label_address_expr
7166 (c_parser_peek_token (parser)->value, op_loc);
7167 set_c_expr_source_range (&ret, op_loc,
7168 c_parser_peek_token (parser)->get_finish ());
7169 c_parser_consume_token (parser);
7171 else
7173 c_parser_error (parser, "expected identifier");
7174 ret.set_error ();
7176 return ret;
7177 case CPP_KEYWORD:
7178 switch (c_parser_peek_token (parser)->keyword)
7180 case RID_SIZEOF:
7181 return c_parser_sizeof_expression (parser);
7182 case RID_ALIGNOF:
7183 return c_parser_alignof_expression (parser);
7184 case RID_EXTENSION:
7185 c_parser_consume_token (parser);
7186 ext = disable_extension_diagnostics ();
7187 ret = c_parser_cast_expression (parser, NULL);
7188 restore_extension_diagnostics (ext);
7189 return ret;
7190 case RID_REALPART:
7191 c_parser_consume_token (parser);
7192 exp_loc = c_parser_peek_token (parser)->location;
7193 op = c_parser_cast_expression (parser, NULL);
7194 op = default_function_array_conversion (exp_loc, op);
7195 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7196 case RID_IMAGPART:
7197 c_parser_consume_token (parser);
7198 exp_loc = c_parser_peek_token (parser)->location;
7199 op = c_parser_cast_expression (parser, NULL);
7200 op = default_function_array_conversion (exp_loc, op);
7201 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7202 case RID_TRANSACTION_ATOMIC:
7203 case RID_TRANSACTION_RELAXED:
7204 return c_parser_transaction_expression (parser,
7205 c_parser_peek_token (parser)->keyword);
7206 default:
7207 return c_parser_postfix_expression (parser);
7209 default:
7210 return c_parser_postfix_expression (parser);
7214 /* Parse a sizeof expression. */
7216 static struct c_expr
7217 c_parser_sizeof_expression (c_parser *parser)
7219 struct c_expr expr;
7220 struct c_expr result;
7221 location_t expr_loc;
7222 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7224 location_t start;
7225 location_t finish = UNKNOWN_LOCATION;
7227 start = c_parser_peek_token (parser)->location;
7229 c_parser_consume_token (parser);
7230 c_inhibit_evaluation_warnings++;
7231 in_sizeof++;
7232 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7233 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7235 /* Either sizeof ( type-name ) or sizeof unary-expression
7236 starting with a compound literal. */
7237 struct c_type_name *type_name;
7238 matching_parens parens;
7239 parens.consume_open (parser);
7240 expr_loc = c_parser_peek_token (parser)->location;
7241 type_name = c_parser_type_name (parser);
7242 parens.skip_until_found_close (parser);
7243 finish = parser->tokens_buf[0].location;
7244 if (type_name == NULL)
7246 struct c_expr ret;
7247 c_inhibit_evaluation_warnings--;
7248 in_sizeof--;
7249 ret.value = error_mark_node;
7250 ret.original_code = ERROR_MARK;
7251 ret.original_type = NULL;
7252 return ret;
7254 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7256 expr = c_parser_postfix_expression_after_paren_type (parser,
7257 type_name,
7258 expr_loc);
7259 finish = expr.get_finish ();
7260 goto sizeof_expr;
7262 /* sizeof ( type-name ). */
7263 c_inhibit_evaluation_warnings--;
7264 in_sizeof--;
7265 result = c_expr_sizeof_type (expr_loc, type_name);
7267 else
7269 expr_loc = c_parser_peek_token (parser)->location;
7270 expr = c_parser_unary_expression (parser);
7271 finish = expr.get_finish ();
7272 sizeof_expr:
7273 c_inhibit_evaluation_warnings--;
7274 in_sizeof--;
7275 mark_exp_read (expr.value);
7276 if (TREE_CODE (expr.value) == COMPONENT_REF
7277 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7278 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7279 result = c_expr_sizeof_expr (expr_loc, expr);
7281 if (finish != UNKNOWN_LOCATION)
7282 set_c_expr_source_range (&result, start, finish);
7283 return result;
7286 /* Parse an alignof expression. */
7288 static struct c_expr
7289 c_parser_alignof_expression (c_parser *parser)
7291 struct c_expr expr;
7292 location_t start_loc = c_parser_peek_token (parser)->location;
7293 location_t end_loc;
7294 tree alignof_spelling = c_parser_peek_token (parser)->value;
7295 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7296 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7297 "_Alignof") == 0;
7298 /* A diagnostic is not required for the use of this identifier in
7299 the implementation namespace; only diagnose it for the C11
7300 spelling because of existing code using the other spellings. */
7301 if (is_c11_alignof)
7303 if (flag_isoc99)
7304 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7305 alignof_spelling);
7306 else
7307 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7308 alignof_spelling);
7310 c_parser_consume_token (parser);
7311 c_inhibit_evaluation_warnings++;
7312 in_alignof++;
7313 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7314 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7316 /* Either __alignof__ ( type-name ) or __alignof__
7317 unary-expression starting with a compound literal. */
7318 location_t loc;
7319 struct c_type_name *type_name;
7320 struct c_expr ret;
7321 matching_parens parens;
7322 parens.consume_open (parser);
7323 loc = c_parser_peek_token (parser)->location;
7324 type_name = c_parser_type_name (parser);
7325 end_loc = c_parser_peek_token (parser)->location;
7326 parens.skip_until_found_close (parser);
7327 if (type_name == NULL)
7329 struct c_expr ret;
7330 c_inhibit_evaluation_warnings--;
7331 in_alignof--;
7332 ret.value = error_mark_node;
7333 ret.original_code = ERROR_MARK;
7334 ret.original_type = NULL;
7335 return ret;
7337 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7339 expr = c_parser_postfix_expression_after_paren_type (parser,
7340 type_name,
7341 loc);
7342 goto alignof_expr;
7344 /* alignof ( type-name ). */
7345 c_inhibit_evaluation_warnings--;
7346 in_alignof--;
7347 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7348 NULL, NULL),
7349 false, is_c11_alignof, 1);
7350 ret.original_code = ERROR_MARK;
7351 ret.original_type = NULL;
7352 set_c_expr_source_range (&ret, start_loc, end_loc);
7353 return ret;
7355 else
7357 struct c_expr ret;
7358 expr = c_parser_unary_expression (parser);
7359 end_loc = expr.src_range.m_finish;
7360 alignof_expr:
7361 mark_exp_read (expr.value);
7362 c_inhibit_evaluation_warnings--;
7363 in_alignof--;
7364 if (is_c11_alignof)
7365 pedwarn (start_loc,
7366 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7367 alignof_spelling);
7368 ret.value = c_alignof_expr (start_loc, expr.value);
7369 ret.original_code = ERROR_MARK;
7370 ret.original_type = NULL;
7371 set_c_expr_source_range (&ret, start_loc, end_loc);
7372 return ret;
7376 /* Helper function to read arguments of builtins which are interfaces
7377 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7378 others. The name of the builtin is passed using BNAME parameter.
7379 Function returns true if there were no errors while parsing and
7380 stores the arguments in CEXPR_LIST. If it returns true,
7381 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7382 parenthesis. */
7383 static bool
7384 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7385 vec<c_expr_t, va_gc> **ret_cexpr_list,
7386 bool choose_expr_p,
7387 location_t *out_close_paren_loc)
7389 location_t loc = c_parser_peek_token (parser)->location;
7390 vec<c_expr_t, va_gc> *cexpr_list;
7391 c_expr_t expr;
7392 bool saved_force_folding_builtin_constant_p;
7394 *ret_cexpr_list = NULL;
7395 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7397 error_at (loc, "cannot take address of %qs", bname);
7398 return false;
7401 c_parser_consume_token (parser);
7403 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7405 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7406 c_parser_consume_token (parser);
7407 return true;
7410 saved_force_folding_builtin_constant_p
7411 = force_folding_builtin_constant_p;
7412 force_folding_builtin_constant_p |= choose_expr_p;
7413 expr = c_parser_expr_no_commas (parser, NULL);
7414 force_folding_builtin_constant_p
7415 = saved_force_folding_builtin_constant_p;
7416 vec_alloc (cexpr_list, 1);
7417 vec_safe_push (cexpr_list, expr);
7418 while (c_parser_next_token_is (parser, CPP_COMMA))
7420 c_parser_consume_token (parser);
7421 expr = c_parser_expr_no_commas (parser, NULL);
7422 vec_safe_push (cexpr_list, expr);
7425 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7426 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7427 return false;
7429 *ret_cexpr_list = cexpr_list;
7430 return true;
7433 /* This represents a single generic-association. */
7435 struct c_generic_association
7437 /* The location of the starting token of the type. */
7438 location_t type_location;
7439 /* The association's type, or NULL_TREE for 'default'. */
7440 tree type;
7441 /* The association's expression. */
7442 struct c_expr expression;
7445 /* Parse a generic-selection. (C11 6.5.1.1).
7447 generic-selection:
7448 _Generic ( assignment-expression , generic-assoc-list )
7450 generic-assoc-list:
7451 generic-association
7452 generic-assoc-list , generic-association
7454 generic-association:
7455 type-name : assignment-expression
7456 default : assignment-expression
7459 static struct c_expr
7460 c_parser_generic_selection (c_parser *parser)
7462 struct c_expr selector, error_expr;
7463 tree selector_type;
7464 struct c_generic_association matched_assoc;
7465 bool match_found = false;
7466 location_t generic_loc, selector_loc;
7468 error_expr.original_code = ERROR_MARK;
7469 error_expr.original_type = NULL;
7470 error_expr.set_error ();
7471 matched_assoc.type_location = UNKNOWN_LOCATION;
7472 matched_assoc.type = NULL_TREE;
7473 matched_assoc.expression = error_expr;
7475 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7476 generic_loc = c_parser_peek_token (parser)->location;
7477 c_parser_consume_token (parser);
7478 if (flag_isoc99)
7479 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7480 "ISO C99 does not support %<_Generic%>");
7481 else
7482 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7483 "ISO C90 does not support %<_Generic%>");
7485 matching_parens parens;
7486 if (!parens.require_open (parser))
7487 return error_expr;
7489 c_inhibit_evaluation_warnings++;
7490 selector_loc = c_parser_peek_token (parser)->location;
7491 selector = c_parser_expr_no_commas (parser, NULL);
7492 selector = default_function_array_conversion (selector_loc, selector);
7493 c_inhibit_evaluation_warnings--;
7495 if (selector.value == error_mark_node)
7497 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7498 return selector;
7500 selector_type = TREE_TYPE (selector.value);
7501 /* In ISO C terms, rvalues (including the controlling expression of
7502 _Generic) do not have qualified types. */
7503 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7504 selector_type = TYPE_MAIN_VARIANT (selector_type);
7505 /* In ISO C terms, _Noreturn is not part of the type of expressions
7506 such as &abort, but in GCC it is represented internally as a type
7507 qualifier. */
7508 if (FUNCTION_POINTER_TYPE_P (selector_type)
7509 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7510 selector_type
7511 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7513 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7515 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7516 return error_expr;
7519 auto_vec<c_generic_association> associations;
7520 while (1)
7522 struct c_generic_association assoc, *iter;
7523 unsigned int ix;
7524 c_token *token = c_parser_peek_token (parser);
7526 assoc.type_location = token->location;
7527 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7529 c_parser_consume_token (parser);
7530 assoc.type = NULL_TREE;
7532 else
7534 struct c_type_name *type_name;
7536 type_name = c_parser_type_name (parser);
7537 if (type_name == NULL)
7539 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7540 return error_expr;
7542 assoc.type = groktypename (type_name, NULL, NULL);
7543 if (assoc.type == error_mark_node)
7545 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7546 return error_expr;
7549 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7550 error_at (assoc.type_location,
7551 "%<_Generic%> association has function type");
7552 else if (!COMPLETE_TYPE_P (assoc.type))
7553 error_at (assoc.type_location,
7554 "%<_Generic%> association has incomplete type");
7556 if (variably_modified_type_p (assoc.type, NULL_TREE))
7557 error_at (assoc.type_location,
7558 "%<_Generic%> association has "
7559 "variable length type");
7562 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7564 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7565 return error_expr;
7568 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7569 if (assoc.expression.value == error_mark_node)
7571 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7572 return error_expr;
7575 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7577 if (assoc.type == NULL_TREE)
7579 if (iter->type == NULL_TREE)
7581 error_at (assoc.type_location,
7582 "duplicate %<default%> case in %<_Generic%>");
7583 inform (iter->type_location, "original %<default%> is here");
7586 else if (iter->type != NULL_TREE)
7588 if (comptypes (assoc.type, iter->type))
7590 error_at (assoc.type_location,
7591 "%<_Generic%> specifies two compatible types");
7592 inform (iter->type_location, "compatible type is here");
7597 if (assoc.type == NULL_TREE)
7599 if (!match_found)
7601 matched_assoc = assoc;
7602 match_found = true;
7605 else if (comptypes (assoc.type, selector_type))
7607 if (!match_found || matched_assoc.type == NULL_TREE)
7609 matched_assoc = assoc;
7610 match_found = true;
7612 else
7614 error_at (assoc.type_location,
7615 "%<_Generic%> selector matches multiple associations");
7616 inform (matched_assoc.type_location,
7617 "other match is here");
7621 associations.safe_push (assoc);
7623 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7624 break;
7625 c_parser_consume_token (parser);
7628 if (!parens.require_close (parser))
7630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7631 return error_expr;
7634 if (!match_found)
7636 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7637 "compatible with any association",
7638 selector_type);
7639 return error_expr;
7642 return matched_assoc.expression;
7645 /* Check the validity of a function pointer argument *EXPR (argument
7646 position POS) to __builtin_tgmath. Return the number of function
7647 arguments if possibly valid; return 0 having reported an error if
7648 not valid. */
7650 static unsigned int
7651 check_tgmath_function (c_expr *expr, unsigned int pos)
7653 tree type = TREE_TYPE (expr->value);
7654 if (!FUNCTION_POINTER_TYPE_P (type))
7656 error_at (expr->get_location (),
7657 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7658 pos);
7659 return 0;
7661 type = TREE_TYPE (type);
7662 if (!prototype_p (type))
7664 error_at (expr->get_location (),
7665 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7666 return 0;
7668 if (stdarg_p (type))
7670 error_at (expr->get_location (),
7671 "argument %u of %<__builtin_tgmath%> has variable arguments",
7672 pos);
7673 return 0;
7675 unsigned int nargs = 0;
7676 function_args_iterator iter;
7677 tree t;
7678 FOREACH_FUNCTION_ARGS (type, t, iter)
7680 if (t == void_type_node)
7681 break;
7682 nargs++;
7684 if (nargs == 0)
7686 error_at (expr->get_location (),
7687 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7688 return 0;
7690 return nargs;
7693 /* Ways in which a parameter or return value of a type-generic macro
7694 may vary between the different functions the macro may call. */
7695 enum tgmath_parm_kind
7697 tgmath_fixed, tgmath_real, tgmath_complex
7700 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7701 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7702 call c_parser_postfix_expression_after_paren_type on encountering them.
7704 postfix-expression:
7705 primary-expression
7706 postfix-expression [ expression ]
7707 postfix-expression ( argument-expression-list[opt] )
7708 postfix-expression . identifier
7709 postfix-expression -> identifier
7710 postfix-expression ++
7711 postfix-expression --
7712 ( type-name ) { initializer-list }
7713 ( type-name ) { initializer-list , }
7715 argument-expression-list:
7716 argument-expression
7717 argument-expression-list , argument-expression
7719 primary-expression:
7720 identifier
7721 constant
7722 string-literal
7723 ( expression )
7724 generic-selection
7726 GNU extensions:
7728 primary-expression:
7729 __func__
7730 (treated as a keyword in GNU C)
7731 __FUNCTION__
7732 __PRETTY_FUNCTION__
7733 ( compound-statement )
7734 __builtin_va_arg ( assignment-expression , type-name )
7735 __builtin_offsetof ( type-name , offsetof-member-designator )
7736 __builtin_choose_expr ( assignment-expression ,
7737 assignment-expression ,
7738 assignment-expression )
7739 __builtin_types_compatible_p ( type-name , type-name )
7740 __builtin_tgmath ( expr-list )
7741 __builtin_complex ( assignment-expression , assignment-expression )
7742 __builtin_shuffle ( assignment-expression , assignment-expression )
7743 __builtin_shuffle ( assignment-expression ,
7744 assignment-expression ,
7745 assignment-expression, )
7747 offsetof-member-designator:
7748 identifier
7749 offsetof-member-designator . identifier
7750 offsetof-member-designator [ expression ]
7752 Objective-C:
7754 primary-expression:
7755 [ objc-receiver objc-message-args ]
7756 @selector ( objc-selector-arg )
7757 @protocol ( identifier )
7758 @encode ( type-name )
7759 objc-string-literal
7760 Classname . identifier
7763 static struct c_expr
7764 c_parser_postfix_expression (c_parser *parser)
7766 struct c_expr expr, e1;
7767 struct c_type_name *t1, *t2;
7768 location_t loc = c_parser_peek_token (parser)->location;
7769 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7770 expr.original_code = ERROR_MARK;
7771 expr.original_type = NULL;
7772 switch (c_parser_peek_token (parser)->type)
7774 case CPP_NUMBER:
7775 expr.value = c_parser_peek_token (parser)->value;
7776 set_c_expr_source_range (&expr, tok_range);
7777 loc = c_parser_peek_token (parser)->location;
7778 c_parser_consume_token (parser);
7779 if (TREE_CODE (expr.value) == FIXED_CST
7780 && !targetm.fixed_point_supported_p ())
7782 error_at (loc, "fixed-point types not supported for this target");
7783 expr.value = error_mark_node;
7785 break;
7786 case CPP_CHAR:
7787 case CPP_CHAR16:
7788 case CPP_CHAR32:
7789 case CPP_WCHAR:
7790 expr.value = c_parser_peek_token (parser)->value;
7791 /* For the purpose of warning when a pointer is compared with
7792 a zero character constant. */
7793 expr.original_type = char_type_node;
7794 set_c_expr_source_range (&expr, tok_range);
7795 c_parser_consume_token (parser);
7796 break;
7797 case CPP_STRING:
7798 case CPP_STRING16:
7799 case CPP_STRING32:
7800 case CPP_WSTRING:
7801 case CPP_UTF8STRING:
7802 expr.value = c_parser_peek_token (parser)->value;
7803 set_c_expr_source_range (&expr, tok_range);
7804 expr.original_code = STRING_CST;
7805 c_parser_consume_token (parser);
7806 break;
7807 case CPP_OBJC_STRING:
7808 gcc_assert (c_dialect_objc ());
7809 expr.value
7810 = objc_build_string_object (c_parser_peek_token (parser)->value);
7811 set_c_expr_source_range (&expr, tok_range);
7812 c_parser_consume_token (parser);
7813 break;
7814 case CPP_NAME:
7815 switch (c_parser_peek_token (parser)->id_kind)
7817 case C_ID_ID:
7819 tree id = c_parser_peek_token (parser)->value;
7820 c_parser_consume_token (parser);
7821 expr.value = build_external_ref (loc, id,
7822 (c_parser_peek_token (parser)->type
7823 == CPP_OPEN_PAREN),
7824 &expr.original_type);
7825 set_c_expr_source_range (&expr, tok_range);
7826 break;
7828 case C_ID_CLASSNAME:
7830 /* Here we parse the Objective-C 2.0 Class.name dot
7831 syntax. */
7832 tree class_name = c_parser_peek_token (parser)->value;
7833 tree component;
7834 c_parser_consume_token (parser);
7835 gcc_assert (c_dialect_objc ());
7836 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7838 expr.set_error ();
7839 break;
7841 if (c_parser_next_token_is_not (parser, CPP_NAME))
7843 c_parser_error (parser, "expected identifier");
7844 expr.set_error ();
7845 break;
7847 c_token *component_tok = c_parser_peek_token (parser);
7848 component = component_tok->value;
7849 location_t end_loc = component_tok->get_finish ();
7850 c_parser_consume_token (parser);
7851 expr.value = objc_build_class_component_ref (class_name,
7852 component);
7853 set_c_expr_source_range (&expr, loc, end_loc);
7854 break;
7856 default:
7857 c_parser_error (parser, "expected expression");
7858 expr.set_error ();
7859 break;
7861 break;
7862 case CPP_OPEN_PAREN:
7863 /* A parenthesized expression, statement expression or compound
7864 literal. */
7865 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7867 /* A statement expression. */
7868 tree stmt;
7869 location_t brace_loc;
7870 c_parser_consume_token (parser);
7871 brace_loc = c_parser_peek_token (parser)->location;
7872 c_parser_consume_token (parser);
7873 if (!building_stmt_list_p ())
7875 error_at (loc, "braced-group within expression allowed "
7876 "only inside a function");
7877 parser->error = true;
7878 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7879 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7880 expr.set_error ();
7881 break;
7883 stmt = c_begin_stmt_expr ();
7884 c_parser_compound_statement_nostart (parser);
7885 location_t close_loc = c_parser_peek_token (parser)->location;
7886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7887 "expected %<)%>");
7888 pedwarn (loc, OPT_Wpedantic,
7889 "ISO C forbids braced-groups within expressions");
7890 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7891 set_c_expr_source_range (&expr, loc, close_loc);
7892 mark_exp_read (expr.value);
7894 else
7896 /* A parenthesized expression. */
7897 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7898 c_parser_consume_token (parser);
7899 expr = c_parser_expression (parser);
7900 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7901 TREE_NO_WARNING (expr.value) = 1;
7902 if (expr.original_code != C_MAYBE_CONST_EXPR
7903 && expr.original_code != SIZEOF_EXPR)
7904 expr.original_code = ERROR_MARK;
7905 /* Don't change EXPR.ORIGINAL_TYPE. */
7906 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7907 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7908 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7909 "expected %<)%>", loc_open_paren);
7911 break;
7912 case CPP_KEYWORD:
7913 switch (c_parser_peek_token (parser)->keyword)
7915 case RID_FUNCTION_NAME:
7916 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7917 "%<__FUNCTION__%> predefined identifier");
7918 expr.value = fname_decl (loc,
7919 c_parser_peek_token (parser)->keyword,
7920 c_parser_peek_token (parser)->value);
7921 set_c_expr_source_range (&expr, loc, loc);
7922 c_parser_consume_token (parser);
7923 break;
7924 case RID_PRETTY_FUNCTION_NAME:
7925 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7926 "%<__PRETTY_FUNCTION__%> predefined identifier");
7927 expr.value = fname_decl (loc,
7928 c_parser_peek_token (parser)->keyword,
7929 c_parser_peek_token (parser)->value);
7930 set_c_expr_source_range (&expr, loc, loc);
7931 c_parser_consume_token (parser);
7932 break;
7933 case RID_C99_FUNCTION_NAME:
7934 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7935 "%<__func__%> predefined identifier");
7936 expr.value = fname_decl (loc,
7937 c_parser_peek_token (parser)->keyword,
7938 c_parser_peek_token (parser)->value);
7939 set_c_expr_source_range (&expr, loc, loc);
7940 c_parser_consume_token (parser);
7941 break;
7942 case RID_VA_ARG:
7944 location_t start_loc = loc;
7945 c_parser_consume_token (parser);
7946 matching_parens parens;
7947 if (!parens.require_open (parser))
7949 expr.set_error ();
7950 break;
7952 e1 = c_parser_expr_no_commas (parser, NULL);
7953 mark_exp_read (e1.value);
7954 e1.value = c_fully_fold (e1.value, false, NULL);
7955 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7957 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7958 expr.set_error ();
7959 break;
7961 loc = c_parser_peek_token (parser)->location;
7962 t1 = c_parser_type_name (parser);
7963 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7965 "expected %<)%>");
7966 if (t1 == NULL)
7968 expr.set_error ();
7970 else
7972 tree type_expr = NULL_TREE;
7973 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7974 groktypename (t1, &type_expr, NULL));
7975 if (type_expr)
7977 expr.value = build2 (C_MAYBE_CONST_EXPR,
7978 TREE_TYPE (expr.value), type_expr,
7979 expr.value);
7980 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7982 set_c_expr_source_range (&expr, start_loc, end_loc);
7985 break;
7986 case RID_OFFSETOF:
7988 c_parser_consume_token (parser);
7989 matching_parens parens;
7990 if (!parens.require_open (parser))
7992 expr.set_error ();
7993 break;
7995 t1 = c_parser_type_name (parser);
7996 if (t1 == NULL)
7997 parser->error = true;
7998 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7999 gcc_assert (parser->error);
8000 if (parser->error)
8002 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8003 expr.set_error ();
8004 break;
8006 tree type = groktypename (t1, NULL, NULL);
8007 tree offsetof_ref;
8008 if (type == error_mark_node)
8009 offsetof_ref = error_mark_node;
8010 else
8012 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8013 SET_EXPR_LOCATION (offsetof_ref, loc);
8015 /* Parse the second argument to __builtin_offsetof. We
8016 must have one identifier, and beyond that we want to
8017 accept sub structure and sub array references. */
8018 if (c_parser_next_token_is (parser, CPP_NAME))
8020 c_token *comp_tok = c_parser_peek_token (parser);
8021 offsetof_ref = build_component_ref
8022 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8023 c_parser_consume_token (parser);
8024 while (c_parser_next_token_is (parser, CPP_DOT)
8025 || c_parser_next_token_is (parser,
8026 CPP_OPEN_SQUARE)
8027 || c_parser_next_token_is (parser,
8028 CPP_DEREF))
8030 if (c_parser_next_token_is (parser, CPP_DEREF))
8032 loc = c_parser_peek_token (parser)->location;
8033 offsetof_ref = build_array_ref (loc,
8034 offsetof_ref,
8035 integer_zero_node);
8036 goto do_dot;
8038 else if (c_parser_next_token_is (parser, CPP_DOT))
8040 do_dot:
8041 c_parser_consume_token (parser);
8042 if (c_parser_next_token_is_not (parser,
8043 CPP_NAME))
8045 c_parser_error (parser, "expected identifier");
8046 break;
8048 c_token *comp_tok = c_parser_peek_token (parser);
8049 offsetof_ref = build_component_ref
8050 (loc, offsetof_ref, comp_tok->value,
8051 comp_tok->location);
8052 c_parser_consume_token (parser);
8054 else
8056 struct c_expr ce;
8057 tree idx;
8058 loc = c_parser_peek_token (parser)->location;
8059 c_parser_consume_token (parser);
8060 ce = c_parser_expression (parser);
8061 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8062 idx = ce.value;
8063 idx = c_fully_fold (idx, false, NULL);
8064 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8065 "expected %<]%>");
8066 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8070 else
8071 c_parser_error (parser, "expected identifier");
8072 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8073 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8074 "expected %<)%>");
8075 expr.value = fold_offsetof (offsetof_ref);
8076 set_c_expr_source_range (&expr, loc, end_loc);
8078 break;
8079 case RID_CHOOSE_EXPR:
8081 vec<c_expr_t, va_gc> *cexpr_list;
8082 c_expr_t *e1_p, *e2_p, *e3_p;
8083 tree c;
8084 location_t close_paren_loc;
8086 c_parser_consume_token (parser);
8087 if (!c_parser_get_builtin_args (parser,
8088 "__builtin_choose_expr",
8089 &cexpr_list, true,
8090 &close_paren_loc))
8092 expr.set_error ();
8093 break;
8096 if (vec_safe_length (cexpr_list) != 3)
8098 error_at (loc, "wrong number of arguments to "
8099 "%<__builtin_choose_expr%>");
8100 expr.set_error ();
8101 break;
8104 e1_p = &(*cexpr_list)[0];
8105 e2_p = &(*cexpr_list)[1];
8106 e3_p = &(*cexpr_list)[2];
8108 c = e1_p->value;
8109 mark_exp_read (e2_p->value);
8110 mark_exp_read (e3_p->value);
8111 if (TREE_CODE (c) != INTEGER_CST
8112 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8113 error_at (loc,
8114 "first argument to %<__builtin_choose_expr%> not"
8115 " a constant");
8116 constant_expression_warning (c);
8117 expr = integer_zerop (c) ? *e3_p : *e2_p;
8118 set_c_expr_source_range (&expr, loc, close_paren_loc);
8119 break;
8121 case RID_TYPES_COMPATIBLE_P:
8123 c_parser_consume_token (parser);
8124 matching_parens parens;
8125 if (!parens.require_open (parser))
8127 expr.set_error ();
8128 break;
8130 t1 = c_parser_type_name (parser);
8131 if (t1 == NULL)
8133 expr.set_error ();
8134 break;
8136 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8138 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8139 expr.set_error ();
8140 break;
8142 t2 = c_parser_type_name (parser);
8143 if (t2 == NULL)
8145 expr.set_error ();
8146 break;
8148 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8149 parens.skip_until_found_close (parser);
8150 tree e1, e2;
8151 e1 = groktypename (t1, NULL, NULL);
8152 e2 = groktypename (t2, NULL, NULL);
8153 if (e1 == error_mark_node || e2 == error_mark_node)
8155 expr.set_error ();
8156 break;
8159 e1 = TYPE_MAIN_VARIANT (e1);
8160 e2 = TYPE_MAIN_VARIANT (e2);
8162 expr.value
8163 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8164 set_c_expr_source_range (&expr, loc, close_paren_loc);
8166 break;
8167 case RID_BUILTIN_TGMATH:
8169 vec<c_expr_t, va_gc> *cexpr_list;
8170 location_t close_paren_loc;
8172 c_parser_consume_token (parser);
8173 if (!c_parser_get_builtin_args (parser,
8174 "__builtin_tgmath",
8175 &cexpr_list, false,
8176 &close_paren_loc))
8178 expr.set_error ();
8179 break;
8182 if (vec_safe_length (cexpr_list) < 3)
8184 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8185 expr.set_error ();
8186 break;
8189 unsigned int i;
8190 c_expr_t *p;
8191 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8192 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8193 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8194 if (nargs == 0)
8196 expr.set_error ();
8197 break;
8199 if (vec_safe_length (cexpr_list) < nargs)
8201 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8202 expr.set_error ();
8203 break;
8205 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8206 if (num_functions < 2)
8208 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8209 expr.set_error ();
8210 break;
8213 /* The first NUM_FUNCTIONS expressions are the function
8214 pointers. The remaining NARGS expressions are the
8215 arguments that are to be passed to one of those
8216 functions, chosen following <tgmath.h> rules. */
8217 for (unsigned int j = 1; j < num_functions; j++)
8219 unsigned int this_nargs
8220 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8221 if (this_nargs == 0)
8223 expr.set_error ();
8224 goto out;
8226 if (this_nargs != nargs)
8228 error_at ((*cexpr_list)[j].get_location (),
8229 "argument %u of %<__builtin_tgmath%> has "
8230 "wrong number of arguments", j + 1);
8231 expr.set_error ();
8232 goto out;
8236 /* The functions all have the same number of arguments.
8237 Determine whether arguments and return types vary in
8238 ways permitted for <tgmath.h> functions. */
8239 /* The first entry in each of these vectors is for the
8240 return type, subsequent entries for parameter
8241 types. */
8242 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8243 auto_vec<tree> parm_first (nargs + 1);
8244 auto_vec<bool> parm_complex (nargs + 1);
8245 auto_vec<bool> parm_varies (nargs + 1);
8246 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8247 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8248 parm_first.quick_push (first_ret);
8249 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8250 parm_varies.quick_push (false);
8251 function_args_iterator iter;
8252 tree t;
8253 unsigned int argpos;
8254 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8256 if (t == void_type_node)
8257 break;
8258 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8259 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8260 parm_varies.quick_push (false);
8262 for (unsigned int j = 1; j < num_functions; j++)
8264 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8265 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8266 if (ret != parm_first[0])
8268 parm_varies[0] = true;
8269 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8270 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8272 error_at ((*cexpr_list)[0].get_location (),
8273 "invalid type-generic return type for "
8274 "argument %u of %<__builtin_tgmath%>",
8276 expr.set_error ();
8277 goto out;
8279 if (!SCALAR_FLOAT_TYPE_P (ret)
8280 && !COMPLEX_FLOAT_TYPE_P (ret))
8282 error_at ((*cexpr_list)[j].get_location (),
8283 "invalid type-generic return type for "
8284 "argument %u of %<__builtin_tgmath%>",
8285 j + 1);
8286 expr.set_error ();
8287 goto out;
8290 if (TREE_CODE (ret) == COMPLEX_TYPE)
8291 parm_complex[0] = true;
8292 argpos = 1;
8293 FOREACH_FUNCTION_ARGS (type, t, iter)
8295 if (t == void_type_node)
8296 break;
8297 t = TYPE_MAIN_VARIANT (t);
8298 if (t != parm_first[argpos])
8300 parm_varies[argpos] = true;
8301 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8302 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8304 error_at ((*cexpr_list)[0].get_location (),
8305 "invalid type-generic type for "
8306 "argument %u of argument %u of "
8307 "%<__builtin_tgmath%>", argpos, 1);
8308 expr.set_error ();
8309 goto out;
8311 if (!SCALAR_FLOAT_TYPE_P (t)
8312 && !COMPLEX_FLOAT_TYPE_P (t))
8314 error_at ((*cexpr_list)[j].get_location (),
8315 "invalid type-generic type for "
8316 "argument %u of argument %u of "
8317 "%<__builtin_tgmath%>", argpos, j + 1);
8318 expr.set_error ();
8319 goto out;
8322 if (TREE_CODE (t) == COMPLEX_TYPE)
8323 parm_complex[argpos] = true;
8324 argpos++;
8327 enum tgmath_parm_kind max_variation = tgmath_fixed;
8328 for (unsigned int j = 0; j <= nargs; j++)
8330 enum tgmath_parm_kind this_kind;
8331 if (parm_varies[j])
8333 if (parm_complex[j])
8334 max_variation = this_kind = tgmath_complex;
8335 else
8337 this_kind = tgmath_real;
8338 if (max_variation != tgmath_complex)
8339 max_variation = tgmath_real;
8342 else
8343 this_kind = tgmath_fixed;
8344 parm_kind.quick_push (this_kind);
8346 if (max_variation == tgmath_fixed)
8348 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8349 "all have the same type");
8350 expr.set_error ();
8351 break;
8354 /* Identify a parameter (not the return type) that varies,
8355 including with complex types if any variation includes
8356 complex types; there must be at least one such
8357 parameter. */
8358 unsigned int tgarg = 0;
8359 for (unsigned int j = 1; j <= nargs; j++)
8360 if (parm_kind[j] == max_variation)
8362 tgarg = j;
8363 break;
8365 if (tgarg == 0)
8367 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8368 "lack type-generic parameter");
8369 expr.set_error ();
8370 break;
8373 /* Determine the type of the relevant parameter for each
8374 function. */
8375 auto_vec<tree> tg_type (num_functions);
8376 for (unsigned int j = 0; j < num_functions; j++)
8378 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8379 argpos = 1;
8380 FOREACH_FUNCTION_ARGS (type, t, iter)
8382 if (argpos == tgarg)
8384 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8385 break;
8387 argpos++;
8391 /* Verify that the corresponding types are different for
8392 all the listed functions. Also determine whether all
8393 the types are complex, whether all the types are
8394 standard or binary, and whether all the types are
8395 decimal. */
8396 bool all_complex = true;
8397 bool all_binary = true;
8398 bool all_decimal = true;
8399 hash_set<tree> tg_types;
8400 FOR_EACH_VEC_ELT (tg_type, i, t)
8402 if (TREE_CODE (t) == COMPLEX_TYPE)
8403 all_decimal = false;
8404 else
8406 all_complex = false;
8407 if (DECIMAL_FLOAT_TYPE_P (t))
8408 all_binary = false;
8409 else
8410 all_decimal = false;
8412 if (tg_types.add (t))
8414 error_at ((*cexpr_list)[i].get_location (),
8415 "duplicate type-generic parameter type for "
8416 "function argument %u of %<__builtin_tgmath%>",
8417 i + 1);
8418 expr.set_error ();
8419 goto out;
8423 /* Verify that other parameters and the return type whose
8424 types vary have their types varying in the correct
8425 way. */
8426 for (unsigned int j = 0; j < num_functions; j++)
8428 tree exp_type = tg_type[j];
8429 tree exp_real_type = exp_type;
8430 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8431 exp_real_type = TREE_TYPE (exp_type);
8432 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8433 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8434 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8435 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8437 error_at ((*cexpr_list)[j].get_location (),
8438 "bad return type for function argument %u "
8439 "of %<__builtin_tgmath%>", j + 1);
8440 expr.set_error ();
8441 goto out;
8443 argpos = 1;
8444 FOREACH_FUNCTION_ARGS (type, t, iter)
8446 if (t == void_type_node)
8447 break;
8448 t = TYPE_MAIN_VARIANT (t);
8449 if ((parm_kind[argpos] == tgmath_complex
8450 && t != exp_type)
8451 || (parm_kind[argpos] == tgmath_real
8452 && t != exp_real_type))
8454 error_at ((*cexpr_list)[j].get_location (),
8455 "bad type for argument %u of "
8456 "function argument %u of "
8457 "%<__builtin_tgmath%>", argpos, j + 1);
8458 expr.set_error ();
8459 goto out;
8461 argpos++;
8465 /* The functions listed are a valid set of functions for a
8466 <tgmath.h> macro to select between. Identify the
8467 matching function, if any. First, the argument types
8468 must be combined following <tgmath.h> rules. Integer
8469 types are treated as _Decimal64 if any type-generic
8470 argument is decimal, or if the only alternatives for
8471 type-generic arguments are of decimal types, and are
8472 otherwise treated as double (or _Complex double for
8473 complex integer types). After that adjustment, types
8474 are combined following the usual arithmetic
8475 conversions. If the function only accepts complex
8476 arguments, a complex type is produced. */
8477 bool arg_complex = all_complex;
8478 bool arg_binary = all_binary;
8479 bool arg_int_decimal = all_decimal;
8480 for (unsigned int j = 1; j <= nargs; j++)
8482 if (parm_kind[j] == tgmath_fixed)
8483 continue;
8484 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8485 tree type = TREE_TYPE (ce->value);
8486 if (!INTEGRAL_TYPE_P (type)
8487 && !SCALAR_FLOAT_TYPE_P (type)
8488 && TREE_CODE (type) != COMPLEX_TYPE)
8490 error_at (ce->get_location (),
8491 "invalid type of argument %u of type-generic "
8492 "function", j);
8493 expr.set_error ();
8494 goto out;
8496 if (DECIMAL_FLOAT_TYPE_P (type))
8498 arg_int_decimal = true;
8499 if (all_complex)
8501 error_at (ce->get_location (),
8502 "decimal floating-point argument %u to "
8503 "complex-only type-generic function", j);
8504 expr.set_error ();
8505 goto out;
8507 else if (all_binary)
8509 error_at (ce->get_location (),
8510 "decimal floating-point argument %u to "
8511 "binary-only type-generic function", j);
8512 expr.set_error ();
8513 goto out;
8515 else if (arg_complex)
8517 error_at (ce->get_location (),
8518 "both complex and decimal floating-point "
8519 "arguments to type-generic function");
8520 expr.set_error ();
8521 goto out;
8523 else if (arg_binary)
8525 error_at (ce->get_location (),
8526 "both binary and decimal floating-point "
8527 "arguments to type-generic function");
8528 expr.set_error ();
8529 goto out;
8532 else if (TREE_CODE (type) == COMPLEX_TYPE)
8534 arg_complex = true;
8535 if (COMPLEX_FLOAT_TYPE_P (type))
8536 arg_binary = true;
8537 if (all_decimal)
8539 error_at (ce->get_location (),
8540 "complex argument %u to "
8541 "decimal-only type-generic function", j);
8542 expr.set_error ();
8543 goto out;
8545 else if (arg_int_decimal)
8547 error_at (ce->get_location (),
8548 "both complex and decimal floating-point "
8549 "arguments to type-generic function");
8550 expr.set_error ();
8551 goto out;
8554 else if (SCALAR_FLOAT_TYPE_P (type))
8556 arg_binary = true;
8557 if (all_decimal)
8559 error_at (ce->get_location (),
8560 "binary argument %u to "
8561 "decimal-only type-generic function", j);
8562 expr.set_error ();
8563 goto out;
8565 else if (arg_int_decimal)
8567 error_at (ce->get_location (),
8568 "both binary and decimal floating-point "
8569 "arguments to type-generic function");
8570 expr.set_error ();
8571 goto out;
8575 tree arg_real = NULL_TREE;
8576 for (unsigned int j = 1; j <= nargs; j++)
8578 if (parm_kind[j] == tgmath_fixed)
8579 continue;
8580 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8581 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8582 if (TREE_CODE (type) == COMPLEX_TYPE)
8583 type = TREE_TYPE (type);
8584 if (INTEGRAL_TYPE_P (type))
8585 type = (arg_int_decimal
8586 ? dfloat64_type_node
8587 : double_type_node);
8588 if (arg_real == NULL_TREE)
8589 arg_real = type;
8590 else
8591 arg_real = common_type (arg_real, type);
8592 if (arg_real == error_mark_node)
8594 expr.set_error ();
8595 goto out;
8598 tree arg_type = (arg_complex
8599 ? build_complex_type (arg_real)
8600 : arg_real);
8602 /* Look for a function to call with type-generic parameter
8603 type ARG_TYPE. */
8604 c_expr_t *fn = NULL;
8605 for (unsigned int j = 0; j < num_functions; j++)
8607 if (tg_type[j] == arg_type)
8609 fn = &(*cexpr_list)[j];
8610 break;
8613 if (fn == NULL
8614 && parm_kind[0] == tgmath_fixed
8615 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8617 /* Presume this is a macro that rounds its result to a
8618 narrower type, and look for the first function with
8619 at least the range and precision of the argument
8620 type. */
8621 for (unsigned int j = 0; j < num_functions; j++)
8623 if (arg_complex
8624 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8625 continue;
8626 tree real_tg_type = (arg_complex
8627 ? TREE_TYPE (tg_type[j])
8628 : tg_type[j]);
8629 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8630 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8631 continue;
8632 scalar_float_mode arg_mode
8633 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8634 scalar_float_mode tg_mode
8635 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8636 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8637 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8638 if (arg_fmt->b == tg_fmt->b
8639 && arg_fmt->p <= tg_fmt->p
8640 && arg_fmt->emax <= tg_fmt->emax
8641 && (arg_fmt->emin - arg_fmt->p
8642 >= tg_fmt->emin - tg_fmt->p))
8644 fn = &(*cexpr_list)[j];
8645 break;
8649 if (fn == NULL)
8651 error_at (loc, "no matching function for type-generic call");
8652 expr.set_error ();
8653 break;
8656 /* Construct a call to FN. */
8657 vec<tree, va_gc> *args;
8658 vec_alloc (args, nargs);
8659 vec<tree, va_gc> *origtypes;
8660 vec_alloc (origtypes, nargs);
8661 auto_vec<location_t> arg_loc (nargs);
8662 for (unsigned int j = 0; j < nargs; j++)
8664 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8665 args->quick_push (ce->value);
8666 arg_loc.quick_push (ce->get_location ());
8667 origtypes->quick_push (ce->original_type);
8669 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8670 args, origtypes);
8671 set_c_expr_source_range (&expr, loc, close_paren_loc);
8672 break;
8674 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8676 vec<c_expr_t, va_gc> *cexpr_list;
8677 c_expr_t *e2_p;
8678 tree chain_value;
8679 location_t close_paren_loc;
8681 c_parser_consume_token (parser);
8682 if (!c_parser_get_builtin_args (parser,
8683 "__builtin_call_with_static_chain",
8684 &cexpr_list, false,
8685 &close_paren_loc))
8687 expr.set_error ();
8688 break;
8690 if (vec_safe_length (cexpr_list) != 2)
8692 error_at (loc, "wrong number of arguments to "
8693 "%<__builtin_call_with_static_chain%>");
8694 expr.set_error ();
8695 break;
8698 expr = (*cexpr_list)[0];
8699 e2_p = &(*cexpr_list)[1];
8700 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8701 chain_value = e2_p->value;
8702 mark_exp_read (chain_value);
8704 if (TREE_CODE (expr.value) != CALL_EXPR)
8705 error_at (loc, "first argument to "
8706 "%<__builtin_call_with_static_chain%> "
8707 "must be a call expression");
8708 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8709 error_at (loc, "second argument to "
8710 "%<__builtin_call_with_static_chain%> "
8711 "must be a pointer type");
8712 else
8713 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8714 set_c_expr_source_range (&expr, loc, close_paren_loc);
8715 break;
8717 case RID_BUILTIN_COMPLEX:
8719 vec<c_expr_t, va_gc> *cexpr_list;
8720 c_expr_t *e1_p, *e2_p;
8721 location_t close_paren_loc;
8723 c_parser_consume_token (parser);
8724 if (!c_parser_get_builtin_args (parser,
8725 "__builtin_complex",
8726 &cexpr_list, false,
8727 &close_paren_loc))
8729 expr.set_error ();
8730 break;
8733 if (vec_safe_length (cexpr_list) != 2)
8735 error_at (loc, "wrong number of arguments to "
8736 "%<__builtin_complex%>");
8737 expr.set_error ();
8738 break;
8741 e1_p = &(*cexpr_list)[0];
8742 e2_p = &(*cexpr_list)[1];
8744 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8745 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8746 e1_p->value = convert (TREE_TYPE (e1_p->value),
8747 TREE_OPERAND (e1_p->value, 0));
8748 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8749 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8750 e2_p->value = convert (TREE_TYPE (e2_p->value),
8751 TREE_OPERAND (e2_p->value, 0));
8752 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8753 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8754 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8755 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8757 error_at (loc, "%<__builtin_complex%> operand "
8758 "not of real binary floating-point type");
8759 expr.set_error ();
8760 break;
8762 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8763 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8765 error_at (loc,
8766 "%<__builtin_complex%> operands of different types");
8767 expr.set_error ();
8768 break;
8770 pedwarn_c90 (loc, OPT_Wpedantic,
8771 "ISO C90 does not support complex types");
8772 expr.value = build2_loc (loc, COMPLEX_EXPR,
8773 build_complex_type
8774 (TYPE_MAIN_VARIANT
8775 (TREE_TYPE (e1_p->value))),
8776 e1_p->value, e2_p->value);
8777 set_c_expr_source_range (&expr, loc, close_paren_loc);
8778 break;
8780 case RID_BUILTIN_SHUFFLE:
8782 vec<c_expr_t, va_gc> *cexpr_list;
8783 unsigned int i;
8784 c_expr_t *p;
8785 location_t close_paren_loc;
8787 c_parser_consume_token (parser);
8788 if (!c_parser_get_builtin_args (parser,
8789 "__builtin_shuffle",
8790 &cexpr_list, false,
8791 &close_paren_loc))
8793 expr.set_error ();
8794 break;
8797 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8798 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8800 if (vec_safe_length (cexpr_list) == 2)
8801 expr.value =
8802 c_build_vec_perm_expr
8803 (loc, (*cexpr_list)[0].value,
8804 NULL_TREE, (*cexpr_list)[1].value);
8806 else if (vec_safe_length (cexpr_list) == 3)
8807 expr.value =
8808 c_build_vec_perm_expr
8809 (loc, (*cexpr_list)[0].value,
8810 (*cexpr_list)[1].value,
8811 (*cexpr_list)[2].value);
8812 else
8814 error_at (loc, "wrong number of arguments to "
8815 "%<__builtin_shuffle%>");
8816 expr.set_error ();
8818 set_c_expr_source_range (&expr, loc, close_paren_loc);
8819 break;
8821 case RID_AT_SELECTOR:
8823 gcc_assert (c_dialect_objc ());
8824 c_parser_consume_token (parser);
8825 matching_parens parens;
8826 if (!parens.require_open (parser))
8828 expr.set_error ();
8829 break;
8831 tree sel = c_parser_objc_selector_arg (parser);
8832 location_t close_loc = c_parser_peek_token (parser)->location;
8833 parens.skip_until_found_close (parser);
8834 expr.value = objc_build_selector_expr (loc, sel);
8835 set_c_expr_source_range (&expr, loc, close_loc);
8837 break;
8838 case RID_AT_PROTOCOL:
8840 gcc_assert (c_dialect_objc ());
8841 c_parser_consume_token (parser);
8842 matching_parens parens;
8843 if (!parens.require_open (parser))
8845 expr.set_error ();
8846 break;
8848 if (c_parser_next_token_is_not (parser, CPP_NAME))
8850 c_parser_error (parser, "expected identifier");
8851 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8852 expr.set_error ();
8853 break;
8855 tree id = c_parser_peek_token (parser)->value;
8856 c_parser_consume_token (parser);
8857 location_t close_loc = c_parser_peek_token (parser)->location;
8858 parens.skip_until_found_close (parser);
8859 expr.value = objc_build_protocol_expr (id);
8860 set_c_expr_source_range (&expr, loc, close_loc);
8862 break;
8863 case RID_AT_ENCODE:
8865 /* Extension to support C-structures in the archiver. */
8866 gcc_assert (c_dialect_objc ());
8867 c_parser_consume_token (parser);
8868 matching_parens parens;
8869 if (!parens.require_open (parser))
8871 expr.set_error ();
8872 break;
8874 t1 = c_parser_type_name (parser);
8875 if (t1 == NULL)
8877 expr.set_error ();
8878 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8879 break;
8881 location_t close_loc = c_parser_peek_token (parser)->location;
8882 parens.skip_until_found_close (parser);
8883 tree type = groktypename (t1, NULL, NULL);
8884 expr.value = objc_build_encode_expr (type);
8885 set_c_expr_source_range (&expr, loc, close_loc);
8887 break;
8888 case RID_GENERIC:
8889 expr = c_parser_generic_selection (parser);
8890 break;
8891 default:
8892 c_parser_error (parser, "expected expression");
8893 expr.set_error ();
8894 break;
8896 break;
8897 case CPP_OPEN_SQUARE:
8898 if (c_dialect_objc ())
8900 tree receiver, args;
8901 c_parser_consume_token (parser);
8902 receiver = c_parser_objc_receiver (parser);
8903 args = c_parser_objc_message_args (parser);
8904 location_t close_loc = c_parser_peek_token (parser)->location;
8905 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8906 "expected %<]%>");
8907 expr.value = objc_build_message_expr (receiver, args);
8908 set_c_expr_source_range (&expr, loc, close_loc);
8909 break;
8911 /* Else fall through to report error. */
8912 /* FALLTHRU */
8913 default:
8914 c_parser_error (parser, "expected expression");
8915 expr.set_error ();
8916 break;
8918 out:
8919 return c_parser_postfix_expression_after_primary
8920 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8923 /* Parse a postfix expression after a parenthesized type name: the
8924 brace-enclosed initializer of a compound literal, possibly followed
8925 by some postfix operators. This is separate because it is not
8926 possible to tell until after the type name whether a cast
8927 expression has a cast or a compound literal, or whether the operand
8928 of sizeof is a parenthesized type name or starts with a compound
8929 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8930 location of the first token after the parentheses around the type
8931 name. */
8933 static struct c_expr
8934 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8935 struct c_type_name *type_name,
8936 location_t type_loc)
8938 tree type;
8939 struct c_expr init;
8940 bool non_const;
8941 struct c_expr expr;
8942 location_t start_loc;
8943 tree type_expr = NULL_TREE;
8944 bool type_expr_const = true;
8945 check_compound_literal_type (type_loc, type_name);
8946 rich_location richloc (line_table, type_loc);
8947 start_init (NULL_TREE, NULL, 0, &richloc);
8948 type = groktypename (type_name, &type_expr, &type_expr_const);
8949 start_loc = c_parser_peek_token (parser)->location;
8950 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8952 error_at (type_loc, "compound literal has variable size");
8953 type = error_mark_node;
8955 init = c_parser_braced_init (parser, type, false, NULL);
8956 finish_init ();
8957 maybe_warn_string_init (type_loc, type, init);
8959 if (type != error_mark_node
8960 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8961 && current_function_decl)
8963 error ("compound literal qualified by address-space qualifier");
8964 type = error_mark_node;
8967 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8968 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8969 ? CONSTRUCTOR_NON_CONST (init.value)
8970 : init.original_code == C_MAYBE_CONST_EXPR);
8971 non_const |= !type_expr_const;
8972 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8973 set_c_expr_source_range (&expr, init.src_range);
8974 expr.original_code = ERROR_MARK;
8975 expr.original_type = NULL;
8976 if (type != error_mark_node
8977 && expr.value != error_mark_node
8978 && type_expr)
8980 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8982 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8983 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8985 else
8987 gcc_assert (!non_const);
8988 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8989 type_expr, expr.value);
8992 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8995 /* Callback function for sizeof_pointer_memaccess_warning to compare
8996 types. */
8998 static bool
8999 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9001 return comptypes (type1, type2) == 1;
9004 /* Parse a postfix expression after the initial primary or compound
9005 literal; that is, parse a series of postfix operators.
9007 EXPR_LOC is the location of the primary expression. */
9009 static struct c_expr
9010 c_parser_postfix_expression_after_primary (c_parser *parser,
9011 location_t expr_loc,
9012 struct c_expr expr)
9014 struct c_expr orig_expr;
9015 tree ident, idx;
9016 location_t sizeof_arg_loc[3], comp_loc;
9017 tree sizeof_arg[3];
9018 unsigned int literal_zero_mask;
9019 unsigned int i;
9020 vec<tree, va_gc> *exprlist;
9021 vec<tree, va_gc> *origtypes = NULL;
9022 vec<location_t> arg_loc = vNULL;
9023 location_t start;
9024 location_t finish;
9026 while (true)
9028 location_t op_loc = c_parser_peek_token (parser)->location;
9029 switch (c_parser_peek_token (parser)->type)
9031 case CPP_OPEN_SQUARE:
9032 /* Array reference. */
9033 c_parser_consume_token (parser);
9034 idx = c_parser_expression (parser).value;
9035 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9036 "expected %<]%>");
9037 start = expr.get_start ();
9038 finish = parser->tokens_buf[0].location;
9039 expr.value = build_array_ref (op_loc, expr.value, idx);
9040 set_c_expr_source_range (&expr, start, finish);
9041 expr.original_code = ERROR_MARK;
9042 expr.original_type = NULL;
9043 break;
9044 case CPP_OPEN_PAREN:
9045 /* Function call. */
9046 c_parser_consume_token (parser);
9047 for (i = 0; i < 3; i++)
9049 sizeof_arg[i] = NULL_TREE;
9050 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9052 literal_zero_mask = 0;
9053 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9054 exprlist = NULL;
9055 else
9056 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9057 sizeof_arg_loc, sizeof_arg,
9058 &arg_loc, &literal_zero_mask);
9059 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9060 "expected %<)%>");
9061 orig_expr = expr;
9062 mark_exp_read (expr.value);
9063 if (warn_sizeof_pointer_memaccess)
9064 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9065 expr.value, exprlist,
9066 sizeof_arg,
9067 sizeof_ptr_memacc_comptypes);
9068 if (TREE_CODE (expr.value) == FUNCTION_DECL
9069 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9070 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9071 && vec_safe_length (exprlist) == 3)
9073 tree arg0 = (*exprlist)[0];
9074 tree arg2 = (*exprlist)[2];
9075 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9078 start = expr.get_start ();
9079 finish = parser->tokens_buf[0].get_finish ();
9080 expr.value
9081 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9082 exprlist, origtypes);
9083 set_c_expr_source_range (&expr, start, finish);
9085 expr.original_code = ERROR_MARK;
9086 if (TREE_CODE (expr.value) == INTEGER_CST
9087 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9088 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9089 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9090 expr.original_code = C_MAYBE_CONST_EXPR;
9091 expr.original_type = NULL;
9092 if (exprlist)
9094 release_tree_vector (exprlist);
9095 release_tree_vector (origtypes);
9097 arg_loc.release ();
9098 break;
9099 case CPP_DOT:
9100 /* Structure element reference. */
9101 c_parser_consume_token (parser);
9102 expr = default_function_array_conversion (expr_loc, expr);
9103 if (c_parser_next_token_is (parser, CPP_NAME))
9105 c_token *comp_tok = c_parser_peek_token (parser);
9106 ident = comp_tok->value;
9107 comp_loc = comp_tok->location;
9109 else
9111 c_parser_error (parser, "expected identifier");
9112 expr.set_error ();
9113 expr.original_code = ERROR_MARK;
9114 expr.original_type = NULL;
9115 return expr;
9117 start = expr.get_start ();
9118 finish = c_parser_peek_token (parser)->get_finish ();
9119 c_parser_consume_token (parser);
9120 expr.value = build_component_ref (op_loc, expr.value, ident,
9121 comp_loc);
9122 set_c_expr_source_range (&expr, start, finish);
9123 expr.original_code = ERROR_MARK;
9124 if (TREE_CODE (expr.value) != COMPONENT_REF)
9125 expr.original_type = NULL;
9126 else
9128 /* Remember the original type of a bitfield. */
9129 tree field = TREE_OPERAND (expr.value, 1);
9130 if (TREE_CODE (field) != FIELD_DECL)
9131 expr.original_type = NULL;
9132 else
9133 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9135 break;
9136 case CPP_DEREF:
9137 /* Structure element reference. */
9138 c_parser_consume_token (parser);
9139 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9140 if (c_parser_next_token_is (parser, CPP_NAME))
9142 c_token *comp_tok = c_parser_peek_token (parser);
9143 ident = comp_tok->value;
9144 comp_loc = comp_tok->location;
9146 else
9148 c_parser_error (parser, "expected identifier");
9149 expr.set_error ();
9150 expr.original_code = ERROR_MARK;
9151 expr.original_type = NULL;
9152 return expr;
9154 start = expr.get_start ();
9155 finish = c_parser_peek_token (parser)->get_finish ();
9156 c_parser_consume_token (parser);
9157 expr.value = build_component_ref (op_loc,
9158 build_indirect_ref (op_loc,
9159 expr.value,
9160 RO_ARROW),
9161 ident, comp_loc);
9162 set_c_expr_source_range (&expr, start, finish);
9163 expr.original_code = ERROR_MARK;
9164 if (TREE_CODE (expr.value) != COMPONENT_REF)
9165 expr.original_type = NULL;
9166 else
9168 /* Remember the original type of a bitfield. */
9169 tree field = TREE_OPERAND (expr.value, 1);
9170 if (TREE_CODE (field) != FIELD_DECL)
9171 expr.original_type = NULL;
9172 else
9173 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9175 break;
9176 case CPP_PLUS_PLUS:
9177 /* Postincrement. */
9178 start = expr.get_start ();
9179 finish = c_parser_peek_token (parser)->get_finish ();
9180 c_parser_consume_token (parser);
9181 expr = default_function_array_read_conversion (expr_loc, expr);
9182 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9183 expr.value, false);
9184 set_c_expr_source_range (&expr, start, finish);
9185 expr.original_code = ERROR_MARK;
9186 expr.original_type = NULL;
9187 break;
9188 case CPP_MINUS_MINUS:
9189 /* Postdecrement. */
9190 start = expr.get_start ();
9191 finish = c_parser_peek_token (parser)->get_finish ();
9192 c_parser_consume_token (parser);
9193 expr = default_function_array_read_conversion (expr_loc, expr);
9194 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9195 expr.value, false);
9196 set_c_expr_source_range (&expr, start, finish);
9197 expr.original_code = ERROR_MARK;
9198 expr.original_type = NULL;
9199 break;
9200 default:
9201 return expr;
9206 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9208 expression:
9209 assignment-expression
9210 expression , assignment-expression
9213 static struct c_expr
9214 c_parser_expression (c_parser *parser)
9216 location_t tloc = c_parser_peek_token (parser)->location;
9217 struct c_expr expr;
9218 expr = c_parser_expr_no_commas (parser, NULL);
9219 if (c_parser_next_token_is (parser, CPP_COMMA))
9220 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9221 while (c_parser_next_token_is (parser, CPP_COMMA))
9223 struct c_expr next;
9224 tree lhsval;
9225 location_t loc = c_parser_peek_token (parser)->location;
9226 location_t expr_loc;
9227 c_parser_consume_token (parser);
9228 expr_loc = c_parser_peek_token (parser)->location;
9229 lhsval = expr.value;
9230 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9231 lhsval = TREE_OPERAND (lhsval, 1);
9232 if (DECL_P (lhsval) || handled_component_p (lhsval))
9233 mark_exp_read (lhsval);
9234 next = c_parser_expr_no_commas (parser, NULL);
9235 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9236 expr.value = build_compound_expr (loc, expr.value, next.value);
9237 expr.original_code = COMPOUND_EXPR;
9238 expr.original_type = next.original_type;
9240 return expr;
9243 /* Parse an expression and convert functions or arrays to pointers and
9244 lvalues to rvalues. */
9246 static struct c_expr
9247 c_parser_expression_conv (c_parser *parser)
9249 struct c_expr expr;
9250 location_t loc = c_parser_peek_token (parser)->location;
9251 expr = c_parser_expression (parser);
9252 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9253 return expr;
9256 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9257 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9259 static inline void
9260 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9261 unsigned int idx)
9263 if (idx >= HOST_BITS_PER_INT)
9264 return;
9266 c_token *tok = c_parser_peek_token (parser);
9267 switch (tok->type)
9269 case CPP_NUMBER:
9270 case CPP_CHAR:
9271 case CPP_WCHAR:
9272 case CPP_CHAR16:
9273 case CPP_CHAR32:
9274 /* If a parameter is literal zero alone, remember it
9275 for -Wmemset-transposed-args warning. */
9276 if (integer_zerop (tok->value)
9277 && !TREE_OVERFLOW (tok->value)
9278 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9279 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9280 *literal_zero_mask |= 1U << idx;
9281 default:
9282 break;
9286 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9287 functions and arrays to pointers and lvalues to rvalues. If
9288 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9289 locations of function arguments into this vector.
9291 nonempty-expr-list:
9292 assignment-expression
9293 nonempty-expr-list , assignment-expression
9296 static vec<tree, va_gc> *
9297 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9298 vec<tree, va_gc> **p_orig_types,
9299 location_t *sizeof_arg_loc, tree *sizeof_arg,
9300 vec<location_t> *locations,
9301 unsigned int *literal_zero_mask)
9303 vec<tree, va_gc> *ret;
9304 vec<tree, va_gc> *orig_types;
9305 struct c_expr expr;
9306 unsigned int idx = 0;
9308 ret = make_tree_vector ();
9309 if (p_orig_types == NULL)
9310 orig_types = NULL;
9311 else
9312 orig_types = make_tree_vector ();
9314 if (literal_zero_mask)
9315 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9316 expr = c_parser_expr_no_commas (parser, NULL);
9317 if (convert_p)
9318 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9319 if (fold_p)
9320 expr.value = c_fully_fold (expr.value, false, NULL);
9321 ret->quick_push (expr.value);
9322 if (orig_types)
9323 orig_types->quick_push (expr.original_type);
9324 if (locations)
9325 locations->safe_push (expr.get_location ());
9326 if (sizeof_arg != NULL
9327 && expr.original_code == SIZEOF_EXPR)
9329 sizeof_arg[0] = c_last_sizeof_arg;
9330 sizeof_arg_loc[0] = c_last_sizeof_loc;
9332 while (c_parser_next_token_is (parser, CPP_COMMA))
9334 c_parser_consume_token (parser);
9335 if (literal_zero_mask)
9336 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9337 expr = c_parser_expr_no_commas (parser, NULL);
9338 if (convert_p)
9339 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9340 true);
9341 if (fold_p)
9342 expr.value = c_fully_fold (expr.value, false, NULL);
9343 vec_safe_push (ret, expr.value);
9344 if (orig_types)
9345 vec_safe_push (orig_types, expr.original_type);
9346 if (locations)
9347 locations->safe_push (expr.get_location ());
9348 if (++idx < 3
9349 && sizeof_arg != NULL
9350 && expr.original_code == SIZEOF_EXPR)
9352 sizeof_arg[idx] = c_last_sizeof_arg;
9353 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9356 if (orig_types)
9357 *p_orig_types = orig_types;
9358 return ret;
9361 /* Parse Objective-C-specific constructs. */
9363 /* Parse an objc-class-definition.
9365 objc-class-definition:
9366 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9367 objc-class-instance-variables[opt] objc-methodprotolist @end
9368 @implementation identifier objc-superclass[opt]
9369 objc-class-instance-variables[opt]
9370 @interface identifier ( identifier ) objc-protocol-refs[opt]
9371 objc-methodprotolist @end
9372 @interface identifier ( ) objc-protocol-refs[opt]
9373 objc-methodprotolist @end
9374 @implementation identifier ( identifier )
9376 objc-superclass:
9377 : identifier
9379 "@interface identifier (" must start "@interface identifier (
9380 identifier ) ...": objc-methodprotolist in the first production may
9381 not start with a parenthesized identifier as a declarator of a data
9382 definition with no declaration specifiers if the objc-superclass,
9383 objc-protocol-refs and objc-class-instance-variables are omitted. */
9385 static void
9386 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9388 bool iface_p;
9389 tree id1;
9390 tree superclass;
9391 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9392 iface_p = true;
9393 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9394 iface_p = false;
9395 else
9396 gcc_unreachable ();
9398 c_parser_consume_token (parser);
9399 if (c_parser_next_token_is_not (parser, CPP_NAME))
9401 c_parser_error (parser, "expected identifier");
9402 return;
9404 id1 = c_parser_peek_token (parser)->value;
9405 c_parser_consume_token (parser);
9406 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9408 /* We have a category or class extension. */
9409 tree id2;
9410 tree proto = NULL_TREE;
9411 matching_parens parens;
9412 parens.consume_open (parser);
9413 if (c_parser_next_token_is_not (parser, CPP_NAME))
9415 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9417 /* We have a class extension. */
9418 id2 = NULL_TREE;
9420 else
9422 c_parser_error (parser, "expected identifier or %<)%>");
9423 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9424 return;
9427 else
9429 id2 = c_parser_peek_token (parser)->value;
9430 c_parser_consume_token (parser);
9432 parens.skip_until_found_close (parser);
9433 if (!iface_p)
9435 objc_start_category_implementation (id1, id2);
9436 return;
9438 if (c_parser_next_token_is (parser, CPP_LESS))
9439 proto = c_parser_objc_protocol_refs (parser);
9440 objc_start_category_interface (id1, id2, proto, attributes);
9441 c_parser_objc_methodprotolist (parser);
9442 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9443 objc_finish_interface ();
9444 return;
9446 if (c_parser_next_token_is (parser, CPP_COLON))
9448 c_parser_consume_token (parser);
9449 if (c_parser_next_token_is_not (parser, CPP_NAME))
9451 c_parser_error (parser, "expected identifier");
9452 return;
9454 superclass = c_parser_peek_token (parser)->value;
9455 c_parser_consume_token (parser);
9457 else
9458 superclass = NULL_TREE;
9459 if (iface_p)
9461 tree proto = NULL_TREE;
9462 if (c_parser_next_token_is (parser, CPP_LESS))
9463 proto = c_parser_objc_protocol_refs (parser);
9464 objc_start_class_interface (id1, superclass, proto, attributes);
9466 else
9467 objc_start_class_implementation (id1, superclass);
9468 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9469 c_parser_objc_class_instance_variables (parser);
9470 if (iface_p)
9472 objc_continue_interface ();
9473 c_parser_objc_methodprotolist (parser);
9474 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9475 objc_finish_interface ();
9477 else
9479 objc_continue_implementation ();
9480 return;
9484 /* Parse objc-class-instance-variables.
9486 objc-class-instance-variables:
9487 { objc-instance-variable-decl-list[opt] }
9489 objc-instance-variable-decl-list:
9490 objc-visibility-spec
9491 objc-instance-variable-decl ;
9493 objc-instance-variable-decl-list objc-visibility-spec
9494 objc-instance-variable-decl-list objc-instance-variable-decl ;
9495 objc-instance-variable-decl-list ;
9497 objc-visibility-spec:
9498 @private
9499 @protected
9500 @public
9502 objc-instance-variable-decl:
9503 struct-declaration
9506 static void
9507 c_parser_objc_class_instance_variables (c_parser *parser)
9509 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9510 c_parser_consume_token (parser);
9511 while (c_parser_next_token_is_not (parser, CPP_EOF))
9513 tree decls;
9514 /* Parse any stray semicolon. */
9515 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9517 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9518 "extra semicolon");
9519 c_parser_consume_token (parser);
9520 continue;
9522 /* Stop if at the end of the instance variables. */
9523 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9525 c_parser_consume_token (parser);
9526 break;
9528 /* Parse any objc-visibility-spec. */
9529 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9531 c_parser_consume_token (parser);
9532 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9533 continue;
9535 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9537 c_parser_consume_token (parser);
9538 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9539 continue;
9541 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9543 c_parser_consume_token (parser);
9544 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9545 continue;
9547 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9549 c_parser_consume_token (parser);
9550 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9551 continue;
9553 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9555 c_parser_pragma (parser, pragma_external, NULL);
9556 continue;
9559 /* Parse some comma-separated declarations. */
9560 decls = c_parser_struct_declaration (parser);
9561 if (decls == NULL)
9563 /* There is a syntax error. We want to skip the offending
9564 tokens up to the next ';' (included) or '}'
9565 (excluded). */
9567 /* First, skip manually a ')' or ']'. This is because they
9568 reduce the nesting level, so c_parser_skip_until_found()
9569 wouldn't be able to skip past them. */
9570 c_token *token = c_parser_peek_token (parser);
9571 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9572 c_parser_consume_token (parser);
9574 /* Then, do the standard skipping. */
9575 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9577 /* We hopefully recovered. Start normal parsing again. */
9578 parser->error = false;
9579 continue;
9581 else
9583 /* Comma-separated instance variables are chained together
9584 in reverse order; add them one by one. */
9585 tree ivar = nreverse (decls);
9586 for (; ivar; ivar = DECL_CHAIN (ivar))
9587 objc_add_instance_variable (copy_node (ivar));
9589 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9593 /* Parse an objc-class-declaration.
9595 objc-class-declaration:
9596 @class identifier-list ;
9599 static void
9600 c_parser_objc_class_declaration (c_parser *parser)
9602 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9603 c_parser_consume_token (parser);
9604 /* Any identifiers, including those declared as type names, are OK
9605 here. */
9606 while (true)
9608 tree id;
9609 if (c_parser_next_token_is_not (parser, CPP_NAME))
9611 c_parser_error (parser, "expected identifier");
9612 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9613 parser->error = false;
9614 return;
9616 id = c_parser_peek_token (parser)->value;
9617 objc_declare_class (id);
9618 c_parser_consume_token (parser);
9619 if (c_parser_next_token_is (parser, CPP_COMMA))
9620 c_parser_consume_token (parser);
9621 else
9622 break;
9624 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9627 /* Parse an objc-alias-declaration.
9629 objc-alias-declaration:
9630 @compatibility_alias identifier identifier ;
9633 static void
9634 c_parser_objc_alias_declaration (c_parser *parser)
9636 tree id1, id2;
9637 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9638 c_parser_consume_token (parser);
9639 if (c_parser_next_token_is_not (parser, CPP_NAME))
9641 c_parser_error (parser, "expected identifier");
9642 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9643 return;
9645 id1 = c_parser_peek_token (parser)->value;
9646 c_parser_consume_token (parser);
9647 if (c_parser_next_token_is_not (parser, CPP_NAME))
9649 c_parser_error (parser, "expected identifier");
9650 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9651 return;
9653 id2 = c_parser_peek_token (parser)->value;
9654 c_parser_consume_token (parser);
9655 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9656 objc_declare_alias (id1, id2);
9659 /* Parse an objc-protocol-definition.
9661 objc-protocol-definition:
9662 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9663 @protocol identifier-list ;
9665 "@protocol identifier ;" should be resolved as "@protocol
9666 identifier-list ;": objc-methodprotolist may not start with a
9667 semicolon in the first alternative if objc-protocol-refs are
9668 omitted. */
9670 static void
9671 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9673 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9675 c_parser_consume_token (parser);
9676 if (c_parser_next_token_is_not (parser, CPP_NAME))
9678 c_parser_error (parser, "expected identifier");
9679 return;
9681 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9682 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9684 /* Any identifiers, including those declared as type names, are
9685 OK here. */
9686 while (true)
9688 tree id;
9689 if (c_parser_next_token_is_not (parser, CPP_NAME))
9691 c_parser_error (parser, "expected identifier");
9692 break;
9694 id = c_parser_peek_token (parser)->value;
9695 objc_declare_protocol (id, attributes);
9696 c_parser_consume_token (parser);
9697 if (c_parser_next_token_is (parser, CPP_COMMA))
9698 c_parser_consume_token (parser);
9699 else
9700 break;
9702 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9704 else
9706 tree id = c_parser_peek_token (parser)->value;
9707 tree proto = NULL_TREE;
9708 c_parser_consume_token (parser);
9709 if (c_parser_next_token_is (parser, CPP_LESS))
9710 proto = c_parser_objc_protocol_refs (parser);
9711 parser->objc_pq_context = true;
9712 objc_start_protocol (id, proto, attributes);
9713 c_parser_objc_methodprotolist (parser);
9714 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9715 parser->objc_pq_context = false;
9716 objc_finish_interface ();
9720 /* Parse an objc-method-type.
9722 objc-method-type:
9726 Return true if it is a class method (+) and false if it is
9727 an instance method (-).
9729 static inline bool
9730 c_parser_objc_method_type (c_parser *parser)
9732 switch (c_parser_peek_token (parser)->type)
9734 case CPP_PLUS:
9735 c_parser_consume_token (parser);
9736 return true;
9737 case CPP_MINUS:
9738 c_parser_consume_token (parser);
9739 return false;
9740 default:
9741 gcc_unreachable ();
9745 /* Parse an objc-method-definition.
9747 objc-method-definition:
9748 objc-method-type objc-method-decl ;[opt] compound-statement
9751 static void
9752 c_parser_objc_method_definition (c_parser *parser)
9754 bool is_class_method = c_parser_objc_method_type (parser);
9755 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9756 parser->objc_pq_context = true;
9757 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9758 &expr);
9759 if (decl == error_mark_node)
9760 return; /* Bail here. */
9762 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9764 c_parser_consume_token (parser);
9765 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9766 "extra semicolon in method definition specified");
9769 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9771 c_parser_error (parser, "expected %<{%>");
9772 return;
9775 parser->objc_pq_context = false;
9776 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9778 add_stmt (c_parser_compound_statement (parser));
9779 objc_finish_method_definition (current_function_decl);
9781 else
9783 /* This code is executed when we find a method definition
9784 outside of an @implementation context (or invalid for other
9785 reasons). Parse the method (to keep going) but do not emit
9786 any code.
9788 c_parser_compound_statement (parser);
9792 /* Parse an objc-methodprotolist.
9794 objc-methodprotolist:
9795 empty
9796 objc-methodprotolist objc-methodproto
9797 objc-methodprotolist declaration
9798 objc-methodprotolist ;
9799 @optional
9800 @required
9802 The declaration is a data definition, which may be missing
9803 declaration specifiers under the same rules and diagnostics as
9804 other data definitions outside functions, and the stray semicolon
9805 is diagnosed the same way as a stray semicolon outside a
9806 function. */
9808 static void
9809 c_parser_objc_methodprotolist (c_parser *parser)
9811 while (true)
9813 /* The list is terminated by @end. */
9814 switch (c_parser_peek_token (parser)->type)
9816 case CPP_SEMICOLON:
9817 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9818 "ISO C does not allow extra %<;%> outside of a function");
9819 c_parser_consume_token (parser);
9820 break;
9821 case CPP_PLUS:
9822 case CPP_MINUS:
9823 c_parser_objc_methodproto (parser);
9824 break;
9825 case CPP_PRAGMA:
9826 c_parser_pragma (parser, pragma_external, NULL);
9827 break;
9828 case CPP_EOF:
9829 return;
9830 default:
9831 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9832 return;
9833 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9834 c_parser_objc_at_property_declaration (parser);
9835 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9837 objc_set_method_opt (true);
9838 c_parser_consume_token (parser);
9840 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9842 objc_set_method_opt (false);
9843 c_parser_consume_token (parser);
9845 else
9846 c_parser_declaration_or_fndef (parser, false, false, true,
9847 false, true, NULL, vNULL);
9848 break;
9853 /* Parse an objc-methodproto.
9855 objc-methodproto:
9856 objc-method-type objc-method-decl ;
9859 static void
9860 c_parser_objc_methodproto (c_parser *parser)
9862 bool is_class_method = c_parser_objc_method_type (parser);
9863 tree decl, attributes = NULL_TREE;
9865 /* Remember protocol qualifiers in prototypes. */
9866 parser->objc_pq_context = true;
9867 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9868 NULL);
9869 /* Forget protocol qualifiers now. */
9870 parser->objc_pq_context = false;
9872 /* Do not allow the presence of attributes to hide an erroneous
9873 method implementation in the interface section. */
9874 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9876 c_parser_error (parser, "expected %<;%>");
9877 return;
9880 if (decl != error_mark_node)
9881 objc_add_method_declaration (is_class_method, decl, attributes);
9883 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9886 /* If we are at a position that method attributes may be present, check that
9887 there are not any parsed already (a syntax error) and then collect any
9888 specified at the current location. Finally, if new attributes were present,
9889 check that the next token is legal ( ';' for decls and '{' for defs). */
9891 static bool
9892 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9894 bool bad = false;
9895 if (*attributes)
9897 c_parser_error (parser,
9898 "method attributes must be specified at the end only");
9899 *attributes = NULL_TREE;
9900 bad = true;
9903 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9904 *attributes = c_parser_attributes (parser);
9906 /* If there were no attributes here, just report any earlier error. */
9907 if (*attributes == NULL_TREE || bad)
9908 return bad;
9910 /* If the attributes are followed by a ; or {, then just report any earlier
9911 error. */
9912 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9913 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9914 return bad;
9916 /* We've got attributes, but not at the end. */
9917 c_parser_error (parser,
9918 "expected %<;%> or %<{%> after method attribute definition");
9919 return true;
9922 /* Parse an objc-method-decl.
9924 objc-method-decl:
9925 ( objc-type-name ) objc-selector
9926 objc-selector
9927 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9928 objc-keyword-selector objc-optparmlist
9929 attributes
9931 objc-keyword-selector:
9932 objc-keyword-decl
9933 objc-keyword-selector objc-keyword-decl
9935 objc-keyword-decl:
9936 objc-selector : ( objc-type-name ) identifier
9937 objc-selector : identifier
9938 : ( objc-type-name ) identifier
9939 : identifier
9941 objc-optparmlist:
9942 objc-optparms objc-optellipsis
9944 objc-optparms:
9945 empty
9946 objc-opt-parms , parameter-declaration
9948 objc-optellipsis:
9949 empty
9950 , ...
9953 static tree
9954 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9955 tree *attributes, tree *expr)
9957 tree type = NULL_TREE;
9958 tree sel;
9959 tree parms = NULL_TREE;
9960 bool ellipsis = false;
9961 bool attr_err = false;
9963 *attributes = NULL_TREE;
9964 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9966 matching_parens parens;
9967 parens.consume_open (parser);
9968 type = c_parser_objc_type_name (parser);
9969 parens.skip_until_found_close (parser);
9971 sel = c_parser_objc_selector (parser);
9972 /* If there is no selector, or a colon follows, we have an
9973 objc-keyword-selector. If there is a selector, and a colon does
9974 not follow, that selector ends the objc-method-decl. */
9975 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9977 tree tsel = sel;
9978 tree list = NULL_TREE;
9979 while (true)
9981 tree atype = NULL_TREE, id, keyworddecl;
9982 tree param_attr = NULL_TREE;
9983 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9984 break;
9985 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9987 c_parser_consume_token (parser);
9988 atype = c_parser_objc_type_name (parser);
9989 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9990 "expected %<)%>");
9992 /* New ObjC allows attributes on method parameters. */
9993 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9994 param_attr = c_parser_attributes (parser);
9995 if (c_parser_next_token_is_not (parser, CPP_NAME))
9997 c_parser_error (parser, "expected identifier");
9998 return error_mark_node;
10000 id = c_parser_peek_token (parser)->value;
10001 c_parser_consume_token (parser);
10002 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10003 list = chainon (list, keyworddecl);
10004 tsel = c_parser_objc_selector (parser);
10005 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10006 break;
10009 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10011 /* Parse the optional parameter list. Optional Objective-C
10012 method parameters follow the C syntax, and may include '...'
10013 to denote a variable number of arguments. */
10014 parms = make_node (TREE_LIST);
10015 while (c_parser_next_token_is (parser, CPP_COMMA))
10017 struct c_parm *parm;
10018 c_parser_consume_token (parser);
10019 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10021 ellipsis = true;
10022 c_parser_consume_token (parser);
10023 attr_err |= c_parser_objc_maybe_method_attributes
10024 (parser, attributes) ;
10025 break;
10027 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10028 if (parm == NULL)
10029 break;
10030 parms = chainon (parms,
10031 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10033 sel = list;
10035 else
10036 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10038 if (sel == NULL)
10040 c_parser_error (parser, "objective-c method declaration is expected");
10041 return error_mark_node;
10044 if (attr_err)
10045 return error_mark_node;
10047 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10050 /* Parse an objc-type-name.
10052 objc-type-name:
10053 objc-type-qualifiers[opt] type-name
10054 objc-type-qualifiers[opt]
10056 objc-type-qualifiers:
10057 objc-type-qualifier
10058 objc-type-qualifiers objc-type-qualifier
10060 objc-type-qualifier: one of
10061 in out inout bycopy byref oneway
10064 static tree
10065 c_parser_objc_type_name (c_parser *parser)
10067 tree quals = NULL_TREE;
10068 struct c_type_name *type_name = NULL;
10069 tree type = NULL_TREE;
10070 while (true)
10072 c_token *token = c_parser_peek_token (parser);
10073 if (token->type == CPP_KEYWORD
10074 && (token->keyword == RID_IN
10075 || token->keyword == RID_OUT
10076 || token->keyword == RID_INOUT
10077 || token->keyword == RID_BYCOPY
10078 || token->keyword == RID_BYREF
10079 || token->keyword == RID_ONEWAY))
10081 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10082 c_parser_consume_token (parser);
10084 else
10085 break;
10087 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10088 type_name = c_parser_type_name (parser);
10089 if (type_name)
10090 type = groktypename (type_name, NULL, NULL);
10092 /* If the type is unknown, and error has already been produced and
10093 we need to recover from the error. In that case, use NULL_TREE
10094 for the type, as if no type had been specified; this will use the
10095 default type ('id') which is good for error recovery. */
10096 if (type == error_mark_node)
10097 type = NULL_TREE;
10099 return build_tree_list (quals, type);
10102 /* Parse objc-protocol-refs.
10104 objc-protocol-refs:
10105 < identifier-list >
10108 static tree
10109 c_parser_objc_protocol_refs (c_parser *parser)
10111 tree list = NULL_TREE;
10112 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10113 c_parser_consume_token (parser);
10114 /* Any identifiers, including those declared as type names, are OK
10115 here. */
10116 while (true)
10118 tree id;
10119 if (c_parser_next_token_is_not (parser, CPP_NAME))
10121 c_parser_error (parser, "expected identifier");
10122 break;
10124 id = c_parser_peek_token (parser)->value;
10125 list = chainon (list, build_tree_list (NULL_TREE, id));
10126 c_parser_consume_token (parser);
10127 if (c_parser_next_token_is (parser, CPP_COMMA))
10128 c_parser_consume_token (parser);
10129 else
10130 break;
10132 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10133 return list;
10136 /* Parse an objc-try-catch-finally-statement.
10138 objc-try-catch-finally-statement:
10139 @try compound-statement objc-catch-list[opt]
10140 @try compound-statement objc-catch-list[opt] @finally compound-statement
10142 objc-catch-list:
10143 @catch ( objc-catch-parameter-declaration ) compound-statement
10144 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10146 objc-catch-parameter-declaration:
10147 parameter-declaration
10148 '...'
10150 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10152 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10153 for C++. Keep them in sync. */
10155 static void
10156 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10158 location_t location;
10159 tree stmt;
10161 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10162 c_parser_consume_token (parser);
10163 location = c_parser_peek_token (parser)->location;
10164 objc_maybe_warn_exceptions (location);
10165 stmt = c_parser_compound_statement (parser);
10166 objc_begin_try_stmt (location, stmt);
10168 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10170 struct c_parm *parm;
10171 tree parameter_declaration = error_mark_node;
10172 bool seen_open_paren = false;
10174 c_parser_consume_token (parser);
10175 matching_parens parens;
10176 if (!parens.require_open (parser))
10177 seen_open_paren = true;
10178 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10180 /* We have "@catch (...)" (where the '...' are literally
10181 what is in the code). Skip the '...'.
10182 parameter_declaration is set to NULL_TREE, and
10183 objc_being_catch_clauses() knows that that means
10184 '...'. */
10185 c_parser_consume_token (parser);
10186 parameter_declaration = NULL_TREE;
10188 else
10190 /* We have "@catch (NSException *exception)" or something
10191 like that. Parse the parameter declaration. */
10192 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10193 if (parm == NULL)
10194 parameter_declaration = error_mark_node;
10195 else
10196 parameter_declaration = grokparm (parm, NULL);
10198 if (seen_open_paren)
10199 parens.require_close (parser);
10200 else
10202 /* If there was no open parenthesis, we are recovering from
10203 an error, and we are trying to figure out what mistake
10204 the user has made. */
10206 /* If there is an immediate closing parenthesis, the user
10207 probably forgot the opening one (ie, they typed "@catch
10208 NSException *e)". Parse the closing parenthesis and keep
10209 going. */
10210 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10211 c_parser_consume_token (parser);
10213 /* If these is no immediate closing parenthesis, the user
10214 probably doesn't know that parenthesis are required at
10215 all (ie, they typed "@catch NSException *e"). So, just
10216 forget about the closing parenthesis and keep going. */
10218 objc_begin_catch_clause (parameter_declaration);
10219 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10220 c_parser_compound_statement_nostart (parser);
10221 objc_finish_catch_clause ();
10223 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10225 c_parser_consume_token (parser);
10226 location = c_parser_peek_token (parser)->location;
10227 stmt = c_parser_compound_statement (parser);
10228 objc_build_finally_clause (location, stmt);
10230 objc_finish_try_stmt ();
10233 /* Parse an objc-synchronized-statement.
10235 objc-synchronized-statement:
10236 @synchronized ( expression ) compound-statement
10239 static void
10240 c_parser_objc_synchronized_statement (c_parser *parser)
10242 location_t loc;
10243 tree expr, stmt;
10244 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10245 c_parser_consume_token (parser);
10246 loc = c_parser_peek_token (parser)->location;
10247 objc_maybe_warn_exceptions (loc);
10248 matching_parens parens;
10249 if (parens.require_open (parser))
10251 struct c_expr ce = c_parser_expression (parser);
10252 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10253 expr = ce.value;
10254 expr = c_fully_fold (expr, false, NULL);
10255 parens.skip_until_found_close (parser);
10257 else
10258 expr = error_mark_node;
10259 stmt = c_parser_compound_statement (parser);
10260 objc_build_synchronized (loc, expr, stmt);
10263 /* Parse an objc-selector; return NULL_TREE without an error if the
10264 next token is not an objc-selector.
10266 objc-selector:
10267 identifier
10268 one of
10269 enum struct union if else while do for switch case default
10270 break continue return goto asm sizeof typeof __alignof
10271 unsigned long const short volatile signed restrict _Complex
10272 in out inout bycopy byref oneway int char float double void _Bool
10273 _Atomic
10275 ??? Why this selection of keywords but not, for example, storage
10276 class specifiers? */
10278 static tree
10279 c_parser_objc_selector (c_parser *parser)
10281 c_token *token = c_parser_peek_token (parser);
10282 tree value = token->value;
10283 if (token->type == CPP_NAME)
10285 c_parser_consume_token (parser);
10286 return value;
10288 if (token->type != CPP_KEYWORD)
10289 return NULL_TREE;
10290 switch (token->keyword)
10292 case RID_ENUM:
10293 case RID_STRUCT:
10294 case RID_UNION:
10295 case RID_IF:
10296 case RID_ELSE:
10297 case RID_WHILE:
10298 case RID_DO:
10299 case RID_FOR:
10300 case RID_SWITCH:
10301 case RID_CASE:
10302 case RID_DEFAULT:
10303 case RID_BREAK:
10304 case RID_CONTINUE:
10305 case RID_RETURN:
10306 case RID_GOTO:
10307 case RID_ASM:
10308 case RID_SIZEOF:
10309 case RID_TYPEOF:
10310 case RID_ALIGNOF:
10311 case RID_UNSIGNED:
10312 case RID_LONG:
10313 case RID_CONST:
10314 case RID_SHORT:
10315 case RID_VOLATILE:
10316 case RID_SIGNED:
10317 case RID_RESTRICT:
10318 case RID_COMPLEX:
10319 case RID_IN:
10320 case RID_OUT:
10321 case RID_INOUT:
10322 case RID_BYCOPY:
10323 case RID_BYREF:
10324 case RID_ONEWAY:
10325 case RID_INT:
10326 case RID_CHAR:
10327 case RID_FLOAT:
10328 case RID_DOUBLE:
10329 CASE_RID_FLOATN_NX:
10330 case RID_VOID:
10331 case RID_BOOL:
10332 case RID_ATOMIC:
10333 case RID_AUTO_TYPE:
10334 case RID_INT_N_0:
10335 case RID_INT_N_1:
10336 case RID_INT_N_2:
10337 case RID_INT_N_3:
10338 c_parser_consume_token (parser);
10339 return value;
10340 default:
10341 return NULL_TREE;
10345 /* Parse an objc-selector-arg.
10347 objc-selector-arg:
10348 objc-selector
10349 objc-keywordname-list
10351 objc-keywordname-list:
10352 objc-keywordname
10353 objc-keywordname-list objc-keywordname
10355 objc-keywordname:
10356 objc-selector :
10360 static tree
10361 c_parser_objc_selector_arg (c_parser *parser)
10363 tree sel = c_parser_objc_selector (parser);
10364 tree list = NULL_TREE;
10365 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10366 return sel;
10367 while (true)
10369 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10370 return list;
10371 list = chainon (list, build_tree_list (sel, NULL_TREE));
10372 sel = c_parser_objc_selector (parser);
10373 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10374 break;
10376 return list;
10379 /* Parse an objc-receiver.
10381 objc-receiver:
10382 expression
10383 class-name
10384 type-name
10387 static tree
10388 c_parser_objc_receiver (c_parser *parser)
10390 location_t loc = c_parser_peek_token (parser)->location;
10392 if (c_parser_peek_token (parser)->type == CPP_NAME
10393 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10394 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10396 tree id = c_parser_peek_token (parser)->value;
10397 c_parser_consume_token (parser);
10398 return objc_get_class_reference (id);
10400 struct c_expr ce = c_parser_expression (parser);
10401 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10402 return c_fully_fold (ce.value, false, NULL);
10405 /* Parse objc-message-args.
10407 objc-message-args:
10408 objc-selector
10409 objc-keywordarg-list
10411 objc-keywordarg-list:
10412 objc-keywordarg
10413 objc-keywordarg-list objc-keywordarg
10415 objc-keywordarg:
10416 objc-selector : objc-keywordexpr
10417 : objc-keywordexpr
10420 static tree
10421 c_parser_objc_message_args (c_parser *parser)
10423 tree sel = c_parser_objc_selector (parser);
10424 tree list = NULL_TREE;
10425 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10426 return sel;
10427 while (true)
10429 tree keywordexpr;
10430 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10431 return error_mark_node;
10432 keywordexpr = c_parser_objc_keywordexpr (parser);
10433 list = chainon (list, build_tree_list (sel, keywordexpr));
10434 sel = c_parser_objc_selector (parser);
10435 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10436 break;
10438 return list;
10441 /* Parse an objc-keywordexpr.
10443 objc-keywordexpr:
10444 nonempty-expr-list
10447 static tree
10448 c_parser_objc_keywordexpr (c_parser *parser)
10450 tree ret;
10451 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10452 NULL, NULL, NULL, NULL);
10453 if (vec_safe_length (expr_list) == 1)
10455 /* Just return the expression, remove a level of
10456 indirection. */
10457 ret = (*expr_list)[0];
10459 else
10461 /* We have a comma expression, we will collapse later. */
10462 ret = build_tree_list_vec (expr_list);
10464 release_tree_vector (expr_list);
10465 return ret;
10468 /* A check, needed in several places, that ObjC interface, implementation or
10469 method definitions are not prefixed by incorrect items. */
10470 static bool
10471 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10472 struct c_declspecs *specs)
10474 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10475 || specs->typespec_kind != ctsk_none)
10477 c_parser_error (parser,
10478 "no type or storage class may be specified here,");
10479 c_parser_skip_to_end_of_block_or_statement (parser);
10480 return true;
10482 return false;
10485 /* Parse an Objective-C @property declaration. The syntax is:
10487 objc-property-declaration:
10488 '@property' objc-property-attributes[opt] struct-declaration ;
10490 objc-property-attributes:
10491 '(' objc-property-attribute-list ')'
10493 objc-property-attribute-list:
10494 objc-property-attribute
10495 objc-property-attribute-list, objc-property-attribute
10497 objc-property-attribute
10498 'getter' = identifier
10499 'setter' = identifier
10500 'readonly'
10501 'readwrite'
10502 'assign'
10503 'retain'
10504 'copy'
10505 'nonatomic'
10507 For example:
10508 @property NSString *name;
10509 @property (readonly) id object;
10510 @property (retain, nonatomic, getter=getTheName) id name;
10511 @property int a, b, c;
10513 PS: This function is identical to cp_parser_objc_at_propery_declaration
10514 for C++. Keep them in sync. */
10515 static void
10516 c_parser_objc_at_property_declaration (c_parser *parser)
10518 /* The following variables hold the attributes of the properties as
10519 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10520 seen. When we see an attribute, we set them to 'true' (if they
10521 are boolean properties) or to the identifier (if they have an
10522 argument, ie, for getter and setter). Note that here we only
10523 parse the list of attributes, check the syntax and accumulate the
10524 attributes that we find. objc_add_property_declaration() will
10525 then process the information. */
10526 bool property_assign = false;
10527 bool property_copy = false;
10528 tree property_getter_ident = NULL_TREE;
10529 bool property_nonatomic = false;
10530 bool property_readonly = false;
10531 bool property_readwrite = false;
10532 bool property_retain = false;
10533 tree property_setter_ident = NULL_TREE;
10535 /* 'properties' is the list of properties that we read. Usually a
10536 single one, but maybe more (eg, in "@property int a, b, c;" there
10537 are three). */
10538 tree properties;
10539 location_t loc;
10541 loc = c_parser_peek_token (parser)->location;
10542 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10544 c_parser_consume_token (parser); /* Eat '@property'. */
10546 /* Parse the optional attribute list... */
10547 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10549 matching_parens parens;
10551 /* Eat the '(' */
10552 parens.consume_open (parser);
10554 /* Property attribute keywords are valid now. */
10555 parser->objc_property_attr_context = true;
10557 while (true)
10559 bool syntax_error = false;
10560 c_token *token = c_parser_peek_token (parser);
10561 enum rid keyword;
10563 if (token->type != CPP_KEYWORD)
10565 if (token->type == CPP_CLOSE_PAREN)
10566 c_parser_error (parser, "expected identifier");
10567 else
10569 c_parser_consume_token (parser);
10570 c_parser_error (parser, "unknown property attribute");
10572 break;
10574 keyword = token->keyword;
10575 c_parser_consume_token (parser);
10576 switch (keyword)
10578 case RID_ASSIGN: property_assign = true; break;
10579 case RID_COPY: property_copy = true; break;
10580 case RID_NONATOMIC: property_nonatomic = true; break;
10581 case RID_READONLY: property_readonly = true; break;
10582 case RID_READWRITE: property_readwrite = true; break;
10583 case RID_RETAIN: property_retain = true; break;
10585 case RID_GETTER:
10586 case RID_SETTER:
10587 if (c_parser_next_token_is_not (parser, CPP_EQ))
10589 if (keyword == RID_GETTER)
10590 c_parser_error (parser,
10591 "missing %<=%> (after %<getter%> attribute)");
10592 else
10593 c_parser_error (parser,
10594 "missing %<=%> (after %<setter%> attribute)");
10595 syntax_error = true;
10596 break;
10598 c_parser_consume_token (parser); /* eat the = */
10599 if (c_parser_next_token_is_not (parser, CPP_NAME))
10601 c_parser_error (parser, "expected identifier");
10602 syntax_error = true;
10603 break;
10605 if (keyword == RID_SETTER)
10607 if (property_setter_ident != NULL_TREE)
10608 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10609 else
10610 property_setter_ident = c_parser_peek_token (parser)->value;
10611 c_parser_consume_token (parser);
10612 if (c_parser_next_token_is_not (parser, CPP_COLON))
10613 c_parser_error (parser, "setter name must terminate with %<:%>");
10614 else
10615 c_parser_consume_token (parser);
10617 else
10619 if (property_getter_ident != NULL_TREE)
10620 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10621 else
10622 property_getter_ident = c_parser_peek_token (parser)->value;
10623 c_parser_consume_token (parser);
10625 break;
10626 default:
10627 c_parser_error (parser, "unknown property attribute");
10628 syntax_error = true;
10629 break;
10632 if (syntax_error)
10633 break;
10635 if (c_parser_next_token_is (parser, CPP_COMMA))
10636 c_parser_consume_token (parser);
10637 else
10638 break;
10640 parser->objc_property_attr_context = false;
10641 parens.skip_until_found_close (parser);
10643 /* ... and the property declaration(s). */
10644 properties = c_parser_struct_declaration (parser);
10646 if (properties == error_mark_node)
10648 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10649 parser->error = false;
10650 return;
10653 if (properties == NULL_TREE)
10654 c_parser_error (parser, "expected identifier");
10655 else
10657 /* Comma-separated properties are chained together in
10658 reverse order; add them one by one. */
10659 properties = nreverse (properties);
10661 for (; properties; properties = TREE_CHAIN (properties))
10662 objc_add_property_declaration (loc, copy_node (properties),
10663 property_readonly, property_readwrite,
10664 property_assign, property_retain,
10665 property_copy, property_nonatomic,
10666 property_getter_ident, property_setter_ident);
10669 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10670 parser->error = false;
10673 /* Parse an Objective-C @synthesize declaration. The syntax is:
10675 objc-synthesize-declaration:
10676 @synthesize objc-synthesize-identifier-list ;
10678 objc-synthesize-identifier-list:
10679 objc-synthesize-identifier
10680 objc-synthesize-identifier-list, objc-synthesize-identifier
10682 objc-synthesize-identifier
10683 identifier
10684 identifier = identifier
10686 For example:
10687 @synthesize MyProperty;
10688 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10690 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10691 for C++. Keep them in sync.
10693 static void
10694 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10696 tree list = NULL_TREE;
10697 location_t loc;
10698 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10699 loc = c_parser_peek_token (parser)->location;
10701 c_parser_consume_token (parser);
10702 while (true)
10704 tree property, ivar;
10705 if (c_parser_next_token_is_not (parser, CPP_NAME))
10707 c_parser_error (parser, "expected identifier");
10708 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10709 /* Once we find the semicolon, we can resume normal parsing.
10710 We have to reset parser->error manually because
10711 c_parser_skip_until_found() won't reset it for us if the
10712 next token is precisely a semicolon. */
10713 parser->error = false;
10714 return;
10716 property = c_parser_peek_token (parser)->value;
10717 c_parser_consume_token (parser);
10718 if (c_parser_next_token_is (parser, CPP_EQ))
10720 c_parser_consume_token (parser);
10721 if (c_parser_next_token_is_not (parser, CPP_NAME))
10723 c_parser_error (parser, "expected identifier");
10724 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10725 parser->error = false;
10726 return;
10728 ivar = c_parser_peek_token (parser)->value;
10729 c_parser_consume_token (parser);
10731 else
10732 ivar = NULL_TREE;
10733 list = chainon (list, build_tree_list (ivar, property));
10734 if (c_parser_next_token_is (parser, CPP_COMMA))
10735 c_parser_consume_token (parser);
10736 else
10737 break;
10739 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10740 objc_add_synthesize_declaration (loc, list);
10743 /* Parse an Objective-C @dynamic declaration. The syntax is:
10745 objc-dynamic-declaration:
10746 @dynamic identifier-list ;
10748 For example:
10749 @dynamic MyProperty;
10750 @dynamic MyProperty, AnotherProperty;
10752 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10753 for C++. Keep them in sync.
10755 static void
10756 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10758 tree list = NULL_TREE;
10759 location_t loc;
10760 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10761 loc = c_parser_peek_token (parser)->location;
10763 c_parser_consume_token (parser);
10764 while (true)
10766 tree property;
10767 if (c_parser_next_token_is_not (parser, CPP_NAME))
10769 c_parser_error (parser, "expected identifier");
10770 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10771 parser->error = false;
10772 return;
10774 property = c_parser_peek_token (parser)->value;
10775 list = chainon (list, build_tree_list (NULL_TREE, property));
10776 c_parser_consume_token (parser);
10777 if (c_parser_next_token_is (parser, CPP_COMMA))
10778 c_parser_consume_token (parser);
10779 else
10780 break;
10782 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10783 objc_add_dynamic_declaration (loc, list);
10787 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10788 should be considered, statements. ALLOW_STMT is true if we're within
10789 the context of a function and such pragmas are to be allowed. Returns
10790 true if we actually parsed such a pragma. */
10792 static bool
10793 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10795 unsigned int id;
10796 const char *construct = NULL;
10798 id = c_parser_peek_token (parser)->pragma_kind;
10799 gcc_assert (id != PRAGMA_NONE);
10801 switch (id)
10803 case PRAGMA_OACC_DECLARE:
10804 c_parser_oacc_declare (parser);
10805 return false;
10807 case PRAGMA_OACC_ENTER_DATA:
10808 if (context != pragma_compound)
10810 construct = "acc enter data";
10811 in_compound:
10812 if (context == pragma_stmt)
10814 error_at (c_parser_peek_token (parser)->location,
10815 "%<#pragma %s%> may only be used in compound "
10816 "statements", construct);
10817 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10818 return false;
10820 goto bad_stmt;
10822 c_parser_oacc_enter_exit_data (parser, true);
10823 return false;
10825 case PRAGMA_OACC_EXIT_DATA:
10826 if (context != pragma_compound)
10828 construct = "acc exit data";
10829 goto in_compound;
10831 c_parser_oacc_enter_exit_data (parser, false);
10832 return false;
10834 case PRAGMA_OACC_ROUTINE:
10835 if (context != pragma_external)
10837 error_at (c_parser_peek_token (parser)->location,
10838 "%<#pragma acc routine%> must be at file scope");
10839 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10840 return false;
10842 c_parser_oacc_routine (parser, context);
10843 return false;
10845 case PRAGMA_OACC_UPDATE:
10846 if (context != pragma_compound)
10848 construct = "acc update";
10849 goto in_compound;
10851 c_parser_oacc_update (parser);
10852 return false;
10854 case PRAGMA_OMP_BARRIER:
10855 if (context != pragma_compound)
10857 construct = "omp barrier";
10858 goto in_compound;
10860 c_parser_omp_barrier (parser);
10861 return false;
10863 case PRAGMA_OMP_FLUSH:
10864 if (context != pragma_compound)
10866 construct = "omp flush";
10867 goto in_compound;
10869 c_parser_omp_flush (parser);
10870 return false;
10872 case PRAGMA_OMP_TASKWAIT:
10873 if (context != pragma_compound)
10875 construct = "omp taskwait";
10876 goto in_compound;
10878 c_parser_omp_taskwait (parser);
10879 return false;
10881 case PRAGMA_OMP_TASKYIELD:
10882 if (context != pragma_compound)
10884 construct = "omp taskyield";
10885 goto in_compound;
10887 c_parser_omp_taskyield (parser);
10888 return false;
10890 case PRAGMA_OMP_CANCEL:
10891 if (context != pragma_compound)
10893 construct = "omp cancel";
10894 goto in_compound;
10896 c_parser_omp_cancel (parser);
10897 return false;
10899 case PRAGMA_OMP_CANCELLATION_POINT:
10900 c_parser_omp_cancellation_point (parser, context);
10901 return false;
10903 case PRAGMA_OMP_THREADPRIVATE:
10904 c_parser_omp_threadprivate (parser);
10905 return false;
10907 case PRAGMA_OMP_TARGET:
10908 return c_parser_omp_target (parser, context, if_p);
10910 case PRAGMA_OMP_END_DECLARE_TARGET:
10911 c_parser_omp_end_declare_target (parser);
10912 return false;
10914 case PRAGMA_OMP_SECTION:
10915 error_at (c_parser_peek_token (parser)->location,
10916 "%<#pragma omp section%> may only be used in "
10917 "%<#pragma omp sections%> construct");
10918 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10919 return false;
10921 case PRAGMA_OMP_DECLARE:
10922 c_parser_omp_declare (parser, context);
10923 return false;
10925 case PRAGMA_OMP_ORDERED:
10926 return c_parser_omp_ordered (parser, context, if_p);
10928 case PRAGMA_IVDEP:
10929 c_parser_consume_pragma (parser);
10930 c_parser_skip_to_pragma_eol (parser);
10931 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10932 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10933 && !c_parser_next_token_is_keyword (parser, RID_DO))
10935 c_parser_error (parser, "for, while or do statement expected");
10936 return false;
10938 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10939 c_parser_for_statement (parser, true, if_p);
10940 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10941 c_parser_while_statement (parser, true, if_p);
10942 else
10943 c_parser_do_statement (parser, true);
10944 return false;
10946 case PRAGMA_GCC_PCH_PREPROCESS:
10947 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10948 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10949 return false;
10951 case PRAGMA_OACC_WAIT:
10952 if (context != pragma_compound)
10954 construct = "acc wait";
10955 goto in_compound;
10957 /* FALL THROUGH. */
10959 default:
10960 if (id < PRAGMA_FIRST_EXTERNAL)
10962 if (context != pragma_stmt && context != pragma_compound)
10964 bad_stmt:
10965 c_parser_error (parser, "expected declaration specifiers");
10966 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10967 return false;
10969 c_parser_omp_construct (parser, if_p);
10970 return true;
10972 break;
10975 c_parser_consume_pragma (parser);
10976 c_invoke_pragma_handler (id);
10978 /* Skip to EOL, but suppress any error message. Those will have been
10979 generated by the handler routine through calling error, as opposed
10980 to calling c_parser_error. */
10981 parser->error = true;
10982 c_parser_skip_to_pragma_eol (parser);
10984 return false;
10987 /* The interface the pragma parsers have to the lexer. */
10989 enum cpp_ttype
10990 pragma_lex (tree *value, location_t *loc)
10992 c_token *tok = c_parser_peek_token (the_parser);
10993 enum cpp_ttype ret = tok->type;
10995 *value = tok->value;
10996 if (loc)
10997 *loc = tok->location;
10999 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11000 ret = CPP_EOF;
11001 else
11003 if (ret == CPP_KEYWORD)
11004 ret = CPP_NAME;
11005 c_parser_consume_token (the_parser);
11008 return ret;
11011 static void
11012 c_parser_pragma_pch_preprocess (c_parser *parser)
11014 tree name = NULL;
11016 c_parser_consume_pragma (parser);
11017 if (c_parser_next_token_is (parser, CPP_STRING))
11019 name = c_parser_peek_token (parser)->value;
11020 c_parser_consume_token (parser);
11022 else
11023 c_parser_error (parser, "expected string literal");
11024 c_parser_skip_to_pragma_eol (parser);
11026 if (name)
11027 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11030 /* OpenACC and OpenMP parsing routines. */
11032 /* Returns name of the next clause.
11033 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11034 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11035 returned and the token is consumed. */
11037 static pragma_omp_clause
11038 c_parser_omp_clause_name (c_parser *parser)
11040 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11042 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11043 result = PRAGMA_OACC_CLAUSE_AUTO;
11044 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11045 result = PRAGMA_OMP_CLAUSE_IF;
11046 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11047 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11048 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11049 result = PRAGMA_OMP_CLAUSE_FOR;
11050 else if (c_parser_next_token_is (parser, CPP_NAME))
11052 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11054 switch (p[0])
11056 case 'a':
11057 if (!strcmp ("aligned", p))
11058 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11059 else if (!strcmp ("async", p))
11060 result = PRAGMA_OACC_CLAUSE_ASYNC;
11061 break;
11062 case 'c':
11063 if (!strcmp ("collapse", p))
11064 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11065 else if (!strcmp ("copy", p))
11066 result = PRAGMA_OACC_CLAUSE_COPY;
11067 else if (!strcmp ("copyin", p))
11068 result = PRAGMA_OMP_CLAUSE_COPYIN;
11069 else if (!strcmp ("copyout", p))
11070 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11071 else if (!strcmp ("copyprivate", p))
11072 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11073 else if (!strcmp ("create", p))
11074 result = PRAGMA_OACC_CLAUSE_CREATE;
11075 break;
11076 case 'd':
11077 if (!strcmp ("defaultmap", p))
11078 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11079 else if (!strcmp ("delete", p))
11080 result = PRAGMA_OACC_CLAUSE_DELETE;
11081 else if (!strcmp ("depend", p))
11082 result = PRAGMA_OMP_CLAUSE_DEPEND;
11083 else if (!strcmp ("device", p))
11084 result = PRAGMA_OMP_CLAUSE_DEVICE;
11085 else if (!strcmp ("deviceptr", p))
11086 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11087 else if (!strcmp ("device_resident", p))
11088 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11089 else if (!strcmp ("dist_schedule", p))
11090 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11091 break;
11092 case 'f':
11093 if (!strcmp ("final", p))
11094 result = PRAGMA_OMP_CLAUSE_FINAL;
11095 else if (!strcmp ("firstprivate", p))
11096 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11097 else if (!strcmp ("from", p))
11098 result = PRAGMA_OMP_CLAUSE_FROM;
11099 break;
11100 case 'g':
11101 if (!strcmp ("gang", p))
11102 result = PRAGMA_OACC_CLAUSE_GANG;
11103 else if (!strcmp ("grainsize", p))
11104 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11105 break;
11106 case 'h':
11107 if (!strcmp ("hint", p))
11108 result = PRAGMA_OMP_CLAUSE_HINT;
11109 else if (!strcmp ("host", p))
11110 result = PRAGMA_OACC_CLAUSE_HOST;
11111 break;
11112 case 'i':
11113 if (!strcmp ("inbranch", p))
11114 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11115 else if (!strcmp ("independent", p))
11116 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11117 else if (!strcmp ("is_device_ptr", p))
11118 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11119 break;
11120 case 'l':
11121 if (!strcmp ("lastprivate", p))
11122 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11123 else if (!strcmp ("linear", p))
11124 result = PRAGMA_OMP_CLAUSE_LINEAR;
11125 else if (!strcmp ("link", p))
11126 result = PRAGMA_OMP_CLAUSE_LINK;
11127 break;
11128 case 'm':
11129 if (!strcmp ("map", p))
11130 result = PRAGMA_OMP_CLAUSE_MAP;
11131 else if (!strcmp ("mergeable", p))
11132 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11133 break;
11134 case 'n':
11135 if (!strcmp ("nogroup", p))
11136 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11137 else if (!strcmp ("notinbranch", p))
11138 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11139 else if (!strcmp ("nowait", p))
11140 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11141 else if (!strcmp ("num_gangs", p))
11142 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11143 else if (!strcmp ("num_tasks", p))
11144 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11145 else if (!strcmp ("num_teams", p))
11146 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11147 else if (!strcmp ("num_threads", p))
11148 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11149 else if (!strcmp ("num_workers", p))
11150 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11151 break;
11152 case 'o':
11153 if (!strcmp ("ordered", p))
11154 result = PRAGMA_OMP_CLAUSE_ORDERED;
11155 break;
11156 case 'p':
11157 if (!strcmp ("parallel", p))
11158 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11159 else if (!strcmp ("present", p))
11160 result = PRAGMA_OACC_CLAUSE_PRESENT;
11161 else if (!strcmp ("present_or_copy", p)
11162 || !strcmp ("pcopy", p))
11163 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
11164 else if (!strcmp ("present_or_copyin", p)
11165 || !strcmp ("pcopyin", p))
11166 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
11167 else if (!strcmp ("present_or_copyout", p)
11168 || !strcmp ("pcopyout", p))
11169 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
11170 else if (!strcmp ("present_or_create", p)
11171 || !strcmp ("pcreate", p))
11172 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
11173 else if (!strcmp ("priority", p))
11174 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11175 else if (!strcmp ("private", p))
11176 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11177 else if (!strcmp ("proc_bind", p))
11178 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11179 break;
11180 case 'r':
11181 if (!strcmp ("reduction", p))
11182 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11183 break;
11184 case 's':
11185 if (!strcmp ("safelen", p))
11186 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11187 else if (!strcmp ("schedule", p))
11188 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11189 else if (!strcmp ("sections", p))
11190 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11191 else if (!strcmp ("seq", p))
11192 result = PRAGMA_OACC_CLAUSE_SEQ;
11193 else if (!strcmp ("shared", p))
11194 result = PRAGMA_OMP_CLAUSE_SHARED;
11195 else if (!strcmp ("simd", p))
11196 result = PRAGMA_OMP_CLAUSE_SIMD;
11197 else if (!strcmp ("simdlen", p))
11198 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11199 else if (!strcmp ("self", p))
11200 result = PRAGMA_OACC_CLAUSE_SELF;
11201 break;
11202 case 't':
11203 if (!strcmp ("taskgroup", p))
11204 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11205 else if (!strcmp ("thread_limit", p))
11206 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11207 else if (!strcmp ("threads", p))
11208 result = PRAGMA_OMP_CLAUSE_THREADS;
11209 else if (!strcmp ("tile", p))
11210 result = PRAGMA_OACC_CLAUSE_TILE;
11211 else if (!strcmp ("to", p))
11212 result = PRAGMA_OMP_CLAUSE_TO;
11213 break;
11214 case 'u':
11215 if (!strcmp ("uniform", p))
11216 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11217 else if (!strcmp ("untied", p))
11218 result = PRAGMA_OMP_CLAUSE_UNTIED;
11219 else if (!strcmp ("use_device", p))
11220 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11221 else if (!strcmp ("use_device_ptr", p))
11222 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11223 break;
11224 case 'v':
11225 if (!strcmp ("vector", p))
11226 result = PRAGMA_OACC_CLAUSE_VECTOR;
11227 else if (!strcmp ("vector_length", p))
11228 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11229 break;
11230 case 'w':
11231 if (!strcmp ("wait", p))
11232 result = PRAGMA_OACC_CLAUSE_WAIT;
11233 else if (!strcmp ("worker", p))
11234 result = PRAGMA_OACC_CLAUSE_WORKER;
11235 break;
11239 if (result != PRAGMA_OMP_CLAUSE_NONE)
11240 c_parser_consume_token (parser);
11242 return result;
11245 /* Validate that a clause of the given type does not already exist. */
11247 static void
11248 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11249 const char *name)
11251 tree c;
11253 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11254 if (OMP_CLAUSE_CODE (c) == code)
11256 location_t loc = OMP_CLAUSE_LOCATION (c);
11257 error_at (loc, "too many %qs clauses", name);
11258 break;
11262 /* OpenACC 2.0
11263 Parse wait clause or wait directive parameters. */
11265 static tree
11266 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11268 vec<tree, va_gc> *args;
11269 tree t, args_tree;
11271 matching_parens parens;
11272 if (!parens.require_open (parser))
11273 return list;
11275 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11277 if (args->length () == 0)
11279 c_parser_error (parser, "expected integer expression before ')'");
11280 release_tree_vector (args);
11281 return list;
11284 args_tree = build_tree_list_vec (args);
11286 for (t = args_tree; t; t = TREE_CHAIN (t))
11288 tree targ = TREE_VALUE (t);
11290 if (targ != error_mark_node)
11292 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11294 c_parser_error (parser, "expression must be integral");
11295 targ = error_mark_node;
11297 else
11299 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11301 OMP_CLAUSE_DECL (c) = targ;
11302 OMP_CLAUSE_CHAIN (c) = list;
11303 list = c;
11308 release_tree_vector (args);
11309 parens.require_close (parser);
11310 return list;
11313 /* OpenACC 2.0, OpenMP 2.5:
11314 variable-list:
11315 identifier
11316 variable-list , identifier
11318 If KIND is nonzero, create the appropriate node and install the
11319 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11320 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11322 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11323 return the list created. */
11325 static tree
11326 c_parser_omp_variable_list (c_parser *parser,
11327 location_t clause_loc,
11328 enum omp_clause_code kind, tree list)
11330 if (c_parser_next_token_is_not (parser, CPP_NAME)
11331 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11332 c_parser_error (parser, "expected identifier");
11334 while (c_parser_next_token_is (parser, CPP_NAME)
11335 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11337 tree t = lookup_name (c_parser_peek_token (parser)->value);
11339 if (t == NULL_TREE)
11341 undeclared_variable (c_parser_peek_token (parser)->location,
11342 c_parser_peek_token (parser)->value);
11343 t = error_mark_node;
11346 c_parser_consume_token (parser);
11348 if (t == error_mark_node)
11350 else if (kind != 0)
11352 switch (kind)
11354 case OMP_CLAUSE__CACHE_:
11355 /* The OpenACC cache directive explicitly only allows "array
11356 elements or subarrays". */
11357 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11359 c_parser_error (parser, "expected %<[%>");
11360 t = error_mark_node;
11361 break;
11363 /* FALLTHROUGH */
11364 case OMP_CLAUSE_MAP:
11365 case OMP_CLAUSE_FROM:
11366 case OMP_CLAUSE_TO:
11367 while (c_parser_next_token_is (parser, CPP_DOT))
11369 location_t op_loc = c_parser_peek_token (parser)->location;
11370 c_parser_consume_token (parser);
11371 if (!c_parser_next_token_is (parser, CPP_NAME))
11373 c_parser_error (parser, "expected identifier");
11374 t = error_mark_node;
11375 break;
11378 c_token *comp_tok = c_parser_peek_token (parser);
11379 tree ident = comp_tok->value;
11380 location_t comp_loc = comp_tok->location;
11381 c_parser_consume_token (parser);
11382 t = build_component_ref (op_loc, t, ident, comp_loc);
11384 /* FALLTHROUGH */
11385 case OMP_CLAUSE_DEPEND:
11386 case OMP_CLAUSE_REDUCTION:
11387 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11389 tree low_bound = NULL_TREE, length = NULL_TREE;
11391 c_parser_consume_token (parser);
11392 if (!c_parser_next_token_is (parser, CPP_COLON))
11394 location_t expr_loc
11395 = c_parser_peek_token (parser)->location;
11396 c_expr expr = c_parser_expression (parser);
11397 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11398 false, true);
11399 low_bound = expr.value;
11401 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11402 length = integer_one_node;
11403 else
11405 /* Look for `:'. */
11406 if (!c_parser_require (parser, CPP_COLON,
11407 "expected %<:%>"))
11409 t = error_mark_node;
11410 break;
11412 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11414 location_t expr_loc
11415 = c_parser_peek_token (parser)->location;
11416 c_expr expr = c_parser_expression (parser);
11417 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11418 false, true);
11419 length = expr.value;
11422 /* Look for the closing `]'. */
11423 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11424 "expected %<]%>"))
11426 t = error_mark_node;
11427 break;
11430 t = tree_cons (low_bound, length, t);
11432 break;
11433 default:
11434 break;
11437 if (t != error_mark_node)
11439 tree u = build_omp_clause (clause_loc, kind);
11440 OMP_CLAUSE_DECL (u) = t;
11441 OMP_CLAUSE_CHAIN (u) = list;
11442 list = u;
11445 else
11446 list = tree_cons (t, NULL_TREE, list);
11448 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11449 break;
11451 c_parser_consume_token (parser);
11454 return list;
11457 /* Similarly, but expect leading and trailing parenthesis. This is a very
11458 common case for OpenACC and OpenMP clauses. */
11460 static tree
11461 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11462 tree list)
11464 /* The clauses location. */
11465 location_t loc = c_parser_peek_token (parser)->location;
11467 matching_parens parens;
11468 if (parens.require_open (parser))
11470 list = c_parser_omp_variable_list (parser, loc, kind, list);
11471 parens.skip_until_found_close (parser);
11473 return list;
11476 /* OpenACC 2.0:
11477 copy ( variable-list )
11478 copyin ( variable-list )
11479 copyout ( variable-list )
11480 create ( variable-list )
11481 delete ( variable-list )
11482 present ( variable-list )
11483 present_or_copy ( variable-list )
11484 pcopy ( variable-list )
11485 present_or_copyin ( variable-list )
11486 pcopyin ( variable-list )
11487 present_or_copyout ( variable-list )
11488 pcopyout ( variable-list )
11489 present_or_create ( variable-list )
11490 pcreate ( variable-list ) */
11492 static tree
11493 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11494 tree list)
11496 enum gomp_map_kind kind;
11497 switch (c_kind)
11499 case PRAGMA_OACC_CLAUSE_COPY:
11500 kind = GOMP_MAP_FORCE_TOFROM;
11501 break;
11502 case PRAGMA_OACC_CLAUSE_COPYIN:
11503 kind = GOMP_MAP_FORCE_TO;
11504 break;
11505 case PRAGMA_OACC_CLAUSE_COPYOUT:
11506 kind = GOMP_MAP_FORCE_FROM;
11507 break;
11508 case PRAGMA_OACC_CLAUSE_CREATE:
11509 kind = GOMP_MAP_FORCE_ALLOC;
11510 break;
11511 case PRAGMA_OACC_CLAUSE_DELETE:
11512 kind = GOMP_MAP_DELETE;
11513 break;
11514 case PRAGMA_OACC_CLAUSE_DEVICE:
11515 kind = GOMP_MAP_FORCE_TO;
11516 break;
11517 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11518 kind = GOMP_MAP_DEVICE_RESIDENT;
11519 break;
11520 case PRAGMA_OACC_CLAUSE_HOST:
11521 case PRAGMA_OACC_CLAUSE_SELF:
11522 kind = GOMP_MAP_FORCE_FROM;
11523 break;
11524 case PRAGMA_OACC_CLAUSE_LINK:
11525 kind = GOMP_MAP_LINK;
11526 break;
11527 case PRAGMA_OACC_CLAUSE_PRESENT:
11528 kind = GOMP_MAP_FORCE_PRESENT;
11529 break;
11530 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11531 kind = GOMP_MAP_TOFROM;
11532 break;
11533 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11534 kind = GOMP_MAP_TO;
11535 break;
11536 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11537 kind = GOMP_MAP_FROM;
11538 break;
11539 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11540 kind = GOMP_MAP_ALLOC;
11541 break;
11542 default:
11543 gcc_unreachable ();
11545 tree nl, c;
11546 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11548 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11549 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11551 return nl;
11554 /* OpenACC 2.0:
11555 deviceptr ( variable-list ) */
11557 static tree
11558 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11560 location_t loc = c_parser_peek_token (parser)->location;
11561 tree vars, t;
11563 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11564 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11565 variable-list must only allow for pointer variables. */
11566 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11567 for (t = vars; t && t; t = TREE_CHAIN (t))
11569 tree v = TREE_PURPOSE (t);
11571 /* FIXME diagnostics: Ideally we should keep individual
11572 locations for all the variables in the var list to make the
11573 following errors more precise. Perhaps
11574 c_parser_omp_var_list_parens() should construct a list of
11575 locations to go along with the var list. */
11577 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11578 error_at (loc, "%qD is not a variable", v);
11579 else if (TREE_TYPE (v) == error_mark_node)
11581 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11582 error_at (loc, "%qD is not a pointer variable", v);
11584 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11585 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11586 OMP_CLAUSE_DECL (u) = v;
11587 OMP_CLAUSE_CHAIN (u) = list;
11588 list = u;
11591 return list;
11594 /* OpenACC 2.0, OpenMP 3.0:
11595 collapse ( constant-expression ) */
11597 static tree
11598 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11600 tree c, num = error_mark_node;
11601 HOST_WIDE_INT n;
11602 location_t loc;
11604 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11605 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11607 loc = c_parser_peek_token (parser)->location;
11608 matching_parens parens;
11609 if (parens.require_open (parser))
11611 num = c_parser_expr_no_commas (parser, NULL).value;
11612 parens.skip_until_found_close (parser);
11614 if (num == error_mark_node)
11615 return list;
11616 mark_exp_read (num);
11617 num = c_fully_fold (num, false, NULL);
11618 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11619 || !tree_fits_shwi_p (num)
11620 || (n = tree_to_shwi (num)) <= 0
11621 || (int) n != n)
11623 error_at (loc,
11624 "collapse argument needs positive constant integer expression");
11625 return list;
11627 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11628 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11629 OMP_CLAUSE_CHAIN (c) = list;
11630 return c;
11633 /* OpenMP 2.5:
11634 copyin ( variable-list ) */
11636 static tree
11637 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11639 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11642 /* OpenMP 2.5:
11643 copyprivate ( variable-list ) */
11645 static tree
11646 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11648 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11651 /* OpenMP 2.5:
11652 default ( none | shared )
11654 OpenACC:
11655 default ( none | present ) */
11657 static tree
11658 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11660 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11661 location_t loc = c_parser_peek_token (parser)->location;
11662 tree c;
11664 matching_parens parens;
11665 if (!parens.require_open (parser))
11666 return list;
11667 if (c_parser_next_token_is (parser, CPP_NAME))
11669 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11671 switch (p[0])
11673 case 'n':
11674 if (strcmp ("none", p) != 0)
11675 goto invalid_kind;
11676 kind = OMP_CLAUSE_DEFAULT_NONE;
11677 break;
11679 case 'p':
11680 if (strcmp ("present", p) != 0 || !is_oacc)
11681 goto invalid_kind;
11682 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11683 break;
11685 case 's':
11686 if (strcmp ("shared", p) != 0 || is_oacc)
11687 goto invalid_kind;
11688 kind = OMP_CLAUSE_DEFAULT_SHARED;
11689 break;
11691 default:
11692 goto invalid_kind;
11695 c_parser_consume_token (parser);
11697 else
11699 invalid_kind:
11700 if (is_oacc)
11701 c_parser_error (parser, "expected %<none%> or %<present%>");
11702 else
11703 c_parser_error (parser, "expected %<none%> or %<shared%>");
11705 parens.skip_until_found_close (parser);
11707 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11708 return list;
11710 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11711 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11712 OMP_CLAUSE_CHAIN (c) = list;
11713 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11715 return c;
11718 /* OpenMP 2.5:
11719 firstprivate ( variable-list ) */
11721 static tree
11722 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11724 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11727 /* OpenMP 3.1:
11728 final ( expression ) */
11730 static tree
11731 c_parser_omp_clause_final (c_parser *parser, tree list)
11733 location_t loc = c_parser_peek_token (parser)->location;
11734 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11736 tree t = c_parser_paren_condition (parser);
11737 tree c;
11739 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11741 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11742 OMP_CLAUSE_FINAL_EXPR (c) = t;
11743 OMP_CLAUSE_CHAIN (c) = list;
11744 list = c;
11746 else
11747 c_parser_error (parser, "expected %<(%>");
11749 return list;
11752 /* OpenACC, OpenMP 2.5:
11753 if ( expression )
11755 OpenMP 4.5:
11756 if ( directive-name-modifier : expression )
11758 directive-name-modifier:
11759 parallel | task | taskloop | target data | target | target update
11760 | target enter data | target exit data */
11762 static tree
11763 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11765 location_t location = c_parser_peek_token (parser)->location;
11766 enum tree_code if_modifier = ERROR_MARK;
11768 matching_parens parens;
11769 if (!parens.require_open (parser))
11770 return list;
11772 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11774 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11775 int n = 2;
11776 if (strcmp (p, "parallel") == 0)
11777 if_modifier = OMP_PARALLEL;
11778 else if (strcmp (p, "task") == 0)
11779 if_modifier = OMP_TASK;
11780 else if (strcmp (p, "taskloop") == 0)
11781 if_modifier = OMP_TASKLOOP;
11782 else if (strcmp (p, "target") == 0)
11784 if_modifier = OMP_TARGET;
11785 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11787 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11788 if (strcmp ("data", p) == 0)
11789 if_modifier = OMP_TARGET_DATA;
11790 else if (strcmp ("update", p) == 0)
11791 if_modifier = OMP_TARGET_UPDATE;
11792 else if (strcmp ("enter", p) == 0)
11793 if_modifier = OMP_TARGET_ENTER_DATA;
11794 else if (strcmp ("exit", p) == 0)
11795 if_modifier = OMP_TARGET_EXIT_DATA;
11796 if (if_modifier != OMP_TARGET)
11798 n = 3;
11799 c_parser_consume_token (parser);
11801 else
11803 location_t loc = c_parser_peek_2nd_token (parser)->location;
11804 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11805 "or %<exit%>");
11806 if_modifier = ERROR_MARK;
11808 if (if_modifier == OMP_TARGET_ENTER_DATA
11809 || if_modifier == OMP_TARGET_EXIT_DATA)
11811 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11813 p = IDENTIFIER_POINTER
11814 (c_parser_peek_2nd_token (parser)->value);
11815 if (strcmp ("data", p) == 0)
11816 n = 4;
11818 if (n == 4)
11819 c_parser_consume_token (parser);
11820 else
11822 location_t loc
11823 = c_parser_peek_2nd_token (parser)->location;
11824 error_at (loc, "expected %<data%>");
11825 if_modifier = ERROR_MARK;
11830 if (if_modifier != ERROR_MARK)
11832 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11834 c_parser_consume_token (parser);
11835 c_parser_consume_token (parser);
11837 else
11839 if (n > 2)
11841 location_t loc = c_parser_peek_2nd_token (parser)->location;
11842 error_at (loc, "expected %<:%>");
11844 if_modifier = ERROR_MARK;
11849 tree t = c_parser_condition (parser), c;
11850 parens.skip_until_found_close (parser);
11852 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11853 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11855 if (if_modifier != ERROR_MARK
11856 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11858 const char *p = NULL;
11859 switch (if_modifier)
11861 case OMP_PARALLEL: p = "parallel"; break;
11862 case OMP_TASK: p = "task"; break;
11863 case OMP_TASKLOOP: p = "taskloop"; break;
11864 case OMP_TARGET_DATA: p = "target data"; break;
11865 case OMP_TARGET: p = "target"; break;
11866 case OMP_TARGET_UPDATE: p = "target update"; break;
11867 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11868 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11869 default: gcc_unreachable ();
11871 error_at (location, "too many %<if%> clauses with %qs modifier",
11873 return list;
11875 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11877 if (!is_omp)
11878 error_at (location, "too many %<if%> clauses");
11879 else
11880 error_at (location, "too many %<if%> clauses without modifier");
11881 return list;
11883 else if (if_modifier == ERROR_MARK
11884 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11886 error_at (location, "if any %<if%> clause has modifier, then all "
11887 "%<if%> clauses have to use modifier");
11888 return list;
11892 c = build_omp_clause (location, OMP_CLAUSE_IF);
11893 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11894 OMP_CLAUSE_IF_EXPR (c) = t;
11895 OMP_CLAUSE_CHAIN (c) = list;
11896 return c;
11899 /* OpenMP 2.5:
11900 lastprivate ( variable-list ) */
11902 static tree
11903 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11905 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11908 /* OpenMP 3.1:
11909 mergeable */
11911 static tree
11912 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11914 tree c;
11916 /* FIXME: Should we allow duplicates? */
11917 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11919 c = build_omp_clause (c_parser_peek_token (parser)->location,
11920 OMP_CLAUSE_MERGEABLE);
11921 OMP_CLAUSE_CHAIN (c) = list;
11923 return c;
11926 /* OpenMP 2.5:
11927 nowait */
11929 static tree
11930 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11932 tree c;
11933 location_t loc = c_parser_peek_token (parser)->location;
11935 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11937 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11938 OMP_CLAUSE_CHAIN (c) = list;
11939 return c;
11942 /* OpenMP 2.5:
11943 num_threads ( expression ) */
11945 static tree
11946 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11948 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11949 matching_parens parens;
11950 if (parens.require_open (parser))
11952 location_t expr_loc = c_parser_peek_token (parser)->location;
11953 c_expr expr = c_parser_expression (parser);
11954 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11955 tree c, t = expr.value;
11956 t = c_fully_fold (t, false, NULL);
11958 parens.skip_until_found_close (parser);
11960 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11962 c_parser_error (parser, "expected integer expression");
11963 return list;
11966 /* Attempt to statically determine when the number isn't positive. */
11967 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11968 build_int_cst (TREE_TYPE (t), 0));
11969 protected_set_expr_location (c, expr_loc);
11970 if (c == boolean_true_node)
11972 warning_at (expr_loc, 0,
11973 "%<num_threads%> value must be positive");
11974 t = integer_one_node;
11977 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11979 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11980 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11981 OMP_CLAUSE_CHAIN (c) = list;
11982 list = c;
11985 return list;
11988 /* OpenMP 4.5:
11989 num_tasks ( expression ) */
11991 static tree
11992 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11994 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11995 matching_parens parens;
11996 if (parens.require_open (parser))
11998 location_t expr_loc = c_parser_peek_token (parser)->location;
11999 c_expr expr = c_parser_expression (parser);
12000 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12001 tree c, t = expr.value;
12002 t = c_fully_fold (t, false, NULL);
12004 parens.skip_until_found_close (parser);
12006 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12008 c_parser_error (parser, "expected integer expression");
12009 return list;
12012 /* Attempt to statically determine when the number isn't positive. */
12013 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12014 build_int_cst (TREE_TYPE (t), 0));
12015 if (CAN_HAVE_LOCATION_P (c))
12016 SET_EXPR_LOCATION (c, expr_loc);
12017 if (c == boolean_true_node)
12019 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12020 t = integer_one_node;
12023 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12025 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12026 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12027 OMP_CLAUSE_CHAIN (c) = list;
12028 list = c;
12031 return list;
12034 /* OpenMP 4.5:
12035 grainsize ( expression ) */
12037 static tree
12038 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12040 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12041 matching_parens parens;
12042 if (parens.require_open (parser))
12044 location_t expr_loc = c_parser_peek_token (parser)->location;
12045 c_expr expr = c_parser_expression (parser);
12046 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12047 tree c, t = expr.value;
12048 t = c_fully_fold (t, false, NULL);
12050 parens.skip_until_found_close (parser);
12052 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12054 c_parser_error (parser, "expected integer expression");
12055 return list;
12058 /* Attempt to statically determine when the number isn't positive. */
12059 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12060 build_int_cst (TREE_TYPE (t), 0));
12061 if (CAN_HAVE_LOCATION_P (c))
12062 SET_EXPR_LOCATION (c, expr_loc);
12063 if (c == boolean_true_node)
12065 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12066 t = integer_one_node;
12069 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12071 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12072 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12073 OMP_CLAUSE_CHAIN (c) = list;
12074 list = c;
12077 return list;
12080 /* OpenMP 4.5:
12081 priority ( expression ) */
12083 static tree
12084 c_parser_omp_clause_priority (c_parser *parser, tree list)
12086 location_t priority_loc = c_parser_peek_token (parser)->location;
12087 matching_parens parens;
12088 if (parens.require_open (parser))
12090 location_t expr_loc = c_parser_peek_token (parser)->location;
12091 c_expr expr = c_parser_expression (parser);
12092 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12093 tree c, t = expr.value;
12094 t = c_fully_fold (t, false, NULL);
12096 parens.skip_until_found_close (parser);
12098 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12100 c_parser_error (parser, "expected integer expression");
12101 return list;
12104 /* Attempt to statically determine when the number isn't
12105 non-negative. */
12106 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12107 build_int_cst (TREE_TYPE (t), 0));
12108 if (CAN_HAVE_LOCATION_P (c))
12109 SET_EXPR_LOCATION (c, expr_loc);
12110 if (c == boolean_true_node)
12112 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12113 t = integer_one_node;
12116 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12118 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12119 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12120 OMP_CLAUSE_CHAIN (c) = list;
12121 list = c;
12124 return list;
12127 /* OpenMP 4.5:
12128 hint ( expression ) */
12130 static tree
12131 c_parser_omp_clause_hint (c_parser *parser, tree list)
12133 location_t hint_loc = c_parser_peek_token (parser)->location;
12134 matching_parens parens;
12135 if (parens.require_open (parser))
12137 location_t expr_loc = c_parser_peek_token (parser)->location;
12138 c_expr expr = c_parser_expression (parser);
12139 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12140 tree c, t = expr.value;
12141 t = c_fully_fold (t, false, NULL);
12143 parens.skip_until_found_close (parser);
12145 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12147 c_parser_error (parser, "expected integer expression");
12148 return list;
12151 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12153 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12154 OMP_CLAUSE_HINT_EXPR (c) = t;
12155 OMP_CLAUSE_CHAIN (c) = list;
12156 list = c;
12159 return list;
12162 /* OpenMP 4.5:
12163 defaultmap ( tofrom : scalar ) */
12165 static tree
12166 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12168 location_t loc = c_parser_peek_token (parser)->location;
12169 tree c;
12170 const char *p;
12172 matching_parens parens;
12173 if (!parens.require_open (parser))
12174 return list;
12175 if (!c_parser_next_token_is (parser, CPP_NAME))
12177 c_parser_error (parser, "expected %<tofrom%>");
12178 goto out_err;
12180 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12181 if (strcmp (p, "tofrom") != 0)
12183 c_parser_error (parser, "expected %<tofrom%>");
12184 goto out_err;
12186 c_parser_consume_token (parser);
12187 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12188 goto out_err;
12189 if (!c_parser_next_token_is (parser, CPP_NAME))
12191 c_parser_error (parser, "expected %<scalar%>");
12192 goto out_err;
12194 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12195 if (strcmp (p, "scalar") != 0)
12197 c_parser_error (parser, "expected %<scalar%>");
12198 goto out_err;
12200 c_parser_consume_token (parser);
12201 parens.skip_until_found_close (parser);
12202 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12203 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12204 OMP_CLAUSE_CHAIN (c) = list;
12205 return c;
12207 out_err:
12208 parens.skip_until_found_close (parser);
12209 return list;
12212 /* OpenACC 2.0:
12213 use_device ( variable-list )
12215 OpenMP 4.5:
12216 use_device_ptr ( variable-list ) */
12218 static tree
12219 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12221 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12222 list);
12225 /* OpenMP 4.5:
12226 is_device_ptr ( variable-list ) */
12228 static tree
12229 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12231 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12234 /* OpenACC:
12235 num_gangs ( expression )
12236 num_workers ( expression )
12237 vector_length ( expression ) */
12239 static tree
12240 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12241 tree list)
12243 location_t loc = c_parser_peek_token (parser)->location;
12245 matching_parens parens;
12246 if (!parens.require_open (parser))
12247 return list;
12249 location_t expr_loc = c_parser_peek_token (parser)->location;
12250 c_expr expr = c_parser_expression (parser);
12251 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12252 tree c, t = expr.value;
12253 t = c_fully_fold (t, false, NULL);
12255 parens.skip_until_found_close (parser);
12257 if (t == error_mark_node)
12258 return list;
12259 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12261 error_at (expr_loc, "%qs expression must be integral",
12262 omp_clause_code_name[code]);
12263 return list;
12266 /* Attempt to statically determine when the number isn't positive. */
12267 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12268 build_int_cst (TREE_TYPE (t), 0));
12269 protected_set_expr_location (c, expr_loc);
12270 if (c == boolean_true_node)
12272 warning_at (expr_loc, 0,
12273 "%qs value must be positive",
12274 omp_clause_code_name[code]);
12275 t = integer_one_node;
12278 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12280 c = build_omp_clause (loc, code);
12281 OMP_CLAUSE_OPERAND (c, 0) = t;
12282 OMP_CLAUSE_CHAIN (c) = list;
12283 return c;
12286 /* OpenACC:
12288 gang [( gang-arg-list )]
12289 worker [( [num:] int-expr )]
12290 vector [( [length:] int-expr )]
12292 where gang-arg is one of:
12294 [num:] int-expr
12295 static: size-expr
12297 and size-expr may be:
12300 int-expr
12303 static tree
12304 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12305 const char *str, tree list)
12307 const char *id = "num";
12308 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12309 location_t loc = c_parser_peek_token (parser)->location;
12311 if (kind == OMP_CLAUSE_VECTOR)
12312 id = "length";
12314 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12316 c_parser_consume_token (parser);
12320 c_token *next = c_parser_peek_token (parser);
12321 int idx = 0;
12323 /* Gang static argument. */
12324 if (kind == OMP_CLAUSE_GANG
12325 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12327 c_parser_consume_token (parser);
12329 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12330 goto cleanup_error;
12332 idx = 1;
12333 if (ops[idx] != NULL_TREE)
12335 c_parser_error (parser, "too many %<static%> arguments");
12336 goto cleanup_error;
12339 /* Check for the '*' argument. */
12340 if (c_parser_next_token_is (parser, CPP_MULT)
12341 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12342 || c_parser_peek_2nd_token (parser)->type
12343 == CPP_CLOSE_PAREN))
12345 c_parser_consume_token (parser);
12346 ops[idx] = integer_minus_one_node;
12348 if (c_parser_next_token_is (parser, CPP_COMMA))
12350 c_parser_consume_token (parser);
12351 continue;
12353 else
12354 break;
12357 /* Worker num: argument and vector length: arguments. */
12358 else if (c_parser_next_token_is (parser, CPP_NAME)
12359 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12360 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12362 c_parser_consume_token (parser); /* id */
12363 c_parser_consume_token (parser); /* ':' */
12366 /* Now collect the actual argument. */
12367 if (ops[idx] != NULL_TREE)
12369 c_parser_error (parser, "unexpected argument");
12370 goto cleanup_error;
12373 location_t expr_loc = c_parser_peek_token (parser)->location;
12374 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12375 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12376 tree expr = cexpr.value;
12377 if (expr == error_mark_node)
12378 goto cleanup_error;
12380 expr = c_fully_fold (expr, false, NULL);
12382 /* Attempt to statically determine when the number isn't a
12383 positive integer. */
12385 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12387 c_parser_error (parser, "expected integer expression");
12388 return list;
12391 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12392 build_int_cst (TREE_TYPE (expr), 0));
12393 if (c == boolean_true_node)
12395 warning_at (loc, 0,
12396 "%qs value must be positive", str);
12397 expr = integer_one_node;
12400 ops[idx] = expr;
12402 if (kind == OMP_CLAUSE_GANG
12403 && c_parser_next_token_is (parser, CPP_COMMA))
12405 c_parser_consume_token (parser);
12406 continue;
12408 break;
12410 while (1);
12412 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12413 goto cleanup_error;
12416 check_no_duplicate_clause (list, kind, str);
12418 c = build_omp_clause (loc, kind);
12420 if (ops[1])
12421 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12423 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12424 OMP_CLAUSE_CHAIN (c) = list;
12426 return c;
12428 cleanup_error:
12429 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12430 return list;
12433 /* OpenACC:
12434 auto
12435 independent
12436 nohost
12437 seq */
12439 static tree
12440 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12441 tree list)
12443 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12445 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12446 OMP_CLAUSE_CHAIN (c) = list;
12448 return c;
12451 /* OpenACC:
12452 async [( int-expr )] */
12454 static tree
12455 c_parser_oacc_clause_async (c_parser *parser, tree list)
12457 tree c, t;
12458 location_t loc = c_parser_peek_token (parser)->location;
12460 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12462 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12464 c_parser_consume_token (parser);
12466 t = c_parser_expression (parser).value;
12467 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12468 c_parser_error (parser, "expected integer expression");
12469 else if (t == error_mark_node
12470 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12471 return list;
12473 else
12474 t = c_fully_fold (t, false, NULL);
12476 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12478 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12479 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12480 OMP_CLAUSE_CHAIN (c) = list;
12481 list = c;
12483 return list;
12486 /* OpenACC 2.0:
12487 tile ( size-expr-list ) */
12489 static tree
12490 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12492 tree c, expr = error_mark_node;
12493 location_t loc;
12494 tree tile = NULL_TREE;
12496 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12497 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12499 loc = c_parser_peek_token (parser)->location;
12500 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12501 return list;
12505 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12506 return list;
12508 if (c_parser_next_token_is (parser, CPP_MULT)
12509 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12510 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12512 c_parser_consume_token (parser);
12513 expr = integer_zero_node;
12515 else
12517 location_t expr_loc = c_parser_peek_token (parser)->location;
12518 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12519 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12520 expr = cexpr.value;
12522 if (expr == error_mark_node)
12524 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12525 "expected %<)%>");
12526 return list;
12529 expr = c_fully_fold (expr, false, NULL);
12531 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12532 || !tree_fits_shwi_p (expr)
12533 || tree_to_shwi (expr) <= 0)
12535 error_at (expr_loc, "%<tile%> argument needs positive"
12536 " integral constant");
12537 expr = integer_zero_node;
12541 tile = tree_cons (NULL_TREE, expr, tile);
12543 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12545 /* Consume the trailing ')'. */
12546 c_parser_consume_token (parser);
12548 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12549 tile = nreverse (tile);
12550 OMP_CLAUSE_TILE_LIST (c) = tile;
12551 OMP_CLAUSE_CHAIN (c) = list;
12552 return c;
12555 /* OpenACC:
12556 wait ( int-expr-list ) */
12558 static tree
12559 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12561 location_t clause_loc = c_parser_peek_token (parser)->location;
12563 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12564 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12566 return list;
12569 /* OpenMP 2.5:
12570 ordered
12572 OpenMP 4.5:
12573 ordered ( constant-expression ) */
12575 static tree
12576 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12578 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12580 tree c, num = NULL_TREE;
12581 HOST_WIDE_INT n;
12582 location_t loc = c_parser_peek_token (parser)->location;
12583 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12585 matching_parens parens;
12586 parens.consume_open (parser);
12587 num = c_parser_expr_no_commas (parser, NULL).value;
12588 parens.skip_until_found_close (parser);
12590 if (num == error_mark_node)
12591 return list;
12592 if (num)
12594 mark_exp_read (num);
12595 num = c_fully_fold (num, false, NULL);
12596 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12597 || !tree_fits_shwi_p (num)
12598 || (n = tree_to_shwi (num)) <= 0
12599 || (int) n != n)
12601 error_at (loc, "ordered argument needs positive "
12602 "constant integer expression");
12603 return list;
12606 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12607 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12608 OMP_CLAUSE_CHAIN (c) = list;
12609 return c;
12612 /* OpenMP 2.5:
12613 private ( variable-list ) */
12615 static tree
12616 c_parser_omp_clause_private (c_parser *parser, tree list)
12618 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12621 /* OpenMP 2.5:
12622 reduction ( reduction-operator : variable-list )
12624 reduction-operator:
12625 One of: + * - & ^ | && ||
12627 OpenMP 3.1:
12629 reduction-operator:
12630 One of: + * - & ^ | && || max min
12632 OpenMP 4.0:
12634 reduction-operator:
12635 One of: + * - & ^ | && ||
12636 identifier */
12638 static tree
12639 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12641 location_t clause_loc = c_parser_peek_token (parser)->location;
12642 matching_parens parens;
12643 if (parens.require_open (parser))
12645 enum tree_code code = ERROR_MARK;
12646 tree reduc_id = NULL_TREE;
12648 switch (c_parser_peek_token (parser)->type)
12650 case CPP_PLUS:
12651 code = PLUS_EXPR;
12652 break;
12653 case CPP_MULT:
12654 code = MULT_EXPR;
12655 break;
12656 case CPP_MINUS:
12657 code = MINUS_EXPR;
12658 break;
12659 case CPP_AND:
12660 code = BIT_AND_EXPR;
12661 break;
12662 case CPP_XOR:
12663 code = BIT_XOR_EXPR;
12664 break;
12665 case CPP_OR:
12666 code = BIT_IOR_EXPR;
12667 break;
12668 case CPP_AND_AND:
12669 code = TRUTH_ANDIF_EXPR;
12670 break;
12671 case CPP_OR_OR:
12672 code = TRUTH_ORIF_EXPR;
12673 break;
12674 case CPP_NAME:
12676 const char *p
12677 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12678 if (strcmp (p, "min") == 0)
12680 code = MIN_EXPR;
12681 break;
12683 if (strcmp (p, "max") == 0)
12685 code = MAX_EXPR;
12686 break;
12688 reduc_id = c_parser_peek_token (parser)->value;
12689 break;
12691 default:
12692 c_parser_error (parser,
12693 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12694 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12695 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12696 return list;
12698 c_parser_consume_token (parser);
12699 reduc_id = c_omp_reduction_id (code, reduc_id);
12700 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12702 tree nl, c;
12704 nl = c_parser_omp_variable_list (parser, clause_loc,
12705 OMP_CLAUSE_REDUCTION, list);
12706 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12708 tree d = OMP_CLAUSE_DECL (c), type;
12709 if (TREE_CODE (d) != TREE_LIST)
12710 type = TREE_TYPE (d);
12711 else
12713 int cnt = 0;
12714 tree t;
12715 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12716 cnt++;
12717 type = TREE_TYPE (t);
12718 while (cnt > 0)
12720 if (TREE_CODE (type) != POINTER_TYPE
12721 && TREE_CODE (type) != ARRAY_TYPE)
12722 break;
12723 type = TREE_TYPE (type);
12724 cnt--;
12727 while (TREE_CODE (type) == ARRAY_TYPE)
12728 type = TREE_TYPE (type);
12729 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12730 if (code == ERROR_MARK
12731 || !(INTEGRAL_TYPE_P (type)
12732 || TREE_CODE (type) == REAL_TYPE
12733 || TREE_CODE (type) == COMPLEX_TYPE))
12734 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12735 = c_omp_reduction_lookup (reduc_id,
12736 TYPE_MAIN_VARIANT (type));
12739 list = nl;
12741 parens.skip_until_found_close (parser);
12743 return list;
12746 /* OpenMP 2.5:
12747 schedule ( schedule-kind )
12748 schedule ( schedule-kind , expression )
12750 schedule-kind:
12751 static | dynamic | guided | runtime | auto
12753 OpenMP 4.5:
12754 schedule ( schedule-modifier : schedule-kind )
12755 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12757 schedule-modifier:
12758 simd
12759 monotonic
12760 nonmonotonic */
12762 static tree
12763 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12765 tree c, t;
12766 location_t loc = c_parser_peek_token (parser)->location;
12767 int modifiers = 0, nmodifiers = 0;
12769 matching_parens parens;
12770 if (!parens.require_open (parser))
12771 return list;
12773 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12775 while (c_parser_next_token_is (parser, CPP_NAME))
12777 tree kind = c_parser_peek_token (parser)->value;
12778 const char *p = IDENTIFIER_POINTER (kind);
12779 if (strcmp ("simd", p) == 0)
12780 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12781 else if (strcmp ("monotonic", p) == 0)
12782 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12783 else if (strcmp ("nonmonotonic", p) == 0)
12784 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12785 else
12786 break;
12787 c_parser_consume_token (parser);
12788 if (nmodifiers++ == 0
12789 && c_parser_next_token_is (parser, CPP_COMMA))
12790 c_parser_consume_token (parser);
12791 else
12793 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12794 break;
12798 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12799 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12800 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12801 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12803 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12804 "specified");
12805 modifiers = 0;
12808 if (c_parser_next_token_is (parser, CPP_NAME))
12810 tree kind = c_parser_peek_token (parser)->value;
12811 const char *p = IDENTIFIER_POINTER (kind);
12813 switch (p[0])
12815 case 'd':
12816 if (strcmp ("dynamic", p) != 0)
12817 goto invalid_kind;
12818 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12819 break;
12821 case 'g':
12822 if (strcmp ("guided", p) != 0)
12823 goto invalid_kind;
12824 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12825 break;
12827 case 'r':
12828 if (strcmp ("runtime", p) != 0)
12829 goto invalid_kind;
12830 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12831 break;
12833 default:
12834 goto invalid_kind;
12837 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12838 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12839 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12840 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12841 else
12842 goto invalid_kind;
12844 c_parser_consume_token (parser);
12845 if (c_parser_next_token_is (parser, CPP_COMMA))
12847 location_t here;
12848 c_parser_consume_token (parser);
12850 here = c_parser_peek_token (parser)->location;
12851 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12852 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12853 t = expr.value;
12854 t = c_fully_fold (t, false, NULL);
12856 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12857 error_at (here, "schedule %<runtime%> does not take "
12858 "a %<chunk_size%> parameter");
12859 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12860 error_at (here,
12861 "schedule %<auto%> does not take "
12862 "a %<chunk_size%> parameter");
12863 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12865 /* Attempt to statically determine when the number isn't
12866 positive. */
12867 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12868 build_int_cst (TREE_TYPE (t), 0));
12869 protected_set_expr_location (s, loc);
12870 if (s == boolean_true_node)
12872 warning_at (loc, 0,
12873 "chunk size value must be positive");
12874 t = integer_one_node;
12876 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12878 else
12879 c_parser_error (parser, "expected integer expression");
12881 parens.skip_until_found_close (parser);
12883 else
12884 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12885 "expected %<,%> or %<)%>");
12887 OMP_CLAUSE_SCHEDULE_KIND (c)
12888 = (enum omp_clause_schedule_kind)
12889 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12891 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12892 OMP_CLAUSE_CHAIN (c) = list;
12893 return c;
12895 invalid_kind:
12896 c_parser_error (parser, "invalid schedule kind");
12897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12898 return list;
12901 /* OpenMP 2.5:
12902 shared ( variable-list ) */
12904 static tree
12905 c_parser_omp_clause_shared (c_parser *parser, tree list)
12907 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12910 /* OpenMP 3.0:
12911 untied */
12913 static tree
12914 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12916 tree c;
12918 /* FIXME: Should we allow duplicates? */
12919 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12921 c = build_omp_clause (c_parser_peek_token (parser)->location,
12922 OMP_CLAUSE_UNTIED);
12923 OMP_CLAUSE_CHAIN (c) = list;
12925 return c;
12928 /* OpenMP 4.0:
12929 inbranch
12930 notinbranch */
12932 static tree
12933 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12934 enum omp_clause_code code, tree list)
12936 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12938 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12939 OMP_CLAUSE_CHAIN (c) = list;
12941 return c;
12944 /* OpenMP 4.0:
12945 parallel
12947 sections
12948 taskgroup */
12950 static tree
12951 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12952 enum omp_clause_code code, tree list)
12954 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12955 OMP_CLAUSE_CHAIN (c) = list;
12957 return c;
12960 /* OpenMP 4.5:
12961 nogroup */
12963 static tree
12964 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12966 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12967 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12968 OMP_CLAUSE_NOGROUP);
12969 OMP_CLAUSE_CHAIN (c) = list;
12970 return c;
12973 /* OpenMP 4.5:
12974 simd
12975 threads */
12977 static tree
12978 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12979 enum omp_clause_code code, tree list)
12981 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12982 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12983 OMP_CLAUSE_CHAIN (c) = list;
12984 return c;
12987 /* OpenMP 4.0:
12988 num_teams ( expression ) */
12990 static tree
12991 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12993 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12994 matching_parens parens;
12995 if (parens.require_open (parser))
12997 location_t expr_loc = c_parser_peek_token (parser)->location;
12998 c_expr expr = c_parser_expression (parser);
12999 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13000 tree c, t = expr.value;
13001 t = c_fully_fold (t, false, NULL);
13003 parens.skip_until_found_close (parser);
13005 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13007 c_parser_error (parser, "expected integer expression");
13008 return list;
13011 /* Attempt to statically determine when the number isn't positive. */
13012 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13013 build_int_cst (TREE_TYPE (t), 0));
13014 protected_set_expr_location (c, expr_loc);
13015 if (c == boolean_true_node)
13017 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13018 t = integer_one_node;
13021 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13023 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13024 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13025 OMP_CLAUSE_CHAIN (c) = list;
13026 list = c;
13029 return list;
13032 /* OpenMP 4.0:
13033 thread_limit ( expression ) */
13035 static tree
13036 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13038 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13039 matching_parens parens;
13040 if (parens.require_open (parser))
13042 location_t expr_loc = c_parser_peek_token (parser)->location;
13043 c_expr expr = c_parser_expression (parser);
13044 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13045 tree c, t = expr.value;
13046 t = c_fully_fold (t, false, NULL);
13048 parens.skip_until_found_close (parser);
13050 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13052 c_parser_error (parser, "expected integer expression");
13053 return list;
13056 /* Attempt to statically determine when the number isn't positive. */
13057 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13058 build_int_cst (TREE_TYPE (t), 0));
13059 protected_set_expr_location (c, expr_loc);
13060 if (c == boolean_true_node)
13062 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13063 t = integer_one_node;
13066 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13067 "thread_limit");
13069 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13070 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13071 OMP_CLAUSE_CHAIN (c) = list;
13072 list = c;
13075 return list;
13078 /* OpenMP 4.0:
13079 aligned ( variable-list )
13080 aligned ( variable-list : constant-expression ) */
13082 static tree
13083 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13085 location_t clause_loc = c_parser_peek_token (parser)->location;
13086 tree nl, c;
13088 matching_parens parens;
13089 if (!parens.require_open (parser))
13090 return list;
13092 nl = c_parser_omp_variable_list (parser, clause_loc,
13093 OMP_CLAUSE_ALIGNED, list);
13095 if (c_parser_next_token_is (parser, CPP_COLON))
13097 c_parser_consume_token (parser);
13098 location_t expr_loc = c_parser_peek_token (parser)->location;
13099 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13100 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13101 tree alignment = expr.value;
13102 alignment = c_fully_fold (alignment, false, NULL);
13103 if (TREE_CODE (alignment) != INTEGER_CST
13104 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13105 || tree_int_cst_sgn (alignment) != 1)
13107 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13108 "be positive constant integer expression");
13109 alignment = NULL_TREE;
13112 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13113 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13116 parens.skip_until_found_close (parser);
13117 return nl;
13120 /* OpenMP 4.0:
13121 linear ( variable-list )
13122 linear ( variable-list : expression )
13124 OpenMP 4.5:
13125 linear ( modifier ( variable-list ) )
13126 linear ( modifier ( variable-list ) : expression ) */
13128 static tree
13129 c_parser_omp_clause_linear (c_parser *parser, tree list)
13131 location_t clause_loc = c_parser_peek_token (parser)->location;
13132 tree nl, c, step;
13133 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13135 matching_parens parens;
13136 if (!parens.require_open (parser))
13137 return list;
13139 if (c_parser_next_token_is (parser, CPP_NAME))
13141 c_token *tok = c_parser_peek_token (parser);
13142 const char *p = IDENTIFIER_POINTER (tok->value);
13143 if (strcmp ("val", p) == 0)
13144 kind = OMP_CLAUSE_LINEAR_VAL;
13145 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13146 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13147 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13149 c_parser_consume_token (parser);
13150 c_parser_consume_token (parser);
13154 nl = c_parser_omp_variable_list (parser, clause_loc,
13155 OMP_CLAUSE_LINEAR, list);
13157 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13158 parens.skip_until_found_close (parser);
13160 if (c_parser_next_token_is (parser, CPP_COLON))
13162 c_parser_consume_token (parser);
13163 location_t expr_loc = c_parser_peek_token (parser)->location;
13164 c_expr expr = c_parser_expression (parser);
13165 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13166 step = expr.value;
13167 step = c_fully_fold (step, false, NULL);
13168 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13170 error_at (clause_loc, "%<linear%> clause step expression must "
13171 "be integral");
13172 step = integer_one_node;
13176 else
13177 step = integer_one_node;
13179 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13181 OMP_CLAUSE_LINEAR_STEP (c) = step;
13182 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13185 parens.skip_until_found_close (parser);
13186 return nl;
13189 /* OpenMP 4.0:
13190 safelen ( constant-expression ) */
13192 static tree
13193 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13195 location_t clause_loc = c_parser_peek_token (parser)->location;
13196 tree c, t;
13198 matching_parens parens;
13199 if (!parens.require_open (parser))
13200 return list;
13202 location_t expr_loc = c_parser_peek_token (parser)->location;
13203 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13204 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13205 t = expr.value;
13206 t = c_fully_fold (t, false, NULL);
13207 if (TREE_CODE (t) != INTEGER_CST
13208 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13209 || tree_int_cst_sgn (t) != 1)
13211 error_at (clause_loc, "%<safelen%> clause expression must "
13212 "be positive constant integer expression");
13213 t = NULL_TREE;
13216 parens.skip_until_found_close (parser);
13217 if (t == NULL_TREE || t == error_mark_node)
13218 return list;
13220 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13222 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13223 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13224 OMP_CLAUSE_CHAIN (c) = list;
13225 return c;
13228 /* OpenMP 4.0:
13229 simdlen ( constant-expression ) */
13231 static tree
13232 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13234 location_t clause_loc = c_parser_peek_token (parser)->location;
13235 tree c, t;
13237 matching_parens parens;
13238 if (!parens.require_open (parser))
13239 return list;
13241 location_t expr_loc = c_parser_peek_token (parser)->location;
13242 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13243 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13244 t = expr.value;
13245 t = c_fully_fold (t, false, NULL);
13246 if (TREE_CODE (t) != INTEGER_CST
13247 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13248 || tree_int_cst_sgn (t) != 1)
13250 error_at (clause_loc, "%<simdlen%> clause expression must "
13251 "be positive constant integer expression");
13252 t = NULL_TREE;
13255 parens.skip_until_found_close (parser);
13256 if (t == NULL_TREE || t == error_mark_node)
13257 return list;
13259 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13261 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13262 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13263 OMP_CLAUSE_CHAIN (c) = list;
13264 return c;
13267 /* OpenMP 4.5:
13268 vec:
13269 identifier [+/- integer]
13270 vec , identifier [+/- integer]
13273 static tree
13274 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13275 tree list)
13277 tree vec = NULL;
13278 if (c_parser_next_token_is_not (parser, CPP_NAME)
13279 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13281 c_parser_error (parser, "expected identifier");
13282 return list;
13285 while (c_parser_next_token_is (parser, CPP_NAME)
13286 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13288 tree t = lookup_name (c_parser_peek_token (parser)->value);
13289 tree addend = NULL;
13291 if (t == NULL_TREE)
13293 undeclared_variable (c_parser_peek_token (parser)->location,
13294 c_parser_peek_token (parser)->value);
13295 t = error_mark_node;
13298 c_parser_consume_token (parser);
13300 bool neg = false;
13301 if (c_parser_next_token_is (parser, CPP_MINUS))
13302 neg = true;
13303 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13305 addend = integer_zero_node;
13306 neg = false;
13307 goto add_to_vector;
13309 c_parser_consume_token (parser);
13311 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13313 c_parser_error (parser, "expected integer");
13314 return list;
13317 addend = c_parser_peek_token (parser)->value;
13318 if (TREE_CODE (addend) != INTEGER_CST)
13320 c_parser_error (parser, "expected integer");
13321 return list;
13323 c_parser_consume_token (parser);
13325 add_to_vector:
13326 if (t != error_mark_node)
13328 vec = tree_cons (addend, t, vec);
13329 if (neg)
13330 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13333 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13334 break;
13336 c_parser_consume_token (parser);
13339 if (vec == NULL_TREE)
13340 return list;
13342 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13343 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13344 OMP_CLAUSE_DECL (u) = nreverse (vec);
13345 OMP_CLAUSE_CHAIN (u) = list;
13346 return u;
13349 /* OpenMP 4.0:
13350 depend ( depend-kind: variable-list )
13352 depend-kind:
13353 in | out | inout
13355 OpenMP 4.5:
13356 depend ( source )
13358 depend ( sink : vec ) */
13360 static tree
13361 c_parser_omp_clause_depend (c_parser *parser, tree list)
13363 location_t clause_loc = c_parser_peek_token (parser)->location;
13364 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13365 tree nl, c;
13367 matching_parens parens;
13368 if (!parens.require_open (parser))
13369 return list;
13371 if (c_parser_next_token_is (parser, CPP_NAME))
13373 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13374 if (strcmp ("in", p) == 0)
13375 kind = OMP_CLAUSE_DEPEND_IN;
13376 else if (strcmp ("inout", p) == 0)
13377 kind = OMP_CLAUSE_DEPEND_INOUT;
13378 else if (strcmp ("out", p) == 0)
13379 kind = OMP_CLAUSE_DEPEND_OUT;
13380 else if (strcmp ("source", p) == 0)
13381 kind = OMP_CLAUSE_DEPEND_SOURCE;
13382 else if (strcmp ("sink", p) == 0)
13383 kind = OMP_CLAUSE_DEPEND_SINK;
13384 else
13385 goto invalid_kind;
13387 else
13388 goto invalid_kind;
13390 c_parser_consume_token (parser);
13392 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13394 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13395 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13396 OMP_CLAUSE_DECL (c) = NULL_TREE;
13397 OMP_CLAUSE_CHAIN (c) = list;
13398 parens.skip_until_found_close (parser);
13399 return c;
13402 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13403 goto resync_fail;
13405 if (kind == OMP_CLAUSE_DEPEND_SINK)
13406 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13407 else
13409 nl = c_parser_omp_variable_list (parser, clause_loc,
13410 OMP_CLAUSE_DEPEND, list);
13412 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13413 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13416 parens.skip_until_found_close (parser);
13417 return nl;
13419 invalid_kind:
13420 c_parser_error (parser, "invalid depend kind");
13421 resync_fail:
13422 parens.skip_until_found_close (parser);
13423 return list;
13426 /* OpenMP 4.0:
13427 map ( map-kind: variable-list )
13428 map ( variable-list )
13430 map-kind:
13431 alloc | to | from | tofrom
13433 OpenMP 4.5:
13434 map-kind:
13435 alloc | to | from | tofrom | release | delete
13437 map ( always [,] map-kind: variable-list ) */
13439 static tree
13440 c_parser_omp_clause_map (c_parser *parser, tree list)
13442 location_t clause_loc = c_parser_peek_token (parser)->location;
13443 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13444 int always = 0;
13445 enum c_id_kind always_id_kind = C_ID_NONE;
13446 location_t always_loc = UNKNOWN_LOCATION;
13447 tree always_id = NULL_TREE;
13448 tree nl, c;
13450 matching_parens parens;
13451 if (!parens.require_open (parser))
13452 return list;
13454 if (c_parser_next_token_is (parser, CPP_NAME))
13456 c_token *tok = c_parser_peek_token (parser);
13457 const char *p = IDENTIFIER_POINTER (tok->value);
13458 always_id_kind = tok->id_kind;
13459 always_loc = tok->location;
13460 always_id = tok->value;
13461 if (strcmp ("always", p) == 0)
13463 c_token *sectok = c_parser_peek_2nd_token (parser);
13464 if (sectok->type == CPP_COMMA)
13466 c_parser_consume_token (parser);
13467 c_parser_consume_token (parser);
13468 always = 2;
13470 else if (sectok->type == CPP_NAME)
13472 p = IDENTIFIER_POINTER (sectok->value);
13473 if (strcmp ("alloc", p) == 0
13474 || strcmp ("to", p) == 0
13475 || strcmp ("from", p) == 0
13476 || strcmp ("tofrom", p) == 0
13477 || strcmp ("release", p) == 0
13478 || strcmp ("delete", p) == 0)
13480 c_parser_consume_token (parser);
13481 always = 1;
13487 if (c_parser_next_token_is (parser, CPP_NAME)
13488 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13490 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13491 if (strcmp ("alloc", p) == 0)
13492 kind = GOMP_MAP_ALLOC;
13493 else if (strcmp ("to", p) == 0)
13494 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13495 else if (strcmp ("from", p) == 0)
13496 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13497 else if (strcmp ("tofrom", p) == 0)
13498 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13499 else if (strcmp ("release", p) == 0)
13500 kind = GOMP_MAP_RELEASE;
13501 else if (strcmp ("delete", p) == 0)
13502 kind = GOMP_MAP_DELETE;
13503 else
13505 c_parser_error (parser, "invalid map kind");
13506 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13507 "expected %<)%>");
13508 return list;
13510 c_parser_consume_token (parser);
13511 c_parser_consume_token (parser);
13513 else if (always)
13515 if (always_id_kind != C_ID_ID)
13517 c_parser_error (parser, "expected identifier");
13518 parens.skip_until_found_close (parser);
13519 return list;
13522 tree t = lookup_name (always_id);
13523 if (t == NULL_TREE)
13525 undeclared_variable (always_loc, always_id);
13526 t = error_mark_node;
13528 if (t != error_mark_node)
13530 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13531 OMP_CLAUSE_DECL (u) = t;
13532 OMP_CLAUSE_CHAIN (u) = list;
13533 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13534 list = u;
13536 if (always == 1)
13538 parens.skip_until_found_close (parser);
13539 return list;
13543 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13545 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13546 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13548 parens.skip_until_found_close (parser);
13549 return nl;
13552 /* OpenMP 4.0:
13553 device ( expression ) */
13555 static tree
13556 c_parser_omp_clause_device (c_parser *parser, tree list)
13558 location_t clause_loc = c_parser_peek_token (parser)->location;
13559 matching_parens parens;
13560 if (parens.require_open (parser))
13562 location_t expr_loc = c_parser_peek_token (parser)->location;
13563 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13564 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13565 tree c, t = expr.value;
13566 t = c_fully_fold (t, false, NULL);
13568 parens.skip_until_found_close (parser);
13570 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13572 c_parser_error (parser, "expected integer expression");
13573 return list;
13576 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13578 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13579 OMP_CLAUSE_DEVICE_ID (c) = t;
13580 OMP_CLAUSE_CHAIN (c) = list;
13581 list = c;
13584 return list;
13587 /* OpenMP 4.0:
13588 dist_schedule ( static )
13589 dist_schedule ( static , expression ) */
13591 static tree
13592 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13594 tree c, t = NULL_TREE;
13595 location_t loc = c_parser_peek_token (parser)->location;
13597 matching_parens parens;
13598 if (!parens.require_open (parser))
13599 return list;
13601 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13603 c_parser_error (parser, "invalid dist_schedule kind");
13604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13605 "expected %<)%>");
13606 return list;
13609 c_parser_consume_token (parser);
13610 if (c_parser_next_token_is (parser, CPP_COMMA))
13612 c_parser_consume_token (parser);
13614 location_t expr_loc = c_parser_peek_token (parser)->location;
13615 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13616 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13617 t = expr.value;
13618 t = c_fully_fold (t, false, NULL);
13619 parens.skip_until_found_close (parser);
13621 else
13622 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13623 "expected %<,%> or %<)%>");
13625 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13626 if (t == error_mark_node)
13627 return list;
13629 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13630 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13631 OMP_CLAUSE_CHAIN (c) = list;
13632 return c;
13635 /* OpenMP 4.0:
13636 proc_bind ( proc-bind-kind )
13638 proc-bind-kind:
13639 master | close | spread */
13641 static tree
13642 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13644 location_t clause_loc = c_parser_peek_token (parser)->location;
13645 enum omp_clause_proc_bind_kind kind;
13646 tree c;
13648 matching_parens parens;
13649 if (!parens.require_open (parser))
13650 return list;
13652 if (c_parser_next_token_is (parser, CPP_NAME))
13654 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13655 if (strcmp ("master", p) == 0)
13656 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13657 else if (strcmp ("close", p) == 0)
13658 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13659 else if (strcmp ("spread", p) == 0)
13660 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13661 else
13662 goto invalid_kind;
13664 else
13665 goto invalid_kind;
13667 c_parser_consume_token (parser);
13668 parens.skip_until_found_close (parser);
13669 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13670 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13671 OMP_CLAUSE_CHAIN (c) = list;
13672 return c;
13674 invalid_kind:
13675 c_parser_error (parser, "invalid proc_bind kind");
13676 parens.skip_until_found_close (parser);
13677 return list;
13680 /* OpenMP 4.0:
13681 to ( variable-list ) */
13683 static tree
13684 c_parser_omp_clause_to (c_parser *parser, tree list)
13686 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13689 /* OpenMP 4.0:
13690 from ( variable-list ) */
13692 static tree
13693 c_parser_omp_clause_from (c_parser *parser, tree list)
13695 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13698 /* OpenMP 4.0:
13699 uniform ( variable-list ) */
13701 static tree
13702 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13704 /* The clauses location. */
13705 location_t loc = c_parser_peek_token (parser)->location;
13707 matching_parens parens;
13708 if (parens.require_open (parser))
13710 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13711 list);
13712 parens.skip_until_found_close (parser);
13714 return list;
13717 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13718 is a bitmask in MASK. Return the list of clauses found. */
13720 static tree
13721 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13722 const char *where, bool finish_p = true)
13724 tree clauses = NULL;
13725 bool first = true;
13727 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13729 location_t here;
13730 pragma_omp_clause c_kind;
13731 const char *c_name;
13732 tree prev = clauses;
13734 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13735 c_parser_consume_token (parser);
13737 here = c_parser_peek_token (parser)->location;
13738 c_kind = c_parser_omp_clause_name (parser);
13740 switch (c_kind)
13742 case PRAGMA_OACC_CLAUSE_ASYNC:
13743 clauses = c_parser_oacc_clause_async (parser, clauses);
13744 c_name = "async";
13745 break;
13746 case PRAGMA_OACC_CLAUSE_AUTO:
13747 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13748 clauses);
13749 c_name = "auto";
13750 break;
13751 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13752 clauses = c_parser_omp_clause_collapse (parser, clauses);
13753 c_name = "collapse";
13754 break;
13755 case PRAGMA_OACC_CLAUSE_COPY:
13756 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13757 c_name = "copy";
13758 break;
13759 case PRAGMA_OACC_CLAUSE_COPYIN:
13760 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13761 c_name = "copyin";
13762 break;
13763 case PRAGMA_OACC_CLAUSE_COPYOUT:
13764 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13765 c_name = "copyout";
13766 break;
13767 case PRAGMA_OACC_CLAUSE_CREATE:
13768 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13769 c_name = "create";
13770 break;
13771 case PRAGMA_OACC_CLAUSE_DELETE:
13772 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13773 c_name = "delete";
13774 break;
13775 case PRAGMA_OMP_CLAUSE_DEFAULT:
13776 clauses = c_parser_omp_clause_default (parser, clauses, true);
13777 c_name = "default";
13778 break;
13779 case PRAGMA_OACC_CLAUSE_DEVICE:
13780 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13781 c_name = "device";
13782 break;
13783 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13784 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13785 c_name = "deviceptr";
13786 break;
13787 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13788 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13789 c_name = "device_resident";
13790 break;
13791 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13792 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13793 c_name = "firstprivate";
13794 break;
13795 case PRAGMA_OACC_CLAUSE_GANG:
13796 c_name = "gang";
13797 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13798 c_name, clauses);
13799 break;
13800 case PRAGMA_OACC_CLAUSE_HOST:
13801 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13802 c_name = "host";
13803 break;
13804 case PRAGMA_OACC_CLAUSE_IF:
13805 clauses = c_parser_omp_clause_if (parser, clauses, false);
13806 c_name = "if";
13807 break;
13808 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13809 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13810 clauses);
13811 c_name = "independent";
13812 break;
13813 case PRAGMA_OACC_CLAUSE_LINK:
13814 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13815 c_name = "link";
13816 break;
13817 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13818 clauses = c_parser_oacc_single_int_clause (parser,
13819 OMP_CLAUSE_NUM_GANGS,
13820 clauses);
13821 c_name = "num_gangs";
13822 break;
13823 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13824 clauses = c_parser_oacc_single_int_clause (parser,
13825 OMP_CLAUSE_NUM_WORKERS,
13826 clauses);
13827 c_name = "num_workers";
13828 break;
13829 case PRAGMA_OACC_CLAUSE_PRESENT:
13830 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13831 c_name = "present";
13832 break;
13833 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13834 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13835 c_name = "present_or_copy";
13836 break;
13837 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13838 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13839 c_name = "present_or_copyin";
13840 break;
13841 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13842 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13843 c_name = "present_or_copyout";
13844 break;
13845 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13846 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13847 c_name = "present_or_create";
13848 break;
13849 case PRAGMA_OACC_CLAUSE_PRIVATE:
13850 clauses = c_parser_omp_clause_private (parser, clauses);
13851 c_name = "private";
13852 break;
13853 case PRAGMA_OACC_CLAUSE_REDUCTION:
13854 clauses = c_parser_omp_clause_reduction (parser, clauses);
13855 c_name = "reduction";
13856 break;
13857 case PRAGMA_OACC_CLAUSE_SELF:
13858 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13859 c_name = "self";
13860 break;
13861 case PRAGMA_OACC_CLAUSE_SEQ:
13862 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13863 clauses);
13864 c_name = "seq";
13865 break;
13866 case PRAGMA_OACC_CLAUSE_TILE:
13867 clauses = c_parser_oacc_clause_tile (parser, clauses);
13868 c_name = "tile";
13869 break;
13870 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13871 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13872 c_name = "use_device";
13873 break;
13874 case PRAGMA_OACC_CLAUSE_VECTOR:
13875 c_name = "vector";
13876 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13877 c_name, clauses);
13878 break;
13879 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13880 clauses = c_parser_oacc_single_int_clause (parser,
13881 OMP_CLAUSE_VECTOR_LENGTH,
13882 clauses);
13883 c_name = "vector_length";
13884 break;
13885 case PRAGMA_OACC_CLAUSE_WAIT:
13886 clauses = c_parser_oacc_clause_wait (parser, clauses);
13887 c_name = "wait";
13888 break;
13889 case PRAGMA_OACC_CLAUSE_WORKER:
13890 c_name = "worker";
13891 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13892 c_name, clauses);
13893 break;
13894 default:
13895 c_parser_error (parser, "expected %<#pragma acc%> clause");
13896 goto saw_error;
13899 first = false;
13901 if (((mask >> c_kind) & 1) == 0)
13903 /* Remove the invalid clause(s) from the list to avoid
13904 confusing the rest of the compiler. */
13905 clauses = prev;
13906 error_at (here, "%qs is not valid for %qs", c_name, where);
13910 saw_error:
13911 c_parser_skip_to_pragma_eol (parser);
13913 if (finish_p)
13914 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13916 return clauses;
13919 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13920 is a bitmask in MASK. Return the list of clauses found. */
13922 static tree
13923 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13924 const char *where, bool finish_p = true)
13926 tree clauses = NULL;
13927 bool first = true;
13929 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13931 location_t here;
13932 pragma_omp_clause c_kind;
13933 const char *c_name;
13934 tree prev = clauses;
13936 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13937 c_parser_consume_token (parser);
13939 here = c_parser_peek_token (parser)->location;
13940 c_kind = c_parser_omp_clause_name (parser);
13942 switch (c_kind)
13944 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13945 clauses = c_parser_omp_clause_collapse (parser, clauses);
13946 c_name = "collapse";
13947 break;
13948 case PRAGMA_OMP_CLAUSE_COPYIN:
13949 clauses = c_parser_omp_clause_copyin (parser, clauses);
13950 c_name = "copyin";
13951 break;
13952 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13953 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13954 c_name = "copyprivate";
13955 break;
13956 case PRAGMA_OMP_CLAUSE_DEFAULT:
13957 clauses = c_parser_omp_clause_default (parser, clauses, false);
13958 c_name = "default";
13959 break;
13960 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13961 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13962 c_name = "firstprivate";
13963 break;
13964 case PRAGMA_OMP_CLAUSE_FINAL:
13965 clauses = c_parser_omp_clause_final (parser, clauses);
13966 c_name = "final";
13967 break;
13968 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13969 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13970 c_name = "grainsize";
13971 break;
13972 case PRAGMA_OMP_CLAUSE_HINT:
13973 clauses = c_parser_omp_clause_hint (parser, clauses);
13974 c_name = "hint";
13975 break;
13976 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13977 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13978 c_name = "defaultmap";
13979 break;
13980 case PRAGMA_OMP_CLAUSE_IF:
13981 clauses = c_parser_omp_clause_if (parser, clauses, true);
13982 c_name = "if";
13983 break;
13984 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13985 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13986 c_name = "lastprivate";
13987 break;
13988 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13989 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13990 c_name = "mergeable";
13991 break;
13992 case PRAGMA_OMP_CLAUSE_NOWAIT:
13993 clauses = c_parser_omp_clause_nowait (parser, clauses);
13994 c_name = "nowait";
13995 break;
13996 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13997 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13998 c_name = "num_tasks";
13999 break;
14000 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14001 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14002 c_name = "num_threads";
14003 break;
14004 case PRAGMA_OMP_CLAUSE_ORDERED:
14005 clauses = c_parser_omp_clause_ordered (parser, clauses);
14006 c_name = "ordered";
14007 break;
14008 case PRAGMA_OMP_CLAUSE_PRIORITY:
14009 clauses = c_parser_omp_clause_priority (parser, clauses);
14010 c_name = "priority";
14011 break;
14012 case PRAGMA_OMP_CLAUSE_PRIVATE:
14013 clauses = c_parser_omp_clause_private (parser, clauses);
14014 c_name = "private";
14015 break;
14016 case PRAGMA_OMP_CLAUSE_REDUCTION:
14017 clauses = c_parser_omp_clause_reduction (parser, clauses);
14018 c_name = "reduction";
14019 break;
14020 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14021 clauses = c_parser_omp_clause_schedule (parser, clauses);
14022 c_name = "schedule";
14023 break;
14024 case PRAGMA_OMP_CLAUSE_SHARED:
14025 clauses = c_parser_omp_clause_shared (parser, clauses);
14026 c_name = "shared";
14027 break;
14028 case PRAGMA_OMP_CLAUSE_UNTIED:
14029 clauses = c_parser_omp_clause_untied (parser, clauses);
14030 c_name = "untied";
14031 break;
14032 case PRAGMA_OMP_CLAUSE_INBRANCH:
14033 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14034 clauses);
14035 c_name = "inbranch";
14036 break;
14037 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14038 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14039 clauses);
14040 c_name = "notinbranch";
14041 break;
14042 case PRAGMA_OMP_CLAUSE_PARALLEL:
14043 clauses
14044 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14045 clauses);
14046 c_name = "parallel";
14047 if (!first)
14049 clause_not_first:
14050 error_at (here, "%qs must be the first clause of %qs",
14051 c_name, where);
14052 clauses = prev;
14054 break;
14055 case PRAGMA_OMP_CLAUSE_FOR:
14056 clauses
14057 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14058 clauses);
14059 c_name = "for";
14060 if (!first)
14061 goto clause_not_first;
14062 break;
14063 case PRAGMA_OMP_CLAUSE_SECTIONS:
14064 clauses
14065 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14066 clauses);
14067 c_name = "sections";
14068 if (!first)
14069 goto clause_not_first;
14070 break;
14071 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14072 clauses
14073 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14074 clauses);
14075 c_name = "taskgroup";
14076 if (!first)
14077 goto clause_not_first;
14078 break;
14079 case PRAGMA_OMP_CLAUSE_LINK:
14080 clauses
14081 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14082 c_name = "link";
14083 break;
14084 case PRAGMA_OMP_CLAUSE_TO:
14085 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14086 clauses
14087 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14088 clauses);
14089 else
14090 clauses = c_parser_omp_clause_to (parser, clauses);
14091 c_name = "to";
14092 break;
14093 case PRAGMA_OMP_CLAUSE_FROM:
14094 clauses = c_parser_omp_clause_from (parser, clauses);
14095 c_name = "from";
14096 break;
14097 case PRAGMA_OMP_CLAUSE_UNIFORM:
14098 clauses = c_parser_omp_clause_uniform (parser, clauses);
14099 c_name = "uniform";
14100 break;
14101 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14102 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14103 c_name = "num_teams";
14104 break;
14105 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14106 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14107 c_name = "thread_limit";
14108 break;
14109 case PRAGMA_OMP_CLAUSE_ALIGNED:
14110 clauses = c_parser_omp_clause_aligned (parser, clauses);
14111 c_name = "aligned";
14112 break;
14113 case PRAGMA_OMP_CLAUSE_LINEAR:
14114 clauses = c_parser_omp_clause_linear (parser, clauses);
14115 c_name = "linear";
14116 break;
14117 case PRAGMA_OMP_CLAUSE_DEPEND:
14118 clauses = c_parser_omp_clause_depend (parser, clauses);
14119 c_name = "depend";
14120 break;
14121 case PRAGMA_OMP_CLAUSE_MAP:
14122 clauses = c_parser_omp_clause_map (parser, clauses);
14123 c_name = "map";
14124 break;
14125 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14126 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14127 c_name = "use_device_ptr";
14128 break;
14129 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14130 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14131 c_name = "is_device_ptr";
14132 break;
14133 case PRAGMA_OMP_CLAUSE_DEVICE:
14134 clauses = c_parser_omp_clause_device (parser, clauses);
14135 c_name = "device";
14136 break;
14137 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14138 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14139 c_name = "dist_schedule";
14140 break;
14141 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14142 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14143 c_name = "proc_bind";
14144 break;
14145 case PRAGMA_OMP_CLAUSE_SAFELEN:
14146 clauses = c_parser_omp_clause_safelen (parser, clauses);
14147 c_name = "safelen";
14148 break;
14149 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14150 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14151 c_name = "simdlen";
14152 break;
14153 case PRAGMA_OMP_CLAUSE_NOGROUP:
14154 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14155 c_name = "nogroup";
14156 break;
14157 case PRAGMA_OMP_CLAUSE_THREADS:
14158 clauses
14159 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14160 clauses);
14161 c_name = "threads";
14162 break;
14163 case PRAGMA_OMP_CLAUSE_SIMD:
14164 clauses
14165 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14166 clauses);
14167 c_name = "simd";
14168 break;
14169 default:
14170 c_parser_error (parser, "expected %<#pragma omp%> clause");
14171 goto saw_error;
14174 first = false;
14176 if (((mask >> c_kind) & 1) == 0)
14178 /* Remove the invalid clause(s) from the list to avoid
14179 confusing the rest of the compiler. */
14180 clauses = prev;
14181 error_at (here, "%qs is not valid for %qs", c_name, where);
14185 saw_error:
14186 c_parser_skip_to_pragma_eol (parser);
14188 if (finish_p)
14190 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14191 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14192 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14195 return clauses;
14198 /* OpenACC 2.0, OpenMP 2.5:
14199 structured-block:
14200 statement
14202 In practice, we're also interested in adding the statement to an
14203 outer node. So it is convenient if we work around the fact that
14204 c_parser_statement calls add_stmt. */
14206 static tree
14207 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14209 tree stmt = push_stmt_list ();
14210 c_parser_statement (parser, if_p);
14211 return pop_stmt_list (stmt);
14214 /* OpenACC 2.0:
14215 # pragma acc cache (variable-list) new-line
14217 LOC is the location of the #pragma token.
14220 static tree
14221 c_parser_oacc_cache (location_t loc, c_parser *parser)
14223 tree stmt, clauses;
14225 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14226 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14228 c_parser_skip_to_pragma_eol (parser);
14230 stmt = make_node (OACC_CACHE);
14231 TREE_TYPE (stmt) = void_type_node;
14232 OACC_CACHE_CLAUSES (stmt) = clauses;
14233 SET_EXPR_LOCATION (stmt, loc);
14234 add_stmt (stmt);
14236 return stmt;
14239 /* OpenACC 2.0:
14240 # pragma acc data oacc-data-clause[optseq] new-line
14241 structured-block
14243 LOC is the location of the #pragma token.
14246 #define OACC_DATA_CLAUSE_MASK \
14247 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14259 static tree
14260 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14262 tree stmt, clauses, block;
14264 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14265 "#pragma acc data");
14267 block = c_begin_omp_parallel ();
14268 add_stmt (c_parser_omp_structured_block (parser, if_p));
14270 stmt = c_finish_oacc_data (loc, clauses, block);
14272 return stmt;
14275 /* OpenACC 2.0:
14276 # pragma acc declare oacc-data-clause[optseq] new-line
14279 #define OACC_DECLARE_CLAUSE_MASK \
14280 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14293 static void
14294 c_parser_oacc_declare (c_parser *parser)
14296 location_t pragma_loc = c_parser_peek_token (parser)->location;
14297 tree clauses, stmt, t, decl;
14299 bool error = false;
14301 c_parser_consume_pragma (parser);
14303 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14304 "#pragma acc declare");
14305 if (!clauses)
14307 error_at (pragma_loc,
14308 "no valid clauses specified in %<#pragma acc declare%>");
14309 return;
14312 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14314 location_t loc = OMP_CLAUSE_LOCATION (t);
14315 decl = OMP_CLAUSE_DECL (t);
14316 if (!DECL_P (decl))
14318 error_at (loc, "array section in %<#pragma acc declare%>");
14319 error = true;
14320 continue;
14323 switch (OMP_CLAUSE_MAP_KIND (t))
14325 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14326 case GOMP_MAP_FORCE_ALLOC:
14327 case GOMP_MAP_FORCE_TO:
14328 case GOMP_MAP_FORCE_DEVICEPTR:
14329 case GOMP_MAP_DEVICE_RESIDENT:
14330 break;
14332 case GOMP_MAP_LINK:
14333 if (!global_bindings_p ()
14334 && (TREE_STATIC (decl)
14335 || !DECL_EXTERNAL (decl)))
14337 error_at (loc,
14338 "%qD must be a global variable in "
14339 "%<#pragma acc declare link%>",
14340 decl);
14341 error = true;
14342 continue;
14344 break;
14346 default:
14347 if (global_bindings_p ())
14349 error_at (loc, "invalid OpenACC clause at file scope");
14350 error = true;
14351 continue;
14353 if (DECL_EXTERNAL (decl))
14355 error_at (loc,
14356 "invalid use of %<extern%> variable %qD "
14357 "in %<#pragma acc declare%>", decl);
14358 error = true;
14359 continue;
14361 else if (TREE_PUBLIC (decl))
14363 error_at (loc,
14364 "invalid use of %<global%> variable %qD "
14365 "in %<#pragma acc declare%>", decl);
14366 error = true;
14367 continue;
14369 break;
14372 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14373 || lookup_attribute ("omp declare target link",
14374 DECL_ATTRIBUTES (decl)))
14376 error_at (loc, "variable %qD used more than once with "
14377 "%<#pragma acc declare%>", decl);
14378 error = true;
14379 continue;
14382 if (!error)
14384 tree id;
14386 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14387 id = get_identifier ("omp declare target link");
14388 else
14389 id = get_identifier ("omp declare target");
14391 DECL_ATTRIBUTES (decl)
14392 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14394 if (global_bindings_p ())
14396 symtab_node *node = symtab_node::get (decl);
14397 if (node != NULL)
14399 node->offloadable = 1;
14400 if (ENABLE_OFFLOADING)
14402 g->have_offload = true;
14403 if (is_a <varpool_node *> (node))
14404 vec_safe_push (offload_vars, decl);
14411 if (error || global_bindings_p ())
14412 return;
14414 stmt = make_node (OACC_DECLARE);
14415 TREE_TYPE (stmt) = void_type_node;
14416 OACC_DECLARE_CLAUSES (stmt) = clauses;
14417 SET_EXPR_LOCATION (stmt, pragma_loc);
14419 add_stmt (stmt);
14421 return;
14424 /* OpenACC 2.0:
14425 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14429 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14432 LOC is the location of the #pragma token.
14435 #define OACC_ENTER_DATA_CLAUSE_MASK \
14436 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14444 #define OACC_EXIT_DATA_CLAUSE_MASK \
14445 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14451 static void
14452 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14454 location_t loc = c_parser_peek_token (parser)->location;
14455 tree clauses, stmt;
14456 const char *p = "";
14458 c_parser_consume_pragma (parser);
14460 if (c_parser_next_token_is (parser, CPP_NAME))
14462 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14463 c_parser_consume_token (parser);
14466 if (strcmp (p, "data") != 0)
14468 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14469 enter ? "enter" : "exit");
14470 parser->error = true;
14471 c_parser_skip_to_pragma_eol (parser);
14472 return;
14475 if (enter)
14476 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14477 "#pragma acc enter data");
14478 else
14479 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14480 "#pragma acc exit data");
14482 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14484 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14485 enter ? "enter" : "exit");
14486 return;
14489 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14490 TREE_TYPE (stmt) = void_type_node;
14491 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14492 SET_EXPR_LOCATION (stmt, loc);
14493 add_stmt (stmt);
14497 /* OpenACC 2.0:
14498 # pragma acc host_data oacc-data-clause[optseq] new-line
14499 structured-block
14502 #define OACC_HOST_DATA_CLAUSE_MASK \
14503 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14505 static tree
14506 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14508 tree stmt, clauses, block;
14510 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14511 "#pragma acc host_data");
14513 block = c_begin_omp_parallel ();
14514 add_stmt (c_parser_omp_structured_block (parser, if_p));
14515 stmt = c_finish_oacc_host_data (loc, clauses, block);
14516 return stmt;
14520 /* OpenACC 2.0:
14522 # pragma acc loop oacc-loop-clause[optseq] new-line
14523 structured-block
14525 LOC is the location of the #pragma token.
14528 #define OACC_LOOP_CLAUSE_MASK \
14529 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14539 static tree
14540 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14541 omp_clause_mask mask, tree *cclauses, bool *if_p)
14543 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14545 strcat (p_name, " loop");
14546 mask |= OACC_LOOP_CLAUSE_MASK;
14548 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14549 cclauses == NULL);
14550 if (cclauses)
14552 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14553 if (*cclauses)
14554 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14555 if (clauses)
14556 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14559 tree block = c_begin_compound_stmt (true);
14560 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14561 if_p);
14562 block = c_end_compound_stmt (loc, block, true);
14563 add_stmt (block);
14565 return stmt;
14568 /* OpenACC 2.0:
14569 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14570 structured-block
14574 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14575 structured-block
14577 LOC is the location of the #pragma token.
14580 #define OACC_KERNELS_CLAUSE_MASK \
14581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14599 #define OACC_PARALLEL_CLAUSE_MASK \
14600 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14621 static tree
14622 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14623 enum pragma_kind p_kind, char *p_name,
14624 bool *if_p)
14626 omp_clause_mask mask;
14627 enum tree_code code;
14628 switch (p_kind)
14630 case PRAGMA_OACC_KERNELS:
14631 strcat (p_name, " kernels");
14632 mask = OACC_KERNELS_CLAUSE_MASK;
14633 code = OACC_KERNELS;
14634 break;
14635 case PRAGMA_OACC_PARALLEL:
14636 strcat (p_name, " parallel");
14637 mask = OACC_PARALLEL_CLAUSE_MASK;
14638 code = OACC_PARALLEL;
14639 break;
14640 default:
14641 gcc_unreachable ();
14644 if (c_parser_next_token_is (parser, CPP_NAME))
14646 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14647 if (strcmp (p, "loop") == 0)
14649 c_parser_consume_token (parser);
14650 tree block = c_begin_omp_parallel ();
14651 tree clauses;
14652 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14653 return c_finish_omp_construct (loc, code, block, clauses);
14657 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14659 tree block = c_begin_omp_parallel ();
14660 add_stmt (c_parser_omp_structured_block (parser, if_p));
14662 return c_finish_omp_construct (loc, code, block, clauses);
14665 /* OpenACC 2.0:
14666 # pragma acc routine oacc-routine-clause[optseq] new-line
14667 function-definition
14669 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14672 #define OACC_ROUTINE_CLAUSE_MASK \
14673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14678 /* Parse an OpenACC routine directive. For named directives, we apply
14679 immediately to the named function. For unnamed ones we then parse
14680 a declaration or definition, which must be for a function. */
14682 static void
14683 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14685 gcc_checking_assert (context == pragma_external);
14687 oacc_routine_data data;
14688 data.error_seen = false;
14689 data.fndecl_seen = false;
14690 data.clauses = NULL_TREE;
14691 data.loc = c_parser_peek_token (parser)->location;
14693 c_parser_consume_pragma (parser);
14695 /* Look for optional '( name )'. */
14696 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14698 c_parser_consume_token (parser); /* '(' */
14700 tree decl = NULL_TREE;
14701 c_token *name_token = c_parser_peek_token (parser);
14702 location_t name_loc = name_token->location;
14703 if (name_token->type == CPP_NAME
14704 && (name_token->id_kind == C_ID_ID
14705 || name_token->id_kind == C_ID_TYPENAME))
14707 decl = lookup_name (name_token->value);
14708 if (!decl)
14709 error_at (name_loc,
14710 "%qE has not been declared", name_token->value);
14711 c_parser_consume_token (parser);
14713 else
14714 c_parser_error (parser, "expected function name");
14716 if (!decl
14717 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14719 c_parser_skip_to_pragma_eol (parser, false);
14720 return;
14723 data.clauses
14724 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14725 "#pragma acc routine");
14727 if (TREE_CODE (decl) != FUNCTION_DECL)
14729 error_at (name_loc, "%qD does not refer to a function", decl);
14730 return;
14733 c_finish_oacc_routine (&data, decl, false);
14735 else /* No optional '( name )'. */
14737 data.clauses
14738 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14739 "#pragma acc routine");
14741 /* Emit a helpful diagnostic if there's another pragma following this
14742 one. Also don't allow a static assertion declaration, as in the
14743 following we'll just parse a *single* "declaration or function
14744 definition", and the static assertion counts an one. */
14745 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14746 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14748 error_at (data.loc,
14749 "%<#pragma acc routine%> not immediately followed by"
14750 " function declaration or definition");
14751 /* ..., and then just keep going. */
14752 return;
14755 /* We only have to consider the pragma_external case here. */
14756 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14757 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14759 int ext = disable_extension_diagnostics ();
14761 c_parser_consume_token (parser);
14762 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14763 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14764 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14765 NULL, vNULL, &data);
14766 restore_extension_diagnostics (ext);
14768 else
14769 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14770 NULL, vNULL, &data);
14774 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14775 IS_DEFN is true if we're applying it to the definition. */
14777 static void
14778 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14779 bool is_defn)
14781 /* Keep going if we're in error reporting mode. */
14782 if (data->error_seen
14783 || fndecl == error_mark_node)
14784 return;
14786 if (data->fndecl_seen)
14788 error_at (data->loc,
14789 "%<#pragma acc routine%> not immediately followed by"
14790 " a single function declaration or definition");
14791 data->error_seen = true;
14792 return;
14794 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14796 error_at (data->loc,
14797 "%<#pragma acc routine%> not immediately followed by"
14798 " function declaration or definition");
14799 data->error_seen = true;
14800 return;
14803 if (oacc_get_fn_attrib (fndecl))
14805 error_at (data->loc,
14806 "%<#pragma acc routine%> already applied to %qD", fndecl);
14807 data->error_seen = true;
14808 return;
14811 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14813 error_at (data->loc,
14814 TREE_USED (fndecl)
14815 ? G_("%<#pragma acc routine%> must be applied before use")
14816 : G_("%<#pragma acc routine%> must be applied before "
14817 "definition"));
14818 data->error_seen = true;
14819 return;
14822 /* Process the routine's dimension clauses. */
14823 tree dims = oacc_build_routine_dims (data->clauses);
14824 oacc_replace_fn_attrib (fndecl, dims);
14826 /* Add an "omp declare target" attribute. */
14827 DECL_ATTRIBUTES (fndecl)
14828 = tree_cons (get_identifier ("omp declare target"),
14829 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14831 /* Remember that we've used this "#pragma acc routine". */
14832 data->fndecl_seen = true;
14835 /* OpenACC 2.0:
14836 # pragma acc update oacc-update-clause[optseq] new-line
14839 #define OACC_UPDATE_CLAUSE_MASK \
14840 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14847 static void
14848 c_parser_oacc_update (c_parser *parser)
14850 location_t loc = c_parser_peek_token (parser)->location;
14852 c_parser_consume_pragma (parser);
14854 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14855 "#pragma acc update");
14856 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14858 error_at (loc,
14859 "%<#pragma acc update%> must contain at least one "
14860 "%<device%> or %<host%> or %<self%> clause");
14861 return;
14864 if (parser->error)
14865 return;
14867 tree stmt = make_node (OACC_UPDATE);
14868 TREE_TYPE (stmt) = void_type_node;
14869 OACC_UPDATE_CLAUSES (stmt) = clauses;
14870 SET_EXPR_LOCATION (stmt, loc);
14871 add_stmt (stmt);
14874 /* OpenACC 2.0:
14875 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14877 LOC is the location of the #pragma token.
14880 #define OACC_WAIT_CLAUSE_MASK \
14881 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14883 static tree
14884 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14886 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14888 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14889 list = c_parser_oacc_wait_list (parser, loc, list);
14891 strcpy (p_name, " wait");
14892 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14893 stmt = c_finish_oacc_wait (loc, list, clauses);
14894 add_stmt (stmt);
14896 return stmt;
14899 /* OpenMP 2.5:
14900 # pragma omp atomic new-line
14901 expression-stmt
14903 expression-stmt:
14904 x binop= expr | x++ | ++x | x-- | --x
14905 binop:
14906 +, *, -, /, &, ^, |, <<, >>
14908 where x is an lvalue expression with scalar type.
14910 OpenMP 3.1:
14911 # pragma omp atomic new-line
14912 update-stmt
14914 # pragma omp atomic read new-line
14915 read-stmt
14917 # pragma omp atomic write new-line
14918 write-stmt
14920 # pragma omp atomic update new-line
14921 update-stmt
14923 # pragma omp atomic capture new-line
14924 capture-stmt
14926 # pragma omp atomic capture new-line
14927 capture-block
14929 read-stmt:
14930 v = x
14931 write-stmt:
14932 x = expr
14933 update-stmt:
14934 expression-stmt | x = x binop expr
14935 capture-stmt:
14936 v = expression-stmt
14937 capture-block:
14938 { v = x; update-stmt; } | { update-stmt; v = x; }
14940 OpenMP 4.0:
14941 update-stmt:
14942 expression-stmt | x = x binop expr | x = expr binop x
14943 capture-stmt:
14944 v = update-stmt
14945 capture-block:
14946 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14948 where x and v are lvalue expressions with scalar type.
14950 LOC is the location of the #pragma token. */
14952 static void
14953 c_parser_omp_atomic (location_t loc, c_parser *parser)
14955 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14956 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14957 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14958 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14959 struct c_expr expr;
14960 location_t eloc;
14961 bool structured_block = false;
14962 bool swapped = false;
14963 bool seq_cst = false;
14964 bool non_lvalue_p;
14966 if (c_parser_next_token_is (parser, CPP_NAME))
14968 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14969 if (!strcmp (p, "seq_cst"))
14971 seq_cst = true;
14972 c_parser_consume_token (parser);
14973 if (c_parser_next_token_is (parser, CPP_COMMA)
14974 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14975 c_parser_consume_token (parser);
14978 if (c_parser_next_token_is (parser, CPP_NAME))
14980 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14982 if (!strcmp (p, "read"))
14983 code = OMP_ATOMIC_READ;
14984 else if (!strcmp (p, "write"))
14985 code = NOP_EXPR;
14986 else if (!strcmp (p, "update"))
14987 code = OMP_ATOMIC;
14988 else if (!strcmp (p, "capture"))
14989 code = OMP_ATOMIC_CAPTURE_NEW;
14990 else
14991 p = NULL;
14992 if (p)
14993 c_parser_consume_token (parser);
14995 if (!seq_cst)
14997 if (c_parser_next_token_is (parser, CPP_COMMA)
14998 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14999 c_parser_consume_token (parser);
15001 if (c_parser_next_token_is (parser, CPP_NAME))
15003 const char *p
15004 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15005 if (!strcmp (p, "seq_cst"))
15007 seq_cst = true;
15008 c_parser_consume_token (parser);
15012 c_parser_skip_to_pragma_eol (parser);
15014 switch (code)
15016 case OMP_ATOMIC_READ:
15017 case NOP_EXPR: /* atomic write */
15018 v = c_parser_cast_expression (parser, NULL).value;
15019 non_lvalue_p = !lvalue_p (v);
15020 v = c_fully_fold (v, false, NULL, true);
15021 if (v == error_mark_node)
15022 goto saw_error;
15023 if (non_lvalue_p)
15024 v = non_lvalue (v);
15025 loc = c_parser_peek_token (parser)->location;
15026 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15027 goto saw_error;
15028 if (code == NOP_EXPR)
15030 lhs = c_parser_expression (parser).value;
15031 lhs = c_fully_fold (lhs, false, NULL);
15032 if (lhs == error_mark_node)
15033 goto saw_error;
15035 else
15037 lhs = c_parser_cast_expression (parser, NULL).value;
15038 non_lvalue_p = !lvalue_p (lhs);
15039 lhs = c_fully_fold (lhs, false, NULL, true);
15040 if (lhs == error_mark_node)
15041 goto saw_error;
15042 if (non_lvalue_p)
15043 lhs = non_lvalue (lhs);
15045 if (code == NOP_EXPR)
15047 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15048 opcode. */
15049 code = OMP_ATOMIC;
15050 rhs = lhs;
15051 lhs = v;
15052 v = NULL_TREE;
15054 goto done;
15055 case OMP_ATOMIC_CAPTURE_NEW:
15056 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15058 c_parser_consume_token (parser);
15059 structured_block = true;
15061 else
15063 v = c_parser_cast_expression (parser, NULL).value;
15064 non_lvalue_p = !lvalue_p (v);
15065 v = c_fully_fold (v, false, NULL, true);
15066 if (v == error_mark_node)
15067 goto saw_error;
15068 if (non_lvalue_p)
15069 v = non_lvalue (v);
15070 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15071 goto saw_error;
15073 break;
15074 default:
15075 break;
15078 /* For structured_block case we don't know yet whether
15079 old or new x should be captured. */
15080 restart:
15081 eloc = c_parser_peek_token (parser)->location;
15082 expr = c_parser_cast_expression (parser, NULL);
15083 lhs = expr.value;
15084 expr = default_function_array_conversion (eloc, expr);
15085 unfolded_lhs = expr.value;
15086 lhs = c_fully_fold (lhs, false, NULL, true);
15087 orig_lhs = lhs;
15088 switch (TREE_CODE (lhs))
15090 case ERROR_MARK:
15091 saw_error:
15092 c_parser_skip_to_end_of_block_or_statement (parser);
15093 if (structured_block)
15095 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15096 c_parser_consume_token (parser);
15097 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15099 c_parser_skip_to_end_of_block_or_statement (parser);
15100 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15101 c_parser_consume_token (parser);
15104 return;
15106 case POSTINCREMENT_EXPR:
15107 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15108 code = OMP_ATOMIC_CAPTURE_OLD;
15109 /* FALLTHROUGH */
15110 case PREINCREMENT_EXPR:
15111 lhs = TREE_OPERAND (lhs, 0);
15112 unfolded_lhs = NULL_TREE;
15113 opcode = PLUS_EXPR;
15114 rhs = integer_one_node;
15115 break;
15117 case POSTDECREMENT_EXPR:
15118 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15119 code = OMP_ATOMIC_CAPTURE_OLD;
15120 /* FALLTHROUGH */
15121 case PREDECREMENT_EXPR:
15122 lhs = TREE_OPERAND (lhs, 0);
15123 unfolded_lhs = NULL_TREE;
15124 opcode = MINUS_EXPR;
15125 rhs = integer_one_node;
15126 break;
15128 case COMPOUND_EXPR:
15129 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15130 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15131 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15132 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15133 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15134 (TREE_OPERAND (lhs, 1), 0), 0)))
15135 == BOOLEAN_TYPE)
15136 /* Undo effects of boolean_increment for post {in,de}crement. */
15137 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15138 /* FALLTHRU */
15139 case MODIFY_EXPR:
15140 if (TREE_CODE (lhs) == MODIFY_EXPR
15141 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15143 /* Undo effects of boolean_increment. */
15144 if (integer_onep (TREE_OPERAND (lhs, 1)))
15146 /* This is pre or post increment. */
15147 rhs = TREE_OPERAND (lhs, 1);
15148 lhs = TREE_OPERAND (lhs, 0);
15149 unfolded_lhs = NULL_TREE;
15150 opcode = NOP_EXPR;
15151 if (code == OMP_ATOMIC_CAPTURE_NEW
15152 && !structured_block
15153 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15154 code = OMP_ATOMIC_CAPTURE_OLD;
15155 break;
15157 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15158 && TREE_OPERAND (lhs, 0)
15159 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15161 /* This is pre or post decrement. */
15162 rhs = TREE_OPERAND (lhs, 1);
15163 lhs = TREE_OPERAND (lhs, 0);
15164 unfolded_lhs = NULL_TREE;
15165 opcode = NOP_EXPR;
15166 if (code == OMP_ATOMIC_CAPTURE_NEW
15167 && !structured_block
15168 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15169 code = OMP_ATOMIC_CAPTURE_OLD;
15170 break;
15173 /* FALLTHRU */
15174 default:
15175 if (!lvalue_p (unfolded_lhs))
15176 lhs = non_lvalue (lhs);
15177 switch (c_parser_peek_token (parser)->type)
15179 case CPP_MULT_EQ:
15180 opcode = MULT_EXPR;
15181 break;
15182 case CPP_DIV_EQ:
15183 opcode = TRUNC_DIV_EXPR;
15184 break;
15185 case CPP_PLUS_EQ:
15186 opcode = PLUS_EXPR;
15187 break;
15188 case CPP_MINUS_EQ:
15189 opcode = MINUS_EXPR;
15190 break;
15191 case CPP_LSHIFT_EQ:
15192 opcode = LSHIFT_EXPR;
15193 break;
15194 case CPP_RSHIFT_EQ:
15195 opcode = RSHIFT_EXPR;
15196 break;
15197 case CPP_AND_EQ:
15198 opcode = BIT_AND_EXPR;
15199 break;
15200 case CPP_OR_EQ:
15201 opcode = BIT_IOR_EXPR;
15202 break;
15203 case CPP_XOR_EQ:
15204 opcode = BIT_XOR_EXPR;
15205 break;
15206 case CPP_EQ:
15207 c_parser_consume_token (parser);
15208 eloc = c_parser_peek_token (parser)->location;
15209 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15210 rhs1 = expr.value;
15211 switch (TREE_CODE (rhs1))
15213 case MULT_EXPR:
15214 case TRUNC_DIV_EXPR:
15215 case RDIV_EXPR:
15216 case PLUS_EXPR:
15217 case MINUS_EXPR:
15218 case LSHIFT_EXPR:
15219 case RSHIFT_EXPR:
15220 case BIT_AND_EXPR:
15221 case BIT_IOR_EXPR:
15222 case BIT_XOR_EXPR:
15223 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15225 opcode = TREE_CODE (rhs1);
15226 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15227 true);
15228 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15229 true);
15230 goto stmt_done;
15232 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15234 opcode = TREE_CODE (rhs1);
15235 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15236 true);
15237 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15238 true);
15239 swapped = !commutative_tree_code (opcode);
15240 goto stmt_done;
15242 break;
15243 case ERROR_MARK:
15244 goto saw_error;
15245 default:
15246 break;
15248 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15250 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15252 code = OMP_ATOMIC_CAPTURE_OLD;
15253 v = lhs;
15254 lhs = NULL_TREE;
15255 expr = default_function_array_read_conversion (eloc, expr);
15256 unfolded_lhs1 = expr.value;
15257 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15258 rhs1 = NULL_TREE;
15259 c_parser_consume_token (parser);
15260 goto restart;
15262 if (structured_block)
15264 opcode = NOP_EXPR;
15265 expr = default_function_array_read_conversion (eloc, expr);
15266 rhs = c_fully_fold (expr.value, false, NULL, true);
15267 rhs1 = NULL_TREE;
15268 goto stmt_done;
15271 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15272 goto saw_error;
15273 default:
15274 c_parser_error (parser,
15275 "invalid operator for %<#pragma omp atomic%>");
15276 goto saw_error;
15279 /* Arrange to pass the location of the assignment operator to
15280 c_finish_omp_atomic. */
15281 loc = c_parser_peek_token (parser)->location;
15282 c_parser_consume_token (parser);
15283 eloc = c_parser_peek_token (parser)->location;
15284 expr = c_parser_expression (parser);
15285 expr = default_function_array_read_conversion (eloc, expr);
15286 rhs = expr.value;
15287 rhs = c_fully_fold (rhs, false, NULL, true);
15288 break;
15290 stmt_done:
15291 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15293 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15294 goto saw_error;
15295 v = c_parser_cast_expression (parser, NULL).value;
15296 non_lvalue_p = !lvalue_p (v);
15297 v = c_fully_fold (v, false, NULL, true);
15298 if (v == error_mark_node)
15299 goto saw_error;
15300 if (non_lvalue_p)
15301 v = non_lvalue (v);
15302 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15303 goto saw_error;
15304 eloc = c_parser_peek_token (parser)->location;
15305 expr = c_parser_cast_expression (parser, NULL);
15306 lhs1 = expr.value;
15307 expr = default_function_array_read_conversion (eloc, expr);
15308 unfolded_lhs1 = expr.value;
15309 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15310 if (lhs1 == error_mark_node)
15311 goto saw_error;
15312 if (!lvalue_p (unfolded_lhs1))
15313 lhs1 = non_lvalue (lhs1);
15315 if (structured_block)
15317 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15318 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15320 done:
15321 if (unfolded_lhs && unfolded_lhs1
15322 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15324 error ("%<#pragma omp atomic capture%> uses two different "
15325 "expressions for memory");
15326 stmt = error_mark_node;
15328 else
15329 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15330 swapped, seq_cst);
15331 if (stmt != error_mark_node)
15332 add_stmt (stmt);
15334 if (!structured_block)
15335 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15339 /* OpenMP 2.5:
15340 # pragma omp barrier new-line
15343 static void
15344 c_parser_omp_barrier (c_parser *parser)
15346 location_t loc = c_parser_peek_token (parser)->location;
15347 c_parser_consume_pragma (parser);
15348 c_parser_skip_to_pragma_eol (parser);
15350 c_finish_omp_barrier (loc);
15353 /* OpenMP 2.5:
15354 # pragma omp critical [(name)] new-line
15355 structured-block
15357 OpenMP 4.5:
15358 # pragma omp critical [(name) [hint(expression)]] new-line
15360 LOC is the location of the #pragma itself. */
15362 #define OMP_CRITICAL_CLAUSE_MASK \
15363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15365 static tree
15366 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15368 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15370 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15372 c_parser_consume_token (parser);
15373 if (c_parser_next_token_is (parser, CPP_NAME))
15375 name = c_parser_peek_token (parser)->value;
15376 c_parser_consume_token (parser);
15377 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15379 else
15380 c_parser_error (parser, "expected identifier");
15382 clauses = c_parser_omp_all_clauses (parser,
15383 OMP_CRITICAL_CLAUSE_MASK,
15384 "#pragma omp critical");
15386 else
15388 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15389 c_parser_error (parser, "expected %<(%> or end of line");
15390 c_parser_skip_to_pragma_eol (parser);
15393 stmt = c_parser_omp_structured_block (parser, if_p);
15394 return c_finish_omp_critical (loc, stmt, name, clauses);
15397 /* OpenMP 2.5:
15398 # pragma omp flush flush-vars[opt] new-line
15400 flush-vars:
15401 ( variable-list ) */
15403 static void
15404 c_parser_omp_flush (c_parser *parser)
15406 location_t loc = c_parser_peek_token (parser)->location;
15407 c_parser_consume_pragma (parser);
15408 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15409 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15410 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15411 c_parser_error (parser, "expected %<(%> or end of line");
15412 c_parser_skip_to_pragma_eol (parser);
15414 c_finish_omp_flush (loc);
15417 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15418 The real trick here is to determine the loop control variable early
15419 so that we can push a new decl if necessary to make it private.
15420 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15421 respectively. */
15423 static tree
15424 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15425 tree clauses, tree *cclauses, bool *if_p)
15427 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15428 tree declv, condv, incrv, initv, ret = NULL_TREE;
15429 tree pre_body = NULL_TREE, this_pre_body;
15430 tree ordered_cl = NULL_TREE;
15431 bool fail = false, open_brace_parsed = false;
15432 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15433 location_t for_loc;
15434 bool tiling = false;
15435 vec<tree, va_gc> *for_block = make_tree_vector ();
15437 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15438 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15439 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15440 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15442 tiling = true;
15443 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15445 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15446 && OMP_CLAUSE_ORDERED_EXPR (cl))
15448 ordered_cl = cl;
15449 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15452 if (ordered && ordered < collapse)
15454 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15455 "%<ordered%> clause parameter is less than %<collapse%>");
15456 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15457 = build_int_cst (NULL_TREE, collapse);
15458 ordered = collapse;
15460 if (ordered)
15462 for (tree *pc = &clauses; *pc; )
15463 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15465 error_at (OMP_CLAUSE_LOCATION (*pc),
15466 "%<linear%> clause may not be specified together "
15467 "with %<ordered%> clause with a parameter");
15468 *pc = OMP_CLAUSE_CHAIN (*pc);
15470 else
15471 pc = &OMP_CLAUSE_CHAIN (*pc);
15474 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15475 count = ordered ? ordered : collapse;
15477 declv = make_tree_vec (count);
15478 initv = make_tree_vec (count);
15479 condv = make_tree_vec (count);
15480 incrv = make_tree_vec (count);
15482 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15484 c_parser_error (parser, "for statement expected");
15485 return NULL;
15487 for_loc = c_parser_peek_token (parser)->location;
15488 c_parser_consume_token (parser);
15490 for (i = 0; i < count; i++)
15492 int bracecount = 0;
15494 matching_parens parens;
15495 if (!parens.require_open (parser))
15496 goto pop_scopes;
15498 /* Parse the initialization declaration or expression. */
15499 if (c_parser_next_tokens_start_declaration (parser))
15501 if (i > 0)
15502 vec_safe_push (for_block, c_begin_compound_stmt (true));
15503 this_pre_body = push_stmt_list ();
15504 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15505 NULL, vNULL);
15506 if (this_pre_body)
15508 this_pre_body = pop_stmt_list (this_pre_body);
15509 if (pre_body)
15511 tree t = pre_body;
15512 pre_body = push_stmt_list ();
15513 add_stmt (t);
15514 add_stmt (this_pre_body);
15515 pre_body = pop_stmt_list (pre_body);
15517 else
15518 pre_body = this_pre_body;
15520 decl = check_for_loop_decls (for_loc, flag_isoc99);
15521 if (decl == NULL)
15522 goto error_init;
15523 if (DECL_INITIAL (decl) == error_mark_node)
15524 decl = error_mark_node;
15525 init = decl;
15527 else if (c_parser_next_token_is (parser, CPP_NAME)
15528 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15530 struct c_expr decl_exp;
15531 struct c_expr init_exp;
15532 location_t init_loc;
15534 decl_exp = c_parser_postfix_expression (parser);
15535 decl = decl_exp.value;
15537 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15539 init_loc = c_parser_peek_token (parser)->location;
15540 init_exp = c_parser_expr_no_commas (parser, NULL);
15541 init_exp = default_function_array_read_conversion (init_loc,
15542 init_exp);
15543 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15544 NOP_EXPR, init_loc, init_exp.value,
15545 init_exp.original_type);
15546 init = c_process_expr_stmt (init_loc, init);
15548 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15550 else
15552 error_init:
15553 c_parser_error (parser,
15554 "expected iteration declaration or initialization");
15555 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15556 "expected %<)%>");
15557 fail = true;
15558 goto parse_next;
15561 /* Parse the loop condition. */
15562 cond = NULL_TREE;
15563 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15565 location_t cond_loc = c_parser_peek_token (parser)->location;
15566 struct c_expr cond_expr
15567 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15569 cond = cond_expr.value;
15570 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15571 if (COMPARISON_CLASS_P (cond))
15573 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15574 op0 = c_fully_fold (op0, false, NULL);
15575 op1 = c_fully_fold (op1, false, NULL);
15576 TREE_OPERAND (cond, 0) = op0;
15577 TREE_OPERAND (cond, 1) = op1;
15579 switch (cond_expr.original_code)
15581 case GT_EXPR:
15582 case GE_EXPR:
15583 case LT_EXPR:
15584 case LE_EXPR:
15585 break;
15586 default:
15587 /* Can't be cond = error_mark_node, because we want to preserve
15588 the location until c_finish_omp_for. */
15589 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15590 break;
15592 protected_set_expr_location (cond, cond_loc);
15594 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15596 /* Parse the increment expression. */
15597 incr = NULL_TREE;
15598 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15600 location_t incr_loc = c_parser_peek_token (parser)->location;
15602 incr = c_process_expr_stmt (incr_loc,
15603 c_parser_expression (parser).value);
15605 parens.skip_until_found_close (parser);
15607 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15608 fail = true;
15609 else
15611 TREE_VEC_ELT (declv, i) = decl;
15612 TREE_VEC_ELT (initv, i) = init;
15613 TREE_VEC_ELT (condv, i) = cond;
15614 TREE_VEC_ELT (incrv, i) = incr;
15617 parse_next:
15618 if (i == count - 1)
15619 break;
15621 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15622 in between the collapsed for loops to be still considered perfectly
15623 nested. Hopefully the final version clarifies this.
15624 For now handle (multiple) {'s and empty statements. */
15627 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15629 c_parser_consume_token (parser);
15630 break;
15632 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15634 c_parser_consume_token (parser);
15635 bracecount++;
15637 else if (bracecount
15638 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15639 c_parser_consume_token (parser);
15640 else
15642 c_parser_error (parser, "not enough perfectly nested loops");
15643 if (bracecount)
15645 open_brace_parsed = true;
15646 bracecount--;
15648 fail = true;
15649 count = 0;
15650 break;
15653 while (1);
15655 nbraces += bracecount;
15658 if (nbraces)
15659 if_p = NULL;
15661 save_break = c_break_label;
15662 c_break_label = size_one_node;
15663 save_cont = c_cont_label;
15664 c_cont_label = NULL_TREE;
15665 body = push_stmt_list ();
15667 if (open_brace_parsed)
15669 location_t here = c_parser_peek_token (parser)->location;
15670 stmt = c_begin_compound_stmt (true);
15671 c_parser_compound_statement_nostart (parser);
15672 add_stmt (c_end_compound_stmt (here, stmt, true));
15674 else
15675 add_stmt (c_parser_c99_block_statement (parser, if_p));
15676 if (c_cont_label)
15678 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15679 SET_EXPR_LOCATION (t, loc);
15680 add_stmt (t);
15683 body = pop_stmt_list (body);
15684 c_break_label = save_break;
15685 c_cont_label = save_cont;
15687 while (nbraces)
15689 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15691 c_parser_consume_token (parser);
15692 nbraces--;
15694 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15695 c_parser_consume_token (parser);
15696 else
15698 c_parser_error (parser, "collapsed loops not perfectly nested");
15699 while (nbraces)
15701 location_t here = c_parser_peek_token (parser)->location;
15702 stmt = c_begin_compound_stmt (true);
15703 add_stmt (body);
15704 c_parser_compound_statement_nostart (parser);
15705 body = c_end_compound_stmt (here, stmt, true);
15706 nbraces--;
15708 goto pop_scopes;
15712 /* Only bother calling c_finish_omp_for if we haven't already generated
15713 an error from the initialization parsing. */
15714 if (!fail)
15716 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15717 incrv, body, pre_body);
15719 /* Check for iterators appearing in lb, b or incr expressions. */
15720 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15721 stmt = NULL_TREE;
15723 if (stmt)
15725 add_stmt (stmt);
15727 if (cclauses != NULL
15728 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15730 tree *c;
15731 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15732 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15733 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15734 c = &OMP_CLAUSE_CHAIN (*c);
15735 else
15737 for (i = 0; i < count; i++)
15738 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15739 break;
15740 if (i == count)
15741 c = &OMP_CLAUSE_CHAIN (*c);
15742 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15744 error_at (loc,
15745 "iteration variable %qD should not be firstprivate",
15746 OMP_CLAUSE_DECL (*c));
15747 *c = OMP_CLAUSE_CHAIN (*c);
15749 else
15751 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15752 tree l = *c;
15753 *c = OMP_CLAUSE_CHAIN (*c);
15754 if (code == OMP_SIMD)
15756 OMP_CLAUSE_CHAIN (l)
15757 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15758 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15760 else
15762 OMP_CLAUSE_CHAIN (l) = clauses;
15763 clauses = l;
15768 OMP_FOR_CLAUSES (stmt) = clauses;
15770 ret = stmt;
15772 pop_scopes:
15773 while (!for_block->is_empty ())
15775 /* FIXME diagnostics: LOC below should be the actual location of
15776 this particular for block. We need to build a list of
15777 locations to go along with FOR_BLOCK. */
15778 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15779 add_stmt (stmt);
15781 release_tree_vector (for_block);
15782 return ret;
15785 /* Helper function for OpenMP parsing, split clauses and call
15786 finish_omp_clauses on each of the set of clauses afterwards. */
15788 static void
15789 omp_split_clauses (location_t loc, enum tree_code code,
15790 omp_clause_mask mask, tree clauses, tree *cclauses)
15792 int i;
15793 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15794 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15795 if (cclauses[i])
15796 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15799 /* OpenMP 4.0:
15800 #pragma omp simd simd-clause[optseq] new-line
15801 for-loop
15803 LOC is the location of the #pragma token.
15806 #define OMP_SIMD_CLAUSE_MASK \
15807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15816 static tree
15817 c_parser_omp_simd (location_t loc, c_parser *parser,
15818 char *p_name, omp_clause_mask mask, tree *cclauses,
15819 bool *if_p)
15821 tree block, clauses, ret;
15823 strcat (p_name, " simd");
15824 mask |= OMP_SIMD_CLAUSE_MASK;
15826 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15827 if (cclauses)
15829 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15830 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15831 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15832 OMP_CLAUSE_ORDERED);
15833 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15835 error_at (OMP_CLAUSE_LOCATION (c),
15836 "%<ordered%> clause with parameter may not be specified "
15837 "on %qs construct", p_name);
15838 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15842 block = c_begin_compound_stmt (true);
15843 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15844 block = c_end_compound_stmt (loc, block, true);
15845 add_stmt (block);
15847 return ret;
15850 /* OpenMP 2.5:
15851 #pragma omp for for-clause[optseq] new-line
15852 for-loop
15854 OpenMP 4.0:
15855 #pragma omp for simd for-simd-clause[optseq] new-line
15856 for-loop
15858 LOC is the location of the #pragma token.
15861 #define OMP_FOR_CLAUSE_MASK \
15862 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15872 static tree
15873 c_parser_omp_for (location_t loc, c_parser *parser,
15874 char *p_name, omp_clause_mask mask, tree *cclauses,
15875 bool *if_p)
15877 tree block, clauses, ret;
15879 strcat (p_name, " for");
15880 mask |= OMP_FOR_CLAUSE_MASK;
15881 /* parallel for{, simd} disallows nowait clause, but for
15882 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15883 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15884 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15885 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15886 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15887 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15889 if (c_parser_next_token_is (parser, CPP_NAME))
15891 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15893 if (strcmp (p, "simd") == 0)
15895 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15896 if (cclauses == NULL)
15897 cclauses = cclauses_buf;
15899 c_parser_consume_token (parser);
15900 if (!flag_openmp) /* flag_openmp_simd */
15901 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15902 if_p);
15903 block = c_begin_compound_stmt (true);
15904 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15905 block = c_end_compound_stmt (loc, block, true);
15906 if (ret == NULL_TREE)
15907 return ret;
15908 ret = make_node (OMP_FOR);
15909 TREE_TYPE (ret) = void_type_node;
15910 OMP_FOR_BODY (ret) = block;
15911 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15912 SET_EXPR_LOCATION (ret, loc);
15913 add_stmt (ret);
15914 return ret;
15917 if (!flag_openmp) /* flag_openmp_simd */
15919 c_parser_skip_to_pragma_eol (parser, false);
15920 return NULL_TREE;
15923 /* Composite distribute parallel for disallows linear clause. */
15924 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15925 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15927 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15928 if (cclauses)
15930 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15931 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15934 block = c_begin_compound_stmt (true);
15935 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15936 block = c_end_compound_stmt (loc, block, true);
15937 add_stmt (block);
15939 return ret;
15942 /* OpenMP 2.5:
15943 # pragma omp master new-line
15944 structured-block
15946 LOC is the location of the #pragma token.
15949 static tree
15950 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15952 c_parser_skip_to_pragma_eol (parser);
15953 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15954 if_p));
15957 /* OpenMP 2.5:
15958 # pragma omp ordered new-line
15959 structured-block
15961 OpenMP 4.5:
15962 # pragma omp ordered ordered-clauses new-line
15963 structured-block
15965 # pragma omp ordered depend-clauses new-line */
15967 #define OMP_ORDERED_CLAUSE_MASK \
15968 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15971 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15972 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15974 static bool
15975 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15976 bool *if_p)
15978 location_t loc = c_parser_peek_token (parser)->location;
15979 c_parser_consume_pragma (parser);
15981 if (context != pragma_stmt && context != pragma_compound)
15983 c_parser_error (parser, "expected declaration specifiers");
15984 c_parser_skip_to_pragma_eol (parser, false);
15985 return false;
15988 if (c_parser_next_token_is (parser, CPP_NAME))
15990 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15992 if (!strcmp ("depend", p))
15994 if (!flag_openmp) /* flag_openmp_simd */
15996 c_parser_skip_to_pragma_eol (parser, false);
15997 return false;
15999 if (context == pragma_stmt)
16001 error_at (loc,
16002 "%<#pragma omp ordered%> with %<depend%> clause may "
16003 "only be used in compound statements");
16004 c_parser_skip_to_pragma_eol (parser, false);
16005 return false;
16008 tree clauses
16009 = c_parser_omp_all_clauses (parser,
16010 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16011 "#pragma omp ordered");
16012 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16013 return false;
16017 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16018 "#pragma omp ordered");
16020 if (!flag_openmp /* flag_openmp_simd */
16021 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16022 return false;
16024 c_finish_omp_ordered (loc, clauses,
16025 c_parser_omp_structured_block (parser, if_p));
16026 return true;
16029 /* OpenMP 2.5:
16031 section-scope:
16032 { section-sequence }
16034 section-sequence:
16035 section-directive[opt] structured-block
16036 section-sequence section-directive structured-block
16038 SECTIONS_LOC is the location of the #pragma omp sections. */
16040 static tree
16041 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16043 tree stmt, substmt;
16044 bool error_suppress = false;
16045 location_t loc;
16047 loc = c_parser_peek_token (parser)->location;
16048 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16050 /* Avoid skipping until the end of the block. */
16051 parser->error = false;
16052 return NULL_TREE;
16055 stmt = push_stmt_list ();
16057 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16059 substmt = c_parser_omp_structured_block (parser, NULL);
16060 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16061 SET_EXPR_LOCATION (substmt, loc);
16062 add_stmt (substmt);
16065 while (1)
16067 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16068 break;
16069 if (c_parser_next_token_is (parser, CPP_EOF))
16070 break;
16072 loc = c_parser_peek_token (parser)->location;
16073 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16075 c_parser_consume_pragma (parser);
16076 c_parser_skip_to_pragma_eol (parser);
16077 error_suppress = false;
16079 else if (!error_suppress)
16081 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16082 error_suppress = true;
16085 substmt = c_parser_omp_structured_block (parser, NULL);
16086 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16087 SET_EXPR_LOCATION (substmt, loc);
16088 add_stmt (substmt);
16090 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16091 "expected %<#pragma omp section%> or %<}%>");
16093 substmt = pop_stmt_list (stmt);
16095 stmt = make_node (OMP_SECTIONS);
16096 SET_EXPR_LOCATION (stmt, sections_loc);
16097 TREE_TYPE (stmt) = void_type_node;
16098 OMP_SECTIONS_BODY (stmt) = substmt;
16100 return add_stmt (stmt);
16103 /* OpenMP 2.5:
16104 # pragma omp sections sections-clause[optseq] newline
16105 sections-scope
16107 LOC is the location of the #pragma token.
16110 #define OMP_SECTIONS_CLAUSE_MASK \
16111 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16117 static tree
16118 c_parser_omp_sections (location_t loc, c_parser *parser,
16119 char *p_name, omp_clause_mask mask, tree *cclauses)
16121 tree block, clauses, ret;
16123 strcat (p_name, " sections");
16124 mask |= OMP_SECTIONS_CLAUSE_MASK;
16125 if (cclauses)
16126 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16128 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16129 if (cclauses)
16131 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16132 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16135 block = c_begin_compound_stmt (true);
16136 ret = c_parser_omp_sections_scope (loc, parser);
16137 if (ret)
16138 OMP_SECTIONS_CLAUSES (ret) = clauses;
16139 block = c_end_compound_stmt (loc, block, true);
16140 add_stmt (block);
16142 return ret;
16145 /* OpenMP 2.5:
16146 # pragma omp parallel parallel-clause[optseq] new-line
16147 structured-block
16148 # pragma omp parallel for parallel-for-clause[optseq] new-line
16149 structured-block
16150 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16151 structured-block
16153 OpenMP 4.0:
16154 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16155 structured-block
16157 LOC is the location of the #pragma token.
16160 #define OMP_PARALLEL_CLAUSE_MASK \
16161 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16171 static tree
16172 c_parser_omp_parallel (location_t loc, c_parser *parser,
16173 char *p_name, omp_clause_mask mask, tree *cclauses,
16174 bool *if_p)
16176 tree stmt, clauses, block;
16178 strcat (p_name, " parallel");
16179 mask |= OMP_PARALLEL_CLAUSE_MASK;
16180 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16181 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16182 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16183 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16185 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16187 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16188 if (cclauses == NULL)
16189 cclauses = cclauses_buf;
16191 c_parser_consume_token (parser);
16192 if (!flag_openmp) /* flag_openmp_simd */
16193 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16194 block = c_begin_omp_parallel ();
16195 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16196 stmt
16197 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16198 block);
16199 if (ret == NULL_TREE)
16200 return ret;
16201 OMP_PARALLEL_COMBINED (stmt) = 1;
16202 return stmt;
16204 /* When combined with distribute, parallel has to be followed by for.
16205 #pragma omp target parallel is allowed though. */
16206 else if (cclauses
16207 && (mask & (OMP_CLAUSE_MASK_1
16208 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16210 error_at (loc, "expected %<for%> after %qs", p_name);
16211 c_parser_skip_to_pragma_eol (parser);
16212 return NULL_TREE;
16214 else if (!flag_openmp) /* flag_openmp_simd */
16216 c_parser_skip_to_pragma_eol (parser, false);
16217 return NULL_TREE;
16219 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16221 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16222 if (strcmp (p, "sections") == 0)
16224 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16225 if (cclauses == NULL)
16226 cclauses = cclauses_buf;
16228 c_parser_consume_token (parser);
16229 block = c_begin_omp_parallel ();
16230 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16231 stmt = c_finish_omp_parallel (loc,
16232 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16233 block);
16234 OMP_PARALLEL_COMBINED (stmt) = 1;
16235 return stmt;
16239 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16240 if (cclauses)
16242 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16243 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16246 block = c_begin_omp_parallel ();
16247 c_parser_statement (parser, if_p);
16248 stmt = c_finish_omp_parallel (loc, clauses, block);
16250 return stmt;
16253 /* OpenMP 2.5:
16254 # pragma omp single single-clause[optseq] new-line
16255 structured-block
16257 LOC is the location of the #pragma.
16260 #define OMP_SINGLE_CLAUSE_MASK \
16261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16266 static tree
16267 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16269 tree stmt = make_node (OMP_SINGLE);
16270 SET_EXPR_LOCATION (stmt, loc);
16271 TREE_TYPE (stmt) = void_type_node;
16273 OMP_SINGLE_CLAUSES (stmt)
16274 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16275 "#pragma omp single");
16276 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16278 return add_stmt (stmt);
16281 /* OpenMP 3.0:
16282 # pragma omp task task-clause[optseq] new-line
16284 LOC is the location of the #pragma.
16287 #define OMP_TASK_CLAUSE_MASK \
16288 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16299 static tree
16300 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16302 tree clauses, block;
16304 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16305 "#pragma omp task");
16307 block = c_begin_omp_task ();
16308 c_parser_statement (parser, if_p);
16309 return c_finish_omp_task (loc, clauses, block);
16312 /* OpenMP 3.0:
16313 # pragma omp taskwait new-line
16316 static void
16317 c_parser_omp_taskwait (c_parser *parser)
16319 location_t loc = c_parser_peek_token (parser)->location;
16320 c_parser_consume_pragma (parser);
16321 c_parser_skip_to_pragma_eol (parser);
16323 c_finish_omp_taskwait (loc);
16326 /* OpenMP 3.1:
16327 # pragma omp taskyield new-line
16330 static void
16331 c_parser_omp_taskyield (c_parser *parser)
16333 location_t loc = c_parser_peek_token (parser)->location;
16334 c_parser_consume_pragma (parser);
16335 c_parser_skip_to_pragma_eol (parser);
16337 c_finish_omp_taskyield (loc);
16340 /* OpenMP 4.0:
16341 # pragma omp taskgroup new-line
16344 static tree
16345 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16347 location_t loc = c_parser_peek_token (parser)->location;
16348 c_parser_skip_to_pragma_eol (parser);
16349 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16350 if_p));
16353 /* OpenMP 4.0:
16354 # pragma omp cancel cancel-clause[optseq] new-line
16356 LOC is the location of the #pragma.
16359 #define OMP_CANCEL_CLAUSE_MASK \
16360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16366 static void
16367 c_parser_omp_cancel (c_parser *parser)
16369 location_t loc = c_parser_peek_token (parser)->location;
16371 c_parser_consume_pragma (parser);
16372 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16373 "#pragma omp cancel");
16375 c_finish_omp_cancel (loc, clauses);
16378 /* OpenMP 4.0:
16379 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16381 LOC is the location of the #pragma.
16384 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16385 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16390 static void
16391 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16393 location_t loc = c_parser_peek_token (parser)->location;
16394 tree clauses;
16395 bool point_seen = false;
16397 c_parser_consume_pragma (parser);
16398 if (c_parser_next_token_is (parser, CPP_NAME))
16400 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16401 if (strcmp (p, "point") == 0)
16403 c_parser_consume_token (parser);
16404 point_seen = true;
16407 if (!point_seen)
16409 c_parser_error (parser, "expected %<point%>");
16410 c_parser_skip_to_pragma_eol (parser);
16411 return;
16414 if (context != pragma_compound)
16416 if (context == pragma_stmt)
16417 error_at (loc,
16418 "%<#pragma %s%> may only be used in compound statements",
16419 "omp cancellation point");
16420 else
16421 c_parser_error (parser, "expected declaration specifiers");
16422 c_parser_skip_to_pragma_eol (parser, false);
16423 return;
16426 clauses
16427 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16428 "#pragma omp cancellation point");
16430 c_finish_omp_cancellation_point (loc, clauses);
16433 /* OpenMP 4.0:
16434 #pragma omp distribute distribute-clause[optseq] new-line
16435 for-loop */
16437 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16438 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16444 static tree
16445 c_parser_omp_distribute (location_t loc, c_parser *parser,
16446 char *p_name, omp_clause_mask mask, tree *cclauses,
16447 bool *if_p)
16449 tree clauses, block, ret;
16451 strcat (p_name, " distribute");
16452 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16454 if (c_parser_next_token_is (parser, CPP_NAME))
16456 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16457 bool simd = false;
16458 bool parallel = false;
16460 if (strcmp (p, "simd") == 0)
16461 simd = true;
16462 else
16463 parallel = strcmp (p, "parallel") == 0;
16464 if (parallel || simd)
16466 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16467 if (cclauses == NULL)
16468 cclauses = cclauses_buf;
16469 c_parser_consume_token (parser);
16470 if (!flag_openmp) /* flag_openmp_simd */
16472 if (simd)
16473 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16474 if_p);
16475 else
16476 return c_parser_omp_parallel (loc, parser, p_name, mask,
16477 cclauses, if_p);
16479 block = c_begin_compound_stmt (true);
16480 if (simd)
16481 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16482 if_p);
16483 else
16484 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16485 if_p);
16486 block = c_end_compound_stmt (loc, block, true);
16487 if (ret == NULL)
16488 return ret;
16489 ret = make_node (OMP_DISTRIBUTE);
16490 TREE_TYPE (ret) = void_type_node;
16491 OMP_FOR_BODY (ret) = block;
16492 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16493 SET_EXPR_LOCATION (ret, loc);
16494 add_stmt (ret);
16495 return ret;
16498 if (!flag_openmp) /* flag_openmp_simd */
16500 c_parser_skip_to_pragma_eol (parser, false);
16501 return NULL_TREE;
16504 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16505 if (cclauses)
16507 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16508 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16511 block = c_begin_compound_stmt (true);
16512 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16513 if_p);
16514 block = c_end_compound_stmt (loc, block, true);
16515 add_stmt (block);
16517 return ret;
16520 /* OpenMP 4.0:
16521 # pragma omp teams teams-clause[optseq] new-line
16522 structured-block */
16524 #define OMP_TEAMS_CLAUSE_MASK \
16525 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16533 static tree
16534 c_parser_omp_teams (location_t loc, c_parser *parser,
16535 char *p_name, omp_clause_mask mask, tree *cclauses,
16536 bool *if_p)
16538 tree clauses, block, ret;
16540 strcat (p_name, " teams");
16541 mask |= OMP_TEAMS_CLAUSE_MASK;
16543 if (c_parser_next_token_is (parser, CPP_NAME))
16545 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16546 if (strcmp (p, "distribute") == 0)
16548 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16549 if (cclauses == NULL)
16550 cclauses = cclauses_buf;
16552 c_parser_consume_token (parser);
16553 if (!flag_openmp) /* flag_openmp_simd */
16554 return c_parser_omp_distribute (loc, parser, p_name, mask,
16555 cclauses, if_p);
16556 block = c_begin_compound_stmt (true);
16557 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16558 if_p);
16559 block = c_end_compound_stmt (loc, block, true);
16560 if (ret == NULL)
16561 return ret;
16562 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16563 ret = make_node (OMP_TEAMS);
16564 TREE_TYPE (ret) = void_type_node;
16565 OMP_TEAMS_CLAUSES (ret) = clauses;
16566 OMP_TEAMS_BODY (ret) = block;
16567 OMP_TEAMS_COMBINED (ret) = 1;
16568 return add_stmt (ret);
16571 if (!flag_openmp) /* flag_openmp_simd */
16573 c_parser_skip_to_pragma_eol (parser, false);
16574 return NULL_TREE;
16577 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16578 if (cclauses)
16580 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16581 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16584 tree stmt = make_node (OMP_TEAMS);
16585 TREE_TYPE (stmt) = void_type_node;
16586 OMP_TEAMS_CLAUSES (stmt) = clauses;
16587 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16589 return add_stmt (stmt);
16592 /* OpenMP 4.0:
16593 # pragma omp target data target-data-clause[optseq] new-line
16594 structured-block */
16596 #define OMP_TARGET_DATA_CLAUSE_MASK \
16597 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16602 static tree
16603 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16605 tree clauses
16606 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16607 "#pragma omp target data");
16608 int map_seen = 0;
16609 for (tree *pc = &clauses; *pc;)
16611 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16612 switch (OMP_CLAUSE_MAP_KIND (*pc))
16614 case GOMP_MAP_TO:
16615 case GOMP_MAP_ALWAYS_TO:
16616 case GOMP_MAP_FROM:
16617 case GOMP_MAP_ALWAYS_FROM:
16618 case GOMP_MAP_TOFROM:
16619 case GOMP_MAP_ALWAYS_TOFROM:
16620 case GOMP_MAP_ALLOC:
16621 map_seen = 3;
16622 break;
16623 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16624 case GOMP_MAP_ALWAYS_POINTER:
16625 break;
16626 default:
16627 map_seen |= 1;
16628 error_at (OMP_CLAUSE_LOCATION (*pc),
16629 "%<#pragma omp target data%> with map-type other "
16630 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16631 "on %<map%> clause");
16632 *pc = OMP_CLAUSE_CHAIN (*pc);
16633 continue;
16635 pc = &OMP_CLAUSE_CHAIN (*pc);
16638 if (map_seen != 3)
16640 if (map_seen == 0)
16641 error_at (loc,
16642 "%<#pragma omp target data%> must contain at least "
16643 "one %<map%> clause");
16644 return NULL_TREE;
16647 tree stmt = make_node (OMP_TARGET_DATA);
16648 TREE_TYPE (stmt) = void_type_node;
16649 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16650 keep_next_level ();
16651 tree block = c_begin_compound_stmt (true);
16652 add_stmt (c_parser_omp_structured_block (parser, if_p));
16653 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16655 SET_EXPR_LOCATION (stmt, loc);
16656 return add_stmt (stmt);
16659 /* OpenMP 4.0:
16660 # pragma omp target update target-update-clause[optseq] new-line */
16662 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16670 static bool
16671 c_parser_omp_target_update (location_t loc, c_parser *parser,
16672 enum pragma_context context)
16674 if (context == pragma_stmt)
16676 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16677 "omp target update");
16678 c_parser_skip_to_pragma_eol (parser, false);
16679 return false;
16682 tree clauses
16683 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16684 "#pragma omp target update");
16685 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16686 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16688 error_at (loc,
16689 "%<#pragma omp target update%> must contain at least one "
16690 "%<from%> or %<to%> clauses");
16691 return false;
16694 tree stmt = make_node (OMP_TARGET_UPDATE);
16695 TREE_TYPE (stmt) = void_type_node;
16696 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16697 SET_EXPR_LOCATION (stmt, loc);
16698 add_stmt (stmt);
16699 return false;
16702 /* OpenMP 4.5:
16703 # pragma omp target enter data target-data-clause[optseq] new-line */
16705 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16706 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16712 static tree
16713 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16714 enum pragma_context context)
16716 bool data_seen = false;
16717 if (c_parser_next_token_is (parser, CPP_NAME))
16719 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16720 if (strcmp (p, "data") == 0)
16722 c_parser_consume_token (parser);
16723 data_seen = true;
16726 if (!data_seen)
16728 c_parser_error (parser, "expected %<data%>");
16729 c_parser_skip_to_pragma_eol (parser);
16730 return NULL_TREE;
16733 if (context == pragma_stmt)
16735 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16736 "omp target enter data");
16737 c_parser_skip_to_pragma_eol (parser, false);
16738 return NULL_TREE;
16741 tree clauses
16742 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16743 "#pragma omp target enter data");
16744 int map_seen = 0;
16745 for (tree *pc = &clauses; *pc;)
16747 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16748 switch (OMP_CLAUSE_MAP_KIND (*pc))
16750 case GOMP_MAP_TO:
16751 case GOMP_MAP_ALWAYS_TO:
16752 case GOMP_MAP_ALLOC:
16753 map_seen = 3;
16754 break;
16755 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16756 case GOMP_MAP_ALWAYS_POINTER:
16757 break;
16758 default:
16759 map_seen |= 1;
16760 error_at (OMP_CLAUSE_LOCATION (*pc),
16761 "%<#pragma omp target enter data%> with map-type other "
16762 "than %<to%> or %<alloc%> on %<map%> clause");
16763 *pc = OMP_CLAUSE_CHAIN (*pc);
16764 continue;
16766 pc = &OMP_CLAUSE_CHAIN (*pc);
16769 if (map_seen != 3)
16771 if (map_seen == 0)
16772 error_at (loc,
16773 "%<#pragma omp target enter data%> must contain at least "
16774 "one %<map%> clause");
16775 return NULL_TREE;
16778 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16779 TREE_TYPE (stmt) = void_type_node;
16780 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16781 SET_EXPR_LOCATION (stmt, loc);
16782 add_stmt (stmt);
16783 return stmt;
16786 /* OpenMP 4.5:
16787 # pragma omp target exit data target-data-clause[optseq] new-line */
16789 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16790 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16796 static tree
16797 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16798 enum pragma_context context)
16800 bool data_seen = false;
16801 if (c_parser_next_token_is (parser, CPP_NAME))
16803 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16804 if (strcmp (p, "data") == 0)
16806 c_parser_consume_token (parser);
16807 data_seen = true;
16810 if (!data_seen)
16812 c_parser_error (parser, "expected %<data%>");
16813 c_parser_skip_to_pragma_eol (parser);
16814 return NULL_TREE;
16817 if (context == pragma_stmt)
16819 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16820 "omp target exit data");
16821 c_parser_skip_to_pragma_eol (parser, false);
16822 return NULL_TREE;
16825 tree clauses
16826 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16827 "#pragma omp target exit data");
16829 int map_seen = 0;
16830 for (tree *pc = &clauses; *pc;)
16832 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16833 switch (OMP_CLAUSE_MAP_KIND (*pc))
16835 case GOMP_MAP_FROM:
16836 case GOMP_MAP_ALWAYS_FROM:
16837 case GOMP_MAP_RELEASE:
16838 case GOMP_MAP_DELETE:
16839 map_seen = 3;
16840 break;
16841 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16842 case GOMP_MAP_ALWAYS_POINTER:
16843 break;
16844 default:
16845 map_seen |= 1;
16846 error_at (OMP_CLAUSE_LOCATION (*pc),
16847 "%<#pragma omp target exit data%> with map-type other "
16848 "than %<from%>, %<release%> or %<delete%> on %<map%>"
16849 " clause");
16850 *pc = OMP_CLAUSE_CHAIN (*pc);
16851 continue;
16853 pc = &OMP_CLAUSE_CHAIN (*pc);
16856 if (map_seen != 3)
16858 if (map_seen == 0)
16859 error_at (loc,
16860 "%<#pragma omp target exit data%> must contain at least one "
16861 "%<map%> clause");
16862 return NULL_TREE;
16865 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16866 TREE_TYPE (stmt) = void_type_node;
16867 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16868 SET_EXPR_LOCATION (stmt, loc);
16869 add_stmt (stmt);
16870 return stmt;
16873 /* OpenMP 4.0:
16874 # pragma omp target target-clause[optseq] new-line
16875 structured-block */
16877 #define OMP_TARGET_CLAUSE_MASK \
16878 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16888 static bool
16889 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16891 location_t loc = c_parser_peek_token (parser)->location;
16892 c_parser_consume_pragma (parser);
16893 tree *pc = NULL, stmt, block;
16895 if (context != pragma_stmt && context != pragma_compound)
16897 c_parser_error (parser, "expected declaration specifiers");
16898 c_parser_skip_to_pragma_eol (parser);
16899 return false;
16902 if (c_parser_next_token_is (parser, CPP_NAME))
16904 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16905 enum tree_code ccode = ERROR_MARK;
16907 if (strcmp (p, "teams") == 0)
16908 ccode = OMP_TEAMS;
16909 else if (strcmp (p, "parallel") == 0)
16910 ccode = OMP_PARALLEL;
16911 else if (strcmp (p, "simd") == 0)
16912 ccode = OMP_SIMD;
16913 if (ccode != ERROR_MARK)
16915 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16916 char p_name[sizeof ("#pragma omp target teams distribute "
16917 "parallel for simd")];
16919 c_parser_consume_token (parser);
16920 strcpy (p_name, "#pragma omp target");
16921 if (!flag_openmp) /* flag_openmp_simd */
16923 tree stmt;
16924 switch (ccode)
16926 case OMP_TEAMS:
16927 stmt = c_parser_omp_teams (loc, parser, p_name,
16928 OMP_TARGET_CLAUSE_MASK,
16929 cclauses, if_p);
16930 break;
16931 case OMP_PARALLEL:
16932 stmt = c_parser_omp_parallel (loc, parser, p_name,
16933 OMP_TARGET_CLAUSE_MASK,
16934 cclauses, if_p);
16935 break;
16936 case OMP_SIMD:
16937 stmt = c_parser_omp_simd (loc, parser, p_name,
16938 OMP_TARGET_CLAUSE_MASK,
16939 cclauses, if_p);
16940 break;
16941 default:
16942 gcc_unreachable ();
16944 return stmt != NULL_TREE;
16946 keep_next_level ();
16947 tree block = c_begin_compound_stmt (true), ret;
16948 switch (ccode)
16950 case OMP_TEAMS:
16951 ret = c_parser_omp_teams (loc, parser, p_name,
16952 OMP_TARGET_CLAUSE_MASK, cclauses,
16953 if_p);
16954 break;
16955 case OMP_PARALLEL:
16956 ret = c_parser_omp_parallel (loc, parser, p_name,
16957 OMP_TARGET_CLAUSE_MASK, cclauses,
16958 if_p);
16959 break;
16960 case OMP_SIMD:
16961 ret = c_parser_omp_simd (loc, parser, p_name,
16962 OMP_TARGET_CLAUSE_MASK, cclauses,
16963 if_p);
16964 break;
16965 default:
16966 gcc_unreachable ();
16968 block = c_end_compound_stmt (loc, block, true);
16969 if (ret == NULL_TREE)
16970 return false;
16971 if (ccode == OMP_TEAMS)
16973 /* For combined target teams, ensure the num_teams and
16974 thread_limit clause expressions are evaluated on the host,
16975 before entering the target construct. */
16976 tree c;
16977 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16978 c; c = OMP_CLAUSE_CHAIN (c))
16979 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16980 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16981 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16983 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16984 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16985 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16986 expr, NULL_TREE, NULL_TREE);
16987 add_stmt (expr);
16988 OMP_CLAUSE_OPERAND (c, 0) = expr;
16989 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16990 OMP_CLAUSE_FIRSTPRIVATE);
16991 OMP_CLAUSE_DECL (tc) = tmp;
16992 OMP_CLAUSE_CHAIN (tc)
16993 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16994 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16997 tree stmt = make_node (OMP_TARGET);
16998 TREE_TYPE (stmt) = void_type_node;
16999 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17000 OMP_TARGET_BODY (stmt) = block;
17001 OMP_TARGET_COMBINED (stmt) = 1;
17002 add_stmt (stmt);
17003 pc = &OMP_TARGET_CLAUSES (stmt);
17004 goto check_clauses;
17006 else if (!flag_openmp) /* flag_openmp_simd */
17008 c_parser_skip_to_pragma_eol (parser, false);
17009 return false;
17011 else if (strcmp (p, "data") == 0)
17013 c_parser_consume_token (parser);
17014 c_parser_omp_target_data (loc, parser, if_p);
17015 return true;
17017 else if (strcmp (p, "enter") == 0)
17019 c_parser_consume_token (parser);
17020 c_parser_omp_target_enter_data (loc, parser, context);
17021 return false;
17023 else if (strcmp (p, "exit") == 0)
17025 c_parser_consume_token (parser);
17026 c_parser_omp_target_exit_data (loc, parser, context);
17027 return false;
17029 else if (strcmp (p, "update") == 0)
17031 c_parser_consume_token (parser);
17032 return c_parser_omp_target_update (loc, parser, context);
17035 if (!flag_openmp) /* flag_openmp_simd */
17037 c_parser_skip_to_pragma_eol (parser, false);
17038 return false;
17041 stmt = make_node (OMP_TARGET);
17042 TREE_TYPE (stmt) = void_type_node;
17044 OMP_TARGET_CLAUSES (stmt)
17045 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17046 "#pragma omp target");
17047 pc = &OMP_TARGET_CLAUSES (stmt);
17048 keep_next_level ();
17049 block = c_begin_compound_stmt (true);
17050 add_stmt (c_parser_omp_structured_block (parser, if_p));
17051 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17053 SET_EXPR_LOCATION (stmt, loc);
17054 add_stmt (stmt);
17056 check_clauses:
17057 while (*pc)
17059 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17060 switch (OMP_CLAUSE_MAP_KIND (*pc))
17062 case GOMP_MAP_TO:
17063 case GOMP_MAP_ALWAYS_TO:
17064 case GOMP_MAP_FROM:
17065 case GOMP_MAP_ALWAYS_FROM:
17066 case GOMP_MAP_TOFROM:
17067 case GOMP_MAP_ALWAYS_TOFROM:
17068 case GOMP_MAP_ALLOC:
17069 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17070 case GOMP_MAP_ALWAYS_POINTER:
17071 break;
17072 default:
17073 error_at (OMP_CLAUSE_LOCATION (*pc),
17074 "%<#pragma omp target%> with map-type other "
17075 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17076 "on %<map%> clause");
17077 *pc = OMP_CLAUSE_CHAIN (*pc);
17078 continue;
17080 pc = &OMP_CLAUSE_CHAIN (*pc);
17082 return true;
17085 /* OpenMP 4.0:
17086 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17088 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17096 static void
17097 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17099 auto_vec<c_token> clauses;
17100 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17102 c_token *token = c_parser_peek_token (parser);
17103 if (token->type == CPP_EOF)
17105 c_parser_skip_to_pragma_eol (parser);
17106 return;
17108 clauses.safe_push (*token);
17109 c_parser_consume_token (parser);
17111 clauses.safe_push (*c_parser_peek_token (parser));
17112 c_parser_skip_to_pragma_eol (parser);
17114 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17116 if (c_parser_peek_token (parser)->pragma_kind
17117 != PRAGMA_OMP_DECLARE
17118 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17119 || strcmp (IDENTIFIER_POINTER
17120 (c_parser_peek_2nd_token (parser)->value),
17121 "simd") != 0)
17123 c_parser_error (parser,
17124 "%<#pragma omp declare simd%> must be followed by "
17125 "function declaration or definition or another "
17126 "%<#pragma omp declare simd%>");
17127 return;
17129 c_parser_consume_pragma (parser);
17130 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17132 c_token *token = c_parser_peek_token (parser);
17133 if (token->type == CPP_EOF)
17135 c_parser_skip_to_pragma_eol (parser);
17136 return;
17138 clauses.safe_push (*token);
17139 c_parser_consume_token (parser);
17141 clauses.safe_push (*c_parser_peek_token (parser));
17142 c_parser_skip_to_pragma_eol (parser);
17145 /* Make sure nothing tries to read past the end of the tokens. */
17146 c_token eof_token;
17147 memset (&eof_token, 0, sizeof (eof_token));
17148 eof_token.type = CPP_EOF;
17149 clauses.safe_push (eof_token);
17150 clauses.safe_push (eof_token);
17152 switch (context)
17154 case pragma_external:
17155 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17156 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17158 int ext = disable_extension_diagnostics ();
17160 c_parser_consume_token (parser);
17161 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17162 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17163 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17164 NULL, clauses);
17165 restore_extension_diagnostics (ext);
17167 else
17168 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17169 NULL, clauses);
17170 break;
17171 case pragma_struct:
17172 case pragma_param:
17173 case pragma_stmt:
17174 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17175 "function declaration or definition");
17176 break;
17177 case pragma_compound:
17178 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17179 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17181 int ext = disable_extension_diagnostics ();
17183 c_parser_consume_token (parser);
17184 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17185 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17186 if (c_parser_next_tokens_start_declaration (parser))
17188 c_parser_declaration_or_fndef (parser, true, true, true, true,
17189 true, NULL, clauses);
17190 restore_extension_diagnostics (ext);
17191 break;
17193 restore_extension_diagnostics (ext);
17195 else if (c_parser_next_tokens_start_declaration (parser))
17197 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17198 NULL, clauses);
17199 break;
17201 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17202 "function declaration or definition");
17203 break;
17204 default:
17205 gcc_unreachable ();
17209 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17210 and put that into "omp declare simd" attribute. */
17212 static void
17213 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17214 vec<c_token> clauses)
17216 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17217 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17218 has already processed the tokens. */
17219 if (clauses.exists () && clauses[0].type == CPP_EOF)
17220 return;
17221 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17223 error ("%<#pragma omp declare simd%> not immediately followed by "
17224 "a function declaration or definition");
17225 clauses[0].type = CPP_EOF;
17226 return;
17228 if (clauses.exists () && clauses[0].type != CPP_NAME)
17230 error_at (DECL_SOURCE_LOCATION (fndecl),
17231 "%<#pragma omp declare simd%> not immediately followed by "
17232 "a single function declaration or definition");
17233 clauses[0].type = CPP_EOF;
17234 return;
17237 if (parms == NULL_TREE)
17238 parms = DECL_ARGUMENTS (fndecl);
17240 unsigned int tokens_avail = parser->tokens_avail;
17241 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17244 parser->tokens = clauses.address ();
17245 parser->tokens_avail = clauses.length ();
17247 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17248 while (parser->tokens_avail > 3)
17250 c_token *token = c_parser_peek_token (parser);
17251 gcc_assert (token->type == CPP_NAME
17252 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17253 c_parser_consume_token (parser);
17254 parser->in_pragma = true;
17256 tree c = NULL_TREE;
17257 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17258 "#pragma omp declare simd");
17259 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17260 if (c != NULL_TREE)
17261 c = tree_cons (NULL_TREE, c, NULL_TREE);
17262 c = build_tree_list (get_identifier ("omp declare simd"), c);
17263 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17264 DECL_ATTRIBUTES (fndecl) = c;
17267 parser->tokens = &parser->tokens_buf[0];
17268 parser->tokens_avail = tokens_avail;
17269 if (clauses.exists ())
17270 clauses[0].type = CPP_PRAGMA;
17274 /* OpenMP 4.0:
17275 # pragma omp declare target new-line
17276 declarations and definitions
17277 # pragma omp end declare target new-line
17279 OpenMP 4.5:
17280 # pragma omp declare target ( extended-list ) new-line
17282 # pragma omp declare target declare-target-clauses[seq] new-line */
17284 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17288 static void
17289 c_parser_omp_declare_target (c_parser *parser)
17291 location_t loc = c_parser_peek_token (parser)->location;
17292 tree clauses = NULL_TREE;
17293 if (c_parser_next_token_is (parser, CPP_NAME))
17294 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17295 "#pragma omp declare target");
17296 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17298 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17299 clauses);
17300 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17301 c_parser_skip_to_pragma_eol (parser);
17303 else
17305 c_parser_skip_to_pragma_eol (parser);
17306 current_omp_declare_target_attribute++;
17307 return;
17309 if (current_omp_declare_target_attribute)
17310 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17311 "%<#pragma omp declare target%> without clauses and "
17312 "%<#pragma omp end declare target%>");
17313 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17315 tree t = OMP_CLAUSE_DECL (c), id;
17316 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17317 tree at2 = lookup_attribute ("omp declare target link",
17318 DECL_ATTRIBUTES (t));
17319 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17321 id = get_identifier ("omp declare target link");
17322 std::swap (at1, at2);
17324 else
17325 id = get_identifier ("omp declare target");
17326 if (at2)
17328 error_at (OMP_CLAUSE_LOCATION (c),
17329 "%qD specified both in declare target %<link%> and %<to%>"
17330 " clauses", t);
17331 continue;
17333 if (!at1)
17335 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17336 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17337 continue;
17339 symtab_node *node = symtab_node::get (t);
17340 if (node != NULL)
17342 node->offloadable = 1;
17343 if (ENABLE_OFFLOADING)
17345 g->have_offload = true;
17346 if (is_a <varpool_node *> (node))
17347 vec_safe_push (offload_vars, t);
17354 static void
17355 c_parser_omp_end_declare_target (c_parser *parser)
17357 location_t loc = c_parser_peek_token (parser)->location;
17358 c_parser_consume_pragma (parser);
17359 if (c_parser_next_token_is (parser, CPP_NAME)
17360 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17361 "declare") == 0)
17363 c_parser_consume_token (parser);
17364 if (c_parser_next_token_is (parser, CPP_NAME)
17365 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17366 "target") == 0)
17367 c_parser_consume_token (parser);
17368 else
17370 c_parser_error (parser, "expected %<target%>");
17371 c_parser_skip_to_pragma_eol (parser);
17372 return;
17375 else
17377 c_parser_error (parser, "expected %<declare%>");
17378 c_parser_skip_to_pragma_eol (parser);
17379 return;
17381 c_parser_skip_to_pragma_eol (parser);
17382 if (!current_omp_declare_target_attribute)
17383 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17384 "%<#pragma omp declare target%>");
17385 else
17386 current_omp_declare_target_attribute--;
17390 /* OpenMP 4.0
17391 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17392 initializer-clause[opt] new-line
17394 initializer-clause:
17395 initializer (omp_priv = initializer)
17396 initializer (function-name (argument-list)) */
17398 static void
17399 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17401 unsigned int tokens_avail = 0, i;
17402 vec<tree> types = vNULL;
17403 vec<c_token> clauses = vNULL;
17404 enum tree_code reduc_code = ERROR_MARK;
17405 tree reduc_id = NULL_TREE;
17406 tree type;
17407 location_t rloc = c_parser_peek_token (parser)->location;
17409 if (context == pragma_struct || context == pragma_param)
17411 error ("%<#pragma omp declare reduction%> not at file or block scope");
17412 goto fail;
17415 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17416 goto fail;
17418 switch (c_parser_peek_token (parser)->type)
17420 case CPP_PLUS:
17421 reduc_code = PLUS_EXPR;
17422 break;
17423 case CPP_MULT:
17424 reduc_code = MULT_EXPR;
17425 break;
17426 case CPP_MINUS:
17427 reduc_code = MINUS_EXPR;
17428 break;
17429 case CPP_AND:
17430 reduc_code = BIT_AND_EXPR;
17431 break;
17432 case CPP_XOR:
17433 reduc_code = BIT_XOR_EXPR;
17434 break;
17435 case CPP_OR:
17436 reduc_code = BIT_IOR_EXPR;
17437 break;
17438 case CPP_AND_AND:
17439 reduc_code = TRUTH_ANDIF_EXPR;
17440 break;
17441 case CPP_OR_OR:
17442 reduc_code = TRUTH_ORIF_EXPR;
17443 break;
17444 case CPP_NAME:
17445 const char *p;
17446 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17447 if (strcmp (p, "min") == 0)
17449 reduc_code = MIN_EXPR;
17450 break;
17452 if (strcmp (p, "max") == 0)
17454 reduc_code = MAX_EXPR;
17455 break;
17457 reduc_id = c_parser_peek_token (parser)->value;
17458 break;
17459 default:
17460 c_parser_error (parser,
17461 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17462 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17463 goto fail;
17466 tree orig_reduc_id, reduc_decl;
17467 orig_reduc_id = reduc_id;
17468 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17469 reduc_decl = c_omp_reduction_decl (reduc_id);
17470 c_parser_consume_token (parser);
17472 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17473 goto fail;
17475 while (true)
17477 location_t loc = c_parser_peek_token (parser)->location;
17478 struct c_type_name *ctype = c_parser_type_name (parser);
17479 if (ctype != NULL)
17481 type = groktypename (ctype, NULL, NULL);
17482 if (type == error_mark_node)
17484 else if ((INTEGRAL_TYPE_P (type)
17485 || TREE_CODE (type) == REAL_TYPE
17486 || TREE_CODE (type) == COMPLEX_TYPE)
17487 && orig_reduc_id == NULL_TREE)
17488 error_at (loc, "predeclared arithmetic type in "
17489 "%<#pragma omp declare reduction%>");
17490 else if (TREE_CODE (type) == FUNCTION_TYPE
17491 || TREE_CODE (type) == ARRAY_TYPE)
17492 error_at (loc, "function or array type in "
17493 "%<#pragma omp declare reduction%>");
17494 else if (TYPE_ATOMIC (type))
17495 error_at (loc, "%<_Atomic%> qualified type in "
17496 "%<#pragma omp declare reduction%>");
17497 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17498 error_at (loc, "const, volatile or restrict qualified type in "
17499 "%<#pragma omp declare reduction%>");
17500 else
17502 tree t;
17503 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17504 if (comptypes (TREE_PURPOSE (t), type))
17506 error_at (loc, "redeclaration of %qs "
17507 "%<#pragma omp declare reduction%> for "
17508 "type %qT",
17509 IDENTIFIER_POINTER (reduc_id)
17510 + sizeof ("omp declare reduction ") - 1,
17511 type);
17512 location_t ploc
17513 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17514 0));
17515 error_at (ploc, "previous %<#pragma omp declare "
17516 "reduction%>");
17517 break;
17519 if (t == NULL_TREE)
17520 types.safe_push (type);
17522 if (c_parser_next_token_is (parser, CPP_COMMA))
17523 c_parser_consume_token (parser);
17524 else
17525 break;
17527 else
17528 break;
17531 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17532 || types.is_empty ())
17534 fail:
17535 clauses.release ();
17536 types.release ();
17537 while (true)
17539 c_token *token = c_parser_peek_token (parser);
17540 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17541 break;
17542 c_parser_consume_token (parser);
17544 c_parser_skip_to_pragma_eol (parser);
17545 return;
17548 if (types.length () > 1)
17550 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17552 c_token *token = c_parser_peek_token (parser);
17553 if (token->type == CPP_EOF)
17554 goto fail;
17555 clauses.safe_push (*token);
17556 c_parser_consume_token (parser);
17558 clauses.safe_push (*c_parser_peek_token (parser));
17559 c_parser_skip_to_pragma_eol (parser);
17561 /* Make sure nothing tries to read past the end of the tokens. */
17562 c_token eof_token;
17563 memset (&eof_token, 0, sizeof (eof_token));
17564 eof_token.type = CPP_EOF;
17565 clauses.safe_push (eof_token);
17566 clauses.safe_push (eof_token);
17569 int errs = errorcount;
17570 FOR_EACH_VEC_ELT (types, i, type)
17572 tokens_avail = parser->tokens_avail;
17573 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17574 if (!clauses.is_empty ())
17576 parser->tokens = clauses.address ();
17577 parser->tokens_avail = clauses.length ();
17578 parser->in_pragma = true;
17581 bool nested = current_function_decl != NULL_TREE;
17582 if (nested)
17583 c_push_function_context ();
17584 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17585 reduc_id, default_function_type);
17586 current_function_decl = fndecl;
17587 allocate_struct_function (fndecl, true);
17588 push_scope ();
17589 tree stmt = push_stmt_list ();
17590 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17591 warn about these. */
17592 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17593 get_identifier ("omp_out"), type);
17594 DECL_ARTIFICIAL (omp_out) = 1;
17595 DECL_CONTEXT (omp_out) = fndecl;
17596 pushdecl (omp_out);
17597 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17598 get_identifier ("omp_in"), type);
17599 DECL_ARTIFICIAL (omp_in) = 1;
17600 DECL_CONTEXT (omp_in) = fndecl;
17601 pushdecl (omp_in);
17602 struct c_expr combiner = c_parser_expression (parser);
17603 struct c_expr initializer;
17604 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17605 bool bad = false;
17606 initializer.value = error_mark_node;
17607 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17608 bad = true;
17609 else if (c_parser_next_token_is (parser, CPP_NAME)
17610 && strcmp (IDENTIFIER_POINTER
17611 (c_parser_peek_token (parser)->value),
17612 "initializer") == 0)
17614 c_parser_consume_token (parser);
17615 pop_scope ();
17616 push_scope ();
17617 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17618 get_identifier ("omp_priv"), type);
17619 DECL_ARTIFICIAL (omp_priv) = 1;
17620 DECL_INITIAL (omp_priv) = error_mark_node;
17621 DECL_CONTEXT (omp_priv) = fndecl;
17622 pushdecl (omp_priv);
17623 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17624 get_identifier ("omp_orig"), type);
17625 DECL_ARTIFICIAL (omp_orig) = 1;
17626 DECL_CONTEXT (omp_orig) = fndecl;
17627 pushdecl (omp_orig);
17628 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17629 bad = true;
17630 else if (!c_parser_next_token_is (parser, CPP_NAME))
17632 c_parser_error (parser, "expected %<omp_priv%> or "
17633 "function-name");
17634 bad = true;
17636 else if (strcmp (IDENTIFIER_POINTER
17637 (c_parser_peek_token (parser)->value),
17638 "omp_priv") != 0)
17640 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17641 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17643 c_parser_error (parser, "expected function-name %<(%>");
17644 bad = true;
17646 else
17647 initializer = c_parser_postfix_expression (parser);
17648 if (initializer.value
17649 && TREE_CODE (initializer.value) == CALL_EXPR)
17651 int j;
17652 tree c = initializer.value;
17653 for (j = 0; j < call_expr_nargs (c); j++)
17655 tree a = CALL_EXPR_ARG (c, j);
17656 STRIP_NOPS (a);
17657 if (TREE_CODE (a) == ADDR_EXPR
17658 && TREE_OPERAND (a, 0) == omp_priv)
17659 break;
17661 if (j == call_expr_nargs (c))
17662 error ("one of the initializer call arguments should be "
17663 "%<&omp_priv%>");
17666 else
17668 c_parser_consume_token (parser);
17669 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17670 bad = true;
17671 else
17673 tree st = push_stmt_list ();
17674 location_t loc = c_parser_peek_token (parser)->location;
17675 rich_location richloc (line_table, loc);
17676 start_init (omp_priv, NULL_TREE, 0, &richloc);
17677 struct c_expr init = c_parser_initializer (parser);
17678 finish_init ();
17679 finish_decl (omp_priv, loc, init.value,
17680 init.original_type, NULL_TREE);
17681 pop_stmt_list (st);
17684 if (!bad
17685 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17686 bad = true;
17689 if (!bad)
17691 c_parser_skip_to_pragma_eol (parser);
17693 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17694 DECL_INITIAL (reduc_decl));
17695 DECL_INITIAL (reduc_decl) = t;
17696 DECL_SOURCE_LOCATION (omp_out) = rloc;
17697 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17698 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17699 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17700 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17701 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17702 if (omp_priv)
17704 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17705 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17706 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17707 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17708 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17709 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17710 walk_tree (&DECL_INITIAL (omp_priv),
17711 c_check_omp_declare_reduction_r,
17712 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17716 pop_stmt_list (stmt);
17717 pop_scope ();
17718 if (cfun->language != NULL)
17720 ggc_free (cfun->language);
17721 cfun->language = NULL;
17723 set_cfun (NULL);
17724 current_function_decl = NULL_TREE;
17725 if (nested)
17726 c_pop_function_context ();
17728 if (!clauses.is_empty ())
17730 parser->tokens = &parser->tokens_buf[0];
17731 parser->tokens_avail = tokens_avail;
17733 if (bad)
17734 goto fail;
17735 if (errs != errorcount)
17736 break;
17739 clauses.release ();
17740 types.release ();
17744 /* OpenMP 4.0
17745 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17746 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17747 initializer-clause[opt] new-line
17748 #pragma omp declare target new-line */
17750 static void
17751 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17753 c_parser_consume_pragma (parser);
17754 if (c_parser_next_token_is (parser, CPP_NAME))
17756 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17757 if (strcmp (p, "simd") == 0)
17759 /* c_parser_consume_token (parser); done in
17760 c_parser_omp_declare_simd. */
17761 c_parser_omp_declare_simd (parser, context);
17762 return;
17764 if (strcmp (p, "reduction") == 0)
17766 c_parser_consume_token (parser);
17767 c_parser_omp_declare_reduction (parser, context);
17768 return;
17770 if (!flag_openmp) /* flag_openmp_simd */
17772 c_parser_skip_to_pragma_eol (parser, false);
17773 return;
17775 if (strcmp (p, "target") == 0)
17777 c_parser_consume_token (parser);
17778 c_parser_omp_declare_target (parser);
17779 return;
17783 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17784 "or %<target%>");
17785 c_parser_skip_to_pragma_eol (parser);
17788 /* OpenMP 4.5:
17789 #pragma omp taskloop taskloop-clause[optseq] new-line
17790 for-loop
17792 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17793 for-loop */
17795 #define OMP_TASKLOOP_CLAUSE_MASK \
17796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17811 static tree
17812 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17813 char *p_name, omp_clause_mask mask, tree *cclauses,
17814 bool *if_p)
17816 tree clauses, block, ret;
17818 strcat (p_name, " taskloop");
17819 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17821 if (c_parser_next_token_is (parser, CPP_NAME))
17823 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17825 if (strcmp (p, "simd") == 0)
17827 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17828 if (cclauses == NULL)
17829 cclauses = cclauses_buf;
17830 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17831 c_parser_consume_token (parser);
17832 if (!flag_openmp) /* flag_openmp_simd */
17833 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17834 if_p);
17835 block = c_begin_compound_stmt (true);
17836 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17837 block = c_end_compound_stmt (loc, block, true);
17838 if (ret == NULL)
17839 return ret;
17840 ret = make_node (OMP_TASKLOOP);
17841 TREE_TYPE (ret) = void_type_node;
17842 OMP_FOR_BODY (ret) = block;
17843 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17844 SET_EXPR_LOCATION (ret, loc);
17845 add_stmt (ret);
17846 return ret;
17849 if (!flag_openmp) /* flag_openmp_simd */
17851 c_parser_skip_to_pragma_eol (parser, false);
17852 return NULL_TREE;
17855 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17856 if (cclauses)
17858 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17859 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17862 block = c_begin_compound_stmt (true);
17863 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17864 block = c_end_compound_stmt (loc, block, true);
17865 add_stmt (block);
17867 return ret;
17870 /* Main entry point to parsing most OpenMP pragmas. */
17872 static void
17873 c_parser_omp_construct (c_parser *parser, bool *if_p)
17875 enum pragma_kind p_kind;
17876 location_t loc;
17877 tree stmt;
17878 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17879 omp_clause_mask mask (0);
17881 loc = c_parser_peek_token (parser)->location;
17882 p_kind = c_parser_peek_token (parser)->pragma_kind;
17883 c_parser_consume_pragma (parser);
17885 switch (p_kind)
17887 case PRAGMA_OACC_ATOMIC:
17888 c_parser_omp_atomic (loc, parser);
17889 return;
17890 case PRAGMA_OACC_CACHE:
17891 strcpy (p_name, "#pragma acc");
17892 stmt = c_parser_oacc_cache (loc, parser);
17893 break;
17894 case PRAGMA_OACC_DATA:
17895 stmt = c_parser_oacc_data (loc, parser, if_p);
17896 break;
17897 case PRAGMA_OACC_HOST_DATA:
17898 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17899 break;
17900 case PRAGMA_OACC_KERNELS:
17901 case PRAGMA_OACC_PARALLEL:
17902 strcpy (p_name, "#pragma acc");
17903 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17904 if_p);
17905 break;
17906 case PRAGMA_OACC_LOOP:
17907 strcpy (p_name, "#pragma acc");
17908 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17909 break;
17910 case PRAGMA_OACC_WAIT:
17911 strcpy (p_name, "#pragma wait");
17912 stmt = c_parser_oacc_wait (loc, parser, p_name);
17913 break;
17914 case PRAGMA_OMP_ATOMIC:
17915 c_parser_omp_atomic (loc, parser);
17916 return;
17917 case PRAGMA_OMP_CRITICAL:
17918 stmt = c_parser_omp_critical (loc, parser, if_p);
17919 break;
17920 case PRAGMA_OMP_DISTRIBUTE:
17921 strcpy (p_name, "#pragma omp");
17922 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17923 break;
17924 case PRAGMA_OMP_FOR:
17925 strcpy (p_name, "#pragma omp");
17926 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17927 break;
17928 case PRAGMA_OMP_MASTER:
17929 stmt = c_parser_omp_master (loc, parser, if_p);
17930 break;
17931 case PRAGMA_OMP_PARALLEL:
17932 strcpy (p_name, "#pragma omp");
17933 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17934 break;
17935 case PRAGMA_OMP_SECTIONS:
17936 strcpy (p_name, "#pragma omp");
17937 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17938 break;
17939 case PRAGMA_OMP_SIMD:
17940 strcpy (p_name, "#pragma omp");
17941 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17942 break;
17943 case PRAGMA_OMP_SINGLE:
17944 stmt = c_parser_omp_single (loc, parser, if_p);
17945 break;
17946 case PRAGMA_OMP_TASK:
17947 stmt = c_parser_omp_task (loc, parser, if_p);
17948 break;
17949 case PRAGMA_OMP_TASKGROUP:
17950 stmt = c_parser_omp_taskgroup (parser, if_p);
17951 break;
17952 case PRAGMA_OMP_TASKLOOP:
17953 strcpy (p_name, "#pragma omp");
17954 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17955 break;
17956 case PRAGMA_OMP_TEAMS:
17957 strcpy (p_name, "#pragma omp");
17958 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17959 break;
17960 default:
17961 gcc_unreachable ();
17964 if (stmt)
17965 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17969 /* OpenMP 2.5:
17970 # pragma omp threadprivate (variable-list) */
17972 static void
17973 c_parser_omp_threadprivate (c_parser *parser)
17975 tree vars, t;
17976 location_t loc;
17978 c_parser_consume_pragma (parser);
17979 loc = c_parser_peek_token (parser)->location;
17980 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17982 /* Mark every variable in VARS to be assigned thread local storage. */
17983 for (t = vars; t; t = TREE_CHAIN (t))
17985 tree v = TREE_PURPOSE (t);
17987 /* FIXME diagnostics: Ideally we should keep individual
17988 locations for all the variables in the var list to make the
17989 following errors more precise. Perhaps
17990 c_parser_omp_var_list_parens() should construct a list of
17991 locations to go along with the var list. */
17993 /* If V had already been marked threadprivate, it doesn't matter
17994 whether it had been used prior to this point. */
17995 if (!VAR_P (v))
17996 error_at (loc, "%qD is not a variable", v);
17997 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17998 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17999 else if (! is_global_var (v))
18000 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18001 else if (TREE_TYPE (v) == error_mark_node)
18003 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18004 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18005 else
18007 if (! DECL_THREAD_LOCAL_P (v))
18009 set_decl_tls_model (v, decl_default_tls_model (v));
18010 /* If rtl has been already set for this var, call
18011 make_decl_rtl once again, so that encode_section_info
18012 has a chance to look at the new decl flags. */
18013 if (DECL_RTL_SET_P (v))
18014 make_decl_rtl (v);
18016 C_DECL_THREADPRIVATE_P (v) = 1;
18020 c_parser_skip_to_pragma_eol (parser);
18023 /* Parse a transaction attribute (GCC Extension).
18025 transaction-attribute:
18026 attributes
18027 [ [ any-word ] ]
18029 The transactional memory language description is written for C++,
18030 and uses the C++0x attribute syntax. For compatibility, allow the
18031 bracket style for transactions in C as well. */
18033 static tree
18034 c_parser_transaction_attributes (c_parser *parser)
18036 tree attr_name, attr = NULL;
18038 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18039 return c_parser_attributes (parser);
18041 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18042 return NULL_TREE;
18043 c_parser_consume_token (parser);
18044 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18045 goto error1;
18047 attr_name = c_parser_attribute_any_word (parser);
18048 if (attr_name)
18050 c_parser_consume_token (parser);
18051 attr = build_tree_list (attr_name, NULL_TREE);
18053 else
18054 c_parser_error (parser, "expected identifier");
18056 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18057 error1:
18058 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18059 return attr;
18062 /* Parse a __transaction_atomic or __transaction_relaxed statement
18063 (GCC Extension).
18065 transaction-statement:
18066 __transaction_atomic transaction-attribute[opt] compound-statement
18067 __transaction_relaxed compound-statement
18069 Note that the only valid attribute is: "outer".
18072 static tree
18073 c_parser_transaction (c_parser *parser, enum rid keyword)
18075 unsigned int old_in = parser->in_transaction;
18076 unsigned int this_in = 1, new_in;
18077 location_t loc = c_parser_peek_token (parser)->location;
18078 tree stmt, attrs;
18080 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18081 || keyword == RID_TRANSACTION_RELAXED)
18082 && c_parser_next_token_is_keyword (parser, keyword));
18083 c_parser_consume_token (parser);
18085 if (keyword == RID_TRANSACTION_RELAXED)
18086 this_in |= TM_STMT_ATTR_RELAXED;
18087 else
18089 attrs = c_parser_transaction_attributes (parser);
18090 if (attrs)
18091 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18094 /* Keep track if we're in the lexical scope of an outer transaction. */
18095 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18097 parser->in_transaction = new_in;
18098 stmt = c_parser_compound_statement (parser);
18099 parser->in_transaction = old_in;
18101 if (flag_tm)
18102 stmt = c_finish_transaction (loc, stmt, this_in);
18103 else
18104 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18105 "%<__transaction_atomic%> without transactional memory support enabled"
18106 : "%<__transaction_relaxed %> "
18107 "without transactional memory support enabled"));
18109 return stmt;
18112 /* Parse a __transaction_atomic or __transaction_relaxed expression
18113 (GCC Extension).
18115 transaction-expression:
18116 __transaction_atomic ( expression )
18117 __transaction_relaxed ( expression )
18120 static struct c_expr
18121 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18123 struct c_expr ret;
18124 unsigned int old_in = parser->in_transaction;
18125 unsigned int this_in = 1;
18126 location_t loc = c_parser_peek_token (parser)->location;
18127 tree attrs;
18129 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18130 || keyword == RID_TRANSACTION_RELAXED)
18131 && c_parser_next_token_is_keyword (parser, keyword));
18132 c_parser_consume_token (parser);
18134 if (keyword == RID_TRANSACTION_RELAXED)
18135 this_in |= TM_STMT_ATTR_RELAXED;
18136 else
18138 attrs = c_parser_transaction_attributes (parser);
18139 if (attrs)
18140 this_in |= parse_tm_stmt_attr (attrs, 0);
18143 parser->in_transaction = this_in;
18144 matching_parens parens;
18145 if (parens.require_open (parser))
18147 tree expr = c_parser_expression (parser).value;
18148 ret.original_type = TREE_TYPE (expr);
18149 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18150 if (this_in & TM_STMT_ATTR_RELAXED)
18151 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18152 SET_EXPR_LOCATION (ret.value, loc);
18153 ret.original_code = TRANSACTION_EXPR;
18154 if (!parens.require_close (parser))
18156 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18157 goto error;
18160 else
18162 error:
18163 ret.value = error_mark_node;
18164 ret.original_code = ERROR_MARK;
18165 ret.original_type = NULL;
18167 parser->in_transaction = old_in;
18169 if (!flag_tm)
18170 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18171 "%<__transaction_atomic%> without transactional memory support enabled"
18172 : "%<__transaction_relaxed %> "
18173 "without transactional memory support enabled"));
18175 set_c_expr_source_range (&ret, loc, loc);
18177 return ret;
18180 /* Parse a __transaction_cancel statement (GCC Extension).
18182 transaction-cancel-statement:
18183 __transaction_cancel transaction-attribute[opt] ;
18185 Note that the only valid attribute is "outer".
18188 static tree
18189 c_parser_transaction_cancel (c_parser *parser)
18191 location_t loc = c_parser_peek_token (parser)->location;
18192 tree attrs;
18193 bool is_outer = false;
18195 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18196 c_parser_consume_token (parser);
18198 attrs = c_parser_transaction_attributes (parser);
18199 if (attrs)
18200 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18202 if (!flag_tm)
18204 error_at (loc, "%<__transaction_cancel%> without "
18205 "transactional memory support enabled");
18206 goto ret_error;
18208 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18210 error_at (loc, "%<__transaction_cancel%> within a "
18211 "%<__transaction_relaxed%>");
18212 goto ret_error;
18214 else if (is_outer)
18216 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18217 && !is_tm_may_cancel_outer (current_function_decl))
18219 error_at (loc, "outer %<__transaction_cancel%> not "
18220 "within outer %<__transaction_atomic%>");
18221 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18222 goto ret_error;
18225 else if (parser->in_transaction == 0)
18227 error_at (loc, "%<__transaction_cancel%> not within "
18228 "%<__transaction_atomic%>");
18229 goto ret_error;
18232 return add_stmt (build_tm_abort_call (loc, is_outer));
18234 ret_error:
18235 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18238 /* Parse a single source file. */
18240 void
18241 c_parse_file (void)
18243 /* Use local storage to begin. If the first token is a pragma, parse it.
18244 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18245 which will cause garbage collection. */
18246 c_parser tparser;
18248 memset (&tparser, 0, sizeof tparser);
18249 tparser.tokens = &tparser.tokens_buf[0];
18250 the_parser = &tparser;
18252 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18253 c_parser_pragma_pch_preprocess (&tparser);
18255 the_parser = ggc_alloc<c_parser> ();
18256 *the_parser = tparser;
18257 if (tparser.tokens == &tparser.tokens_buf[0])
18258 the_parser->tokens = &the_parser->tokens_buf[0];
18260 /* Initialize EH, if we've been told to do so. */
18261 if (flag_exceptions)
18262 using_eh_for_cleanups ();
18264 c_parser_translation_unit (the_parser);
18265 the_parser = NULL;
18268 /* Parse the body of a function declaration marked with "__RTL".
18270 The RTL parser works on the level of characters read from a
18271 FILE *, whereas c_parser works at the level of tokens.
18272 Square this circle by consuming all of the tokens up to and
18273 including the closing brace, recording the start/end of the RTL
18274 fragment, and reopening the file and re-reading the relevant
18275 lines within the RTL parser.
18277 This requires the opening and closing braces of the C function
18278 to be on separate lines from the RTL they wrap.
18280 Take ownership of START_WITH_PASS, if non-NULL. */
18282 void
18283 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18285 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18287 free (start_with_pass);
18288 return;
18291 location_t start_loc = c_parser_peek_token (parser)->location;
18293 /* Consume all tokens, up to the closing brace, handling
18294 matching pairs of braces in the rtl dump. */
18295 int num_open_braces = 1;
18296 while (1)
18298 switch (c_parser_peek_token (parser)->type)
18300 case CPP_OPEN_BRACE:
18301 num_open_braces++;
18302 break;
18303 case CPP_CLOSE_BRACE:
18304 if (--num_open_braces == 0)
18305 goto found_closing_brace;
18306 break;
18307 case CPP_EOF:
18308 error_at (start_loc, "no closing brace");
18309 free (start_with_pass);
18310 return;
18311 default:
18312 break;
18314 c_parser_consume_token (parser);
18317 found_closing_brace:
18318 /* At the closing brace; record its location. */
18319 location_t end_loc = c_parser_peek_token (parser)->location;
18321 /* Consume the closing brace. */
18322 c_parser_consume_token (parser);
18324 /* Invoke the RTL parser. */
18325 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18327 free (start_with_pass);
18328 return;
18331 /* If a pass name was provided for START_WITH_PASS, run the backend
18332 accordingly now, on the cfun created above, transferring
18333 ownership of START_WITH_PASS. */
18334 if (start_with_pass)
18335 run_rtl_passes (start_with_pass);
18338 #include "gt-c-c-parser.h"