OpenACC cache directive maintenance.
[official-gcc.git] / gcc / c / c-parser.c
blob40d4314d842e55b873926a8d8ea14b4a03be0eba
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2014 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 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h" /* For rtl.h: needs enum reg_class. */
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "stor-layout.h"
46 #include "varasm.h"
47 #include "trans-mem.h"
48 #include "langhooks.h"
49 #include "input.h"
50 #include "cpplib.h"
51 #include "timevar.h"
52 #include "c-family/c-pragma.h"
53 #include "c-tree.h"
54 #include "c-lang.h"
55 #include "flags.h"
56 #include "ggc.h"
57 #include "c-family/c-common.h"
58 #include "c-family/c-objc.h"
59 #include "vec.h"
60 #include "target.h"
61 #include "hash-map.h"
62 #include "is-a.h"
63 #include "plugin-api.h"
64 #include "hashtab.h"
65 #include "hash-set.h"
66 #include "machmode.h"
67 #include "hard-reg-set.h"
68 #include "function.h"
69 #include "ipa-ref.h"
70 #include "cgraph.h"
71 #include "plugin.h"
72 #include "omp-low.h"
73 #include "builtins.h"
76 /* Initialization routine for this file. */
78 void
79 c_parse_init (void)
81 /* The only initialization required is of the reserved word
82 identifiers. */
83 unsigned int i;
84 tree id;
85 int mask = 0;
87 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
88 the c_token structure. */
89 gcc_assert (RID_MAX <= 255);
91 mask |= D_CXXONLY;
92 if (!flag_isoc99)
93 mask |= D_C99;
94 if (flag_no_asm)
96 mask |= D_ASM | D_EXT;
97 if (!flag_isoc99)
98 mask |= D_EXT89;
100 if (!c_dialect_objc ())
101 mask |= D_OBJC | D_CXX_OBJC;
103 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
104 for (i = 0; i < num_c_common_reswords; i++)
106 /* If a keyword is disabled, do not enter it into the table
107 and so create a canonical spelling that isn't a keyword. */
108 if (c_common_reswords[i].disable & mask)
110 if (warn_cxx_compat
111 && (c_common_reswords[i].disable & D_CXXWARN))
113 id = get_identifier (c_common_reswords[i].word);
114 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
115 C_IS_RESERVED_WORD (id) = 1;
117 continue;
120 id = get_identifier (c_common_reswords[i].word);
121 C_SET_RID_CODE (id, c_common_reswords[i].rid);
122 C_IS_RESERVED_WORD (id) = 1;
123 ridpointers [(int) c_common_reswords[i].rid] = id;
126 for (i = 0; i < NUM_INT_N_ENTS; i++)
128 /* We always create the symbols but they aren't always supported. */
129 char name[50];
130 sprintf (name, "__int%d", int_n_data[i].bitsize);
131 id = get_identifier (xstrdup (name));
132 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
133 C_IS_RESERVED_WORD (id) = 1;
137 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
138 and the C parser. Unlike the C++ lexer, the parser structure
139 stores the lexer information instead of using a separate structure.
140 Identifiers are separated into ordinary identifiers, type names,
141 keywords and some other Objective-C types of identifiers, and some
142 look-ahead is maintained.
144 ??? It might be a good idea to lex the whole file up front (as for
145 C++). It would then be possible to share more of the C and C++
146 lexer code, if desired. */
148 /* More information about the type of a CPP_NAME token. */
149 typedef enum c_id_kind {
150 /* An ordinary identifier. */
151 C_ID_ID,
152 /* An identifier declared as a typedef name. */
153 C_ID_TYPENAME,
154 /* An identifier declared as an Objective-C class name. */
155 C_ID_CLASSNAME,
156 /* An address space identifier. */
157 C_ID_ADDRSPACE,
158 /* Not an identifier. */
159 C_ID_NONE
160 } c_id_kind;
162 /* A single C token after string literal concatenation and conversion
163 of preprocessing tokens to tokens. */
164 typedef struct GTY (()) c_token {
165 /* The kind of token. */
166 ENUM_BITFIELD (cpp_ttype) type : 8;
167 /* If this token is a CPP_NAME, this value indicates whether also
168 declared as some kind of type. Otherwise, it is C_ID_NONE. */
169 ENUM_BITFIELD (c_id_kind) id_kind : 8;
170 /* If this token is a keyword, this value indicates which keyword.
171 Otherwise, this value is RID_MAX. */
172 ENUM_BITFIELD (rid) keyword : 8;
173 /* If this token is a CPP_PRAGMA, this indicates the pragma that
174 was seen. Otherwise it is PRAGMA_NONE. */
175 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
176 /* The location at which this token was found. */
177 location_t location;
178 /* The value associated with this token, if any. */
179 tree value;
180 } c_token;
182 /* A parser structure recording information about the state and
183 context of parsing. Includes lexer information with up to two
184 tokens of look-ahead; more are not needed for C. */
185 typedef struct GTY(()) c_parser {
186 /* The look-ahead tokens. */
187 c_token * GTY((skip)) tokens;
188 /* Buffer for look-ahead tokens. */
189 c_token tokens_buf[2];
190 /* How many look-ahead tokens are available (0, 1 or 2, or
191 more if parsing from pre-lexed tokens). */
192 unsigned int tokens_avail;
193 /* True if a syntax error is being recovered from; false otherwise.
194 c_parser_error sets this flag. It should clear this flag when
195 enough tokens have been consumed to recover from the error. */
196 BOOL_BITFIELD error : 1;
197 /* True if we're processing a pragma, and shouldn't automatically
198 consume CPP_PRAGMA_EOL. */
199 BOOL_BITFIELD in_pragma : 1;
200 /* True if we're parsing the outermost block of an if statement. */
201 BOOL_BITFIELD in_if_block : 1;
202 /* True if we want to lex an untranslated string. */
203 BOOL_BITFIELD lex_untranslated_string : 1;
205 /* Objective-C specific parser/lexer information. */
207 /* True if we are in a context where the Objective-C "PQ" keywords
208 are considered keywords. */
209 BOOL_BITFIELD objc_pq_context : 1;
210 /* True if we are parsing a (potential) Objective-C foreach
211 statement. This is set to true after we parsed 'for (' and while
212 we wait for 'in' or ';' to decide if it's a standard C for loop or an
213 Objective-C foreach loop. */
214 BOOL_BITFIELD objc_could_be_foreach_context : 1;
215 /* The following flag is needed to contextualize Objective-C lexical
216 analysis. In some cases (e.g., 'int NSObject;'), it is
217 undesirable to bind an identifier to an Objective-C class, even
218 if a class with that name exists. */
219 BOOL_BITFIELD objc_need_raw_identifier : 1;
220 /* Nonzero if we're processing a __transaction statement. The value
221 is 1 | TM_STMT_ATTR_*. */
222 unsigned int in_transaction : 4;
223 /* True if we are in a context where the Objective-C "Property attribute"
224 keywords are valid. */
225 BOOL_BITFIELD objc_property_attr_context : 1;
227 /* Cilk Plus specific parser/lexer information. */
229 /* Buffer to hold all the tokens from parsing the vector attribute for the
230 SIMD-enabled functions (formerly known as elemental functions). */
231 vec <c_token, va_gc> *cilk_simd_fn_tokens;
232 } c_parser;
235 /* The actual parser and external interface. ??? Does this need to be
236 garbage-collected? */
238 static GTY (()) c_parser *the_parser;
240 /* Read in and lex a single token, storing it in *TOKEN. */
242 static void
243 c_lex_one_token (c_parser *parser, c_token *token)
245 timevar_push (TV_LEX);
247 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
248 (parser->lex_untranslated_string
249 ? C_LEX_STRING_NO_TRANSLATE : 0));
250 token->id_kind = C_ID_NONE;
251 token->keyword = RID_MAX;
252 token->pragma_kind = PRAGMA_NONE;
254 switch (token->type)
256 case CPP_NAME:
258 tree decl;
260 bool objc_force_identifier = parser->objc_need_raw_identifier;
261 if (c_dialect_objc ())
262 parser->objc_need_raw_identifier = false;
264 if (C_IS_RESERVED_WORD (token->value))
266 enum rid rid_code = C_RID_CODE (token->value);
268 if (rid_code == RID_CXX_COMPAT_WARN)
270 warning_at (token->location,
271 OPT_Wc___compat,
272 "identifier %qE conflicts with C++ keyword",
273 token->value);
275 else if (rid_code >= RID_FIRST_ADDR_SPACE
276 && rid_code <= RID_LAST_ADDR_SPACE)
278 token->id_kind = C_ID_ADDRSPACE;
279 token->keyword = rid_code;
280 break;
282 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
284 /* We found an Objective-C "pq" keyword (in, out,
285 inout, bycopy, byref, oneway). They need special
286 care because the interpretation depends on the
287 context. */
288 if (parser->objc_pq_context)
290 token->type = CPP_KEYWORD;
291 token->keyword = rid_code;
292 break;
294 else if (parser->objc_could_be_foreach_context
295 && rid_code == RID_IN)
297 /* We are in Objective-C, inside a (potential)
298 foreach context (which means after having
299 parsed 'for (', but before having parsed ';'),
300 and we found 'in'. We consider it the keyword
301 which terminates the declaration at the
302 beginning of a foreach-statement. Note that
303 this means you can't use 'in' for anything else
304 in that context; in particular, in Objective-C
305 you can't use 'in' as the name of the running
306 variable in a C for loop. We could potentially
307 try to add code here to disambiguate, but it
308 seems a reasonable limitation. */
309 token->type = CPP_KEYWORD;
310 token->keyword = rid_code;
311 break;
313 /* Else, "pq" keywords outside of the "pq" context are
314 not keywords, and we fall through to the code for
315 normal tokens. */
317 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
319 /* We found an Objective-C "property attribute"
320 keyword (getter, setter, readonly, etc). These are
321 only valid in the property context. */
322 if (parser->objc_property_attr_context)
324 token->type = CPP_KEYWORD;
325 token->keyword = rid_code;
326 break;
328 /* Else they are not special keywords.
331 else if (c_dialect_objc ()
332 && (OBJC_IS_AT_KEYWORD (rid_code)
333 || OBJC_IS_CXX_KEYWORD (rid_code)))
335 /* We found one of the Objective-C "@" keywords (defs,
336 selector, synchronized, etc) or one of the
337 Objective-C "cxx" keywords (class, private,
338 protected, public, try, catch, throw) without a
339 preceding '@' sign. Do nothing and fall through to
340 the code for normal tokens (in C++ we would still
341 consider the CXX ones keywords, but not in C). */
344 else
346 token->type = CPP_KEYWORD;
347 token->keyword = rid_code;
348 break;
352 decl = lookup_name (token->value);
353 if (decl)
355 if (TREE_CODE (decl) == TYPE_DECL)
357 token->id_kind = C_ID_TYPENAME;
358 break;
361 else if (c_dialect_objc ())
363 tree objc_interface_decl = objc_is_class_name (token->value);
364 /* Objective-C class names are in the same namespace as
365 variables and typedefs, and hence are shadowed by local
366 declarations. */
367 if (objc_interface_decl
368 && (!objc_force_identifier || global_bindings_p ()))
370 token->value = objc_interface_decl;
371 token->id_kind = C_ID_CLASSNAME;
372 break;
375 token->id_kind = C_ID_ID;
377 break;
378 case CPP_AT_NAME:
379 /* This only happens in Objective-C; it must be a keyword. */
380 token->type = CPP_KEYWORD;
381 switch (C_RID_CODE (token->value))
383 /* Replace 'class' with '@class', 'private' with '@private',
384 etc. This prevents confusion with the C++ keyword
385 'class', and makes the tokens consistent with other
386 Objective-C 'AT' keywords. For example '@class' is
387 reported as RID_AT_CLASS which is consistent with
388 '@synchronized', which is reported as
389 RID_AT_SYNCHRONIZED.
391 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
392 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
393 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
394 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
395 case RID_THROW: token->keyword = RID_AT_THROW; break;
396 case RID_TRY: token->keyword = RID_AT_TRY; break;
397 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
398 default: token->keyword = C_RID_CODE (token->value);
400 break;
401 case CPP_COLON:
402 case CPP_COMMA:
403 case CPP_CLOSE_PAREN:
404 case CPP_SEMICOLON:
405 /* These tokens may affect the interpretation of any identifiers
406 following, if doing Objective-C. */
407 if (c_dialect_objc ())
408 parser->objc_need_raw_identifier = false;
409 break;
410 case CPP_PRAGMA:
411 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
412 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
413 token->value = NULL;
414 break;
415 default:
416 break;
418 timevar_pop (TV_LEX);
421 /* Return a pointer to the next token from PARSER, reading it in if
422 necessary. */
424 static inline c_token *
425 c_parser_peek_token (c_parser *parser)
427 if (parser->tokens_avail == 0)
429 c_lex_one_token (parser, &parser->tokens[0]);
430 parser->tokens_avail = 1;
432 return &parser->tokens[0];
435 /* Return true if the next token from PARSER has the indicated
436 TYPE. */
438 static inline bool
439 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
441 return c_parser_peek_token (parser)->type == type;
444 /* Return true if the next token from PARSER does not have the
445 indicated TYPE. */
447 static inline bool
448 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
450 return !c_parser_next_token_is (parser, type);
453 /* Return true if the next token from PARSER is the indicated
454 KEYWORD. */
456 static inline bool
457 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
459 return c_parser_peek_token (parser)->keyword == keyword;
462 /* Return a pointer to the next-but-one token from PARSER, reading it
463 in if necessary. The next token is already read in. */
465 static c_token *
466 c_parser_peek_2nd_token (c_parser *parser)
468 if (parser->tokens_avail >= 2)
469 return &parser->tokens[1];
470 gcc_assert (parser->tokens_avail == 1);
471 gcc_assert (parser->tokens[0].type != CPP_EOF);
472 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
473 c_lex_one_token (parser, &parser->tokens[1]);
474 parser->tokens_avail = 2;
475 return &parser->tokens[1];
478 /* Return true if TOKEN can start a type name,
479 false otherwise. */
480 static bool
481 c_token_starts_typename (c_token *token)
483 switch (token->type)
485 case CPP_NAME:
486 switch (token->id_kind)
488 case C_ID_ID:
489 return false;
490 case C_ID_ADDRSPACE:
491 return true;
492 case C_ID_TYPENAME:
493 return true;
494 case C_ID_CLASSNAME:
495 gcc_assert (c_dialect_objc ());
496 return true;
497 default:
498 gcc_unreachable ();
500 case CPP_KEYWORD:
501 switch (token->keyword)
503 case RID_UNSIGNED:
504 case RID_LONG:
505 case RID_SHORT:
506 case RID_SIGNED:
507 case RID_COMPLEX:
508 case RID_INT:
509 case RID_CHAR:
510 case RID_FLOAT:
511 case RID_DOUBLE:
512 case RID_VOID:
513 case RID_DFLOAT32:
514 case RID_DFLOAT64:
515 case RID_DFLOAT128:
516 case RID_BOOL:
517 case RID_ENUM:
518 case RID_STRUCT:
519 case RID_UNION:
520 case RID_TYPEOF:
521 case RID_CONST:
522 case RID_ATOMIC:
523 case RID_VOLATILE:
524 case RID_RESTRICT:
525 case RID_ATTRIBUTE:
526 case RID_FRACT:
527 case RID_ACCUM:
528 case RID_SAT:
529 case RID_AUTO_TYPE:
530 return true;
531 default:
532 if (token->keyword >= RID_FIRST_INT_N
533 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
534 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
535 return true;
536 return false;
538 case CPP_LESS:
539 if (c_dialect_objc ())
540 return true;
541 return false;
542 default:
543 return false;
547 enum c_lookahead_kind {
548 /* Always treat unknown identifiers as typenames. */
549 cla_prefer_type,
551 /* Could be parsing a nonabstract declarator. Only treat an identifier
552 as a typename if followed by another identifier or a star. */
553 cla_nonabstract_decl,
555 /* Never treat identifiers as typenames. */
556 cla_prefer_id
559 /* Return true if the next token from PARSER can start a type name,
560 false otherwise. LA specifies how to do lookahead in order to
561 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
563 static inline bool
564 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
566 c_token *token = c_parser_peek_token (parser);
567 if (c_token_starts_typename (token))
568 return true;
570 /* Try a bit harder to detect an unknown typename. */
571 if (la != cla_prefer_id
572 && token->type == CPP_NAME
573 && token->id_kind == C_ID_ID
575 /* Do not try too hard when we could have "object in array". */
576 && !parser->objc_could_be_foreach_context
578 && (la == cla_prefer_type
579 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
580 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
582 /* Only unknown identifiers. */
583 && !lookup_name (token->value))
584 return true;
586 return false;
589 /* Return true if TOKEN is a type qualifier, false otherwise. */
590 static bool
591 c_token_is_qualifier (c_token *token)
593 switch (token->type)
595 case CPP_NAME:
596 switch (token->id_kind)
598 case C_ID_ADDRSPACE:
599 return true;
600 default:
601 return false;
603 case CPP_KEYWORD:
604 switch (token->keyword)
606 case RID_CONST:
607 case RID_VOLATILE:
608 case RID_RESTRICT:
609 case RID_ATTRIBUTE:
610 case RID_ATOMIC:
611 return true;
612 default:
613 return false;
615 case CPP_LESS:
616 return false;
617 default:
618 gcc_unreachable ();
622 /* Return true if the next token from PARSER is a type qualifier,
623 false otherwise. */
624 static inline bool
625 c_parser_next_token_is_qualifier (c_parser *parser)
627 c_token *token = c_parser_peek_token (parser);
628 return c_token_is_qualifier (token);
631 /* Return true if TOKEN can start declaration specifiers, false
632 otherwise. */
633 static bool
634 c_token_starts_declspecs (c_token *token)
636 switch (token->type)
638 case CPP_NAME:
639 switch (token->id_kind)
641 case C_ID_ID:
642 return false;
643 case C_ID_ADDRSPACE:
644 return true;
645 case C_ID_TYPENAME:
646 return true;
647 case C_ID_CLASSNAME:
648 gcc_assert (c_dialect_objc ());
649 return true;
650 default:
651 gcc_unreachable ();
653 case CPP_KEYWORD:
654 switch (token->keyword)
656 case RID_STATIC:
657 case RID_EXTERN:
658 case RID_REGISTER:
659 case RID_TYPEDEF:
660 case RID_INLINE:
661 case RID_NORETURN:
662 case RID_AUTO:
663 case RID_THREAD:
664 case RID_UNSIGNED:
665 case RID_LONG:
666 case RID_SHORT:
667 case RID_SIGNED:
668 case RID_COMPLEX:
669 case RID_INT:
670 case RID_CHAR:
671 case RID_FLOAT:
672 case RID_DOUBLE:
673 case RID_VOID:
674 case RID_DFLOAT32:
675 case RID_DFLOAT64:
676 case RID_DFLOAT128:
677 case RID_BOOL:
678 case RID_ENUM:
679 case RID_STRUCT:
680 case RID_UNION:
681 case RID_TYPEOF:
682 case RID_CONST:
683 case RID_VOLATILE:
684 case RID_RESTRICT:
685 case RID_ATTRIBUTE:
686 case RID_FRACT:
687 case RID_ACCUM:
688 case RID_SAT:
689 case RID_ALIGNAS:
690 case RID_ATOMIC:
691 case RID_AUTO_TYPE:
692 return true;
693 default:
694 if (token->keyword >= RID_FIRST_INT_N
695 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
696 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
697 return true;
698 return false;
700 case CPP_LESS:
701 if (c_dialect_objc ())
702 return true;
703 return false;
704 default:
705 return false;
710 /* Return true if TOKEN can start declaration specifiers or a static
711 assertion, false otherwise. */
712 static bool
713 c_token_starts_declaration (c_token *token)
715 if (c_token_starts_declspecs (token)
716 || token->keyword == RID_STATIC_ASSERT)
717 return true;
718 else
719 return false;
722 /* Return true if the next token from PARSER can start declaration
723 specifiers, false otherwise. */
724 static inline bool
725 c_parser_next_token_starts_declspecs (c_parser *parser)
727 c_token *token = c_parser_peek_token (parser);
729 /* In Objective-C, a classname normally starts a declspecs unless it
730 is immediately followed by a dot. In that case, it is the
731 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
732 setter/getter on the class. c_token_starts_declspecs() can't
733 differentiate between the two cases because it only checks the
734 current token, so we have a special check here. */
735 if (c_dialect_objc ()
736 && token->type == CPP_NAME
737 && token->id_kind == C_ID_CLASSNAME
738 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
739 return false;
741 return c_token_starts_declspecs (token);
744 /* Return true if the next tokens from PARSER can start declaration
745 specifiers or a static assertion, false otherwise. */
746 static inline bool
747 c_parser_next_tokens_start_declaration (c_parser *parser)
749 c_token *token = c_parser_peek_token (parser);
751 /* Same as above. */
752 if (c_dialect_objc ()
753 && token->type == CPP_NAME
754 && token->id_kind == C_ID_CLASSNAME
755 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
756 return false;
758 /* Labels do not start declarations. */
759 if (token->type == CPP_NAME
760 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
761 return false;
763 if (c_token_starts_declaration (token))
764 return true;
766 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
767 return true;
769 return false;
772 /* Consume the next token from PARSER. */
774 static void
775 c_parser_consume_token (c_parser *parser)
777 gcc_assert (parser->tokens_avail >= 1);
778 gcc_assert (parser->tokens[0].type != CPP_EOF);
779 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
780 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
781 if (parser->tokens != &parser->tokens_buf[0])
782 parser->tokens++;
783 else if (parser->tokens_avail == 2)
784 parser->tokens[0] = parser->tokens[1];
785 parser->tokens_avail--;
788 /* Expect the current token to be a #pragma. Consume it and remember
789 that we've begun parsing a pragma. */
791 static void
792 c_parser_consume_pragma (c_parser *parser)
794 gcc_assert (!parser->in_pragma);
795 gcc_assert (parser->tokens_avail >= 1);
796 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
797 if (parser->tokens != &parser->tokens_buf[0])
798 parser->tokens++;
799 else if (parser->tokens_avail == 2)
800 parser->tokens[0] = parser->tokens[1];
801 parser->tokens_avail--;
802 parser->in_pragma = true;
805 /* Update the global input_location from TOKEN. */
806 static inline void
807 c_parser_set_source_position_from_token (c_token *token)
809 if (token->type != CPP_EOF)
811 input_location = token->location;
815 /* Issue a diagnostic of the form
816 FILE:LINE: MESSAGE before TOKEN
817 where TOKEN is the next token in the input stream of PARSER.
818 MESSAGE (specified by the caller) is usually of the form "expected
819 OTHER-TOKEN".
821 Do not issue a diagnostic if still recovering from an error.
823 ??? This is taken from the C++ parser, but building up messages in
824 this way is not i18n-friendly and some other approach should be
825 used. */
827 static void
828 c_parser_error (c_parser *parser, const char *gmsgid)
830 c_token *token = c_parser_peek_token (parser);
831 if (parser->error)
832 return;
833 parser->error = true;
834 if (!gmsgid)
835 return;
836 /* This diagnostic makes more sense if it is tagged to the line of
837 the token we just peeked at. */
838 c_parser_set_source_position_from_token (token);
839 c_parse_error (gmsgid,
840 /* Because c_parse_error does not understand
841 CPP_KEYWORD, keywords are treated like
842 identifiers. */
843 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
844 /* ??? The C parser does not save the cpp flags of a
845 token, we need to pass 0 here and we will not get
846 the source spelling of some tokens but rather the
847 canonical spelling. */
848 token->value, /*flags=*/0);
851 /* If the next token is of the indicated TYPE, consume it. Otherwise,
852 issue the error MSGID. If MSGID is NULL then a message has already
853 been produced and no message will be produced this time. Returns
854 true if found, false otherwise. */
856 static bool
857 c_parser_require (c_parser *parser,
858 enum cpp_ttype type,
859 const char *msgid)
861 if (c_parser_next_token_is (parser, type))
863 c_parser_consume_token (parser);
864 return true;
866 else
868 c_parser_error (parser, msgid);
869 return false;
873 /* If the next token is the indicated keyword, consume it. Otherwise,
874 issue the error MSGID. Returns true if found, false otherwise. */
876 static bool
877 c_parser_require_keyword (c_parser *parser,
878 enum rid keyword,
879 const char *msgid)
881 if (c_parser_next_token_is_keyword (parser, keyword))
883 c_parser_consume_token (parser);
884 return true;
886 else
888 c_parser_error (parser, msgid);
889 return false;
893 /* Like c_parser_require, except that tokens will be skipped until the
894 desired token is found. An error message is still produced if the
895 next token is not as expected. If MSGID is NULL then a message has
896 already been produced and no message will be produced this
897 time. */
899 static void
900 c_parser_skip_until_found (c_parser *parser,
901 enum cpp_ttype type,
902 const char *msgid)
904 unsigned nesting_depth = 0;
906 if (c_parser_require (parser, type, msgid))
907 return;
909 /* Skip tokens until the desired token is found. */
910 while (true)
912 /* Peek at the next token. */
913 c_token *token = c_parser_peek_token (parser);
914 /* If we've reached the token we want, consume it and stop. */
915 if (token->type == type && !nesting_depth)
917 c_parser_consume_token (parser);
918 break;
921 /* If we've run out of tokens, stop. */
922 if (token->type == CPP_EOF)
923 return;
924 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
925 return;
926 if (token->type == CPP_OPEN_BRACE
927 || token->type == CPP_OPEN_PAREN
928 || token->type == CPP_OPEN_SQUARE)
929 ++nesting_depth;
930 else if (token->type == CPP_CLOSE_BRACE
931 || token->type == CPP_CLOSE_PAREN
932 || token->type == CPP_CLOSE_SQUARE)
934 if (nesting_depth-- == 0)
935 break;
937 /* Consume this token. */
938 c_parser_consume_token (parser);
940 parser->error = false;
943 /* Skip tokens until the end of a parameter is found, but do not
944 consume the comma, semicolon or closing delimiter. */
946 static void
947 c_parser_skip_to_end_of_parameter (c_parser *parser)
949 unsigned nesting_depth = 0;
951 while (true)
953 c_token *token = c_parser_peek_token (parser);
954 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
955 && !nesting_depth)
956 break;
957 /* If we've run out of tokens, stop. */
958 if (token->type == CPP_EOF)
959 return;
960 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
961 return;
962 if (token->type == CPP_OPEN_BRACE
963 || token->type == CPP_OPEN_PAREN
964 || token->type == CPP_OPEN_SQUARE)
965 ++nesting_depth;
966 else if (token->type == CPP_CLOSE_BRACE
967 || token->type == CPP_CLOSE_PAREN
968 || token->type == CPP_CLOSE_SQUARE)
970 if (nesting_depth-- == 0)
971 break;
973 /* Consume this token. */
974 c_parser_consume_token (parser);
976 parser->error = false;
979 /* Expect to be at the end of the pragma directive and consume an
980 end of line marker. */
982 static void
983 c_parser_skip_to_pragma_eol (c_parser *parser)
985 gcc_assert (parser->in_pragma);
986 parser->in_pragma = false;
988 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
989 while (true)
991 c_token *token = c_parser_peek_token (parser);
992 if (token->type == CPP_EOF)
993 break;
994 if (token->type == CPP_PRAGMA_EOL)
996 c_parser_consume_token (parser);
997 break;
999 c_parser_consume_token (parser);
1002 parser->error = false;
1005 /* Skip tokens until we have consumed an entire block, or until we
1006 have consumed a non-nested ';'. */
1008 static void
1009 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1011 unsigned nesting_depth = 0;
1012 bool save_error = parser->error;
1014 while (true)
1016 c_token *token;
1018 /* Peek at the next token. */
1019 token = c_parser_peek_token (parser);
1021 switch (token->type)
1023 case CPP_EOF:
1024 return;
1026 case CPP_PRAGMA_EOL:
1027 if (parser->in_pragma)
1028 return;
1029 break;
1031 case CPP_SEMICOLON:
1032 /* If the next token is a ';', we have reached the
1033 end of the statement. */
1034 if (!nesting_depth)
1036 /* Consume the ';'. */
1037 c_parser_consume_token (parser);
1038 goto finished;
1040 break;
1042 case CPP_CLOSE_BRACE:
1043 /* If the next token is a non-nested '}', then we have
1044 reached the end of the current block. */
1045 if (nesting_depth == 0 || --nesting_depth == 0)
1047 c_parser_consume_token (parser);
1048 goto finished;
1050 break;
1052 case CPP_OPEN_BRACE:
1053 /* If it the next token is a '{', then we are entering a new
1054 block. Consume the entire block. */
1055 ++nesting_depth;
1056 break;
1058 case CPP_PRAGMA:
1059 /* If we see a pragma, consume the whole thing at once. We
1060 have some safeguards against consuming pragmas willy-nilly.
1061 Normally, we'd expect to be here with parser->error set,
1062 which disables these safeguards. But it's possible to get
1063 here for secondary error recovery, after parser->error has
1064 been cleared. */
1065 c_parser_consume_pragma (parser);
1066 c_parser_skip_to_pragma_eol (parser);
1067 parser->error = save_error;
1068 continue;
1070 default:
1071 break;
1074 c_parser_consume_token (parser);
1077 finished:
1078 parser->error = false;
1081 /* CPP's options (initialized by c-opts.c). */
1082 extern cpp_options *cpp_opts;
1084 /* Save the warning flags which are controlled by __extension__. */
1086 static inline int
1087 disable_extension_diagnostics (void)
1089 int ret = (pedantic
1090 | (warn_pointer_arith << 1)
1091 | (warn_traditional << 2)
1092 | (flag_iso << 3)
1093 | (warn_long_long << 4)
1094 | (warn_cxx_compat << 5)
1095 | (warn_overlength_strings << 6)
1096 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1097 play tricks to properly restore it. */
1098 | ((warn_c90_c99_compat == 1) << 7)
1099 | ((warn_c90_c99_compat == -1) << 8)
1100 /* Similarly for warn_c99_c11_compat. */
1101 | ((warn_c99_c11_compat == 1) << 9)
1102 | ((warn_c99_c11_compat == -1) << 10)
1104 cpp_opts->cpp_pedantic = pedantic = 0;
1105 warn_pointer_arith = 0;
1106 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1107 flag_iso = 0;
1108 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1109 warn_cxx_compat = 0;
1110 warn_overlength_strings = 0;
1111 warn_c90_c99_compat = 0;
1112 warn_c99_c11_compat = 0;
1113 return ret;
1116 /* Restore the warning flags which are controlled by __extension__.
1117 FLAGS is the return value from disable_extension_diagnostics. */
1119 static inline void
1120 restore_extension_diagnostics (int flags)
1122 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1123 warn_pointer_arith = (flags >> 1) & 1;
1124 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1125 flag_iso = (flags >> 3) & 1;
1126 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1127 warn_cxx_compat = (flags >> 5) & 1;
1128 warn_overlength_strings = (flags >> 6) & 1;
1129 /* See above for why is this needed. */
1130 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1131 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1134 /* Possibly kinds of declarator to parse. */
1135 typedef enum c_dtr_syn {
1136 /* A normal declarator with an identifier. */
1137 C_DTR_NORMAL,
1138 /* An abstract declarator (maybe empty). */
1139 C_DTR_ABSTRACT,
1140 /* A parameter declarator: may be either, but after a type name does
1141 not redeclare a typedef name as an identifier if it can
1142 alternatively be interpreted as a typedef name; see DR#009,
1143 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1144 following DR#249. For example, given a typedef T, "int T" and
1145 "int *T" are valid parameter declarations redeclaring T, while
1146 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1147 abstract declarators rather than involving redundant parentheses;
1148 the same applies with attributes inside the parentheses before
1149 "T". */
1150 C_DTR_PARM
1151 } c_dtr_syn;
1153 /* The binary operation precedence levels, where 0 is a dummy lowest level
1154 used for the bottom of the stack. */
1155 enum c_parser_prec {
1156 PREC_NONE,
1157 PREC_LOGOR,
1158 PREC_LOGAND,
1159 PREC_BITOR,
1160 PREC_BITXOR,
1161 PREC_BITAND,
1162 PREC_EQ,
1163 PREC_REL,
1164 PREC_SHIFT,
1165 PREC_ADD,
1166 PREC_MULT,
1167 NUM_PRECS
1170 static void c_parser_external_declaration (c_parser *);
1171 static void c_parser_asm_definition (c_parser *);
1172 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1173 bool, bool, tree *, vec<c_token>);
1174 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1175 static void c_parser_static_assert_declaration (c_parser *);
1176 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1177 bool, bool, bool, enum c_lookahead_kind);
1178 static struct c_typespec c_parser_enum_specifier (c_parser *);
1179 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1180 static tree c_parser_struct_declaration (c_parser *);
1181 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1182 static tree c_parser_alignas_specifier (c_parser *);
1183 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1184 bool *);
1185 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1186 c_dtr_syn, bool *);
1187 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1188 bool,
1189 struct c_declarator *);
1190 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1191 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1192 tree);
1193 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1194 static tree c_parser_simple_asm_expr (c_parser *);
1195 static tree c_parser_attributes (c_parser *);
1196 static struct c_type_name *c_parser_type_name (c_parser *);
1197 static struct c_expr c_parser_initializer (c_parser *);
1198 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1199 static void c_parser_initelt (c_parser *, struct obstack *);
1200 static void c_parser_initval (c_parser *, struct c_expr *,
1201 struct obstack *);
1202 static tree c_parser_compound_statement (c_parser *);
1203 static void c_parser_compound_statement_nostart (c_parser *);
1204 static void c_parser_label (c_parser *);
1205 static void c_parser_statement (c_parser *);
1206 static void c_parser_statement_after_labels (c_parser *);
1207 static void c_parser_if_statement (c_parser *);
1208 static void c_parser_switch_statement (c_parser *);
1209 static void c_parser_while_statement (c_parser *, bool);
1210 static void c_parser_do_statement (c_parser *, bool);
1211 static void c_parser_for_statement (c_parser *, bool);
1212 static tree c_parser_asm_statement (c_parser *);
1213 static tree c_parser_asm_operands (c_parser *);
1214 static tree c_parser_asm_goto_operands (c_parser *);
1215 static tree c_parser_asm_clobbers (c_parser *);
1216 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1217 tree = NULL_TREE);
1218 static struct c_expr c_parser_conditional_expression (c_parser *,
1219 struct c_expr *, tree);
1220 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1221 tree);
1222 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1223 static struct c_expr c_parser_unary_expression (c_parser *);
1224 static struct c_expr c_parser_sizeof_expression (c_parser *);
1225 static struct c_expr c_parser_alignof_expression (c_parser *);
1226 static struct c_expr c_parser_postfix_expression (c_parser *);
1227 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1228 struct c_type_name *,
1229 location_t);
1230 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1231 location_t loc,
1232 struct c_expr);
1233 static tree c_parser_transaction (c_parser *, enum rid);
1234 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1235 static tree c_parser_transaction_cancel (c_parser *);
1236 static struct c_expr c_parser_expression (c_parser *);
1237 static struct c_expr c_parser_expression_conv (c_parser *);
1238 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1239 vec<tree, va_gc> **, location_t *,
1240 tree *, vec<location_t> *,
1241 unsigned int * = NULL);
1242 static tree c_parser_oacc_loop (location_t, c_parser *, char *);
1243 static void c_parser_omp_construct (c_parser *);
1244 static void c_parser_omp_threadprivate (c_parser *);
1245 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1246 static void c_parser_oacc_update (c_parser *);
1247 static void c_parser_omp_barrier (c_parser *);
1248 static void c_parser_omp_flush (c_parser *);
1249 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1250 tree, tree *);
1251 static void c_parser_omp_taskwait (c_parser *);
1252 static void c_parser_omp_taskyield (c_parser *);
1253 static void c_parser_omp_cancel (c_parser *);
1254 static void c_parser_omp_cancellation_point (c_parser *);
1256 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1257 pragma_stmt, pragma_compound };
1258 static bool c_parser_pragma (c_parser *, enum pragma_context);
1259 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1260 static void c_parser_omp_end_declare_target (c_parser *);
1261 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1263 /* These Objective-C parser functions are only ever called when
1264 compiling Objective-C. */
1265 static void c_parser_objc_class_definition (c_parser *, tree);
1266 static void c_parser_objc_class_instance_variables (c_parser *);
1267 static void c_parser_objc_class_declaration (c_parser *);
1268 static void c_parser_objc_alias_declaration (c_parser *);
1269 static void c_parser_objc_protocol_definition (c_parser *, tree);
1270 static bool c_parser_objc_method_type (c_parser *);
1271 static void c_parser_objc_method_definition (c_parser *);
1272 static void c_parser_objc_methodprotolist (c_parser *);
1273 static void c_parser_objc_methodproto (c_parser *);
1274 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1275 static tree c_parser_objc_type_name (c_parser *);
1276 static tree c_parser_objc_protocol_refs (c_parser *);
1277 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1278 static void c_parser_objc_synchronized_statement (c_parser *);
1279 static tree c_parser_objc_selector (c_parser *);
1280 static tree c_parser_objc_selector_arg (c_parser *);
1281 static tree c_parser_objc_receiver (c_parser *);
1282 static tree c_parser_objc_message_args (c_parser *);
1283 static tree c_parser_objc_keywordexpr (c_parser *);
1284 static void c_parser_objc_at_property_declaration (c_parser *);
1285 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1286 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1287 static bool c_parser_objc_diagnose_bad_element_prefix
1288 (c_parser *, struct c_declspecs *);
1290 /* Cilk Plus supporting routines. */
1291 static void c_parser_cilk_simd (c_parser *);
1292 static void c_parser_cilk_for (c_parser *, tree);
1293 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1294 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1295 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1296 static void c_parser_cilk_grainsize (c_parser *);
1298 /* Parse a translation unit (C90 6.7, C99 6.9).
1300 translation-unit:
1301 external-declarations
1303 external-declarations:
1304 external-declaration
1305 external-declarations external-declaration
1307 GNU extensions:
1309 translation-unit:
1310 empty
1313 static void
1314 c_parser_translation_unit (c_parser *parser)
1316 if (c_parser_next_token_is (parser, CPP_EOF))
1318 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1319 "ISO C forbids an empty translation unit");
1321 else
1323 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1324 mark_valid_location_for_stdc_pragma (false);
1327 ggc_collect ();
1328 c_parser_external_declaration (parser);
1329 obstack_free (&parser_obstack, obstack_position);
1331 while (c_parser_next_token_is_not (parser, CPP_EOF));
1335 /* Parse an external declaration (C90 6.7, C99 6.9).
1337 external-declaration:
1338 function-definition
1339 declaration
1341 GNU extensions:
1343 external-declaration:
1344 asm-definition
1346 __extension__ external-declaration
1348 Objective-C:
1350 external-declaration:
1351 objc-class-definition
1352 objc-class-declaration
1353 objc-alias-declaration
1354 objc-protocol-definition
1355 objc-method-definition
1356 @end
1359 static void
1360 c_parser_external_declaration (c_parser *parser)
1362 int ext;
1363 switch (c_parser_peek_token (parser)->type)
1365 case CPP_KEYWORD:
1366 switch (c_parser_peek_token (parser)->keyword)
1368 case RID_EXTENSION:
1369 ext = disable_extension_diagnostics ();
1370 c_parser_consume_token (parser);
1371 c_parser_external_declaration (parser);
1372 restore_extension_diagnostics (ext);
1373 break;
1374 case RID_ASM:
1375 c_parser_asm_definition (parser);
1376 break;
1377 case RID_AT_INTERFACE:
1378 case RID_AT_IMPLEMENTATION:
1379 gcc_assert (c_dialect_objc ());
1380 c_parser_objc_class_definition (parser, NULL_TREE);
1381 break;
1382 case RID_AT_CLASS:
1383 gcc_assert (c_dialect_objc ());
1384 c_parser_objc_class_declaration (parser);
1385 break;
1386 case RID_AT_ALIAS:
1387 gcc_assert (c_dialect_objc ());
1388 c_parser_objc_alias_declaration (parser);
1389 break;
1390 case RID_AT_PROTOCOL:
1391 gcc_assert (c_dialect_objc ());
1392 c_parser_objc_protocol_definition (parser, NULL_TREE);
1393 break;
1394 case RID_AT_PROPERTY:
1395 gcc_assert (c_dialect_objc ());
1396 c_parser_objc_at_property_declaration (parser);
1397 break;
1398 case RID_AT_SYNTHESIZE:
1399 gcc_assert (c_dialect_objc ());
1400 c_parser_objc_at_synthesize_declaration (parser);
1401 break;
1402 case RID_AT_DYNAMIC:
1403 gcc_assert (c_dialect_objc ());
1404 c_parser_objc_at_dynamic_declaration (parser);
1405 break;
1406 case RID_AT_END:
1407 gcc_assert (c_dialect_objc ());
1408 c_parser_consume_token (parser);
1409 objc_finish_implementation ();
1410 break;
1411 default:
1412 goto decl_or_fndef;
1414 break;
1415 case CPP_SEMICOLON:
1416 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1417 "ISO C does not allow extra %<;%> outside of a function");
1418 c_parser_consume_token (parser);
1419 break;
1420 case CPP_PRAGMA:
1421 mark_valid_location_for_stdc_pragma (true);
1422 c_parser_pragma (parser, pragma_external);
1423 mark_valid_location_for_stdc_pragma (false);
1424 break;
1425 case CPP_PLUS:
1426 case CPP_MINUS:
1427 if (c_dialect_objc ())
1429 c_parser_objc_method_definition (parser);
1430 break;
1432 /* Else fall through, and yield a syntax error trying to parse
1433 as a declaration or function definition. */
1434 default:
1435 decl_or_fndef:
1436 /* A declaration or a function definition (or, in Objective-C,
1437 an @interface or @protocol with prefix attributes). We can
1438 only tell which after parsing the declaration specifiers, if
1439 any, and the first declarator. */
1440 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1441 NULL, vNULL);
1442 break;
1446 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1448 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1449 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1450 accepted; otherwise (old-style parameter declarations) only other
1451 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1452 assertion is accepted; otherwise (old-style parameter declarations)
1453 it is not. If NESTED is true, we are inside a function or parsing
1454 old-style parameter declarations; any functions encountered are
1455 nested functions and declaration specifiers are required; otherwise
1456 we are at top level and functions are normal functions and
1457 declaration specifiers may be optional. If EMPTY_OK is true, empty
1458 declarations are OK (subject to all other constraints); otherwise
1459 (old-style parameter declarations) they are diagnosed. If
1460 START_ATTR_OK is true, the declaration specifiers may start with
1461 attributes; otherwise they may not.
1462 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1463 declaration when parsing an Objective-C foreach statement.
1465 declaration:
1466 declaration-specifiers init-declarator-list[opt] ;
1467 static_assert-declaration
1469 function-definition:
1470 declaration-specifiers[opt] declarator declaration-list[opt]
1471 compound-statement
1473 declaration-list:
1474 declaration
1475 declaration-list declaration
1477 init-declarator-list:
1478 init-declarator
1479 init-declarator-list , init-declarator
1481 init-declarator:
1482 declarator simple-asm-expr[opt] attributes[opt]
1483 declarator simple-asm-expr[opt] attributes[opt] = initializer
1485 GNU extensions:
1487 nested-function-definition:
1488 declaration-specifiers declarator declaration-list[opt]
1489 compound-statement
1491 Objective-C:
1492 attributes objc-class-definition
1493 attributes objc-category-definition
1494 attributes objc-protocol-definition
1496 The simple-asm-expr and attributes are GNU extensions.
1498 This function does not handle __extension__; that is handled in its
1499 callers. ??? Following the old parser, __extension__ may start
1500 external declarations, declarations in functions and declarations
1501 at the start of "for" loops, but not old-style parameter
1502 declarations.
1504 C99 requires declaration specifiers in a function definition; the
1505 absence is diagnosed through the diagnosis of implicit int. In GNU
1506 C we also allow but diagnose declarations without declaration
1507 specifiers, but only at top level (elsewhere they conflict with
1508 other syntax).
1510 In Objective-C, declarations of the looping variable in a foreach
1511 statement are exceptionally terminated by 'in' (for example, 'for
1512 (NSObject *object in array) { ... }').
1514 OpenMP:
1516 declaration:
1517 threadprivate-directive */
1519 static void
1520 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1521 bool static_assert_ok, bool empty_ok,
1522 bool nested, bool start_attr_ok,
1523 tree *objc_foreach_object_declaration,
1524 vec<c_token> omp_declare_simd_clauses)
1526 struct c_declspecs *specs;
1527 tree prefix_attrs;
1528 tree all_prefix_attrs;
1529 bool diagnosed_no_specs = false;
1530 location_t here = c_parser_peek_token (parser)->location;
1532 if (static_assert_ok
1533 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1535 c_parser_static_assert_declaration (parser);
1536 return;
1538 specs = build_null_declspecs ();
1540 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1541 if (c_parser_peek_token (parser)->type == CPP_NAME
1542 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1543 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1544 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1545 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1547 error_at (here, "unknown type name %qE",
1548 c_parser_peek_token (parser)->value);
1550 /* Parse declspecs normally to get a correct pointer type, but avoid
1551 a further "fails to be a type name" error. Refuse nested functions
1552 since it is not how the user likely wants us to recover. */
1553 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1554 c_parser_peek_token (parser)->keyword = RID_VOID;
1555 c_parser_peek_token (parser)->value = error_mark_node;
1556 fndef_ok = !nested;
1559 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1560 true, true, cla_nonabstract_decl);
1561 if (parser->error)
1563 c_parser_skip_to_end_of_block_or_statement (parser);
1564 return;
1566 if (nested && !specs->declspecs_seen_p)
1568 c_parser_error (parser, "expected declaration specifiers");
1569 c_parser_skip_to_end_of_block_or_statement (parser);
1570 return;
1572 finish_declspecs (specs);
1573 bool auto_type_p = specs->typespec_word == cts_auto_type;
1574 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1576 if (auto_type_p)
1577 error_at (here, "%<__auto_type%> in empty declaration");
1578 else if (empty_ok)
1579 shadow_tag (specs);
1580 else
1582 shadow_tag_warned (specs, 1);
1583 pedwarn (here, 0, "empty declaration");
1585 c_parser_consume_token (parser);
1586 return;
1589 /* Provide better error recovery. Note that a type name here is usually
1590 better diagnosed as a redeclaration. */
1591 if (empty_ok
1592 && specs->typespec_kind == ctsk_tagdef
1593 && c_parser_next_token_starts_declspecs (parser)
1594 && !c_parser_next_token_is (parser, CPP_NAME))
1596 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1597 parser->error = false;
1598 shadow_tag_warned (specs, 1);
1599 return;
1601 else if (c_dialect_objc () && !auto_type_p)
1603 /* Prefix attributes are an error on method decls. */
1604 switch (c_parser_peek_token (parser)->type)
1606 case CPP_PLUS:
1607 case CPP_MINUS:
1608 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1609 return;
1610 if (specs->attrs)
1612 warning_at (c_parser_peek_token (parser)->location,
1613 OPT_Wattributes,
1614 "prefix attributes are ignored for methods");
1615 specs->attrs = NULL_TREE;
1617 if (fndef_ok)
1618 c_parser_objc_method_definition (parser);
1619 else
1620 c_parser_objc_methodproto (parser);
1621 return;
1622 break;
1623 default:
1624 break;
1626 /* This is where we parse 'attributes @interface ...',
1627 'attributes @implementation ...', 'attributes @protocol ...'
1628 (where attributes could be, for example, __attribute__
1629 ((deprecated)).
1631 switch (c_parser_peek_token (parser)->keyword)
1633 case RID_AT_INTERFACE:
1635 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1636 return;
1637 c_parser_objc_class_definition (parser, specs->attrs);
1638 return;
1640 break;
1641 case RID_AT_IMPLEMENTATION:
1643 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1644 return;
1645 if (specs->attrs)
1647 warning_at (c_parser_peek_token (parser)->location,
1648 OPT_Wattributes,
1649 "prefix attributes are ignored for implementations");
1650 specs->attrs = NULL_TREE;
1652 c_parser_objc_class_definition (parser, NULL_TREE);
1653 return;
1655 break;
1656 case RID_AT_PROTOCOL:
1658 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1659 return;
1660 c_parser_objc_protocol_definition (parser, specs->attrs);
1661 return;
1663 break;
1664 case RID_AT_ALIAS:
1665 case RID_AT_CLASS:
1666 case RID_AT_END:
1667 case RID_AT_PROPERTY:
1668 if (specs->attrs)
1670 c_parser_error (parser, "unexpected attribute");
1671 specs->attrs = NULL;
1673 break;
1674 default:
1675 break;
1679 pending_xref_error ();
1680 prefix_attrs = specs->attrs;
1681 all_prefix_attrs = prefix_attrs;
1682 specs->attrs = NULL_TREE;
1683 while (true)
1685 struct c_declarator *declarator;
1686 bool dummy = false;
1687 timevar_id_t tv;
1688 tree fnbody;
1689 /* Declaring either one or more declarators (in which case we
1690 should diagnose if there were no declaration specifiers) or a
1691 function definition (in which case the diagnostic for
1692 implicit int suffices). */
1693 declarator = c_parser_declarator (parser,
1694 specs->typespec_kind != ctsk_none,
1695 C_DTR_NORMAL, &dummy);
1696 if (declarator == NULL)
1698 if (omp_declare_simd_clauses.exists ()
1699 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1700 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1701 omp_declare_simd_clauses);
1702 c_parser_skip_to_end_of_block_or_statement (parser);
1703 return;
1705 if (auto_type_p && declarator->kind != cdk_id)
1707 error_at (here,
1708 "%<__auto_type%> requires a plain identifier"
1709 " as declarator");
1710 c_parser_skip_to_end_of_block_or_statement (parser);
1711 return;
1713 if (c_parser_next_token_is (parser, CPP_EQ)
1714 || c_parser_next_token_is (parser, CPP_COMMA)
1715 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1716 || c_parser_next_token_is_keyword (parser, RID_ASM)
1717 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1718 || c_parser_next_token_is_keyword (parser, RID_IN))
1720 tree asm_name = NULL_TREE;
1721 tree postfix_attrs = NULL_TREE;
1722 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1724 diagnosed_no_specs = true;
1725 pedwarn (here, 0, "data definition has no type or storage class");
1727 /* Having seen a data definition, there cannot now be a
1728 function definition. */
1729 fndef_ok = false;
1730 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1731 asm_name = c_parser_simple_asm_expr (parser);
1732 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1734 postfix_attrs = c_parser_attributes (parser);
1735 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1737 /* This means there is an attribute specifier after
1738 the declarator in a function definition. Provide
1739 some more information for the user. */
1740 error_at (here, "attributes should be specified before the "
1741 "declarator in a function definition");
1742 c_parser_skip_to_end_of_block_or_statement (parser);
1743 return;
1746 if (c_parser_next_token_is (parser, CPP_EQ))
1748 tree d;
1749 struct c_expr init;
1750 location_t init_loc;
1751 c_parser_consume_token (parser);
1752 if (auto_type_p)
1754 start_init (NULL_TREE, asm_name, global_bindings_p ());
1755 init_loc = c_parser_peek_token (parser)->location;
1756 init = c_parser_expr_no_commas (parser, NULL);
1757 if (TREE_CODE (init.value) == COMPONENT_REF
1758 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1759 error_at (here,
1760 "%<__auto_type%> used with a bit-field"
1761 " initializer");
1762 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1763 tree init_type = TREE_TYPE (init.value);
1764 /* As with typeof, remove all qualifiers from atomic types. */
1765 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1766 init_type
1767 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1768 bool vm_type = variably_modified_type_p (init_type,
1769 NULL_TREE);
1770 if (vm_type)
1771 init.value = c_save_expr (init.value);
1772 finish_init ();
1773 specs->typespec_kind = ctsk_typeof;
1774 specs->locations[cdw_typedef] = init_loc;
1775 specs->typedef_p = true;
1776 specs->type = init_type;
1777 if (vm_type)
1779 bool maybe_const = true;
1780 tree type_expr = c_fully_fold (init.value, false,
1781 &maybe_const);
1782 specs->expr_const_operands &= maybe_const;
1783 if (specs->expr)
1784 specs->expr = build2 (COMPOUND_EXPR,
1785 TREE_TYPE (type_expr),
1786 specs->expr, type_expr);
1787 else
1788 specs->expr = type_expr;
1790 d = start_decl (declarator, specs, true,
1791 chainon (postfix_attrs, all_prefix_attrs));
1792 if (!d)
1793 d = error_mark_node;
1794 if (omp_declare_simd_clauses.exists ()
1795 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1796 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1797 omp_declare_simd_clauses);
1799 else
1801 /* The declaration of the variable is in effect while
1802 its initializer is parsed. */
1803 d = start_decl (declarator, specs, true,
1804 chainon (postfix_attrs, all_prefix_attrs));
1805 if (!d)
1806 d = error_mark_node;
1807 if (omp_declare_simd_clauses.exists ()
1808 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1809 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1810 omp_declare_simd_clauses);
1811 start_init (d, asm_name, global_bindings_p ());
1812 init_loc = c_parser_peek_token (parser)->location;
1813 init = c_parser_initializer (parser);
1814 finish_init ();
1816 if (d != error_mark_node)
1818 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1819 finish_decl (d, init_loc, init.value,
1820 init.original_type, asm_name);
1823 else
1825 if (auto_type_p)
1827 error_at (here,
1828 "%<__auto_type%> requires an initialized "
1829 "data declaration");
1830 c_parser_skip_to_end_of_block_or_statement (parser);
1831 return;
1833 tree d = start_decl (declarator, specs, false,
1834 chainon (postfix_attrs,
1835 all_prefix_attrs));
1836 if (omp_declare_simd_clauses.exists ()
1837 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1839 tree parms = NULL_TREE;
1840 if (d && TREE_CODE (d) == FUNCTION_DECL)
1842 struct c_declarator *ce = declarator;
1843 while (ce != NULL)
1844 if (ce->kind == cdk_function)
1846 parms = ce->u.arg_info->parms;
1847 break;
1849 else
1850 ce = ce->declarator;
1852 if (parms)
1853 temp_store_parm_decls (d, parms);
1854 c_finish_omp_declare_simd (parser, d, parms,
1855 omp_declare_simd_clauses);
1856 if (parms)
1857 temp_pop_parm_decls ();
1859 if (d)
1860 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1861 NULL_TREE, asm_name);
1863 if (c_parser_next_token_is_keyword (parser, RID_IN))
1865 if (d)
1866 *objc_foreach_object_declaration = d;
1867 else
1868 *objc_foreach_object_declaration = error_mark_node;
1871 if (c_parser_next_token_is (parser, CPP_COMMA))
1873 if (auto_type_p)
1875 error_at (here,
1876 "%<__auto_type%> may only be used with"
1877 " a single declarator");
1878 c_parser_skip_to_end_of_block_or_statement (parser);
1879 return;
1881 c_parser_consume_token (parser);
1882 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1883 all_prefix_attrs = chainon (c_parser_attributes (parser),
1884 prefix_attrs);
1885 else
1886 all_prefix_attrs = prefix_attrs;
1887 continue;
1889 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1891 c_parser_consume_token (parser);
1892 return;
1894 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1896 /* This can only happen in Objective-C: we found the
1897 'in' that terminates the declaration inside an
1898 Objective-C foreach statement. Do not consume the
1899 token, so that the caller can use it to determine
1900 that this indeed is a foreach context. */
1901 return;
1903 else
1905 c_parser_error (parser, "expected %<,%> or %<;%>");
1906 c_parser_skip_to_end_of_block_or_statement (parser);
1907 return;
1910 else if (auto_type_p)
1912 error_at (here,
1913 "%<__auto_type%> requires an initialized data declaration");
1914 c_parser_skip_to_end_of_block_or_statement (parser);
1915 return;
1917 else if (!fndef_ok)
1919 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1920 "%<asm%> or %<__attribute__%>");
1921 c_parser_skip_to_end_of_block_or_statement (parser);
1922 return;
1924 /* Function definition (nested or otherwise). */
1925 if (nested)
1927 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1928 c_push_function_context ();
1930 if (!start_function (specs, declarator, all_prefix_attrs))
1932 /* This can appear in many cases looking nothing like a
1933 function definition, so we don't give a more specific
1934 error suggesting there was one. */
1935 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1936 "or %<__attribute__%>");
1937 if (nested)
1938 c_pop_function_context ();
1939 break;
1942 if (DECL_DECLARED_INLINE_P (current_function_decl))
1943 tv = TV_PARSE_INLINE;
1944 else
1945 tv = TV_PARSE_FUNC;
1946 timevar_push (tv);
1948 /* Parse old-style parameter declarations. ??? Attributes are
1949 not allowed to start declaration specifiers here because of a
1950 syntax conflict between a function declaration with attribute
1951 suffix and a function definition with an attribute prefix on
1952 first old-style parameter declaration. Following the old
1953 parser, they are not accepted on subsequent old-style
1954 parameter declarations either. However, there is no
1955 ambiguity after the first declaration, nor indeed on the
1956 first as long as we don't allow postfix attributes after a
1957 declarator with a nonempty identifier list in a definition;
1958 and postfix attributes have never been accepted here in
1959 function definitions either. */
1960 while (c_parser_next_token_is_not (parser, CPP_EOF)
1961 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1962 c_parser_declaration_or_fndef (parser, false, false, false,
1963 true, false, NULL, vNULL);
1964 store_parm_decls ();
1965 if (omp_declare_simd_clauses.exists ()
1966 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1967 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1968 omp_declare_simd_clauses);
1969 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1970 = c_parser_peek_token (parser)->location;
1971 fnbody = c_parser_compound_statement (parser);
1972 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1973 fnbody = expand_array_notation_exprs (fnbody);
1974 if (nested)
1976 tree decl = current_function_decl;
1977 /* Mark nested functions as needing static-chain initially.
1978 lower_nested_functions will recompute it but the
1979 DECL_STATIC_CHAIN flag is also used before that happens,
1980 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1981 DECL_STATIC_CHAIN (decl) = 1;
1982 add_stmt (fnbody);
1983 finish_function ();
1984 c_pop_function_context ();
1985 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1987 else
1989 add_stmt (fnbody);
1990 finish_function ();
1993 timevar_pop (tv);
1994 break;
1998 /* Parse an asm-definition (asm() outside a function body). This is a
1999 GNU extension.
2001 asm-definition:
2002 simple-asm-expr ;
2005 static void
2006 c_parser_asm_definition (c_parser *parser)
2008 tree asm_str = c_parser_simple_asm_expr (parser);
2009 if (asm_str)
2010 symtab->finalize_toplevel_asm (asm_str);
2011 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2014 /* Parse a static assertion (C11 6.7.10).
2016 static_assert-declaration:
2017 static_assert-declaration-no-semi ;
2020 static void
2021 c_parser_static_assert_declaration (c_parser *parser)
2023 c_parser_static_assert_declaration_no_semi (parser);
2024 if (parser->error
2025 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2026 c_parser_skip_to_end_of_block_or_statement (parser);
2029 /* Parse a static assertion (C11 6.7.10), without the trailing
2030 semicolon.
2032 static_assert-declaration-no-semi:
2033 _Static_assert ( constant-expression , string-literal )
2036 static void
2037 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2039 location_t assert_loc, value_loc;
2040 tree value;
2041 tree string;
2043 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2044 assert_loc = c_parser_peek_token (parser)->location;
2045 if (flag_isoc99)
2046 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2047 "ISO C99 does not support %<_Static_assert%>");
2048 else
2049 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2050 "ISO C90 does not support %<_Static_assert%>");
2051 c_parser_consume_token (parser);
2052 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2053 return;
2054 value_loc = c_parser_peek_token (parser)->location;
2055 value = c_parser_expr_no_commas (parser, NULL).value;
2056 parser->lex_untranslated_string = true;
2057 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2059 parser->lex_untranslated_string = false;
2060 return;
2062 switch (c_parser_peek_token (parser)->type)
2064 case CPP_STRING:
2065 case CPP_STRING16:
2066 case CPP_STRING32:
2067 case CPP_WSTRING:
2068 case CPP_UTF8STRING:
2069 string = c_parser_peek_token (parser)->value;
2070 c_parser_consume_token (parser);
2071 parser->lex_untranslated_string = false;
2072 break;
2073 default:
2074 c_parser_error (parser, "expected string literal");
2075 parser->lex_untranslated_string = false;
2076 return;
2078 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2080 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2082 error_at (value_loc, "expression in static assertion is not an integer");
2083 return;
2085 if (TREE_CODE (value) != INTEGER_CST)
2087 value = c_fully_fold (value, false, NULL);
2088 /* Strip no-op conversions. */
2089 STRIP_TYPE_NOPS (value);
2090 if (TREE_CODE (value) == INTEGER_CST)
2091 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2092 "is not an integer constant expression");
2094 if (TREE_CODE (value) != INTEGER_CST)
2096 error_at (value_loc, "expression in static assertion is not constant");
2097 return;
2099 constant_expression_warning (value);
2100 if (integer_zerop (value))
2101 error_at (assert_loc, "static assertion failed: %E", string);
2104 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2105 6.7), adding them to SPECS (which may already include some).
2106 Storage class specifiers are accepted iff SCSPEC_OK; type
2107 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2108 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2109 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2111 declaration-specifiers:
2112 storage-class-specifier declaration-specifiers[opt]
2113 type-specifier declaration-specifiers[opt]
2114 type-qualifier declaration-specifiers[opt]
2115 function-specifier declaration-specifiers[opt]
2116 alignment-specifier declaration-specifiers[opt]
2118 Function specifiers (inline) are from C99, and are currently
2119 handled as storage class specifiers, as is __thread. Alignment
2120 specifiers are from C11.
2122 C90 6.5.1, C99 6.7.1:
2123 storage-class-specifier:
2124 typedef
2125 extern
2126 static
2127 auto
2128 register
2129 _Thread_local
2131 (_Thread_local is new in C11.)
2133 C99 6.7.4:
2134 function-specifier:
2135 inline
2136 _Noreturn
2138 (_Noreturn is new in C11.)
2140 C90 6.5.2, C99 6.7.2:
2141 type-specifier:
2142 void
2143 char
2144 short
2146 long
2147 float
2148 double
2149 signed
2150 unsigned
2151 _Bool
2152 _Complex
2153 [_Imaginary removed in C99 TC2]
2154 struct-or-union-specifier
2155 enum-specifier
2156 typedef-name
2157 atomic-type-specifier
2159 (_Bool and _Complex are new in C99.)
2160 (atomic-type-specifier is new in C11.)
2162 C90 6.5.3, C99 6.7.3:
2164 type-qualifier:
2165 const
2166 restrict
2167 volatile
2168 address-space-qualifier
2169 _Atomic
2171 (restrict is new in C99.)
2172 (_Atomic is new in C11.)
2174 GNU extensions:
2176 declaration-specifiers:
2177 attributes declaration-specifiers[opt]
2179 type-qualifier:
2180 address-space
2182 address-space:
2183 identifier recognized by the target
2185 storage-class-specifier:
2186 __thread
2188 type-specifier:
2189 typeof-specifier
2190 __auto_type
2191 __intN
2192 _Decimal32
2193 _Decimal64
2194 _Decimal128
2195 _Fract
2196 _Accum
2197 _Sat
2199 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2200 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2202 atomic-type-specifier
2203 _Atomic ( type-name )
2205 Objective-C:
2207 type-specifier:
2208 class-name objc-protocol-refs[opt]
2209 typedef-name objc-protocol-refs
2210 objc-protocol-refs
2213 static void
2214 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2215 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2216 bool alignspec_ok, bool auto_type_ok,
2217 enum c_lookahead_kind la)
2219 bool attrs_ok = start_attr_ok;
2220 bool seen_type = specs->typespec_kind != ctsk_none;
2222 if (!typespec_ok)
2223 gcc_assert (la == cla_prefer_id);
2225 while (c_parser_next_token_is (parser, CPP_NAME)
2226 || c_parser_next_token_is (parser, CPP_KEYWORD)
2227 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2229 struct c_typespec t;
2230 tree attrs;
2231 tree align;
2232 location_t loc = c_parser_peek_token (parser)->location;
2234 /* If we cannot accept a type, exit if the next token must start
2235 one. Also, if we already have seen a tagged definition,
2236 a typename would be an error anyway and likely the user
2237 has simply forgotten a semicolon, so we exit. */
2238 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2239 && c_parser_next_tokens_start_typename (parser, la)
2240 && !c_parser_next_token_is_qualifier (parser))
2241 break;
2243 if (c_parser_next_token_is (parser, CPP_NAME))
2245 c_token *name_token = c_parser_peek_token (parser);
2246 tree value = name_token->value;
2247 c_id_kind kind = name_token->id_kind;
2249 if (kind == C_ID_ADDRSPACE)
2251 addr_space_t as
2252 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2253 declspecs_add_addrspace (name_token->location, specs, as);
2254 c_parser_consume_token (parser);
2255 attrs_ok = true;
2256 continue;
2259 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2261 /* If we cannot accept a type, and the next token must start one,
2262 exit. Do the same if we already have seen a tagged definition,
2263 since it would be an error anyway and likely the user has simply
2264 forgotten a semicolon. */
2265 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2266 break;
2268 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2269 a C_ID_CLASSNAME. */
2270 c_parser_consume_token (parser);
2271 seen_type = true;
2272 attrs_ok = true;
2273 if (kind == C_ID_ID)
2275 error_at (loc, "unknown type name %qE", value);
2276 t.kind = ctsk_typedef;
2277 t.spec = error_mark_node;
2279 else if (kind == C_ID_TYPENAME
2280 && (!c_dialect_objc ()
2281 || c_parser_next_token_is_not (parser, CPP_LESS)))
2283 t.kind = ctsk_typedef;
2284 /* For a typedef name, record the meaning, not the name.
2285 In case of 'foo foo, bar;'. */
2286 t.spec = lookup_name (value);
2288 else
2290 tree proto = NULL_TREE;
2291 gcc_assert (c_dialect_objc ());
2292 t.kind = ctsk_objc;
2293 if (c_parser_next_token_is (parser, CPP_LESS))
2294 proto = c_parser_objc_protocol_refs (parser);
2295 t.spec = objc_get_protocol_qualified_type (value, proto);
2297 t.expr = NULL_TREE;
2298 t.expr_const_operands = true;
2299 declspecs_add_type (name_token->location, specs, t);
2300 continue;
2302 if (c_parser_next_token_is (parser, CPP_LESS))
2304 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2305 nisse@lysator.liu.se. */
2306 tree proto;
2307 gcc_assert (c_dialect_objc ());
2308 if (!typespec_ok || seen_type)
2309 break;
2310 proto = c_parser_objc_protocol_refs (parser);
2311 t.kind = ctsk_objc;
2312 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2313 t.expr = NULL_TREE;
2314 t.expr_const_operands = true;
2315 declspecs_add_type (loc, specs, t);
2316 continue;
2318 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2319 switch (c_parser_peek_token (parser)->keyword)
2321 case RID_STATIC:
2322 case RID_EXTERN:
2323 case RID_REGISTER:
2324 case RID_TYPEDEF:
2325 case RID_INLINE:
2326 case RID_NORETURN:
2327 case RID_AUTO:
2328 case RID_THREAD:
2329 if (!scspec_ok)
2330 goto out;
2331 attrs_ok = true;
2332 /* TODO: Distinguish between function specifiers (inline, noreturn)
2333 and storage class specifiers, either here or in
2334 declspecs_add_scspec. */
2335 declspecs_add_scspec (loc, specs,
2336 c_parser_peek_token (parser)->value);
2337 c_parser_consume_token (parser);
2338 break;
2339 case RID_AUTO_TYPE:
2340 if (!auto_type_ok)
2341 goto out;
2342 /* Fall through. */
2343 case RID_UNSIGNED:
2344 case RID_LONG:
2345 case RID_SHORT:
2346 case RID_SIGNED:
2347 case RID_COMPLEX:
2348 case RID_INT:
2349 case RID_CHAR:
2350 case RID_FLOAT:
2351 case RID_DOUBLE:
2352 case RID_VOID:
2353 case RID_DFLOAT32:
2354 case RID_DFLOAT64:
2355 case RID_DFLOAT128:
2356 case RID_BOOL:
2357 case RID_FRACT:
2358 case RID_ACCUM:
2359 case RID_SAT:
2360 case RID_INT_N_0:
2361 case RID_INT_N_1:
2362 case RID_INT_N_2:
2363 case RID_INT_N_3:
2364 if (!typespec_ok)
2365 goto out;
2366 attrs_ok = true;
2367 seen_type = true;
2368 if (c_dialect_objc ())
2369 parser->objc_need_raw_identifier = true;
2370 t.kind = ctsk_resword;
2371 t.spec = c_parser_peek_token (parser)->value;
2372 t.expr = NULL_TREE;
2373 t.expr_const_operands = true;
2374 declspecs_add_type (loc, specs, t);
2375 c_parser_consume_token (parser);
2376 break;
2377 case RID_ENUM:
2378 if (!typespec_ok)
2379 goto out;
2380 attrs_ok = true;
2381 seen_type = true;
2382 t = c_parser_enum_specifier (parser);
2383 declspecs_add_type (loc, specs, t);
2384 break;
2385 case RID_STRUCT:
2386 case RID_UNION:
2387 if (!typespec_ok)
2388 goto out;
2389 attrs_ok = true;
2390 seen_type = true;
2391 t = c_parser_struct_or_union_specifier (parser);
2392 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2393 declspecs_add_type (loc, specs, t);
2394 break;
2395 case RID_TYPEOF:
2396 /* ??? The old parser rejected typeof after other type
2397 specifiers, but is a syntax error the best way of
2398 handling this? */
2399 if (!typespec_ok || seen_type)
2400 goto out;
2401 attrs_ok = true;
2402 seen_type = true;
2403 t = c_parser_typeof_specifier (parser);
2404 declspecs_add_type (loc, specs, t);
2405 break;
2406 case RID_ATOMIC:
2407 /* C parser handling of Objective-C constructs needs
2408 checking for correct lvalue-to-rvalue conversions, and
2409 the code in build_modify_expr handling various
2410 Objective-C cases, and that in build_unary_op handling
2411 Objective-C cases for increment / decrement, also needs
2412 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2413 and objc_types_are_equivalent may also need updates. */
2414 if (c_dialect_objc ())
2415 sorry ("%<_Atomic%> in Objective-C");
2416 /* C parser handling of OpenMP constructs needs checking for
2417 correct lvalue-to-rvalue conversions. */
2418 if (flag_openmp)
2419 sorry ("%<_Atomic%> with OpenMP");
2420 if (flag_isoc99)
2421 pedwarn_c99 (loc, OPT_Wpedantic,
2422 "ISO C99 does not support the %<_Atomic%> qualifier");
2423 else
2424 pedwarn_c99 (loc, OPT_Wpedantic,
2425 "ISO C90 does not support the %<_Atomic%> qualifier");
2426 attrs_ok = true;
2427 tree value;
2428 value = c_parser_peek_token (parser)->value;
2429 c_parser_consume_token (parser);
2430 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2432 /* _Atomic ( type-name ). */
2433 seen_type = true;
2434 c_parser_consume_token (parser);
2435 struct c_type_name *type = c_parser_type_name (parser);
2436 t.kind = ctsk_typeof;
2437 t.spec = error_mark_node;
2438 t.expr = NULL_TREE;
2439 t.expr_const_operands = true;
2440 if (type != NULL)
2441 t.spec = groktypename (type, &t.expr,
2442 &t.expr_const_operands);
2443 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2444 "expected %<)%>");
2445 if (t.spec != error_mark_node)
2447 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2448 error_at (loc, "%<_Atomic%>-qualified array type");
2449 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2450 error_at (loc, "%<_Atomic%>-qualified function type");
2451 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2452 error_at (loc, "%<_Atomic%> applied to a qualified type");
2453 else
2454 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2456 declspecs_add_type (loc, specs, t);
2458 else
2459 declspecs_add_qual (loc, specs, value);
2460 break;
2461 case RID_CONST:
2462 case RID_VOLATILE:
2463 case RID_RESTRICT:
2464 attrs_ok = true;
2465 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2466 c_parser_consume_token (parser);
2467 break;
2468 case RID_ATTRIBUTE:
2469 if (!attrs_ok)
2470 goto out;
2471 attrs = c_parser_attributes (parser);
2472 declspecs_add_attrs (loc, specs, attrs);
2473 break;
2474 case RID_ALIGNAS:
2475 if (!alignspec_ok)
2476 goto out;
2477 align = c_parser_alignas_specifier (parser);
2478 declspecs_add_alignas (loc, specs, align);
2479 break;
2480 default:
2481 goto out;
2484 out: ;
2487 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2489 enum-specifier:
2490 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2491 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2492 enum attributes[opt] identifier
2494 The form with trailing comma is new in C99. The forms with
2495 attributes are GNU extensions. In GNU C, we accept any expression
2496 without commas in the syntax (assignment expressions, not just
2497 conditional expressions); assignment expressions will be diagnosed
2498 as non-constant.
2500 enumerator-list:
2501 enumerator
2502 enumerator-list , enumerator
2504 enumerator:
2505 enumeration-constant
2506 enumeration-constant = constant-expression
2509 static struct c_typespec
2510 c_parser_enum_specifier (c_parser *parser)
2512 struct c_typespec ret;
2513 tree attrs;
2514 tree ident = NULL_TREE;
2515 location_t enum_loc;
2516 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2517 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2518 enum_loc = c_parser_peek_token (parser)->location;
2519 c_parser_consume_token (parser);
2520 attrs = c_parser_attributes (parser);
2521 enum_loc = c_parser_peek_token (parser)->location;
2522 /* Set the location in case we create a decl now. */
2523 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2524 if (c_parser_next_token_is (parser, CPP_NAME))
2526 ident = c_parser_peek_token (parser)->value;
2527 ident_loc = c_parser_peek_token (parser)->location;
2528 enum_loc = ident_loc;
2529 c_parser_consume_token (parser);
2531 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2533 /* Parse an enum definition. */
2534 struct c_enum_contents the_enum;
2535 tree type;
2536 tree postfix_attrs;
2537 /* We chain the enumerators in reverse order, then put them in
2538 forward order at the end. */
2539 tree values;
2540 timevar_push (TV_PARSE_ENUM);
2541 type = start_enum (enum_loc, &the_enum, ident);
2542 values = NULL_TREE;
2543 c_parser_consume_token (parser);
2544 while (true)
2546 tree enum_id;
2547 tree enum_value;
2548 tree enum_decl;
2549 bool seen_comma;
2550 c_token *token;
2551 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2552 location_t decl_loc, value_loc;
2553 if (c_parser_next_token_is_not (parser, CPP_NAME))
2555 c_parser_error (parser, "expected identifier");
2556 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2557 values = error_mark_node;
2558 break;
2560 token = c_parser_peek_token (parser);
2561 enum_id = token->value;
2562 /* Set the location in case we create a decl now. */
2563 c_parser_set_source_position_from_token (token);
2564 decl_loc = value_loc = token->location;
2565 c_parser_consume_token (parser);
2566 if (c_parser_next_token_is (parser, CPP_EQ))
2568 c_parser_consume_token (parser);
2569 value_loc = c_parser_peek_token (parser)->location;
2570 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2572 else
2573 enum_value = NULL_TREE;
2574 enum_decl = build_enumerator (decl_loc, value_loc,
2575 &the_enum, enum_id, enum_value);
2576 TREE_CHAIN (enum_decl) = values;
2577 values = enum_decl;
2578 seen_comma = false;
2579 if (c_parser_next_token_is (parser, CPP_COMMA))
2581 comma_loc = c_parser_peek_token (parser)->location;
2582 seen_comma = true;
2583 c_parser_consume_token (parser);
2585 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2587 if (seen_comma)
2588 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2589 "comma at end of enumerator list");
2590 c_parser_consume_token (parser);
2591 break;
2593 if (!seen_comma)
2595 c_parser_error (parser, "expected %<,%> or %<}%>");
2596 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2597 values = error_mark_node;
2598 break;
2601 postfix_attrs = c_parser_attributes (parser);
2602 ret.spec = finish_enum (type, nreverse (values),
2603 chainon (attrs, postfix_attrs));
2604 ret.kind = ctsk_tagdef;
2605 ret.expr = NULL_TREE;
2606 ret.expr_const_operands = true;
2607 timevar_pop (TV_PARSE_ENUM);
2608 return ret;
2610 else if (!ident)
2612 c_parser_error (parser, "expected %<{%>");
2613 ret.spec = error_mark_node;
2614 ret.kind = ctsk_tagref;
2615 ret.expr = NULL_TREE;
2616 ret.expr_const_operands = true;
2617 return ret;
2619 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2620 /* In ISO C, enumerated types can be referred to only if already
2621 defined. */
2622 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2624 gcc_assert (ident);
2625 pedwarn (enum_loc, OPT_Wpedantic,
2626 "ISO C forbids forward references to %<enum%> types");
2628 return ret;
2631 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2633 struct-or-union-specifier:
2634 struct-or-union attributes[opt] identifier[opt]
2635 { struct-contents } attributes[opt]
2636 struct-or-union attributes[opt] identifier
2638 struct-contents:
2639 struct-declaration-list
2641 struct-declaration-list:
2642 struct-declaration ;
2643 struct-declaration-list struct-declaration ;
2645 GNU extensions:
2647 struct-contents:
2648 empty
2649 struct-declaration
2650 struct-declaration-list struct-declaration
2652 struct-declaration-list:
2653 struct-declaration-list ;
2656 (Note that in the syntax here, unlike that in ISO C, the semicolons
2657 are included here rather than in struct-declaration, in order to
2658 describe the syntax with extra semicolons and missing semicolon at
2659 end.)
2661 Objective-C:
2663 struct-declaration-list:
2664 @defs ( class-name )
2666 (Note this does not include a trailing semicolon, but can be
2667 followed by further declarations, and gets a pedwarn-if-pedantic
2668 when followed by a semicolon.) */
2670 static struct c_typespec
2671 c_parser_struct_or_union_specifier (c_parser *parser)
2673 struct c_typespec ret;
2674 tree attrs;
2675 tree ident = NULL_TREE;
2676 location_t struct_loc;
2677 location_t ident_loc = UNKNOWN_LOCATION;
2678 enum tree_code code;
2679 switch (c_parser_peek_token (parser)->keyword)
2681 case RID_STRUCT:
2682 code = RECORD_TYPE;
2683 break;
2684 case RID_UNION:
2685 code = UNION_TYPE;
2686 break;
2687 default:
2688 gcc_unreachable ();
2690 struct_loc = c_parser_peek_token (parser)->location;
2691 c_parser_consume_token (parser);
2692 attrs = c_parser_attributes (parser);
2694 /* Set the location in case we create a decl now. */
2695 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2697 if (c_parser_next_token_is (parser, CPP_NAME))
2699 ident = c_parser_peek_token (parser)->value;
2700 ident_loc = c_parser_peek_token (parser)->location;
2701 struct_loc = ident_loc;
2702 c_parser_consume_token (parser);
2704 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2706 /* Parse a struct or union definition. Start the scope of the
2707 tag before parsing components. */
2708 struct c_struct_parse_info *struct_info;
2709 tree type = start_struct (struct_loc, code, ident, &struct_info);
2710 tree postfix_attrs;
2711 /* We chain the components in reverse order, then put them in
2712 forward order at the end. Each struct-declaration may
2713 declare multiple components (comma-separated), so we must use
2714 chainon to join them, although when parsing each
2715 struct-declaration we can use TREE_CHAIN directly.
2717 The theory behind all this is that there will be more
2718 semicolon separated fields than comma separated fields, and
2719 so we'll be minimizing the number of node traversals required
2720 by chainon. */
2721 tree contents;
2722 timevar_push (TV_PARSE_STRUCT);
2723 contents = NULL_TREE;
2724 c_parser_consume_token (parser);
2725 /* Handle the Objective-C @defs construct,
2726 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2727 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2729 tree name;
2730 gcc_assert (c_dialect_objc ());
2731 c_parser_consume_token (parser);
2732 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2733 goto end_at_defs;
2734 if (c_parser_next_token_is (parser, CPP_NAME)
2735 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2737 name = c_parser_peek_token (parser)->value;
2738 c_parser_consume_token (parser);
2740 else
2742 c_parser_error (parser, "expected class name");
2743 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2744 goto end_at_defs;
2746 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2747 "expected %<)%>");
2748 contents = nreverse (objc_get_class_ivars (name));
2750 end_at_defs:
2751 /* Parse the struct-declarations and semicolons. Problems with
2752 semicolons are diagnosed here; empty structures are diagnosed
2753 elsewhere. */
2754 while (true)
2756 tree decls;
2757 /* Parse any stray semicolon. */
2758 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2760 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2761 "extra semicolon in struct or union specified");
2762 c_parser_consume_token (parser);
2763 continue;
2765 /* Stop if at the end of the struct or union contents. */
2766 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2768 c_parser_consume_token (parser);
2769 break;
2771 /* Accept #pragmas at struct scope. */
2772 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2774 c_parser_pragma (parser, pragma_struct);
2775 continue;
2777 /* Parse some comma-separated declarations, but not the
2778 trailing semicolon if any. */
2779 decls = c_parser_struct_declaration (parser);
2780 contents = chainon (decls, contents);
2781 /* If no semicolon follows, either we have a parse error or
2782 are at the end of the struct or union and should
2783 pedwarn. */
2784 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2785 c_parser_consume_token (parser);
2786 else
2788 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2789 pedwarn (c_parser_peek_token (parser)->location, 0,
2790 "no semicolon at end of struct or union");
2791 else if (parser->error
2792 || !c_parser_next_token_starts_declspecs (parser))
2794 c_parser_error (parser, "expected %<;%>");
2795 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2796 break;
2799 /* If we come here, we have already emitted an error
2800 for an expected `;', identifier or `(', and we also
2801 recovered already. Go on with the next field. */
2804 postfix_attrs = c_parser_attributes (parser);
2805 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2806 chainon (attrs, postfix_attrs), struct_info);
2807 ret.kind = ctsk_tagdef;
2808 ret.expr = NULL_TREE;
2809 ret.expr_const_operands = true;
2810 timevar_pop (TV_PARSE_STRUCT);
2811 return ret;
2813 else if (!ident)
2815 c_parser_error (parser, "expected %<{%>");
2816 ret.spec = error_mark_node;
2817 ret.kind = ctsk_tagref;
2818 ret.expr = NULL_TREE;
2819 ret.expr_const_operands = true;
2820 return ret;
2822 ret = parser_xref_tag (ident_loc, code, ident);
2823 return ret;
2826 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2827 the trailing semicolon.
2829 struct-declaration:
2830 specifier-qualifier-list struct-declarator-list
2831 static_assert-declaration-no-semi
2833 specifier-qualifier-list:
2834 type-specifier specifier-qualifier-list[opt]
2835 type-qualifier specifier-qualifier-list[opt]
2836 attributes specifier-qualifier-list[opt]
2838 struct-declarator-list:
2839 struct-declarator
2840 struct-declarator-list , attributes[opt] struct-declarator
2842 struct-declarator:
2843 declarator attributes[opt]
2844 declarator[opt] : constant-expression attributes[opt]
2846 GNU extensions:
2848 struct-declaration:
2849 __extension__ struct-declaration
2850 specifier-qualifier-list
2852 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2853 of attributes where shown is a GNU extension. In GNU C, we accept
2854 any expression without commas in the syntax (assignment
2855 expressions, not just conditional expressions); assignment
2856 expressions will be diagnosed as non-constant. */
2858 static tree
2859 c_parser_struct_declaration (c_parser *parser)
2861 struct c_declspecs *specs;
2862 tree prefix_attrs;
2863 tree all_prefix_attrs;
2864 tree decls;
2865 location_t decl_loc;
2866 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2868 int ext;
2869 tree decl;
2870 ext = disable_extension_diagnostics ();
2871 c_parser_consume_token (parser);
2872 decl = c_parser_struct_declaration (parser);
2873 restore_extension_diagnostics (ext);
2874 return decl;
2876 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2878 c_parser_static_assert_declaration_no_semi (parser);
2879 return NULL_TREE;
2881 specs = build_null_declspecs ();
2882 decl_loc = c_parser_peek_token (parser)->location;
2883 /* Strictly by the standard, we shouldn't allow _Alignas here,
2884 but it appears to have been intended to allow it there, so
2885 we're keeping it as it is until WG14 reaches a conclusion
2886 of N1731.
2887 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2888 c_parser_declspecs (parser, specs, false, true, true,
2889 true, false, cla_nonabstract_decl);
2890 if (parser->error)
2891 return NULL_TREE;
2892 if (!specs->declspecs_seen_p)
2894 c_parser_error (parser, "expected specifier-qualifier-list");
2895 return NULL_TREE;
2897 finish_declspecs (specs);
2898 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2899 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2901 tree ret;
2902 if (specs->typespec_kind == ctsk_none)
2904 pedwarn (decl_loc, OPT_Wpedantic,
2905 "ISO C forbids member declarations with no members");
2906 shadow_tag_warned (specs, pedantic);
2907 ret = NULL_TREE;
2909 else
2911 /* Support for unnamed structs or unions as members of
2912 structs or unions (which is [a] useful and [b] supports
2913 MS P-SDK). */
2914 tree attrs = NULL;
2916 ret = grokfield (c_parser_peek_token (parser)->location,
2917 build_id_declarator (NULL_TREE), specs,
2918 NULL_TREE, &attrs);
2919 if (ret)
2920 decl_attributes (&ret, attrs, 0);
2922 return ret;
2925 /* Provide better error recovery. Note that a type name here is valid,
2926 and will be treated as a field name. */
2927 if (specs->typespec_kind == ctsk_tagdef
2928 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2929 && c_parser_next_token_starts_declspecs (parser)
2930 && !c_parser_next_token_is (parser, CPP_NAME))
2932 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2933 parser->error = false;
2934 return NULL_TREE;
2937 pending_xref_error ();
2938 prefix_attrs = specs->attrs;
2939 all_prefix_attrs = prefix_attrs;
2940 specs->attrs = NULL_TREE;
2941 decls = NULL_TREE;
2942 while (true)
2944 /* Declaring one or more declarators or un-named bit-fields. */
2945 struct c_declarator *declarator;
2946 bool dummy = false;
2947 if (c_parser_next_token_is (parser, CPP_COLON))
2948 declarator = build_id_declarator (NULL_TREE);
2949 else
2950 declarator = c_parser_declarator (parser,
2951 specs->typespec_kind != ctsk_none,
2952 C_DTR_NORMAL, &dummy);
2953 if (declarator == NULL)
2955 c_parser_skip_to_end_of_block_or_statement (parser);
2956 break;
2958 if (c_parser_next_token_is (parser, CPP_COLON)
2959 || c_parser_next_token_is (parser, CPP_COMMA)
2960 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2961 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2962 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2964 tree postfix_attrs = NULL_TREE;
2965 tree width = NULL_TREE;
2966 tree d;
2967 if (c_parser_next_token_is (parser, CPP_COLON))
2969 c_parser_consume_token (parser);
2970 width = c_parser_expr_no_commas (parser, NULL).value;
2972 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2973 postfix_attrs = c_parser_attributes (parser);
2974 d = grokfield (c_parser_peek_token (parser)->location,
2975 declarator, specs, width, &all_prefix_attrs);
2976 decl_attributes (&d, chainon (postfix_attrs,
2977 all_prefix_attrs), 0);
2978 DECL_CHAIN (d) = decls;
2979 decls = d;
2980 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2981 all_prefix_attrs = chainon (c_parser_attributes (parser),
2982 prefix_attrs);
2983 else
2984 all_prefix_attrs = prefix_attrs;
2985 if (c_parser_next_token_is (parser, CPP_COMMA))
2986 c_parser_consume_token (parser);
2987 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2988 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2990 /* Semicolon consumed in caller. */
2991 break;
2993 else
2995 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2996 break;
2999 else
3001 c_parser_error (parser,
3002 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3003 "%<__attribute__%>");
3004 break;
3007 return decls;
3010 /* Parse a typeof specifier (a GNU extension).
3012 typeof-specifier:
3013 typeof ( expression )
3014 typeof ( type-name )
3017 static struct c_typespec
3018 c_parser_typeof_specifier (c_parser *parser)
3020 struct c_typespec ret;
3021 ret.kind = ctsk_typeof;
3022 ret.spec = error_mark_node;
3023 ret.expr = NULL_TREE;
3024 ret.expr_const_operands = true;
3025 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3026 c_parser_consume_token (parser);
3027 c_inhibit_evaluation_warnings++;
3028 in_typeof++;
3029 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3031 c_inhibit_evaluation_warnings--;
3032 in_typeof--;
3033 return ret;
3035 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3037 struct c_type_name *type = c_parser_type_name (parser);
3038 c_inhibit_evaluation_warnings--;
3039 in_typeof--;
3040 if (type != NULL)
3042 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3043 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3046 else
3048 bool was_vm;
3049 location_t here = c_parser_peek_token (parser)->location;
3050 struct c_expr expr = c_parser_expression (parser);
3051 c_inhibit_evaluation_warnings--;
3052 in_typeof--;
3053 if (TREE_CODE (expr.value) == COMPONENT_REF
3054 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3055 error_at (here, "%<typeof%> applied to a bit-field");
3056 mark_exp_read (expr.value);
3057 ret.spec = TREE_TYPE (expr.value);
3058 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3059 /* This is returned with the type so that when the type is
3060 evaluated, this can be evaluated. */
3061 if (was_vm)
3062 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3063 pop_maybe_used (was_vm);
3064 /* For use in macros such as those in <stdatomic.h>, remove all
3065 qualifiers from atomic types. (const can be an issue for more macros
3066 using typeof than just the <stdatomic.h> ones.) */
3067 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3068 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3070 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3071 return ret;
3074 /* Parse an alignment-specifier.
3076 C11 6.7.5:
3078 alignment-specifier:
3079 _Alignas ( type-name )
3080 _Alignas ( constant-expression )
3083 static tree
3084 c_parser_alignas_specifier (c_parser * parser)
3086 tree ret = error_mark_node;
3087 location_t loc = c_parser_peek_token (parser)->location;
3088 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3089 c_parser_consume_token (parser);
3090 if (flag_isoc99)
3091 pedwarn_c99 (loc, OPT_Wpedantic,
3092 "ISO C99 does not support %<_Alignas%>");
3093 else
3094 pedwarn_c99 (loc, OPT_Wpedantic,
3095 "ISO C90 does not support %<_Alignas%>");
3096 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3097 return ret;
3098 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3100 struct c_type_name *type = c_parser_type_name (parser);
3101 if (type != NULL)
3102 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3103 false, true, 1);
3105 else
3106 ret = c_parser_expr_no_commas (parser, NULL).value;
3107 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3108 return ret;
3111 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3112 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3113 be redeclared; otherwise it may not. KIND indicates which kind of
3114 declarator is wanted. Returns a valid declarator except in the
3115 case of a syntax error in which case NULL is returned. *SEEN_ID is
3116 set to true if an identifier being declared is seen; this is used
3117 to diagnose bad forms of abstract array declarators and to
3118 determine whether an identifier list is syntactically permitted.
3120 declarator:
3121 pointer[opt] direct-declarator
3123 direct-declarator:
3124 identifier
3125 ( attributes[opt] declarator )
3126 direct-declarator array-declarator
3127 direct-declarator ( parameter-type-list )
3128 direct-declarator ( identifier-list[opt] )
3130 pointer:
3131 * type-qualifier-list[opt]
3132 * type-qualifier-list[opt] pointer
3134 type-qualifier-list:
3135 type-qualifier
3136 attributes
3137 type-qualifier-list type-qualifier
3138 type-qualifier-list attributes
3140 array-declarator:
3141 [ type-qualifier-list[opt] assignment-expression[opt] ]
3142 [ static type-qualifier-list[opt] assignment-expression ]
3143 [ type-qualifier-list static assignment-expression ]
3144 [ type-qualifier-list[opt] * ]
3146 parameter-type-list:
3147 parameter-list
3148 parameter-list , ...
3150 parameter-list:
3151 parameter-declaration
3152 parameter-list , parameter-declaration
3154 parameter-declaration:
3155 declaration-specifiers declarator attributes[opt]
3156 declaration-specifiers abstract-declarator[opt] attributes[opt]
3158 identifier-list:
3159 identifier
3160 identifier-list , identifier
3162 abstract-declarator:
3163 pointer
3164 pointer[opt] direct-abstract-declarator
3166 direct-abstract-declarator:
3167 ( attributes[opt] abstract-declarator )
3168 direct-abstract-declarator[opt] array-declarator
3169 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3171 GNU extensions:
3173 direct-declarator:
3174 direct-declarator ( parameter-forward-declarations
3175 parameter-type-list[opt] )
3177 direct-abstract-declarator:
3178 direct-abstract-declarator[opt] ( parameter-forward-declarations
3179 parameter-type-list[opt] )
3181 parameter-forward-declarations:
3182 parameter-list ;
3183 parameter-forward-declarations parameter-list ;
3185 The uses of attributes shown above are GNU extensions.
3187 Some forms of array declarator are not included in C99 in the
3188 syntax for abstract declarators; these are disallowed elsewhere.
3189 This may be a defect (DR#289).
3191 This function also accepts an omitted abstract declarator as being
3192 an abstract declarator, although not part of the formal syntax. */
3194 static struct c_declarator *
3195 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3196 bool *seen_id)
3198 /* Parse any initial pointer part. */
3199 if (c_parser_next_token_is (parser, CPP_MULT))
3201 struct c_declspecs *quals_attrs = build_null_declspecs ();
3202 struct c_declarator *inner;
3203 c_parser_consume_token (parser);
3204 c_parser_declspecs (parser, quals_attrs, false, false, true,
3205 false, false, cla_prefer_id);
3206 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3207 if (inner == NULL)
3208 return NULL;
3209 else
3210 return make_pointer_declarator (quals_attrs, inner);
3212 /* Now we have a direct declarator, direct abstract declarator or
3213 nothing (which counts as a direct abstract declarator here). */
3214 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3217 /* Parse a direct declarator or direct abstract declarator; arguments
3218 as c_parser_declarator. */
3220 static struct c_declarator *
3221 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3222 bool *seen_id)
3224 /* The direct declarator must start with an identifier (possibly
3225 omitted) or a parenthesized declarator (possibly abstract). In
3226 an ordinary declarator, initial parentheses must start a
3227 parenthesized declarator. In an abstract declarator or parameter
3228 declarator, they could start a parenthesized declarator or a
3229 parameter list. To tell which, the open parenthesis and any
3230 following attributes must be read. If a declaration specifier
3231 follows, then it is a parameter list; if the specifier is a
3232 typedef name, there might be an ambiguity about redeclaring it,
3233 which is resolved in the direction of treating it as a typedef
3234 name. If a close parenthesis follows, it is also an empty
3235 parameter list, as the syntax does not permit empty abstract
3236 declarators. Otherwise, it is a parenthesized declarator (in
3237 which case the analysis may be repeated inside it, recursively).
3239 ??? There is an ambiguity in a parameter declaration "int
3240 (__attribute__((foo)) x)", where x is not a typedef name: it
3241 could be an abstract declarator for a function, or declare x with
3242 parentheses. The proper resolution of this ambiguity needs
3243 documenting. At present we follow an accident of the old
3244 parser's implementation, whereby the first parameter must have
3245 some declaration specifiers other than just attributes. Thus as
3246 a parameter declaration it is treated as a parenthesized
3247 parameter named x, and as an abstract declarator it is
3248 rejected.
3250 ??? Also following the old parser, attributes inside an empty
3251 parameter list are ignored, making it a list not yielding a
3252 prototype, rather than giving an error or making it have one
3253 parameter with implicit type int.
3255 ??? Also following the old parser, typedef names may be
3256 redeclared in declarators, but not Objective-C class names. */
3258 if (kind != C_DTR_ABSTRACT
3259 && c_parser_next_token_is (parser, CPP_NAME)
3260 && ((type_seen_p
3261 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3262 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3263 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3265 struct c_declarator *inner
3266 = build_id_declarator (c_parser_peek_token (parser)->value);
3267 *seen_id = true;
3268 inner->id_loc = c_parser_peek_token (parser)->location;
3269 c_parser_consume_token (parser);
3270 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3273 if (kind != C_DTR_NORMAL
3274 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3276 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3277 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3280 /* Either we are at the end of an abstract declarator, or we have
3281 parentheses. */
3283 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3285 tree attrs;
3286 struct c_declarator *inner;
3287 c_parser_consume_token (parser);
3288 attrs = c_parser_attributes (parser);
3289 if (kind != C_DTR_NORMAL
3290 && (c_parser_next_token_starts_declspecs (parser)
3291 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3293 struct c_arg_info *args
3294 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3295 attrs);
3296 if (args == NULL)
3297 return NULL;
3298 else
3300 inner
3301 = build_function_declarator (args,
3302 build_id_declarator (NULL_TREE));
3303 return c_parser_direct_declarator_inner (parser, *seen_id,
3304 inner);
3307 /* A parenthesized declarator. */
3308 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3309 if (inner != NULL && attrs != NULL)
3310 inner = build_attrs_declarator (attrs, inner);
3311 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3313 c_parser_consume_token (parser);
3314 if (inner == NULL)
3315 return NULL;
3316 else
3317 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3319 else
3321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3322 "expected %<)%>");
3323 return NULL;
3326 else
3328 if (kind == C_DTR_NORMAL)
3330 c_parser_error (parser, "expected identifier or %<(%>");
3331 return NULL;
3333 else
3334 return build_id_declarator (NULL_TREE);
3338 /* Parse part of a direct declarator or direct abstract declarator,
3339 given that some (in INNER) has already been parsed; ID_PRESENT is
3340 true if an identifier is present, false for an abstract
3341 declarator. */
3343 static struct c_declarator *
3344 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3345 struct c_declarator *inner)
3347 /* Parse a sequence of array declarators and parameter lists. */
3348 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3350 location_t brace_loc = c_parser_peek_token (parser)->location;
3351 struct c_declarator *declarator;
3352 struct c_declspecs *quals_attrs = build_null_declspecs ();
3353 bool static_seen;
3354 bool star_seen;
3355 struct c_expr dimen;
3356 dimen.value = NULL_TREE;
3357 dimen.original_code = ERROR_MARK;
3358 dimen.original_type = NULL_TREE;
3359 c_parser_consume_token (parser);
3360 c_parser_declspecs (parser, quals_attrs, false, false, true,
3361 false, false, cla_prefer_id);
3362 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3363 if (static_seen)
3364 c_parser_consume_token (parser);
3365 if (static_seen && !quals_attrs->declspecs_seen_p)
3366 c_parser_declspecs (parser, quals_attrs, false, false, true,
3367 false, false, cla_prefer_id);
3368 if (!quals_attrs->declspecs_seen_p)
3369 quals_attrs = NULL;
3370 /* If "static" is present, there must be an array dimension.
3371 Otherwise, there may be a dimension, "*", or no
3372 dimension. */
3373 if (static_seen)
3375 star_seen = false;
3376 dimen = c_parser_expr_no_commas (parser, NULL);
3378 else
3380 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3382 dimen.value = NULL_TREE;
3383 star_seen = false;
3385 else if (flag_cilkplus
3386 && c_parser_next_token_is (parser, CPP_COLON))
3388 dimen.value = error_mark_node;
3389 star_seen = false;
3390 error_at (c_parser_peek_token (parser)->location,
3391 "array notations cannot be used in declaration");
3392 c_parser_consume_token (parser);
3394 else if (c_parser_next_token_is (parser, CPP_MULT))
3396 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3398 dimen.value = NULL_TREE;
3399 star_seen = true;
3400 c_parser_consume_token (parser);
3402 else
3404 star_seen = false;
3405 dimen = c_parser_expr_no_commas (parser, NULL);
3408 else
3410 star_seen = false;
3411 dimen = c_parser_expr_no_commas (parser, NULL);
3414 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3415 c_parser_consume_token (parser);
3416 else if (flag_cilkplus
3417 && c_parser_next_token_is (parser, CPP_COLON))
3419 error_at (c_parser_peek_token (parser)->location,
3420 "array notations cannot be used in declaration");
3421 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3422 return NULL;
3424 else
3426 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3427 "expected %<]%>");
3428 return NULL;
3430 if (dimen.value)
3431 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3432 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3433 static_seen, star_seen);
3434 if (declarator == NULL)
3435 return NULL;
3436 inner = set_array_declarator_inner (declarator, inner);
3437 return c_parser_direct_declarator_inner (parser, id_present, inner);
3439 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3441 tree attrs;
3442 struct c_arg_info *args;
3443 c_parser_consume_token (parser);
3444 attrs = c_parser_attributes (parser);
3445 args = c_parser_parms_declarator (parser, id_present, attrs);
3446 if (args == NULL)
3447 return NULL;
3448 else
3450 inner = build_function_declarator (args, inner);
3451 return c_parser_direct_declarator_inner (parser, id_present, inner);
3454 return inner;
3457 /* Parse a parameter list or identifier list, including the closing
3458 parenthesis but not the opening one. ATTRS are the attributes at
3459 the start of the list. ID_LIST_OK is true if an identifier list is
3460 acceptable; such a list must not have attributes at the start. */
3462 static struct c_arg_info *
3463 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3465 push_scope ();
3466 declare_parm_level ();
3467 /* If the list starts with an identifier, it is an identifier list.
3468 Otherwise, it is either a prototype list or an empty list. */
3469 if (id_list_ok
3470 && !attrs
3471 && c_parser_next_token_is (parser, CPP_NAME)
3472 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3474 /* Look ahead to detect typos in type names. */
3475 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3476 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3477 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3478 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3480 tree list = NULL_TREE, *nextp = &list;
3481 while (c_parser_next_token_is (parser, CPP_NAME)
3482 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3484 *nextp = build_tree_list (NULL_TREE,
3485 c_parser_peek_token (parser)->value);
3486 nextp = & TREE_CHAIN (*nextp);
3487 c_parser_consume_token (parser);
3488 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3489 break;
3490 c_parser_consume_token (parser);
3491 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3493 c_parser_error (parser, "expected identifier");
3494 break;
3497 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3499 struct c_arg_info *ret = build_arg_info ();
3500 ret->types = list;
3501 c_parser_consume_token (parser);
3502 pop_scope ();
3503 return ret;
3505 else
3507 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3508 "expected %<)%>");
3509 pop_scope ();
3510 return NULL;
3513 else
3515 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3516 NULL);
3517 pop_scope ();
3518 return ret;
3522 /* Parse a parameter list (possibly empty), including the closing
3523 parenthesis but not the opening one. ATTRS are the attributes at
3524 the start of the list. EXPR is NULL or an expression that needs to
3525 be evaluated for the side effects of array size expressions in the
3526 parameters. */
3528 static struct c_arg_info *
3529 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3531 bool bad_parm = false;
3533 /* ??? Following the old parser, forward parameter declarations may
3534 use abstract declarators, and if no real parameter declarations
3535 follow the forward declarations then this is not diagnosed. Also
3536 note as above that attributes are ignored as the only contents of
3537 the parentheses, or as the only contents after forward
3538 declarations. */
3539 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3541 struct c_arg_info *ret = build_arg_info ();
3542 c_parser_consume_token (parser);
3543 return ret;
3545 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3547 struct c_arg_info *ret = build_arg_info ();
3549 if (flag_allow_parameterless_variadic_functions)
3551 /* F (...) is allowed. */
3552 ret->types = NULL_TREE;
3554 else
3556 /* Suppress -Wold-style-definition for this case. */
3557 ret->types = error_mark_node;
3558 error_at (c_parser_peek_token (parser)->location,
3559 "ISO C requires a named argument before %<...%>");
3561 c_parser_consume_token (parser);
3562 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3564 c_parser_consume_token (parser);
3565 return ret;
3567 else
3569 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3570 "expected %<)%>");
3571 return NULL;
3574 /* Nonempty list of parameters, either terminated with semicolon
3575 (forward declarations; recurse) or with close parenthesis (normal
3576 function) or with ", ... )" (variadic function). */
3577 while (true)
3579 /* Parse a parameter. */
3580 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3581 attrs = NULL_TREE;
3582 if (parm == NULL)
3583 bad_parm = true;
3584 else
3585 push_parm_decl (parm, &expr);
3586 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3588 tree new_attrs;
3589 c_parser_consume_token (parser);
3590 mark_forward_parm_decls ();
3591 new_attrs = c_parser_attributes (parser);
3592 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3594 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3596 c_parser_consume_token (parser);
3597 if (bad_parm)
3598 return NULL;
3599 else
3600 return get_parm_info (false, expr);
3602 if (!c_parser_require (parser, CPP_COMMA,
3603 "expected %<;%>, %<,%> or %<)%>"))
3605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3606 return NULL;
3608 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3610 c_parser_consume_token (parser);
3611 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3613 c_parser_consume_token (parser);
3614 if (bad_parm)
3615 return NULL;
3616 else
3617 return get_parm_info (true, expr);
3619 else
3621 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3622 "expected %<)%>");
3623 return NULL;
3629 /* Parse a parameter declaration. ATTRS are the attributes at the
3630 start of the declaration if it is the first parameter. */
3632 static struct c_parm *
3633 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3635 struct c_declspecs *specs;
3636 struct c_declarator *declarator;
3637 tree prefix_attrs;
3638 tree postfix_attrs = NULL_TREE;
3639 bool dummy = false;
3641 /* Accept #pragmas between parameter declarations. */
3642 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3643 c_parser_pragma (parser, pragma_param);
3645 if (!c_parser_next_token_starts_declspecs (parser))
3647 c_token *token = c_parser_peek_token (parser);
3648 if (parser->error)
3649 return NULL;
3650 c_parser_set_source_position_from_token (token);
3651 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3653 error_at (token->location, "unknown type name %qE", token->value);
3654 parser->error = true;
3656 /* ??? In some Objective-C cases '...' isn't applicable so there
3657 should be a different message. */
3658 else
3659 c_parser_error (parser,
3660 "expected declaration specifiers or %<...%>");
3661 c_parser_skip_to_end_of_parameter (parser);
3662 return NULL;
3664 specs = build_null_declspecs ();
3665 if (attrs)
3667 declspecs_add_attrs (input_location, specs, attrs);
3668 attrs = NULL_TREE;
3670 c_parser_declspecs (parser, specs, true, true, true, true, false,
3671 cla_nonabstract_decl);
3672 finish_declspecs (specs);
3673 pending_xref_error ();
3674 prefix_attrs = specs->attrs;
3675 specs->attrs = NULL_TREE;
3676 declarator = c_parser_declarator (parser,
3677 specs->typespec_kind != ctsk_none,
3678 C_DTR_PARM, &dummy);
3679 if (declarator == NULL)
3681 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3682 return NULL;
3684 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3685 postfix_attrs = c_parser_attributes (parser);
3686 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3687 declarator);
3690 /* Parse a string literal in an asm expression. It should not be
3691 translated, and wide string literals are an error although
3692 permitted by the syntax. This is a GNU extension.
3694 asm-string-literal:
3695 string-literal
3697 ??? At present, following the old parser, the caller needs to have
3698 set lex_untranslated_string to 1. It would be better to follow the
3699 C++ parser rather than using this kludge. */
3701 static tree
3702 c_parser_asm_string_literal (c_parser *parser)
3704 tree str;
3705 int save_flag = warn_overlength_strings;
3706 warn_overlength_strings = 0;
3707 if (c_parser_next_token_is (parser, CPP_STRING))
3709 str = c_parser_peek_token (parser)->value;
3710 c_parser_consume_token (parser);
3712 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3714 error_at (c_parser_peek_token (parser)->location,
3715 "wide string literal in %<asm%>");
3716 str = build_string (1, "");
3717 c_parser_consume_token (parser);
3719 else
3721 c_parser_error (parser, "expected string literal");
3722 str = NULL_TREE;
3724 warn_overlength_strings = save_flag;
3725 return str;
3728 /* Parse a simple asm expression. This is used in restricted
3729 contexts, where a full expression with inputs and outputs does not
3730 make sense. This is a GNU extension.
3732 simple-asm-expr:
3733 asm ( asm-string-literal )
3736 static tree
3737 c_parser_simple_asm_expr (c_parser *parser)
3739 tree str;
3740 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3741 /* ??? Follow the C++ parser rather than using the
3742 lex_untranslated_string kludge. */
3743 parser->lex_untranslated_string = true;
3744 c_parser_consume_token (parser);
3745 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3747 parser->lex_untranslated_string = false;
3748 return NULL_TREE;
3750 str = c_parser_asm_string_literal (parser);
3751 parser->lex_untranslated_string = false;
3752 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3754 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3755 return NULL_TREE;
3757 return str;
3760 static tree
3761 c_parser_attribute_any_word (c_parser *parser)
3763 tree attr_name = NULL_TREE;
3765 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3767 /* ??? See comment above about what keywords are accepted here. */
3768 bool ok;
3769 switch (c_parser_peek_token (parser)->keyword)
3771 case RID_STATIC:
3772 case RID_UNSIGNED:
3773 case RID_LONG:
3774 case RID_CONST:
3775 case RID_EXTERN:
3776 case RID_REGISTER:
3777 case RID_TYPEDEF:
3778 case RID_SHORT:
3779 case RID_INLINE:
3780 case RID_NORETURN:
3781 case RID_VOLATILE:
3782 case RID_SIGNED:
3783 case RID_AUTO:
3784 case RID_RESTRICT:
3785 case RID_COMPLEX:
3786 case RID_THREAD:
3787 case RID_INT:
3788 case RID_CHAR:
3789 case RID_FLOAT:
3790 case RID_DOUBLE:
3791 case RID_VOID:
3792 case RID_DFLOAT32:
3793 case RID_DFLOAT64:
3794 case RID_DFLOAT128:
3795 case RID_BOOL:
3796 case RID_FRACT:
3797 case RID_ACCUM:
3798 case RID_SAT:
3799 case RID_TRANSACTION_ATOMIC:
3800 case RID_TRANSACTION_CANCEL:
3801 case RID_ATOMIC:
3802 case RID_AUTO_TYPE:
3803 case RID_INT_N_0:
3804 case RID_INT_N_1:
3805 case RID_INT_N_2:
3806 case RID_INT_N_3:
3807 ok = true;
3808 break;
3809 default:
3810 ok = false;
3811 break;
3813 if (!ok)
3814 return NULL_TREE;
3816 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3817 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3819 else if (c_parser_next_token_is (parser, CPP_NAME))
3820 attr_name = c_parser_peek_token (parser)->value;
3822 return attr_name;
3825 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3826 "__vector" or "__vector__." */
3828 static inline bool
3829 is_cilkplus_vector_p (tree name)
3831 if (flag_cilkplus && is_attribute_p ("vector", name))
3832 return true;
3833 return false;
3836 #define CILK_SIMD_FN_CLAUSE_MASK \
3837 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3838 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3839 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3840 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3841 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3843 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3844 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3845 pushed into the token list.
3846 Syntax:
3847 vector
3848 vector (<vector attributes>). */
3850 static void
3851 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3853 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3855 int paren_scope = 0;
3856 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3857 /* Consume the "vector" token. */
3858 c_parser_consume_token (parser);
3860 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3862 c_parser_consume_token (parser);
3863 paren_scope++;
3865 while (paren_scope > 0)
3867 c_token *token = c_parser_peek_token (parser);
3868 if (token->type == CPP_OPEN_PAREN)
3869 paren_scope++;
3870 else if (token->type == CPP_CLOSE_PAREN)
3871 paren_scope--;
3872 /* Do not push the last ')' since we are not pushing the '('. */
3873 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3874 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3875 c_parser_consume_token (parser);
3878 /* Since we are converting an attribute to a pragma, we need to end the
3879 attribute with PRAGMA_EOL. */
3880 c_token eol_token;
3881 memset (&eol_token, 0, sizeof (eol_token));
3882 eol_token.type = CPP_PRAGMA_EOL;
3883 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3886 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3888 static void
3889 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3891 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3893 /* c_parser_attributes is called in several places, so if these EOF
3894 tokens are already inserted, then don't do them again. */
3895 if (last_token.type == CPP_EOF)
3896 return;
3898 /* Two CPP_EOF token are added as a safety net since the normal C
3899 front-end has two token look-ahead. */
3900 c_token eof_token;
3901 eof_token.type = CPP_EOF;
3902 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3903 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3906 /* Parse (possibly empty) attributes. This is a GNU extension.
3908 attributes:
3909 empty
3910 attributes attribute
3912 attribute:
3913 __attribute__ ( ( attribute-list ) )
3915 attribute-list:
3916 attrib
3917 attribute_list , attrib
3919 attrib:
3920 empty
3921 any-word
3922 any-word ( identifier )
3923 any-word ( identifier , nonempty-expr-list )
3924 any-word ( expr-list )
3926 where the "identifier" must not be declared as a type, and
3927 "any-word" may be any identifier (including one declared as a
3928 type), a reserved word storage class specifier, type specifier or
3929 type qualifier. ??? This still leaves out most reserved keywords
3930 (following the old parser), shouldn't we include them, and why not
3931 allow identifiers declared as types to start the arguments? */
3933 static tree
3934 c_parser_attributes (c_parser *parser)
3936 tree attrs = NULL_TREE;
3937 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3939 /* ??? Follow the C++ parser rather than using the
3940 lex_untranslated_string kludge. */
3941 parser->lex_untranslated_string = true;
3942 c_parser_consume_token (parser);
3943 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3945 parser->lex_untranslated_string = false;
3946 return attrs;
3948 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3950 parser->lex_untranslated_string = false;
3951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3952 return attrs;
3954 /* Parse the attribute list. */
3955 while (c_parser_next_token_is (parser, CPP_COMMA)
3956 || c_parser_next_token_is (parser, CPP_NAME)
3957 || c_parser_next_token_is (parser, CPP_KEYWORD))
3959 tree attr, attr_name, attr_args;
3960 vec<tree, va_gc> *expr_list;
3961 if (c_parser_next_token_is (parser, CPP_COMMA))
3963 c_parser_consume_token (parser);
3964 continue;
3967 attr_name = c_parser_attribute_any_word (parser);
3968 if (attr_name == NULL)
3969 break;
3970 if (is_cilkplus_vector_p (attr_name))
3972 c_token *v_token = c_parser_peek_token (parser);
3973 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3974 continue;
3976 c_parser_consume_token (parser);
3977 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3979 attr = build_tree_list (attr_name, NULL_TREE);
3980 attrs = chainon (attrs, attr);
3981 continue;
3983 c_parser_consume_token (parser);
3984 /* Parse the attribute contents. If they start with an
3985 identifier which is followed by a comma or close
3986 parenthesis, then the arguments start with that
3987 identifier; otherwise they are an expression list.
3988 In objective-c the identifier may be a classname. */
3989 if (c_parser_next_token_is (parser, CPP_NAME)
3990 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3991 || (c_dialect_objc ()
3992 && c_parser_peek_token (parser)->id_kind
3993 == C_ID_CLASSNAME))
3994 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3995 || (c_parser_peek_2nd_token (parser)->type
3996 == CPP_CLOSE_PAREN))
3997 && (attribute_takes_identifier_p (attr_name)
3998 || (c_dialect_objc ()
3999 && c_parser_peek_token (parser)->id_kind
4000 == C_ID_CLASSNAME)))
4002 tree arg1 = c_parser_peek_token (parser)->value;
4003 c_parser_consume_token (parser);
4004 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4005 attr_args = build_tree_list (NULL_TREE, arg1);
4006 else
4008 tree tree_list;
4009 c_parser_consume_token (parser);
4010 expr_list = c_parser_expr_list (parser, false, true,
4011 NULL, NULL, NULL, NULL);
4012 tree_list = build_tree_list_vec (expr_list);
4013 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4014 release_tree_vector (expr_list);
4017 else
4019 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4020 attr_args = NULL_TREE;
4021 else
4023 expr_list = c_parser_expr_list (parser, false, true,
4024 NULL, NULL, NULL, NULL);
4025 attr_args = build_tree_list_vec (expr_list);
4026 release_tree_vector (expr_list);
4029 attr = build_tree_list (attr_name, attr_args);
4030 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4031 c_parser_consume_token (parser);
4032 else
4034 parser->lex_untranslated_string = false;
4035 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4036 "expected %<)%>");
4037 return attrs;
4039 attrs = chainon (attrs, attr);
4041 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4042 c_parser_consume_token (parser);
4043 else
4045 parser->lex_untranslated_string = false;
4046 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4047 "expected %<)%>");
4048 return attrs;
4050 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4051 c_parser_consume_token (parser);
4052 else
4054 parser->lex_untranslated_string = false;
4055 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4056 "expected %<)%>");
4057 return attrs;
4059 parser->lex_untranslated_string = false;
4062 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4063 c_finish_cilk_simd_fn_tokens (parser);
4064 return attrs;
4067 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4069 type-name:
4070 specifier-qualifier-list abstract-declarator[opt]
4073 static struct c_type_name *
4074 c_parser_type_name (c_parser *parser)
4076 struct c_declspecs *specs = build_null_declspecs ();
4077 struct c_declarator *declarator;
4078 struct c_type_name *ret;
4079 bool dummy = false;
4080 c_parser_declspecs (parser, specs, false, true, true, false, false,
4081 cla_prefer_type);
4082 if (!specs->declspecs_seen_p)
4084 c_parser_error (parser, "expected specifier-qualifier-list");
4085 return NULL;
4087 if (specs->type != error_mark_node)
4089 pending_xref_error ();
4090 finish_declspecs (specs);
4092 declarator = c_parser_declarator (parser,
4093 specs->typespec_kind != ctsk_none,
4094 C_DTR_ABSTRACT, &dummy);
4095 if (declarator == NULL)
4096 return NULL;
4097 ret = XOBNEW (&parser_obstack, struct c_type_name);
4098 ret->specs = specs;
4099 ret->declarator = declarator;
4100 return ret;
4103 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4105 initializer:
4106 assignment-expression
4107 { initializer-list }
4108 { initializer-list , }
4110 initializer-list:
4111 designation[opt] initializer
4112 initializer-list , designation[opt] initializer
4114 designation:
4115 designator-list =
4117 designator-list:
4118 designator
4119 designator-list designator
4121 designator:
4122 array-designator
4123 . identifier
4125 array-designator:
4126 [ constant-expression ]
4128 GNU extensions:
4130 initializer:
4133 designation:
4134 array-designator
4135 identifier :
4137 array-designator:
4138 [ constant-expression ... constant-expression ]
4140 Any expression without commas is accepted in the syntax for the
4141 constant-expressions, with non-constant expressions rejected later.
4143 This function is only used for top-level initializers; for nested
4144 ones, see c_parser_initval. */
4146 static struct c_expr
4147 c_parser_initializer (c_parser *parser)
4149 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4150 return c_parser_braced_init (parser, NULL_TREE, false);
4151 else
4153 struct c_expr ret;
4154 location_t loc = c_parser_peek_token (parser)->location;
4155 ret = c_parser_expr_no_commas (parser, NULL);
4156 if (TREE_CODE (ret.value) != STRING_CST
4157 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4158 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4159 return ret;
4163 /* Parse a braced initializer list. TYPE is the type specified for a
4164 compound literal, and NULL_TREE for other initializers and for
4165 nested braced lists. NESTED_P is true for nested braced lists,
4166 false for the list of a compound literal or the list that is the
4167 top-level initializer in a declaration. */
4169 static struct c_expr
4170 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4172 struct c_expr ret;
4173 struct obstack braced_init_obstack;
4174 location_t brace_loc = c_parser_peek_token (parser)->location;
4175 gcc_obstack_init (&braced_init_obstack);
4176 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4177 c_parser_consume_token (parser);
4178 if (nested_p)
4179 push_init_level (brace_loc, 0, &braced_init_obstack);
4180 else
4181 really_start_incremental_init (type);
4182 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4184 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4186 else
4188 /* Parse a non-empty initializer list, possibly with a trailing
4189 comma. */
4190 while (true)
4192 c_parser_initelt (parser, &braced_init_obstack);
4193 if (parser->error)
4194 break;
4195 if (c_parser_next_token_is (parser, CPP_COMMA))
4196 c_parser_consume_token (parser);
4197 else
4198 break;
4199 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4200 break;
4203 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4205 ret.value = error_mark_node;
4206 ret.original_code = ERROR_MARK;
4207 ret.original_type = NULL;
4208 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4209 pop_init_level (brace_loc, 0, &braced_init_obstack);
4210 obstack_free (&braced_init_obstack, NULL);
4211 return ret;
4213 c_parser_consume_token (parser);
4214 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4215 obstack_free (&braced_init_obstack, NULL);
4216 return ret;
4219 /* Parse a nested initializer, including designators. */
4221 static void
4222 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4224 /* Parse any designator or designator list. A single array
4225 designator may have the subsequent "=" omitted in GNU C, but a
4226 longer list or a structure member designator may not. */
4227 if (c_parser_next_token_is (parser, CPP_NAME)
4228 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4230 /* Old-style structure member designator. */
4231 set_init_label (c_parser_peek_token (parser)->location,
4232 c_parser_peek_token (parser)->value,
4233 braced_init_obstack);
4234 /* Use the colon as the error location. */
4235 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4236 "obsolete use of designated initializer with %<:%>");
4237 c_parser_consume_token (parser);
4238 c_parser_consume_token (parser);
4240 else
4242 /* des_seen is 0 if there have been no designators, 1 if there
4243 has been a single array designator and 2 otherwise. */
4244 int des_seen = 0;
4245 /* Location of a designator. */
4246 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4247 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4248 || c_parser_next_token_is (parser, CPP_DOT))
4250 int des_prev = des_seen;
4251 if (!des_seen)
4252 des_loc = c_parser_peek_token (parser)->location;
4253 if (des_seen < 2)
4254 des_seen++;
4255 if (c_parser_next_token_is (parser, CPP_DOT))
4257 des_seen = 2;
4258 c_parser_consume_token (parser);
4259 if (c_parser_next_token_is (parser, CPP_NAME))
4261 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4262 braced_init_obstack);
4263 c_parser_consume_token (parser);
4265 else
4267 struct c_expr init;
4268 init.value = error_mark_node;
4269 init.original_code = ERROR_MARK;
4270 init.original_type = NULL;
4271 c_parser_error (parser, "expected identifier");
4272 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4273 process_init_element (input_location, init, false,
4274 braced_init_obstack);
4275 return;
4278 else
4280 tree first, second;
4281 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4282 location_t array_index_loc = UNKNOWN_LOCATION;
4283 /* ??? Following the old parser, [ objc-receiver
4284 objc-message-args ] is accepted as an initializer,
4285 being distinguished from a designator by what follows
4286 the first assignment expression inside the square
4287 brackets, but after a first array designator a
4288 subsequent square bracket is for Objective-C taken to
4289 start an expression, using the obsolete form of
4290 designated initializer without '=', rather than
4291 possibly being a second level of designation: in LALR
4292 terms, the '[' is shifted rather than reducing
4293 designator to designator-list. */
4294 if (des_prev == 1 && c_dialect_objc ())
4296 des_seen = des_prev;
4297 break;
4299 if (des_prev == 0 && c_dialect_objc ())
4301 /* This might be an array designator or an
4302 Objective-C message expression. If the former,
4303 continue parsing here; if the latter, parse the
4304 remainder of the initializer given the starting
4305 primary-expression. ??? It might make sense to
4306 distinguish when des_prev == 1 as well; see
4307 previous comment. */
4308 tree rec, args;
4309 struct c_expr mexpr;
4310 c_parser_consume_token (parser);
4311 if (c_parser_peek_token (parser)->type == CPP_NAME
4312 && ((c_parser_peek_token (parser)->id_kind
4313 == C_ID_TYPENAME)
4314 || (c_parser_peek_token (parser)->id_kind
4315 == C_ID_CLASSNAME)))
4317 /* Type name receiver. */
4318 tree id = c_parser_peek_token (parser)->value;
4319 c_parser_consume_token (parser);
4320 rec = objc_get_class_reference (id);
4321 goto parse_message_args;
4323 first = c_parser_expr_no_commas (parser, NULL).value;
4324 mark_exp_read (first);
4325 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4326 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4327 goto array_desig_after_first;
4328 /* Expression receiver. So far only one part
4329 without commas has been parsed; there might be
4330 more of the expression. */
4331 rec = first;
4332 while (c_parser_next_token_is (parser, CPP_COMMA))
4334 struct c_expr next;
4335 location_t comma_loc, exp_loc;
4336 comma_loc = c_parser_peek_token (parser)->location;
4337 c_parser_consume_token (parser);
4338 exp_loc = c_parser_peek_token (parser)->location;
4339 next = c_parser_expr_no_commas (parser, NULL);
4340 next = convert_lvalue_to_rvalue (exp_loc, next,
4341 true, true);
4342 rec = build_compound_expr (comma_loc, rec, next.value);
4344 parse_message_args:
4345 /* Now parse the objc-message-args. */
4346 args = c_parser_objc_message_args (parser);
4347 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4348 "expected %<]%>");
4349 mexpr.value
4350 = objc_build_message_expr (rec, args);
4351 mexpr.original_code = ERROR_MARK;
4352 mexpr.original_type = NULL;
4353 /* Now parse and process the remainder of the
4354 initializer, starting with this message
4355 expression as a primary-expression. */
4356 c_parser_initval (parser, &mexpr, braced_init_obstack);
4357 return;
4359 c_parser_consume_token (parser);
4360 array_index_loc = c_parser_peek_token (parser)->location;
4361 first = c_parser_expr_no_commas (parser, NULL).value;
4362 mark_exp_read (first);
4363 array_desig_after_first:
4364 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4366 ellipsis_loc = c_parser_peek_token (parser)->location;
4367 c_parser_consume_token (parser);
4368 second = c_parser_expr_no_commas (parser, NULL).value;
4369 mark_exp_read (second);
4371 else
4372 second = NULL_TREE;
4373 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4375 c_parser_consume_token (parser);
4376 set_init_index (array_index_loc, first, second,
4377 braced_init_obstack);
4378 if (second)
4379 pedwarn (ellipsis_loc, OPT_Wpedantic,
4380 "ISO C forbids specifying range of elements to initialize");
4382 else
4383 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4384 "expected %<]%>");
4387 if (des_seen >= 1)
4389 if (c_parser_next_token_is (parser, CPP_EQ))
4391 pedwarn_c90 (des_loc, OPT_Wpedantic,
4392 "ISO C90 forbids specifying subobject "
4393 "to initialize");
4394 c_parser_consume_token (parser);
4396 else
4398 if (des_seen == 1)
4399 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4400 "obsolete use of designated initializer without %<=%>");
4401 else
4403 struct c_expr init;
4404 init.value = error_mark_node;
4405 init.original_code = ERROR_MARK;
4406 init.original_type = NULL;
4407 c_parser_error (parser, "expected %<=%>");
4408 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4409 process_init_element (input_location, init, false,
4410 braced_init_obstack);
4411 return;
4416 c_parser_initval (parser, NULL, braced_init_obstack);
4419 /* Parse a nested initializer; as c_parser_initializer but parses
4420 initializers within braced lists, after any designators have been
4421 applied. If AFTER is not NULL then it is an Objective-C message
4422 expression which is the primary-expression starting the
4423 initializer. */
4425 static void
4426 c_parser_initval (c_parser *parser, struct c_expr *after,
4427 struct obstack * braced_init_obstack)
4429 struct c_expr init;
4430 gcc_assert (!after || c_dialect_objc ());
4431 location_t loc = c_parser_peek_token (parser)->location;
4433 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4434 init = c_parser_braced_init (parser, NULL_TREE, true);
4435 else
4437 init = c_parser_expr_no_commas (parser, after);
4438 if (init.value != NULL_TREE
4439 && TREE_CODE (init.value) != STRING_CST
4440 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4441 init = convert_lvalue_to_rvalue (loc, init, true, true);
4443 process_init_element (loc, init, false, braced_init_obstack);
4446 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4447 C99 6.8.2).
4449 compound-statement:
4450 { block-item-list[opt] }
4451 { label-declarations block-item-list }
4453 block-item-list:
4454 block-item
4455 block-item-list block-item
4457 block-item:
4458 nested-declaration
4459 statement
4461 nested-declaration:
4462 declaration
4464 GNU extensions:
4466 compound-statement:
4467 { label-declarations block-item-list }
4469 nested-declaration:
4470 __extension__ nested-declaration
4471 nested-function-definition
4473 label-declarations:
4474 label-declaration
4475 label-declarations label-declaration
4477 label-declaration:
4478 __label__ identifier-list ;
4480 Allowing the mixing of declarations and code is new in C99. The
4481 GNU syntax also permits (not shown above) labels at the end of
4482 compound statements, which yield an error. We don't allow labels
4483 on declarations; this might seem like a natural extension, but
4484 there would be a conflict between attributes on the label and
4485 prefix attributes on the declaration. ??? The syntax follows the
4486 old parser in requiring something after label declarations.
4487 Although they are erroneous if the labels declared aren't defined,
4488 is it useful for the syntax to be this way?
4490 OpenACC:
4492 block-item:
4493 openacc-directive
4495 openacc-directive:
4496 update-directive
4498 OpenMP:
4500 block-item:
4501 openmp-directive
4503 openmp-directive:
4504 barrier-directive
4505 flush-directive
4506 taskwait-directive
4507 taskyield-directive
4508 cancel-directive
4509 cancellation-point-directive */
4511 static tree
4512 c_parser_compound_statement (c_parser *parser)
4514 tree stmt;
4515 location_t brace_loc;
4516 brace_loc = c_parser_peek_token (parser)->location;
4517 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4519 /* Ensure a scope is entered and left anyway to avoid confusion
4520 if we have just prepared to enter a function body. */
4521 stmt = c_begin_compound_stmt (true);
4522 c_end_compound_stmt (brace_loc, stmt, true);
4523 return error_mark_node;
4525 stmt = c_begin_compound_stmt (true);
4526 c_parser_compound_statement_nostart (parser);
4528 /* If the compound stmt contains array notations, then we expand them. */
4529 if (flag_cilkplus && contains_array_notation_expr (stmt))
4530 stmt = expand_array_notation_exprs (stmt);
4531 return c_end_compound_stmt (brace_loc, stmt, true);
4534 /* Parse a compound statement except for the opening brace. This is
4535 used for parsing both compound statements and statement expressions
4536 (which follow different paths to handling the opening). */
4538 static void
4539 c_parser_compound_statement_nostart (c_parser *parser)
4541 bool last_stmt = false;
4542 bool last_label = false;
4543 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4544 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4545 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4547 c_parser_consume_token (parser);
4548 return;
4550 mark_valid_location_for_stdc_pragma (true);
4551 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4553 /* Read zero or more forward-declarations for labels that nested
4554 functions can jump to. */
4555 mark_valid_location_for_stdc_pragma (false);
4556 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4558 label_loc = c_parser_peek_token (parser)->location;
4559 c_parser_consume_token (parser);
4560 /* Any identifiers, including those declared as type names,
4561 are OK here. */
4562 while (true)
4564 tree label;
4565 if (c_parser_next_token_is_not (parser, CPP_NAME))
4567 c_parser_error (parser, "expected identifier");
4568 break;
4570 label
4571 = declare_label (c_parser_peek_token (parser)->value);
4572 C_DECLARED_LABEL_FLAG (label) = 1;
4573 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4574 c_parser_consume_token (parser);
4575 if (c_parser_next_token_is (parser, CPP_COMMA))
4576 c_parser_consume_token (parser);
4577 else
4578 break;
4580 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4582 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4584 /* We must now have at least one statement, label or declaration. */
4585 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4587 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4588 c_parser_error (parser, "expected declaration or statement");
4589 c_parser_consume_token (parser);
4590 return;
4592 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4594 location_t loc = c_parser_peek_token (parser)->location;
4595 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4596 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4597 || (c_parser_next_token_is (parser, CPP_NAME)
4598 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4600 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4601 label_loc = c_parser_peek_2nd_token (parser)->location;
4602 else
4603 label_loc = c_parser_peek_token (parser)->location;
4604 last_label = true;
4605 last_stmt = false;
4606 mark_valid_location_for_stdc_pragma (false);
4607 c_parser_label (parser);
4609 else if (!last_label
4610 && c_parser_next_tokens_start_declaration (parser))
4612 last_label = false;
4613 mark_valid_location_for_stdc_pragma (false);
4614 c_parser_declaration_or_fndef (parser, true, true, true, true,
4615 true, NULL, vNULL);
4616 if (last_stmt)
4617 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4618 "ISO C90 forbids mixed declarations and code");
4619 last_stmt = false;
4621 else if (!last_label
4622 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4624 /* __extension__ can start a declaration, but is also an
4625 unary operator that can start an expression. Consume all
4626 but the last of a possible series of __extension__ to
4627 determine which. */
4628 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4629 && (c_parser_peek_2nd_token (parser)->keyword
4630 == RID_EXTENSION))
4631 c_parser_consume_token (parser);
4632 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4634 int ext;
4635 ext = disable_extension_diagnostics ();
4636 c_parser_consume_token (parser);
4637 last_label = false;
4638 mark_valid_location_for_stdc_pragma (false);
4639 c_parser_declaration_or_fndef (parser, true, true, true, true,
4640 true, NULL, vNULL);
4641 /* Following the old parser, __extension__ does not
4642 disable this diagnostic. */
4643 restore_extension_diagnostics (ext);
4644 if (last_stmt)
4645 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4646 "ISO C90 forbids mixed declarations and code");
4647 last_stmt = false;
4649 else
4650 goto statement;
4652 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4654 /* External pragmas, and some omp pragmas, are not associated
4655 with regular c code, and so are not to be considered statements
4656 syntactically. This ensures that the user doesn't put them
4657 places that would turn into syntax errors if the directive
4658 were ignored. */
4659 if (c_parser_pragma (parser, pragma_compound))
4660 last_label = false, last_stmt = true;
4662 else if (c_parser_next_token_is (parser, CPP_EOF))
4664 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4665 c_parser_error (parser, "expected declaration or statement");
4666 return;
4668 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4670 if (parser->in_if_block)
4672 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4673 error_at (loc, """expected %<}%> before %<else%>");
4674 return;
4676 else
4678 error_at (loc, "%<else%> without a previous %<if%>");
4679 c_parser_consume_token (parser);
4680 continue;
4683 else
4685 statement:
4686 last_label = false;
4687 last_stmt = true;
4688 mark_valid_location_for_stdc_pragma (false);
4689 c_parser_statement_after_labels (parser);
4692 parser->error = false;
4694 if (last_label)
4695 error_at (label_loc, "label at end of compound statement");
4696 c_parser_consume_token (parser);
4697 /* Restore the value we started with. */
4698 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4701 /* Parse all consecutive labels. */
4703 static void
4704 c_parser_all_labels (c_parser *parser)
4706 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4707 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4708 || (c_parser_next_token_is (parser, CPP_NAME)
4709 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4710 c_parser_label (parser);
4713 /* Parse a label (C90 6.6.1, C99 6.8.1).
4715 label:
4716 identifier : attributes[opt]
4717 case constant-expression :
4718 default :
4720 GNU extensions:
4722 label:
4723 case constant-expression ... constant-expression :
4725 The use of attributes on labels is a GNU extension. The syntax in
4726 GNU C accepts any expressions without commas, non-constant
4727 expressions being rejected later. */
4729 static void
4730 c_parser_label (c_parser *parser)
4732 location_t loc1 = c_parser_peek_token (parser)->location;
4733 tree label = NULL_TREE;
4734 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4736 tree exp1, exp2;
4737 c_parser_consume_token (parser);
4738 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4739 if (c_parser_next_token_is (parser, CPP_COLON))
4741 c_parser_consume_token (parser);
4742 label = do_case (loc1, exp1, NULL_TREE);
4744 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4746 c_parser_consume_token (parser);
4747 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4748 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4749 label = do_case (loc1, exp1, exp2);
4751 else
4752 c_parser_error (parser, "expected %<:%> or %<...%>");
4754 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4756 c_parser_consume_token (parser);
4757 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4758 label = do_case (loc1, NULL_TREE, NULL_TREE);
4760 else
4762 tree name = c_parser_peek_token (parser)->value;
4763 tree tlab;
4764 tree attrs;
4765 location_t loc2 = c_parser_peek_token (parser)->location;
4766 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4767 c_parser_consume_token (parser);
4768 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4769 c_parser_consume_token (parser);
4770 attrs = c_parser_attributes (parser);
4771 tlab = define_label (loc2, name);
4772 if (tlab)
4774 decl_attributes (&tlab, attrs, 0);
4775 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4778 if (label)
4780 if (c_parser_next_tokens_start_declaration (parser))
4782 error_at (c_parser_peek_token (parser)->location,
4783 "a label can only be part of a statement and "
4784 "a declaration is not a statement");
4785 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4786 /*static_assert_ok*/ true,
4787 /*empty_ok*/ true, /*nested*/ true,
4788 /*start_attr_ok*/ true, NULL,
4789 vNULL);
4794 /* Parse a statement (C90 6.6, C99 6.8).
4796 statement:
4797 labeled-statement
4798 compound-statement
4799 expression-statement
4800 selection-statement
4801 iteration-statement
4802 jump-statement
4804 labeled-statement:
4805 label statement
4807 expression-statement:
4808 expression[opt] ;
4810 selection-statement:
4811 if-statement
4812 switch-statement
4814 iteration-statement:
4815 while-statement
4816 do-statement
4817 for-statement
4819 jump-statement:
4820 goto identifier ;
4821 continue ;
4822 break ;
4823 return expression[opt] ;
4825 GNU extensions:
4827 statement:
4828 asm-statement
4830 jump-statement:
4831 goto * expression ;
4833 Objective-C:
4835 statement:
4836 objc-throw-statement
4837 objc-try-catch-statement
4838 objc-synchronized-statement
4840 objc-throw-statement:
4841 @throw expression ;
4842 @throw ;
4844 OpenACC:
4846 statement:
4847 openacc-construct
4849 openacc-construct:
4850 parallel-construct
4851 kernels-construct
4852 data-construct
4853 loop-construct
4855 parallel-construct:
4856 parallel-directive structured-block
4858 kernels-construct:
4859 kernels-directive structured-block
4861 data-construct:
4862 data-directive structured-block
4864 loop-construct:
4865 loop-directive structured-block
4867 OpenMP:
4869 statement:
4870 openmp-construct
4872 openmp-construct:
4873 parallel-construct
4874 for-construct
4875 simd-construct
4876 for-simd-construct
4877 sections-construct
4878 single-construct
4879 parallel-for-construct
4880 parallel-for-simd-construct
4881 parallel-sections-construct
4882 master-construct
4883 critical-construct
4884 atomic-construct
4885 ordered-construct
4887 parallel-construct:
4888 parallel-directive structured-block
4890 for-construct:
4891 for-directive iteration-statement
4893 simd-construct:
4894 simd-directive iteration-statements
4896 for-simd-construct:
4897 for-simd-directive iteration-statements
4899 sections-construct:
4900 sections-directive section-scope
4902 single-construct:
4903 single-directive structured-block
4905 parallel-for-construct:
4906 parallel-for-directive iteration-statement
4908 parallel-for-simd-construct:
4909 parallel-for-simd-directive iteration-statement
4911 parallel-sections-construct:
4912 parallel-sections-directive section-scope
4914 master-construct:
4915 master-directive structured-block
4917 critical-construct:
4918 critical-directive structured-block
4920 atomic-construct:
4921 atomic-directive expression-statement
4923 ordered-construct:
4924 ordered-directive structured-block
4926 Transactional Memory:
4928 statement:
4929 transaction-statement
4930 transaction-cancel-statement
4933 static void
4934 c_parser_statement (c_parser *parser)
4936 c_parser_all_labels (parser);
4937 c_parser_statement_after_labels (parser);
4940 /* Parse a statement, other than a labeled statement. */
4942 static void
4943 c_parser_statement_after_labels (c_parser *parser)
4945 location_t loc = c_parser_peek_token (parser)->location;
4946 tree stmt = NULL_TREE;
4947 bool in_if_block = parser->in_if_block;
4948 parser->in_if_block = false;
4949 switch (c_parser_peek_token (parser)->type)
4951 case CPP_OPEN_BRACE:
4952 add_stmt (c_parser_compound_statement (parser));
4953 break;
4954 case CPP_KEYWORD:
4955 switch (c_parser_peek_token (parser)->keyword)
4957 case RID_IF:
4958 c_parser_if_statement (parser);
4959 break;
4960 case RID_SWITCH:
4961 c_parser_switch_statement (parser);
4962 break;
4963 case RID_WHILE:
4964 c_parser_while_statement (parser, false);
4965 break;
4966 case RID_DO:
4967 c_parser_do_statement (parser, false);
4968 break;
4969 case RID_FOR:
4970 c_parser_for_statement (parser, false);
4971 break;
4972 case RID_CILK_FOR:
4973 if (!flag_cilkplus)
4975 error_at (c_parser_peek_token (parser)->location,
4976 "-fcilkplus must be enabled to use %<_Cilk_for%>");
4977 c_parser_skip_to_end_of_block_or_statement (parser);
4979 else
4980 c_parser_cilk_for (parser, integer_zero_node);
4981 break;
4982 case RID_CILK_SYNC:
4983 c_parser_consume_token (parser);
4984 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4985 if (!flag_cilkplus)
4986 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4987 else
4988 add_stmt (build_cilk_sync ());
4989 break;
4990 case RID_GOTO:
4991 c_parser_consume_token (parser);
4992 if (c_parser_next_token_is (parser, CPP_NAME))
4994 stmt = c_finish_goto_label (loc,
4995 c_parser_peek_token (parser)->value);
4996 c_parser_consume_token (parser);
4998 else if (c_parser_next_token_is (parser, CPP_MULT))
5000 struct c_expr val;
5002 c_parser_consume_token (parser);
5003 val = c_parser_expression (parser);
5004 val = convert_lvalue_to_rvalue (loc, val, false, true);
5005 stmt = c_finish_goto_ptr (loc, val.value);
5007 else
5008 c_parser_error (parser, "expected identifier or %<*%>");
5009 goto expect_semicolon;
5010 case RID_CONTINUE:
5011 c_parser_consume_token (parser);
5012 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5013 goto expect_semicolon;
5014 case RID_BREAK:
5015 c_parser_consume_token (parser);
5016 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5017 goto expect_semicolon;
5018 case RID_RETURN:
5019 c_parser_consume_token (parser);
5020 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5022 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5023 c_parser_consume_token (parser);
5025 else
5027 location_t xloc = c_parser_peek_token (parser)->location;
5028 struct c_expr expr = c_parser_expression_conv (parser);
5029 mark_exp_read (expr.value);
5030 stmt = c_finish_return (xloc, expr.value, expr.original_type);
5031 goto expect_semicolon;
5033 break;
5034 case RID_ASM:
5035 stmt = c_parser_asm_statement (parser);
5036 break;
5037 case RID_TRANSACTION_ATOMIC:
5038 case RID_TRANSACTION_RELAXED:
5039 stmt = c_parser_transaction (parser,
5040 c_parser_peek_token (parser)->keyword);
5041 break;
5042 case RID_TRANSACTION_CANCEL:
5043 stmt = c_parser_transaction_cancel (parser);
5044 goto expect_semicolon;
5045 case RID_AT_THROW:
5046 gcc_assert (c_dialect_objc ());
5047 c_parser_consume_token (parser);
5048 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5050 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5051 c_parser_consume_token (parser);
5053 else
5055 struct c_expr expr = c_parser_expression (parser);
5056 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5057 expr.value = c_fully_fold (expr.value, false, NULL);
5058 stmt = objc_build_throw_stmt (loc, expr.value);
5059 goto expect_semicolon;
5061 break;
5062 case RID_AT_TRY:
5063 gcc_assert (c_dialect_objc ());
5064 c_parser_objc_try_catch_finally_statement (parser);
5065 break;
5066 case RID_AT_SYNCHRONIZED:
5067 gcc_assert (c_dialect_objc ());
5068 c_parser_objc_synchronized_statement (parser);
5069 break;
5070 default:
5071 goto expr_stmt;
5073 break;
5074 case CPP_SEMICOLON:
5075 c_parser_consume_token (parser);
5076 break;
5077 case CPP_CLOSE_PAREN:
5078 case CPP_CLOSE_SQUARE:
5079 /* Avoid infinite loop in error recovery:
5080 c_parser_skip_until_found stops at a closing nesting
5081 delimiter without consuming it, but here we need to consume
5082 it to proceed further. */
5083 c_parser_error (parser, "expected statement");
5084 c_parser_consume_token (parser);
5085 break;
5086 case CPP_PRAGMA:
5087 c_parser_pragma (parser, pragma_stmt);
5088 break;
5089 default:
5090 expr_stmt:
5091 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5092 expect_semicolon:
5093 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5094 break;
5096 /* Two cases cannot and do not have line numbers associated: If stmt
5097 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5098 cannot hold line numbers. But that's OK because the statement
5099 will either be changed to a MODIFY_EXPR during gimplification of
5100 the statement expr, or discarded. If stmt was compound, but
5101 without new variables, we will have skipped the creation of a
5102 BIND and will have a bare STATEMENT_LIST. But that's OK because
5103 (recursively) all of the component statements should already have
5104 line numbers assigned. ??? Can we discard no-op statements
5105 earlier? */
5106 if (CAN_HAVE_LOCATION_P (stmt)
5107 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5108 SET_EXPR_LOCATION (stmt, loc);
5110 parser->in_if_block = in_if_block;
5113 /* Parse the condition from an if, do, while or for statements. */
5115 static tree
5116 c_parser_condition (c_parser *parser)
5118 location_t loc = c_parser_peek_token (parser)->location;
5119 tree cond;
5120 cond = c_parser_expression_conv (parser).value;
5121 cond = c_objc_common_truthvalue_conversion (loc, cond);
5122 cond = c_fully_fold (cond, false, NULL);
5123 if (warn_sequence_point)
5124 verify_sequence_points (cond);
5125 return cond;
5128 /* Parse a parenthesized condition from an if, do or while statement.
5130 condition:
5131 ( expression )
5133 static tree
5134 c_parser_paren_condition (c_parser *parser)
5136 tree cond;
5137 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5138 return error_mark_node;
5139 cond = c_parser_condition (parser);
5140 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5141 return cond;
5144 /* Parse a statement which is a block in C99. */
5146 static tree
5147 c_parser_c99_block_statement (c_parser *parser)
5149 tree block = c_begin_compound_stmt (flag_isoc99);
5150 location_t loc = c_parser_peek_token (parser)->location;
5151 c_parser_statement (parser);
5152 return c_end_compound_stmt (loc, block, flag_isoc99);
5155 /* Parse the body of an if statement. This is just parsing a
5156 statement but (a) it is a block in C99, (b) we track whether the
5157 body is an if statement for the sake of -Wparentheses warnings, (c)
5158 we handle an empty body specially for the sake of -Wempty-body
5159 warnings, and (d) we call parser_compound_statement directly
5160 because c_parser_statement_after_labels resets
5161 parser->in_if_block. */
5163 static tree
5164 c_parser_if_body (c_parser *parser, bool *if_p)
5166 tree block = c_begin_compound_stmt (flag_isoc99);
5167 location_t body_loc = c_parser_peek_token (parser)->location;
5168 c_parser_all_labels (parser);
5169 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5170 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5172 location_t loc = c_parser_peek_token (parser)->location;
5173 add_stmt (build_empty_stmt (loc));
5174 c_parser_consume_token (parser);
5175 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5176 warning_at (loc, OPT_Wempty_body,
5177 "suggest braces around empty body in an %<if%> statement");
5179 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5180 add_stmt (c_parser_compound_statement (parser));
5181 else
5182 c_parser_statement_after_labels (parser);
5183 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5186 /* Parse the else body of an if statement. This is just parsing a
5187 statement but (a) it is a block in C99, (b) we handle an empty body
5188 specially for the sake of -Wempty-body warnings. */
5190 static tree
5191 c_parser_else_body (c_parser *parser)
5193 location_t else_loc = c_parser_peek_token (parser)->location;
5194 tree block = c_begin_compound_stmt (flag_isoc99);
5195 c_parser_all_labels (parser);
5196 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5198 location_t loc = c_parser_peek_token (parser)->location;
5199 warning_at (loc,
5200 OPT_Wempty_body,
5201 "suggest braces around empty body in an %<else%> statement");
5202 add_stmt (build_empty_stmt (loc));
5203 c_parser_consume_token (parser);
5205 else
5206 c_parser_statement_after_labels (parser);
5207 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5210 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5212 if-statement:
5213 if ( expression ) statement
5214 if ( expression ) statement else statement
5217 static void
5218 c_parser_if_statement (c_parser *parser)
5220 tree block;
5221 location_t loc;
5222 tree cond;
5223 bool first_if = false;
5224 tree first_body, second_body;
5225 bool in_if_block;
5226 tree if_stmt;
5228 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5229 c_parser_consume_token (parser);
5230 block = c_begin_compound_stmt (flag_isoc99);
5231 loc = c_parser_peek_token (parser)->location;
5232 cond = c_parser_paren_condition (parser);
5233 in_if_block = parser->in_if_block;
5234 parser->in_if_block = true;
5235 first_body = c_parser_if_body (parser, &first_if);
5236 parser->in_if_block = in_if_block;
5237 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5239 c_parser_consume_token (parser);
5240 second_body = c_parser_else_body (parser);
5242 else
5243 second_body = NULL_TREE;
5244 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5245 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5247 /* If the if statement contains array notations, then we expand them. */
5248 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5249 if_stmt = fix_conditional_array_notations (if_stmt);
5250 add_stmt (if_stmt);
5253 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5255 switch-statement:
5256 switch (expression) statement
5259 static void
5260 c_parser_switch_statement (c_parser *parser)
5262 struct c_expr ce;
5263 tree block, expr, body, save_break;
5264 location_t switch_loc = c_parser_peek_token (parser)->location;
5265 location_t switch_cond_loc;
5266 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5267 c_parser_consume_token (parser);
5268 block = c_begin_compound_stmt (flag_isoc99);
5269 bool explicit_cast_p = false;
5270 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5272 switch_cond_loc = c_parser_peek_token (parser)->location;
5273 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5274 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5275 explicit_cast_p = true;
5276 ce = c_parser_expression (parser);
5277 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5278 expr = ce.value;
5279 if (flag_cilkplus && contains_array_notation_expr (expr))
5281 error_at (switch_cond_loc,
5282 "array notations cannot be used as a condition for switch "
5283 "statement");
5284 expr = error_mark_node;
5286 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5288 else
5290 switch_cond_loc = UNKNOWN_LOCATION;
5291 expr = error_mark_node;
5293 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5294 save_break = c_break_label;
5295 c_break_label = NULL_TREE;
5296 body = c_parser_c99_block_statement (parser);
5297 c_finish_case (body, ce.original_type);
5298 if (c_break_label)
5300 location_t here = c_parser_peek_token (parser)->location;
5301 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5302 SET_EXPR_LOCATION (t, here);
5303 add_stmt (t);
5305 c_break_label = save_break;
5306 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5309 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5311 while-statement:
5312 while (expression) statement
5315 static void
5316 c_parser_while_statement (c_parser *parser, bool ivdep)
5318 tree block, cond, body, save_break, save_cont;
5319 location_t loc;
5320 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5321 c_parser_consume_token (parser);
5322 block = c_begin_compound_stmt (flag_isoc99);
5323 loc = c_parser_peek_token (parser)->location;
5324 cond = c_parser_paren_condition (parser);
5325 if (flag_cilkplus && contains_array_notation_expr (cond))
5327 error_at (loc, "array notations cannot be used as a condition for while "
5328 "statement");
5329 cond = error_mark_node;
5332 if (ivdep && cond != error_mark_node)
5333 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5334 build_int_cst (integer_type_node,
5335 annot_expr_ivdep_kind));
5336 save_break = c_break_label;
5337 c_break_label = NULL_TREE;
5338 save_cont = c_cont_label;
5339 c_cont_label = NULL_TREE;
5340 body = c_parser_c99_block_statement (parser);
5341 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5342 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5343 c_break_label = save_break;
5344 c_cont_label = save_cont;
5347 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5349 do-statement:
5350 do statement while ( expression ) ;
5353 static void
5354 c_parser_do_statement (c_parser *parser, bool ivdep)
5356 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5357 location_t loc;
5358 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5359 c_parser_consume_token (parser);
5360 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5361 warning_at (c_parser_peek_token (parser)->location,
5362 OPT_Wempty_body,
5363 "suggest braces around empty body in %<do%> statement");
5364 block = c_begin_compound_stmt (flag_isoc99);
5365 loc = c_parser_peek_token (parser)->location;
5366 save_break = c_break_label;
5367 c_break_label = NULL_TREE;
5368 save_cont = c_cont_label;
5369 c_cont_label = NULL_TREE;
5370 body = c_parser_c99_block_statement (parser);
5371 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5372 new_break = c_break_label;
5373 c_break_label = save_break;
5374 new_cont = c_cont_label;
5375 c_cont_label = save_cont;
5376 cond = c_parser_paren_condition (parser);
5377 if (flag_cilkplus && contains_array_notation_expr (cond))
5379 error_at (loc, "array notations cannot be used as a condition for a "
5380 "do-while statement");
5381 cond = error_mark_node;
5383 if (ivdep && cond != error_mark_node)
5384 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5385 build_int_cst (integer_type_node,
5386 annot_expr_ivdep_kind));
5387 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5388 c_parser_skip_to_end_of_block_or_statement (parser);
5389 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5390 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5393 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5395 for-statement:
5396 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5397 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5399 The form with a declaration is new in C99.
5401 ??? In accordance with the old parser, the declaration may be a
5402 nested function, which is then rejected in check_for_loop_decls,
5403 but does it make any sense for this to be included in the grammar?
5404 Note in particular that the nested function does not include a
5405 trailing ';', whereas the "declaration" production includes one.
5406 Also, can we reject bad declarations earlier and cheaper than
5407 check_for_loop_decls?
5409 In Objective-C, there are two additional variants:
5411 foreach-statement:
5412 for ( expression in expresssion ) statement
5413 for ( declaration in expression ) statement
5415 This is inconsistent with C, because the second variant is allowed
5416 even if c99 is not enabled.
5418 The rest of the comment documents these Objective-C foreach-statement.
5420 Here is the canonical example of the first variant:
5421 for (object in array) { do something with object }
5422 we call the first expression ("object") the "object_expression" and
5423 the second expression ("array") the "collection_expression".
5424 object_expression must be an lvalue of type "id" (a generic Objective-C
5425 object) because the loop works by assigning to object_expression the
5426 various objects from the collection_expression. collection_expression
5427 must evaluate to something of type "id" which responds to the method
5428 countByEnumeratingWithState:objects:count:.
5430 The canonical example of the second variant is:
5431 for (id object in array) { do something with object }
5432 which is completely equivalent to
5434 id object;
5435 for (object in array) { do something with object }
5437 Note that initizializing 'object' in some way (eg, "for ((object =
5438 xxx) in array) { do something with object }") is possibly
5439 technically valid, but completely pointless as 'object' will be
5440 assigned to something else as soon as the loop starts. We should
5441 most likely reject it (TODO).
5443 The beginning of the Objective-C foreach-statement looks exactly
5444 like the beginning of the for-statement, and we can tell it is a
5445 foreach-statement only because the initial declaration or
5446 expression is terminated by 'in' instead of ';'.
5449 static void
5450 c_parser_for_statement (c_parser *parser, bool ivdep)
5452 tree block, cond, incr, save_break, save_cont, body;
5453 /* The following are only used when parsing an ObjC foreach statement. */
5454 tree object_expression;
5455 /* Silence the bogus uninitialized warning. */
5456 tree collection_expression = NULL;
5457 location_t loc = c_parser_peek_token (parser)->location;
5458 location_t for_loc = c_parser_peek_token (parser)->location;
5459 bool is_foreach_statement = false;
5460 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5461 c_parser_consume_token (parser);
5462 /* Open a compound statement in Objective-C as well, just in case this is
5463 as foreach expression. */
5464 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5465 cond = error_mark_node;
5466 incr = error_mark_node;
5467 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5469 /* Parse the initialization declaration or expression. */
5470 object_expression = error_mark_node;
5471 parser->objc_could_be_foreach_context = c_dialect_objc ();
5472 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5474 parser->objc_could_be_foreach_context = false;
5475 c_parser_consume_token (parser);
5476 c_finish_expr_stmt (loc, NULL_TREE);
5478 else if (c_parser_next_tokens_start_declaration (parser))
5480 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5481 &object_expression, vNULL);
5482 parser->objc_could_be_foreach_context = false;
5484 if (c_parser_next_token_is_keyword (parser, RID_IN))
5486 c_parser_consume_token (parser);
5487 is_foreach_statement = true;
5488 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5489 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5491 else
5492 check_for_loop_decls (for_loc, flag_isoc99);
5494 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5496 /* __extension__ can start a declaration, but is also an
5497 unary operator that can start an expression. Consume all
5498 but the last of a possible series of __extension__ to
5499 determine which. */
5500 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5501 && (c_parser_peek_2nd_token (parser)->keyword
5502 == RID_EXTENSION))
5503 c_parser_consume_token (parser);
5504 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5506 int ext;
5507 ext = disable_extension_diagnostics ();
5508 c_parser_consume_token (parser);
5509 c_parser_declaration_or_fndef (parser, true, true, true, true,
5510 true, &object_expression, vNULL);
5511 parser->objc_could_be_foreach_context = false;
5513 restore_extension_diagnostics (ext);
5514 if (c_parser_next_token_is_keyword (parser, RID_IN))
5516 c_parser_consume_token (parser);
5517 is_foreach_statement = true;
5518 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5519 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5521 else
5522 check_for_loop_decls (for_loc, flag_isoc99);
5524 else
5525 goto init_expr;
5527 else
5529 init_expr:
5531 struct c_expr ce;
5532 tree init_expression;
5533 ce = c_parser_expression (parser);
5534 init_expression = ce.value;
5535 parser->objc_could_be_foreach_context = false;
5536 if (c_parser_next_token_is_keyword (parser, RID_IN))
5538 c_parser_consume_token (parser);
5539 is_foreach_statement = true;
5540 if (! lvalue_p (init_expression))
5541 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5542 object_expression = c_fully_fold (init_expression, false, NULL);
5544 else
5546 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5547 init_expression = ce.value;
5548 c_finish_expr_stmt (loc, init_expression);
5549 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5553 /* Parse the loop condition. In the case of a foreach
5554 statement, there is no loop condition. */
5555 gcc_assert (!parser->objc_could_be_foreach_context);
5556 if (!is_foreach_statement)
5558 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5560 if (ivdep)
5562 c_parser_error (parser, "missing loop condition in loop with "
5563 "%<GCC ivdep%> pragma");
5564 cond = error_mark_node;
5566 else
5568 c_parser_consume_token (parser);
5569 cond = NULL_TREE;
5572 else
5574 cond = c_parser_condition (parser);
5575 if (flag_cilkplus && contains_array_notation_expr (cond))
5577 error_at (loc, "array notations cannot be used in a "
5578 "condition for a for-loop");
5579 cond = error_mark_node;
5581 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5582 "expected %<;%>");
5584 if (ivdep && cond != error_mark_node)
5585 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5586 build_int_cst (integer_type_node,
5587 annot_expr_ivdep_kind));
5589 /* Parse the increment expression (the third expression in a
5590 for-statement). In the case of a foreach-statement, this is
5591 the expression that follows the 'in'. */
5592 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5594 if (is_foreach_statement)
5596 c_parser_error (parser, "missing collection in fast enumeration");
5597 collection_expression = error_mark_node;
5599 else
5600 incr = c_process_expr_stmt (loc, NULL_TREE);
5602 else
5604 if (is_foreach_statement)
5605 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5606 false, NULL);
5607 else
5609 struct c_expr ce = c_parser_expression (parser);
5610 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5611 incr = c_process_expr_stmt (loc, ce.value);
5614 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5616 save_break = c_break_label;
5617 c_break_label = NULL_TREE;
5618 save_cont = c_cont_label;
5619 c_cont_label = NULL_TREE;
5620 body = c_parser_c99_block_statement (parser);
5621 if (is_foreach_statement)
5622 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5623 else
5624 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5625 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5626 c_break_label = save_break;
5627 c_cont_label = save_cont;
5630 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5631 statement with inputs, outputs, clobbers, and volatile tag
5632 allowed.
5634 asm-statement:
5635 asm type-qualifier[opt] ( asm-argument ) ;
5636 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5638 asm-argument:
5639 asm-string-literal
5640 asm-string-literal : asm-operands[opt]
5641 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5642 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5644 asm-goto-argument:
5645 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5646 : asm-goto-operands
5648 Qualifiers other than volatile are accepted in the syntax but
5649 warned for. */
5651 static tree
5652 c_parser_asm_statement (c_parser *parser)
5654 tree quals, str, outputs, inputs, clobbers, labels, ret;
5655 bool simple, is_goto;
5656 location_t asm_loc = c_parser_peek_token (parser)->location;
5657 int section, nsections;
5659 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5660 c_parser_consume_token (parser);
5661 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5663 quals = c_parser_peek_token (parser)->value;
5664 c_parser_consume_token (parser);
5666 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5667 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5669 warning_at (c_parser_peek_token (parser)->location,
5671 "%E qualifier ignored on asm",
5672 c_parser_peek_token (parser)->value);
5673 quals = NULL_TREE;
5674 c_parser_consume_token (parser);
5676 else
5677 quals = NULL_TREE;
5679 is_goto = false;
5680 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5682 c_parser_consume_token (parser);
5683 is_goto = true;
5686 /* ??? Follow the C++ parser rather than using the
5687 lex_untranslated_string kludge. */
5688 parser->lex_untranslated_string = true;
5689 ret = NULL;
5691 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5692 goto error;
5694 str = c_parser_asm_string_literal (parser);
5695 if (str == NULL_TREE)
5696 goto error_close_paren;
5698 simple = true;
5699 outputs = NULL_TREE;
5700 inputs = NULL_TREE;
5701 clobbers = NULL_TREE;
5702 labels = NULL_TREE;
5704 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5705 goto done_asm;
5707 /* Parse each colon-delimited section of operands. */
5708 nsections = 3 + is_goto;
5709 for (section = 0; section < nsections; ++section)
5711 if (!c_parser_require (parser, CPP_COLON,
5712 is_goto
5713 ? "expected %<:%>"
5714 : "expected %<:%> or %<)%>"))
5715 goto error_close_paren;
5717 /* Once past any colon, we're no longer a simple asm. */
5718 simple = false;
5720 if ((!c_parser_next_token_is (parser, CPP_COLON)
5721 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5722 || section == 3)
5723 switch (section)
5725 case 0:
5726 /* For asm goto, we don't allow output operands, but reserve
5727 the slot for a future extension that does allow them. */
5728 if (!is_goto)
5729 outputs = c_parser_asm_operands (parser);
5730 break;
5731 case 1:
5732 inputs = c_parser_asm_operands (parser);
5733 break;
5734 case 2:
5735 clobbers = c_parser_asm_clobbers (parser);
5736 break;
5737 case 3:
5738 labels = c_parser_asm_goto_operands (parser);
5739 break;
5740 default:
5741 gcc_unreachable ();
5744 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5745 goto done_asm;
5748 done_asm:
5749 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5752 goto error;
5755 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5756 c_parser_skip_to_end_of_block_or_statement (parser);
5758 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5759 clobbers, labels, simple));
5761 error:
5762 parser->lex_untranslated_string = false;
5763 return ret;
5765 error_close_paren:
5766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5767 goto error;
5770 /* Parse asm operands, a GNU extension.
5772 asm-operands:
5773 asm-operand
5774 asm-operands , asm-operand
5776 asm-operand:
5777 asm-string-literal ( expression )
5778 [ identifier ] asm-string-literal ( expression )
5781 static tree
5782 c_parser_asm_operands (c_parser *parser)
5784 tree list = NULL_TREE;
5785 while (true)
5787 tree name, str;
5788 struct c_expr expr;
5789 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5791 c_parser_consume_token (parser);
5792 if (c_parser_next_token_is (parser, CPP_NAME))
5794 tree id = c_parser_peek_token (parser)->value;
5795 c_parser_consume_token (parser);
5796 name = build_string (IDENTIFIER_LENGTH (id),
5797 IDENTIFIER_POINTER (id));
5799 else
5801 c_parser_error (parser, "expected identifier");
5802 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5803 return NULL_TREE;
5805 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5806 "expected %<]%>");
5808 else
5809 name = NULL_TREE;
5810 str = c_parser_asm_string_literal (parser);
5811 if (str == NULL_TREE)
5812 return NULL_TREE;
5813 parser->lex_untranslated_string = false;
5814 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5816 parser->lex_untranslated_string = true;
5817 return NULL_TREE;
5819 expr = c_parser_expression (parser);
5820 mark_exp_read (expr.value);
5821 parser->lex_untranslated_string = true;
5822 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5824 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5825 return NULL_TREE;
5827 list = chainon (list, build_tree_list (build_tree_list (name, str),
5828 expr.value));
5829 if (c_parser_next_token_is (parser, CPP_COMMA))
5830 c_parser_consume_token (parser);
5831 else
5832 break;
5834 return list;
5837 /* Parse asm clobbers, a GNU extension.
5839 asm-clobbers:
5840 asm-string-literal
5841 asm-clobbers , asm-string-literal
5844 static tree
5845 c_parser_asm_clobbers (c_parser *parser)
5847 tree list = NULL_TREE;
5848 while (true)
5850 tree str = c_parser_asm_string_literal (parser);
5851 if (str)
5852 list = tree_cons (NULL_TREE, str, list);
5853 else
5854 return NULL_TREE;
5855 if (c_parser_next_token_is (parser, CPP_COMMA))
5856 c_parser_consume_token (parser);
5857 else
5858 break;
5860 return list;
5863 /* Parse asm goto labels, a GNU extension.
5865 asm-goto-operands:
5866 identifier
5867 asm-goto-operands , identifier
5870 static tree
5871 c_parser_asm_goto_operands (c_parser *parser)
5873 tree list = NULL_TREE;
5874 while (true)
5876 tree name, label;
5878 if (c_parser_next_token_is (parser, CPP_NAME))
5880 c_token *tok = c_parser_peek_token (parser);
5881 name = tok->value;
5882 label = lookup_label_for_goto (tok->location, name);
5883 c_parser_consume_token (parser);
5884 TREE_USED (label) = 1;
5886 else
5888 c_parser_error (parser, "expected identifier");
5889 return NULL_TREE;
5892 name = build_string (IDENTIFIER_LENGTH (name),
5893 IDENTIFIER_POINTER (name));
5894 list = tree_cons (name, label, list);
5895 if (c_parser_next_token_is (parser, CPP_COMMA))
5896 c_parser_consume_token (parser);
5897 else
5898 return nreverse (list);
5902 /* Parse an expression other than a compound expression; that is, an
5903 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5904 NULL then it is an Objective-C message expression which is the
5905 primary-expression starting the expression as an initializer.
5907 assignment-expression:
5908 conditional-expression
5909 unary-expression assignment-operator assignment-expression
5911 assignment-operator: one of
5912 = *= /= %= += -= <<= >>= &= ^= |=
5914 In GNU C we accept any conditional expression on the LHS and
5915 diagnose the invalid lvalue rather than producing a syntax
5916 error. */
5918 static struct c_expr
5919 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5920 tree omp_atomic_lhs)
5922 struct c_expr lhs, rhs, ret;
5923 enum tree_code code;
5924 location_t op_location, exp_location;
5925 gcc_assert (!after || c_dialect_objc ());
5926 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5927 op_location = c_parser_peek_token (parser)->location;
5928 switch (c_parser_peek_token (parser)->type)
5930 case CPP_EQ:
5931 code = NOP_EXPR;
5932 break;
5933 case CPP_MULT_EQ:
5934 code = MULT_EXPR;
5935 break;
5936 case CPP_DIV_EQ:
5937 code = TRUNC_DIV_EXPR;
5938 break;
5939 case CPP_MOD_EQ:
5940 code = TRUNC_MOD_EXPR;
5941 break;
5942 case CPP_PLUS_EQ:
5943 code = PLUS_EXPR;
5944 break;
5945 case CPP_MINUS_EQ:
5946 code = MINUS_EXPR;
5947 break;
5948 case CPP_LSHIFT_EQ:
5949 code = LSHIFT_EXPR;
5950 break;
5951 case CPP_RSHIFT_EQ:
5952 code = RSHIFT_EXPR;
5953 break;
5954 case CPP_AND_EQ:
5955 code = BIT_AND_EXPR;
5956 break;
5957 case CPP_XOR_EQ:
5958 code = BIT_XOR_EXPR;
5959 break;
5960 case CPP_OR_EQ:
5961 code = BIT_IOR_EXPR;
5962 break;
5963 default:
5964 return lhs;
5966 c_parser_consume_token (parser);
5967 exp_location = c_parser_peek_token (parser)->location;
5968 rhs = c_parser_expr_no_commas (parser, NULL);
5969 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5971 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5972 code, exp_location, rhs.value,
5973 rhs.original_type);
5974 if (code == NOP_EXPR)
5975 ret.original_code = MODIFY_EXPR;
5976 else
5978 TREE_NO_WARNING (ret.value) = 1;
5979 ret.original_code = ERROR_MARK;
5981 ret.original_type = NULL;
5982 return ret;
5985 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5986 is not NULL then it is an Objective-C message expression which is
5987 the primary-expression starting the expression as an initializer.
5989 conditional-expression:
5990 logical-OR-expression
5991 logical-OR-expression ? expression : conditional-expression
5993 GNU extensions:
5995 conditional-expression:
5996 logical-OR-expression ? : conditional-expression
5999 static struct c_expr
6000 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6001 tree omp_atomic_lhs)
6003 struct c_expr cond, exp1, exp2, ret;
6004 location_t cond_loc, colon_loc, middle_loc;
6006 gcc_assert (!after || c_dialect_objc ());
6008 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6010 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6011 return cond;
6012 cond_loc = c_parser_peek_token (parser)->location;
6013 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6014 c_parser_consume_token (parser);
6015 if (c_parser_next_token_is (parser, CPP_COLON))
6017 tree eptype = NULL_TREE;
6019 middle_loc = c_parser_peek_token (parser)->location;
6020 pedwarn (middle_loc, OPT_Wpedantic,
6021 "ISO C forbids omitting the middle term of a ?: expression");
6022 warn_for_omitted_condop (middle_loc, cond.value);
6023 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6025 eptype = TREE_TYPE (cond.value);
6026 cond.value = TREE_OPERAND (cond.value, 0);
6028 /* Make sure first operand is calculated only once. */
6029 exp1.value = c_save_expr (default_conversion (cond.value));
6030 if (eptype)
6031 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6032 exp1.original_type = NULL;
6033 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6034 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6036 else
6038 cond.value
6039 = c_objc_common_truthvalue_conversion
6040 (cond_loc, default_conversion (cond.value));
6041 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6042 exp1 = c_parser_expression_conv (parser);
6043 mark_exp_read (exp1.value);
6044 c_inhibit_evaluation_warnings +=
6045 ((cond.value == truthvalue_true_node)
6046 - (cond.value == truthvalue_false_node));
6049 colon_loc = c_parser_peek_token (parser)->location;
6050 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6052 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6053 ret.value = error_mark_node;
6054 ret.original_code = ERROR_MARK;
6055 ret.original_type = NULL;
6056 return ret;
6059 location_t exp2_loc = c_parser_peek_token (parser)->location;
6060 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6061 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6063 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6064 ret.value = build_conditional_expr (colon_loc, cond.value,
6065 cond.original_code == C_MAYBE_CONST_EXPR,
6066 exp1.value, exp1.original_type,
6067 exp2.value, exp2.original_type);
6068 ret.original_code = ERROR_MARK;
6069 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6070 ret.original_type = NULL;
6071 else
6073 tree t1, t2;
6075 /* If both sides are enum type, the default conversion will have
6076 made the type of the result be an integer type. We want to
6077 remember the enum types we started with. */
6078 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6079 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6080 ret.original_type = ((t1 != error_mark_node
6081 && t2 != error_mark_node
6082 && (TYPE_MAIN_VARIANT (t1)
6083 == TYPE_MAIN_VARIANT (t2)))
6084 ? t1
6085 : NULL);
6087 return ret;
6090 /* Parse a binary expression; that is, a logical-OR-expression (C90
6091 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6092 an Objective-C message expression which is the primary-expression
6093 starting the expression as an initializer.
6095 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6096 when it should be the unfolded lhs. In a valid OpenMP source,
6097 one of the operands of the toplevel binary expression must be equal
6098 to it. In that case, just return a build2 created binary operation
6099 rather than result of parser_build_binary_op.
6101 multiplicative-expression:
6102 cast-expression
6103 multiplicative-expression * cast-expression
6104 multiplicative-expression / cast-expression
6105 multiplicative-expression % cast-expression
6107 additive-expression:
6108 multiplicative-expression
6109 additive-expression + multiplicative-expression
6110 additive-expression - multiplicative-expression
6112 shift-expression:
6113 additive-expression
6114 shift-expression << additive-expression
6115 shift-expression >> additive-expression
6117 relational-expression:
6118 shift-expression
6119 relational-expression < shift-expression
6120 relational-expression > shift-expression
6121 relational-expression <= shift-expression
6122 relational-expression >= shift-expression
6124 equality-expression:
6125 relational-expression
6126 equality-expression == relational-expression
6127 equality-expression != relational-expression
6129 AND-expression:
6130 equality-expression
6131 AND-expression & equality-expression
6133 exclusive-OR-expression:
6134 AND-expression
6135 exclusive-OR-expression ^ AND-expression
6137 inclusive-OR-expression:
6138 exclusive-OR-expression
6139 inclusive-OR-expression | exclusive-OR-expression
6141 logical-AND-expression:
6142 inclusive-OR-expression
6143 logical-AND-expression && inclusive-OR-expression
6145 logical-OR-expression:
6146 logical-AND-expression
6147 logical-OR-expression || logical-AND-expression
6150 static struct c_expr
6151 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6152 tree omp_atomic_lhs)
6154 /* A binary expression is parsed using operator-precedence parsing,
6155 with the operands being cast expressions. All the binary
6156 operators are left-associative. Thus a binary expression is of
6157 form:
6159 E0 op1 E1 op2 E2 ...
6161 which we represent on a stack. On the stack, the precedence
6162 levels are strictly increasing. When a new operator is
6163 encountered of higher precedence than that at the top of the
6164 stack, it is pushed; its LHS is the top expression, and its RHS
6165 is everything parsed until it is popped. When a new operator is
6166 encountered with precedence less than or equal to that at the top
6167 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6168 by the result of the operation until the operator at the top of
6169 the stack has lower precedence than the new operator or there is
6170 only one element on the stack; then the top expression is the LHS
6171 of the new operator. In the case of logical AND and OR
6172 expressions, we also need to adjust c_inhibit_evaluation_warnings
6173 as appropriate when the operators are pushed and popped. */
6175 struct {
6176 /* The expression at this stack level. */
6177 struct c_expr expr;
6178 /* The precedence of the operator on its left, PREC_NONE at the
6179 bottom of the stack. */
6180 enum c_parser_prec prec;
6181 /* The operation on its left. */
6182 enum tree_code op;
6183 /* The source location of this operation. */
6184 location_t loc;
6185 } stack[NUM_PRECS];
6186 int sp;
6187 /* Location of the binary operator. */
6188 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6189 #define POP \
6190 do { \
6191 switch (stack[sp].op) \
6193 case TRUTH_ANDIF_EXPR: \
6194 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6195 == truthvalue_false_node); \
6196 break; \
6197 case TRUTH_ORIF_EXPR: \
6198 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6199 == truthvalue_true_node); \
6200 break; \
6201 default: \
6202 break; \
6204 stack[sp - 1].expr \
6205 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6206 stack[sp - 1].expr, true, true); \
6207 stack[sp].expr \
6208 = convert_lvalue_to_rvalue (stack[sp].loc, \
6209 stack[sp].expr, true, true); \
6210 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6211 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6212 && ((1 << stack[sp].prec) \
6213 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6214 | PREC_ADD | PREC_MULT))) \
6215 && stack[sp].op != TRUNC_MOD_EXPR \
6216 && stack[0].expr.value != error_mark_node \
6217 && stack[1].expr.value != error_mark_node \
6218 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6219 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6220 stack[0].expr.value \
6221 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6222 stack[0].expr.value, stack[1].expr.value); \
6223 else \
6224 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6225 stack[sp].op, \
6226 stack[sp - 1].expr, \
6227 stack[sp].expr); \
6228 sp--; \
6229 } while (0)
6230 gcc_assert (!after || c_dialect_objc ());
6231 stack[0].loc = c_parser_peek_token (parser)->location;
6232 stack[0].expr = c_parser_cast_expression (parser, after);
6233 stack[0].prec = PREC_NONE;
6234 sp = 0;
6235 while (true)
6237 enum c_parser_prec oprec;
6238 enum tree_code ocode;
6239 if (parser->error)
6240 goto out;
6241 switch (c_parser_peek_token (parser)->type)
6243 case CPP_MULT:
6244 oprec = PREC_MULT;
6245 ocode = MULT_EXPR;
6246 break;
6247 case CPP_DIV:
6248 oprec = PREC_MULT;
6249 ocode = TRUNC_DIV_EXPR;
6250 break;
6251 case CPP_MOD:
6252 oprec = PREC_MULT;
6253 ocode = TRUNC_MOD_EXPR;
6254 break;
6255 case CPP_PLUS:
6256 oprec = PREC_ADD;
6257 ocode = PLUS_EXPR;
6258 break;
6259 case CPP_MINUS:
6260 oprec = PREC_ADD;
6261 ocode = MINUS_EXPR;
6262 break;
6263 case CPP_LSHIFT:
6264 oprec = PREC_SHIFT;
6265 ocode = LSHIFT_EXPR;
6266 break;
6267 case CPP_RSHIFT:
6268 oprec = PREC_SHIFT;
6269 ocode = RSHIFT_EXPR;
6270 break;
6271 case CPP_LESS:
6272 oprec = PREC_REL;
6273 ocode = LT_EXPR;
6274 break;
6275 case CPP_GREATER:
6276 oprec = PREC_REL;
6277 ocode = GT_EXPR;
6278 break;
6279 case CPP_LESS_EQ:
6280 oprec = PREC_REL;
6281 ocode = LE_EXPR;
6282 break;
6283 case CPP_GREATER_EQ:
6284 oprec = PREC_REL;
6285 ocode = GE_EXPR;
6286 break;
6287 case CPP_EQ_EQ:
6288 oprec = PREC_EQ;
6289 ocode = EQ_EXPR;
6290 break;
6291 case CPP_NOT_EQ:
6292 oprec = PREC_EQ;
6293 ocode = NE_EXPR;
6294 break;
6295 case CPP_AND:
6296 oprec = PREC_BITAND;
6297 ocode = BIT_AND_EXPR;
6298 break;
6299 case CPP_XOR:
6300 oprec = PREC_BITXOR;
6301 ocode = BIT_XOR_EXPR;
6302 break;
6303 case CPP_OR:
6304 oprec = PREC_BITOR;
6305 ocode = BIT_IOR_EXPR;
6306 break;
6307 case CPP_AND_AND:
6308 oprec = PREC_LOGAND;
6309 ocode = TRUTH_ANDIF_EXPR;
6310 break;
6311 case CPP_OR_OR:
6312 oprec = PREC_LOGOR;
6313 ocode = TRUTH_ORIF_EXPR;
6314 break;
6315 default:
6316 /* Not a binary operator, so end of the binary
6317 expression. */
6318 goto out;
6320 binary_loc = c_parser_peek_token (parser)->location;
6321 while (oprec <= stack[sp].prec)
6322 POP;
6323 c_parser_consume_token (parser);
6324 switch (ocode)
6326 case TRUTH_ANDIF_EXPR:
6327 stack[sp].expr
6328 = convert_lvalue_to_rvalue (stack[sp].loc,
6329 stack[sp].expr, true, true);
6330 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6331 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6332 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6333 == truthvalue_false_node);
6334 break;
6335 case TRUTH_ORIF_EXPR:
6336 stack[sp].expr
6337 = convert_lvalue_to_rvalue (stack[sp].loc,
6338 stack[sp].expr, true, true);
6339 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6340 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6341 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6342 == truthvalue_true_node);
6343 break;
6344 default:
6345 break;
6347 sp++;
6348 stack[sp].loc = binary_loc;
6349 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6350 stack[sp].prec = oprec;
6351 stack[sp].op = ocode;
6352 stack[sp].loc = binary_loc;
6354 out:
6355 while (sp > 0)
6356 POP;
6357 return stack[0].expr;
6358 #undef POP
6361 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6362 NULL then it is an Objective-C message expression which is the
6363 primary-expression starting the expression as an initializer.
6365 cast-expression:
6366 unary-expression
6367 ( type-name ) unary-expression
6370 static struct c_expr
6371 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6373 location_t cast_loc = c_parser_peek_token (parser)->location;
6374 gcc_assert (!after || c_dialect_objc ());
6375 if (after)
6376 return c_parser_postfix_expression_after_primary (parser,
6377 cast_loc, *after);
6378 /* If the expression begins with a parenthesized type name, it may
6379 be either a cast or a compound literal; we need to see whether
6380 the next character is '{' to tell the difference. If not, it is
6381 an unary expression. Full detection of unknown typenames here
6382 would require a 3-token lookahead. */
6383 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6384 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6386 struct c_type_name *type_name;
6387 struct c_expr ret;
6388 struct c_expr expr;
6389 c_parser_consume_token (parser);
6390 type_name = c_parser_type_name (parser);
6391 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6392 if (type_name == NULL)
6394 ret.value = error_mark_node;
6395 ret.original_code = ERROR_MARK;
6396 ret.original_type = NULL;
6397 return ret;
6400 /* Save casted types in the function's used types hash table. */
6401 used_types_insert (type_name->specs->type);
6403 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6404 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6405 cast_loc);
6407 location_t expr_loc = c_parser_peek_token (parser)->location;
6408 expr = c_parser_cast_expression (parser, NULL);
6409 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6411 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6412 ret.original_code = ERROR_MARK;
6413 ret.original_type = NULL;
6414 return ret;
6416 else
6417 return c_parser_unary_expression (parser);
6420 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6422 unary-expression:
6423 postfix-expression
6424 ++ unary-expression
6425 -- unary-expression
6426 unary-operator cast-expression
6427 sizeof unary-expression
6428 sizeof ( type-name )
6430 unary-operator: one of
6431 & * + - ~ !
6433 GNU extensions:
6435 unary-expression:
6436 __alignof__ unary-expression
6437 __alignof__ ( type-name )
6438 && identifier
6440 (C11 permits _Alignof with type names only.)
6442 unary-operator: one of
6443 __extension__ __real__ __imag__
6445 Transactional Memory:
6447 unary-expression:
6448 transaction-expression
6450 In addition, the GNU syntax treats ++ and -- as unary operators, so
6451 they may be applied to cast expressions with errors for non-lvalues
6452 given later. */
6454 static struct c_expr
6455 c_parser_unary_expression (c_parser *parser)
6457 int ext;
6458 struct c_expr ret, op;
6459 location_t op_loc = c_parser_peek_token (parser)->location;
6460 location_t exp_loc;
6461 ret.original_code = ERROR_MARK;
6462 ret.original_type = NULL;
6463 switch (c_parser_peek_token (parser)->type)
6465 case CPP_PLUS_PLUS:
6466 c_parser_consume_token (parser);
6467 exp_loc = c_parser_peek_token (parser)->location;
6468 op = c_parser_cast_expression (parser, NULL);
6470 /* If there is array notations in op, we expand them. */
6471 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6472 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6473 else
6475 op = default_function_array_read_conversion (exp_loc, op);
6476 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6478 case CPP_MINUS_MINUS:
6479 c_parser_consume_token (parser);
6480 exp_loc = c_parser_peek_token (parser)->location;
6481 op = c_parser_cast_expression (parser, NULL);
6483 /* If there is array notations in op, we expand them. */
6484 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6485 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6486 else
6488 op = default_function_array_read_conversion (exp_loc, op);
6489 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6491 case CPP_AND:
6492 c_parser_consume_token (parser);
6493 op = c_parser_cast_expression (parser, NULL);
6494 mark_exp_read (op.value);
6495 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6496 case CPP_MULT:
6497 c_parser_consume_token (parser);
6498 exp_loc = c_parser_peek_token (parser)->location;
6499 op = c_parser_cast_expression (parser, NULL);
6500 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6501 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6502 return ret;
6503 case CPP_PLUS:
6504 if (!c_dialect_objc () && !in_system_header_at (input_location))
6505 warning_at (op_loc,
6506 OPT_Wtraditional,
6507 "traditional C rejects the unary plus operator");
6508 c_parser_consume_token (parser);
6509 exp_loc = c_parser_peek_token (parser)->location;
6510 op = c_parser_cast_expression (parser, NULL);
6511 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6512 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6513 case CPP_MINUS:
6514 c_parser_consume_token (parser);
6515 exp_loc = c_parser_peek_token (parser)->location;
6516 op = c_parser_cast_expression (parser, NULL);
6517 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6518 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6519 case CPP_COMPL:
6520 c_parser_consume_token (parser);
6521 exp_loc = c_parser_peek_token (parser)->location;
6522 op = c_parser_cast_expression (parser, NULL);
6523 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6524 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6525 case CPP_NOT:
6526 c_parser_consume_token (parser);
6527 exp_loc = c_parser_peek_token (parser)->location;
6528 op = c_parser_cast_expression (parser, NULL);
6529 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6530 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6531 case CPP_AND_AND:
6532 /* Refer to the address of a label as a pointer. */
6533 c_parser_consume_token (parser);
6534 if (c_parser_next_token_is (parser, CPP_NAME))
6536 ret.value = finish_label_address_expr
6537 (c_parser_peek_token (parser)->value, op_loc);
6538 c_parser_consume_token (parser);
6540 else
6542 c_parser_error (parser, "expected identifier");
6543 ret.value = error_mark_node;
6545 return ret;
6546 case CPP_KEYWORD:
6547 switch (c_parser_peek_token (parser)->keyword)
6549 case RID_SIZEOF:
6550 return c_parser_sizeof_expression (parser);
6551 case RID_ALIGNOF:
6552 return c_parser_alignof_expression (parser);
6553 case RID_EXTENSION:
6554 c_parser_consume_token (parser);
6555 ext = disable_extension_diagnostics ();
6556 ret = c_parser_cast_expression (parser, NULL);
6557 restore_extension_diagnostics (ext);
6558 return ret;
6559 case RID_REALPART:
6560 c_parser_consume_token (parser);
6561 exp_loc = c_parser_peek_token (parser)->location;
6562 op = c_parser_cast_expression (parser, NULL);
6563 op = default_function_array_conversion (exp_loc, op);
6564 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6565 case RID_IMAGPART:
6566 c_parser_consume_token (parser);
6567 exp_loc = c_parser_peek_token (parser)->location;
6568 op = c_parser_cast_expression (parser, NULL);
6569 op = default_function_array_conversion (exp_loc, op);
6570 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6571 case RID_TRANSACTION_ATOMIC:
6572 case RID_TRANSACTION_RELAXED:
6573 return c_parser_transaction_expression (parser,
6574 c_parser_peek_token (parser)->keyword);
6575 default:
6576 return c_parser_postfix_expression (parser);
6578 default:
6579 return c_parser_postfix_expression (parser);
6583 /* Parse a sizeof expression. */
6585 static struct c_expr
6586 c_parser_sizeof_expression (c_parser *parser)
6588 struct c_expr expr;
6589 location_t expr_loc;
6590 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6591 c_parser_consume_token (parser);
6592 c_inhibit_evaluation_warnings++;
6593 in_sizeof++;
6594 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6595 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6597 /* Either sizeof ( type-name ) or sizeof unary-expression
6598 starting with a compound literal. */
6599 struct c_type_name *type_name;
6600 c_parser_consume_token (parser);
6601 expr_loc = c_parser_peek_token (parser)->location;
6602 type_name = c_parser_type_name (parser);
6603 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6604 if (type_name == NULL)
6606 struct c_expr ret;
6607 c_inhibit_evaluation_warnings--;
6608 in_sizeof--;
6609 ret.value = error_mark_node;
6610 ret.original_code = ERROR_MARK;
6611 ret.original_type = NULL;
6612 return ret;
6614 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6616 expr = c_parser_postfix_expression_after_paren_type (parser,
6617 type_name,
6618 expr_loc);
6619 goto sizeof_expr;
6621 /* sizeof ( type-name ). */
6622 c_inhibit_evaluation_warnings--;
6623 in_sizeof--;
6624 return c_expr_sizeof_type (expr_loc, type_name);
6626 else
6628 expr_loc = c_parser_peek_token (parser)->location;
6629 expr = c_parser_unary_expression (parser);
6630 sizeof_expr:
6631 c_inhibit_evaluation_warnings--;
6632 in_sizeof--;
6633 mark_exp_read (expr.value);
6634 if (TREE_CODE (expr.value) == COMPONENT_REF
6635 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6636 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6637 return c_expr_sizeof_expr (expr_loc, expr);
6641 /* Parse an alignof expression. */
6643 static struct c_expr
6644 c_parser_alignof_expression (c_parser *parser)
6646 struct c_expr expr;
6647 location_t loc = c_parser_peek_token (parser)->location;
6648 tree alignof_spelling = c_parser_peek_token (parser)->value;
6649 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6650 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6651 "_Alignof") == 0;
6652 /* A diagnostic is not required for the use of this identifier in
6653 the implementation namespace; only diagnose it for the C11
6654 spelling because of existing code using the other spellings. */
6655 if (is_c11_alignof)
6657 if (flag_isoc99)
6658 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6659 alignof_spelling);
6660 else
6661 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6662 alignof_spelling);
6664 c_parser_consume_token (parser);
6665 c_inhibit_evaluation_warnings++;
6666 in_alignof++;
6667 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6668 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6670 /* Either __alignof__ ( type-name ) or __alignof__
6671 unary-expression starting with a compound literal. */
6672 location_t loc;
6673 struct c_type_name *type_name;
6674 struct c_expr ret;
6675 c_parser_consume_token (parser);
6676 loc = c_parser_peek_token (parser)->location;
6677 type_name = c_parser_type_name (parser);
6678 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6679 if (type_name == NULL)
6681 struct c_expr ret;
6682 c_inhibit_evaluation_warnings--;
6683 in_alignof--;
6684 ret.value = error_mark_node;
6685 ret.original_code = ERROR_MARK;
6686 ret.original_type = NULL;
6687 return ret;
6689 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6691 expr = c_parser_postfix_expression_after_paren_type (parser,
6692 type_name,
6693 loc);
6694 goto alignof_expr;
6696 /* alignof ( type-name ). */
6697 c_inhibit_evaluation_warnings--;
6698 in_alignof--;
6699 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6700 NULL, NULL),
6701 false, is_c11_alignof, 1);
6702 ret.original_code = ERROR_MARK;
6703 ret.original_type = NULL;
6704 return ret;
6706 else
6708 struct c_expr ret;
6709 expr = c_parser_unary_expression (parser);
6710 alignof_expr:
6711 mark_exp_read (expr.value);
6712 c_inhibit_evaluation_warnings--;
6713 in_alignof--;
6714 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6715 alignof_spelling);
6716 ret.value = c_alignof_expr (loc, expr.value);
6717 ret.original_code = ERROR_MARK;
6718 ret.original_type = NULL;
6719 return ret;
6723 /* Helper function to read arguments of builtins which are interfaces
6724 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6725 others. The name of the builtin is passed using BNAME parameter.
6726 Function returns true if there were no errors while parsing and
6727 stores the arguments in CEXPR_LIST. */
6728 static bool
6729 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6730 vec<c_expr_t, va_gc> **ret_cexpr_list,
6731 bool choose_expr_p)
6733 location_t loc = c_parser_peek_token (parser)->location;
6734 vec<c_expr_t, va_gc> *cexpr_list;
6735 c_expr_t expr;
6736 bool saved_force_folding_builtin_constant_p;
6738 *ret_cexpr_list = NULL;
6739 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6741 error_at (loc, "cannot take address of %qs", bname);
6742 return false;
6745 c_parser_consume_token (parser);
6747 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6749 c_parser_consume_token (parser);
6750 return true;
6753 saved_force_folding_builtin_constant_p
6754 = force_folding_builtin_constant_p;
6755 force_folding_builtin_constant_p |= choose_expr_p;
6756 expr = c_parser_expr_no_commas (parser, NULL);
6757 force_folding_builtin_constant_p
6758 = saved_force_folding_builtin_constant_p;
6759 vec_alloc (cexpr_list, 1);
6760 vec_safe_push (cexpr_list, expr);
6761 while (c_parser_next_token_is (parser, CPP_COMMA))
6763 c_parser_consume_token (parser);
6764 expr = c_parser_expr_no_commas (parser, NULL);
6765 vec_safe_push (cexpr_list, expr);
6768 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6769 return false;
6771 *ret_cexpr_list = cexpr_list;
6772 return true;
6775 /* This represents a single generic-association. */
6777 struct c_generic_association
6779 /* The location of the starting token of the type. */
6780 location_t type_location;
6781 /* The association's type, or NULL_TREE for 'default'. */
6782 tree type;
6783 /* The association's expression. */
6784 struct c_expr expression;
6787 /* Parse a generic-selection. (C11 6.5.1.1).
6789 generic-selection:
6790 _Generic ( assignment-expression , generic-assoc-list )
6792 generic-assoc-list:
6793 generic-association
6794 generic-assoc-list , generic-association
6796 generic-association:
6797 type-name : assignment-expression
6798 default : assignment-expression
6801 static struct c_expr
6802 c_parser_generic_selection (c_parser *parser)
6804 vec<c_generic_association> associations = vNULL;
6805 struct c_expr selector, error_expr;
6806 tree selector_type;
6807 struct c_generic_association matched_assoc;
6808 bool match_found = false;
6809 location_t generic_loc, selector_loc;
6811 error_expr.original_code = ERROR_MARK;
6812 error_expr.original_type = NULL;
6813 error_expr.value = error_mark_node;
6814 matched_assoc.type_location = UNKNOWN_LOCATION;
6815 matched_assoc.type = NULL_TREE;
6816 matched_assoc.expression = error_expr;
6818 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6819 generic_loc = c_parser_peek_token (parser)->location;
6820 c_parser_consume_token (parser);
6821 if (flag_isoc99)
6822 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6823 "ISO C99 does not support %<_Generic%>");
6824 else
6825 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6826 "ISO C90 does not support %<_Generic%>");
6828 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6829 return error_expr;
6831 c_inhibit_evaluation_warnings++;
6832 selector_loc = c_parser_peek_token (parser)->location;
6833 selector = c_parser_expr_no_commas (parser, NULL);
6834 selector = default_function_array_conversion (selector_loc, selector);
6835 c_inhibit_evaluation_warnings--;
6837 if (selector.value == error_mark_node)
6839 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6840 return selector;
6842 selector_type = TREE_TYPE (selector.value);
6843 /* In ISO C terms, rvalues (including the controlling expression of
6844 _Generic) do not have qualified types. */
6845 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6846 selector_type = TYPE_MAIN_VARIANT (selector_type);
6847 /* In ISO C terms, _Noreturn is not part of the type of expressions
6848 such as &abort, but in GCC it is represented internally as a type
6849 qualifier. */
6850 if (FUNCTION_POINTER_TYPE_P (selector_type)
6851 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6852 selector_type
6853 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6855 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6857 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6858 return error_expr;
6861 while (1)
6863 struct c_generic_association assoc, *iter;
6864 unsigned int ix;
6865 c_token *token = c_parser_peek_token (parser);
6867 assoc.type_location = token->location;
6868 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6870 c_parser_consume_token (parser);
6871 assoc.type = NULL_TREE;
6873 else
6875 struct c_type_name *type_name;
6877 type_name = c_parser_type_name (parser);
6878 if (type_name == NULL)
6880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6881 goto error_exit;
6883 assoc.type = groktypename (type_name, NULL, NULL);
6884 if (assoc.type == error_mark_node)
6886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6887 goto error_exit;
6890 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6891 error_at (assoc.type_location,
6892 "%<_Generic%> association has function type");
6893 else if (!COMPLETE_TYPE_P (assoc.type))
6894 error_at (assoc.type_location,
6895 "%<_Generic%> association has incomplete type");
6897 if (variably_modified_type_p (assoc.type, NULL_TREE))
6898 error_at (assoc.type_location,
6899 "%<_Generic%> association has "
6900 "variable length type");
6903 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6906 goto error_exit;
6909 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6910 if (assoc.expression.value == error_mark_node)
6912 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6913 goto error_exit;
6916 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6918 if (assoc.type == NULL_TREE)
6920 if (iter->type == NULL_TREE)
6922 error_at (assoc.type_location,
6923 "duplicate %<default%> case in %<_Generic%>");
6924 inform (iter->type_location, "original %<default%> is here");
6927 else if (iter->type != NULL_TREE)
6929 if (comptypes (assoc.type, iter->type))
6931 error_at (assoc.type_location,
6932 "%<_Generic%> specifies two compatible types");
6933 inform (iter->type_location, "compatible type is here");
6938 if (assoc.type == NULL_TREE)
6940 if (!match_found)
6942 matched_assoc = assoc;
6943 match_found = true;
6946 else if (comptypes (assoc.type, selector_type))
6948 if (!match_found || matched_assoc.type == NULL_TREE)
6950 matched_assoc = assoc;
6951 match_found = true;
6953 else
6955 error_at (assoc.type_location,
6956 "%<_Generic> selector matches multiple associations");
6957 inform (matched_assoc.type_location,
6958 "other match is here");
6962 associations.safe_push (assoc);
6964 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6965 break;
6966 c_parser_consume_token (parser);
6969 associations.release ();
6971 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6973 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6974 return error_expr;
6977 if (!match_found)
6979 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6980 "compatible with any association",
6981 selector_type);
6982 return error_expr;
6985 return matched_assoc.expression;
6987 error_exit:
6988 associations.release ();
6989 return error_expr;
6992 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6994 postfix-expression:
6995 primary-expression
6996 postfix-expression [ expression ]
6997 postfix-expression ( argument-expression-list[opt] )
6998 postfix-expression . identifier
6999 postfix-expression -> identifier
7000 postfix-expression ++
7001 postfix-expression --
7002 ( type-name ) { initializer-list }
7003 ( type-name ) { initializer-list , }
7005 argument-expression-list:
7006 argument-expression
7007 argument-expression-list , argument-expression
7009 primary-expression:
7010 identifier
7011 constant
7012 string-literal
7013 ( expression )
7014 generic-selection
7016 GNU extensions:
7018 primary-expression:
7019 __func__
7020 (treated as a keyword in GNU C)
7021 __FUNCTION__
7022 __PRETTY_FUNCTION__
7023 ( compound-statement )
7024 __builtin_va_arg ( assignment-expression , type-name )
7025 __builtin_offsetof ( type-name , offsetof-member-designator )
7026 __builtin_choose_expr ( assignment-expression ,
7027 assignment-expression ,
7028 assignment-expression )
7029 __builtin_types_compatible_p ( type-name , type-name )
7030 __builtin_complex ( assignment-expression , assignment-expression )
7031 __builtin_shuffle ( assignment-expression , assignment-expression )
7032 __builtin_shuffle ( assignment-expression ,
7033 assignment-expression ,
7034 assignment-expression, )
7036 offsetof-member-designator:
7037 identifier
7038 offsetof-member-designator . identifier
7039 offsetof-member-designator [ expression ]
7041 Objective-C:
7043 primary-expression:
7044 [ objc-receiver objc-message-args ]
7045 @selector ( objc-selector-arg )
7046 @protocol ( identifier )
7047 @encode ( type-name )
7048 objc-string-literal
7049 Classname . identifier
7052 static struct c_expr
7053 c_parser_postfix_expression (c_parser *parser)
7055 struct c_expr expr, e1;
7056 struct c_type_name *t1, *t2;
7057 location_t loc = c_parser_peek_token (parser)->location;;
7058 expr.original_code = ERROR_MARK;
7059 expr.original_type = NULL;
7060 switch (c_parser_peek_token (parser)->type)
7062 case CPP_NUMBER:
7063 expr.value = c_parser_peek_token (parser)->value;
7064 loc = c_parser_peek_token (parser)->location;
7065 c_parser_consume_token (parser);
7066 if (TREE_CODE (expr.value) == FIXED_CST
7067 && !targetm.fixed_point_supported_p ())
7069 error_at (loc, "fixed-point types not supported for this target");
7070 expr.value = error_mark_node;
7072 break;
7073 case CPP_CHAR:
7074 case CPP_CHAR16:
7075 case CPP_CHAR32:
7076 case CPP_WCHAR:
7077 expr.value = c_parser_peek_token (parser)->value;
7078 c_parser_consume_token (parser);
7079 break;
7080 case CPP_STRING:
7081 case CPP_STRING16:
7082 case CPP_STRING32:
7083 case CPP_WSTRING:
7084 case CPP_UTF8STRING:
7085 expr.value = c_parser_peek_token (parser)->value;
7086 expr.original_code = STRING_CST;
7087 c_parser_consume_token (parser);
7088 break;
7089 case CPP_OBJC_STRING:
7090 gcc_assert (c_dialect_objc ());
7091 expr.value
7092 = objc_build_string_object (c_parser_peek_token (parser)->value);
7093 c_parser_consume_token (parser);
7094 break;
7095 case CPP_NAME:
7096 switch (c_parser_peek_token (parser)->id_kind)
7098 case C_ID_ID:
7100 tree id = c_parser_peek_token (parser)->value;
7101 c_parser_consume_token (parser);
7102 expr.value = build_external_ref (loc, id,
7103 (c_parser_peek_token (parser)->type
7104 == CPP_OPEN_PAREN),
7105 &expr.original_type);
7106 break;
7108 case C_ID_CLASSNAME:
7110 /* Here we parse the Objective-C 2.0 Class.name dot
7111 syntax. */
7112 tree class_name = c_parser_peek_token (parser)->value;
7113 tree component;
7114 c_parser_consume_token (parser);
7115 gcc_assert (c_dialect_objc ());
7116 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7118 expr.value = error_mark_node;
7119 break;
7121 if (c_parser_next_token_is_not (parser, CPP_NAME))
7123 c_parser_error (parser, "expected identifier");
7124 expr.value = error_mark_node;
7125 break;
7127 component = c_parser_peek_token (parser)->value;
7128 c_parser_consume_token (parser);
7129 expr.value = objc_build_class_component_ref (class_name,
7130 component);
7131 break;
7133 default:
7134 c_parser_error (parser, "expected expression");
7135 expr.value = error_mark_node;
7136 break;
7138 break;
7139 case CPP_OPEN_PAREN:
7140 /* A parenthesized expression, statement expression or compound
7141 literal. */
7142 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7144 /* A statement expression. */
7145 tree stmt;
7146 location_t brace_loc;
7147 c_parser_consume_token (parser);
7148 brace_loc = c_parser_peek_token (parser)->location;
7149 c_parser_consume_token (parser);
7150 if (!building_stmt_list_p ())
7152 error_at (loc, "braced-group within expression allowed "
7153 "only inside a function");
7154 parser->error = true;
7155 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7156 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7157 expr.value = error_mark_node;
7158 break;
7160 stmt = c_begin_stmt_expr ();
7161 c_parser_compound_statement_nostart (parser);
7162 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7163 "expected %<)%>");
7164 pedwarn (loc, OPT_Wpedantic,
7165 "ISO C forbids braced-groups within expressions");
7166 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7167 mark_exp_read (expr.value);
7169 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7171 /* A compound literal. ??? Can we actually get here rather
7172 than going directly to
7173 c_parser_postfix_expression_after_paren_type from
7174 elsewhere? */
7175 location_t loc;
7176 struct c_type_name *type_name;
7177 c_parser_consume_token (parser);
7178 loc = c_parser_peek_token (parser)->location;
7179 type_name = c_parser_type_name (parser);
7180 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7181 "expected %<)%>");
7182 if (type_name == NULL)
7184 expr.value = error_mark_node;
7186 else
7187 expr = c_parser_postfix_expression_after_paren_type (parser,
7188 type_name,
7189 loc);
7191 else
7193 /* A parenthesized expression. */
7194 c_parser_consume_token (parser);
7195 expr = c_parser_expression (parser);
7196 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7197 TREE_NO_WARNING (expr.value) = 1;
7198 if (expr.original_code != C_MAYBE_CONST_EXPR)
7199 expr.original_code = ERROR_MARK;
7200 /* Don't change EXPR.ORIGINAL_TYPE. */
7201 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7202 "expected %<)%>");
7204 break;
7205 case CPP_KEYWORD:
7206 switch (c_parser_peek_token (parser)->keyword)
7208 case RID_FUNCTION_NAME:
7209 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7210 "%<__FUNCTION__%> predefined identifier");
7211 expr.value = fname_decl (loc,
7212 c_parser_peek_token (parser)->keyword,
7213 c_parser_peek_token (parser)->value);
7214 c_parser_consume_token (parser);
7215 break;
7216 case RID_PRETTY_FUNCTION_NAME:
7217 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7218 "%<__PRETTY_FUNCTION__%> predefined identifier");
7219 expr.value = fname_decl (loc,
7220 c_parser_peek_token (parser)->keyword,
7221 c_parser_peek_token (parser)->value);
7222 c_parser_consume_token (parser);
7223 break;
7224 case RID_C99_FUNCTION_NAME:
7225 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7226 "%<__func__%> predefined identifier");
7227 expr.value = fname_decl (loc,
7228 c_parser_peek_token (parser)->keyword,
7229 c_parser_peek_token (parser)->value);
7230 c_parser_consume_token (parser);
7231 break;
7232 case RID_VA_ARG:
7233 c_parser_consume_token (parser);
7234 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7236 expr.value = error_mark_node;
7237 break;
7239 e1 = c_parser_expr_no_commas (parser, NULL);
7240 mark_exp_read (e1.value);
7241 e1.value = c_fully_fold (e1.value, false, NULL);
7242 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7244 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7245 expr.value = error_mark_node;
7246 break;
7248 loc = c_parser_peek_token (parser)->location;
7249 t1 = c_parser_type_name (parser);
7250 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7251 "expected %<)%>");
7252 if (t1 == NULL)
7254 expr.value = error_mark_node;
7256 else
7258 tree type_expr = NULL_TREE;
7259 expr.value = c_build_va_arg (loc, e1.value,
7260 groktypename (t1, &type_expr, NULL));
7261 if (type_expr)
7263 expr.value = build2 (C_MAYBE_CONST_EXPR,
7264 TREE_TYPE (expr.value), type_expr,
7265 expr.value);
7266 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7269 break;
7270 case RID_OFFSETOF:
7271 c_parser_consume_token (parser);
7272 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7274 expr.value = error_mark_node;
7275 break;
7277 t1 = c_parser_type_name (parser);
7278 if (t1 == NULL)
7279 parser->error = true;
7280 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7281 gcc_assert (parser->error);
7282 if (parser->error)
7284 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7285 expr.value = error_mark_node;
7286 break;
7290 tree type = groktypename (t1, NULL, NULL);
7291 tree offsetof_ref;
7292 if (type == error_mark_node)
7293 offsetof_ref = error_mark_node;
7294 else
7296 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7297 SET_EXPR_LOCATION (offsetof_ref, loc);
7299 /* Parse the second argument to __builtin_offsetof. We
7300 must have one identifier, and beyond that we want to
7301 accept sub structure and sub array references. */
7302 if (c_parser_next_token_is (parser, CPP_NAME))
7304 offsetof_ref = build_component_ref
7305 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7306 c_parser_consume_token (parser);
7307 while (c_parser_next_token_is (parser, CPP_DOT)
7308 || c_parser_next_token_is (parser,
7309 CPP_OPEN_SQUARE)
7310 || c_parser_next_token_is (parser,
7311 CPP_DEREF))
7313 if (c_parser_next_token_is (parser, CPP_DEREF))
7315 loc = c_parser_peek_token (parser)->location;
7316 offsetof_ref = build_array_ref (loc,
7317 offsetof_ref,
7318 integer_zero_node);
7319 goto do_dot;
7321 else if (c_parser_next_token_is (parser, CPP_DOT))
7323 do_dot:
7324 c_parser_consume_token (parser);
7325 if (c_parser_next_token_is_not (parser,
7326 CPP_NAME))
7328 c_parser_error (parser, "expected identifier");
7329 break;
7331 offsetof_ref = build_component_ref
7332 (loc, offsetof_ref,
7333 c_parser_peek_token (parser)->value);
7334 c_parser_consume_token (parser);
7336 else
7338 struct c_expr ce;
7339 tree idx;
7340 loc = c_parser_peek_token (parser)->location;
7341 c_parser_consume_token (parser);
7342 ce = c_parser_expression (parser);
7343 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7344 idx = ce.value;
7345 idx = c_fully_fold (idx, false, NULL);
7346 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7347 "expected %<]%>");
7348 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7352 else
7353 c_parser_error (parser, "expected identifier");
7354 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7355 "expected %<)%>");
7356 expr.value = fold_offsetof (offsetof_ref);
7358 break;
7359 case RID_CHOOSE_EXPR:
7361 vec<c_expr_t, va_gc> *cexpr_list;
7362 c_expr_t *e1_p, *e2_p, *e3_p;
7363 tree c;
7365 c_parser_consume_token (parser);
7366 if (!c_parser_get_builtin_args (parser,
7367 "__builtin_choose_expr",
7368 &cexpr_list, true))
7370 expr.value = error_mark_node;
7371 break;
7374 if (vec_safe_length (cexpr_list) != 3)
7376 error_at (loc, "wrong number of arguments to "
7377 "%<__builtin_choose_expr%>");
7378 expr.value = error_mark_node;
7379 break;
7382 e1_p = &(*cexpr_list)[0];
7383 e2_p = &(*cexpr_list)[1];
7384 e3_p = &(*cexpr_list)[2];
7386 c = e1_p->value;
7387 mark_exp_read (e2_p->value);
7388 mark_exp_read (e3_p->value);
7389 if (TREE_CODE (c) != INTEGER_CST
7390 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7391 error_at (loc,
7392 "first argument to %<__builtin_choose_expr%> not"
7393 " a constant");
7394 constant_expression_warning (c);
7395 expr = integer_zerop (c) ? *e3_p : *e2_p;
7396 break;
7398 case RID_TYPES_COMPATIBLE_P:
7399 c_parser_consume_token (parser);
7400 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7402 expr.value = error_mark_node;
7403 break;
7405 t1 = c_parser_type_name (parser);
7406 if (t1 == NULL)
7408 expr.value = error_mark_node;
7409 break;
7411 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7413 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7414 expr.value = error_mark_node;
7415 break;
7417 t2 = c_parser_type_name (parser);
7418 if (t2 == NULL)
7420 expr.value = error_mark_node;
7421 break;
7423 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7424 "expected %<)%>");
7426 tree e1, e2;
7427 e1 = groktypename (t1, NULL, NULL);
7428 e2 = groktypename (t2, NULL, NULL);
7429 if (e1 == error_mark_node || e2 == error_mark_node)
7431 expr.value = error_mark_node;
7432 break;
7435 e1 = TYPE_MAIN_VARIANT (e1);
7436 e2 = TYPE_MAIN_VARIANT (e2);
7438 expr.value
7439 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7441 break;
7442 case RID_BUILTIN_COMPLEX:
7444 vec<c_expr_t, va_gc> *cexpr_list;
7445 c_expr_t *e1_p, *e2_p;
7447 c_parser_consume_token (parser);
7448 if (!c_parser_get_builtin_args (parser,
7449 "__builtin_complex",
7450 &cexpr_list, false))
7452 expr.value = error_mark_node;
7453 break;
7456 if (vec_safe_length (cexpr_list) != 2)
7458 error_at (loc, "wrong number of arguments to "
7459 "%<__builtin_complex%>");
7460 expr.value = error_mark_node;
7461 break;
7464 e1_p = &(*cexpr_list)[0];
7465 e2_p = &(*cexpr_list)[1];
7467 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7468 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7469 e1_p->value = convert (TREE_TYPE (e1_p->value),
7470 TREE_OPERAND (e1_p->value, 0));
7471 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7472 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7473 e2_p->value = convert (TREE_TYPE (e2_p->value),
7474 TREE_OPERAND (e2_p->value, 0));
7475 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7476 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7477 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7478 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7480 error_at (loc, "%<__builtin_complex%> operand "
7481 "not of real binary floating-point type");
7482 expr.value = error_mark_node;
7483 break;
7485 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7486 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7488 error_at (loc,
7489 "%<__builtin_complex%> operands of different types");
7490 expr.value = error_mark_node;
7491 break;
7493 pedwarn_c90 (loc, OPT_Wpedantic,
7494 "ISO C90 does not support complex types");
7495 expr.value = build2 (COMPLEX_EXPR,
7496 build_complex_type
7497 (TYPE_MAIN_VARIANT
7498 (TREE_TYPE (e1_p->value))),
7499 e1_p->value, e2_p->value);
7500 break;
7502 case RID_BUILTIN_SHUFFLE:
7504 vec<c_expr_t, va_gc> *cexpr_list;
7505 unsigned int i;
7506 c_expr_t *p;
7508 c_parser_consume_token (parser);
7509 if (!c_parser_get_builtin_args (parser,
7510 "__builtin_shuffle",
7511 &cexpr_list, false))
7513 expr.value = error_mark_node;
7514 break;
7517 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7518 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7520 if (vec_safe_length (cexpr_list) == 2)
7521 expr.value =
7522 c_build_vec_perm_expr
7523 (loc, (*cexpr_list)[0].value,
7524 NULL_TREE, (*cexpr_list)[1].value);
7526 else if (vec_safe_length (cexpr_list) == 3)
7527 expr.value =
7528 c_build_vec_perm_expr
7529 (loc, (*cexpr_list)[0].value,
7530 (*cexpr_list)[1].value,
7531 (*cexpr_list)[2].value);
7532 else
7534 error_at (loc, "wrong number of arguments to "
7535 "%<__builtin_shuffle%>");
7536 expr.value = error_mark_node;
7538 break;
7540 case RID_AT_SELECTOR:
7541 gcc_assert (c_dialect_objc ());
7542 c_parser_consume_token (parser);
7543 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7545 expr.value = error_mark_node;
7546 break;
7549 tree sel = c_parser_objc_selector_arg (parser);
7550 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7551 "expected %<)%>");
7552 expr.value = objc_build_selector_expr (loc, sel);
7554 break;
7555 case RID_AT_PROTOCOL:
7556 gcc_assert (c_dialect_objc ());
7557 c_parser_consume_token (parser);
7558 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7560 expr.value = error_mark_node;
7561 break;
7563 if (c_parser_next_token_is_not (parser, CPP_NAME))
7565 c_parser_error (parser, "expected identifier");
7566 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7567 expr.value = error_mark_node;
7568 break;
7571 tree id = c_parser_peek_token (parser)->value;
7572 c_parser_consume_token (parser);
7573 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7574 "expected %<)%>");
7575 expr.value = objc_build_protocol_expr (id);
7577 break;
7578 case RID_AT_ENCODE:
7579 /* Extension to support C-structures in the archiver. */
7580 gcc_assert (c_dialect_objc ());
7581 c_parser_consume_token (parser);
7582 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7584 expr.value = error_mark_node;
7585 break;
7587 t1 = c_parser_type_name (parser);
7588 if (t1 == NULL)
7590 expr.value = error_mark_node;
7591 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7592 break;
7594 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7595 "expected %<)%>");
7597 tree type = groktypename (t1, NULL, NULL);
7598 expr.value = objc_build_encode_expr (type);
7600 break;
7601 case RID_GENERIC:
7602 expr = c_parser_generic_selection (parser);
7603 break;
7604 case RID_CILK_SPAWN:
7605 c_parser_consume_token (parser);
7606 if (!flag_cilkplus)
7608 error_at (loc, "-fcilkplus must be enabled to use "
7609 "%<_Cilk_spawn%>");
7610 expr = c_parser_postfix_expression (parser);
7611 expr.value = error_mark_node;
7613 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7615 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7616 "are not permitted");
7617 /* Now flush out all the _Cilk_spawns. */
7618 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7619 c_parser_consume_token (parser);
7620 expr = c_parser_postfix_expression (parser);
7622 else
7624 expr = c_parser_postfix_expression (parser);
7625 expr.value = build_cilk_spawn (loc, expr.value);
7627 break;
7628 default:
7629 c_parser_error (parser, "expected expression");
7630 expr.value = error_mark_node;
7631 break;
7633 break;
7634 case CPP_OPEN_SQUARE:
7635 if (c_dialect_objc ())
7637 tree receiver, args;
7638 c_parser_consume_token (parser);
7639 receiver = c_parser_objc_receiver (parser);
7640 args = c_parser_objc_message_args (parser);
7641 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7642 "expected %<]%>");
7643 expr.value = objc_build_message_expr (receiver, args);
7644 break;
7646 /* Else fall through to report error. */
7647 default:
7648 c_parser_error (parser, "expected expression");
7649 expr.value = error_mark_node;
7650 break;
7652 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7655 /* Parse a postfix expression after a parenthesized type name: the
7656 brace-enclosed initializer of a compound literal, possibly followed
7657 by some postfix operators. This is separate because it is not
7658 possible to tell until after the type name whether a cast
7659 expression has a cast or a compound literal, or whether the operand
7660 of sizeof is a parenthesized type name or starts with a compound
7661 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7662 location of the first token after the parentheses around the type
7663 name. */
7665 static struct c_expr
7666 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7667 struct c_type_name *type_name,
7668 location_t type_loc)
7670 tree type;
7671 struct c_expr init;
7672 bool non_const;
7673 struct c_expr expr;
7674 location_t start_loc;
7675 tree type_expr = NULL_TREE;
7676 bool type_expr_const = true;
7677 check_compound_literal_type (type_loc, type_name);
7678 start_init (NULL_TREE, NULL, 0);
7679 type = groktypename (type_name, &type_expr, &type_expr_const);
7680 start_loc = c_parser_peek_token (parser)->location;
7681 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7683 error_at (type_loc, "compound literal has variable size");
7684 type = error_mark_node;
7686 init = c_parser_braced_init (parser, type, false);
7687 finish_init ();
7688 maybe_warn_string_init (type_loc, type, init);
7690 if (type != error_mark_node
7691 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7692 && current_function_decl)
7694 error ("compound literal qualified by address-space qualifier");
7695 type = error_mark_node;
7698 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7699 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7700 ? CONSTRUCTOR_NON_CONST (init.value)
7701 : init.original_code == C_MAYBE_CONST_EXPR);
7702 non_const |= !type_expr_const;
7703 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7704 expr.original_code = ERROR_MARK;
7705 expr.original_type = NULL;
7706 if (type_expr)
7708 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7710 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7711 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7713 else
7715 gcc_assert (!non_const);
7716 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7717 type_expr, expr.value);
7720 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7723 /* Callback function for sizeof_pointer_memaccess_warning to compare
7724 types. */
7726 static bool
7727 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7729 return comptypes (type1, type2) == 1;
7732 /* Parse a postfix expression after the initial primary or compound
7733 literal; that is, parse a series of postfix operators.
7735 EXPR_LOC is the location of the primary expression. */
7737 static struct c_expr
7738 c_parser_postfix_expression_after_primary (c_parser *parser,
7739 location_t expr_loc,
7740 struct c_expr expr)
7742 struct c_expr orig_expr;
7743 tree ident, idx;
7744 location_t sizeof_arg_loc[3];
7745 tree sizeof_arg[3];
7746 unsigned int literal_zero_mask;
7747 unsigned int i;
7748 vec<tree, va_gc> *exprlist;
7749 vec<tree, va_gc> *origtypes = NULL;
7750 vec<location_t> arg_loc = vNULL;
7752 while (true)
7754 location_t op_loc = c_parser_peek_token (parser)->location;
7755 switch (c_parser_peek_token (parser)->type)
7757 case CPP_OPEN_SQUARE:
7758 /* Array reference. */
7759 c_parser_consume_token (parser);
7760 if (flag_cilkplus
7761 && c_parser_peek_token (parser)->type == CPP_COLON)
7762 /* If we are here, then we have something like this:
7763 Array [ : ]
7765 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7766 expr.value);
7767 else
7769 idx = c_parser_expression (parser).value;
7770 /* Here we have 3 options:
7771 1. Array [EXPR] -- Normal Array call.
7772 2. Array [EXPR : EXPR] -- Array notation without stride.
7773 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7775 For 1, we just handle it just like a normal array expression.
7776 For 2 and 3 we handle it like we handle array notations. The
7777 idx value we have above becomes the initial/start index.
7779 if (flag_cilkplus
7780 && c_parser_peek_token (parser)->type == CPP_COLON)
7781 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7782 expr.value);
7783 else
7785 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7786 "expected %<]%>");
7787 expr.value = build_array_ref (op_loc, expr.value, idx);
7790 expr.original_code = ERROR_MARK;
7791 expr.original_type = NULL;
7792 break;
7793 case CPP_OPEN_PAREN:
7794 /* Function call. */
7795 c_parser_consume_token (parser);
7796 for (i = 0; i < 3; i++)
7798 sizeof_arg[i] = NULL_TREE;
7799 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7801 literal_zero_mask = 0;
7802 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7803 exprlist = NULL;
7804 else
7805 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7806 sizeof_arg_loc, sizeof_arg,
7807 &arg_loc, &literal_zero_mask);
7808 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7809 "expected %<)%>");
7810 orig_expr = expr;
7811 mark_exp_read (expr.value);
7812 if (warn_sizeof_pointer_memaccess)
7813 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7814 expr.value, exprlist,
7815 sizeof_arg,
7816 sizeof_ptr_memacc_comptypes);
7817 if (warn_memset_transposed_args
7818 && TREE_CODE (expr.value) == FUNCTION_DECL
7819 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7820 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7821 && vec_safe_length (exprlist) == 3
7822 && integer_zerop ((*exprlist)[2])
7823 && (literal_zero_mask & (1 << 2)) != 0
7824 && (!integer_zerop ((*exprlist)[1])
7825 || (literal_zero_mask & (1 << 1)) == 0))
7826 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7827 "%<memset%> used with constant zero length parameter; "
7828 "this could be due to transposed parameters");
7830 expr.value
7831 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7832 exprlist, origtypes);
7833 expr.original_code = ERROR_MARK;
7834 if (TREE_CODE (expr.value) == INTEGER_CST
7835 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7836 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7837 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7838 expr.original_code = C_MAYBE_CONST_EXPR;
7839 expr.original_type = NULL;
7840 if (exprlist)
7842 release_tree_vector (exprlist);
7843 release_tree_vector (origtypes);
7845 arg_loc.release ();
7846 break;
7847 case CPP_DOT:
7848 /* Structure element reference. */
7849 c_parser_consume_token (parser);
7850 expr = default_function_array_conversion (expr_loc, expr);
7851 if (c_parser_next_token_is (parser, CPP_NAME))
7852 ident = c_parser_peek_token (parser)->value;
7853 else
7855 c_parser_error (parser, "expected identifier");
7856 expr.value = error_mark_node;
7857 expr.original_code = ERROR_MARK;
7858 expr.original_type = NULL;
7859 return expr;
7861 c_parser_consume_token (parser);
7862 expr.value = build_component_ref (op_loc, expr.value, ident);
7863 expr.original_code = ERROR_MARK;
7864 if (TREE_CODE (expr.value) != COMPONENT_REF)
7865 expr.original_type = NULL;
7866 else
7868 /* Remember the original type of a bitfield. */
7869 tree field = TREE_OPERAND (expr.value, 1);
7870 if (TREE_CODE (field) != FIELD_DECL)
7871 expr.original_type = NULL;
7872 else
7873 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7875 break;
7876 case CPP_DEREF:
7877 /* Structure element reference. */
7878 c_parser_consume_token (parser);
7879 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7880 if (c_parser_next_token_is (parser, CPP_NAME))
7881 ident = c_parser_peek_token (parser)->value;
7882 else
7884 c_parser_error (parser, "expected identifier");
7885 expr.value = error_mark_node;
7886 expr.original_code = ERROR_MARK;
7887 expr.original_type = NULL;
7888 return expr;
7890 c_parser_consume_token (parser);
7891 expr.value = build_component_ref (op_loc,
7892 build_indirect_ref (op_loc,
7893 expr.value,
7894 RO_ARROW),
7895 ident);
7896 expr.original_code = ERROR_MARK;
7897 if (TREE_CODE (expr.value) != COMPONENT_REF)
7898 expr.original_type = NULL;
7899 else
7901 /* Remember the original type of a bitfield. */
7902 tree field = TREE_OPERAND (expr.value, 1);
7903 if (TREE_CODE (field) != FIELD_DECL)
7904 expr.original_type = NULL;
7905 else
7906 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7908 break;
7909 case CPP_PLUS_PLUS:
7910 /* Postincrement. */
7911 c_parser_consume_token (parser);
7912 /* If the expressions have array notations, we expand them. */
7913 if (flag_cilkplus
7914 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7915 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7916 else
7918 expr = default_function_array_read_conversion (expr_loc, expr);
7919 expr.value = build_unary_op (op_loc,
7920 POSTINCREMENT_EXPR, expr.value, 0);
7922 expr.original_code = ERROR_MARK;
7923 expr.original_type = NULL;
7924 break;
7925 case CPP_MINUS_MINUS:
7926 /* Postdecrement. */
7927 c_parser_consume_token (parser);
7928 /* If the expressions have array notations, we expand them. */
7929 if (flag_cilkplus
7930 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7931 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7932 else
7934 expr = default_function_array_read_conversion (expr_loc, expr);
7935 expr.value = build_unary_op (op_loc,
7936 POSTDECREMENT_EXPR, expr.value, 0);
7938 expr.original_code = ERROR_MARK;
7939 expr.original_type = NULL;
7940 break;
7941 default:
7942 return expr;
7947 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7949 expression:
7950 assignment-expression
7951 expression , assignment-expression
7954 static struct c_expr
7955 c_parser_expression (c_parser *parser)
7957 location_t tloc = c_parser_peek_token (parser)->location;
7958 struct c_expr expr;
7959 expr = c_parser_expr_no_commas (parser, NULL);
7960 if (c_parser_next_token_is (parser, CPP_COMMA))
7961 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7962 while (c_parser_next_token_is (parser, CPP_COMMA))
7964 struct c_expr next;
7965 tree lhsval;
7966 location_t loc = c_parser_peek_token (parser)->location;
7967 location_t expr_loc;
7968 c_parser_consume_token (parser);
7969 expr_loc = c_parser_peek_token (parser)->location;
7970 lhsval = expr.value;
7971 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7972 lhsval = TREE_OPERAND (lhsval, 1);
7973 if (DECL_P (lhsval) || handled_component_p (lhsval))
7974 mark_exp_read (lhsval);
7975 next = c_parser_expr_no_commas (parser, NULL);
7976 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7977 expr.value = build_compound_expr (loc, expr.value, next.value);
7978 expr.original_code = COMPOUND_EXPR;
7979 expr.original_type = next.original_type;
7981 return expr;
7984 /* Parse an expression and convert functions or arrays to pointers and
7985 lvalues to rvalues. */
7987 static struct c_expr
7988 c_parser_expression_conv (c_parser *parser)
7990 struct c_expr expr;
7991 location_t loc = c_parser_peek_token (parser)->location;
7992 expr = c_parser_expression (parser);
7993 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7994 return expr;
7997 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
7998 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8000 static inline void
8001 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8002 unsigned int idx)
8004 if (idx >= HOST_BITS_PER_INT)
8005 return;
8007 c_token *tok = c_parser_peek_token (parser);
8008 switch (tok->type)
8010 case CPP_NUMBER:
8011 case CPP_CHAR:
8012 case CPP_WCHAR:
8013 case CPP_CHAR16:
8014 case CPP_CHAR32:
8015 /* If a parameter is literal zero alone, remember it
8016 for -Wmemset-transposed-args warning. */
8017 if (integer_zerop (tok->value)
8018 && !TREE_OVERFLOW (tok->value)
8019 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8020 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8021 *literal_zero_mask |= 1U << idx;
8022 default:
8023 break;
8027 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8028 functions and arrays to pointers and lvalues to rvalues. If
8029 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8030 locations of function arguments into this vector.
8032 nonempty-expr-list:
8033 assignment-expression
8034 nonempty-expr-list , assignment-expression
8037 static vec<tree, va_gc> *
8038 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8039 vec<tree, va_gc> **p_orig_types,
8040 location_t *sizeof_arg_loc, tree *sizeof_arg,
8041 vec<location_t> *locations,
8042 unsigned int *literal_zero_mask)
8044 vec<tree, va_gc> *ret;
8045 vec<tree, va_gc> *orig_types;
8046 struct c_expr expr;
8047 location_t loc = c_parser_peek_token (parser)->location;
8048 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8049 unsigned int idx = 0;
8051 ret = make_tree_vector ();
8052 if (p_orig_types == NULL)
8053 orig_types = NULL;
8054 else
8055 orig_types = make_tree_vector ();
8057 if (sizeof_arg != NULL
8058 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8059 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8060 if (literal_zero_mask)
8061 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8062 expr = c_parser_expr_no_commas (parser, NULL);
8063 if (convert_p)
8064 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8065 if (fold_p)
8066 expr.value = c_fully_fold (expr.value, false, NULL);
8067 ret->quick_push (expr.value);
8068 if (orig_types)
8069 orig_types->quick_push (expr.original_type);
8070 if (locations)
8071 locations->safe_push (loc);
8072 if (sizeof_arg != NULL
8073 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8074 && expr.original_code == SIZEOF_EXPR)
8076 sizeof_arg[0] = c_last_sizeof_arg;
8077 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8079 while (c_parser_next_token_is (parser, CPP_COMMA))
8081 c_parser_consume_token (parser);
8082 loc = c_parser_peek_token (parser)->location;
8083 if (sizeof_arg != NULL
8084 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8085 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8086 else
8087 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8088 if (literal_zero_mask)
8089 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8090 expr = c_parser_expr_no_commas (parser, NULL);
8091 if (convert_p)
8092 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8093 if (fold_p)
8094 expr.value = c_fully_fold (expr.value, false, NULL);
8095 vec_safe_push (ret, expr.value);
8096 if (orig_types)
8097 vec_safe_push (orig_types, expr.original_type);
8098 if (locations)
8099 locations->safe_push (loc);
8100 if (++idx < 3
8101 && sizeof_arg != NULL
8102 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8103 && expr.original_code == SIZEOF_EXPR)
8105 sizeof_arg[idx] = c_last_sizeof_arg;
8106 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8109 if (orig_types)
8110 *p_orig_types = orig_types;
8111 return ret;
8114 /* Parse Objective-C-specific constructs. */
8116 /* Parse an objc-class-definition.
8118 objc-class-definition:
8119 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8120 objc-class-instance-variables[opt] objc-methodprotolist @end
8121 @implementation identifier objc-superclass[opt]
8122 objc-class-instance-variables[opt]
8123 @interface identifier ( identifier ) objc-protocol-refs[opt]
8124 objc-methodprotolist @end
8125 @interface identifier ( ) objc-protocol-refs[opt]
8126 objc-methodprotolist @end
8127 @implementation identifier ( identifier )
8129 objc-superclass:
8130 : identifier
8132 "@interface identifier (" must start "@interface identifier (
8133 identifier ) ...": objc-methodprotolist in the first production may
8134 not start with a parenthesized identifier as a declarator of a data
8135 definition with no declaration specifiers if the objc-superclass,
8136 objc-protocol-refs and objc-class-instance-variables are omitted. */
8138 static void
8139 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8141 bool iface_p;
8142 tree id1;
8143 tree superclass;
8144 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8145 iface_p = true;
8146 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8147 iface_p = false;
8148 else
8149 gcc_unreachable ();
8151 c_parser_consume_token (parser);
8152 if (c_parser_next_token_is_not (parser, CPP_NAME))
8154 c_parser_error (parser, "expected identifier");
8155 return;
8157 id1 = c_parser_peek_token (parser)->value;
8158 c_parser_consume_token (parser);
8159 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8161 /* We have a category or class extension. */
8162 tree id2;
8163 tree proto = NULL_TREE;
8164 c_parser_consume_token (parser);
8165 if (c_parser_next_token_is_not (parser, CPP_NAME))
8167 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8169 /* We have a class extension. */
8170 id2 = NULL_TREE;
8172 else
8174 c_parser_error (parser, "expected identifier or %<)%>");
8175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8176 return;
8179 else
8181 id2 = c_parser_peek_token (parser)->value;
8182 c_parser_consume_token (parser);
8184 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8185 if (!iface_p)
8187 objc_start_category_implementation (id1, id2);
8188 return;
8190 if (c_parser_next_token_is (parser, CPP_LESS))
8191 proto = c_parser_objc_protocol_refs (parser);
8192 objc_start_category_interface (id1, id2, proto, attributes);
8193 c_parser_objc_methodprotolist (parser);
8194 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8195 objc_finish_interface ();
8196 return;
8198 if (c_parser_next_token_is (parser, CPP_COLON))
8200 c_parser_consume_token (parser);
8201 if (c_parser_next_token_is_not (parser, CPP_NAME))
8203 c_parser_error (parser, "expected identifier");
8204 return;
8206 superclass = c_parser_peek_token (parser)->value;
8207 c_parser_consume_token (parser);
8209 else
8210 superclass = NULL_TREE;
8211 if (iface_p)
8213 tree proto = NULL_TREE;
8214 if (c_parser_next_token_is (parser, CPP_LESS))
8215 proto = c_parser_objc_protocol_refs (parser);
8216 objc_start_class_interface (id1, superclass, proto, attributes);
8218 else
8219 objc_start_class_implementation (id1, superclass);
8220 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8221 c_parser_objc_class_instance_variables (parser);
8222 if (iface_p)
8224 objc_continue_interface ();
8225 c_parser_objc_methodprotolist (parser);
8226 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8227 objc_finish_interface ();
8229 else
8231 objc_continue_implementation ();
8232 return;
8236 /* Parse objc-class-instance-variables.
8238 objc-class-instance-variables:
8239 { objc-instance-variable-decl-list[opt] }
8241 objc-instance-variable-decl-list:
8242 objc-visibility-spec
8243 objc-instance-variable-decl ;
8245 objc-instance-variable-decl-list objc-visibility-spec
8246 objc-instance-variable-decl-list objc-instance-variable-decl ;
8247 objc-instance-variable-decl-list ;
8249 objc-visibility-spec:
8250 @private
8251 @protected
8252 @public
8254 objc-instance-variable-decl:
8255 struct-declaration
8258 static void
8259 c_parser_objc_class_instance_variables (c_parser *parser)
8261 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8262 c_parser_consume_token (parser);
8263 while (c_parser_next_token_is_not (parser, CPP_EOF))
8265 tree decls;
8266 /* Parse any stray semicolon. */
8267 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8269 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8270 "extra semicolon");
8271 c_parser_consume_token (parser);
8272 continue;
8274 /* Stop if at the end of the instance variables. */
8275 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8277 c_parser_consume_token (parser);
8278 break;
8280 /* Parse any objc-visibility-spec. */
8281 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8283 c_parser_consume_token (parser);
8284 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8285 continue;
8287 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8289 c_parser_consume_token (parser);
8290 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8291 continue;
8293 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8295 c_parser_consume_token (parser);
8296 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8297 continue;
8299 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8301 c_parser_consume_token (parser);
8302 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8303 continue;
8305 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8307 c_parser_pragma (parser, pragma_external);
8308 continue;
8311 /* Parse some comma-separated declarations. */
8312 decls = c_parser_struct_declaration (parser);
8313 if (decls == NULL)
8315 /* There is a syntax error. We want to skip the offending
8316 tokens up to the next ';' (included) or '}'
8317 (excluded). */
8319 /* First, skip manually a ')' or ']'. This is because they
8320 reduce the nesting level, so c_parser_skip_until_found()
8321 wouldn't be able to skip past them. */
8322 c_token *token = c_parser_peek_token (parser);
8323 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8324 c_parser_consume_token (parser);
8326 /* Then, do the standard skipping. */
8327 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8329 /* We hopefully recovered. Start normal parsing again. */
8330 parser->error = false;
8331 continue;
8333 else
8335 /* Comma-separated instance variables are chained together
8336 in reverse order; add them one by one. */
8337 tree ivar = nreverse (decls);
8338 for (; ivar; ivar = DECL_CHAIN (ivar))
8339 objc_add_instance_variable (copy_node (ivar));
8341 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8345 /* Parse an objc-class-declaration.
8347 objc-class-declaration:
8348 @class identifier-list ;
8351 static void
8352 c_parser_objc_class_declaration (c_parser *parser)
8354 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8355 c_parser_consume_token (parser);
8356 /* Any identifiers, including those declared as type names, are OK
8357 here. */
8358 while (true)
8360 tree id;
8361 if (c_parser_next_token_is_not (parser, CPP_NAME))
8363 c_parser_error (parser, "expected identifier");
8364 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8365 parser->error = false;
8366 return;
8368 id = c_parser_peek_token (parser)->value;
8369 objc_declare_class (id);
8370 c_parser_consume_token (parser);
8371 if (c_parser_next_token_is (parser, CPP_COMMA))
8372 c_parser_consume_token (parser);
8373 else
8374 break;
8376 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8379 /* Parse an objc-alias-declaration.
8381 objc-alias-declaration:
8382 @compatibility_alias identifier identifier ;
8385 static void
8386 c_parser_objc_alias_declaration (c_parser *parser)
8388 tree id1, id2;
8389 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8390 c_parser_consume_token (parser);
8391 if (c_parser_next_token_is_not (parser, CPP_NAME))
8393 c_parser_error (parser, "expected identifier");
8394 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8395 return;
8397 id1 = c_parser_peek_token (parser)->value;
8398 c_parser_consume_token (parser);
8399 if (c_parser_next_token_is_not (parser, CPP_NAME))
8401 c_parser_error (parser, "expected identifier");
8402 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8403 return;
8405 id2 = c_parser_peek_token (parser)->value;
8406 c_parser_consume_token (parser);
8407 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8408 objc_declare_alias (id1, id2);
8411 /* Parse an objc-protocol-definition.
8413 objc-protocol-definition:
8414 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8415 @protocol identifier-list ;
8417 "@protocol identifier ;" should be resolved as "@protocol
8418 identifier-list ;": objc-methodprotolist may not start with a
8419 semicolon in the first alternative if objc-protocol-refs are
8420 omitted. */
8422 static void
8423 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8425 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8427 c_parser_consume_token (parser);
8428 if (c_parser_next_token_is_not (parser, CPP_NAME))
8430 c_parser_error (parser, "expected identifier");
8431 return;
8433 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8434 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8436 /* Any identifiers, including those declared as type names, are
8437 OK here. */
8438 while (true)
8440 tree id;
8441 if (c_parser_next_token_is_not (parser, CPP_NAME))
8443 c_parser_error (parser, "expected identifier");
8444 break;
8446 id = c_parser_peek_token (parser)->value;
8447 objc_declare_protocol (id, attributes);
8448 c_parser_consume_token (parser);
8449 if (c_parser_next_token_is (parser, CPP_COMMA))
8450 c_parser_consume_token (parser);
8451 else
8452 break;
8454 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8456 else
8458 tree id = c_parser_peek_token (parser)->value;
8459 tree proto = NULL_TREE;
8460 c_parser_consume_token (parser);
8461 if (c_parser_next_token_is (parser, CPP_LESS))
8462 proto = c_parser_objc_protocol_refs (parser);
8463 parser->objc_pq_context = true;
8464 objc_start_protocol (id, proto, attributes);
8465 c_parser_objc_methodprotolist (parser);
8466 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8467 parser->objc_pq_context = false;
8468 objc_finish_interface ();
8472 /* Parse an objc-method-type.
8474 objc-method-type:
8478 Return true if it is a class method (+) and false if it is
8479 an instance method (-).
8481 static inline bool
8482 c_parser_objc_method_type (c_parser *parser)
8484 switch (c_parser_peek_token (parser)->type)
8486 case CPP_PLUS:
8487 c_parser_consume_token (parser);
8488 return true;
8489 case CPP_MINUS:
8490 c_parser_consume_token (parser);
8491 return false;
8492 default:
8493 gcc_unreachable ();
8497 /* Parse an objc-method-definition.
8499 objc-method-definition:
8500 objc-method-type objc-method-decl ;[opt] compound-statement
8503 static void
8504 c_parser_objc_method_definition (c_parser *parser)
8506 bool is_class_method = c_parser_objc_method_type (parser);
8507 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8508 parser->objc_pq_context = true;
8509 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8510 &expr);
8511 if (decl == error_mark_node)
8512 return; /* Bail here. */
8514 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8516 c_parser_consume_token (parser);
8517 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8518 "extra semicolon in method definition specified");
8521 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8523 c_parser_error (parser, "expected %<{%>");
8524 return;
8527 parser->objc_pq_context = false;
8528 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8530 add_stmt (c_parser_compound_statement (parser));
8531 objc_finish_method_definition (current_function_decl);
8533 else
8535 /* This code is executed when we find a method definition
8536 outside of an @implementation context (or invalid for other
8537 reasons). Parse the method (to keep going) but do not emit
8538 any code.
8540 c_parser_compound_statement (parser);
8544 /* Parse an objc-methodprotolist.
8546 objc-methodprotolist:
8547 empty
8548 objc-methodprotolist objc-methodproto
8549 objc-methodprotolist declaration
8550 objc-methodprotolist ;
8551 @optional
8552 @required
8554 The declaration is a data definition, which may be missing
8555 declaration specifiers under the same rules and diagnostics as
8556 other data definitions outside functions, and the stray semicolon
8557 is diagnosed the same way as a stray semicolon outside a
8558 function. */
8560 static void
8561 c_parser_objc_methodprotolist (c_parser *parser)
8563 while (true)
8565 /* The list is terminated by @end. */
8566 switch (c_parser_peek_token (parser)->type)
8568 case CPP_SEMICOLON:
8569 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8570 "ISO C does not allow extra %<;%> outside of a function");
8571 c_parser_consume_token (parser);
8572 break;
8573 case CPP_PLUS:
8574 case CPP_MINUS:
8575 c_parser_objc_methodproto (parser);
8576 break;
8577 case CPP_PRAGMA:
8578 c_parser_pragma (parser, pragma_external);
8579 break;
8580 case CPP_EOF:
8581 return;
8582 default:
8583 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8584 return;
8585 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8586 c_parser_objc_at_property_declaration (parser);
8587 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8589 objc_set_method_opt (true);
8590 c_parser_consume_token (parser);
8592 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8594 objc_set_method_opt (false);
8595 c_parser_consume_token (parser);
8597 else
8598 c_parser_declaration_or_fndef (parser, false, false, true,
8599 false, true, NULL, vNULL);
8600 break;
8605 /* Parse an objc-methodproto.
8607 objc-methodproto:
8608 objc-method-type objc-method-decl ;
8611 static void
8612 c_parser_objc_methodproto (c_parser *parser)
8614 bool is_class_method = c_parser_objc_method_type (parser);
8615 tree decl, attributes = NULL_TREE;
8617 /* Remember protocol qualifiers in prototypes. */
8618 parser->objc_pq_context = true;
8619 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8620 NULL);
8621 /* Forget protocol qualifiers now. */
8622 parser->objc_pq_context = false;
8624 /* Do not allow the presence of attributes to hide an erroneous
8625 method implementation in the interface section. */
8626 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8628 c_parser_error (parser, "expected %<;%>");
8629 return;
8632 if (decl != error_mark_node)
8633 objc_add_method_declaration (is_class_method, decl, attributes);
8635 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8638 /* If we are at a position that method attributes may be present, check that
8639 there are not any parsed already (a syntax error) and then collect any
8640 specified at the current location. Finally, if new attributes were present,
8641 check that the next token is legal ( ';' for decls and '{' for defs). */
8643 static bool
8644 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8646 bool bad = false;
8647 if (*attributes)
8649 c_parser_error (parser,
8650 "method attributes must be specified at the end only");
8651 *attributes = NULL_TREE;
8652 bad = true;
8655 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8656 *attributes = c_parser_attributes (parser);
8658 /* If there were no attributes here, just report any earlier error. */
8659 if (*attributes == NULL_TREE || bad)
8660 return bad;
8662 /* If the attributes are followed by a ; or {, then just report any earlier
8663 error. */
8664 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8665 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8666 return bad;
8668 /* We've got attributes, but not at the end. */
8669 c_parser_error (parser,
8670 "expected %<;%> or %<{%> after method attribute definition");
8671 return true;
8674 /* Parse an objc-method-decl.
8676 objc-method-decl:
8677 ( objc-type-name ) objc-selector
8678 objc-selector
8679 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8680 objc-keyword-selector objc-optparmlist
8681 attributes
8683 objc-keyword-selector:
8684 objc-keyword-decl
8685 objc-keyword-selector objc-keyword-decl
8687 objc-keyword-decl:
8688 objc-selector : ( objc-type-name ) identifier
8689 objc-selector : identifier
8690 : ( objc-type-name ) identifier
8691 : identifier
8693 objc-optparmlist:
8694 objc-optparms objc-optellipsis
8696 objc-optparms:
8697 empty
8698 objc-opt-parms , parameter-declaration
8700 objc-optellipsis:
8701 empty
8702 , ...
8705 static tree
8706 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8707 tree *attributes, tree *expr)
8709 tree type = NULL_TREE;
8710 tree sel;
8711 tree parms = NULL_TREE;
8712 bool ellipsis = false;
8713 bool attr_err = false;
8715 *attributes = NULL_TREE;
8716 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8718 c_parser_consume_token (parser);
8719 type = c_parser_objc_type_name (parser);
8720 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8722 sel = c_parser_objc_selector (parser);
8723 /* If there is no selector, or a colon follows, we have an
8724 objc-keyword-selector. If there is a selector, and a colon does
8725 not follow, that selector ends the objc-method-decl. */
8726 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8728 tree tsel = sel;
8729 tree list = NULL_TREE;
8730 while (true)
8732 tree atype = NULL_TREE, id, keyworddecl;
8733 tree param_attr = NULL_TREE;
8734 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8735 break;
8736 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8738 c_parser_consume_token (parser);
8739 atype = c_parser_objc_type_name (parser);
8740 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8741 "expected %<)%>");
8743 /* New ObjC allows attributes on method parameters. */
8744 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8745 param_attr = c_parser_attributes (parser);
8746 if (c_parser_next_token_is_not (parser, CPP_NAME))
8748 c_parser_error (parser, "expected identifier");
8749 return error_mark_node;
8751 id = c_parser_peek_token (parser)->value;
8752 c_parser_consume_token (parser);
8753 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8754 list = chainon (list, keyworddecl);
8755 tsel = c_parser_objc_selector (parser);
8756 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8757 break;
8760 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8762 /* Parse the optional parameter list. Optional Objective-C
8763 method parameters follow the C syntax, and may include '...'
8764 to denote a variable number of arguments. */
8765 parms = make_node (TREE_LIST);
8766 while (c_parser_next_token_is (parser, CPP_COMMA))
8768 struct c_parm *parm;
8769 c_parser_consume_token (parser);
8770 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8772 ellipsis = true;
8773 c_parser_consume_token (parser);
8774 attr_err |= c_parser_objc_maybe_method_attributes
8775 (parser, attributes) ;
8776 break;
8778 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8779 if (parm == NULL)
8780 break;
8781 parms = chainon (parms,
8782 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8784 sel = list;
8786 else
8787 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8789 if (sel == NULL)
8791 c_parser_error (parser, "objective-c method declaration is expected");
8792 return error_mark_node;
8795 if (attr_err)
8796 return error_mark_node;
8798 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8801 /* Parse an objc-type-name.
8803 objc-type-name:
8804 objc-type-qualifiers[opt] type-name
8805 objc-type-qualifiers[opt]
8807 objc-type-qualifiers:
8808 objc-type-qualifier
8809 objc-type-qualifiers objc-type-qualifier
8811 objc-type-qualifier: one of
8812 in out inout bycopy byref oneway
8815 static tree
8816 c_parser_objc_type_name (c_parser *parser)
8818 tree quals = NULL_TREE;
8819 struct c_type_name *type_name = NULL;
8820 tree type = NULL_TREE;
8821 while (true)
8823 c_token *token = c_parser_peek_token (parser);
8824 if (token->type == CPP_KEYWORD
8825 && (token->keyword == RID_IN
8826 || token->keyword == RID_OUT
8827 || token->keyword == RID_INOUT
8828 || token->keyword == RID_BYCOPY
8829 || token->keyword == RID_BYREF
8830 || token->keyword == RID_ONEWAY))
8832 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8833 c_parser_consume_token (parser);
8835 else
8836 break;
8838 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8839 type_name = c_parser_type_name (parser);
8840 if (type_name)
8841 type = groktypename (type_name, NULL, NULL);
8843 /* If the type is unknown, and error has already been produced and
8844 we need to recover from the error. In that case, use NULL_TREE
8845 for the type, as if no type had been specified; this will use the
8846 default type ('id') which is good for error recovery. */
8847 if (type == error_mark_node)
8848 type = NULL_TREE;
8850 return build_tree_list (quals, type);
8853 /* Parse objc-protocol-refs.
8855 objc-protocol-refs:
8856 < identifier-list >
8859 static tree
8860 c_parser_objc_protocol_refs (c_parser *parser)
8862 tree list = NULL_TREE;
8863 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8864 c_parser_consume_token (parser);
8865 /* Any identifiers, including those declared as type names, are OK
8866 here. */
8867 while (true)
8869 tree id;
8870 if (c_parser_next_token_is_not (parser, CPP_NAME))
8872 c_parser_error (parser, "expected identifier");
8873 break;
8875 id = c_parser_peek_token (parser)->value;
8876 list = chainon (list, build_tree_list (NULL_TREE, id));
8877 c_parser_consume_token (parser);
8878 if (c_parser_next_token_is (parser, CPP_COMMA))
8879 c_parser_consume_token (parser);
8880 else
8881 break;
8883 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8884 return list;
8887 /* Parse an objc-try-catch-finally-statement.
8889 objc-try-catch-finally-statement:
8890 @try compound-statement objc-catch-list[opt]
8891 @try compound-statement objc-catch-list[opt] @finally compound-statement
8893 objc-catch-list:
8894 @catch ( objc-catch-parameter-declaration ) compound-statement
8895 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8897 objc-catch-parameter-declaration:
8898 parameter-declaration
8899 '...'
8901 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8903 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8904 for C++. Keep them in sync. */
8906 static void
8907 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8909 location_t location;
8910 tree stmt;
8912 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8913 c_parser_consume_token (parser);
8914 location = c_parser_peek_token (parser)->location;
8915 objc_maybe_warn_exceptions (location);
8916 stmt = c_parser_compound_statement (parser);
8917 objc_begin_try_stmt (location, stmt);
8919 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8921 struct c_parm *parm;
8922 tree parameter_declaration = error_mark_node;
8923 bool seen_open_paren = false;
8925 c_parser_consume_token (parser);
8926 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8927 seen_open_paren = true;
8928 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8930 /* We have "@catch (...)" (where the '...' are literally
8931 what is in the code). Skip the '...'.
8932 parameter_declaration is set to NULL_TREE, and
8933 objc_being_catch_clauses() knows that that means
8934 '...'. */
8935 c_parser_consume_token (parser);
8936 parameter_declaration = NULL_TREE;
8938 else
8940 /* We have "@catch (NSException *exception)" or something
8941 like that. Parse the parameter declaration. */
8942 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8943 if (parm == NULL)
8944 parameter_declaration = error_mark_node;
8945 else
8946 parameter_declaration = grokparm (parm, NULL);
8948 if (seen_open_paren)
8949 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8950 else
8952 /* If there was no open parenthesis, we are recovering from
8953 an error, and we are trying to figure out what mistake
8954 the user has made. */
8956 /* If there is an immediate closing parenthesis, the user
8957 probably forgot the opening one (ie, they typed "@catch
8958 NSException *e)". Parse the closing parenthesis and keep
8959 going. */
8960 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8961 c_parser_consume_token (parser);
8963 /* If these is no immediate closing parenthesis, the user
8964 probably doesn't know that parenthesis are required at
8965 all (ie, they typed "@catch NSException *e"). So, just
8966 forget about the closing parenthesis and keep going. */
8968 objc_begin_catch_clause (parameter_declaration);
8969 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8970 c_parser_compound_statement_nostart (parser);
8971 objc_finish_catch_clause ();
8973 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8975 c_parser_consume_token (parser);
8976 location = c_parser_peek_token (parser)->location;
8977 stmt = c_parser_compound_statement (parser);
8978 objc_build_finally_clause (location, stmt);
8980 objc_finish_try_stmt ();
8983 /* Parse an objc-synchronized-statement.
8985 objc-synchronized-statement:
8986 @synchronized ( expression ) compound-statement
8989 static void
8990 c_parser_objc_synchronized_statement (c_parser *parser)
8992 location_t loc;
8993 tree expr, stmt;
8994 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8995 c_parser_consume_token (parser);
8996 loc = c_parser_peek_token (parser)->location;
8997 objc_maybe_warn_exceptions (loc);
8998 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9000 struct c_expr ce = c_parser_expression (parser);
9001 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9002 expr = ce.value;
9003 expr = c_fully_fold (expr, false, NULL);
9004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9006 else
9007 expr = error_mark_node;
9008 stmt = c_parser_compound_statement (parser);
9009 objc_build_synchronized (loc, expr, stmt);
9012 /* Parse an objc-selector; return NULL_TREE without an error if the
9013 next token is not an objc-selector.
9015 objc-selector:
9016 identifier
9017 one of
9018 enum struct union if else while do for switch case default
9019 break continue return goto asm sizeof typeof __alignof
9020 unsigned long const short volatile signed restrict _Complex
9021 in out inout bycopy byref oneway int char float double void _Bool
9022 _Atomic
9024 ??? Why this selection of keywords but not, for example, storage
9025 class specifiers? */
9027 static tree
9028 c_parser_objc_selector (c_parser *parser)
9030 c_token *token = c_parser_peek_token (parser);
9031 tree value = token->value;
9032 if (token->type == CPP_NAME)
9034 c_parser_consume_token (parser);
9035 return value;
9037 if (token->type != CPP_KEYWORD)
9038 return NULL_TREE;
9039 switch (token->keyword)
9041 case RID_ENUM:
9042 case RID_STRUCT:
9043 case RID_UNION:
9044 case RID_IF:
9045 case RID_ELSE:
9046 case RID_WHILE:
9047 case RID_DO:
9048 case RID_FOR:
9049 case RID_SWITCH:
9050 case RID_CASE:
9051 case RID_DEFAULT:
9052 case RID_BREAK:
9053 case RID_CONTINUE:
9054 case RID_RETURN:
9055 case RID_GOTO:
9056 case RID_ASM:
9057 case RID_SIZEOF:
9058 case RID_TYPEOF:
9059 case RID_ALIGNOF:
9060 case RID_UNSIGNED:
9061 case RID_LONG:
9062 case RID_CONST:
9063 case RID_SHORT:
9064 case RID_VOLATILE:
9065 case RID_SIGNED:
9066 case RID_RESTRICT:
9067 case RID_COMPLEX:
9068 case RID_IN:
9069 case RID_OUT:
9070 case RID_INOUT:
9071 case RID_BYCOPY:
9072 case RID_BYREF:
9073 case RID_ONEWAY:
9074 case RID_INT:
9075 case RID_CHAR:
9076 case RID_FLOAT:
9077 case RID_DOUBLE:
9078 case RID_VOID:
9079 case RID_BOOL:
9080 case RID_ATOMIC:
9081 case RID_AUTO_TYPE:
9082 case RID_INT_N_0:
9083 case RID_INT_N_1:
9084 case RID_INT_N_2:
9085 case RID_INT_N_3:
9086 c_parser_consume_token (parser);
9087 return value;
9088 default:
9089 return NULL_TREE;
9093 /* Parse an objc-selector-arg.
9095 objc-selector-arg:
9096 objc-selector
9097 objc-keywordname-list
9099 objc-keywordname-list:
9100 objc-keywordname
9101 objc-keywordname-list objc-keywordname
9103 objc-keywordname:
9104 objc-selector :
9108 static tree
9109 c_parser_objc_selector_arg (c_parser *parser)
9111 tree sel = c_parser_objc_selector (parser);
9112 tree list = NULL_TREE;
9113 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9114 return sel;
9115 while (true)
9117 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9118 return list;
9119 list = chainon (list, build_tree_list (sel, NULL_TREE));
9120 sel = c_parser_objc_selector (parser);
9121 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9122 break;
9124 return list;
9127 /* Parse an objc-receiver.
9129 objc-receiver:
9130 expression
9131 class-name
9132 type-name
9135 static tree
9136 c_parser_objc_receiver (c_parser *parser)
9138 location_t loc = c_parser_peek_token (parser)->location;
9140 if (c_parser_peek_token (parser)->type == CPP_NAME
9141 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9142 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9144 tree id = c_parser_peek_token (parser)->value;
9145 c_parser_consume_token (parser);
9146 return objc_get_class_reference (id);
9148 struct c_expr ce = c_parser_expression (parser);
9149 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9150 return c_fully_fold (ce.value, false, NULL);
9153 /* Parse objc-message-args.
9155 objc-message-args:
9156 objc-selector
9157 objc-keywordarg-list
9159 objc-keywordarg-list:
9160 objc-keywordarg
9161 objc-keywordarg-list objc-keywordarg
9163 objc-keywordarg:
9164 objc-selector : objc-keywordexpr
9165 : objc-keywordexpr
9168 static tree
9169 c_parser_objc_message_args (c_parser *parser)
9171 tree sel = c_parser_objc_selector (parser);
9172 tree list = NULL_TREE;
9173 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9174 return sel;
9175 while (true)
9177 tree keywordexpr;
9178 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9179 return error_mark_node;
9180 keywordexpr = c_parser_objc_keywordexpr (parser);
9181 list = chainon (list, build_tree_list (sel, keywordexpr));
9182 sel = c_parser_objc_selector (parser);
9183 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9184 break;
9186 return list;
9189 /* Parse an objc-keywordexpr.
9191 objc-keywordexpr:
9192 nonempty-expr-list
9195 static tree
9196 c_parser_objc_keywordexpr (c_parser *parser)
9198 tree ret;
9199 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9200 NULL, NULL, NULL, NULL);
9201 if (vec_safe_length (expr_list) == 1)
9203 /* Just return the expression, remove a level of
9204 indirection. */
9205 ret = (*expr_list)[0];
9207 else
9209 /* We have a comma expression, we will collapse later. */
9210 ret = build_tree_list_vec (expr_list);
9212 release_tree_vector (expr_list);
9213 return ret;
9216 /* A check, needed in several places, that ObjC interface, implementation or
9217 method definitions are not prefixed by incorrect items. */
9218 static bool
9219 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9220 struct c_declspecs *specs)
9222 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9223 || specs->typespec_kind != ctsk_none)
9225 c_parser_error (parser,
9226 "no type or storage class may be specified here,");
9227 c_parser_skip_to_end_of_block_or_statement (parser);
9228 return true;
9230 return false;
9233 /* Parse an Objective-C @property declaration. The syntax is:
9235 objc-property-declaration:
9236 '@property' objc-property-attributes[opt] struct-declaration ;
9238 objc-property-attributes:
9239 '(' objc-property-attribute-list ')'
9241 objc-property-attribute-list:
9242 objc-property-attribute
9243 objc-property-attribute-list, objc-property-attribute
9245 objc-property-attribute
9246 'getter' = identifier
9247 'setter' = identifier
9248 'readonly'
9249 'readwrite'
9250 'assign'
9251 'retain'
9252 'copy'
9253 'nonatomic'
9255 For example:
9256 @property NSString *name;
9257 @property (readonly) id object;
9258 @property (retain, nonatomic, getter=getTheName) id name;
9259 @property int a, b, c;
9261 PS: This function is identical to cp_parser_objc_at_propery_declaration
9262 for C++. Keep them in sync. */
9263 static void
9264 c_parser_objc_at_property_declaration (c_parser *parser)
9266 /* The following variables hold the attributes of the properties as
9267 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9268 seen. When we see an attribute, we set them to 'true' (if they
9269 are boolean properties) or to the identifier (if they have an
9270 argument, ie, for getter and setter). Note that here we only
9271 parse the list of attributes, check the syntax and accumulate the
9272 attributes that we find. objc_add_property_declaration() will
9273 then process the information. */
9274 bool property_assign = false;
9275 bool property_copy = false;
9276 tree property_getter_ident = NULL_TREE;
9277 bool property_nonatomic = false;
9278 bool property_readonly = false;
9279 bool property_readwrite = false;
9280 bool property_retain = false;
9281 tree property_setter_ident = NULL_TREE;
9283 /* 'properties' is the list of properties that we read. Usually a
9284 single one, but maybe more (eg, in "@property int a, b, c;" there
9285 are three). */
9286 tree properties;
9287 location_t loc;
9289 loc = c_parser_peek_token (parser)->location;
9290 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9292 c_parser_consume_token (parser); /* Eat '@property'. */
9294 /* Parse the optional attribute list... */
9295 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9297 /* Eat the '(' */
9298 c_parser_consume_token (parser);
9300 /* Property attribute keywords are valid now. */
9301 parser->objc_property_attr_context = true;
9303 while (true)
9305 bool syntax_error = false;
9306 c_token *token = c_parser_peek_token (parser);
9307 enum rid keyword;
9309 if (token->type != CPP_KEYWORD)
9311 if (token->type == CPP_CLOSE_PAREN)
9312 c_parser_error (parser, "expected identifier");
9313 else
9315 c_parser_consume_token (parser);
9316 c_parser_error (parser, "unknown property attribute");
9318 break;
9320 keyword = token->keyword;
9321 c_parser_consume_token (parser);
9322 switch (keyword)
9324 case RID_ASSIGN: property_assign = true; break;
9325 case RID_COPY: property_copy = true; break;
9326 case RID_NONATOMIC: property_nonatomic = true; break;
9327 case RID_READONLY: property_readonly = true; break;
9328 case RID_READWRITE: property_readwrite = true; break;
9329 case RID_RETAIN: property_retain = true; break;
9331 case RID_GETTER:
9332 case RID_SETTER:
9333 if (c_parser_next_token_is_not (parser, CPP_EQ))
9335 if (keyword == RID_GETTER)
9336 c_parser_error (parser,
9337 "missing %<=%> (after %<getter%> attribute)");
9338 else
9339 c_parser_error (parser,
9340 "missing %<=%> (after %<setter%> attribute)");
9341 syntax_error = true;
9342 break;
9344 c_parser_consume_token (parser); /* eat the = */
9345 if (c_parser_next_token_is_not (parser, CPP_NAME))
9347 c_parser_error (parser, "expected identifier");
9348 syntax_error = true;
9349 break;
9351 if (keyword == RID_SETTER)
9353 if (property_setter_ident != NULL_TREE)
9354 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9355 else
9356 property_setter_ident = c_parser_peek_token (parser)->value;
9357 c_parser_consume_token (parser);
9358 if (c_parser_next_token_is_not (parser, CPP_COLON))
9359 c_parser_error (parser, "setter name must terminate with %<:%>");
9360 else
9361 c_parser_consume_token (parser);
9363 else
9365 if (property_getter_ident != NULL_TREE)
9366 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9367 else
9368 property_getter_ident = c_parser_peek_token (parser)->value;
9369 c_parser_consume_token (parser);
9371 break;
9372 default:
9373 c_parser_error (parser, "unknown property attribute");
9374 syntax_error = true;
9375 break;
9378 if (syntax_error)
9379 break;
9381 if (c_parser_next_token_is (parser, CPP_COMMA))
9382 c_parser_consume_token (parser);
9383 else
9384 break;
9386 parser->objc_property_attr_context = false;
9387 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9389 /* ... and the property declaration(s). */
9390 properties = c_parser_struct_declaration (parser);
9392 if (properties == error_mark_node)
9394 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9395 parser->error = false;
9396 return;
9399 if (properties == NULL_TREE)
9400 c_parser_error (parser, "expected identifier");
9401 else
9403 /* Comma-separated properties are chained together in
9404 reverse order; add them one by one. */
9405 properties = nreverse (properties);
9407 for (; properties; properties = TREE_CHAIN (properties))
9408 objc_add_property_declaration (loc, copy_node (properties),
9409 property_readonly, property_readwrite,
9410 property_assign, property_retain,
9411 property_copy, property_nonatomic,
9412 property_getter_ident, property_setter_ident);
9415 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9416 parser->error = false;
9419 /* Parse an Objective-C @synthesize declaration. The syntax is:
9421 objc-synthesize-declaration:
9422 @synthesize objc-synthesize-identifier-list ;
9424 objc-synthesize-identifier-list:
9425 objc-synthesize-identifier
9426 objc-synthesize-identifier-list, objc-synthesize-identifier
9428 objc-synthesize-identifier
9429 identifier
9430 identifier = identifier
9432 For example:
9433 @synthesize MyProperty;
9434 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9436 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9437 for C++. Keep them in sync.
9439 static void
9440 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9442 tree list = NULL_TREE;
9443 location_t loc;
9444 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9445 loc = c_parser_peek_token (parser)->location;
9447 c_parser_consume_token (parser);
9448 while (true)
9450 tree property, ivar;
9451 if (c_parser_next_token_is_not (parser, CPP_NAME))
9453 c_parser_error (parser, "expected identifier");
9454 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9455 /* Once we find the semicolon, we can resume normal parsing.
9456 We have to reset parser->error manually because
9457 c_parser_skip_until_found() won't reset it for us if the
9458 next token is precisely a semicolon. */
9459 parser->error = false;
9460 return;
9462 property = c_parser_peek_token (parser)->value;
9463 c_parser_consume_token (parser);
9464 if (c_parser_next_token_is (parser, CPP_EQ))
9466 c_parser_consume_token (parser);
9467 if (c_parser_next_token_is_not (parser, CPP_NAME))
9469 c_parser_error (parser, "expected identifier");
9470 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9471 parser->error = false;
9472 return;
9474 ivar = c_parser_peek_token (parser)->value;
9475 c_parser_consume_token (parser);
9477 else
9478 ivar = NULL_TREE;
9479 list = chainon (list, build_tree_list (ivar, property));
9480 if (c_parser_next_token_is (parser, CPP_COMMA))
9481 c_parser_consume_token (parser);
9482 else
9483 break;
9485 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9486 objc_add_synthesize_declaration (loc, list);
9489 /* Parse an Objective-C @dynamic declaration. The syntax is:
9491 objc-dynamic-declaration:
9492 @dynamic identifier-list ;
9494 For example:
9495 @dynamic MyProperty;
9496 @dynamic MyProperty, AnotherProperty;
9498 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9499 for C++. Keep them in sync.
9501 static void
9502 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9504 tree list = NULL_TREE;
9505 location_t loc;
9506 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9507 loc = c_parser_peek_token (parser)->location;
9509 c_parser_consume_token (parser);
9510 while (true)
9512 tree property;
9513 if (c_parser_next_token_is_not (parser, CPP_NAME))
9515 c_parser_error (parser, "expected identifier");
9516 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9517 parser->error = false;
9518 return;
9520 property = c_parser_peek_token (parser)->value;
9521 list = chainon (list, build_tree_list (NULL_TREE, property));
9522 c_parser_consume_token (parser);
9523 if (c_parser_next_token_is (parser, CPP_COMMA))
9524 c_parser_consume_token (parser);
9525 else
9526 break;
9528 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9529 objc_add_dynamic_declaration (loc, list);
9533 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9534 should be considered, statements. ALLOW_STMT is true if we're within
9535 the context of a function and such pragmas are to be allowed. Returns
9536 true if we actually parsed such a pragma. */
9538 static bool
9539 c_parser_pragma (c_parser *parser, enum pragma_context context)
9541 unsigned int id;
9543 id = c_parser_peek_token (parser)->pragma_kind;
9544 gcc_assert (id != PRAGMA_NONE);
9546 switch (id)
9548 case PRAGMA_OACC_ENTER_DATA:
9549 c_parser_oacc_enter_exit_data (parser, true);
9550 return false;
9552 case PRAGMA_OACC_EXIT_DATA:
9553 c_parser_oacc_enter_exit_data (parser, false);
9554 return false;
9556 case PRAGMA_OACC_UPDATE:
9557 if (context != pragma_compound)
9559 if (context == pragma_stmt)
9560 c_parser_error (parser, "%<#pragma acc update%> may only be "
9561 "used in compound statements");
9562 goto bad_stmt;
9564 c_parser_oacc_update (parser);
9565 return false;
9567 case PRAGMA_OMP_BARRIER:
9568 if (context != pragma_compound)
9570 if (context == pragma_stmt)
9571 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9572 "used in compound statements");
9573 goto bad_stmt;
9575 c_parser_omp_barrier (parser);
9576 return false;
9578 case PRAGMA_OMP_FLUSH:
9579 if (context != pragma_compound)
9581 if (context == pragma_stmt)
9582 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9583 "used in compound statements");
9584 goto bad_stmt;
9586 c_parser_omp_flush (parser);
9587 return false;
9589 case PRAGMA_OMP_TASKWAIT:
9590 if (context != pragma_compound)
9592 if (context == pragma_stmt)
9593 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9594 "used in compound statements");
9595 goto bad_stmt;
9597 c_parser_omp_taskwait (parser);
9598 return false;
9600 case PRAGMA_OMP_TASKYIELD:
9601 if (context != pragma_compound)
9603 if (context == pragma_stmt)
9604 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9605 "used in compound statements");
9606 goto bad_stmt;
9608 c_parser_omp_taskyield (parser);
9609 return false;
9611 case PRAGMA_OMP_CANCEL:
9612 if (context != pragma_compound)
9614 if (context == pragma_stmt)
9615 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9616 "used in compound statements");
9617 goto bad_stmt;
9619 c_parser_omp_cancel (parser);
9620 return false;
9622 case PRAGMA_OMP_CANCELLATION_POINT:
9623 if (context != pragma_compound)
9625 if (context == pragma_stmt)
9626 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9627 "only be used in compound statements");
9628 goto bad_stmt;
9630 c_parser_omp_cancellation_point (parser);
9631 return false;
9633 case PRAGMA_OMP_THREADPRIVATE:
9634 c_parser_omp_threadprivate (parser);
9635 return false;
9637 case PRAGMA_OMP_TARGET:
9638 return c_parser_omp_target (parser, context);
9640 case PRAGMA_OMP_END_DECLARE_TARGET:
9641 c_parser_omp_end_declare_target (parser);
9642 return false;
9644 case PRAGMA_OMP_SECTION:
9645 error_at (c_parser_peek_token (parser)->location,
9646 "%<#pragma omp section%> may only be used in "
9647 "%<#pragma omp sections%> construct");
9648 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9649 return false;
9651 case PRAGMA_OMP_DECLARE_REDUCTION:
9652 c_parser_omp_declare (parser, context);
9653 return false;
9654 case PRAGMA_IVDEP:
9655 c_parser_consume_pragma (parser);
9656 c_parser_skip_to_pragma_eol (parser);
9657 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9658 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9659 && !c_parser_next_token_is_keyword (parser, RID_DO))
9661 c_parser_error (parser, "for, while or do statement expected");
9662 return false;
9664 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9665 c_parser_for_statement (parser, true);
9666 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9667 c_parser_while_statement (parser, true);
9668 else
9669 c_parser_do_statement (parser, true);
9670 return false;
9672 case PRAGMA_GCC_PCH_PREPROCESS:
9673 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9674 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9675 return false;
9677 case PRAGMA_CILK_SIMD:
9678 if (!c_parser_cilk_verify_simd (parser, context))
9679 return false;
9680 c_parser_consume_pragma (parser);
9681 c_parser_cilk_simd (parser);
9682 return false;
9683 case PRAGMA_CILK_GRAINSIZE:
9684 if (!flag_cilkplus)
9686 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9687 " enabled");
9688 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9689 return false;
9691 if (context == pragma_external)
9693 error_at (c_parser_peek_token (parser)->location,
9694 "%<#pragma grainsize%> must be inside a function");
9695 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9696 return false;
9698 c_parser_cilk_grainsize (parser);
9699 return false;
9701 default:
9702 if (id < PRAGMA_FIRST_EXTERNAL)
9704 if (context != pragma_stmt && context != pragma_compound)
9706 bad_stmt:
9707 c_parser_error (parser, "expected declaration specifiers");
9708 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9709 return false;
9711 c_parser_omp_construct (parser);
9712 return true;
9714 break;
9717 c_parser_consume_pragma (parser);
9718 c_invoke_pragma_handler (id);
9720 /* Skip to EOL, but suppress any error message. Those will have been
9721 generated by the handler routine through calling error, as opposed
9722 to calling c_parser_error. */
9723 parser->error = true;
9724 c_parser_skip_to_pragma_eol (parser);
9726 return false;
9729 /* The interface the pragma parsers have to the lexer. */
9731 enum cpp_ttype
9732 pragma_lex (tree *value)
9734 c_token *tok = c_parser_peek_token (the_parser);
9735 enum cpp_ttype ret = tok->type;
9737 *value = tok->value;
9738 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9739 ret = CPP_EOF;
9740 else
9742 if (ret == CPP_KEYWORD)
9743 ret = CPP_NAME;
9744 c_parser_consume_token (the_parser);
9747 return ret;
9750 static void
9751 c_parser_pragma_pch_preprocess (c_parser *parser)
9753 tree name = NULL;
9755 c_parser_consume_pragma (parser);
9756 if (c_parser_next_token_is (parser, CPP_STRING))
9758 name = c_parser_peek_token (parser)->value;
9759 c_parser_consume_token (parser);
9761 else
9762 c_parser_error (parser, "expected string literal");
9763 c_parser_skip_to_pragma_eol (parser);
9765 if (name)
9766 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9769 /* OpenACC and OpenMP parsing routines. */
9771 /* Returns name of the next clause.
9772 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9773 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9774 returned and the token is consumed. */
9776 static pragma_omp_clause
9777 c_parser_omp_clause_name (c_parser *parser)
9779 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9781 if (c_parser_next_token_is_keyword (parser, RID_IF))
9782 result = PRAGMA_OMP_CLAUSE_IF;
9783 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9784 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9785 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9786 result = PRAGMA_OMP_CLAUSE_FOR;
9787 else if (c_parser_next_token_is (parser, CPP_NAME))
9789 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9791 switch (p[0])
9793 case 'a':
9794 if (!strcmp ("aligned", p))
9795 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9796 else if (!strcmp ("async", p))
9797 result = PRAGMA_OMP_CLAUSE_ASYNC;
9798 break;
9799 case 'c':
9800 if (!strcmp ("collapse", p))
9801 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9802 else if (!strcmp ("copy", p))
9803 result = PRAGMA_OMP_CLAUSE_COPY;
9804 else if (!strcmp ("copyin", p))
9805 result = PRAGMA_OMP_CLAUSE_COPYIN;
9806 else if (!strcmp ("copyout", p))
9807 result = PRAGMA_OMP_CLAUSE_COPYOUT;
9808 else if (!strcmp ("copyprivate", p))
9809 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9810 else if (!strcmp ("create", p))
9811 result = PRAGMA_OMP_CLAUSE_CREATE;
9812 break;
9813 case 'd':
9814 if (!strcmp ("delete", p))
9815 result = PRAGMA_OMP_CLAUSE_DELETE;
9816 else if (!strcmp ("depend", p))
9817 result = PRAGMA_OMP_CLAUSE_DEPEND;
9818 else if (!strcmp ("device", p))
9819 result = PRAGMA_OMP_CLAUSE_DEVICE;
9820 else if (!strcmp ("deviceptr", p))
9821 result = PRAGMA_OMP_CLAUSE_DEVICEPTR;
9822 else if (!strcmp ("dist_schedule", p))
9823 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9824 break;
9825 case 'f':
9826 if (!strcmp ("final", p))
9827 result = PRAGMA_OMP_CLAUSE_FINAL;
9828 else if (!strcmp ("firstprivate", p))
9829 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9830 else if (!strcmp ("from", p))
9831 result = PRAGMA_OMP_CLAUSE_FROM;
9832 break;
9833 case 'h':
9834 if (!strcmp ("host", p))
9835 result = PRAGMA_OMP_CLAUSE_SELF;
9836 break;
9837 case 'i':
9838 if (!strcmp ("inbranch", p))
9839 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9840 break;
9841 case 'l':
9842 if (!strcmp ("lastprivate", p))
9843 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9844 else if (!strcmp ("linear", p))
9845 result = PRAGMA_OMP_CLAUSE_LINEAR;
9846 break;
9847 case 'm':
9848 if (!strcmp ("map", p))
9849 result = PRAGMA_OMP_CLAUSE_MAP;
9850 else if (!strcmp ("mergeable", p))
9851 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9852 else if (flag_cilkplus && !strcmp ("mask", p))
9853 result = PRAGMA_CILK_CLAUSE_MASK;
9854 break;
9855 case 'n':
9856 if (!strcmp ("notinbranch", p))
9857 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9858 else if (!strcmp ("nowait", p))
9859 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9860 else if (!strcmp ("num_gangs", p))
9861 result = PRAGMA_OMP_CLAUSE_NUM_GANGS;
9862 else if (!strcmp ("num_teams", p))
9863 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9864 else if (!strcmp ("num_threads", p))
9865 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9866 else if (!strcmp ("num_workers", p))
9867 result = PRAGMA_OMP_CLAUSE_NUM_WORKERS;
9868 else if (flag_cilkplus && !strcmp ("nomask", p))
9869 result = PRAGMA_CILK_CLAUSE_NOMASK;
9870 break;
9871 case 'o':
9872 if (!strcmp ("ordered", p))
9873 result = PRAGMA_OMP_CLAUSE_ORDERED;
9874 break;
9875 case 'p':
9876 if (!strcmp ("parallel", p))
9877 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9878 else if (!strcmp ("present", p))
9879 result = PRAGMA_OMP_CLAUSE_PRESENT;
9880 else if (!strcmp ("present_or_copy", p)
9881 || !strcmp ("pcopy", p))
9882 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY;
9883 else if (!strcmp ("present_or_copyin", p)
9884 || !strcmp ("pcopyin", p))
9885 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN;
9886 else if (!strcmp ("present_or_copyout", p)
9887 || !strcmp ("pcopyout", p))
9888 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT;
9889 else if (!strcmp ("present_or_create", p)
9890 || !strcmp ("pcreate", p))
9891 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE;
9892 else if (!strcmp ("private", p))
9893 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9894 else if (!strcmp ("proc_bind", p))
9895 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9896 break;
9897 case 'r':
9898 if (!strcmp ("reduction", p))
9899 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9900 break;
9901 case 's':
9902 if (!strcmp ("safelen", p))
9903 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9904 else if (!strcmp ("schedule", p))
9905 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9906 else if (!strcmp ("sections", p))
9907 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9908 else if (!strcmp ("shared", p))
9909 result = PRAGMA_OMP_CLAUSE_SHARED;
9910 else if (!strcmp ("simdlen", p))
9911 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9912 else if (!strcmp ("self", p))
9913 result = PRAGMA_OMP_CLAUSE_SELF;
9914 break;
9915 case 't':
9916 if (!strcmp ("taskgroup", p))
9917 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9918 else if (!strcmp ("thread_limit", p))
9919 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9920 else if (!strcmp ("to", p))
9921 result = PRAGMA_OMP_CLAUSE_TO;
9922 break;
9923 case 'u':
9924 if (!strcmp ("uniform", p))
9925 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9926 else if (!strcmp ("untied", p))
9927 result = PRAGMA_OMP_CLAUSE_UNTIED;
9928 break;
9929 case 'v':
9930 if (!strcmp ("vector_length", p))
9931 result = PRAGMA_OMP_CLAUSE_VECTOR_LENGTH;
9932 else if (flag_cilkplus && !strcmp ("vectorlength", p))
9933 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9934 break;
9935 case 'w':
9936 if (!strcmp ("wait", p))
9937 result = PRAGMA_OMP_CLAUSE_WAIT;
9938 break;
9942 if (result != PRAGMA_OMP_CLAUSE_NONE)
9943 c_parser_consume_token (parser);
9945 return result;
9948 /* Validate that a clause of the given type does not already exist. */
9950 static void
9951 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9952 const char *name)
9954 tree c;
9956 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9957 if (OMP_CLAUSE_CODE (c) == code)
9959 location_t loc = OMP_CLAUSE_LOCATION (c);
9960 error_at (loc, "too many %qs clauses", name);
9961 break;
9965 /* OpenACC 2.0
9966 Parse wait clause or wait directive parameters. */
9968 static tree
9969 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
9971 vec<tree, va_gc> *args;
9972 tree t, args_tree;
9974 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9975 return list;
9977 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
9979 if (args->length () == 0)
9981 c_parser_error (parser, "expected integer expression before ')'");
9982 release_tree_vector (args);
9983 return list;
9986 args_tree = build_tree_list_vec (args);
9988 for (t = args_tree; t; t = TREE_CHAIN (t))
9990 tree targ = TREE_VALUE (t);
9992 if (targ != error_mark_node)
9994 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
9996 c_parser_error (parser, "expression must be integral");
9997 targ = error_mark_node;
9999 else
10001 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10003 OMP_CLAUSE_DECL (c) = targ;
10004 OMP_CLAUSE_CHAIN (c) = list;
10005 list = c;
10010 release_tree_vector (args);
10011 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10012 return list;
10015 /* OpenACC 2.0, OpenMP 2.5:
10016 variable-list:
10017 identifier
10018 variable-list , identifier
10020 If KIND is nonzero, create the appropriate node and install the
10021 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10022 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10024 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10025 return the list created. */
10027 static tree
10028 c_parser_omp_variable_list (c_parser *parser,
10029 location_t clause_loc,
10030 enum omp_clause_code kind, tree list)
10032 if (c_parser_next_token_is_not (parser, CPP_NAME)
10033 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10034 c_parser_error (parser, "expected identifier");
10036 while (c_parser_next_token_is (parser, CPP_NAME)
10037 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10039 tree t = lookup_name (c_parser_peek_token (parser)->value);
10041 if (t == NULL_TREE)
10043 undeclared_variable (c_parser_peek_token (parser)->location,
10044 c_parser_peek_token (parser)->value);
10045 t = error_mark_node;
10048 c_parser_consume_token (parser);
10050 if (t == error_mark_node)
10052 else if (kind != 0)
10054 switch (kind)
10056 case OMP_CLAUSE__CACHE_:
10057 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10059 c_parser_error (parser, "expected %<[%>");
10060 t = error_mark_node;
10061 break;
10063 /* FALL THROUGH. */
10064 case OMP_CLAUSE_MAP:
10065 case OMP_CLAUSE_FROM:
10066 case OMP_CLAUSE_TO:
10067 case OMP_CLAUSE_DEPEND:
10068 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10070 tree low_bound = NULL_TREE, length = NULL_TREE;
10072 c_parser_consume_token (parser);
10073 if (!c_parser_next_token_is (parser, CPP_COLON))
10075 low_bound = c_parser_expression (parser).value;
10076 mark_exp_read (low_bound);
10078 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10079 length = integer_one_node;
10080 else
10082 /* Look for `:'. */
10083 if (!c_parser_require (parser, CPP_COLON,
10084 "expected %<:%>"))
10086 t = error_mark_node;
10087 break;
10089 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10091 length = c_parser_expression (parser).value;
10092 mark_exp_read (length);
10095 /* Look for the closing `]'. */
10096 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10097 "expected %<]%>"))
10099 t = error_mark_node;
10100 break;
10103 if (kind == OMP_CLAUSE__CACHE_)
10105 if (TREE_CODE (low_bound) != INTEGER_CST
10106 && !TREE_READONLY (low_bound))
10108 error_at (clause_loc,
10109 "%qD is not a constant", low_bound);
10110 t = error_mark_node;
10113 if (TREE_CODE (length) != INTEGER_CST
10114 && !TREE_READONLY (length))
10116 error_at (clause_loc,
10117 "%qD is not a constant", length);
10118 t = error_mark_node;
10122 t = tree_cons (low_bound, length, t);
10124 break;
10125 default:
10126 break;
10129 if (t != error_mark_node)
10131 tree u = build_omp_clause (clause_loc, kind);
10132 OMP_CLAUSE_DECL (u) = t;
10133 OMP_CLAUSE_CHAIN (u) = list;
10134 list = u;
10137 else
10138 list = tree_cons (t, NULL_TREE, list);
10140 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10141 break;
10143 c_parser_consume_token (parser);
10146 return list;
10149 /* Similarly, but expect leading and trailing parenthesis. This is a very
10150 common case for OpenACC and OpenMP clauses. */
10152 static tree
10153 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10154 tree list)
10156 /* The clauses location. */
10157 location_t loc = c_parser_peek_token (parser)->location;
10159 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10161 list = c_parser_omp_variable_list (parser, loc, kind, list);
10162 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10164 return list;
10167 /* OpenACC 2.0:
10168 copy ( variable-list )
10169 copyin ( variable-list )
10170 copyout ( variable-list )
10171 create ( variable-list )
10172 delete ( variable-list )
10173 present ( variable-list )
10174 present_or_copy ( variable-list )
10175 pcopy ( variable-list )
10176 present_or_copyin ( variable-list )
10177 pcopyin ( variable-list )
10178 present_or_copyout ( variable-list )
10179 pcopyout ( variable-list )
10180 present_or_create ( variable-list )
10181 pcreate ( variable-list ) */
10183 static tree
10184 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10185 tree list)
10187 enum omp_clause_map_kind kind;
10188 switch (c_kind)
10190 default:
10191 gcc_unreachable ();
10192 case PRAGMA_OMP_CLAUSE_COPY:
10193 kind = OMP_CLAUSE_MAP_FORCE_TOFROM;
10194 break;
10195 case PRAGMA_OMP_CLAUSE_COPYIN:
10196 kind = OMP_CLAUSE_MAP_FORCE_TO;
10197 break;
10198 case PRAGMA_OMP_CLAUSE_COPYOUT:
10199 kind = OMP_CLAUSE_MAP_FORCE_FROM;
10200 break;
10201 case PRAGMA_OMP_CLAUSE_CREATE:
10202 kind = OMP_CLAUSE_MAP_FORCE_ALLOC;
10203 break;
10204 case PRAGMA_OMP_CLAUSE_DELETE:
10205 kind = OMP_CLAUSE_MAP_FORCE_DEALLOC;
10206 break;
10207 case PRAGMA_OMP_CLAUSE_DEVICE:
10208 kind = OMP_CLAUSE_MAP_FORCE_TO;
10209 break;
10210 case PRAGMA_OMP_CLAUSE_HOST:
10211 kind = OMP_CLAUSE_MAP_FORCE_FROM;
10212 break;
10213 case PRAGMA_OMP_CLAUSE_PRESENT:
10214 kind = OMP_CLAUSE_MAP_FORCE_PRESENT;
10215 break;
10216 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
10217 kind = OMP_CLAUSE_MAP_TOFROM;
10218 break;
10219 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
10220 kind = OMP_CLAUSE_MAP_TO;
10221 break;
10222 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
10223 kind = OMP_CLAUSE_MAP_FROM;
10224 break;
10225 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
10226 kind = OMP_CLAUSE_MAP_ALLOC;
10227 break;
10228 case PRAGMA_OMP_CLAUSE_SELF:
10229 kind = OMP_CLAUSE_MAP_FORCE_FROM;
10230 break;
10232 tree nl, c;
10233 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10235 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10236 OMP_CLAUSE_MAP_KIND (c) = kind;
10238 return nl;
10241 /* OpenACC 2.0:
10242 deviceptr ( variable-list ) */
10244 static tree
10245 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10247 location_t loc = c_parser_peek_token (parser)->location;
10248 tree vars, t;
10250 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10251 c_parser_oacc_data_clause), as for PRAGMA_OMP_CLAUSE_DEVICEPTR,
10252 variable-list must only allow for pointer variables. */
10253 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10254 for (t = vars; t && t; t = TREE_CHAIN (t))
10256 tree v = TREE_PURPOSE (t);
10258 /* FIXME diagnostics: Ideally we should keep individual
10259 locations for all the variables in the var list to make the
10260 following errors more precise. Perhaps
10261 c_parser_omp_var_list_parens() should construct a list of
10262 locations to go along with the var list. */
10264 if (TREE_CODE (v) != VAR_DECL)
10265 error_at (loc, "%qD is not a variable", v);
10266 else if (TREE_TYPE (v) == error_mark_node)
10268 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10269 error_at (loc, "%qD is not a pointer variable", v);
10271 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10272 OMP_CLAUSE_MAP_KIND (u) = OMP_CLAUSE_MAP_FORCE_DEVICEPTR;
10273 OMP_CLAUSE_DECL (u) = v;
10274 OMP_CLAUSE_CHAIN (u) = list;
10275 list = u;
10278 return list;
10281 /* OpenACC 2.0, OpenMP 3.0:
10282 collapse ( constant-expression ) */
10284 static tree
10285 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10287 tree c, num = error_mark_node;
10288 HOST_WIDE_INT n;
10289 location_t loc;
10291 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10293 loc = c_parser_peek_token (parser)->location;
10294 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10296 num = c_parser_expr_no_commas (parser, NULL).value;
10297 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10299 if (num == error_mark_node)
10300 return list;
10301 mark_exp_read (num);
10302 num = c_fully_fold (num, false, NULL);
10303 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10304 || !tree_fits_shwi_p (num)
10305 || (n = tree_to_shwi (num)) <= 0
10306 || (int) n != n)
10308 error_at (loc,
10309 "collapse argument needs positive constant integer expression");
10310 return list;
10312 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10313 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10314 OMP_CLAUSE_CHAIN (c) = list;
10315 return c;
10318 /* OpenMP 2.5:
10319 copyin ( variable-list ) */
10321 static tree
10322 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10324 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10327 /* OpenMP 2.5:
10328 copyprivate ( variable-list ) */
10330 static tree
10331 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10333 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10336 /* OpenMP 2.5:
10337 default ( shared | none ) */
10339 static tree
10340 c_parser_omp_clause_default (c_parser *parser, tree list)
10342 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10343 location_t loc = c_parser_peek_token (parser)->location;
10344 tree c;
10346 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10347 return list;
10348 if (c_parser_next_token_is (parser, CPP_NAME))
10350 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10352 switch (p[0])
10354 case 'n':
10355 if (strcmp ("none", p) != 0)
10356 goto invalid_kind;
10357 kind = OMP_CLAUSE_DEFAULT_NONE;
10358 break;
10360 case 's':
10361 if (strcmp ("shared", p) != 0)
10362 goto invalid_kind;
10363 kind = OMP_CLAUSE_DEFAULT_SHARED;
10364 break;
10366 default:
10367 goto invalid_kind;
10370 c_parser_consume_token (parser);
10372 else
10374 invalid_kind:
10375 c_parser_error (parser, "expected %<none%> or %<shared%>");
10377 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10379 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10380 return list;
10382 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10383 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10384 OMP_CLAUSE_CHAIN (c) = list;
10385 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10387 return c;
10390 /* OpenMP 2.5:
10391 firstprivate ( variable-list ) */
10393 static tree
10394 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10396 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10399 /* OpenMP 3.1:
10400 final ( expression ) */
10402 static tree
10403 c_parser_omp_clause_final (c_parser *parser, tree list)
10405 location_t loc = c_parser_peek_token (parser)->location;
10406 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10408 tree t = c_parser_paren_condition (parser);
10409 tree c;
10411 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10413 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10414 OMP_CLAUSE_FINAL_EXPR (c) = t;
10415 OMP_CLAUSE_CHAIN (c) = list;
10416 list = c;
10418 else
10419 c_parser_error (parser, "expected %<(%>");
10421 return list;
10424 /* OpenACC, OpenMP 2.5:
10425 if ( expression ) */
10427 static tree
10428 c_parser_omp_clause_if (c_parser *parser, tree list)
10430 location_t loc = c_parser_peek_token (parser)->location;
10431 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10433 tree t = c_parser_paren_condition (parser);
10434 tree c;
10436 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10438 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10439 OMP_CLAUSE_IF_EXPR (c) = t;
10440 OMP_CLAUSE_CHAIN (c) = list;
10441 list = c;
10443 else
10444 c_parser_error (parser, "expected %<(%>");
10446 return list;
10449 /* OpenMP 2.5:
10450 lastprivate ( variable-list ) */
10452 static tree
10453 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10455 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10458 /* OpenMP 3.1:
10459 mergeable */
10461 static tree
10462 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10464 tree c;
10466 /* FIXME: Should we allow duplicates? */
10467 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10469 c = build_omp_clause (c_parser_peek_token (parser)->location,
10470 OMP_CLAUSE_MERGEABLE);
10471 OMP_CLAUSE_CHAIN (c) = list;
10473 return c;
10476 /* OpenMP 2.5:
10477 nowait */
10479 static tree
10480 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10482 tree c;
10483 location_t loc = c_parser_peek_token (parser)->location;
10485 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10487 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10488 OMP_CLAUSE_CHAIN (c) = list;
10489 return c;
10492 /* OpenACC:
10493 num_gangs ( expression ) */
10495 static tree
10496 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
10498 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
10499 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10501 location_t expr_loc = c_parser_peek_token (parser)->location;
10502 tree c, t = c_parser_expression (parser).value;
10503 mark_exp_read (t);
10504 t = c_fully_fold (t, false, NULL);
10506 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10508 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10510 c_parser_error (parser, "expected integer expression");
10511 return list;
10514 /* Attempt to statically determine when the number isn't positive. */
10515 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10516 build_int_cst (TREE_TYPE (t), 0));
10517 if (CAN_HAVE_LOCATION_P (c))
10518 SET_EXPR_LOCATION (c, expr_loc);
10519 if (c == boolean_true_node)
10521 warning_at (expr_loc, 0,
10522 "%<num_gangs%> value must be positive");
10523 t = integer_one_node;
10526 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
10528 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
10529 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
10530 OMP_CLAUSE_CHAIN (c) = list;
10531 list = c;
10534 return list;
10537 /* OpenMP 2.5:
10538 num_threads ( expression ) */
10540 static tree
10541 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10543 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10544 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10546 location_t expr_loc = c_parser_peek_token (parser)->location;
10547 tree c, t = c_parser_expression (parser).value;
10548 mark_exp_read (t);
10549 t = c_fully_fold (t, false, NULL);
10551 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10553 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10555 c_parser_error (parser, "expected integer expression");
10556 return list;
10559 /* Attempt to statically determine when the number isn't positive. */
10560 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10561 build_int_cst (TREE_TYPE (t), 0));
10562 if (CAN_HAVE_LOCATION_P (c))
10563 SET_EXPR_LOCATION (c, expr_loc);
10564 if (c == boolean_true_node)
10566 warning_at (expr_loc, 0,
10567 "%<num_threads%> value must be positive");
10568 t = integer_one_node;
10571 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10573 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10574 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10575 OMP_CLAUSE_CHAIN (c) = list;
10576 list = c;
10579 return list;
10582 /* OpenACC:
10583 num_workers ( expression ) */
10585 static tree
10586 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
10588 location_t num_workers_loc = c_parser_peek_token (parser)->location;
10589 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10591 location_t expr_loc = c_parser_peek_token (parser)->location;
10592 tree c, t = c_parser_expression (parser).value;
10593 mark_exp_read (t);
10594 t = c_fully_fold (t, false, NULL);
10596 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10598 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10600 c_parser_error (parser, "expected integer expression");
10601 return list;
10604 /* Attempt to statically determine when the number isn't positive. */
10605 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10606 build_int_cst (TREE_TYPE (t), 0));
10607 if (CAN_HAVE_LOCATION_P (c))
10608 SET_EXPR_LOCATION (c, expr_loc);
10609 if (c == boolean_true_node)
10611 warning_at (expr_loc, 0,
10612 "%<num_workers%> value must be positive");
10613 t = integer_one_node;
10616 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
10618 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
10619 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
10620 OMP_CLAUSE_CHAIN (c) = list;
10621 list = c;
10624 return list;
10627 /* OpenACC:
10628 async [( int-expr )] */
10630 static tree
10631 c_parser_oacc_clause_async (c_parser *parser, tree list)
10633 tree c, t;
10634 location_t loc = c_parser_peek_token (parser)->location;
10636 /* TODO XXX: FIX -1 (acc_async_noval). */
10637 t = build_int_cst (integer_type_node, -1);
10639 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10641 c_parser_consume_token (parser);
10643 t = c_parser_expression (parser).value;
10644 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10645 c_parser_error (parser, "expected integer expression");
10646 else if (t == error_mark_node
10647 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10648 return list;
10650 else
10652 t = c_fully_fold (t, false, NULL);
10655 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
10657 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
10658 OMP_CLAUSE_ASYNC_EXPR (c) = t;
10659 OMP_CLAUSE_CHAIN (c) = list;
10660 list = c;
10662 return list;
10665 /* OpenACC:
10666 wait ( int-expr-list ) */
10668 static tree
10669 c_parser_oacc_clause_wait (c_parser *parser, tree list)
10671 location_t clause_loc = c_parser_peek_token (parser)->location;
10673 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10674 list = c_parser_oacc_wait_list (parser, clause_loc, list);
10676 return list;
10679 /* OpenMP 2.5:
10680 ordered */
10682 static tree
10683 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10685 tree c;
10687 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10689 c = build_omp_clause (c_parser_peek_token (parser)->location,
10690 OMP_CLAUSE_ORDERED);
10691 OMP_CLAUSE_CHAIN (c) = list;
10693 return c;
10696 /* OpenMP 2.5:
10697 private ( variable-list ) */
10699 static tree
10700 c_parser_omp_clause_private (c_parser *parser, tree list)
10702 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10705 /* OpenMP 2.5:
10706 reduction ( reduction-operator : variable-list )
10708 reduction-operator:
10709 One of: + * - & ^ | && ||
10711 OpenMP 3.1:
10713 reduction-operator:
10714 One of: + * - & ^ | && || max min
10716 OpenMP 4.0:
10718 reduction-operator:
10719 One of: + * - & ^ | && ||
10720 identifier */
10722 static tree
10723 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10725 location_t clause_loc = c_parser_peek_token (parser)->location;
10726 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10728 enum tree_code code = ERROR_MARK;
10729 tree reduc_id = NULL_TREE;
10731 switch (c_parser_peek_token (parser)->type)
10733 case CPP_PLUS:
10734 code = PLUS_EXPR;
10735 break;
10736 case CPP_MULT:
10737 code = MULT_EXPR;
10738 break;
10739 case CPP_MINUS:
10740 code = MINUS_EXPR;
10741 break;
10742 case CPP_AND:
10743 code = BIT_AND_EXPR;
10744 break;
10745 case CPP_XOR:
10746 code = BIT_XOR_EXPR;
10747 break;
10748 case CPP_OR:
10749 code = BIT_IOR_EXPR;
10750 break;
10751 case CPP_AND_AND:
10752 code = TRUTH_ANDIF_EXPR;
10753 break;
10754 case CPP_OR_OR:
10755 code = TRUTH_ORIF_EXPR;
10756 break;
10757 case CPP_NAME:
10759 const char *p
10760 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10761 if (strcmp (p, "min") == 0)
10763 code = MIN_EXPR;
10764 break;
10766 if (strcmp (p, "max") == 0)
10768 code = MAX_EXPR;
10769 break;
10771 reduc_id = c_parser_peek_token (parser)->value;
10772 break;
10774 default:
10775 c_parser_error (parser,
10776 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10777 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10778 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10779 return list;
10781 c_parser_consume_token (parser);
10782 reduc_id = c_omp_reduction_id (code, reduc_id);
10783 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10785 tree nl, c;
10787 nl = c_parser_omp_variable_list (parser, clause_loc,
10788 OMP_CLAUSE_REDUCTION, list);
10789 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10791 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10792 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10793 if (code == ERROR_MARK
10794 || !(INTEGRAL_TYPE_P (type)
10795 || TREE_CODE (type) == REAL_TYPE
10796 || TREE_CODE (type) == COMPLEX_TYPE))
10797 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10798 = c_omp_reduction_lookup (reduc_id,
10799 TYPE_MAIN_VARIANT (type));
10802 list = nl;
10804 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10806 return list;
10809 /* OpenMP 2.5:
10810 schedule ( schedule-kind )
10811 schedule ( schedule-kind , expression )
10813 schedule-kind:
10814 static | dynamic | guided | runtime | auto
10817 static tree
10818 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10820 tree c, t;
10821 location_t loc = c_parser_peek_token (parser)->location;
10823 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10824 return list;
10826 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10828 if (c_parser_next_token_is (parser, CPP_NAME))
10830 tree kind = c_parser_peek_token (parser)->value;
10831 const char *p = IDENTIFIER_POINTER (kind);
10833 switch (p[0])
10835 case 'd':
10836 if (strcmp ("dynamic", p) != 0)
10837 goto invalid_kind;
10838 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10839 break;
10841 case 'g':
10842 if (strcmp ("guided", p) != 0)
10843 goto invalid_kind;
10844 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10845 break;
10847 case 'r':
10848 if (strcmp ("runtime", p) != 0)
10849 goto invalid_kind;
10850 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10851 break;
10853 default:
10854 goto invalid_kind;
10857 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10858 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10859 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10860 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10861 else
10862 goto invalid_kind;
10864 c_parser_consume_token (parser);
10865 if (c_parser_next_token_is (parser, CPP_COMMA))
10867 location_t here;
10868 c_parser_consume_token (parser);
10870 here = c_parser_peek_token (parser)->location;
10871 t = c_parser_expr_no_commas (parser, NULL).value;
10872 mark_exp_read (t);
10873 t = c_fully_fold (t, false, NULL);
10875 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10876 error_at (here, "schedule %<runtime%> does not take "
10877 "a %<chunk_size%> parameter");
10878 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10879 error_at (here,
10880 "schedule %<auto%> does not take "
10881 "a %<chunk_size%> parameter");
10882 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10883 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10884 else
10885 c_parser_error (parser, "expected integer expression");
10887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10889 else
10890 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10891 "expected %<,%> or %<)%>");
10893 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10894 OMP_CLAUSE_CHAIN (c) = list;
10895 return c;
10897 invalid_kind:
10898 c_parser_error (parser, "invalid schedule kind");
10899 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10900 return list;
10903 /* OpenMP 2.5:
10904 shared ( variable-list ) */
10906 static tree
10907 c_parser_omp_clause_shared (c_parser *parser, tree list)
10909 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10912 /* OpenMP 3.0:
10913 untied */
10915 static tree
10916 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10918 tree c;
10920 /* FIXME: Should we allow duplicates? */
10921 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10923 c = build_omp_clause (c_parser_peek_token (parser)->location,
10924 OMP_CLAUSE_UNTIED);
10925 OMP_CLAUSE_CHAIN (c) = list;
10927 return c;
10930 /* OpenACC:
10931 vector_length ( expression ) */
10933 static tree
10934 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
10936 location_t vector_length_loc = c_parser_peek_token (parser)->location;
10937 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10939 location_t expr_loc = c_parser_peek_token (parser)->location;
10940 tree c, t = c_parser_expression (parser).value;
10941 mark_exp_read (t);
10942 t = c_fully_fold (t, false, NULL);
10944 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10946 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10948 c_parser_error (parser, "expected integer expression");
10949 return list;
10952 /* Attempt to statically determine when the number isn't positive. */
10953 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10954 build_int_cst (TREE_TYPE (t), 0));
10955 if (CAN_HAVE_LOCATION_P (c))
10956 SET_EXPR_LOCATION (c, expr_loc);
10957 if (c == boolean_true_node)
10959 warning_at (expr_loc, 0,
10960 "%<vector_length%> value must be positive");
10961 t = integer_one_node;
10964 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
10966 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
10967 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
10968 OMP_CLAUSE_CHAIN (c) = list;
10969 list = c;
10972 return list;
10975 /* OpenMP 4.0:
10976 inbranch
10977 notinbranch */
10979 static tree
10980 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10981 enum omp_clause_code code, tree list)
10983 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10985 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10986 OMP_CLAUSE_CHAIN (c) = list;
10988 return c;
10991 /* OpenMP 4.0:
10992 parallel
10994 sections
10995 taskgroup */
10997 static tree
10998 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10999 enum omp_clause_code code, tree list)
11001 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11002 OMP_CLAUSE_CHAIN (c) = list;
11004 return c;
11007 /* OpenMP 4.0:
11008 num_teams ( expression ) */
11010 static tree
11011 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
11013 location_t num_teams_loc = c_parser_peek_token (parser)->location;
11014 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11016 location_t expr_loc = c_parser_peek_token (parser)->location;
11017 tree c, t = c_parser_expression (parser).value;
11018 mark_exp_read (t);
11019 t = c_fully_fold (t, false, NULL);
11021 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11023 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11025 c_parser_error (parser, "expected integer expression");
11026 return list;
11029 /* Attempt to statically determine when the number isn't positive. */
11030 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11031 build_int_cst (TREE_TYPE (t), 0));
11032 if (CAN_HAVE_LOCATION_P (c))
11033 SET_EXPR_LOCATION (c, expr_loc);
11034 if (c == boolean_true_node)
11036 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
11037 t = integer_one_node;
11040 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
11042 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11043 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
11044 OMP_CLAUSE_CHAIN (c) = list;
11045 list = c;
11048 return list;
11051 /* OpenMP 4.0:
11052 thread_limit ( expression ) */
11054 static tree
11055 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
11057 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
11058 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11060 location_t expr_loc = c_parser_peek_token (parser)->location;
11061 tree c, t = c_parser_expression (parser).value;
11062 mark_exp_read (t);
11063 t = c_fully_fold (t, false, NULL);
11065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11067 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11069 c_parser_error (parser, "expected integer expression");
11070 return list;
11073 /* Attempt to statically determine when the number isn't positive. */
11074 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11075 build_int_cst (TREE_TYPE (t), 0));
11076 if (CAN_HAVE_LOCATION_P (c))
11077 SET_EXPR_LOCATION (c, expr_loc);
11078 if (c == boolean_true_node)
11080 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
11081 t = integer_one_node;
11084 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
11085 "thread_limit");
11087 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11088 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
11089 OMP_CLAUSE_CHAIN (c) = list;
11090 list = c;
11093 return list;
11096 /* OpenMP 4.0:
11097 aligned ( variable-list )
11098 aligned ( variable-list : constant-expression ) */
11100 static tree
11101 c_parser_omp_clause_aligned (c_parser *parser, tree list)
11103 location_t clause_loc = c_parser_peek_token (parser)->location;
11104 tree nl, c;
11106 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11107 return list;
11109 nl = c_parser_omp_variable_list (parser, clause_loc,
11110 OMP_CLAUSE_ALIGNED, list);
11112 if (c_parser_next_token_is (parser, CPP_COLON))
11114 c_parser_consume_token (parser);
11115 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
11116 mark_exp_read (alignment);
11117 alignment = c_fully_fold (alignment, false, NULL);
11118 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
11119 && TREE_CODE (alignment) != INTEGER_CST
11120 && tree_int_cst_sgn (alignment) != 1)
11122 error_at (clause_loc, "%<aligned%> clause alignment expression must "
11123 "be positive constant integer expression");
11124 alignment = NULL_TREE;
11127 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11128 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
11131 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11132 return nl;
11135 /* OpenMP 4.0:
11136 linear ( variable-list )
11137 linear ( variable-list : expression ) */
11139 static tree
11140 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
11142 location_t clause_loc = c_parser_peek_token (parser)->location;
11143 tree nl, c, step;
11145 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11146 return list;
11148 nl = c_parser_omp_variable_list (parser, clause_loc,
11149 OMP_CLAUSE_LINEAR, list);
11151 if (c_parser_next_token_is (parser, CPP_COLON))
11153 c_parser_consume_token (parser);
11154 step = c_parser_expression (parser).value;
11155 mark_exp_read (step);
11156 step = c_fully_fold (step, false, NULL);
11157 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
11159 sorry ("using parameters for %<linear%> step is not supported yet");
11160 step = integer_one_node;
11162 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
11164 error_at (clause_loc, "%<linear%> clause step expression must "
11165 "be integral");
11166 step = integer_one_node;
11170 else
11171 step = integer_one_node;
11173 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11175 OMP_CLAUSE_LINEAR_STEP (c) = step;
11178 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11179 return nl;
11182 /* OpenMP 4.0:
11183 safelen ( constant-expression ) */
11185 static tree
11186 c_parser_omp_clause_safelen (c_parser *parser, tree list)
11188 location_t clause_loc = c_parser_peek_token (parser)->location;
11189 tree c, t;
11191 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11192 return list;
11194 t = c_parser_expr_no_commas (parser, NULL).value;
11195 mark_exp_read (t);
11196 t = c_fully_fold (t, false, NULL);
11197 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11198 && TREE_CODE (t) != INTEGER_CST
11199 && tree_int_cst_sgn (t) != 1)
11201 error_at (clause_loc, "%<safelen%> clause expression must "
11202 "be positive constant integer expression");
11203 t = NULL_TREE;
11206 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11207 if (t == NULL_TREE || t == error_mark_node)
11208 return list;
11210 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
11212 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
11213 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
11214 OMP_CLAUSE_CHAIN (c) = list;
11215 return c;
11218 /* OpenMP 4.0:
11219 simdlen ( constant-expression ) */
11221 static tree
11222 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
11224 location_t clause_loc = c_parser_peek_token (parser)->location;
11225 tree c, t;
11227 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11228 return list;
11230 t = c_parser_expr_no_commas (parser, NULL).value;
11231 mark_exp_read (t);
11232 t = c_fully_fold (t, false, NULL);
11233 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11234 && TREE_CODE (t) != INTEGER_CST
11235 && tree_int_cst_sgn (t) != 1)
11237 error_at (clause_loc, "%<simdlen%> clause expression must "
11238 "be positive constant integer expression");
11239 t = NULL_TREE;
11242 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11243 if (t == NULL_TREE || t == error_mark_node)
11244 return list;
11246 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
11248 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
11249 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
11250 OMP_CLAUSE_CHAIN (c) = list;
11251 return c;
11254 /* OpenMP 4.0:
11255 depend ( depend-kind: variable-list )
11257 depend-kind:
11258 in | out | inout */
11260 static tree
11261 c_parser_omp_clause_depend (c_parser *parser, tree list)
11263 location_t clause_loc = c_parser_peek_token (parser)->location;
11264 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
11265 tree nl, c;
11267 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11268 return list;
11270 if (c_parser_next_token_is (parser, CPP_NAME))
11272 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11273 if (strcmp ("in", p) == 0)
11274 kind = OMP_CLAUSE_DEPEND_IN;
11275 else if (strcmp ("inout", p) == 0)
11276 kind = OMP_CLAUSE_DEPEND_INOUT;
11277 else if (strcmp ("out", p) == 0)
11278 kind = OMP_CLAUSE_DEPEND_OUT;
11279 else
11280 goto invalid_kind;
11282 else
11283 goto invalid_kind;
11285 c_parser_consume_token (parser);
11286 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11287 goto resync_fail;
11289 nl = c_parser_omp_variable_list (parser, clause_loc,
11290 OMP_CLAUSE_DEPEND, list);
11292 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11293 OMP_CLAUSE_DEPEND_KIND (c) = kind;
11295 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11296 return nl;
11298 invalid_kind:
11299 c_parser_error (parser, "invalid depend kind");
11300 resync_fail:
11301 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11302 return list;
11305 /* OpenMP 4.0:
11306 map ( map-kind: variable-list )
11307 map ( variable-list )
11309 map-kind:
11310 alloc | to | from | tofrom */
11312 static tree
11313 c_parser_omp_clause_map (c_parser *parser, tree list)
11315 location_t clause_loc = c_parser_peek_token (parser)->location;
11316 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
11317 tree nl, c;
11319 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11320 return list;
11322 if (c_parser_next_token_is (parser, CPP_NAME)
11323 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11325 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11326 if (strcmp ("alloc", p) == 0)
11327 kind = OMP_CLAUSE_MAP_ALLOC;
11328 else if (strcmp ("to", p) == 0)
11329 kind = OMP_CLAUSE_MAP_TO;
11330 else if (strcmp ("from", p) == 0)
11331 kind = OMP_CLAUSE_MAP_FROM;
11332 else if (strcmp ("tofrom", p) == 0)
11333 kind = OMP_CLAUSE_MAP_TOFROM;
11334 else
11336 c_parser_error (parser, "invalid map kind");
11337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11338 "expected %<)%>");
11339 return list;
11341 c_parser_consume_token (parser);
11342 c_parser_consume_token (parser);
11345 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
11347 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11348 OMP_CLAUSE_MAP_KIND (c) = kind;
11350 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11351 return nl;
11354 /* OpenMP 4.0:
11355 device ( expression ) */
11357 static tree
11358 c_parser_omp_clause_device (c_parser *parser, tree list)
11360 location_t clause_loc = c_parser_peek_token (parser)->location;
11361 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11363 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
11364 mark_exp_read (t);
11365 t = c_fully_fold (t, false, NULL);
11367 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11369 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11371 c_parser_error (parser, "expected integer expression");
11372 return list;
11375 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
11377 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
11378 OMP_CLAUSE_DEVICE_ID (c) = t;
11379 OMP_CLAUSE_CHAIN (c) = list;
11380 list = c;
11383 return list;
11386 /* OpenMP 4.0:
11387 dist_schedule ( static )
11388 dist_schedule ( static , expression ) */
11390 static tree
11391 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
11393 tree c, t = NULL_TREE;
11394 location_t loc = c_parser_peek_token (parser)->location;
11396 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11397 return list;
11399 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
11401 c_parser_error (parser, "invalid dist_schedule kind");
11402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11403 "expected %<)%>");
11404 return list;
11407 c_parser_consume_token (parser);
11408 if (c_parser_next_token_is (parser, CPP_COMMA))
11410 c_parser_consume_token (parser);
11412 t = c_parser_expr_no_commas (parser, NULL).value;
11413 mark_exp_read (t);
11414 t = c_fully_fold (t, false, NULL);
11415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11417 else
11418 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11419 "expected %<,%> or %<)%>");
11421 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11422 if (t == error_mark_node)
11423 return list;
11425 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
11426 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
11427 OMP_CLAUSE_CHAIN (c) = list;
11428 return c;
11431 /* OpenMP 4.0:
11432 proc_bind ( proc-bind-kind )
11434 proc-bind-kind:
11435 master | close | spread */
11437 static tree
11438 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
11440 location_t clause_loc = c_parser_peek_token (parser)->location;
11441 enum omp_clause_proc_bind_kind kind;
11442 tree c;
11444 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11445 return list;
11447 if (c_parser_next_token_is (parser, CPP_NAME))
11449 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11450 if (strcmp ("master", p) == 0)
11451 kind = OMP_CLAUSE_PROC_BIND_MASTER;
11452 else if (strcmp ("close", p) == 0)
11453 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
11454 else if (strcmp ("spread", p) == 0)
11455 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
11456 else
11457 goto invalid_kind;
11459 else
11460 goto invalid_kind;
11462 c_parser_consume_token (parser);
11463 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11464 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
11465 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
11466 OMP_CLAUSE_CHAIN (c) = list;
11467 return c;
11469 invalid_kind:
11470 c_parser_error (parser, "invalid proc_bind kind");
11471 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11472 return list;
11475 /* OpenMP 4.0:
11476 to ( variable-list ) */
11478 static tree
11479 c_parser_omp_clause_to (c_parser *parser, tree list)
11481 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11484 /* OpenMP 4.0:
11485 from ( variable-list ) */
11487 static tree
11488 c_parser_omp_clause_from (c_parser *parser, tree list)
11490 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11493 /* OpenMP 4.0:
11494 uniform ( variable-list ) */
11496 static tree
11497 c_parser_omp_clause_uniform (c_parser *parser, tree list)
11499 /* The clauses location. */
11500 location_t loc = c_parser_peek_token (parser)->location;
11502 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11504 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11505 list);
11506 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11508 return list;
11511 /* Parse all OpenACC clauses. The set clauses allowed by the directive
11512 is a bitmask in MASK. Return the list of clauses found. */
11514 static tree
11515 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
11516 const char *where, bool finish_p = true)
11518 tree clauses = NULL;
11519 bool first = true;
11521 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11523 location_t here;
11524 pragma_omp_clause c_kind;
11525 const char *c_name;
11526 tree prev = clauses;
11528 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11529 c_parser_consume_token (parser);
11531 here = c_parser_peek_token (parser)->location;
11532 c_kind = c_parser_omp_clause_name (parser);
11534 switch (c_kind)
11536 case PRAGMA_OMP_CLAUSE_ASYNC:
11537 clauses = c_parser_oacc_clause_async (parser, clauses);
11538 c_name = "async";
11539 break;
11540 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11541 clauses = c_parser_omp_clause_collapse (parser, clauses);
11542 c_name = "collapse";
11543 break;
11544 case PRAGMA_OMP_CLAUSE_COPY:
11545 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11546 c_name = "copy";
11547 break;
11548 case PRAGMA_OMP_CLAUSE_COPYIN:
11549 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11550 c_name = "copyin";
11551 break;
11552 case PRAGMA_OMP_CLAUSE_COPYOUT:
11553 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11554 c_name = "copyout";
11555 break;
11556 case PRAGMA_OMP_CLAUSE_CREATE:
11557 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11558 c_name = "create";
11559 break;
11560 case PRAGMA_OMP_CLAUSE_DELETE:
11561 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11562 c_name = "delete";
11563 break;
11564 case PRAGMA_OMP_CLAUSE_DEVICE:
11565 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11566 c_name = "device";
11567 break;
11568 case PRAGMA_OMP_CLAUSE_DEVICEPTR:
11569 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
11570 c_name = "deviceptr";
11571 break;
11572 case PRAGMA_OMP_CLAUSE_HOST:
11573 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11574 c_name = "host";
11575 break;
11576 case PRAGMA_OMP_CLAUSE_IF:
11577 clauses = c_parser_omp_clause_if (parser, clauses);
11578 c_name = "if";
11579 break;
11580 case PRAGMA_OMP_CLAUSE_NUM_GANGS:
11581 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
11582 c_name = "num_gangs";
11583 break;
11584 case PRAGMA_OMP_CLAUSE_NUM_WORKERS:
11585 clauses = c_parser_omp_clause_num_workers (parser, clauses);
11586 c_name = "num_workers";
11587 break;
11588 case PRAGMA_OMP_CLAUSE_PRESENT:
11589 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11590 c_name = "present";
11591 break;
11592 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
11593 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11594 c_name = "present_or_copy";
11595 break;
11596 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
11597 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11598 c_name = "present_or_copyin";
11599 break;
11600 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
11601 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11602 c_name = "present_or_copyout";
11603 break;
11604 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
11605 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11606 c_name = "present_or_create";
11607 break;
11608 case PRAGMA_OMP_CLAUSE_REDUCTION:
11609 clauses = c_parser_omp_clause_reduction (parser, clauses);
11610 c_name = "reduction";
11611 break;
11612 case PRAGMA_OMP_CLAUSE_SELF:
11613 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11614 c_name = "self";
11615 break;
11616 case PRAGMA_OMP_CLAUSE_VECTOR_LENGTH:
11617 clauses = c_parser_omp_clause_vector_length (parser, clauses);
11618 c_name = "vector_length";
11619 break;
11620 case PRAGMA_OMP_CLAUSE_WAIT:
11621 clauses = c_parser_oacc_clause_wait (parser, clauses);
11622 c_name = "wait";
11623 break;
11624 default:
11625 c_parser_error (parser, "expected clause");
11626 goto saw_error;
11629 first = false;
11631 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11633 /* Remove the invalid clause(s) from the list to avoid
11634 confusing the rest of the compiler. */
11635 clauses = prev;
11636 error_at (here, "%qs is not valid for %qs", c_name, where);
11640 saw_error:
11641 c_parser_skip_to_pragma_eol (parser);
11643 if (finish_p)
11644 return c_finish_omp_clauses (clauses);
11646 return clauses;
11649 /* Parse all OpenMP clauses. The set clauses allowed by the directive
11650 is a bitmask in MASK. Return the list of clauses found. */
11652 static tree
11653 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11654 const char *where, bool finish_p = true)
11656 tree clauses = NULL;
11657 bool first = true, cilk_simd_fn = false;
11659 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11661 location_t here;
11662 pragma_omp_clause c_kind;
11663 const char *c_name;
11664 tree prev = clauses;
11666 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11667 c_parser_consume_token (parser);
11669 here = c_parser_peek_token (parser)->location;
11670 c_kind = c_parser_omp_clause_name (parser);
11672 switch (c_kind)
11674 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11675 clauses = c_parser_omp_clause_collapse (parser, clauses);
11676 c_name = "collapse";
11677 break;
11678 case PRAGMA_OMP_CLAUSE_COPYIN:
11679 clauses = c_parser_omp_clause_copyin (parser, clauses);
11680 c_name = "copyin";
11681 break;
11682 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11683 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11684 c_name = "copyprivate";
11685 break;
11686 case PRAGMA_OMP_CLAUSE_DEFAULT:
11687 clauses = c_parser_omp_clause_default (parser, clauses);
11688 c_name = "default";
11689 break;
11690 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11691 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11692 c_name = "firstprivate";
11693 break;
11694 case PRAGMA_OMP_CLAUSE_FINAL:
11695 clauses = c_parser_omp_clause_final (parser, clauses);
11696 c_name = "final";
11697 break;
11698 case PRAGMA_OMP_CLAUSE_IF:
11699 clauses = c_parser_omp_clause_if (parser, clauses);
11700 c_name = "if";
11701 break;
11702 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11703 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11704 c_name = "lastprivate";
11705 break;
11706 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11707 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11708 c_name = "mergeable";
11709 break;
11710 case PRAGMA_OMP_CLAUSE_NOWAIT:
11711 clauses = c_parser_omp_clause_nowait (parser, clauses);
11712 c_name = "nowait";
11713 break;
11714 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11715 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11716 c_name = "num_threads";
11717 break;
11718 case PRAGMA_OMP_CLAUSE_ORDERED:
11719 clauses = c_parser_omp_clause_ordered (parser, clauses);
11720 c_name = "ordered";
11721 break;
11722 case PRAGMA_OMP_CLAUSE_PRIVATE:
11723 clauses = c_parser_omp_clause_private (parser, clauses);
11724 c_name = "private";
11725 break;
11726 case PRAGMA_OMP_CLAUSE_REDUCTION:
11727 clauses = c_parser_omp_clause_reduction (parser, clauses);
11728 c_name = "reduction";
11729 break;
11730 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11731 clauses = c_parser_omp_clause_schedule (parser, clauses);
11732 c_name = "schedule";
11733 break;
11734 case PRAGMA_OMP_CLAUSE_SHARED:
11735 clauses = c_parser_omp_clause_shared (parser, clauses);
11736 c_name = "shared";
11737 break;
11738 case PRAGMA_OMP_CLAUSE_UNTIED:
11739 clauses = c_parser_omp_clause_untied (parser, clauses);
11740 c_name = "untied";
11741 break;
11742 case PRAGMA_OMP_CLAUSE_INBRANCH:
11743 case PRAGMA_CILK_CLAUSE_MASK:
11744 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11745 clauses);
11746 c_name = "inbranch";
11747 break;
11748 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11749 case PRAGMA_CILK_CLAUSE_NOMASK:
11750 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11751 clauses);
11752 c_name = "notinbranch";
11753 break;
11754 case PRAGMA_OMP_CLAUSE_PARALLEL:
11755 clauses
11756 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11757 clauses);
11758 c_name = "parallel";
11759 if (!first)
11761 clause_not_first:
11762 error_at (here, "%qs must be the first clause of %qs",
11763 c_name, where);
11764 clauses = prev;
11766 break;
11767 case PRAGMA_OMP_CLAUSE_FOR:
11768 clauses
11769 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11770 clauses);
11771 c_name = "for";
11772 if (!first)
11773 goto clause_not_first;
11774 break;
11775 case PRAGMA_OMP_CLAUSE_SECTIONS:
11776 clauses
11777 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11778 clauses);
11779 c_name = "sections";
11780 if (!first)
11781 goto clause_not_first;
11782 break;
11783 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11784 clauses
11785 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11786 clauses);
11787 c_name = "taskgroup";
11788 if (!first)
11789 goto clause_not_first;
11790 break;
11791 case PRAGMA_OMP_CLAUSE_TO:
11792 clauses = c_parser_omp_clause_to (parser, clauses);
11793 c_name = "to";
11794 break;
11795 case PRAGMA_OMP_CLAUSE_FROM:
11796 clauses = c_parser_omp_clause_from (parser, clauses);
11797 c_name = "from";
11798 break;
11799 case PRAGMA_OMP_CLAUSE_UNIFORM:
11800 clauses = c_parser_omp_clause_uniform (parser, clauses);
11801 c_name = "uniform";
11802 break;
11803 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11804 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11805 c_name = "num_teams";
11806 break;
11807 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11808 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11809 c_name = "thread_limit";
11810 break;
11811 case PRAGMA_OMP_CLAUSE_ALIGNED:
11812 clauses = c_parser_omp_clause_aligned (parser, clauses);
11813 c_name = "aligned";
11814 break;
11815 case PRAGMA_OMP_CLAUSE_LINEAR:
11816 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11817 cilk_simd_fn = true;
11818 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11819 c_name = "linear";
11820 break;
11821 case PRAGMA_OMP_CLAUSE_DEPEND:
11822 clauses = c_parser_omp_clause_depend (parser, clauses);
11823 c_name = "depend";
11824 break;
11825 case PRAGMA_OMP_CLAUSE_MAP:
11826 clauses = c_parser_omp_clause_map (parser, clauses);
11827 c_name = "map";
11828 break;
11829 case PRAGMA_OMP_CLAUSE_DEVICE:
11830 clauses = c_parser_omp_clause_device (parser, clauses);
11831 c_name = "device";
11832 break;
11833 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11834 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11835 c_name = "dist_schedule";
11836 break;
11837 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11838 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11839 c_name = "proc_bind";
11840 break;
11841 case PRAGMA_OMP_CLAUSE_SAFELEN:
11842 clauses = c_parser_omp_clause_safelen (parser, clauses);
11843 c_name = "safelen";
11844 break;
11845 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11846 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11847 c_name = "simdlen";
11848 break;
11849 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11850 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11851 c_name = "simdlen";
11852 break;
11853 default:
11854 c_parser_error (parser, "expected clause");
11855 goto saw_error;
11858 first = false;
11860 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11862 /* Remove the invalid clause(s) from the list to avoid
11863 confusing the rest of the compiler. */
11864 clauses = prev;
11865 error_at (here, "%qs is not valid for %qs", c_name, where);
11869 saw_error:
11870 c_parser_skip_to_pragma_eol (parser);
11872 if (finish_p)
11873 return c_finish_omp_clauses (clauses);
11875 return clauses;
11878 /* OpenACC 2.0, OpenMP 2.5:
11879 structured-block:
11880 statement
11882 In practice, we're also interested in adding the statement to an
11883 outer node. So it is convenient if we work around the fact that
11884 c_parser_statement calls add_stmt. */
11886 static tree
11887 c_parser_omp_structured_block (c_parser *parser)
11889 tree stmt = push_stmt_list ();
11890 c_parser_statement (parser);
11891 return pop_stmt_list (stmt);
11894 /* OpenACC 2.0:
11895 # pragma acc cache (variable-list) new-line
11897 LOC is the location of the #pragma token.
11900 static tree
11901 c_parser_oacc_cache (location_t loc, c_parser *parser)
11903 tree stmt, clauses;
11905 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
11906 clauses = c_finish_omp_clauses (clauses);
11908 c_parser_skip_to_pragma_eol (parser);
11910 stmt = make_node (OACC_CACHE);
11911 TREE_TYPE (stmt) = void_type_node;
11912 OACC_CACHE_CLAUSES (stmt) = clauses;
11913 SET_EXPR_LOCATION (stmt, loc);
11914 add_stmt (stmt);
11916 return stmt;
11919 /* OpenACC 2.0:
11920 # pragma acc data oacc-data-clause[optseq] new-line
11921 structured-block
11923 LOC is the location of the #pragma token.
11926 #define OACC_DATA_CLAUSE_MASK \
11927 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
11928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
11929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
11930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
11931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
11932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
11933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
11934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
11935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
11936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
11937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) )
11939 static tree
11940 c_parser_oacc_data (location_t loc, c_parser *parser)
11942 tree stmt, clauses, block;
11944 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
11945 "#pragma acc data");
11947 block = c_begin_omp_parallel ();
11948 add_stmt (c_parser_omp_structured_block (parser));
11950 stmt = c_finish_oacc_data (loc, clauses, block);
11952 return stmt;
11955 /* OpenACC 2.0:
11956 # pragma acc kernels oacc-kernels-clause[optseq] new-line
11957 structured-block
11959 LOC is the location of the #pragma token.
11962 #define OACC_KERNELS_CLAUSE_MASK \
11963 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
11964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
11965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
11966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
11967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
11968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
11969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
11970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
11971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
11972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
11973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
11974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
11975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
11977 static tree
11978 c_parser_oacc_kernels (location_t loc, c_parser *parser, char *p_name)
11980 tree stmt, clauses = NULL_TREE, block;
11982 strcat (p_name, " kernels");
11984 if (c_parser_next_token_is (parser, CPP_NAME))
11986 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11987 if (strcmp (p, "loop") == 0)
11989 c_parser_consume_token (parser);
11990 block = c_begin_omp_parallel ();
11991 c_parser_oacc_loop (loc, parser, p_name);
11992 stmt = c_finish_oacc_kernels (loc, clauses, block);
11993 OACC_KERNELS_COMBINED (stmt) = 1;
11994 return stmt;
11998 clauses = c_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
11999 p_name);
12001 block = c_begin_omp_parallel ();
12002 add_stmt (c_parser_omp_structured_block (parser));
12004 stmt = c_finish_oacc_kernels (loc, clauses, block);
12006 return stmt;
12009 /* OpenACC 2.0:
12010 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
12014 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
12017 LOC is the location of the #pragma token.
12020 #define OACC_ENTER_DATA_CLAUSE_MASK \
12021 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
12023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
12025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
12026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
12027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
12029 #define OACC_EXIT_DATA_CLAUSE_MASK \
12030 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
12032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
12033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DELETE) \
12034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
12036 static void
12037 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
12039 location_t loc = c_parser_peek_token (parser)->location;
12040 tree clauses, stmt;
12042 c_parser_consume_pragma (parser);
12044 if (!c_parser_next_token_is (parser, CPP_NAME))
12046 c_parser_error (parser, enter
12047 ? "expected %<data%> in %<#pragma acc enter data%>"
12048 : "expected %<data%> in %<#pragma acc exit data%>");
12049 c_parser_skip_to_pragma_eol (parser);
12050 return;
12053 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12054 if (strcmp (p, "data") != 0)
12056 c_parser_error (parser, "invalid pragma");
12057 c_parser_skip_to_pragma_eol (parser);
12058 return;
12061 c_parser_consume_token (parser);
12063 if (enter)
12064 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
12065 "#pragma acc enter data");
12066 else
12067 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
12068 "#pragma acc exit data");
12070 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12072 error_at (loc, enter
12073 ? "%<#pragma acc enter data%> has no data movement clause"
12074 : "%<#pragma acc exit data%> has no data movement clause");
12075 return;
12078 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);;
12079 TREE_TYPE (stmt) = void_type_node;
12080 if (enter)
12081 OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
12082 else
12083 OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
12084 SET_EXPR_LOCATION (stmt, loc);
12085 add_stmt (stmt);
12089 /* OpenACC 2.0:
12091 # pragma acc loop oacc-loop-clause[optseq] new-line
12092 structured-block
12094 LOC is the location of the #pragma token.
12097 #define OACC_LOOP_CLAUSE_MASK \
12098 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
12099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) )
12101 static tree
12102 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name)
12104 tree stmt, clauses, block;
12106 strcat (p_name, " loop");
12108 clauses = c_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK, p_name);
12110 block = c_begin_compound_stmt (true);
12111 stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
12112 block = c_end_compound_stmt (loc, block, true);
12113 add_stmt (block);
12115 return stmt;
12118 /* OpenACC 2.0:
12119 # pragma acc parallel oacc-parallel-clause[optseq] new-line
12120 structured-block
12122 LOC is the location of the #pragma token.
12125 #define OACC_PARALLEL_CLAUSE_MASK \
12126 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
12127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
12128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
12130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
12131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
12132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_GANGS) \
12134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_WORKERS) \
12135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
12136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
12137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
12138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
12139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
12140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_VECTOR_LENGTH) \
12142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
12144 static tree
12145 c_parser_oacc_parallel (location_t loc, c_parser *parser, char *p_name)
12147 tree stmt, clauses = NULL_TREE, block;
12149 strcat (p_name, " parallel");
12151 if (c_parser_next_token_is (parser, CPP_NAME))
12153 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12154 if (strcmp (p, "loop") == 0)
12156 c_parser_consume_token (parser);
12157 block = c_begin_omp_parallel ();
12158 c_parser_oacc_loop (loc, parser, p_name);
12159 stmt = c_finish_oacc_parallel (loc, clauses, block);
12160 OACC_PARALLEL_COMBINED (stmt) = 1;
12161 return stmt;
12165 clauses = c_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
12166 p_name);
12168 block = c_begin_omp_parallel ();
12169 add_stmt (c_parser_omp_structured_block (parser));
12171 stmt = c_finish_oacc_parallel (loc, clauses, block);
12173 return stmt;
12176 /* OpenACC 2.0:
12177 # pragma acc update oacc-update-clause[optseq] new-line
12180 #define OACC_UPDATE_CLAUSE_MASK \
12181 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
12182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HOST) \
12184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SELF) \
12186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
12188 static void
12189 c_parser_oacc_update (c_parser *parser)
12191 location_t loc = c_parser_peek_token (parser)->location;
12193 c_parser_consume_pragma (parser);
12195 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
12196 "#pragma acc update");
12197 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12199 error_at (loc,
12200 "%<#pragma acc update%> must contain at least one "
12201 "%<device%> or %<host/self%> clause");
12202 return;
12205 if (parser->error)
12206 return;
12208 tree stmt = make_node (OACC_UPDATE);
12209 TREE_TYPE (stmt) = void_type_node;
12210 OACC_UPDATE_CLAUSES (stmt) = clauses;
12211 SET_EXPR_LOCATION (stmt, loc);
12212 add_stmt (stmt);
12215 /* OpenACC 2.0:
12216 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
12218 LOC is the location of the #pragma token.
12221 #define OACC_WAIT_CLAUSE_MASK \
12222 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) )
12224 static tree
12225 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
12227 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
12229 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12230 list = c_parser_oacc_wait_list (parser, loc, list);
12232 strcpy (p_name, " wait");
12233 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
12234 stmt = c_finish_oacc_wait (loc, list, clauses);
12236 return stmt;
12239 /* OpenMP 2.5:
12240 # pragma omp atomic new-line
12241 expression-stmt
12243 expression-stmt:
12244 x binop= expr | x++ | ++x | x-- | --x
12245 binop:
12246 +, *, -, /, &, ^, |, <<, >>
12248 where x is an lvalue expression with scalar type.
12250 OpenMP 3.1:
12251 # pragma omp atomic new-line
12252 update-stmt
12254 # pragma omp atomic read new-line
12255 read-stmt
12257 # pragma omp atomic write new-line
12258 write-stmt
12260 # pragma omp atomic update new-line
12261 update-stmt
12263 # pragma omp atomic capture new-line
12264 capture-stmt
12266 # pragma omp atomic capture new-line
12267 capture-block
12269 read-stmt:
12270 v = x
12271 write-stmt:
12272 x = expr
12273 update-stmt:
12274 expression-stmt | x = x binop expr
12275 capture-stmt:
12276 v = expression-stmt
12277 capture-block:
12278 { v = x; update-stmt; } | { update-stmt; v = x; }
12280 OpenMP 4.0:
12281 update-stmt:
12282 expression-stmt | x = x binop expr | x = expr binop x
12283 capture-stmt:
12284 v = update-stmt
12285 capture-block:
12286 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
12288 where x and v are lvalue expressions with scalar type.
12290 LOC is the location of the #pragma token. */
12292 static void
12293 c_parser_omp_atomic (location_t loc, c_parser *parser)
12295 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
12296 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
12297 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
12298 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
12299 struct c_expr expr;
12300 location_t eloc;
12301 bool structured_block = false;
12302 bool swapped = false;
12303 bool seq_cst = false;
12305 if (c_parser_next_token_is (parser, CPP_NAME))
12307 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12308 if (!strcmp (p, "seq_cst"))
12310 seq_cst = true;
12311 c_parser_consume_token (parser);
12312 if (c_parser_next_token_is (parser, CPP_COMMA)
12313 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12314 c_parser_consume_token (parser);
12317 if (c_parser_next_token_is (parser, CPP_NAME))
12319 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12321 if (!strcmp (p, "read"))
12322 code = OMP_ATOMIC_READ;
12323 else if (!strcmp (p, "write"))
12324 code = NOP_EXPR;
12325 else if (!strcmp (p, "update"))
12326 code = OMP_ATOMIC;
12327 else if (!strcmp (p, "capture"))
12328 code = OMP_ATOMIC_CAPTURE_NEW;
12329 else
12330 p = NULL;
12331 if (p)
12332 c_parser_consume_token (parser);
12334 if (!seq_cst)
12336 if (c_parser_next_token_is (parser, CPP_COMMA)
12337 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12338 c_parser_consume_token (parser);
12340 if (c_parser_next_token_is (parser, CPP_NAME))
12342 const char *p
12343 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12344 if (!strcmp (p, "seq_cst"))
12346 seq_cst = true;
12347 c_parser_consume_token (parser);
12351 c_parser_skip_to_pragma_eol (parser);
12353 switch (code)
12355 case OMP_ATOMIC_READ:
12356 case NOP_EXPR: /* atomic write */
12357 v = c_parser_unary_expression (parser).value;
12358 v = c_fully_fold (v, false, NULL);
12359 if (v == error_mark_node)
12360 goto saw_error;
12361 loc = c_parser_peek_token (parser)->location;
12362 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12363 goto saw_error;
12364 if (code == NOP_EXPR)
12365 lhs = c_parser_expression (parser).value;
12366 else
12367 lhs = c_parser_unary_expression (parser).value;
12368 lhs = c_fully_fold (lhs, false, NULL);
12369 if (lhs == error_mark_node)
12370 goto saw_error;
12371 if (code == NOP_EXPR)
12373 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
12374 opcode. */
12375 code = OMP_ATOMIC;
12376 rhs = lhs;
12377 lhs = v;
12378 v = NULL_TREE;
12380 goto done;
12381 case OMP_ATOMIC_CAPTURE_NEW:
12382 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12384 c_parser_consume_token (parser);
12385 structured_block = true;
12387 else
12389 v = c_parser_unary_expression (parser).value;
12390 v = c_fully_fold (v, false, NULL);
12391 if (v == error_mark_node)
12392 goto saw_error;
12393 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12394 goto saw_error;
12396 break;
12397 default:
12398 break;
12401 /* For structured_block case we don't know yet whether
12402 old or new x should be captured. */
12403 restart:
12404 eloc = c_parser_peek_token (parser)->location;
12405 expr = c_parser_unary_expression (parser);
12406 lhs = expr.value;
12407 expr = default_function_array_conversion (eloc, expr);
12408 unfolded_lhs = expr.value;
12409 lhs = c_fully_fold (lhs, false, NULL);
12410 orig_lhs = lhs;
12411 switch (TREE_CODE (lhs))
12413 case ERROR_MARK:
12414 saw_error:
12415 c_parser_skip_to_end_of_block_or_statement (parser);
12416 if (structured_block)
12418 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12419 c_parser_consume_token (parser);
12420 else if (code == OMP_ATOMIC_CAPTURE_NEW)
12422 c_parser_skip_to_end_of_block_or_statement (parser);
12423 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12424 c_parser_consume_token (parser);
12427 return;
12429 case POSTINCREMENT_EXPR:
12430 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12431 code = OMP_ATOMIC_CAPTURE_OLD;
12432 /* FALLTHROUGH */
12433 case PREINCREMENT_EXPR:
12434 lhs = TREE_OPERAND (lhs, 0);
12435 unfolded_lhs = NULL_TREE;
12436 opcode = PLUS_EXPR;
12437 rhs = integer_one_node;
12438 break;
12440 case POSTDECREMENT_EXPR:
12441 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12442 code = OMP_ATOMIC_CAPTURE_OLD;
12443 /* FALLTHROUGH */
12444 case PREDECREMENT_EXPR:
12445 lhs = TREE_OPERAND (lhs, 0);
12446 unfolded_lhs = NULL_TREE;
12447 opcode = MINUS_EXPR;
12448 rhs = integer_one_node;
12449 break;
12451 case COMPOUND_EXPR:
12452 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
12453 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
12454 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
12455 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
12456 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
12457 (TREE_OPERAND (lhs, 1), 0), 0)))
12458 == BOOLEAN_TYPE)
12459 /* Undo effects of boolean_increment for post {in,de}crement. */
12460 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
12461 /* FALLTHRU */
12462 case MODIFY_EXPR:
12463 if (TREE_CODE (lhs) == MODIFY_EXPR
12464 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
12466 /* Undo effects of boolean_increment. */
12467 if (integer_onep (TREE_OPERAND (lhs, 1)))
12469 /* This is pre or post increment. */
12470 rhs = TREE_OPERAND (lhs, 1);
12471 lhs = TREE_OPERAND (lhs, 0);
12472 unfolded_lhs = NULL_TREE;
12473 opcode = NOP_EXPR;
12474 if (code == OMP_ATOMIC_CAPTURE_NEW
12475 && !structured_block
12476 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12477 code = OMP_ATOMIC_CAPTURE_OLD;
12478 break;
12480 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
12481 && TREE_OPERAND (lhs, 0)
12482 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
12484 /* This is pre or post decrement. */
12485 rhs = TREE_OPERAND (lhs, 1);
12486 lhs = TREE_OPERAND (lhs, 0);
12487 unfolded_lhs = NULL_TREE;
12488 opcode = NOP_EXPR;
12489 if (code == OMP_ATOMIC_CAPTURE_NEW
12490 && !structured_block
12491 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12492 code = OMP_ATOMIC_CAPTURE_OLD;
12493 break;
12496 /* FALLTHRU */
12497 default:
12498 switch (c_parser_peek_token (parser)->type)
12500 case CPP_MULT_EQ:
12501 opcode = MULT_EXPR;
12502 break;
12503 case CPP_DIV_EQ:
12504 opcode = TRUNC_DIV_EXPR;
12505 break;
12506 case CPP_PLUS_EQ:
12507 opcode = PLUS_EXPR;
12508 break;
12509 case CPP_MINUS_EQ:
12510 opcode = MINUS_EXPR;
12511 break;
12512 case CPP_LSHIFT_EQ:
12513 opcode = LSHIFT_EXPR;
12514 break;
12515 case CPP_RSHIFT_EQ:
12516 opcode = RSHIFT_EXPR;
12517 break;
12518 case CPP_AND_EQ:
12519 opcode = BIT_AND_EXPR;
12520 break;
12521 case CPP_OR_EQ:
12522 opcode = BIT_IOR_EXPR;
12523 break;
12524 case CPP_XOR_EQ:
12525 opcode = BIT_XOR_EXPR;
12526 break;
12527 case CPP_EQ:
12528 c_parser_consume_token (parser);
12529 eloc = c_parser_peek_token (parser)->location;
12530 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
12531 rhs1 = expr.value;
12532 switch (TREE_CODE (rhs1))
12534 case MULT_EXPR:
12535 case TRUNC_DIV_EXPR:
12536 case PLUS_EXPR:
12537 case MINUS_EXPR:
12538 case LSHIFT_EXPR:
12539 case RSHIFT_EXPR:
12540 case BIT_AND_EXPR:
12541 case BIT_IOR_EXPR:
12542 case BIT_XOR_EXPR:
12543 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
12545 opcode = TREE_CODE (rhs1);
12546 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12547 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12548 goto stmt_done;
12550 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
12552 opcode = TREE_CODE (rhs1);
12553 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12554 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12555 swapped = !commutative_tree_code (opcode);
12556 goto stmt_done;
12558 break;
12559 case ERROR_MARK:
12560 goto saw_error;
12561 default:
12562 break;
12564 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
12566 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12568 code = OMP_ATOMIC_CAPTURE_OLD;
12569 v = lhs;
12570 lhs = NULL_TREE;
12571 expr = default_function_array_read_conversion (eloc, expr);
12572 unfolded_lhs1 = expr.value;
12573 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
12574 rhs1 = NULL_TREE;
12575 c_parser_consume_token (parser);
12576 goto restart;
12578 if (structured_block)
12580 opcode = NOP_EXPR;
12581 expr = default_function_array_read_conversion (eloc, expr);
12582 rhs = c_fully_fold (expr.value, false, NULL);
12583 rhs1 = NULL_TREE;
12584 goto stmt_done;
12587 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
12588 goto saw_error;
12589 default:
12590 c_parser_error (parser,
12591 "invalid operator for %<#pragma omp atomic%>");
12592 goto saw_error;
12595 /* Arrange to pass the location of the assignment operator to
12596 c_finish_omp_atomic. */
12597 loc = c_parser_peek_token (parser)->location;
12598 c_parser_consume_token (parser);
12599 eloc = c_parser_peek_token (parser)->location;
12600 expr = c_parser_expression (parser);
12601 expr = default_function_array_read_conversion (eloc, expr);
12602 rhs = expr.value;
12603 rhs = c_fully_fold (rhs, false, NULL);
12604 break;
12606 stmt_done:
12607 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12609 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
12610 goto saw_error;
12611 v = c_parser_unary_expression (parser).value;
12612 v = c_fully_fold (v, false, NULL);
12613 if (v == error_mark_node)
12614 goto saw_error;
12615 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12616 goto saw_error;
12617 eloc = c_parser_peek_token (parser)->location;
12618 expr = c_parser_unary_expression (parser);
12619 lhs1 = expr.value;
12620 expr = default_function_array_read_conversion (eloc, expr);
12621 unfolded_lhs1 = expr.value;
12622 lhs1 = c_fully_fold (lhs1, false, NULL);
12623 if (lhs1 == error_mark_node)
12624 goto saw_error;
12626 if (structured_block)
12628 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12629 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
12631 done:
12632 if (unfolded_lhs && unfolded_lhs1
12633 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
12635 error ("%<#pragma omp atomic capture%> uses two different "
12636 "expressions for memory");
12637 stmt = error_mark_node;
12639 else
12640 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
12641 swapped, seq_cst);
12642 if (stmt != error_mark_node)
12643 add_stmt (stmt);
12645 if (!structured_block)
12646 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12650 /* OpenMP 2.5:
12651 # pragma omp barrier new-line
12654 static void
12655 c_parser_omp_barrier (c_parser *parser)
12657 location_t loc = c_parser_peek_token (parser)->location;
12658 c_parser_consume_pragma (parser);
12659 c_parser_skip_to_pragma_eol (parser);
12661 c_finish_omp_barrier (loc);
12664 /* OpenMP 2.5:
12665 # pragma omp critical [(name)] new-line
12666 structured-block
12668 LOC is the location of the #pragma itself. */
12670 static tree
12671 c_parser_omp_critical (location_t loc, c_parser *parser)
12673 tree stmt, name = NULL;
12675 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12677 c_parser_consume_token (parser);
12678 if (c_parser_next_token_is (parser, CPP_NAME))
12680 name = c_parser_peek_token (parser)->value;
12681 c_parser_consume_token (parser);
12682 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12684 else
12685 c_parser_error (parser, "expected identifier");
12687 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12688 c_parser_error (parser, "expected %<(%> or end of line");
12689 c_parser_skip_to_pragma_eol (parser);
12691 stmt = c_parser_omp_structured_block (parser);
12692 return c_finish_omp_critical (loc, stmt, name);
12695 /* OpenMP 2.5:
12696 # pragma omp flush flush-vars[opt] new-line
12698 flush-vars:
12699 ( variable-list ) */
12701 static void
12702 c_parser_omp_flush (c_parser *parser)
12704 location_t loc = c_parser_peek_token (parser)->location;
12705 c_parser_consume_pragma (parser);
12706 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12707 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12708 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12709 c_parser_error (parser, "expected %<(%> or end of line");
12710 c_parser_skip_to_pragma_eol (parser);
12712 c_finish_omp_flush (loc);
12715 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
12716 The real trick here is to determine the loop control variable early
12717 so that we can push a new decl if necessary to make it private.
12718 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
12719 respectively. */
12721 static tree
12722 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
12723 tree clauses, tree *cclauses)
12725 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
12726 tree declv, condv, incrv, initv, ret = NULL;
12727 bool fail = false, open_brace_parsed = false;
12728 int i, collapse = 1, nbraces = 0;
12729 location_t for_loc;
12730 vec<tree, va_gc> *for_block = make_tree_vector ();
12732 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
12733 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
12734 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
12736 gcc_assert (collapse >= 1);
12738 declv = make_tree_vec (collapse);
12739 initv = make_tree_vec (collapse);
12740 condv = make_tree_vec (collapse);
12741 incrv = make_tree_vec (collapse);
12743 if (code != CILK_FOR
12744 && !c_parser_next_token_is_keyword (parser, RID_FOR))
12746 c_parser_error (parser, "for statement expected");
12747 return NULL;
12749 if (code == CILK_FOR
12750 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
12752 c_parser_error (parser, "_Cilk_for statement expected");
12753 return NULL;
12755 for_loc = c_parser_peek_token (parser)->location;
12756 c_parser_consume_token (parser);
12758 for (i = 0; i < collapse; i++)
12760 int bracecount = 0;
12762 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12763 goto pop_scopes;
12765 /* Parse the initialization declaration or expression. */
12766 if (c_parser_next_tokens_start_declaration (parser))
12768 if (i > 0)
12769 vec_safe_push (for_block, c_begin_compound_stmt (true));
12770 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12771 NULL, vNULL);
12772 decl = check_for_loop_decls (for_loc, flag_isoc99);
12773 if (decl == NULL)
12774 goto error_init;
12775 if (DECL_INITIAL (decl) == error_mark_node)
12776 decl = error_mark_node;
12777 init = decl;
12779 else if (c_parser_next_token_is (parser, CPP_NAME)
12780 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
12782 struct c_expr decl_exp;
12783 struct c_expr init_exp;
12784 location_t init_loc;
12786 decl_exp = c_parser_postfix_expression (parser);
12787 decl = decl_exp.value;
12789 c_parser_require (parser, CPP_EQ, "expected %<=%>");
12791 init_loc = c_parser_peek_token (parser)->location;
12792 init_exp = c_parser_expr_no_commas (parser, NULL);
12793 init_exp = default_function_array_read_conversion (init_loc,
12794 init_exp);
12795 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
12796 NOP_EXPR, init_loc, init_exp.value,
12797 init_exp.original_type);
12798 init = c_process_expr_stmt (init_loc, init);
12800 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12802 else
12804 error_init:
12805 c_parser_error (parser,
12806 "expected iteration declaration or initialization");
12807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12808 "expected %<)%>");
12809 fail = true;
12810 goto parse_next;
12813 /* Parse the loop condition. */
12814 cond = NULL_TREE;
12815 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
12817 location_t cond_loc = c_parser_peek_token (parser)->location;
12818 struct c_expr cond_expr
12819 = c_parser_binary_expression (parser, NULL, NULL_TREE);
12821 cond = cond_expr.value;
12822 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
12823 cond = c_fully_fold (cond, false, NULL);
12824 switch (cond_expr.original_code)
12826 case GT_EXPR:
12827 case GE_EXPR:
12828 case LT_EXPR:
12829 case LE_EXPR:
12830 break;
12831 case NE_EXPR:
12832 if (code == CILK_SIMD || code == CILK_FOR)
12833 break;
12834 /* FALLTHRU. */
12835 default:
12836 /* Can't be cond = error_mark_node, because we want to preserve
12837 the location until c_finish_omp_for. */
12838 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
12839 break;
12841 protected_set_expr_location (cond, cond_loc);
12843 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12845 /* Parse the increment expression. */
12846 incr = NULL_TREE;
12847 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
12849 location_t incr_loc = c_parser_peek_token (parser)->location;
12851 incr = c_process_expr_stmt (incr_loc,
12852 c_parser_expression (parser).value);
12854 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12856 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
12857 fail = true;
12858 else
12860 TREE_VEC_ELT (declv, i) = decl;
12861 TREE_VEC_ELT (initv, i) = init;
12862 TREE_VEC_ELT (condv, i) = cond;
12863 TREE_VEC_ELT (incrv, i) = incr;
12866 parse_next:
12867 if (i == collapse - 1)
12868 break;
12870 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
12871 in between the collapsed for loops to be still considered perfectly
12872 nested. Hopefully the final version clarifies this.
12873 For now handle (multiple) {'s and empty statements. */
12876 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12878 c_parser_consume_token (parser);
12879 break;
12881 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12883 c_parser_consume_token (parser);
12884 bracecount++;
12886 else if (bracecount
12887 && c_parser_next_token_is (parser, CPP_SEMICOLON))
12888 c_parser_consume_token (parser);
12889 else
12891 c_parser_error (parser, "not enough perfectly nested loops");
12892 if (bracecount)
12894 open_brace_parsed = true;
12895 bracecount--;
12897 fail = true;
12898 collapse = 0;
12899 break;
12902 while (1);
12904 nbraces += bracecount;
12907 save_break = c_break_label;
12908 if (code == CILK_SIMD)
12909 c_break_label = build_int_cst (size_type_node, 2);
12910 else
12911 c_break_label = size_one_node;
12912 save_cont = c_cont_label;
12913 c_cont_label = NULL_TREE;
12914 body = push_stmt_list ();
12916 if (open_brace_parsed)
12918 location_t here = c_parser_peek_token (parser)->location;
12919 stmt = c_begin_compound_stmt (true);
12920 c_parser_compound_statement_nostart (parser);
12921 add_stmt (c_end_compound_stmt (here, stmt, true));
12923 else
12924 add_stmt (c_parser_c99_block_statement (parser));
12925 if (c_cont_label)
12927 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
12928 SET_EXPR_LOCATION (t, loc);
12929 add_stmt (t);
12932 body = pop_stmt_list (body);
12933 c_break_label = save_break;
12934 c_cont_label = save_cont;
12936 while (nbraces)
12938 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12940 c_parser_consume_token (parser);
12941 nbraces--;
12943 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
12944 c_parser_consume_token (parser);
12945 else
12947 c_parser_error (parser, "collapsed loops not perfectly nested");
12948 while (nbraces)
12950 location_t here = c_parser_peek_token (parser)->location;
12951 stmt = c_begin_compound_stmt (true);
12952 add_stmt (body);
12953 c_parser_compound_statement_nostart (parser);
12954 body = c_end_compound_stmt (here, stmt, true);
12955 nbraces--;
12957 goto pop_scopes;
12961 /* Only bother calling c_finish_omp_for if we haven't already generated
12962 an error from the initialization parsing. */
12963 if (!fail)
12965 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
12966 incrv, body, NULL);
12967 if (stmt)
12969 if (cclauses != NULL
12970 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
12972 gcc_assert (code != OACC_LOOP);
12973 tree *c;
12974 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
12975 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
12976 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
12977 c = &OMP_CLAUSE_CHAIN (*c);
12978 else
12980 for (i = 0; i < collapse; i++)
12981 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
12982 break;
12983 if (i == collapse)
12984 c = &OMP_CLAUSE_CHAIN (*c);
12985 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
12987 error_at (loc,
12988 "iteration variable %qD should not be firstprivate",
12989 OMP_CLAUSE_DECL (*c));
12990 *c = OMP_CLAUSE_CHAIN (*c);
12992 else
12994 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
12995 change it to shared (decl) in
12996 OMP_PARALLEL_CLAUSES. */
12997 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
12998 OMP_CLAUSE_LASTPRIVATE);
12999 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
13000 if (code == OMP_SIMD)
13002 OMP_CLAUSE_CHAIN (l)
13003 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13004 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
13006 else
13008 OMP_CLAUSE_CHAIN (l) = clauses;
13009 clauses = l;
13011 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
13015 OMP_FOR_CLAUSES (stmt) = clauses;
13017 ret = stmt;
13019 pop_scopes:
13020 while (!for_block->is_empty ())
13022 /* FIXME diagnostics: LOC below should be the actual location of
13023 this particular for block. We need to build a list of
13024 locations to go along with FOR_BLOCK. */
13025 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
13026 add_stmt (stmt);
13028 release_tree_vector (for_block);
13029 return ret;
13032 /* Helper function for OpenMP parsing, split clauses and call
13033 finish_omp_clauses on each of the set of clauses afterwards. */
13035 static void
13036 omp_split_clauses (location_t loc, enum tree_code code,
13037 omp_clause_mask mask, tree clauses, tree *cclauses)
13039 int i;
13040 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
13041 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
13042 if (cclauses[i])
13043 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
13046 /* OpenMP 4.0:
13047 #pragma omp simd simd-clause[optseq] new-line
13048 for-loop
13050 LOC is the location of the #pragma token.
13053 #define OMP_SIMD_CLAUSE_MASK \
13054 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
13055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13062 static tree
13063 c_parser_omp_simd (location_t loc, c_parser *parser,
13064 char *p_name, omp_clause_mask mask, tree *cclauses)
13066 tree block, clauses, ret;
13068 strcat (p_name, " simd");
13069 mask |= OMP_SIMD_CLAUSE_MASK;
13070 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
13072 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13073 if (cclauses)
13075 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
13076 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
13079 block = c_begin_compound_stmt (true);
13080 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
13081 block = c_end_compound_stmt (loc, block, true);
13082 add_stmt (block);
13084 return ret;
13087 /* OpenMP 2.5:
13088 #pragma omp for for-clause[optseq] new-line
13089 for-loop
13091 OpenMP 4.0:
13092 #pragma omp for simd for-simd-clause[optseq] new-line
13093 for-loop
13095 LOC is the location of the #pragma token.
13098 #define OMP_FOR_CLAUSE_MASK \
13099 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
13104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
13105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
13106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13108 static tree
13109 c_parser_omp_for (location_t loc, c_parser *parser,
13110 char *p_name, omp_clause_mask mask, tree *cclauses)
13112 tree block, clauses, ret;
13114 strcat (p_name, " for");
13115 mask |= OMP_FOR_CLAUSE_MASK;
13116 if (cclauses)
13117 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13119 if (c_parser_next_token_is (parser, CPP_NAME))
13121 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13123 if (strcmp (p, "simd") == 0)
13125 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13126 if (cclauses == NULL)
13127 cclauses = cclauses_buf;
13129 c_parser_consume_token (parser);
13130 if (!flag_openmp) /* flag_openmp_simd */
13131 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13132 block = c_begin_compound_stmt (true);
13133 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13134 block = c_end_compound_stmt (loc, block, true);
13135 if (ret == NULL_TREE)
13136 return ret;
13137 ret = make_node (OMP_FOR);
13138 TREE_TYPE (ret) = void_type_node;
13139 OMP_FOR_BODY (ret) = block;
13140 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13141 SET_EXPR_LOCATION (ret, loc);
13142 add_stmt (ret);
13143 return ret;
13146 if (!flag_openmp) /* flag_openmp_simd */
13148 c_parser_skip_to_pragma_eol (parser);
13149 return NULL_TREE;
13152 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13153 if (cclauses)
13155 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
13156 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13159 block = c_begin_compound_stmt (true);
13160 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
13161 block = c_end_compound_stmt (loc, block, true);
13162 add_stmt (block);
13164 return ret;
13167 /* OpenMP 2.5:
13168 # pragma omp master new-line
13169 structured-block
13171 LOC is the location of the #pragma token.
13174 static tree
13175 c_parser_omp_master (location_t loc, c_parser *parser)
13177 c_parser_skip_to_pragma_eol (parser);
13178 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
13181 /* OpenMP 2.5:
13182 # pragma omp ordered new-line
13183 structured-block
13185 LOC is the location of the #pragma itself.
13188 static tree
13189 c_parser_omp_ordered (location_t loc, c_parser *parser)
13191 c_parser_skip_to_pragma_eol (parser);
13192 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
13195 /* OpenMP 2.5:
13197 section-scope:
13198 { section-sequence }
13200 section-sequence:
13201 section-directive[opt] structured-block
13202 section-sequence section-directive structured-block
13204 SECTIONS_LOC is the location of the #pragma omp sections. */
13206 static tree
13207 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
13209 tree stmt, substmt;
13210 bool error_suppress = false;
13211 location_t loc;
13213 loc = c_parser_peek_token (parser)->location;
13214 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
13216 /* Avoid skipping until the end of the block. */
13217 parser->error = false;
13218 return NULL_TREE;
13221 stmt = push_stmt_list ();
13223 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
13225 substmt = c_parser_omp_structured_block (parser);
13226 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13227 SET_EXPR_LOCATION (substmt, loc);
13228 add_stmt (substmt);
13231 while (1)
13233 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13234 break;
13235 if (c_parser_next_token_is (parser, CPP_EOF))
13236 break;
13238 loc = c_parser_peek_token (parser)->location;
13239 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
13241 c_parser_consume_pragma (parser);
13242 c_parser_skip_to_pragma_eol (parser);
13243 error_suppress = false;
13245 else if (!error_suppress)
13247 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
13248 error_suppress = true;
13251 substmt = c_parser_omp_structured_block (parser);
13252 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13253 SET_EXPR_LOCATION (substmt, loc);
13254 add_stmt (substmt);
13256 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
13257 "expected %<#pragma omp section%> or %<}%>");
13259 substmt = pop_stmt_list (stmt);
13261 stmt = make_node (OMP_SECTIONS);
13262 SET_EXPR_LOCATION (stmt, sections_loc);
13263 TREE_TYPE (stmt) = void_type_node;
13264 OMP_SECTIONS_BODY (stmt) = substmt;
13266 return add_stmt (stmt);
13269 /* OpenMP 2.5:
13270 # pragma omp sections sections-clause[optseq] newline
13271 sections-scope
13273 LOC is the location of the #pragma token.
13276 #define OMP_SECTIONS_CLAUSE_MASK \
13277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13283 static tree
13284 c_parser_omp_sections (location_t loc, c_parser *parser,
13285 char *p_name, omp_clause_mask mask, tree *cclauses)
13287 tree block, clauses, ret;
13289 strcat (p_name, " sections");
13290 mask |= OMP_SECTIONS_CLAUSE_MASK;
13291 if (cclauses)
13292 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13294 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13295 if (cclauses)
13297 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
13298 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
13301 block = c_begin_compound_stmt (true);
13302 ret = c_parser_omp_sections_scope (loc, parser);
13303 if (ret)
13304 OMP_SECTIONS_CLAUSES (ret) = clauses;
13305 block = c_end_compound_stmt (loc, block, true);
13306 add_stmt (block);
13308 return ret;
13311 /* OpenMP 2.5:
13312 # pragma omp parallel parallel-clause[optseq] new-line
13313 structured-block
13314 # pragma omp parallel for parallel-for-clause[optseq] new-line
13315 structured-block
13316 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
13317 structured-block
13319 OpenMP 4.0:
13320 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
13321 structured-block
13323 LOC is the location of the #pragma token.
13326 #define OMP_PARALLEL_CLAUSE_MASK \
13327 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
13333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
13335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
13337 static tree
13338 c_parser_omp_parallel (location_t loc, c_parser *parser,
13339 char *p_name, omp_clause_mask mask, tree *cclauses)
13341 tree stmt, clauses, block;
13343 strcat (p_name, " parallel");
13344 mask |= OMP_PARALLEL_CLAUSE_MASK;
13346 if (c_parser_next_token_is_keyword (parser, RID_FOR))
13348 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13349 if (cclauses == NULL)
13350 cclauses = cclauses_buf;
13352 c_parser_consume_token (parser);
13353 if (!flag_openmp) /* flag_openmp_simd */
13354 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13355 block = c_begin_omp_parallel ();
13356 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13357 stmt
13358 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13359 block);
13360 if (ret == NULL_TREE)
13361 return ret;
13362 OMP_PARALLEL_COMBINED (stmt) = 1;
13363 return stmt;
13365 else if (cclauses)
13367 error_at (loc, "expected %<for%> after %qs", p_name);
13368 c_parser_skip_to_pragma_eol (parser);
13369 return NULL_TREE;
13371 else if (!flag_openmp) /* flag_openmp_simd */
13373 c_parser_skip_to_pragma_eol (parser);
13374 return NULL_TREE;
13376 else if (c_parser_next_token_is (parser, CPP_NAME))
13378 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13379 if (strcmp (p, "sections") == 0)
13381 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13382 if (cclauses == NULL)
13383 cclauses = cclauses_buf;
13385 c_parser_consume_token (parser);
13386 block = c_begin_omp_parallel ();
13387 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
13388 stmt = c_finish_omp_parallel (loc,
13389 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13390 block);
13391 OMP_PARALLEL_COMBINED (stmt) = 1;
13392 return stmt;
13396 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13398 block = c_begin_omp_parallel ();
13399 c_parser_statement (parser);
13400 stmt = c_finish_omp_parallel (loc, clauses, block);
13402 return stmt;
13405 /* OpenMP 2.5:
13406 # pragma omp single single-clause[optseq] new-line
13407 structured-block
13409 LOC is the location of the #pragma.
13412 #define OMP_SINGLE_CLAUSE_MASK \
13413 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
13416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13418 static tree
13419 c_parser_omp_single (location_t loc, c_parser *parser)
13421 tree stmt = make_node (OMP_SINGLE);
13422 SET_EXPR_LOCATION (stmt, loc);
13423 TREE_TYPE (stmt) = void_type_node;
13425 OMP_SINGLE_CLAUSES (stmt)
13426 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
13427 "#pragma omp single");
13428 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
13430 return add_stmt (stmt);
13433 /* OpenMP 3.0:
13434 # pragma omp task task-clause[optseq] new-line
13436 LOC is the location of the #pragma.
13439 #define OMP_TASK_CLAUSE_MASK \
13440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
13442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
13447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
13448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
13450 static tree
13451 c_parser_omp_task (location_t loc, c_parser *parser)
13453 tree clauses, block;
13455 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
13456 "#pragma omp task");
13458 block = c_begin_omp_task ();
13459 c_parser_statement (parser);
13460 return c_finish_omp_task (loc, clauses, block);
13463 /* OpenMP 3.0:
13464 # pragma omp taskwait new-line
13467 static void
13468 c_parser_omp_taskwait (c_parser *parser)
13470 location_t loc = c_parser_peek_token (parser)->location;
13471 c_parser_consume_pragma (parser);
13472 c_parser_skip_to_pragma_eol (parser);
13474 c_finish_omp_taskwait (loc);
13477 /* OpenMP 3.1:
13478 # pragma omp taskyield new-line
13481 static void
13482 c_parser_omp_taskyield (c_parser *parser)
13484 location_t loc = c_parser_peek_token (parser)->location;
13485 c_parser_consume_pragma (parser);
13486 c_parser_skip_to_pragma_eol (parser);
13488 c_finish_omp_taskyield (loc);
13491 /* OpenMP 4.0:
13492 # pragma omp taskgroup new-line
13495 static tree
13496 c_parser_omp_taskgroup (c_parser *parser)
13498 location_t loc = c_parser_peek_token (parser)->location;
13499 c_parser_skip_to_pragma_eol (parser);
13500 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
13503 /* OpenMP 4.0:
13504 # pragma omp cancel cancel-clause[optseq] new-line
13506 LOC is the location of the #pragma.
13509 #define OMP_CANCEL_CLAUSE_MASK \
13510 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
13514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13516 static void
13517 c_parser_omp_cancel (c_parser *parser)
13519 location_t loc = c_parser_peek_token (parser)->location;
13521 c_parser_consume_pragma (parser);
13522 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
13523 "#pragma omp cancel");
13525 c_finish_omp_cancel (loc, clauses);
13528 /* OpenMP 4.0:
13529 # pragma omp cancellation point cancelpt-clause[optseq] new-line
13531 LOC is the location of the #pragma.
13534 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
13535 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
13540 static void
13541 c_parser_omp_cancellation_point (c_parser *parser)
13543 location_t loc = c_parser_peek_token (parser)->location;
13544 tree clauses;
13545 bool point_seen = false;
13547 c_parser_consume_pragma (parser);
13548 if (c_parser_next_token_is (parser, CPP_NAME))
13550 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13551 if (strcmp (p, "point") == 0)
13553 c_parser_consume_token (parser);
13554 point_seen = true;
13557 if (!point_seen)
13559 c_parser_error (parser, "expected %<point%>");
13560 c_parser_skip_to_pragma_eol (parser);
13561 return;
13564 clauses
13565 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
13566 "#pragma omp cancellation point");
13568 c_finish_omp_cancellation_point (loc, clauses);
13571 /* OpenMP 4.0:
13572 #pragma omp distribute distribute-clause[optseq] new-line
13573 for-loop */
13575 #define OMP_DISTRIBUTE_CLAUSE_MASK \
13576 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
13579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13581 static tree
13582 c_parser_omp_distribute (location_t loc, c_parser *parser,
13583 char *p_name, omp_clause_mask mask, tree *cclauses)
13585 tree clauses, block, ret;
13587 strcat (p_name, " distribute");
13588 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
13590 if (c_parser_next_token_is (parser, CPP_NAME))
13592 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13593 bool simd = false;
13594 bool parallel = false;
13596 if (strcmp (p, "simd") == 0)
13597 simd = true;
13598 else
13599 parallel = strcmp (p, "parallel") == 0;
13600 if (parallel || simd)
13602 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13603 if (cclauses == NULL)
13604 cclauses = cclauses_buf;
13605 c_parser_consume_token (parser);
13606 if (!flag_openmp) /* flag_openmp_simd */
13608 if (simd)
13609 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13610 else
13611 return c_parser_omp_parallel (loc, parser, p_name, mask,
13612 cclauses);
13614 block = c_begin_compound_stmt (true);
13615 if (simd)
13616 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13617 else
13618 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
13619 block = c_end_compound_stmt (loc, block, true);
13620 if (ret == NULL)
13621 return ret;
13622 ret = make_node (OMP_DISTRIBUTE);
13623 TREE_TYPE (ret) = void_type_node;
13624 OMP_FOR_BODY (ret) = block;
13625 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13626 SET_EXPR_LOCATION (ret, loc);
13627 add_stmt (ret);
13628 return ret;
13631 if (!flag_openmp) /* flag_openmp_simd */
13633 c_parser_skip_to_pragma_eol (parser);
13634 return NULL_TREE;
13637 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13638 if (cclauses)
13640 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
13641 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13644 block = c_begin_compound_stmt (true);
13645 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
13646 block = c_end_compound_stmt (loc, block, true);
13647 add_stmt (block);
13649 return ret;
13652 /* OpenMP 4.0:
13653 # pragma omp teams teams-clause[optseq] new-line
13654 structured-block */
13656 #define OMP_TEAMS_CLAUSE_MASK \
13657 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
13662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
13663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
13665 static tree
13666 c_parser_omp_teams (location_t loc, c_parser *parser,
13667 char *p_name, omp_clause_mask mask, tree *cclauses)
13669 tree clauses, block, ret;
13671 strcat (p_name, " teams");
13672 mask |= OMP_TEAMS_CLAUSE_MASK;
13674 if (c_parser_next_token_is (parser, CPP_NAME))
13676 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13677 if (strcmp (p, "distribute") == 0)
13679 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13680 if (cclauses == NULL)
13681 cclauses = cclauses_buf;
13683 c_parser_consume_token (parser);
13684 if (!flag_openmp) /* flag_openmp_simd */
13685 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13686 block = c_begin_compound_stmt (true);
13687 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13688 block = c_end_compound_stmt (loc, block, true);
13689 if (ret == NULL)
13690 return ret;
13691 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13692 ret = make_node (OMP_TEAMS);
13693 TREE_TYPE (ret) = void_type_node;
13694 OMP_TEAMS_CLAUSES (ret) = clauses;
13695 OMP_TEAMS_BODY (ret) = block;
13696 return add_stmt (ret);
13699 if (!flag_openmp) /* flag_openmp_simd */
13701 c_parser_skip_to_pragma_eol (parser);
13702 return NULL_TREE;
13705 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13706 if (cclauses)
13708 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
13709 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13712 tree stmt = make_node (OMP_TEAMS);
13713 TREE_TYPE (stmt) = void_type_node;
13714 OMP_TEAMS_CLAUSES (stmt) = clauses;
13715 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
13717 return add_stmt (stmt);
13720 /* OpenMP 4.0:
13721 # pragma omp target data target-data-clause[optseq] new-line
13722 structured-block */
13724 #define OMP_TARGET_DATA_CLAUSE_MASK \
13725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13729 static tree
13730 c_parser_omp_target_data (location_t loc, c_parser *parser)
13732 tree stmt = make_node (OMP_TARGET_DATA);
13733 TREE_TYPE (stmt) = void_type_node;
13735 OMP_TARGET_DATA_CLAUSES (stmt)
13736 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
13737 "#pragma omp target data");
13738 keep_next_level ();
13739 tree block = c_begin_compound_stmt (true);
13740 add_stmt (c_parser_omp_structured_block (parser));
13741 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13743 SET_EXPR_LOCATION (stmt, loc);
13744 return add_stmt (stmt);
13747 /* OpenMP 4.0:
13748 # pragma omp target update target-update-clause[optseq] new-line */
13750 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
13751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
13752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
13753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13756 static bool
13757 c_parser_omp_target_update (location_t loc, c_parser *parser,
13758 enum pragma_context context)
13760 if (context == pragma_stmt)
13762 error_at (loc,
13763 "%<#pragma omp target update%> may only be "
13764 "used in compound statements");
13765 c_parser_skip_to_pragma_eol (parser);
13766 return false;
13769 tree clauses
13770 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
13771 "#pragma omp target update");
13772 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
13773 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
13775 error_at (loc,
13776 "%<#pragma omp target update must contain at least one "
13777 "%<from%> or %<to%> clauses");
13778 return false;
13781 tree stmt = make_node (OMP_TARGET_UPDATE);
13782 TREE_TYPE (stmt) = void_type_node;
13783 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
13784 SET_EXPR_LOCATION (stmt, loc);
13785 add_stmt (stmt);
13786 return false;
13789 /* OpenMP 4.0:
13790 # pragma omp target target-clause[optseq] new-line
13791 structured-block */
13793 #define OMP_TARGET_CLAUSE_MASK \
13794 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13798 static bool
13799 c_parser_omp_target (c_parser *parser, enum pragma_context context)
13801 location_t loc = c_parser_peek_token (parser)->location;
13802 c_parser_consume_pragma (parser);
13804 if (context != pragma_stmt && context != pragma_compound)
13806 c_parser_error (parser, "expected declaration specifiers");
13807 c_parser_skip_to_pragma_eol (parser);
13808 return false;
13811 if (c_parser_next_token_is (parser, CPP_NAME))
13813 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13815 if (strcmp (p, "teams") == 0)
13817 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
13818 char p_name[sizeof ("#pragma omp target teams distribute "
13819 "parallel for simd")];
13821 c_parser_consume_token (parser);
13822 strcpy (p_name, "#pragma omp target");
13823 if (!flag_openmp) /* flag_openmp_simd */
13825 tree stmt = c_parser_omp_teams (loc, parser, p_name,
13826 OMP_TARGET_CLAUSE_MASK,
13827 cclauses);
13828 return stmt != NULL_TREE;
13830 keep_next_level ();
13831 tree block = c_begin_compound_stmt (true);
13832 tree ret = c_parser_omp_teams (loc, parser, p_name,
13833 OMP_TARGET_CLAUSE_MASK, cclauses);
13834 block = c_end_compound_stmt (loc, block, true);
13835 if (ret == NULL_TREE)
13836 return false;
13837 tree stmt = make_node (OMP_TARGET);
13838 TREE_TYPE (stmt) = void_type_node;
13839 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
13840 OMP_TARGET_BODY (stmt) = block;
13841 add_stmt (stmt);
13842 return true;
13844 else if (!flag_openmp) /* flag_openmp_simd */
13846 c_parser_skip_to_pragma_eol (parser);
13847 return false;
13849 else if (strcmp (p, "data") == 0)
13851 c_parser_consume_token (parser);
13852 c_parser_omp_target_data (loc, parser);
13853 return true;
13855 else if (strcmp (p, "update") == 0)
13857 c_parser_consume_token (parser);
13858 return c_parser_omp_target_update (loc, parser, context);
13862 tree stmt = make_node (OMP_TARGET);
13863 TREE_TYPE (stmt) = void_type_node;
13865 OMP_TARGET_CLAUSES (stmt)
13866 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
13867 "#pragma omp target");
13868 keep_next_level ();
13869 tree block = c_begin_compound_stmt (true);
13870 add_stmt (c_parser_omp_structured_block (parser));
13871 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13873 SET_EXPR_LOCATION (stmt, loc);
13874 add_stmt (stmt);
13875 return true;
13878 /* OpenMP 4.0:
13879 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
13881 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
13882 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
13883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
13886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
13887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
13889 static void
13890 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
13892 vec<c_token> clauses = vNULL;
13893 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13895 c_token *token = c_parser_peek_token (parser);
13896 if (token->type == CPP_EOF)
13898 c_parser_skip_to_pragma_eol (parser);
13899 clauses.release ();
13900 return;
13902 clauses.safe_push (*token);
13903 c_parser_consume_token (parser);
13905 clauses.safe_push (*c_parser_peek_token (parser));
13906 c_parser_skip_to_pragma_eol (parser);
13908 while (c_parser_next_token_is (parser, CPP_PRAGMA))
13910 if (c_parser_peek_token (parser)->pragma_kind
13911 != PRAGMA_OMP_DECLARE_REDUCTION
13912 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
13913 || strcmp (IDENTIFIER_POINTER
13914 (c_parser_peek_2nd_token (parser)->value),
13915 "simd") != 0)
13917 c_parser_error (parser,
13918 "%<#pragma omp declare simd%> must be followed by "
13919 "function declaration or definition or another "
13920 "%<#pragma omp declare simd%>");
13921 clauses.release ();
13922 return;
13924 c_parser_consume_pragma (parser);
13925 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13927 c_token *token = c_parser_peek_token (parser);
13928 if (token->type == CPP_EOF)
13930 c_parser_skip_to_pragma_eol (parser);
13931 clauses.release ();
13932 return;
13934 clauses.safe_push (*token);
13935 c_parser_consume_token (parser);
13937 clauses.safe_push (*c_parser_peek_token (parser));
13938 c_parser_skip_to_pragma_eol (parser);
13941 /* Make sure nothing tries to read past the end of the tokens. */
13942 c_token eof_token;
13943 memset (&eof_token, 0, sizeof (eof_token));
13944 eof_token.type = CPP_EOF;
13945 clauses.safe_push (eof_token);
13946 clauses.safe_push (eof_token);
13948 switch (context)
13950 case pragma_external:
13951 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13952 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13954 int ext = disable_extension_diagnostics ();
13956 c_parser_consume_token (parser);
13957 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13958 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13959 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13960 NULL, clauses);
13961 restore_extension_diagnostics (ext);
13963 else
13964 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13965 NULL, clauses);
13966 break;
13967 case pragma_struct:
13968 case pragma_param:
13969 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13970 "function declaration or definition");
13971 break;
13972 case pragma_compound:
13973 case pragma_stmt:
13974 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13975 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13977 int ext = disable_extension_diagnostics ();
13979 c_parser_consume_token (parser);
13980 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13981 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13982 if (c_parser_next_tokens_start_declaration (parser))
13984 c_parser_declaration_or_fndef (parser, true, true, true, true,
13985 true, NULL, clauses);
13986 restore_extension_diagnostics (ext);
13987 break;
13989 restore_extension_diagnostics (ext);
13991 else if (c_parser_next_tokens_start_declaration (parser))
13993 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
13994 NULL, clauses);
13995 break;
13997 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13998 "function declaration or definition");
13999 break;
14000 default:
14001 gcc_unreachable ();
14003 clauses.release ();
14006 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
14007 and put that into "omp declare simd" attribute. */
14009 static void
14010 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
14011 vec<c_token> clauses)
14013 if (flag_cilkplus
14014 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14016 error ("%<#pragma omp declare simd%> cannot be used in the same "
14017 "function marked as a Cilk Plus SIMD-enabled function");
14018 vec_free (parser->cilk_simd_fn_tokens);
14019 return;
14022 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
14023 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
14024 has already processed the tokens. */
14025 if (clauses.exists () && clauses[0].type == CPP_EOF)
14026 return;
14027 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14029 error ("%<#pragma omp declare simd%> not immediately followed by "
14030 "a function declaration or definition");
14031 clauses[0].type = CPP_EOF;
14032 return;
14034 if (clauses.exists () && clauses[0].type != CPP_NAME)
14036 error_at (DECL_SOURCE_LOCATION (fndecl),
14037 "%<#pragma omp declare simd%> not immediately followed by "
14038 "a single function declaration or definition");
14039 clauses[0].type = CPP_EOF;
14040 return;
14043 if (parms == NULL_TREE)
14044 parms = DECL_ARGUMENTS (fndecl);
14046 unsigned int tokens_avail = parser->tokens_avail;
14047 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14048 bool is_cilkplus_cilk_simd_fn = false;
14050 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14052 parser->tokens = parser->cilk_simd_fn_tokens->address ();
14053 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
14054 is_cilkplus_cilk_simd_fn = true;
14056 else
14058 parser->tokens = clauses.address ();
14059 parser->tokens_avail = clauses.length ();
14062 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
14063 while (parser->tokens_avail > 3)
14065 c_token *token = c_parser_peek_token (parser);
14066 if (!is_cilkplus_cilk_simd_fn)
14067 gcc_assert (token->type == CPP_NAME
14068 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
14069 else
14070 gcc_assert (token->type == CPP_NAME
14071 && is_cilkplus_vector_p (token->value));
14072 c_parser_consume_token (parser);
14073 parser->in_pragma = true;
14075 tree c = NULL_TREE;
14076 if (is_cilkplus_cilk_simd_fn)
14077 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
14078 "SIMD-enabled functions attribute");
14079 else
14080 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
14081 "#pragma omp declare simd");
14082 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
14083 if (c != NULL_TREE)
14084 c = tree_cons (NULL_TREE, c, NULL_TREE);
14085 if (is_cilkplus_cilk_simd_fn)
14087 tree k = build_tree_list (get_identifier ("cilk simd function"),
14088 NULL_TREE);
14089 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
14090 DECL_ATTRIBUTES (fndecl) = k;
14092 c = build_tree_list (get_identifier ("omp declare simd"), c);
14093 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
14094 DECL_ATTRIBUTES (fndecl) = c;
14097 parser->tokens = &parser->tokens_buf[0];
14098 parser->tokens_avail = tokens_avail;
14099 if (clauses.exists ())
14100 clauses[0].type = CPP_PRAGMA;
14102 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14103 vec_free (parser->cilk_simd_fn_tokens);
14107 /* OpenMP 4.0:
14108 # pragma omp declare target new-line
14109 declarations and definitions
14110 # pragma omp end declare target new-line */
14112 static void
14113 c_parser_omp_declare_target (c_parser *parser)
14115 c_parser_skip_to_pragma_eol (parser);
14116 current_omp_declare_target_attribute++;
14119 static void
14120 c_parser_omp_end_declare_target (c_parser *parser)
14122 location_t loc = c_parser_peek_token (parser)->location;
14123 c_parser_consume_pragma (parser);
14124 if (c_parser_next_token_is (parser, CPP_NAME)
14125 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14126 "declare") == 0)
14128 c_parser_consume_token (parser);
14129 if (c_parser_next_token_is (parser, CPP_NAME)
14130 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14131 "target") == 0)
14132 c_parser_consume_token (parser);
14133 else
14135 c_parser_error (parser, "expected %<target%>");
14136 c_parser_skip_to_pragma_eol (parser);
14137 return;
14140 else
14142 c_parser_error (parser, "expected %<declare%>");
14143 c_parser_skip_to_pragma_eol (parser);
14144 return;
14146 c_parser_skip_to_pragma_eol (parser);
14147 if (!current_omp_declare_target_attribute)
14148 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
14149 "%<#pragma omp declare target%>");
14150 else
14151 current_omp_declare_target_attribute--;
14155 /* OpenMP 4.0
14156 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14157 initializer-clause[opt] new-line
14159 initializer-clause:
14160 initializer (omp_priv = initializer)
14161 initializer (function-name (argument-list)) */
14163 static void
14164 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
14166 unsigned int tokens_avail = 0, i;
14167 vec<tree> types = vNULL;
14168 vec<c_token> clauses = vNULL;
14169 enum tree_code reduc_code = ERROR_MARK;
14170 tree reduc_id = NULL_TREE;
14171 tree type;
14172 location_t rloc = c_parser_peek_token (parser)->location;
14174 if (context == pragma_struct || context == pragma_param)
14176 error ("%<#pragma omp declare reduction%> not at file or block scope");
14177 goto fail;
14180 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14181 goto fail;
14183 switch (c_parser_peek_token (parser)->type)
14185 case CPP_PLUS:
14186 reduc_code = PLUS_EXPR;
14187 break;
14188 case CPP_MULT:
14189 reduc_code = MULT_EXPR;
14190 break;
14191 case CPP_MINUS:
14192 reduc_code = MINUS_EXPR;
14193 break;
14194 case CPP_AND:
14195 reduc_code = BIT_AND_EXPR;
14196 break;
14197 case CPP_XOR:
14198 reduc_code = BIT_XOR_EXPR;
14199 break;
14200 case CPP_OR:
14201 reduc_code = BIT_IOR_EXPR;
14202 break;
14203 case CPP_AND_AND:
14204 reduc_code = TRUTH_ANDIF_EXPR;
14205 break;
14206 case CPP_OR_OR:
14207 reduc_code = TRUTH_ORIF_EXPR;
14208 break;
14209 case CPP_NAME:
14210 const char *p;
14211 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14212 if (strcmp (p, "min") == 0)
14214 reduc_code = MIN_EXPR;
14215 break;
14217 if (strcmp (p, "max") == 0)
14219 reduc_code = MAX_EXPR;
14220 break;
14222 reduc_id = c_parser_peek_token (parser)->value;
14223 break;
14224 default:
14225 c_parser_error (parser,
14226 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
14227 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
14228 goto fail;
14231 tree orig_reduc_id, reduc_decl;
14232 orig_reduc_id = reduc_id;
14233 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
14234 reduc_decl = c_omp_reduction_decl (reduc_id);
14235 c_parser_consume_token (parser);
14237 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14238 goto fail;
14240 while (true)
14242 location_t loc = c_parser_peek_token (parser)->location;
14243 struct c_type_name *ctype = c_parser_type_name (parser);
14244 if (ctype != NULL)
14246 type = groktypename (ctype, NULL, NULL);
14247 if (type == error_mark_node)
14249 else if ((INTEGRAL_TYPE_P (type)
14250 || TREE_CODE (type) == REAL_TYPE
14251 || TREE_CODE (type) == COMPLEX_TYPE)
14252 && orig_reduc_id == NULL_TREE)
14253 error_at (loc, "predeclared arithmetic type in "
14254 "%<#pragma omp declare reduction%>");
14255 else if (TREE_CODE (type) == FUNCTION_TYPE
14256 || TREE_CODE (type) == ARRAY_TYPE)
14257 error_at (loc, "function or array type in "
14258 "%<#pragma omp declare reduction%>");
14259 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
14260 error_at (loc, "const, volatile or restrict qualified type in "
14261 "%<#pragma omp declare reduction%>");
14262 else
14264 tree t;
14265 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
14266 if (comptypes (TREE_PURPOSE (t), type))
14268 error_at (loc, "redeclaration of %qs "
14269 "%<#pragma omp declare reduction%> for "
14270 "type %qT",
14271 IDENTIFIER_POINTER (reduc_id)
14272 + sizeof ("omp declare reduction ") - 1,
14273 type);
14274 location_t ploc
14275 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
14276 0));
14277 error_at (ploc, "previous %<#pragma omp declare "
14278 "reduction%>");
14279 break;
14281 if (t == NULL_TREE)
14282 types.safe_push (type);
14284 if (c_parser_next_token_is (parser, CPP_COMMA))
14285 c_parser_consume_token (parser);
14286 else
14287 break;
14289 else
14290 break;
14293 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
14294 || types.is_empty ())
14296 fail:
14297 clauses.release ();
14298 types.release ();
14299 while (true)
14301 c_token *token = c_parser_peek_token (parser);
14302 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
14303 break;
14304 c_parser_consume_token (parser);
14306 c_parser_skip_to_pragma_eol (parser);
14307 return;
14310 if (types.length () > 1)
14312 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14314 c_token *token = c_parser_peek_token (parser);
14315 if (token->type == CPP_EOF)
14316 goto fail;
14317 clauses.safe_push (*token);
14318 c_parser_consume_token (parser);
14320 clauses.safe_push (*c_parser_peek_token (parser));
14321 c_parser_skip_to_pragma_eol (parser);
14323 /* Make sure nothing tries to read past the end of the tokens. */
14324 c_token eof_token;
14325 memset (&eof_token, 0, sizeof (eof_token));
14326 eof_token.type = CPP_EOF;
14327 clauses.safe_push (eof_token);
14328 clauses.safe_push (eof_token);
14331 int errs = errorcount;
14332 FOR_EACH_VEC_ELT (types, i, type)
14334 tokens_avail = parser->tokens_avail;
14335 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14336 if (!clauses.is_empty ())
14338 parser->tokens = clauses.address ();
14339 parser->tokens_avail = clauses.length ();
14340 parser->in_pragma = true;
14343 bool nested = current_function_decl != NULL_TREE;
14344 if (nested)
14345 c_push_function_context ();
14346 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
14347 reduc_id, default_function_type);
14348 current_function_decl = fndecl;
14349 allocate_struct_function (fndecl, true);
14350 push_scope ();
14351 tree stmt = push_stmt_list ();
14352 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
14353 warn about these. */
14354 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
14355 get_identifier ("omp_out"), type);
14356 DECL_ARTIFICIAL (omp_out) = 1;
14357 DECL_CONTEXT (omp_out) = fndecl;
14358 pushdecl (omp_out);
14359 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
14360 get_identifier ("omp_in"), type);
14361 DECL_ARTIFICIAL (omp_in) = 1;
14362 DECL_CONTEXT (omp_in) = fndecl;
14363 pushdecl (omp_in);
14364 struct c_expr combiner = c_parser_expression (parser);
14365 struct c_expr initializer;
14366 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
14367 bool bad = false;
14368 initializer.value = error_mark_node;
14369 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14370 bad = true;
14371 else if (c_parser_next_token_is (parser, CPP_NAME)
14372 && strcmp (IDENTIFIER_POINTER
14373 (c_parser_peek_token (parser)->value),
14374 "initializer") == 0)
14376 c_parser_consume_token (parser);
14377 pop_scope ();
14378 push_scope ();
14379 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
14380 get_identifier ("omp_priv"), type);
14381 DECL_ARTIFICIAL (omp_priv) = 1;
14382 DECL_INITIAL (omp_priv) = error_mark_node;
14383 DECL_CONTEXT (omp_priv) = fndecl;
14384 pushdecl (omp_priv);
14385 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
14386 get_identifier ("omp_orig"), type);
14387 DECL_ARTIFICIAL (omp_orig) = 1;
14388 DECL_CONTEXT (omp_orig) = fndecl;
14389 pushdecl (omp_orig);
14390 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14391 bad = true;
14392 else if (!c_parser_next_token_is (parser, CPP_NAME))
14394 c_parser_error (parser, "expected %<omp_priv%> or "
14395 "function-name");
14396 bad = true;
14398 else if (strcmp (IDENTIFIER_POINTER
14399 (c_parser_peek_token (parser)->value),
14400 "omp_priv") != 0)
14402 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
14403 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14405 c_parser_error (parser, "expected function-name %<(%>");
14406 bad = true;
14408 else
14409 initializer = c_parser_postfix_expression (parser);
14410 if (initializer.value
14411 && TREE_CODE (initializer.value) == CALL_EXPR)
14413 int j;
14414 tree c = initializer.value;
14415 for (j = 0; j < call_expr_nargs (c); j++)
14416 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
14417 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
14418 break;
14419 if (j == call_expr_nargs (c))
14420 error ("one of the initializer call arguments should be "
14421 "%<&omp_priv%>");
14424 else
14426 c_parser_consume_token (parser);
14427 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14428 bad = true;
14429 else
14431 tree st = push_stmt_list ();
14432 start_init (omp_priv, NULL_TREE, 0);
14433 location_t loc = c_parser_peek_token (parser)->location;
14434 struct c_expr init = c_parser_initializer (parser);
14435 finish_init ();
14436 finish_decl (omp_priv, loc, init.value,
14437 init.original_type, NULL_TREE);
14438 pop_stmt_list (st);
14441 if (!bad
14442 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14443 bad = true;
14446 if (!bad)
14448 c_parser_skip_to_pragma_eol (parser);
14450 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
14451 DECL_INITIAL (reduc_decl));
14452 DECL_INITIAL (reduc_decl) = t;
14453 DECL_SOURCE_LOCATION (omp_out) = rloc;
14454 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
14455 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
14456 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
14457 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
14458 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
14459 if (omp_priv)
14461 DECL_SOURCE_LOCATION (omp_priv) = rloc;
14462 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
14463 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
14464 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
14465 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
14466 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14467 walk_tree (&DECL_INITIAL (omp_priv),
14468 c_check_omp_declare_reduction_r,
14469 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14473 pop_stmt_list (stmt);
14474 pop_scope ();
14475 if (cfun->language != NULL)
14477 ggc_free (cfun->language);
14478 cfun->language = NULL;
14480 set_cfun (NULL);
14481 current_function_decl = NULL_TREE;
14482 if (nested)
14483 c_pop_function_context ();
14485 if (!clauses.is_empty ())
14487 parser->tokens = &parser->tokens_buf[0];
14488 parser->tokens_avail = tokens_avail;
14490 if (bad)
14491 goto fail;
14492 if (errs != errorcount)
14493 break;
14496 clauses.release ();
14497 types.release ();
14501 /* OpenMP 4.0
14502 #pragma omp declare simd declare-simd-clauses[optseq] new-line
14503 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14504 initializer-clause[opt] new-line
14505 #pragma omp declare target new-line */
14507 static void
14508 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
14510 c_parser_consume_pragma (parser);
14511 if (c_parser_next_token_is (parser, CPP_NAME))
14513 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14514 if (strcmp (p, "simd") == 0)
14516 /* c_parser_consume_token (parser); done in
14517 c_parser_omp_declare_simd. */
14518 c_parser_omp_declare_simd (parser, context);
14519 return;
14521 if (strcmp (p, "reduction") == 0)
14523 c_parser_consume_token (parser);
14524 c_parser_omp_declare_reduction (parser, context);
14525 return;
14527 if (!flag_openmp) /* flag_openmp_simd */
14529 c_parser_skip_to_pragma_eol (parser);
14530 return;
14532 if (strcmp (p, "target") == 0)
14534 c_parser_consume_token (parser);
14535 c_parser_omp_declare_target (parser);
14536 return;
14540 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
14541 "or %<target%>");
14542 c_parser_skip_to_pragma_eol (parser);
14545 /* Main entry point to parsing most OpenMP pragmas. */
14547 static void
14548 c_parser_omp_construct (c_parser *parser)
14550 enum pragma_kind p_kind;
14551 location_t loc;
14552 tree stmt;
14553 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
14554 omp_clause_mask mask (0);
14556 loc = c_parser_peek_token (parser)->location;
14557 p_kind = c_parser_peek_token (parser)->pragma_kind;
14558 c_parser_consume_pragma (parser);
14560 switch (p_kind)
14562 case PRAGMA_OACC_CACHE:
14563 strcpy (p_name, "#pragma acc");
14564 stmt = c_parser_oacc_cache (loc, parser);
14565 break;
14566 case PRAGMA_OACC_DATA:
14567 stmt = c_parser_oacc_data (loc, parser);
14568 break;
14569 case PRAGMA_OACC_KERNELS:
14570 strcpy (p_name, "#pragma acc");
14571 stmt = c_parser_oacc_kernels (loc, parser, p_name);
14572 break;
14573 case PRAGMA_OACC_LOOP:
14574 strcpy (p_name, "#pragma acc");
14575 stmt = c_parser_oacc_loop (loc, parser, p_name);
14576 break;
14577 case PRAGMA_OACC_PARALLEL:
14578 strcpy (p_name, "#pragma acc");
14579 stmt = c_parser_oacc_parallel (loc, parser, p_name);
14580 break;
14581 case PRAGMA_OACC_WAIT:
14582 strcpy (p_name, "#pragma wait");
14583 stmt = c_parser_oacc_wait (loc, parser, p_name);
14584 break;
14585 case PRAGMA_OMP_ATOMIC:
14586 c_parser_omp_atomic (loc, parser);
14587 return;
14588 case PRAGMA_OMP_CRITICAL:
14589 stmt = c_parser_omp_critical (loc, parser);
14590 break;
14591 case PRAGMA_OMP_DISTRIBUTE:
14592 strcpy (p_name, "#pragma omp");
14593 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
14594 break;
14595 case PRAGMA_OMP_FOR:
14596 strcpy (p_name, "#pragma omp");
14597 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
14598 break;
14599 case PRAGMA_OMP_MASTER:
14600 stmt = c_parser_omp_master (loc, parser);
14601 break;
14602 case PRAGMA_OMP_ORDERED:
14603 stmt = c_parser_omp_ordered (loc, parser);
14604 break;
14605 case PRAGMA_OMP_PARALLEL:
14606 strcpy (p_name, "#pragma omp");
14607 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
14608 break;
14609 case PRAGMA_OMP_SECTIONS:
14610 strcpy (p_name, "#pragma omp");
14611 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
14612 break;
14613 case PRAGMA_OMP_SIMD:
14614 strcpy (p_name, "#pragma omp");
14615 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
14616 break;
14617 case PRAGMA_OMP_SINGLE:
14618 stmt = c_parser_omp_single (loc, parser);
14619 break;
14620 case PRAGMA_OMP_TASK:
14621 stmt = c_parser_omp_task (loc, parser);
14622 break;
14623 case PRAGMA_OMP_TASKGROUP:
14624 stmt = c_parser_omp_taskgroup (parser);
14625 break;
14626 case PRAGMA_OMP_TEAMS:
14627 strcpy (p_name, "#pragma omp");
14628 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
14629 break;
14630 default:
14631 gcc_unreachable ();
14634 if (stmt)
14635 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
14639 /* OpenMP 2.5:
14640 # pragma omp threadprivate (variable-list) */
14642 static void
14643 c_parser_omp_threadprivate (c_parser *parser)
14645 tree vars, t;
14646 location_t loc;
14648 c_parser_consume_pragma (parser);
14649 loc = c_parser_peek_token (parser)->location;
14650 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14652 /* Mark every variable in VARS to be assigned thread local storage. */
14653 for (t = vars; t; t = TREE_CHAIN (t))
14655 tree v = TREE_PURPOSE (t);
14657 /* FIXME diagnostics: Ideally we should keep individual
14658 locations for all the variables in the var list to make the
14659 following errors more precise. Perhaps
14660 c_parser_omp_var_list_parens() should construct a list of
14661 locations to go along with the var list. */
14663 /* If V had already been marked threadprivate, it doesn't matter
14664 whether it had been used prior to this point. */
14665 if (TREE_CODE (v) != VAR_DECL)
14666 error_at (loc, "%qD is not a variable", v);
14667 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
14668 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
14669 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
14670 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
14671 else if (TREE_TYPE (v) == error_mark_node)
14673 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
14674 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
14675 else
14677 if (! DECL_THREAD_LOCAL_P (v))
14679 set_decl_tls_model (v, decl_default_tls_model (v));
14680 /* If rtl has been already set for this var, call
14681 make_decl_rtl once again, so that encode_section_info
14682 has a chance to look at the new decl flags. */
14683 if (DECL_RTL_SET_P (v))
14684 make_decl_rtl (v);
14686 C_DECL_THREADPRIVATE_P (v) = 1;
14690 c_parser_skip_to_pragma_eol (parser);
14693 /* Cilk Plus <#pragma simd> parsing routines. */
14695 /* Helper function for c_parser_pragma. Perform some sanity checking
14696 for <#pragma simd> constructs. Returns FALSE if there was a
14697 problem. */
14699 static bool
14700 c_parser_cilk_verify_simd (c_parser *parser,
14701 enum pragma_context context)
14703 if (!flag_cilkplus)
14705 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
14706 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14707 return false;
14709 if (context == pragma_external)
14711 c_parser_error (parser,"pragma simd must be inside a function");
14712 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14713 return false;
14715 return true;
14718 /* Cilk Plus:
14719 This function is shared by SIMD-enabled functions and #pragma simd.
14720 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
14721 CLAUSES is unused. The main purpose of this function is to parse a
14722 vectorlength attribute or clause and check for parse errors.
14723 When IS_SIMD_FN is true then the function is merely caching the tokens
14724 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
14725 cache is cleared since there is no reason to continue.
14726 Syntax:
14727 vectorlength ( constant-expression ) */
14729 static tree
14730 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
14731 bool is_simd_fn)
14733 if (is_simd_fn)
14734 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
14735 else
14736 /* The vectorlength clause behaves exactly like OpenMP's safelen
14737 clause. Represent it in OpenMP terms. */
14738 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
14740 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14741 return clauses;
14743 location_t loc = c_parser_peek_token (parser)->location;
14744 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14745 expr = c_fully_fold (expr, false, NULL);
14747 /* If expr is an error_mark_node then the above function would have
14748 emitted an error. No reason to do it twice. */
14749 if (expr == error_mark_node)
14751 else if (!TREE_TYPE (expr)
14752 || !TREE_CONSTANT (expr)
14753 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14755 error_at (loc, "vectorlength must be an integer constant");
14756 else if (wi::exact_log2 (expr) == -1)
14757 error_at (loc, "vectorlength must be a power of 2");
14758 else
14760 if (is_simd_fn)
14762 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
14763 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
14764 OMP_CLAUSE_CHAIN (u) = clauses;
14765 clauses = u;
14767 else
14769 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
14770 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
14771 OMP_CLAUSE_CHAIN (u) = clauses;
14772 clauses = u;
14776 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14778 return clauses;
14781 /* Cilk Plus:
14782 linear ( simd-linear-variable-list )
14784 simd-linear-variable-list:
14785 simd-linear-variable
14786 simd-linear-variable-list , simd-linear-variable
14788 simd-linear-variable:
14789 id-expression
14790 id-expression : simd-linear-step
14792 simd-linear-step:
14793 conditional-expression */
14795 static tree
14796 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
14798 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14799 return clauses;
14801 location_t loc = c_parser_peek_token (parser)->location;
14803 if (c_parser_next_token_is_not (parser, CPP_NAME)
14804 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14805 c_parser_error (parser, "expected identifier");
14807 while (c_parser_next_token_is (parser, CPP_NAME)
14808 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14810 tree var = lookup_name (c_parser_peek_token (parser)->value);
14812 if (var == NULL)
14814 undeclared_variable (c_parser_peek_token (parser)->location,
14815 c_parser_peek_token (parser)->value);
14816 c_parser_consume_token (parser);
14818 else if (var == error_mark_node)
14819 c_parser_consume_token (parser);
14820 else
14822 tree step = integer_one_node;
14824 /* Parse the linear step if present. */
14825 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14827 c_parser_consume_token (parser);
14828 c_parser_consume_token (parser);
14830 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14831 expr = c_fully_fold (expr, false, NULL);
14833 if (TREE_TYPE (expr)
14834 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
14835 && (TREE_CONSTANT (expr)
14836 || DECL_P (expr)))
14837 step = expr;
14838 else
14839 c_parser_error (parser,
14840 "step size must be an integer constant "
14841 "expression or an integer variable");
14843 else
14844 c_parser_consume_token (parser);
14846 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
14847 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
14848 OMP_CLAUSE_DECL (u) = var;
14849 OMP_CLAUSE_LINEAR_STEP (u) = step;
14850 OMP_CLAUSE_CHAIN (u) = clauses;
14851 clauses = u;
14854 if (c_parser_next_token_is_not (parser, CPP_COMMA))
14855 break;
14857 c_parser_consume_token (parser);
14860 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14862 return clauses;
14865 /* Returns the name of the next clause. If the clause is not
14866 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
14867 not consumed. Otherwise, the appropriate pragma_simd_clause is
14868 returned and the token is consumed. */
14870 static pragma_omp_clause
14871 c_parser_cilk_clause_name (c_parser *parser)
14873 pragma_omp_clause result;
14874 c_token *token = c_parser_peek_token (parser);
14876 if (!token->value || token->type != CPP_NAME)
14877 return PRAGMA_CILK_CLAUSE_NONE;
14879 const char *p = IDENTIFIER_POINTER (token->value);
14881 if (!strcmp (p, "vectorlength"))
14882 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
14883 else if (!strcmp (p, "linear"))
14884 result = PRAGMA_CILK_CLAUSE_LINEAR;
14885 else if (!strcmp (p, "private"))
14886 result = PRAGMA_CILK_CLAUSE_PRIVATE;
14887 else if (!strcmp (p, "firstprivate"))
14888 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
14889 else if (!strcmp (p, "lastprivate"))
14890 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
14891 else if (!strcmp (p, "reduction"))
14892 result = PRAGMA_CILK_CLAUSE_REDUCTION;
14893 else
14894 return PRAGMA_CILK_CLAUSE_NONE;
14896 c_parser_consume_token (parser);
14897 return result;
14900 /* Parse all #<pragma simd> clauses. Return the list of clauses
14901 found. */
14903 static tree
14904 c_parser_cilk_all_clauses (c_parser *parser)
14906 tree clauses = NULL;
14908 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14910 pragma_omp_clause c_kind;
14912 c_kind = c_parser_cilk_clause_name (parser);
14914 switch (c_kind)
14916 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
14917 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
14918 break;
14919 case PRAGMA_CILK_CLAUSE_LINEAR:
14920 clauses = c_parser_cilk_clause_linear (parser, clauses);
14921 break;
14922 case PRAGMA_CILK_CLAUSE_PRIVATE:
14923 /* Use the OpenMP counterpart. */
14924 clauses = c_parser_omp_clause_private (parser, clauses);
14925 break;
14926 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
14927 /* Use the OpenMP counterpart. */
14928 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14929 break;
14930 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
14931 /* Use the OpenMP counterpart. */
14932 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14933 break;
14934 case PRAGMA_CILK_CLAUSE_REDUCTION:
14935 /* Use the OpenMP counterpart. */
14936 clauses = c_parser_omp_clause_reduction (parser, clauses);
14937 break;
14938 default:
14939 c_parser_error (parser, "expected %<#pragma simd%> clause");
14940 goto saw_error;
14944 saw_error:
14945 c_parser_skip_to_pragma_eol (parser);
14946 return c_finish_cilk_clauses (clauses);
14949 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
14950 Here is the correct syntax of this pragma:
14951 #pragma cilk grainsize = <EXP>
14954 static void
14955 c_parser_cilk_grainsize (c_parser *parser)
14957 extern tree convert_to_integer (tree, tree);
14959 /* consume the 'grainsize' keyword. */
14960 c_parser_consume_pragma (parser);
14962 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
14964 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
14965 if (g_expr.value == error_mark_node)
14967 c_parser_skip_to_pragma_eol (parser);
14968 return;
14970 tree grain = convert_to_integer (long_integer_type_node,
14971 c_fully_fold (g_expr.value, false,
14972 NULL));
14973 c_parser_skip_to_pragma_eol (parser);
14974 c_token *token = c_parser_peek_token (parser);
14975 if (token && token->type == CPP_KEYWORD
14976 && token->keyword == RID_CILK_FOR)
14978 if (grain == NULL_TREE || grain == error_mark_node)
14979 grain = integer_zero_node;
14980 c_parser_cilk_for (parser, grain);
14982 else
14983 warning (0, "%<#pragma cilk grainsize%> is not followed by "
14984 "%<_Cilk_for%>");
14986 else
14987 c_parser_skip_to_pragma_eol (parser);
14990 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
14992 static void
14993 c_parser_cilk_simd (c_parser *parser)
14995 tree clauses = c_parser_cilk_all_clauses (parser);
14996 tree block = c_begin_compound_stmt (true);
14997 location_t loc = c_parser_peek_token (parser)->location;
14998 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
14999 block = c_end_compound_stmt (loc, block, true);
15000 add_stmt (block);
15003 /* Create an artificial decl with TYPE and emit initialization of it with
15004 INIT. */
15006 static tree
15007 c_get_temp_regvar (tree type, tree init)
15009 location_t loc = EXPR_LOCATION (init);
15010 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
15011 DECL_ARTIFICIAL (decl) = 1;
15012 DECL_IGNORED_P (decl) = 1;
15013 pushdecl (decl);
15014 tree t = build2 (INIT_EXPR, type, decl, init);
15015 add_stmt (t);
15016 return decl;
15019 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
15020 GRAIN is the grain value passed in through pragma or 0. */
15022 static void
15023 c_parser_cilk_for (c_parser *parser, tree grain)
15025 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
15026 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
15027 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
15028 clauses = c_finish_omp_clauses (clauses);
15030 tree block = c_begin_compound_stmt (true);
15031 tree sb = push_stmt_list ();
15032 location_t loc = c_parser_peek_token (parser)->location;
15033 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
15034 sb = pop_stmt_list (sb);
15036 if (omp_for)
15038 tree omp_par = make_node (OMP_PARALLEL);
15039 TREE_TYPE (omp_par) = void_type_node;
15040 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
15041 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
15042 TREE_SIDE_EFFECTS (bind) = 1;
15043 BIND_EXPR_BODY (bind) = sb;
15044 OMP_PARALLEL_BODY (omp_par) = bind;
15045 if (OMP_FOR_PRE_BODY (omp_for))
15047 add_stmt (OMP_FOR_PRE_BODY (omp_for));
15048 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
15050 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
15051 tree decl = TREE_OPERAND (init, 0);
15052 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
15053 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
15054 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
15055 if (TREE_CODE (t) != INTEGER_CST)
15057 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15058 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15059 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
15060 OMP_CLAUSE_CHAIN (c) = clauses;
15061 clauses = c;
15063 if (TREE_CODE (incr) == MODIFY_EXPR)
15065 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15066 if (TREE_CODE (t) != INTEGER_CST)
15068 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
15069 = c_get_temp_regvar (TREE_TYPE (t), t);
15070 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15071 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15072 OMP_CLAUSE_CHAIN (c) = clauses;
15073 clauses = c;
15076 t = TREE_OPERAND (init, 1);
15077 if (TREE_CODE (t) != INTEGER_CST)
15079 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15080 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15081 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
15082 OMP_CLAUSE_CHAIN (c) = clauses;
15083 clauses = c;
15085 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15086 OMP_CLAUSE_DECL (c) = decl;
15087 OMP_CLAUSE_CHAIN (c) = clauses;
15088 clauses = c;
15089 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
15090 OMP_CLAUSE_OPERAND (c, 0)
15091 = cilk_for_number_of_iterations (omp_for);
15092 OMP_CLAUSE_CHAIN (c) = clauses;
15093 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c);
15094 add_stmt (omp_par);
15097 block = c_end_compound_stmt (loc, block, true);
15098 add_stmt (block);
15102 /* Parse a transaction attribute (GCC Extension).
15104 transaction-attribute:
15105 attributes
15106 [ [ any-word ] ]
15108 The transactional memory language description is written for C++,
15109 and uses the C++0x attribute syntax. For compatibility, allow the
15110 bracket style for transactions in C as well. */
15112 static tree
15113 c_parser_transaction_attributes (c_parser *parser)
15115 tree attr_name, attr = NULL;
15117 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
15118 return c_parser_attributes (parser);
15120 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
15121 return NULL_TREE;
15122 c_parser_consume_token (parser);
15123 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
15124 goto error1;
15126 attr_name = c_parser_attribute_any_word (parser);
15127 if (attr_name)
15129 c_parser_consume_token (parser);
15130 attr = build_tree_list (attr_name, NULL_TREE);
15132 else
15133 c_parser_error (parser, "expected identifier");
15135 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15136 error1:
15137 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15138 return attr;
15141 /* Parse a __transaction_atomic or __transaction_relaxed statement
15142 (GCC Extension).
15144 transaction-statement:
15145 __transaction_atomic transaction-attribute[opt] compound-statement
15146 __transaction_relaxed compound-statement
15148 Note that the only valid attribute is: "outer".
15151 static tree
15152 c_parser_transaction (c_parser *parser, enum rid keyword)
15154 unsigned int old_in = parser->in_transaction;
15155 unsigned int this_in = 1, new_in;
15156 location_t loc = c_parser_peek_token (parser)->location;
15157 tree stmt, attrs;
15159 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15160 || keyword == RID_TRANSACTION_RELAXED)
15161 && c_parser_next_token_is_keyword (parser, keyword));
15162 c_parser_consume_token (parser);
15164 if (keyword == RID_TRANSACTION_RELAXED)
15165 this_in |= TM_STMT_ATTR_RELAXED;
15166 else
15168 attrs = c_parser_transaction_attributes (parser);
15169 if (attrs)
15170 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
15173 /* Keep track if we're in the lexical scope of an outer transaction. */
15174 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
15176 parser->in_transaction = new_in;
15177 stmt = c_parser_compound_statement (parser);
15178 parser->in_transaction = old_in;
15180 if (flag_tm)
15181 stmt = c_finish_transaction (loc, stmt, this_in);
15182 else
15183 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15184 "%<__transaction_atomic%> without transactional memory support enabled"
15185 : "%<__transaction_relaxed %> "
15186 "without transactional memory support enabled"));
15188 return stmt;
15191 /* Parse a __transaction_atomic or __transaction_relaxed expression
15192 (GCC Extension).
15194 transaction-expression:
15195 __transaction_atomic ( expression )
15196 __transaction_relaxed ( expression )
15199 static struct c_expr
15200 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
15202 struct c_expr ret;
15203 unsigned int old_in = parser->in_transaction;
15204 unsigned int this_in = 1;
15205 location_t loc = c_parser_peek_token (parser)->location;
15206 tree attrs;
15208 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15209 || keyword == RID_TRANSACTION_RELAXED)
15210 && c_parser_next_token_is_keyword (parser, keyword));
15211 c_parser_consume_token (parser);
15213 if (keyword == RID_TRANSACTION_RELAXED)
15214 this_in |= TM_STMT_ATTR_RELAXED;
15215 else
15217 attrs = c_parser_transaction_attributes (parser);
15218 if (attrs)
15219 this_in |= parse_tm_stmt_attr (attrs, 0);
15222 parser->in_transaction = this_in;
15223 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
15225 tree expr = c_parser_expression (parser).value;
15226 ret.original_type = TREE_TYPE (expr);
15227 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
15228 if (this_in & TM_STMT_ATTR_RELAXED)
15229 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
15230 SET_EXPR_LOCATION (ret.value, loc);
15231 ret.original_code = TRANSACTION_EXPR;
15232 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15234 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
15235 goto error;
15238 else
15240 error:
15241 ret.value = error_mark_node;
15242 ret.original_code = ERROR_MARK;
15243 ret.original_type = NULL;
15245 parser->in_transaction = old_in;
15247 if (!flag_tm)
15248 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15249 "%<__transaction_atomic%> without transactional memory support enabled"
15250 : "%<__transaction_relaxed %> "
15251 "without transactional memory support enabled"));
15253 return ret;
15256 /* Parse a __transaction_cancel statement (GCC Extension).
15258 transaction-cancel-statement:
15259 __transaction_cancel transaction-attribute[opt] ;
15261 Note that the only valid attribute is "outer".
15264 static tree
15265 c_parser_transaction_cancel (c_parser *parser)
15267 location_t loc = c_parser_peek_token (parser)->location;
15268 tree attrs;
15269 bool is_outer = false;
15271 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
15272 c_parser_consume_token (parser);
15274 attrs = c_parser_transaction_attributes (parser);
15275 if (attrs)
15276 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
15278 if (!flag_tm)
15280 error_at (loc, "%<__transaction_cancel%> without "
15281 "transactional memory support enabled");
15282 goto ret_error;
15284 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
15286 error_at (loc, "%<__transaction_cancel%> within a "
15287 "%<__transaction_relaxed%>");
15288 goto ret_error;
15290 else if (is_outer)
15292 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
15293 && !is_tm_may_cancel_outer (current_function_decl))
15295 error_at (loc, "outer %<__transaction_cancel%> not "
15296 "within outer %<__transaction_atomic%>");
15297 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
15298 goto ret_error;
15301 else if (parser->in_transaction == 0)
15303 error_at (loc, "%<__transaction_cancel%> not within "
15304 "%<__transaction_atomic%>");
15305 goto ret_error;
15308 return add_stmt (build_tm_abort_call (loc, is_outer));
15310 ret_error:
15311 return build1 (NOP_EXPR, void_type_node, error_mark_node);
15314 /* Parse a single source file. */
15316 void
15317 c_parse_file (void)
15319 /* Use local storage to begin. If the first token is a pragma, parse it.
15320 If it is #pragma GCC pch_preprocess, then this will load a PCH file
15321 which will cause garbage collection. */
15322 c_parser tparser;
15324 memset (&tparser, 0, sizeof tparser);
15325 tparser.tokens = &tparser.tokens_buf[0];
15326 the_parser = &tparser;
15328 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
15329 c_parser_pragma_pch_preprocess (&tparser);
15331 the_parser = ggc_alloc<c_parser> ();
15332 *the_parser = tparser;
15333 if (tparser.tokens == &tparser.tokens_buf[0])
15334 the_parser->tokens = &the_parser->tokens_buf[0];
15336 /* Initialize EH, if we've been told to do so. */
15337 if (flag_exceptions)
15338 using_eh_for_cleanups ();
15340 c_parser_translation_unit (the_parser);
15341 the_parser = NULL;
15344 /* This function parses Cilk Plus array notation. The starting index is
15345 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
15346 return value of this function is a tree_node called VALUE_TREE of type
15347 ARRAY_NOTATION_REF. */
15349 static tree
15350 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
15351 tree array_value)
15353 c_token *token = NULL;
15354 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
15355 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
15356 tree array_type_domain = NULL_TREE;
15358 if (array_value == error_mark_node || initial_index == error_mark_node)
15360 /* No need to continue. If either of these 2 were true, then an error
15361 must be emitted already. Thus, no need to emit them twice. */
15362 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15363 return error_mark_node;
15366 array_type = TREE_TYPE (array_value);
15367 gcc_assert (array_type);
15368 if (TREE_CODE (array_type) != ARRAY_TYPE
15369 && TREE_CODE (array_type) != POINTER_TYPE)
15371 error_at (loc, "base of array section must be pointer or array type");
15372 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15373 return error_mark_node;
15375 type = TREE_TYPE (array_type);
15376 token = c_parser_peek_token (parser);
15378 if (token->type == CPP_EOF)
15380 c_parser_error (parser, "expected %<:%> or numeral");
15381 return value_tree;
15383 else if (token->type == CPP_COLON)
15385 if (!initial_index)
15387 /* If we are here, then we have a case like this A[:]. */
15388 c_parser_consume_token (parser);
15389 if (TREE_CODE (array_type) == POINTER_TYPE)
15391 error_at (loc, "start-index and length fields necessary for "
15392 "using array notations in pointers");
15393 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15394 return error_mark_node;
15396 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15398 error_at (loc, "array notations cannot be used with function "
15399 "type");
15400 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15401 return error_mark_node;
15403 array_type_domain = TYPE_DOMAIN (array_type);
15405 if (!array_type_domain)
15407 error_at (loc, "start-index and length fields necessary for "
15408 "using array notations in dimensionless arrays");
15409 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15410 return error_mark_node;
15413 start_index = TYPE_MINVAL (array_type_domain);
15414 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
15415 start_index);
15416 if (!TYPE_MAXVAL (array_type_domain)
15417 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
15419 error_at (loc, "start-index and length fields necessary for "
15420 "using array notations in variable-length arrays");
15421 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15422 return error_mark_node;
15424 end_index = TYPE_MAXVAL (array_type_domain);
15425 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
15426 end_index, integer_one_node);
15427 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
15428 stride = build_int_cst (integer_type_node, 1);
15429 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
15431 else if (initial_index != error_mark_node)
15433 /* If we are here, then there should be 2 possibilities:
15434 1. Array [EXPR : EXPR]
15435 2. Array [EXPR : EXPR : EXPR]
15437 start_index = initial_index;
15439 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15441 error_at (loc, "array notations cannot be used with function "
15442 "type");
15443 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15444 return error_mark_node;
15446 c_parser_consume_token (parser); /* consume the ':' */
15447 struct c_expr ce = c_parser_expression (parser);
15448 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15449 end_index = ce.value;
15450 if (!end_index || end_index == error_mark_node)
15452 c_parser_skip_to_end_of_block_or_statement (parser);
15453 return error_mark_node;
15455 if (c_parser_peek_token (parser)->type == CPP_COLON)
15457 c_parser_consume_token (parser);
15458 ce = c_parser_expression (parser);
15459 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15460 stride = ce.value;
15461 if (!stride || stride == error_mark_node)
15463 c_parser_skip_to_end_of_block_or_statement (parser);
15464 return error_mark_node;
15468 else
15469 c_parser_error (parser, "expected array notation expression");
15471 else
15472 c_parser_error (parser, "expected array notation expression");
15474 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15476 value_tree = build_array_notation_ref (loc, array_value, start_index,
15477 end_index, stride, type);
15478 if (value_tree != error_mark_node)
15479 SET_EXPR_LOCATION (value_tree, loc);
15480 return value_tree;
15483 #include "gt-c-c-parser.h"