Thomas Schwinge <thomas@codesourcery.com>
[official-gcc.git] / gcc / c / c-parser.c
blob0480932084714f52c7f006e8eb232e862e911eb2
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 "cgraph.h"
62 #include "plugin.h"
63 #include "omp-low.h"
64 #include "builtins.h"
67 /* Initialization routine for this file. */
69 void
70 c_parse_init (void)
72 /* The only initialization required is of the reserved word
73 identifiers. */
74 unsigned int i;
75 tree id;
76 int mask = 0;
78 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
79 the c_token structure. */
80 gcc_assert (RID_MAX <= 255);
82 mask |= D_CXXONLY;
83 if (!flag_isoc99)
84 mask |= D_C99;
85 if (flag_no_asm)
87 mask |= D_ASM | D_EXT;
88 if (!flag_isoc99)
89 mask |= D_EXT89;
91 if (!c_dialect_objc ())
92 mask |= D_OBJC | D_CXX_OBJC;
94 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
95 for (i = 0; i < num_c_common_reswords; i++)
97 /* If a keyword is disabled, do not enter it into the table
98 and so create a canonical spelling that isn't a keyword. */
99 if (c_common_reswords[i].disable & mask)
101 if (warn_cxx_compat
102 && (c_common_reswords[i].disable & D_CXXWARN))
104 id = get_identifier (c_common_reswords[i].word);
105 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
106 C_IS_RESERVED_WORD (id) = 1;
108 continue;
111 id = get_identifier (c_common_reswords[i].word);
112 C_SET_RID_CODE (id, c_common_reswords[i].rid);
113 C_IS_RESERVED_WORD (id) = 1;
114 ridpointers [(int) c_common_reswords[i].rid] = id;
118 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
119 and the C parser. Unlike the C++ lexer, the parser structure
120 stores the lexer information instead of using a separate structure.
121 Identifiers are separated into ordinary identifiers, type names,
122 keywords and some other Objective-C types of identifiers, and some
123 look-ahead is maintained.
125 ??? It might be a good idea to lex the whole file up front (as for
126 C++). It would then be possible to share more of the C and C++
127 lexer code, if desired. */
129 /* More information about the type of a CPP_NAME token. */
130 typedef enum c_id_kind {
131 /* An ordinary identifier. */
132 C_ID_ID,
133 /* An identifier declared as a typedef name. */
134 C_ID_TYPENAME,
135 /* An identifier declared as an Objective-C class name. */
136 C_ID_CLASSNAME,
137 /* An address space identifier. */
138 C_ID_ADDRSPACE,
139 /* Not an identifier. */
140 C_ID_NONE
141 } c_id_kind;
143 /* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
145 typedef struct GTY (()) c_token {
146 /* The kind of token. */
147 ENUM_BITFIELD (cpp_ttype) type : 8;
148 /* If this token is a CPP_NAME, this value indicates whether also
149 declared as some kind of type. Otherwise, it is C_ID_NONE. */
150 ENUM_BITFIELD (c_id_kind) id_kind : 8;
151 /* If this token is a keyword, this value indicates which keyword.
152 Otherwise, this value is RID_MAX. */
153 ENUM_BITFIELD (rid) keyword : 8;
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
157 /* The location at which this token was found. */
158 location_t location;
159 /* The value associated with this token, if any. */
160 tree value;
161 } c_token;
163 /* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
166 typedef struct GTY(()) c_parser {
167 /* The look-ahead tokens. */
168 c_token * GTY((skip)) tokens;
169 /* Buffer for look-ahead tokens. */
170 c_token tokens_buf[2];
171 /* How many look-ahead tokens are available (0, 1 or 2, or
172 more if parsing from pre-lexed tokens). */
173 unsigned int tokens_avail;
174 /* True if a syntax error is being recovered from; false otherwise.
175 c_parser_error sets this flag. It should clear this flag when
176 enough tokens have been consumed to recover from the error. */
177 BOOL_BITFIELD error : 1;
178 /* True if we're processing a pragma, and shouldn't automatically
179 consume CPP_PRAGMA_EOL. */
180 BOOL_BITFIELD in_pragma : 1;
181 /* True if we're parsing the outermost block of an if statement. */
182 BOOL_BITFIELD in_if_block : 1;
183 /* True if we want to lex an untranslated string. */
184 BOOL_BITFIELD lex_untranslated_string : 1;
186 /* Objective-C specific parser/lexer information. */
188 /* True if we are in a context where the Objective-C "PQ" keywords
189 are considered keywords. */
190 BOOL_BITFIELD objc_pq_context : 1;
191 /* True if we are parsing a (potential) Objective-C foreach
192 statement. This is set to true after we parsed 'for (' and while
193 we wait for 'in' or ';' to decide if it's a standard C for loop or an
194 Objective-C foreach loop. */
195 BOOL_BITFIELD objc_could_be_foreach_context : 1;
196 /* The following flag is needed to contextualize Objective-C lexical
197 analysis. In some cases (e.g., 'int NSObject;'), it is
198 undesirable to bind an identifier to an Objective-C class, even
199 if a class with that name exists. */
200 BOOL_BITFIELD objc_need_raw_identifier : 1;
201 /* Nonzero if we're processing a __transaction statement. The value
202 is 1 | TM_STMT_ATTR_*. */
203 unsigned int in_transaction : 4;
204 /* True if we are in a context where the Objective-C "Property attribute"
205 keywords are valid. */
206 BOOL_BITFIELD objc_property_attr_context : 1;
208 /* Cilk Plus specific parser/lexer information. */
210 /* Buffer to hold all the tokens from parsing the vector attribute for the
211 SIMD-enabled functions (formerly known as elemental functions). */
212 vec <c_token, va_gc> *cilk_simd_fn_tokens;
213 } c_parser;
216 /* The actual parser and external interface. ??? Does this need to be
217 garbage-collected? */
219 static GTY (()) c_parser *the_parser;
221 /* Read in and lex a single token, storing it in *TOKEN. */
223 static void
224 c_lex_one_token (c_parser *parser, c_token *token)
226 timevar_push (TV_LEX);
228 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
229 (parser->lex_untranslated_string
230 ? C_LEX_STRING_NO_TRANSLATE : 0));
231 token->id_kind = C_ID_NONE;
232 token->keyword = RID_MAX;
233 token->pragma_kind = PRAGMA_NONE;
235 switch (token->type)
237 case CPP_NAME:
239 tree decl;
241 bool objc_force_identifier = parser->objc_need_raw_identifier;
242 if (c_dialect_objc ())
243 parser->objc_need_raw_identifier = false;
245 if (C_IS_RESERVED_WORD (token->value))
247 enum rid rid_code = C_RID_CODE (token->value);
249 if (rid_code == RID_CXX_COMPAT_WARN)
251 warning_at (token->location,
252 OPT_Wc___compat,
253 "identifier %qE conflicts with C++ keyword",
254 token->value);
256 else if (rid_code >= RID_FIRST_ADDR_SPACE
257 && rid_code <= RID_LAST_ADDR_SPACE)
259 token->id_kind = C_ID_ADDRSPACE;
260 token->keyword = rid_code;
261 break;
263 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
265 /* We found an Objective-C "pq" keyword (in, out,
266 inout, bycopy, byref, oneway). They need special
267 care because the interpretation depends on the
268 context. */
269 if (parser->objc_pq_context)
271 token->type = CPP_KEYWORD;
272 token->keyword = rid_code;
273 break;
275 else if (parser->objc_could_be_foreach_context
276 && rid_code == RID_IN)
278 /* We are in Objective-C, inside a (potential)
279 foreach context (which means after having
280 parsed 'for (', but before having parsed ';'),
281 and we found 'in'. We consider it the keyword
282 which terminates the declaration at the
283 beginning of a foreach-statement. Note that
284 this means you can't use 'in' for anything else
285 in that context; in particular, in Objective-C
286 you can't use 'in' as the name of the running
287 variable in a C for loop. We could potentially
288 try to add code here to disambiguate, but it
289 seems a reasonable limitation. */
290 token->type = CPP_KEYWORD;
291 token->keyword = rid_code;
292 break;
294 /* Else, "pq" keywords outside of the "pq" context are
295 not keywords, and we fall through to the code for
296 normal tokens. */
298 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
300 /* We found an Objective-C "property attribute"
301 keyword (getter, setter, readonly, etc). These are
302 only valid in the property context. */
303 if (parser->objc_property_attr_context)
305 token->type = CPP_KEYWORD;
306 token->keyword = rid_code;
307 break;
309 /* Else they are not special keywords.
312 else if (c_dialect_objc ()
313 && (OBJC_IS_AT_KEYWORD (rid_code)
314 || OBJC_IS_CXX_KEYWORD (rid_code)))
316 /* We found one of the Objective-C "@" keywords (defs,
317 selector, synchronized, etc) or one of the
318 Objective-C "cxx" keywords (class, private,
319 protected, public, try, catch, throw) without a
320 preceding '@' sign. Do nothing and fall through to
321 the code for normal tokens (in C++ we would still
322 consider the CXX ones keywords, but not in C). */
325 else
327 token->type = CPP_KEYWORD;
328 token->keyword = rid_code;
329 break;
333 decl = lookup_name (token->value);
334 if (decl)
336 if (TREE_CODE (decl) == TYPE_DECL)
338 token->id_kind = C_ID_TYPENAME;
339 break;
342 else if (c_dialect_objc ())
344 tree objc_interface_decl = objc_is_class_name (token->value);
345 /* Objective-C class names are in the same namespace as
346 variables and typedefs, and hence are shadowed by local
347 declarations. */
348 if (objc_interface_decl
349 && (!objc_force_identifier || global_bindings_p ()))
351 token->value = objc_interface_decl;
352 token->id_kind = C_ID_CLASSNAME;
353 break;
356 token->id_kind = C_ID_ID;
358 break;
359 case CPP_AT_NAME:
360 /* This only happens in Objective-C; it must be a keyword. */
361 token->type = CPP_KEYWORD;
362 switch (C_RID_CODE (token->value))
364 /* Replace 'class' with '@class', 'private' with '@private',
365 etc. This prevents confusion with the C++ keyword
366 'class', and makes the tokens consistent with other
367 Objective-C 'AT' keywords. For example '@class' is
368 reported as RID_AT_CLASS which is consistent with
369 '@synchronized', which is reported as
370 RID_AT_SYNCHRONIZED.
372 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
373 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
374 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
375 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
376 case RID_THROW: token->keyword = RID_AT_THROW; break;
377 case RID_TRY: token->keyword = RID_AT_TRY; break;
378 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
379 default: token->keyword = C_RID_CODE (token->value);
381 break;
382 case CPP_COLON:
383 case CPP_COMMA:
384 case CPP_CLOSE_PAREN:
385 case CPP_SEMICOLON:
386 /* These tokens may affect the interpretation of any identifiers
387 following, if doing Objective-C. */
388 if (c_dialect_objc ())
389 parser->objc_need_raw_identifier = false;
390 break;
391 case CPP_PRAGMA:
392 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
393 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
394 token->value = NULL;
395 break;
396 default:
397 break;
399 timevar_pop (TV_LEX);
402 /* Return a pointer to the next token from PARSER, reading it in if
403 necessary. */
405 static inline c_token *
406 c_parser_peek_token (c_parser *parser)
408 if (parser->tokens_avail == 0)
410 c_lex_one_token (parser, &parser->tokens[0]);
411 parser->tokens_avail = 1;
413 return &parser->tokens[0];
416 /* Return true if the next token from PARSER has the indicated
417 TYPE. */
419 static inline bool
420 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
422 return c_parser_peek_token (parser)->type == type;
425 /* Return true if the next token from PARSER does not have the
426 indicated TYPE. */
428 static inline bool
429 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
431 return !c_parser_next_token_is (parser, type);
434 /* Return true if the next token from PARSER is the indicated
435 KEYWORD. */
437 static inline bool
438 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
440 return c_parser_peek_token (parser)->keyword == keyword;
443 /* Return a pointer to the next-but-one token from PARSER, reading it
444 in if necessary. The next token is already read in. */
446 static c_token *
447 c_parser_peek_2nd_token (c_parser *parser)
449 if (parser->tokens_avail >= 2)
450 return &parser->tokens[1];
451 gcc_assert (parser->tokens_avail == 1);
452 gcc_assert (parser->tokens[0].type != CPP_EOF);
453 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
454 c_lex_one_token (parser, &parser->tokens[1]);
455 parser->tokens_avail = 2;
456 return &parser->tokens[1];
459 /* Return true if TOKEN can start a type name,
460 false otherwise. */
461 static bool
462 c_token_starts_typename (c_token *token)
464 switch (token->type)
466 case CPP_NAME:
467 switch (token->id_kind)
469 case C_ID_ID:
470 return false;
471 case C_ID_ADDRSPACE:
472 return true;
473 case C_ID_TYPENAME:
474 return true;
475 case C_ID_CLASSNAME:
476 gcc_assert (c_dialect_objc ());
477 return true;
478 default:
479 gcc_unreachable ();
481 case CPP_KEYWORD:
482 switch (token->keyword)
484 case RID_UNSIGNED:
485 case RID_LONG:
486 case RID_INT128:
487 case RID_SHORT:
488 case RID_SIGNED:
489 case RID_COMPLEX:
490 case RID_INT:
491 case RID_CHAR:
492 case RID_FLOAT:
493 case RID_DOUBLE:
494 case RID_VOID:
495 case RID_DFLOAT32:
496 case RID_DFLOAT64:
497 case RID_DFLOAT128:
498 case RID_BOOL:
499 case RID_ENUM:
500 case RID_STRUCT:
501 case RID_UNION:
502 case RID_TYPEOF:
503 case RID_CONST:
504 case RID_ATOMIC:
505 case RID_VOLATILE:
506 case RID_RESTRICT:
507 case RID_ATTRIBUTE:
508 case RID_FRACT:
509 case RID_ACCUM:
510 case RID_SAT:
511 case RID_AUTO_TYPE:
512 return true;
513 default:
514 return false;
516 case CPP_LESS:
517 if (c_dialect_objc ())
518 return true;
519 return false;
520 default:
521 return false;
525 enum c_lookahead_kind {
526 /* Always treat unknown identifiers as typenames. */
527 cla_prefer_type,
529 /* Could be parsing a nonabstract declarator. Only treat an identifier
530 as a typename if followed by another identifier or a star. */
531 cla_nonabstract_decl,
533 /* Never treat identifiers as typenames. */
534 cla_prefer_id
537 /* Return true if the next token from PARSER can start a type name,
538 false otherwise. LA specifies how to do lookahead in order to
539 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
541 static inline bool
542 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
544 c_token *token = c_parser_peek_token (parser);
545 if (c_token_starts_typename (token))
546 return true;
548 /* Try a bit harder to detect an unknown typename. */
549 if (la != cla_prefer_id
550 && token->type == CPP_NAME
551 && token->id_kind == C_ID_ID
553 /* Do not try too hard when we could have "object in array". */
554 && !parser->objc_could_be_foreach_context
556 && (la == cla_prefer_type
557 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
558 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
560 /* Only unknown identifiers. */
561 && !lookup_name (token->value))
562 return true;
564 return false;
567 /* Return true if TOKEN is a type qualifier, false otherwise. */
568 static bool
569 c_token_is_qualifier (c_token *token)
571 switch (token->type)
573 case CPP_NAME:
574 switch (token->id_kind)
576 case C_ID_ADDRSPACE:
577 return true;
578 default:
579 return false;
581 case CPP_KEYWORD:
582 switch (token->keyword)
584 case RID_CONST:
585 case RID_VOLATILE:
586 case RID_RESTRICT:
587 case RID_ATTRIBUTE:
588 case RID_ATOMIC:
589 return true;
590 default:
591 return false;
593 case CPP_LESS:
594 return false;
595 default:
596 gcc_unreachable ();
600 /* Return true if the next token from PARSER is a type qualifier,
601 false otherwise. */
602 static inline bool
603 c_parser_next_token_is_qualifier (c_parser *parser)
605 c_token *token = c_parser_peek_token (parser);
606 return c_token_is_qualifier (token);
609 /* Return true if TOKEN can start declaration specifiers, false
610 otherwise. */
611 static bool
612 c_token_starts_declspecs (c_token *token)
614 switch (token->type)
616 case CPP_NAME:
617 switch (token->id_kind)
619 case C_ID_ID:
620 return false;
621 case C_ID_ADDRSPACE:
622 return true;
623 case C_ID_TYPENAME:
624 return true;
625 case C_ID_CLASSNAME:
626 gcc_assert (c_dialect_objc ());
627 return true;
628 default:
629 gcc_unreachable ();
631 case CPP_KEYWORD:
632 switch (token->keyword)
634 case RID_STATIC:
635 case RID_EXTERN:
636 case RID_REGISTER:
637 case RID_TYPEDEF:
638 case RID_INLINE:
639 case RID_NORETURN:
640 case RID_AUTO:
641 case RID_THREAD:
642 case RID_UNSIGNED:
643 case RID_LONG:
644 case RID_INT128:
645 case RID_SHORT:
646 case RID_SIGNED:
647 case RID_COMPLEX:
648 case RID_INT:
649 case RID_CHAR:
650 case RID_FLOAT:
651 case RID_DOUBLE:
652 case RID_VOID:
653 case RID_DFLOAT32:
654 case RID_DFLOAT64:
655 case RID_DFLOAT128:
656 case RID_BOOL:
657 case RID_ENUM:
658 case RID_STRUCT:
659 case RID_UNION:
660 case RID_TYPEOF:
661 case RID_CONST:
662 case RID_VOLATILE:
663 case RID_RESTRICT:
664 case RID_ATTRIBUTE:
665 case RID_FRACT:
666 case RID_ACCUM:
667 case RID_SAT:
668 case RID_ALIGNAS:
669 case RID_ATOMIC:
670 case RID_AUTO_TYPE:
671 return true;
672 default:
673 return false;
675 case CPP_LESS:
676 if (c_dialect_objc ())
677 return true;
678 return false;
679 default:
680 return false;
685 /* Return true if TOKEN can start declaration specifiers or a static
686 assertion, false otherwise. */
687 static bool
688 c_token_starts_declaration (c_token *token)
690 if (c_token_starts_declspecs (token)
691 || token->keyword == RID_STATIC_ASSERT)
692 return true;
693 else
694 return false;
697 /* Return true if the next token from PARSER can start declaration
698 specifiers, false otherwise. */
699 static inline bool
700 c_parser_next_token_starts_declspecs (c_parser *parser)
702 c_token *token = c_parser_peek_token (parser);
704 /* In Objective-C, a classname normally starts a declspecs unless it
705 is immediately followed by a dot. In that case, it is the
706 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
707 setter/getter on the class. c_token_starts_declspecs() can't
708 differentiate between the two cases because it only checks the
709 current token, so we have a special check here. */
710 if (c_dialect_objc ()
711 && token->type == CPP_NAME
712 && token->id_kind == C_ID_CLASSNAME
713 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
714 return false;
716 return c_token_starts_declspecs (token);
719 /* Return true if the next tokens from PARSER can start declaration
720 specifiers or a static assertion, false otherwise. */
721 static inline bool
722 c_parser_next_tokens_start_declaration (c_parser *parser)
724 c_token *token = c_parser_peek_token (parser);
726 /* Same as above. */
727 if (c_dialect_objc ()
728 && token->type == CPP_NAME
729 && token->id_kind == C_ID_CLASSNAME
730 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
731 return false;
733 /* Labels do not start declarations. */
734 if (token->type == CPP_NAME
735 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
736 return false;
738 if (c_token_starts_declaration (token))
739 return true;
741 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
742 return true;
744 return false;
747 /* Consume the next token from PARSER. */
749 static void
750 c_parser_consume_token (c_parser *parser)
752 gcc_assert (parser->tokens_avail >= 1);
753 gcc_assert (parser->tokens[0].type != CPP_EOF);
754 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
755 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
756 if (parser->tokens != &parser->tokens_buf[0])
757 parser->tokens++;
758 else if (parser->tokens_avail == 2)
759 parser->tokens[0] = parser->tokens[1];
760 parser->tokens_avail--;
763 /* Expect the current token to be a #pragma. Consume it and remember
764 that we've begun parsing a pragma. */
766 static void
767 c_parser_consume_pragma (c_parser *parser)
769 gcc_assert (!parser->in_pragma);
770 gcc_assert (parser->tokens_avail >= 1);
771 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
772 if (parser->tokens != &parser->tokens_buf[0])
773 parser->tokens++;
774 else if (parser->tokens_avail == 2)
775 parser->tokens[0] = parser->tokens[1];
776 parser->tokens_avail--;
777 parser->in_pragma = true;
780 /* Update the global input_location from TOKEN. */
781 static inline void
782 c_parser_set_source_position_from_token (c_token *token)
784 if (token->type != CPP_EOF)
786 input_location = token->location;
790 /* Issue a diagnostic of the form
791 FILE:LINE: MESSAGE before TOKEN
792 where TOKEN is the next token in the input stream of PARSER.
793 MESSAGE (specified by the caller) is usually of the form "expected
794 OTHER-TOKEN".
796 Do not issue a diagnostic if still recovering from an error.
798 ??? This is taken from the C++ parser, but building up messages in
799 this way is not i18n-friendly and some other approach should be
800 used. */
802 static void
803 c_parser_error (c_parser *parser, const char *gmsgid)
805 c_token *token = c_parser_peek_token (parser);
806 if (parser->error)
807 return;
808 parser->error = true;
809 if (!gmsgid)
810 return;
811 /* This diagnostic makes more sense if it is tagged to the line of
812 the token we just peeked at. */
813 c_parser_set_source_position_from_token (token);
814 c_parse_error (gmsgid,
815 /* Because c_parse_error does not understand
816 CPP_KEYWORD, keywords are treated like
817 identifiers. */
818 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
819 /* ??? The C parser does not save the cpp flags of a
820 token, we need to pass 0 here and we will not get
821 the source spelling of some tokens but rather the
822 canonical spelling. */
823 token->value, /*flags=*/0);
826 /* If the next token is of the indicated TYPE, consume it. Otherwise,
827 issue the error MSGID. If MSGID is NULL then a message has already
828 been produced and no message will be produced this time. Returns
829 true if found, false otherwise. */
831 static bool
832 c_parser_require (c_parser *parser,
833 enum cpp_ttype type,
834 const char *msgid)
836 if (c_parser_next_token_is (parser, type))
838 c_parser_consume_token (parser);
839 return true;
841 else
843 c_parser_error (parser, msgid);
844 return false;
848 /* If the next token is the indicated keyword, consume it. Otherwise,
849 issue the error MSGID. Returns true if found, false otherwise. */
851 static bool
852 c_parser_require_keyword (c_parser *parser,
853 enum rid keyword,
854 const char *msgid)
856 if (c_parser_next_token_is_keyword (parser, keyword))
858 c_parser_consume_token (parser);
859 return true;
861 else
863 c_parser_error (parser, msgid);
864 return false;
868 /* Like c_parser_require, except that tokens will be skipped until the
869 desired token is found. An error message is still produced if the
870 next token is not as expected. If MSGID is NULL then a message has
871 already been produced and no message will be produced this
872 time. */
874 static void
875 c_parser_skip_until_found (c_parser *parser,
876 enum cpp_ttype type,
877 const char *msgid)
879 unsigned nesting_depth = 0;
881 if (c_parser_require (parser, type, msgid))
882 return;
884 /* Skip tokens until the desired token is found. */
885 while (true)
887 /* Peek at the next token. */
888 c_token *token = c_parser_peek_token (parser);
889 /* If we've reached the token we want, consume it and stop. */
890 if (token->type == type && !nesting_depth)
892 c_parser_consume_token (parser);
893 break;
896 /* If we've run out of tokens, stop. */
897 if (token->type == CPP_EOF)
898 return;
899 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
900 return;
901 if (token->type == CPP_OPEN_BRACE
902 || token->type == CPP_OPEN_PAREN
903 || token->type == CPP_OPEN_SQUARE)
904 ++nesting_depth;
905 else if (token->type == CPP_CLOSE_BRACE
906 || token->type == CPP_CLOSE_PAREN
907 || token->type == CPP_CLOSE_SQUARE)
909 if (nesting_depth-- == 0)
910 break;
912 /* Consume this token. */
913 c_parser_consume_token (parser);
915 parser->error = false;
918 /* Skip tokens until the end of a parameter is found, but do not
919 consume the comma, semicolon or closing delimiter. */
921 static void
922 c_parser_skip_to_end_of_parameter (c_parser *parser)
924 unsigned nesting_depth = 0;
926 while (true)
928 c_token *token = c_parser_peek_token (parser);
929 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
930 && !nesting_depth)
931 break;
932 /* If we've run out of tokens, stop. */
933 if (token->type == CPP_EOF)
934 return;
935 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
936 return;
937 if (token->type == CPP_OPEN_BRACE
938 || token->type == CPP_OPEN_PAREN
939 || token->type == CPP_OPEN_SQUARE)
940 ++nesting_depth;
941 else if (token->type == CPP_CLOSE_BRACE
942 || token->type == CPP_CLOSE_PAREN
943 || token->type == CPP_CLOSE_SQUARE)
945 if (nesting_depth-- == 0)
946 break;
948 /* Consume this token. */
949 c_parser_consume_token (parser);
951 parser->error = false;
954 /* Expect to be at the end of the pragma directive and consume an
955 end of line marker. */
957 static void
958 c_parser_skip_to_pragma_eol (c_parser *parser)
960 gcc_assert (parser->in_pragma);
961 parser->in_pragma = false;
963 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
964 while (true)
966 c_token *token = c_parser_peek_token (parser);
967 if (token->type == CPP_EOF)
968 break;
969 if (token->type == CPP_PRAGMA_EOL)
971 c_parser_consume_token (parser);
972 break;
974 c_parser_consume_token (parser);
977 parser->error = false;
980 /* Skip tokens until we have consumed an entire block, or until we
981 have consumed a non-nested ';'. */
983 static void
984 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
986 unsigned nesting_depth = 0;
987 bool save_error = parser->error;
989 while (true)
991 c_token *token;
993 /* Peek at the next token. */
994 token = c_parser_peek_token (parser);
996 switch (token->type)
998 case CPP_EOF:
999 return;
1001 case CPP_PRAGMA_EOL:
1002 if (parser->in_pragma)
1003 return;
1004 break;
1006 case CPP_SEMICOLON:
1007 /* If the next token is a ';', we have reached the
1008 end of the statement. */
1009 if (!nesting_depth)
1011 /* Consume the ';'. */
1012 c_parser_consume_token (parser);
1013 goto finished;
1015 break;
1017 case CPP_CLOSE_BRACE:
1018 /* If the next token is a non-nested '}', then we have
1019 reached the end of the current block. */
1020 if (nesting_depth == 0 || --nesting_depth == 0)
1022 c_parser_consume_token (parser);
1023 goto finished;
1025 break;
1027 case CPP_OPEN_BRACE:
1028 /* If it the next token is a '{', then we are entering a new
1029 block. Consume the entire block. */
1030 ++nesting_depth;
1031 break;
1033 case CPP_PRAGMA:
1034 /* If we see a pragma, consume the whole thing at once. We
1035 have some safeguards against consuming pragmas willy-nilly.
1036 Normally, we'd expect to be here with parser->error set,
1037 which disables these safeguards. But it's possible to get
1038 here for secondary error recovery, after parser->error has
1039 been cleared. */
1040 c_parser_consume_pragma (parser);
1041 c_parser_skip_to_pragma_eol (parser);
1042 parser->error = save_error;
1043 continue;
1045 default:
1046 break;
1049 c_parser_consume_token (parser);
1052 finished:
1053 parser->error = false;
1056 /* CPP's options (initialized by c-opts.c). */
1057 extern cpp_options *cpp_opts;
1059 /* Save the warning flags which are controlled by __extension__. */
1061 static inline int
1062 disable_extension_diagnostics (void)
1064 int ret = (pedantic
1065 | (warn_pointer_arith << 1)
1066 | (warn_traditional << 2)
1067 | (flag_iso << 3)
1068 | (warn_long_long << 4)
1069 | (warn_cxx_compat << 5)
1070 | (warn_overlength_strings << 6)
1071 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1072 play tricks to properly restore it. */
1073 | ((warn_c90_c99_compat == 1) << 7)
1074 | ((warn_c90_c99_compat == -1) << 8)
1075 /* Similarly for warn_c99_c11_compat. */
1076 | ((warn_c99_c11_compat == 1) << 9)
1077 | ((warn_c99_c11_compat == -1) << 10)
1079 cpp_opts->cpp_pedantic = pedantic = 0;
1080 warn_pointer_arith = 0;
1081 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1082 flag_iso = 0;
1083 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1084 warn_cxx_compat = 0;
1085 warn_overlength_strings = 0;
1086 warn_c90_c99_compat = 0;
1087 warn_c99_c11_compat = 0;
1088 return ret;
1091 /* Restore the warning flags which are controlled by __extension__.
1092 FLAGS is the return value from disable_extension_diagnostics. */
1094 static inline void
1095 restore_extension_diagnostics (int flags)
1097 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1098 warn_pointer_arith = (flags >> 1) & 1;
1099 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1100 flag_iso = (flags >> 3) & 1;
1101 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1102 warn_cxx_compat = (flags >> 5) & 1;
1103 warn_overlength_strings = (flags >> 6) & 1;
1104 /* See above for why is this needed. */
1105 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1106 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1109 /* Possibly kinds of declarator to parse. */
1110 typedef enum c_dtr_syn {
1111 /* A normal declarator with an identifier. */
1112 C_DTR_NORMAL,
1113 /* An abstract declarator (maybe empty). */
1114 C_DTR_ABSTRACT,
1115 /* A parameter declarator: may be either, but after a type name does
1116 not redeclare a typedef name as an identifier if it can
1117 alternatively be interpreted as a typedef name; see DR#009,
1118 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1119 following DR#249. For example, given a typedef T, "int T" and
1120 "int *T" are valid parameter declarations redeclaring T, while
1121 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1122 abstract declarators rather than involving redundant parentheses;
1123 the same applies with attributes inside the parentheses before
1124 "T". */
1125 C_DTR_PARM
1126 } c_dtr_syn;
1128 /* The binary operation precedence levels, where 0 is a dummy lowest level
1129 used for the bottom of the stack. */
1130 enum c_parser_prec {
1131 PREC_NONE,
1132 PREC_LOGOR,
1133 PREC_LOGAND,
1134 PREC_BITOR,
1135 PREC_BITXOR,
1136 PREC_BITAND,
1137 PREC_EQ,
1138 PREC_REL,
1139 PREC_SHIFT,
1140 PREC_ADD,
1141 PREC_MULT,
1142 NUM_PRECS
1145 static void c_parser_external_declaration (c_parser *);
1146 static void c_parser_asm_definition (c_parser *);
1147 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1148 bool, bool, tree *, vec<c_token>);
1149 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1150 static void c_parser_static_assert_declaration (c_parser *);
1151 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1152 bool, bool, bool, enum c_lookahead_kind);
1153 static struct c_typespec c_parser_enum_specifier (c_parser *);
1154 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1155 static tree c_parser_struct_declaration (c_parser *);
1156 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1157 static tree c_parser_alignas_specifier (c_parser *);
1158 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1159 bool *);
1160 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1161 c_dtr_syn, bool *);
1162 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1163 bool,
1164 struct c_declarator *);
1165 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1166 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1167 tree);
1168 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1169 static tree c_parser_simple_asm_expr (c_parser *);
1170 static tree c_parser_attributes (c_parser *);
1171 static struct c_type_name *c_parser_type_name (c_parser *);
1172 static struct c_expr c_parser_initializer (c_parser *);
1173 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1174 static void c_parser_initelt (c_parser *, struct obstack *);
1175 static void c_parser_initval (c_parser *, struct c_expr *,
1176 struct obstack *);
1177 static tree c_parser_compound_statement (c_parser *);
1178 static void c_parser_compound_statement_nostart (c_parser *);
1179 static void c_parser_label (c_parser *);
1180 static void c_parser_statement (c_parser *);
1181 static void c_parser_statement_after_labels (c_parser *);
1182 static void c_parser_if_statement (c_parser *);
1183 static void c_parser_switch_statement (c_parser *);
1184 static void c_parser_while_statement (c_parser *, bool);
1185 static void c_parser_do_statement (c_parser *, bool);
1186 static void c_parser_for_statement (c_parser *, bool);
1187 static tree c_parser_asm_statement (c_parser *);
1188 static tree c_parser_asm_operands (c_parser *);
1189 static tree c_parser_asm_goto_operands (c_parser *);
1190 static tree c_parser_asm_clobbers (c_parser *);
1191 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1192 tree = NULL_TREE);
1193 static struct c_expr c_parser_conditional_expression (c_parser *,
1194 struct c_expr *, tree);
1195 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1196 tree);
1197 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1198 static struct c_expr c_parser_unary_expression (c_parser *);
1199 static struct c_expr c_parser_sizeof_expression (c_parser *);
1200 static struct c_expr c_parser_alignof_expression (c_parser *);
1201 static struct c_expr c_parser_postfix_expression (c_parser *);
1202 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1203 struct c_type_name *,
1204 location_t);
1205 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1206 location_t loc,
1207 struct c_expr);
1208 static tree c_parser_transaction (c_parser *, enum rid);
1209 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1210 static tree c_parser_transaction_cancel (c_parser *);
1211 static struct c_expr c_parser_expression (c_parser *);
1212 static struct c_expr c_parser_expression_conv (c_parser *);
1213 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1214 vec<tree, va_gc> **, location_t *,
1215 tree *, vec<location_t> *,
1216 unsigned int * = NULL);
1217 static tree c_parser_oacc_loop (location_t, c_parser *, char *);
1218 static void c_parser_omp_construct (c_parser *);
1219 static void c_parser_omp_threadprivate (c_parser *);
1220 static void c_parser_oacc_update (c_parser *);
1221 static void c_parser_omp_barrier (c_parser *);
1222 static void c_parser_omp_flush (c_parser *);
1223 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1224 tree, tree *);
1225 static void c_parser_omp_taskwait (c_parser *);
1226 static void c_parser_omp_taskyield (c_parser *);
1227 static void c_parser_omp_cancel (c_parser *);
1228 static void c_parser_omp_cancellation_point (c_parser *);
1230 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1231 pragma_stmt, pragma_compound };
1232 static bool c_parser_pragma (c_parser *, enum pragma_context);
1233 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1234 static void c_parser_omp_end_declare_target (c_parser *);
1235 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1237 /* These Objective-C parser functions are only ever called when
1238 compiling Objective-C. */
1239 static void c_parser_objc_class_definition (c_parser *, tree);
1240 static void c_parser_objc_class_instance_variables (c_parser *);
1241 static void c_parser_objc_class_declaration (c_parser *);
1242 static void c_parser_objc_alias_declaration (c_parser *);
1243 static void c_parser_objc_protocol_definition (c_parser *, tree);
1244 static bool c_parser_objc_method_type (c_parser *);
1245 static void c_parser_objc_method_definition (c_parser *);
1246 static void c_parser_objc_methodprotolist (c_parser *);
1247 static void c_parser_objc_methodproto (c_parser *);
1248 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1249 static tree c_parser_objc_type_name (c_parser *);
1250 static tree c_parser_objc_protocol_refs (c_parser *);
1251 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1252 static void c_parser_objc_synchronized_statement (c_parser *);
1253 static tree c_parser_objc_selector (c_parser *);
1254 static tree c_parser_objc_selector_arg (c_parser *);
1255 static tree c_parser_objc_receiver (c_parser *);
1256 static tree c_parser_objc_message_args (c_parser *);
1257 static tree c_parser_objc_keywordexpr (c_parser *);
1258 static void c_parser_objc_at_property_declaration (c_parser *);
1259 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1260 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1261 static bool c_parser_objc_diagnose_bad_element_prefix
1262 (c_parser *, struct c_declspecs *);
1264 /* Cilk Plus supporting routines. */
1265 static void c_parser_cilk_simd (c_parser *);
1266 static void c_parser_cilk_for (c_parser *, tree);
1267 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1268 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1269 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1270 static void c_parser_cilk_grainsize (c_parser *);
1272 /* Parse a translation unit (C90 6.7, C99 6.9).
1274 translation-unit:
1275 external-declarations
1277 external-declarations:
1278 external-declaration
1279 external-declarations external-declaration
1281 GNU extensions:
1283 translation-unit:
1284 empty
1287 static void
1288 c_parser_translation_unit (c_parser *parser)
1290 if (c_parser_next_token_is (parser, CPP_EOF))
1292 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1293 "ISO C forbids an empty translation unit");
1295 else
1297 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1298 mark_valid_location_for_stdc_pragma (false);
1301 ggc_collect ();
1302 c_parser_external_declaration (parser);
1303 obstack_free (&parser_obstack, obstack_position);
1305 while (c_parser_next_token_is_not (parser, CPP_EOF));
1309 /* Parse an external declaration (C90 6.7, C99 6.9).
1311 external-declaration:
1312 function-definition
1313 declaration
1315 GNU extensions:
1317 external-declaration:
1318 asm-definition
1320 __extension__ external-declaration
1322 Objective-C:
1324 external-declaration:
1325 objc-class-definition
1326 objc-class-declaration
1327 objc-alias-declaration
1328 objc-protocol-definition
1329 objc-method-definition
1330 @end
1333 static void
1334 c_parser_external_declaration (c_parser *parser)
1336 int ext;
1337 switch (c_parser_peek_token (parser)->type)
1339 case CPP_KEYWORD:
1340 switch (c_parser_peek_token (parser)->keyword)
1342 case RID_EXTENSION:
1343 ext = disable_extension_diagnostics ();
1344 c_parser_consume_token (parser);
1345 c_parser_external_declaration (parser);
1346 restore_extension_diagnostics (ext);
1347 break;
1348 case RID_ASM:
1349 c_parser_asm_definition (parser);
1350 break;
1351 case RID_AT_INTERFACE:
1352 case RID_AT_IMPLEMENTATION:
1353 gcc_assert (c_dialect_objc ());
1354 c_parser_objc_class_definition (parser, NULL_TREE);
1355 break;
1356 case RID_AT_CLASS:
1357 gcc_assert (c_dialect_objc ());
1358 c_parser_objc_class_declaration (parser);
1359 break;
1360 case RID_AT_ALIAS:
1361 gcc_assert (c_dialect_objc ());
1362 c_parser_objc_alias_declaration (parser);
1363 break;
1364 case RID_AT_PROTOCOL:
1365 gcc_assert (c_dialect_objc ());
1366 c_parser_objc_protocol_definition (parser, NULL_TREE);
1367 break;
1368 case RID_AT_PROPERTY:
1369 gcc_assert (c_dialect_objc ());
1370 c_parser_objc_at_property_declaration (parser);
1371 break;
1372 case RID_AT_SYNTHESIZE:
1373 gcc_assert (c_dialect_objc ());
1374 c_parser_objc_at_synthesize_declaration (parser);
1375 break;
1376 case RID_AT_DYNAMIC:
1377 gcc_assert (c_dialect_objc ());
1378 c_parser_objc_at_dynamic_declaration (parser);
1379 break;
1380 case RID_AT_END:
1381 gcc_assert (c_dialect_objc ());
1382 c_parser_consume_token (parser);
1383 objc_finish_implementation ();
1384 break;
1385 default:
1386 goto decl_or_fndef;
1388 break;
1389 case CPP_SEMICOLON:
1390 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1391 "ISO C does not allow extra %<;%> outside of a function");
1392 c_parser_consume_token (parser);
1393 break;
1394 case CPP_PRAGMA:
1395 mark_valid_location_for_stdc_pragma (true);
1396 c_parser_pragma (parser, pragma_external);
1397 mark_valid_location_for_stdc_pragma (false);
1398 break;
1399 case CPP_PLUS:
1400 case CPP_MINUS:
1401 if (c_dialect_objc ())
1403 c_parser_objc_method_definition (parser);
1404 break;
1406 /* Else fall through, and yield a syntax error trying to parse
1407 as a declaration or function definition. */
1408 default:
1409 decl_or_fndef:
1410 /* A declaration or a function definition (or, in Objective-C,
1411 an @interface or @protocol with prefix attributes). We can
1412 only tell which after parsing the declaration specifiers, if
1413 any, and the first declarator. */
1414 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1415 NULL, vNULL);
1416 break;
1420 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1422 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1423 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1424 accepted; otherwise (old-style parameter declarations) only other
1425 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1426 assertion is accepted; otherwise (old-style parameter declarations)
1427 it is not. If NESTED is true, we are inside a function or parsing
1428 old-style parameter declarations; any functions encountered are
1429 nested functions and declaration specifiers are required; otherwise
1430 we are at top level and functions are normal functions and
1431 declaration specifiers may be optional. If EMPTY_OK is true, empty
1432 declarations are OK (subject to all other constraints); otherwise
1433 (old-style parameter declarations) they are diagnosed. If
1434 START_ATTR_OK is true, the declaration specifiers may start with
1435 attributes; otherwise they may not.
1436 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1437 declaration when parsing an Objective-C foreach statement.
1439 declaration:
1440 declaration-specifiers init-declarator-list[opt] ;
1441 static_assert-declaration
1443 function-definition:
1444 declaration-specifiers[opt] declarator declaration-list[opt]
1445 compound-statement
1447 declaration-list:
1448 declaration
1449 declaration-list declaration
1451 init-declarator-list:
1452 init-declarator
1453 init-declarator-list , init-declarator
1455 init-declarator:
1456 declarator simple-asm-expr[opt] attributes[opt]
1457 declarator simple-asm-expr[opt] attributes[opt] = initializer
1459 GNU extensions:
1461 nested-function-definition:
1462 declaration-specifiers declarator declaration-list[opt]
1463 compound-statement
1465 Objective-C:
1466 attributes objc-class-definition
1467 attributes objc-category-definition
1468 attributes objc-protocol-definition
1470 The simple-asm-expr and attributes are GNU extensions.
1472 This function does not handle __extension__; that is handled in its
1473 callers. ??? Following the old parser, __extension__ may start
1474 external declarations, declarations in functions and declarations
1475 at the start of "for" loops, but not old-style parameter
1476 declarations.
1478 C99 requires declaration specifiers in a function definition; the
1479 absence is diagnosed through the diagnosis of implicit int. In GNU
1480 C we also allow but diagnose declarations without declaration
1481 specifiers, but only at top level (elsewhere they conflict with
1482 other syntax).
1484 In Objective-C, declarations of the looping variable in a foreach
1485 statement are exceptionally terminated by 'in' (for example, 'for
1486 (NSObject *object in array) { ... }').
1488 OpenMP:
1490 declaration:
1491 threadprivate-directive */
1493 static void
1494 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1495 bool static_assert_ok, bool empty_ok,
1496 bool nested, bool start_attr_ok,
1497 tree *objc_foreach_object_declaration,
1498 vec<c_token> omp_declare_simd_clauses)
1500 struct c_declspecs *specs;
1501 tree prefix_attrs;
1502 tree all_prefix_attrs;
1503 bool diagnosed_no_specs = false;
1504 location_t here = c_parser_peek_token (parser)->location;
1506 if (static_assert_ok
1507 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1509 c_parser_static_assert_declaration (parser);
1510 return;
1512 specs = build_null_declspecs ();
1514 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1515 if (c_parser_peek_token (parser)->type == CPP_NAME
1516 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1517 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1518 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1519 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1521 error_at (here, "unknown type name %qE",
1522 c_parser_peek_token (parser)->value);
1524 /* Parse declspecs normally to get a correct pointer type, but avoid
1525 a further "fails to be a type name" error. Refuse nested functions
1526 since it is not how the user likely wants us to recover. */
1527 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1528 c_parser_peek_token (parser)->keyword = RID_VOID;
1529 c_parser_peek_token (parser)->value = error_mark_node;
1530 fndef_ok = !nested;
1533 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1534 true, true, cla_nonabstract_decl);
1535 if (parser->error)
1537 c_parser_skip_to_end_of_block_or_statement (parser);
1538 return;
1540 if (nested && !specs->declspecs_seen_p)
1542 c_parser_error (parser, "expected declaration specifiers");
1543 c_parser_skip_to_end_of_block_or_statement (parser);
1544 return;
1546 finish_declspecs (specs);
1547 bool auto_type_p = specs->typespec_word == cts_auto_type;
1548 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1550 if (auto_type_p)
1551 error_at (here, "%<__auto_type%> in empty declaration");
1552 else if (empty_ok)
1553 shadow_tag (specs);
1554 else
1556 shadow_tag_warned (specs, 1);
1557 pedwarn (here, 0, "empty declaration");
1559 c_parser_consume_token (parser);
1560 return;
1563 /* Provide better error recovery. Note that a type name here is usually
1564 better diagnosed as a redeclaration. */
1565 if (empty_ok
1566 && specs->typespec_kind == ctsk_tagdef
1567 && c_parser_next_token_starts_declspecs (parser)
1568 && !c_parser_next_token_is (parser, CPP_NAME))
1570 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1571 parser->error = false;
1572 shadow_tag_warned (specs, 1);
1573 return;
1575 else if (c_dialect_objc () && !auto_type_p)
1577 /* Prefix attributes are an error on method decls. */
1578 switch (c_parser_peek_token (parser)->type)
1580 case CPP_PLUS:
1581 case CPP_MINUS:
1582 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1583 return;
1584 if (specs->attrs)
1586 warning_at (c_parser_peek_token (parser)->location,
1587 OPT_Wattributes,
1588 "prefix attributes are ignored for methods");
1589 specs->attrs = NULL_TREE;
1591 if (fndef_ok)
1592 c_parser_objc_method_definition (parser);
1593 else
1594 c_parser_objc_methodproto (parser);
1595 return;
1596 break;
1597 default:
1598 break;
1600 /* This is where we parse 'attributes @interface ...',
1601 'attributes @implementation ...', 'attributes @protocol ...'
1602 (where attributes could be, for example, __attribute__
1603 ((deprecated)).
1605 switch (c_parser_peek_token (parser)->keyword)
1607 case RID_AT_INTERFACE:
1609 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1610 return;
1611 c_parser_objc_class_definition (parser, specs->attrs);
1612 return;
1614 break;
1615 case RID_AT_IMPLEMENTATION:
1617 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1618 return;
1619 if (specs->attrs)
1621 warning_at (c_parser_peek_token (parser)->location,
1622 OPT_Wattributes,
1623 "prefix attributes are ignored for implementations");
1624 specs->attrs = NULL_TREE;
1626 c_parser_objc_class_definition (parser, NULL_TREE);
1627 return;
1629 break;
1630 case RID_AT_PROTOCOL:
1632 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1633 return;
1634 c_parser_objc_protocol_definition (parser, specs->attrs);
1635 return;
1637 break;
1638 case RID_AT_ALIAS:
1639 case RID_AT_CLASS:
1640 case RID_AT_END:
1641 case RID_AT_PROPERTY:
1642 if (specs->attrs)
1644 c_parser_error (parser, "unexpected attribute");
1645 specs->attrs = NULL;
1647 break;
1648 default:
1649 break;
1653 pending_xref_error ();
1654 prefix_attrs = specs->attrs;
1655 all_prefix_attrs = prefix_attrs;
1656 specs->attrs = NULL_TREE;
1657 while (true)
1659 struct c_declarator *declarator;
1660 bool dummy = false;
1661 timevar_id_t tv;
1662 tree fnbody;
1663 /* Declaring either one or more declarators (in which case we
1664 should diagnose if there were no declaration specifiers) or a
1665 function definition (in which case the diagnostic for
1666 implicit int suffices). */
1667 declarator = c_parser_declarator (parser,
1668 specs->typespec_kind != ctsk_none,
1669 C_DTR_NORMAL, &dummy);
1670 if (declarator == NULL)
1672 if (omp_declare_simd_clauses.exists ()
1673 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1674 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1675 omp_declare_simd_clauses);
1676 c_parser_skip_to_end_of_block_or_statement (parser);
1677 return;
1679 if (auto_type_p && declarator->kind != cdk_id)
1681 error_at (here,
1682 "%<__auto_type%> requires a plain identifier"
1683 " as declarator");
1684 c_parser_skip_to_end_of_block_or_statement (parser);
1685 return;
1687 if (c_parser_next_token_is (parser, CPP_EQ)
1688 || c_parser_next_token_is (parser, CPP_COMMA)
1689 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1690 || c_parser_next_token_is_keyword (parser, RID_ASM)
1691 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1692 || c_parser_next_token_is_keyword (parser, RID_IN))
1694 tree asm_name = NULL_TREE;
1695 tree postfix_attrs = NULL_TREE;
1696 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1698 diagnosed_no_specs = true;
1699 pedwarn (here, 0, "data definition has no type or storage class");
1701 /* Having seen a data definition, there cannot now be a
1702 function definition. */
1703 fndef_ok = false;
1704 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1705 asm_name = c_parser_simple_asm_expr (parser);
1706 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1708 postfix_attrs = c_parser_attributes (parser);
1709 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1711 /* This means there is an attribute specifier after
1712 the declarator in a function definition. Provide
1713 some more information for the user. */
1714 error_at (here, "attributes should be specified before the "
1715 "declarator in a function definition");
1716 c_parser_skip_to_end_of_block_or_statement (parser);
1717 return;
1720 if (c_parser_next_token_is (parser, CPP_EQ))
1722 tree d;
1723 struct c_expr init;
1724 location_t init_loc;
1725 c_parser_consume_token (parser);
1726 if (auto_type_p)
1728 start_init (NULL_TREE, asm_name, global_bindings_p ());
1729 init_loc = c_parser_peek_token (parser)->location;
1730 init = c_parser_expr_no_commas (parser, NULL);
1731 if (TREE_CODE (init.value) == COMPONENT_REF
1732 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1733 error_at (here,
1734 "%<__auto_type%> used with a bit-field"
1735 " initializer");
1736 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1737 tree init_type = TREE_TYPE (init.value);
1738 /* As with typeof, remove all qualifiers from atomic types. */
1739 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1740 init_type
1741 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1742 bool vm_type = variably_modified_type_p (init_type,
1743 NULL_TREE);
1744 if (vm_type)
1745 init.value = c_save_expr (init.value);
1746 finish_init ();
1747 specs->typespec_kind = ctsk_typeof;
1748 specs->locations[cdw_typedef] = init_loc;
1749 specs->typedef_p = true;
1750 specs->type = init_type;
1751 if (vm_type)
1753 bool maybe_const = true;
1754 tree type_expr = c_fully_fold (init.value, false,
1755 &maybe_const);
1756 specs->expr_const_operands &= maybe_const;
1757 if (specs->expr)
1758 specs->expr = build2 (COMPOUND_EXPR,
1759 TREE_TYPE (type_expr),
1760 specs->expr, type_expr);
1761 else
1762 specs->expr = type_expr;
1764 d = start_decl (declarator, specs, true,
1765 chainon (postfix_attrs, all_prefix_attrs));
1766 if (!d)
1767 d = error_mark_node;
1768 if (omp_declare_simd_clauses.exists ()
1769 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1770 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1771 omp_declare_simd_clauses);
1773 else
1775 /* The declaration of the variable is in effect while
1776 its initializer is parsed. */
1777 d = start_decl (declarator, specs, true,
1778 chainon (postfix_attrs, all_prefix_attrs));
1779 if (!d)
1780 d = error_mark_node;
1781 if (omp_declare_simd_clauses.exists ()
1782 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1783 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1784 omp_declare_simd_clauses);
1785 start_init (d, asm_name, global_bindings_p ());
1786 init_loc = c_parser_peek_token (parser)->location;
1787 init = c_parser_initializer (parser);
1788 finish_init ();
1790 if (d != error_mark_node)
1792 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1793 finish_decl (d, init_loc, init.value,
1794 init.original_type, asm_name);
1797 else
1799 if (auto_type_p)
1801 error_at (here,
1802 "%<__auto_type%> requires an initialized "
1803 "data declaration");
1804 c_parser_skip_to_end_of_block_or_statement (parser);
1805 return;
1807 tree d = start_decl (declarator, specs, false,
1808 chainon (postfix_attrs,
1809 all_prefix_attrs));
1810 if (omp_declare_simd_clauses.exists ()
1811 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1813 tree parms = NULL_TREE;
1814 if (d && TREE_CODE (d) == FUNCTION_DECL)
1816 struct c_declarator *ce = declarator;
1817 while (ce != NULL)
1818 if (ce->kind == cdk_function)
1820 parms = ce->u.arg_info->parms;
1821 break;
1823 else
1824 ce = ce->declarator;
1826 if (parms)
1827 temp_store_parm_decls (d, parms);
1828 c_finish_omp_declare_simd (parser, d, parms,
1829 omp_declare_simd_clauses);
1830 if (parms)
1831 temp_pop_parm_decls ();
1833 if (d)
1834 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1835 NULL_TREE, asm_name);
1837 if (c_parser_next_token_is_keyword (parser, RID_IN))
1839 if (d)
1840 *objc_foreach_object_declaration = d;
1841 else
1842 *objc_foreach_object_declaration = error_mark_node;
1845 if (c_parser_next_token_is (parser, CPP_COMMA))
1847 if (auto_type_p)
1849 error_at (here,
1850 "%<__auto_type%> may only be used with"
1851 " a single declarator");
1852 c_parser_skip_to_end_of_block_or_statement (parser);
1853 return;
1855 c_parser_consume_token (parser);
1856 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1857 all_prefix_attrs = chainon (c_parser_attributes (parser),
1858 prefix_attrs);
1859 else
1860 all_prefix_attrs = prefix_attrs;
1861 continue;
1863 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1865 c_parser_consume_token (parser);
1866 return;
1868 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1870 /* This can only happen in Objective-C: we found the
1871 'in' that terminates the declaration inside an
1872 Objective-C foreach statement. Do not consume the
1873 token, so that the caller can use it to determine
1874 that this indeed is a foreach context. */
1875 return;
1877 else
1879 c_parser_error (parser, "expected %<,%> or %<;%>");
1880 c_parser_skip_to_end_of_block_or_statement (parser);
1881 return;
1884 else if (auto_type_p)
1886 error_at (here,
1887 "%<__auto_type%> requires an initialized data declaration");
1888 c_parser_skip_to_end_of_block_or_statement (parser);
1889 return;
1891 else if (!fndef_ok)
1893 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1894 "%<asm%> or %<__attribute__%>");
1895 c_parser_skip_to_end_of_block_or_statement (parser);
1896 return;
1898 /* Function definition (nested or otherwise). */
1899 if (nested)
1901 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1902 c_push_function_context ();
1904 if (!start_function (specs, declarator, all_prefix_attrs))
1906 /* This can appear in many cases looking nothing like a
1907 function definition, so we don't give a more specific
1908 error suggesting there was one. */
1909 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1910 "or %<__attribute__%>");
1911 if (nested)
1912 c_pop_function_context ();
1913 break;
1916 if (DECL_DECLARED_INLINE_P (current_function_decl))
1917 tv = TV_PARSE_INLINE;
1918 else
1919 tv = TV_PARSE_FUNC;
1920 timevar_push (tv);
1922 /* Parse old-style parameter declarations. ??? Attributes are
1923 not allowed to start declaration specifiers here because of a
1924 syntax conflict between a function declaration with attribute
1925 suffix and a function definition with an attribute prefix on
1926 first old-style parameter declaration. Following the old
1927 parser, they are not accepted on subsequent old-style
1928 parameter declarations either. However, there is no
1929 ambiguity after the first declaration, nor indeed on the
1930 first as long as we don't allow postfix attributes after a
1931 declarator with a nonempty identifier list in a definition;
1932 and postfix attributes have never been accepted here in
1933 function definitions either. */
1934 while (c_parser_next_token_is_not (parser, CPP_EOF)
1935 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1936 c_parser_declaration_or_fndef (parser, false, false, false,
1937 true, false, NULL, vNULL);
1938 store_parm_decls ();
1939 if (omp_declare_simd_clauses.exists ()
1940 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1941 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1942 omp_declare_simd_clauses);
1943 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1944 = c_parser_peek_token (parser)->location;
1945 fnbody = c_parser_compound_statement (parser);
1946 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1947 fnbody = expand_array_notation_exprs (fnbody);
1948 if (nested)
1950 tree decl = current_function_decl;
1951 /* Mark nested functions as needing static-chain initially.
1952 lower_nested_functions will recompute it but the
1953 DECL_STATIC_CHAIN flag is also used before that happens,
1954 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1955 DECL_STATIC_CHAIN (decl) = 1;
1956 add_stmt (fnbody);
1957 finish_function ();
1958 c_pop_function_context ();
1959 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1961 else
1963 add_stmt (fnbody);
1964 finish_function ();
1967 timevar_pop (tv);
1968 break;
1972 /* Parse an asm-definition (asm() outside a function body). This is a
1973 GNU extension.
1975 asm-definition:
1976 simple-asm-expr ;
1979 static void
1980 c_parser_asm_definition (c_parser *parser)
1982 tree asm_str = c_parser_simple_asm_expr (parser);
1983 if (asm_str)
1984 symtab->finalize_toplevel_asm (asm_str);
1985 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1988 /* Parse a static assertion (C11 6.7.10).
1990 static_assert-declaration:
1991 static_assert-declaration-no-semi ;
1994 static void
1995 c_parser_static_assert_declaration (c_parser *parser)
1997 c_parser_static_assert_declaration_no_semi (parser);
1998 if (parser->error
1999 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2000 c_parser_skip_to_end_of_block_or_statement (parser);
2003 /* Parse a static assertion (C11 6.7.10), without the trailing
2004 semicolon.
2006 static_assert-declaration-no-semi:
2007 _Static_assert ( constant-expression , string-literal )
2010 static void
2011 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2013 location_t assert_loc, value_loc;
2014 tree value;
2015 tree string;
2017 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2018 assert_loc = c_parser_peek_token (parser)->location;
2019 if (flag_isoc99)
2020 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2021 "ISO C99 does not support %<_Static_assert%>");
2022 else
2023 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2024 "ISO C90 does not support %<_Static_assert%>");
2025 c_parser_consume_token (parser);
2026 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2027 return;
2028 value_loc = c_parser_peek_token (parser)->location;
2029 value = c_parser_expr_no_commas (parser, NULL).value;
2030 parser->lex_untranslated_string = true;
2031 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2033 parser->lex_untranslated_string = false;
2034 return;
2036 switch (c_parser_peek_token (parser)->type)
2038 case CPP_STRING:
2039 case CPP_STRING16:
2040 case CPP_STRING32:
2041 case CPP_WSTRING:
2042 case CPP_UTF8STRING:
2043 string = c_parser_peek_token (parser)->value;
2044 c_parser_consume_token (parser);
2045 parser->lex_untranslated_string = false;
2046 break;
2047 default:
2048 c_parser_error (parser, "expected string literal");
2049 parser->lex_untranslated_string = false;
2050 return;
2052 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2054 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2056 error_at (value_loc, "expression in static assertion is not an integer");
2057 return;
2059 if (TREE_CODE (value) != INTEGER_CST)
2061 value = c_fully_fold (value, false, NULL);
2062 /* Strip no-op conversions. */
2063 STRIP_TYPE_NOPS (value);
2064 if (TREE_CODE (value) == INTEGER_CST)
2065 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2066 "is not an integer constant expression");
2068 if (TREE_CODE (value) != INTEGER_CST)
2070 error_at (value_loc, "expression in static assertion is not constant");
2071 return;
2073 constant_expression_warning (value);
2074 if (integer_zerop (value))
2075 error_at (assert_loc, "static assertion failed: %E", string);
2078 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2079 6.7), adding them to SPECS (which may already include some).
2080 Storage class specifiers are accepted iff SCSPEC_OK; type
2081 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2082 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2083 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2085 declaration-specifiers:
2086 storage-class-specifier declaration-specifiers[opt]
2087 type-specifier declaration-specifiers[opt]
2088 type-qualifier declaration-specifiers[opt]
2089 function-specifier declaration-specifiers[opt]
2090 alignment-specifier declaration-specifiers[opt]
2092 Function specifiers (inline) are from C99, and are currently
2093 handled as storage class specifiers, as is __thread. Alignment
2094 specifiers are from C11.
2096 C90 6.5.1, C99 6.7.1:
2097 storage-class-specifier:
2098 typedef
2099 extern
2100 static
2101 auto
2102 register
2103 _Thread_local
2105 (_Thread_local is new in C11.)
2107 C99 6.7.4:
2108 function-specifier:
2109 inline
2110 _Noreturn
2112 (_Noreturn is new in C11.)
2114 C90 6.5.2, C99 6.7.2:
2115 type-specifier:
2116 void
2117 char
2118 short
2120 long
2121 float
2122 double
2123 signed
2124 unsigned
2125 _Bool
2126 _Complex
2127 [_Imaginary removed in C99 TC2]
2128 struct-or-union-specifier
2129 enum-specifier
2130 typedef-name
2131 atomic-type-specifier
2133 (_Bool and _Complex are new in C99.)
2134 (atomic-type-specifier is new in C11.)
2136 C90 6.5.3, C99 6.7.3:
2138 type-qualifier:
2139 const
2140 restrict
2141 volatile
2142 address-space-qualifier
2143 _Atomic
2145 (restrict is new in C99.)
2146 (_Atomic is new in C11.)
2148 GNU extensions:
2150 declaration-specifiers:
2151 attributes declaration-specifiers[opt]
2153 type-qualifier:
2154 address-space
2156 address-space:
2157 identifier recognized by the target
2159 storage-class-specifier:
2160 __thread
2162 type-specifier:
2163 typeof-specifier
2164 __auto_type
2165 __int128
2166 _Decimal32
2167 _Decimal64
2168 _Decimal128
2169 _Fract
2170 _Accum
2171 _Sat
2173 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2174 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2176 atomic-type-specifier
2177 _Atomic ( type-name )
2179 Objective-C:
2181 type-specifier:
2182 class-name objc-protocol-refs[opt]
2183 typedef-name objc-protocol-refs
2184 objc-protocol-refs
2187 static void
2188 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2189 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2190 bool alignspec_ok, bool auto_type_ok,
2191 enum c_lookahead_kind la)
2193 bool attrs_ok = start_attr_ok;
2194 bool seen_type = specs->typespec_kind != ctsk_none;
2196 if (!typespec_ok)
2197 gcc_assert (la == cla_prefer_id);
2199 while (c_parser_next_token_is (parser, CPP_NAME)
2200 || c_parser_next_token_is (parser, CPP_KEYWORD)
2201 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2203 struct c_typespec t;
2204 tree attrs;
2205 tree align;
2206 location_t loc = c_parser_peek_token (parser)->location;
2208 /* If we cannot accept a type, exit if the next token must start
2209 one. Also, if we already have seen a tagged definition,
2210 a typename would be an error anyway and likely the user
2211 has simply forgotten a semicolon, so we exit. */
2212 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2213 && c_parser_next_tokens_start_typename (parser, la)
2214 && !c_parser_next_token_is_qualifier (parser))
2215 break;
2217 if (c_parser_next_token_is (parser, CPP_NAME))
2219 c_token *name_token = c_parser_peek_token (parser);
2220 tree value = name_token->value;
2221 c_id_kind kind = name_token->id_kind;
2223 if (kind == C_ID_ADDRSPACE)
2225 addr_space_t as
2226 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2227 declspecs_add_addrspace (name_token->location, specs, as);
2228 c_parser_consume_token (parser);
2229 attrs_ok = true;
2230 continue;
2233 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2235 /* If we cannot accept a type, and the next token must start one,
2236 exit. Do the same if we already have seen a tagged definition,
2237 since it would be an error anyway and likely the user has simply
2238 forgotten a semicolon. */
2239 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2240 break;
2242 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2243 a C_ID_CLASSNAME. */
2244 c_parser_consume_token (parser);
2245 seen_type = true;
2246 attrs_ok = true;
2247 if (kind == C_ID_ID)
2249 error_at (loc, "unknown type name %qE", value);
2250 t.kind = ctsk_typedef;
2251 t.spec = error_mark_node;
2253 else if (kind == C_ID_TYPENAME
2254 && (!c_dialect_objc ()
2255 || c_parser_next_token_is_not (parser, CPP_LESS)))
2257 t.kind = ctsk_typedef;
2258 /* For a typedef name, record the meaning, not the name.
2259 In case of 'foo foo, bar;'. */
2260 t.spec = lookup_name (value);
2262 else
2264 tree proto = NULL_TREE;
2265 gcc_assert (c_dialect_objc ());
2266 t.kind = ctsk_objc;
2267 if (c_parser_next_token_is (parser, CPP_LESS))
2268 proto = c_parser_objc_protocol_refs (parser);
2269 t.spec = objc_get_protocol_qualified_type (value, proto);
2271 t.expr = NULL_TREE;
2272 t.expr_const_operands = true;
2273 declspecs_add_type (name_token->location, specs, t);
2274 continue;
2276 if (c_parser_next_token_is (parser, CPP_LESS))
2278 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2279 nisse@lysator.liu.se. */
2280 tree proto;
2281 gcc_assert (c_dialect_objc ());
2282 if (!typespec_ok || seen_type)
2283 break;
2284 proto = c_parser_objc_protocol_refs (parser);
2285 t.kind = ctsk_objc;
2286 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2287 t.expr = NULL_TREE;
2288 t.expr_const_operands = true;
2289 declspecs_add_type (loc, specs, t);
2290 continue;
2292 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2293 switch (c_parser_peek_token (parser)->keyword)
2295 case RID_STATIC:
2296 case RID_EXTERN:
2297 case RID_REGISTER:
2298 case RID_TYPEDEF:
2299 case RID_INLINE:
2300 case RID_NORETURN:
2301 case RID_AUTO:
2302 case RID_THREAD:
2303 if (!scspec_ok)
2304 goto out;
2305 attrs_ok = true;
2306 /* TODO: Distinguish between function specifiers (inline, noreturn)
2307 and storage class specifiers, either here or in
2308 declspecs_add_scspec. */
2309 declspecs_add_scspec (loc, specs,
2310 c_parser_peek_token (parser)->value);
2311 c_parser_consume_token (parser);
2312 break;
2313 case RID_AUTO_TYPE:
2314 if (!auto_type_ok)
2315 goto out;
2316 /* Fall through. */
2317 case RID_UNSIGNED:
2318 case RID_LONG:
2319 case RID_INT128:
2320 case RID_SHORT:
2321 case RID_SIGNED:
2322 case RID_COMPLEX:
2323 case RID_INT:
2324 case RID_CHAR:
2325 case RID_FLOAT:
2326 case RID_DOUBLE:
2327 case RID_VOID:
2328 case RID_DFLOAT32:
2329 case RID_DFLOAT64:
2330 case RID_DFLOAT128:
2331 case RID_BOOL:
2332 case RID_FRACT:
2333 case RID_ACCUM:
2334 case RID_SAT:
2335 if (!typespec_ok)
2336 goto out;
2337 attrs_ok = true;
2338 seen_type = true;
2339 if (c_dialect_objc ())
2340 parser->objc_need_raw_identifier = true;
2341 t.kind = ctsk_resword;
2342 t.spec = c_parser_peek_token (parser)->value;
2343 t.expr = NULL_TREE;
2344 t.expr_const_operands = true;
2345 declspecs_add_type (loc, specs, t);
2346 c_parser_consume_token (parser);
2347 break;
2348 case RID_ENUM:
2349 if (!typespec_ok)
2350 goto out;
2351 attrs_ok = true;
2352 seen_type = true;
2353 t = c_parser_enum_specifier (parser);
2354 declspecs_add_type (loc, specs, t);
2355 break;
2356 case RID_STRUCT:
2357 case RID_UNION:
2358 if (!typespec_ok)
2359 goto out;
2360 attrs_ok = true;
2361 seen_type = true;
2362 t = c_parser_struct_or_union_specifier (parser);
2363 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2364 declspecs_add_type (loc, specs, t);
2365 break;
2366 case RID_TYPEOF:
2367 /* ??? The old parser rejected typeof after other type
2368 specifiers, but is a syntax error the best way of
2369 handling this? */
2370 if (!typespec_ok || seen_type)
2371 goto out;
2372 attrs_ok = true;
2373 seen_type = true;
2374 t = c_parser_typeof_specifier (parser);
2375 declspecs_add_type (loc, specs, t);
2376 break;
2377 case RID_ATOMIC:
2378 /* C parser handling of Objective-C constructs needs
2379 checking for correct lvalue-to-rvalue conversions, and
2380 the code in build_modify_expr handling various
2381 Objective-C cases, and that in build_unary_op handling
2382 Objective-C cases for increment / decrement, also needs
2383 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2384 and objc_types_are_equivalent may also need updates. */
2385 if (c_dialect_objc ())
2386 sorry ("%<_Atomic%> in Objective-C");
2387 /* C parser handling of OpenMP constructs needs checking for
2388 correct lvalue-to-rvalue conversions. */
2389 if (flag_openmp)
2390 sorry ("%<_Atomic%> with OpenMP");
2391 if (flag_isoc99)
2392 pedwarn_c99 (loc, OPT_Wpedantic,
2393 "ISO C99 does not support the %<_Atomic%> qualifier");
2394 else
2395 pedwarn_c99 (loc, OPT_Wpedantic,
2396 "ISO C90 does not support the %<_Atomic%> qualifier");
2397 attrs_ok = true;
2398 tree value;
2399 value = c_parser_peek_token (parser)->value;
2400 c_parser_consume_token (parser);
2401 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2403 /* _Atomic ( type-name ). */
2404 seen_type = true;
2405 c_parser_consume_token (parser);
2406 struct c_type_name *type = c_parser_type_name (parser);
2407 t.kind = ctsk_typeof;
2408 t.spec = error_mark_node;
2409 t.expr = NULL_TREE;
2410 t.expr_const_operands = true;
2411 if (type != NULL)
2412 t.spec = groktypename (type, &t.expr,
2413 &t.expr_const_operands);
2414 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2415 "expected %<)%>");
2416 if (t.spec != error_mark_node)
2418 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2419 error_at (loc, "%<_Atomic%>-qualified array type");
2420 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2421 error_at (loc, "%<_Atomic%>-qualified function type");
2422 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2423 error_at (loc, "%<_Atomic%> applied to a qualified type");
2424 else
2425 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2427 declspecs_add_type (loc, specs, t);
2429 else
2430 declspecs_add_qual (loc, specs, value);
2431 break;
2432 case RID_CONST:
2433 case RID_VOLATILE:
2434 case RID_RESTRICT:
2435 attrs_ok = true;
2436 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2437 c_parser_consume_token (parser);
2438 break;
2439 case RID_ATTRIBUTE:
2440 if (!attrs_ok)
2441 goto out;
2442 attrs = c_parser_attributes (parser);
2443 declspecs_add_attrs (loc, specs, attrs);
2444 break;
2445 case RID_ALIGNAS:
2446 if (!alignspec_ok)
2447 goto out;
2448 align = c_parser_alignas_specifier (parser);
2449 declspecs_add_alignas (loc, specs, align);
2450 break;
2451 default:
2452 goto out;
2455 out: ;
2458 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2460 enum-specifier:
2461 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2462 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2463 enum attributes[opt] identifier
2465 The form with trailing comma is new in C99. The forms with
2466 attributes are GNU extensions. In GNU C, we accept any expression
2467 without commas in the syntax (assignment expressions, not just
2468 conditional expressions); assignment expressions will be diagnosed
2469 as non-constant.
2471 enumerator-list:
2472 enumerator
2473 enumerator-list , enumerator
2475 enumerator:
2476 enumeration-constant
2477 enumeration-constant = constant-expression
2480 static struct c_typespec
2481 c_parser_enum_specifier (c_parser *parser)
2483 struct c_typespec ret;
2484 tree attrs;
2485 tree ident = NULL_TREE;
2486 location_t enum_loc;
2487 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2488 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2489 enum_loc = c_parser_peek_token (parser)->location;
2490 c_parser_consume_token (parser);
2491 attrs = c_parser_attributes (parser);
2492 enum_loc = c_parser_peek_token (parser)->location;
2493 /* Set the location in case we create a decl now. */
2494 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2495 if (c_parser_next_token_is (parser, CPP_NAME))
2497 ident = c_parser_peek_token (parser)->value;
2498 ident_loc = c_parser_peek_token (parser)->location;
2499 enum_loc = ident_loc;
2500 c_parser_consume_token (parser);
2502 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2504 /* Parse an enum definition. */
2505 struct c_enum_contents the_enum;
2506 tree type;
2507 tree postfix_attrs;
2508 /* We chain the enumerators in reverse order, then put them in
2509 forward order at the end. */
2510 tree values;
2511 timevar_push (TV_PARSE_ENUM);
2512 type = start_enum (enum_loc, &the_enum, ident);
2513 values = NULL_TREE;
2514 c_parser_consume_token (parser);
2515 while (true)
2517 tree enum_id;
2518 tree enum_value;
2519 tree enum_decl;
2520 bool seen_comma;
2521 c_token *token;
2522 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2523 location_t decl_loc, value_loc;
2524 if (c_parser_next_token_is_not (parser, CPP_NAME))
2526 c_parser_error (parser, "expected identifier");
2527 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2528 values = error_mark_node;
2529 break;
2531 token = c_parser_peek_token (parser);
2532 enum_id = token->value;
2533 /* Set the location in case we create a decl now. */
2534 c_parser_set_source_position_from_token (token);
2535 decl_loc = value_loc = token->location;
2536 c_parser_consume_token (parser);
2537 if (c_parser_next_token_is (parser, CPP_EQ))
2539 c_parser_consume_token (parser);
2540 value_loc = c_parser_peek_token (parser)->location;
2541 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2543 else
2544 enum_value = NULL_TREE;
2545 enum_decl = build_enumerator (decl_loc, value_loc,
2546 &the_enum, enum_id, enum_value);
2547 TREE_CHAIN (enum_decl) = values;
2548 values = enum_decl;
2549 seen_comma = false;
2550 if (c_parser_next_token_is (parser, CPP_COMMA))
2552 comma_loc = c_parser_peek_token (parser)->location;
2553 seen_comma = true;
2554 c_parser_consume_token (parser);
2556 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2558 if (seen_comma)
2559 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2560 "comma at end of enumerator list");
2561 c_parser_consume_token (parser);
2562 break;
2564 if (!seen_comma)
2566 c_parser_error (parser, "expected %<,%> or %<}%>");
2567 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2568 values = error_mark_node;
2569 break;
2572 postfix_attrs = c_parser_attributes (parser);
2573 ret.spec = finish_enum (type, nreverse (values),
2574 chainon (attrs, postfix_attrs));
2575 ret.kind = ctsk_tagdef;
2576 ret.expr = NULL_TREE;
2577 ret.expr_const_operands = true;
2578 timevar_pop (TV_PARSE_ENUM);
2579 return ret;
2581 else if (!ident)
2583 c_parser_error (parser, "expected %<{%>");
2584 ret.spec = error_mark_node;
2585 ret.kind = ctsk_tagref;
2586 ret.expr = NULL_TREE;
2587 ret.expr_const_operands = true;
2588 return ret;
2590 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2591 /* In ISO C, enumerated types can be referred to only if already
2592 defined. */
2593 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2595 gcc_assert (ident);
2596 pedwarn (enum_loc, OPT_Wpedantic,
2597 "ISO C forbids forward references to %<enum%> types");
2599 return ret;
2602 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2604 struct-or-union-specifier:
2605 struct-or-union attributes[opt] identifier[opt]
2606 { struct-contents } attributes[opt]
2607 struct-or-union attributes[opt] identifier
2609 struct-contents:
2610 struct-declaration-list
2612 struct-declaration-list:
2613 struct-declaration ;
2614 struct-declaration-list struct-declaration ;
2616 GNU extensions:
2618 struct-contents:
2619 empty
2620 struct-declaration
2621 struct-declaration-list struct-declaration
2623 struct-declaration-list:
2624 struct-declaration-list ;
2627 (Note that in the syntax here, unlike that in ISO C, the semicolons
2628 are included here rather than in struct-declaration, in order to
2629 describe the syntax with extra semicolons and missing semicolon at
2630 end.)
2632 Objective-C:
2634 struct-declaration-list:
2635 @defs ( class-name )
2637 (Note this does not include a trailing semicolon, but can be
2638 followed by further declarations, and gets a pedwarn-if-pedantic
2639 when followed by a semicolon.) */
2641 static struct c_typespec
2642 c_parser_struct_or_union_specifier (c_parser *parser)
2644 struct c_typespec ret;
2645 tree attrs;
2646 tree ident = NULL_TREE;
2647 location_t struct_loc;
2648 location_t ident_loc = UNKNOWN_LOCATION;
2649 enum tree_code code;
2650 switch (c_parser_peek_token (parser)->keyword)
2652 case RID_STRUCT:
2653 code = RECORD_TYPE;
2654 break;
2655 case RID_UNION:
2656 code = UNION_TYPE;
2657 break;
2658 default:
2659 gcc_unreachable ();
2661 struct_loc = c_parser_peek_token (parser)->location;
2662 c_parser_consume_token (parser);
2663 attrs = c_parser_attributes (parser);
2665 /* Set the location in case we create a decl now. */
2666 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2668 if (c_parser_next_token_is (parser, CPP_NAME))
2670 ident = c_parser_peek_token (parser)->value;
2671 ident_loc = c_parser_peek_token (parser)->location;
2672 struct_loc = ident_loc;
2673 c_parser_consume_token (parser);
2675 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2677 /* Parse a struct or union definition. Start the scope of the
2678 tag before parsing components. */
2679 struct c_struct_parse_info *struct_info;
2680 tree type = start_struct (struct_loc, code, ident, &struct_info);
2681 tree postfix_attrs;
2682 /* We chain the components in reverse order, then put them in
2683 forward order at the end. Each struct-declaration may
2684 declare multiple components (comma-separated), so we must use
2685 chainon to join them, although when parsing each
2686 struct-declaration we can use TREE_CHAIN directly.
2688 The theory behind all this is that there will be more
2689 semicolon separated fields than comma separated fields, and
2690 so we'll be minimizing the number of node traversals required
2691 by chainon. */
2692 tree contents;
2693 timevar_push (TV_PARSE_STRUCT);
2694 contents = NULL_TREE;
2695 c_parser_consume_token (parser);
2696 /* Handle the Objective-C @defs construct,
2697 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2698 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2700 tree name;
2701 gcc_assert (c_dialect_objc ());
2702 c_parser_consume_token (parser);
2703 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2704 goto end_at_defs;
2705 if (c_parser_next_token_is (parser, CPP_NAME)
2706 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2708 name = c_parser_peek_token (parser)->value;
2709 c_parser_consume_token (parser);
2711 else
2713 c_parser_error (parser, "expected class name");
2714 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2715 goto end_at_defs;
2717 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2718 "expected %<)%>");
2719 contents = nreverse (objc_get_class_ivars (name));
2721 end_at_defs:
2722 /* Parse the struct-declarations and semicolons. Problems with
2723 semicolons are diagnosed here; empty structures are diagnosed
2724 elsewhere. */
2725 while (true)
2727 tree decls;
2728 /* Parse any stray semicolon. */
2729 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2731 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2732 "extra semicolon in struct or union specified");
2733 c_parser_consume_token (parser);
2734 continue;
2736 /* Stop if at the end of the struct or union contents. */
2737 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2739 c_parser_consume_token (parser);
2740 break;
2742 /* Accept #pragmas at struct scope. */
2743 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2745 c_parser_pragma (parser, pragma_struct);
2746 continue;
2748 /* Parse some comma-separated declarations, but not the
2749 trailing semicolon if any. */
2750 decls = c_parser_struct_declaration (parser);
2751 contents = chainon (decls, contents);
2752 /* If no semicolon follows, either we have a parse error or
2753 are at the end of the struct or union and should
2754 pedwarn. */
2755 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2756 c_parser_consume_token (parser);
2757 else
2759 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2760 pedwarn (c_parser_peek_token (parser)->location, 0,
2761 "no semicolon at end of struct or union");
2762 else if (parser->error
2763 || !c_parser_next_token_starts_declspecs (parser))
2765 c_parser_error (parser, "expected %<;%>");
2766 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2767 break;
2770 /* If we come here, we have already emitted an error
2771 for an expected `;', identifier or `(', and we also
2772 recovered already. Go on with the next field. */
2775 postfix_attrs = c_parser_attributes (parser);
2776 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2777 chainon (attrs, postfix_attrs), struct_info);
2778 ret.kind = ctsk_tagdef;
2779 ret.expr = NULL_TREE;
2780 ret.expr_const_operands = true;
2781 timevar_pop (TV_PARSE_STRUCT);
2782 return ret;
2784 else if (!ident)
2786 c_parser_error (parser, "expected %<{%>");
2787 ret.spec = error_mark_node;
2788 ret.kind = ctsk_tagref;
2789 ret.expr = NULL_TREE;
2790 ret.expr_const_operands = true;
2791 return ret;
2793 ret = parser_xref_tag (ident_loc, code, ident);
2794 return ret;
2797 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2798 the trailing semicolon.
2800 struct-declaration:
2801 specifier-qualifier-list struct-declarator-list
2802 static_assert-declaration-no-semi
2804 specifier-qualifier-list:
2805 type-specifier specifier-qualifier-list[opt]
2806 type-qualifier specifier-qualifier-list[opt]
2807 attributes specifier-qualifier-list[opt]
2809 struct-declarator-list:
2810 struct-declarator
2811 struct-declarator-list , attributes[opt] struct-declarator
2813 struct-declarator:
2814 declarator attributes[opt]
2815 declarator[opt] : constant-expression attributes[opt]
2817 GNU extensions:
2819 struct-declaration:
2820 __extension__ struct-declaration
2821 specifier-qualifier-list
2823 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2824 of attributes where shown is a GNU extension. In GNU C, we accept
2825 any expression without commas in the syntax (assignment
2826 expressions, not just conditional expressions); assignment
2827 expressions will be diagnosed as non-constant. */
2829 static tree
2830 c_parser_struct_declaration (c_parser *parser)
2832 struct c_declspecs *specs;
2833 tree prefix_attrs;
2834 tree all_prefix_attrs;
2835 tree decls;
2836 location_t decl_loc;
2837 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2839 int ext;
2840 tree decl;
2841 ext = disable_extension_diagnostics ();
2842 c_parser_consume_token (parser);
2843 decl = c_parser_struct_declaration (parser);
2844 restore_extension_diagnostics (ext);
2845 return decl;
2847 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2849 c_parser_static_assert_declaration_no_semi (parser);
2850 return NULL_TREE;
2852 specs = build_null_declspecs ();
2853 decl_loc = c_parser_peek_token (parser)->location;
2854 /* Strictly by the standard, we shouldn't allow _Alignas here,
2855 but it appears to have been intended to allow it there, so
2856 we're keeping it as it is until WG14 reaches a conclusion
2857 of N1731.
2858 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2859 c_parser_declspecs (parser, specs, false, true, true,
2860 true, false, cla_nonabstract_decl);
2861 if (parser->error)
2862 return NULL_TREE;
2863 if (!specs->declspecs_seen_p)
2865 c_parser_error (parser, "expected specifier-qualifier-list");
2866 return NULL_TREE;
2868 finish_declspecs (specs);
2869 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2870 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2872 tree ret;
2873 if (specs->typespec_kind == ctsk_none)
2875 pedwarn (decl_loc, OPT_Wpedantic,
2876 "ISO C forbids member declarations with no members");
2877 shadow_tag_warned (specs, pedantic);
2878 ret = NULL_TREE;
2880 else
2882 /* Support for unnamed structs or unions as members of
2883 structs or unions (which is [a] useful and [b] supports
2884 MS P-SDK). */
2885 tree attrs = NULL;
2887 ret = grokfield (c_parser_peek_token (parser)->location,
2888 build_id_declarator (NULL_TREE), specs,
2889 NULL_TREE, &attrs);
2890 if (ret)
2891 decl_attributes (&ret, attrs, 0);
2893 return ret;
2896 /* Provide better error recovery. Note that a type name here is valid,
2897 and will be treated as a field name. */
2898 if (specs->typespec_kind == ctsk_tagdef
2899 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2900 && c_parser_next_token_starts_declspecs (parser)
2901 && !c_parser_next_token_is (parser, CPP_NAME))
2903 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2904 parser->error = false;
2905 return NULL_TREE;
2908 pending_xref_error ();
2909 prefix_attrs = specs->attrs;
2910 all_prefix_attrs = prefix_attrs;
2911 specs->attrs = NULL_TREE;
2912 decls = NULL_TREE;
2913 while (true)
2915 /* Declaring one or more declarators or un-named bit-fields. */
2916 struct c_declarator *declarator;
2917 bool dummy = false;
2918 if (c_parser_next_token_is (parser, CPP_COLON))
2919 declarator = build_id_declarator (NULL_TREE);
2920 else
2921 declarator = c_parser_declarator (parser,
2922 specs->typespec_kind != ctsk_none,
2923 C_DTR_NORMAL, &dummy);
2924 if (declarator == NULL)
2926 c_parser_skip_to_end_of_block_or_statement (parser);
2927 break;
2929 if (c_parser_next_token_is (parser, CPP_COLON)
2930 || c_parser_next_token_is (parser, CPP_COMMA)
2931 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2932 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2933 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2935 tree postfix_attrs = NULL_TREE;
2936 tree width = NULL_TREE;
2937 tree d;
2938 if (c_parser_next_token_is (parser, CPP_COLON))
2940 c_parser_consume_token (parser);
2941 width = c_parser_expr_no_commas (parser, NULL).value;
2943 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2944 postfix_attrs = c_parser_attributes (parser);
2945 d = grokfield (c_parser_peek_token (parser)->location,
2946 declarator, specs, width, &all_prefix_attrs);
2947 decl_attributes (&d, chainon (postfix_attrs,
2948 all_prefix_attrs), 0);
2949 DECL_CHAIN (d) = decls;
2950 decls = d;
2951 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2952 all_prefix_attrs = chainon (c_parser_attributes (parser),
2953 prefix_attrs);
2954 else
2955 all_prefix_attrs = prefix_attrs;
2956 if (c_parser_next_token_is (parser, CPP_COMMA))
2957 c_parser_consume_token (parser);
2958 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2959 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2961 /* Semicolon consumed in caller. */
2962 break;
2964 else
2966 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2967 break;
2970 else
2972 c_parser_error (parser,
2973 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2974 "%<__attribute__%>");
2975 break;
2978 return decls;
2981 /* Parse a typeof specifier (a GNU extension).
2983 typeof-specifier:
2984 typeof ( expression )
2985 typeof ( type-name )
2988 static struct c_typespec
2989 c_parser_typeof_specifier (c_parser *parser)
2991 struct c_typespec ret;
2992 ret.kind = ctsk_typeof;
2993 ret.spec = error_mark_node;
2994 ret.expr = NULL_TREE;
2995 ret.expr_const_operands = true;
2996 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2997 c_parser_consume_token (parser);
2998 c_inhibit_evaluation_warnings++;
2999 in_typeof++;
3000 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3002 c_inhibit_evaluation_warnings--;
3003 in_typeof--;
3004 return ret;
3006 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3008 struct c_type_name *type = c_parser_type_name (parser);
3009 c_inhibit_evaluation_warnings--;
3010 in_typeof--;
3011 if (type != NULL)
3013 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3014 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3017 else
3019 bool was_vm;
3020 location_t here = c_parser_peek_token (parser)->location;
3021 struct c_expr expr = c_parser_expression (parser);
3022 c_inhibit_evaluation_warnings--;
3023 in_typeof--;
3024 if (TREE_CODE (expr.value) == COMPONENT_REF
3025 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3026 error_at (here, "%<typeof%> applied to a bit-field");
3027 mark_exp_read (expr.value);
3028 ret.spec = TREE_TYPE (expr.value);
3029 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3030 /* This is returned with the type so that when the type is
3031 evaluated, this can be evaluated. */
3032 if (was_vm)
3033 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3034 pop_maybe_used (was_vm);
3035 /* For use in macros such as those in <stdatomic.h>, remove all
3036 qualifiers from atomic types. (const can be an issue for more macros
3037 using typeof than just the <stdatomic.h> ones.) */
3038 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3039 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3041 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3042 return ret;
3045 /* Parse an alignment-specifier.
3047 C11 6.7.5:
3049 alignment-specifier:
3050 _Alignas ( type-name )
3051 _Alignas ( constant-expression )
3054 static tree
3055 c_parser_alignas_specifier (c_parser * parser)
3057 tree ret = error_mark_node;
3058 location_t loc = c_parser_peek_token (parser)->location;
3059 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3060 c_parser_consume_token (parser);
3061 if (flag_isoc99)
3062 pedwarn_c99 (loc, OPT_Wpedantic,
3063 "ISO C99 does not support %<_Alignas%>");
3064 else
3065 pedwarn_c99 (loc, OPT_Wpedantic,
3066 "ISO C90 does not support %<_Alignas%>");
3067 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3068 return ret;
3069 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3071 struct c_type_name *type = c_parser_type_name (parser);
3072 if (type != NULL)
3073 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3074 false, true, 1);
3076 else
3077 ret = c_parser_expr_no_commas (parser, NULL).value;
3078 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3079 return ret;
3082 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3083 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3084 be redeclared; otherwise it may not. KIND indicates which kind of
3085 declarator is wanted. Returns a valid declarator except in the
3086 case of a syntax error in which case NULL is returned. *SEEN_ID is
3087 set to true if an identifier being declared is seen; this is used
3088 to diagnose bad forms of abstract array declarators and to
3089 determine whether an identifier list is syntactically permitted.
3091 declarator:
3092 pointer[opt] direct-declarator
3094 direct-declarator:
3095 identifier
3096 ( attributes[opt] declarator )
3097 direct-declarator array-declarator
3098 direct-declarator ( parameter-type-list )
3099 direct-declarator ( identifier-list[opt] )
3101 pointer:
3102 * type-qualifier-list[opt]
3103 * type-qualifier-list[opt] pointer
3105 type-qualifier-list:
3106 type-qualifier
3107 attributes
3108 type-qualifier-list type-qualifier
3109 type-qualifier-list attributes
3111 array-declarator:
3112 [ type-qualifier-list[opt] assignment-expression[opt] ]
3113 [ static type-qualifier-list[opt] assignment-expression ]
3114 [ type-qualifier-list static assignment-expression ]
3115 [ type-qualifier-list[opt] * ]
3117 parameter-type-list:
3118 parameter-list
3119 parameter-list , ...
3121 parameter-list:
3122 parameter-declaration
3123 parameter-list , parameter-declaration
3125 parameter-declaration:
3126 declaration-specifiers declarator attributes[opt]
3127 declaration-specifiers abstract-declarator[opt] attributes[opt]
3129 identifier-list:
3130 identifier
3131 identifier-list , identifier
3133 abstract-declarator:
3134 pointer
3135 pointer[opt] direct-abstract-declarator
3137 direct-abstract-declarator:
3138 ( attributes[opt] abstract-declarator )
3139 direct-abstract-declarator[opt] array-declarator
3140 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3142 GNU extensions:
3144 direct-declarator:
3145 direct-declarator ( parameter-forward-declarations
3146 parameter-type-list[opt] )
3148 direct-abstract-declarator:
3149 direct-abstract-declarator[opt] ( parameter-forward-declarations
3150 parameter-type-list[opt] )
3152 parameter-forward-declarations:
3153 parameter-list ;
3154 parameter-forward-declarations parameter-list ;
3156 The uses of attributes shown above are GNU extensions.
3158 Some forms of array declarator are not included in C99 in the
3159 syntax for abstract declarators; these are disallowed elsewhere.
3160 This may be a defect (DR#289).
3162 This function also accepts an omitted abstract declarator as being
3163 an abstract declarator, although not part of the formal syntax. */
3165 static struct c_declarator *
3166 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3167 bool *seen_id)
3169 /* Parse any initial pointer part. */
3170 if (c_parser_next_token_is (parser, CPP_MULT))
3172 struct c_declspecs *quals_attrs = build_null_declspecs ();
3173 struct c_declarator *inner;
3174 c_parser_consume_token (parser);
3175 c_parser_declspecs (parser, quals_attrs, false, false, true,
3176 false, false, cla_prefer_id);
3177 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3178 if (inner == NULL)
3179 return NULL;
3180 else
3181 return make_pointer_declarator (quals_attrs, inner);
3183 /* Now we have a direct declarator, direct abstract declarator or
3184 nothing (which counts as a direct abstract declarator here). */
3185 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3188 /* Parse a direct declarator or direct abstract declarator; arguments
3189 as c_parser_declarator. */
3191 static struct c_declarator *
3192 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3193 bool *seen_id)
3195 /* The direct declarator must start with an identifier (possibly
3196 omitted) or a parenthesized declarator (possibly abstract). In
3197 an ordinary declarator, initial parentheses must start a
3198 parenthesized declarator. In an abstract declarator or parameter
3199 declarator, they could start a parenthesized declarator or a
3200 parameter list. To tell which, the open parenthesis and any
3201 following attributes must be read. If a declaration specifier
3202 follows, then it is a parameter list; if the specifier is a
3203 typedef name, there might be an ambiguity about redeclaring it,
3204 which is resolved in the direction of treating it as a typedef
3205 name. If a close parenthesis follows, it is also an empty
3206 parameter list, as the syntax does not permit empty abstract
3207 declarators. Otherwise, it is a parenthesized declarator (in
3208 which case the analysis may be repeated inside it, recursively).
3210 ??? There is an ambiguity in a parameter declaration "int
3211 (__attribute__((foo)) x)", where x is not a typedef name: it
3212 could be an abstract declarator for a function, or declare x with
3213 parentheses. The proper resolution of this ambiguity needs
3214 documenting. At present we follow an accident of the old
3215 parser's implementation, whereby the first parameter must have
3216 some declaration specifiers other than just attributes. Thus as
3217 a parameter declaration it is treated as a parenthesized
3218 parameter named x, and as an abstract declarator it is
3219 rejected.
3221 ??? Also following the old parser, attributes inside an empty
3222 parameter list are ignored, making it a list not yielding a
3223 prototype, rather than giving an error or making it have one
3224 parameter with implicit type int.
3226 ??? Also following the old parser, typedef names may be
3227 redeclared in declarators, but not Objective-C class names. */
3229 if (kind != C_DTR_ABSTRACT
3230 && c_parser_next_token_is (parser, CPP_NAME)
3231 && ((type_seen_p
3232 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3233 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3234 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3236 struct c_declarator *inner
3237 = build_id_declarator (c_parser_peek_token (parser)->value);
3238 *seen_id = true;
3239 inner->id_loc = c_parser_peek_token (parser)->location;
3240 c_parser_consume_token (parser);
3241 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3244 if (kind != C_DTR_NORMAL
3245 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3247 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3248 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3251 /* Either we are at the end of an abstract declarator, or we have
3252 parentheses. */
3254 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3256 tree attrs;
3257 struct c_declarator *inner;
3258 c_parser_consume_token (parser);
3259 attrs = c_parser_attributes (parser);
3260 if (kind != C_DTR_NORMAL
3261 && (c_parser_next_token_starts_declspecs (parser)
3262 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3264 struct c_arg_info *args
3265 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3266 attrs);
3267 if (args == NULL)
3268 return NULL;
3269 else
3271 inner
3272 = build_function_declarator (args,
3273 build_id_declarator (NULL_TREE));
3274 return c_parser_direct_declarator_inner (parser, *seen_id,
3275 inner);
3278 /* A parenthesized declarator. */
3279 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3280 if (inner != NULL && attrs != NULL)
3281 inner = build_attrs_declarator (attrs, inner);
3282 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3284 c_parser_consume_token (parser);
3285 if (inner == NULL)
3286 return NULL;
3287 else
3288 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3290 else
3292 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3293 "expected %<)%>");
3294 return NULL;
3297 else
3299 if (kind == C_DTR_NORMAL)
3301 c_parser_error (parser, "expected identifier or %<(%>");
3302 return NULL;
3304 else
3305 return build_id_declarator (NULL_TREE);
3309 /* Parse part of a direct declarator or direct abstract declarator,
3310 given that some (in INNER) has already been parsed; ID_PRESENT is
3311 true if an identifier is present, false for an abstract
3312 declarator. */
3314 static struct c_declarator *
3315 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3316 struct c_declarator *inner)
3318 /* Parse a sequence of array declarators and parameter lists. */
3319 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3321 location_t brace_loc = c_parser_peek_token (parser)->location;
3322 struct c_declarator *declarator;
3323 struct c_declspecs *quals_attrs = build_null_declspecs ();
3324 bool static_seen;
3325 bool star_seen;
3326 struct c_expr dimen;
3327 dimen.value = NULL_TREE;
3328 dimen.original_code = ERROR_MARK;
3329 dimen.original_type = NULL_TREE;
3330 c_parser_consume_token (parser);
3331 c_parser_declspecs (parser, quals_attrs, false, false, true,
3332 false, false, cla_prefer_id);
3333 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3334 if (static_seen)
3335 c_parser_consume_token (parser);
3336 if (static_seen && !quals_attrs->declspecs_seen_p)
3337 c_parser_declspecs (parser, quals_attrs, false, false, true,
3338 false, false, cla_prefer_id);
3339 if (!quals_attrs->declspecs_seen_p)
3340 quals_attrs = NULL;
3341 /* If "static" is present, there must be an array dimension.
3342 Otherwise, there may be a dimension, "*", or no
3343 dimension. */
3344 if (static_seen)
3346 star_seen = false;
3347 dimen = c_parser_expr_no_commas (parser, NULL);
3349 else
3351 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3353 dimen.value = NULL_TREE;
3354 star_seen = false;
3356 else if (flag_cilkplus
3357 && c_parser_next_token_is (parser, CPP_COLON))
3359 dimen.value = error_mark_node;
3360 star_seen = false;
3361 error_at (c_parser_peek_token (parser)->location,
3362 "array notations cannot be used in declaration");
3363 c_parser_consume_token (parser);
3365 else if (c_parser_next_token_is (parser, CPP_MULT))
3367 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3369 dimen.value = NULL_TREE;
3370 star_seen = true;
3371 c_parser_consume_token (parser);
3373 else
3375 star_seen = false;
3376 dimen = c_parser_expr_no_commas (parser, NULL);
3379 else
3381 star_seen = false;
3382 dimen = c_parser_expr_no_commas (parser, NULL);
3385 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3386 c_parser_consume_token (parser);
3387 else if (flag_cilkplus
3388 && c_parser_next_token_is (parser, CPP_COLON))
3390 error_at (c_parser_peek_token (parser)->location,
3391 "array notations cannot be used in declaration");
3392 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3393 return NULL;
3395 else
3397 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3398 "expected %<]%>");
3399 return NULL;
3401 if (dimen.value)
3402 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3403 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3404 static_seen, star_seen);
3405 if (declarator == NULL)
3406 return NULL;
3407 inner = set_array_declarator_inner (declarator, inner);
3408 return c_parser_direct_declarator_inner (parser, id_present, inner);
3410 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3412 tree attrs;
3413 struct c_arg_info *args;
3414 c_parser_consume_token (parser);
3415 attrs = c_parser_attributes (parser);
3416 args = c_parser_parms_declarator (parser, id_present, attrs);
3417 if (args == NULL)
3418 return NULL;
3419 else
3421 inner = build_function_declarator (args, inner);
3422 return c_parser_direct_declarator_inner (parser, id_present, inner);
3425 return inner;
3428 /* Parse a parameter list or identifier list, including the closing
3429 parenthesis but not the opening one. ATTRS are the attributes at
3430 the start of the list. ID_LIST_OK is true if an identifier list is
3431 acceptable; such a list must not have attributes at the start. */
3433 static struct c_arg_info *
3434 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3436 push_scope ();
3437 declare_parm_level ();
3438 /* If the list starts with an identifier, it is an identifier list.
3439 Otherwise, it is either a prototype list or an empty list. */
3440 if (id_list_ok
3441 && !attrs
3442 && c_parser_next_token_is (parser, CPP_NAME)
3443 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3445 /* Look ahead to detect typos in type names. */
3446 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3447 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3448 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3449 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3451 tree list = NULL_TREE, *nextp = &list;
3452 while (c_parser_next_token_is (parser, CPP_NAME)
3453 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3455 *nextp = build_tree_list (NULL_TREE,
3456 c_parser_peek_token (parser)->value);
3457 nextp = & TREE_CHAIN (*nextp);
3458 c_parser_consume_token (parser);
3459 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3460 break;
3461 c_parser_consume_token (parser);
3462 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3464 c_parser_error (parser, "expected identifier");
3465 break;
3468 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3470 struct c_arg_info *ret = build_arg_info ();
3471 ret->types = list;
3472 c_parser_consume_token (parser);
3473 pop_scope ();
3474 return ret;
3476 else
3478 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3479 "expected %<)%>");
3480 pop_scope ();
3481 return NULL;
3484 else
3486 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3487 NULL);
3488 pop_scope ();
3489 return ret;
3493 /* Parse a parameter list (possibly empty), including the closing
3494 parenthesis but not the opening one. ATTRS are the attributes at
3495 the start of the list. EXPR is NULL or an expression that needs to
3496 be evaluated for the side effects of array size expressions in the
3497 parameters. */
3499 static struct c_arg_info *
3500 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3502 bool bad_parm = false;
3504 /* ??? Following the old parser, forward parameter declarations may
3505 use abstract declarators, and if no real parameter declarations
3506 follow the forward declarations then this is not diagnosed. Also
3507 note as above that attributes are ignored as the only contents of
3508 the parentheses, or as the only contents after forward
3509 declarations. */
3510 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3512 struct c_arg_info *ret = build_arg_info ();
3513 c_parser_consume_token (parser);
3514 return ret;
3516 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3518 struct c_arg_info *ret = build_arg_info ();
3520 if (flag_allow_parameterless_variadic_functions)
3522 /* F (...) is allowed. */
3523 ret->types = NULL_TREE;
3525 else
3527 /* Suppress -Wold-style-definition for this case. */
3528 ret->types = error_mark_node;
3529 error_at (c_parser_peek_token (parser)->location,
3530 "ISO C requires a named argument before %<...%>");
3532 c_parser_consume_token (parser);
3533 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3535 c_parser_consume_token (parser);
3536 return ret;
3538 else
3540 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3541 "expected %<)%>");
3542 return NULL;
3545 /* Nonempty list of parameters, either terminated with semicolon
3546 (forward declarations; recurse) or with close parenthesis (normal
3547 function) or with ", ... )" (variadic function). */
3548 while (true)
3550 /* Parse a parameter. */
3551 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3552 attrs = NULL_TREE;
3553 if (parm == NULL)
3554 bad_parm = true;
3555 else
3556 push_parm_decl (parm, &expr);
3557 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3559 tree new_attrs;
3560 c_parser_consume_token (parser);
3561 mark_forward_parm_decls ();
3562 new_attrs = c_parser_attributes (parser);
3563 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3565 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3567 c_parser_consume_token (parser);
3568 if (bad_parm)
3569 return NULL;
3570 else
3571 return get_parm_info (false, expr);
3573 if (!c_parser_require (parser, CPP_COMMA,
3574 "expected %<;%>, %<,%> or %<)%>"))
3576 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3577 return NULL;
3579 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3581 c_parser_consume_token (parser);
3582 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3584 c_parser_consume_token (parser);
3585 if (bad_parm)
3586 return NULL;
3587 else
3588 return get_parm_info (true, expr);
3590 else
3592 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3593 "expected %<)%>");
3594 return NULL;
3600 /* Parse a parameter declaration. ATTRS are the attributes at the
3601 start of the declaration if it is the first parameter. */
3603 static struct c_parm *
3604 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3606 struct c_declspecs *specs;
3607 struct c_declarator *declarator;
3608 tree prefix_attrs;
3609 tree postfix_attrs = NULL_TREE;
3610 bool dummy = false;
3612 /* Accept #pragmas between parameter declarations. */
3613 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3614 c_parser_pragma (parser, pragma_param);
3616 if (!c_parser_next_token_starts_declspecs (parser))
3618 c_token *token = c_parser_peek_token (parser);
3619 if (parser->error)
3620 return NULL;
3621 c_parser_set_source_position_from_token (token);
3622 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3624 error_at (token->location, "unknown type name %qE", token->value);
3625 parser->error = true;
3627 /* ??? In some Objective-C cases '...' isn't applicable so there
3628 should be a different message. */
3629 else
3630 c_parser_error (parser,
3631 "expected declaration specifiers or %<...%>");
3632 c_parser_skip_to_end_of_parameter (parser);
3633 return NULL;
3635 specs = build_null_declspecs ();
3636 if (attrs)
3638 declspecs_add_attrs (input_location, specs, attrs);
3639 attrs = NULL_TREE;
3641 c_parser_declspecs (parser, specs, true, true, true, true, false,
3642 cla_nonabstract_decl);
3643 finish_declspecs (specs);
3644 pending_xref_error ();
3645 prefix_attrs = specs->attrs;
3646 specs->attrs = NULL_TREE;
3647 declarator = c_parser_declarator (parser,
3648 specs->typespec_kind != ctsk_none,
3649 C_DTR_PARM, &dummy);
3650 if (declarator == NULL)
3652 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3653 return NULL;
3655 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3656 postfix_attrs = c_parser_attributes (parser);
3657 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3658 declarator);
3661 /* Parse a string literal in an asm expression. It should not be
3662 translated, and wide string literals are an error although
3663 permitted by the syntax. This is a GNU extension.
3665 asm-string-literal:
3666 string-literal
3668 ??? At present, following the old parser, the caller needs to have
3669 set lex_untranslated_string to 1. It would be better to follow the
3670 C++ parser rather than using this kludge. */
3672 static tree
3673 c_parser_asm_string_literal (c_parser *parser)
3675 tree str;
3676 int save_flag = warn_overlength_strings;
3677 warn_overlength_strings = 0;
3678 if (c_parser_next_token_is (parser, CPP_STRING))
3680 str = c_parser_peek_token (parser)->value;
3681 c_parser_consume_token (parser);
3683 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3685 error_at (c_parser_peek_token (parser)->location,
3686 "wide string literal in %<asm%>");
3687 str = build_string (1, "");
3688 c_parser_consume_token (parser);
3690 else
3692 c_parser_error (parser, "expected string literal");
3693 str = NULL_TREE;
3695 warn_overlength_strings = save_flag;
3696 return str;
3699 /* Parse a simple asm expression. This is used in restricted
3700 contexts, where a full expression with inputs and outputs does not
3701 make sense. This is a GNU extension.
3703 simple-asm-expr:
3704 asm ( asm-string-literal )
3707 static tree
3708 c_parser_simple_asm_expr (c_parser *parser)
3710 tree str;
3711 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3712 /* ??? Follow the C++ parser rather than using the
3713 lex_untranslated_string kludge. */
3714 parser->lex_untranslated_string = true;
3715 c_parser_consume_token (parser);
3716 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3718 parser->lex_untranslated_string = false;
3719 return NULL_TREE;
3721 str = c_parser_asm_string_literal (parser);
3722 parser->lex_untranslated_string = false;
3723 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3725 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3726 return NULL_TREE;
3728 return str;
3731 static tree
3732 c_parser_attribute_any_word (c_parser *parser)
3734 tree attr_name = NULL_TREE;
3736 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3738 /* ??? See comment above about what keywords are accepted here. */
3739 bool ok;
3740 switch (c_parser_peek_token (parser)->keyword)
3742 case RID_STATIC:
3743 case RID_UNSIGNED:
3744 case RID_LONG:
3745 case RID_INT128:
3746 case RID_CONST:
3747 case RID_EXTERN:
3748 case RID_REGISTER:
3749 case RID_TYPEDEF:
3750 case RID_SHORT:
3751 case RID_INLINE:
3752 case RID_NORETURN:
3753 case RID_VOLATILE:
3754 case RID_SIGNED:
3755 case RID_AUTO:
3756 case RID_RESTRICT:
3757 case RID_COMPLEX:
3758 case RID_THREAD:
3759 case RID_INT:
3760 case RID_CHAR:
3761 case RID_FLOAT:
3762 case RID_DOUBLE:
3763 case RID_VOID:
3764 case RID_DFLOAT32:
3765 case RID_DFLOAT64:
3766 case RID_DFLOAT128:
3767 case RID_BOOL:
3768 case RID_FRACT:
3769 case RID_ACCUM:
3770 case RID_SAT:
3771 case RID_TRANSACTION_ATOMIC:
3772 case RID_TRANSACTION_CANCEL:
3773 case RID_ATOMIC:
3774 case RID_AUTO_TYPE:
3775 ok = true;
3776 break;
3777 default:
3778 ok = false;
3779 break;
3781 if (!ok)
3782 return NULL_TREE;
3784 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3785 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3787 else if (c_parser_next_token_is (parser, CPP_NAME))
3788 attr_name = c_parser_peek_token (parser)->value;
3790 return attr_name;
3793 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3794 "__vector" or "__vector__." */
3796 static inline bool
3797 is_cilkplus_vector_p (tree name)
3799 if (flag_cilkplus && is_attribute_p ("vector", name))
3800 return true;
3801 return false;
3804 #define CILK_SIMD_FN_CLAUSE_MASK \
3805 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3806 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3807 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3808 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3809 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3811 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3812 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3813 pushed into the token list.
3814 Syntax:
3815 vector
3816 vector (<vector attributes>). */
3818 static void
3819 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3821 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3823 int paren_scope = 0;
3824 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3825 /* Consume the "vector" token. */
3826 c_parser_consume_token (parser);
3828 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3830 c_parser_consume_token (parser);
3831 paren_scope++;
3833 while (paren_scope > 0)
3835 c_token *token = c_parser_peek_token (parser);
3836 if (token->type == CPP_OPEN_PAREN)
3837 paren_scope++;
3838 else if (token->type == CPP_CLOSE_PAREN)
3839 paren_scope--;
3840 /* Do not push the last ')' since we are not pushing the '('. */
3841 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3842 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3843 c_parser_consume_token (parser);
3846 /* Since we are converting an attribute to a pragma, we need to end the
3847 attribute with PRAGMA_EOL. */
3848 c_token eol_token;
3849 memset (&eol_token, 0, sizeof (eol_token));
3850 eol_token.type = CPP_PRAGMA_EOL;
3851 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3854 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3856 static void
3857 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3859 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3861 /* c_parser_attributes is called in several places, so if these EOF
3862 tokens are already inserted, then don't do them again. */
3863 if (last_token.type == CPP_EOF)
3864 return;
3866 /* Two CPP_EOF token are added as a safety net since the normal C
3867 front-end has two token look-ahead. */
3868 c_token eof_token;
3869 eof_token.type = CPP_EOF;
3870 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3871 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3874 /* Parse (possibly empty) attributes. This is a GNU extension.
3876 attributes:
3877 empty
3878 attributes attribute
3880 attribute:
3881 __attribute__ ( ( attribute-list ) )
3883 attribute-list:
3884 attrib
3885 attribute_list , attrib
3887 attrib:
3888 empty
3889 any-word
3890 any-word ( identifier )
3891 any-word ( identifier , nonempty-expr-list )
3892 any-word ( expr-list )
3894 where the "identifier" must not be declared as a type, and
3895 "any-word" may be any identifier (including one declared as a
3896 type), a reserved word storage class specifier, type specifier or
3897 type qualifier. ??? This still leaves out most reserved keywords
3898 (following the old parser), shouldn't we include them, and why not
3899 allow identifiers declared as types to start the arguments? */
3901 static tree
3902 c_parser_attributes (c_parser *parser)
3904 tree attrs = NULL_TREE;
3905 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3907 /* ??? Follow the C++ parser rather than using the
3908 lex_untranslated_string kludge. */
3909 parser->lex_untranslated_string = true;
3910 c_parser_consume_token (parser);
3911 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3913 parser->lex_untranslated_string = false;
3914 return attrs;
3916 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3918 parser->lex_untranslated_string = false;
3919 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3920 return attrs;
3922 /* Parse the attribute list. */
3923 while (c_parser_next_token_is (parser, CPP_COMMA)
3924 || c_parser_next_token_is (parser, CPP_NAME)
3925 || c_parser_next_token_is (parser, CPP_KEYWORD))
3927 tree attr, attr_name, attr_args;
3928 vec<tree, va_gc> *expr_list;
3929 if (c_parser_next_token_is (parser, CPP_COMMA))
3931 c_parser_consume_token (parser);
3932 continue;
3935 attr_name = c_parser_attribute_any_word (parser);
3936 if (attr_name == NULL)
3937 break;
3938 if (is_cilkplus_vector_p (attr_name))
3940 c_token *v_token = c_parser_peek_token (parser);
3941 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3942 continue;
3944 c_parser_consume_token (parser);
3945 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3947 attr = build_tree_list (attr_name, NULL_TREE);
3948 attrs = chainon (attrs, attr);
3949 continue;
3951 c_parser_consume_token (parser);
3952 /* Parse the attribute contents. If they start with an
3953 identifier which is followed by a comma or close
3954 parenthesis, then the arguments start with that
3955 identifier; otherwise they are an expression list.
3956 In objective-c the identifier may be a classname. */
3957 if (c_parser_next_token_is (parser, CPP_NAME)
3958 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3959 || (c_dialect_objc ()
3960 && c_parser_peek_token (parser)->id_kind
3961 == C_ID_CLASSNAME))
3962 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3963 || (c_parser_peek_2nd_token (parser)->type
3964 == CPP_CLOSE_PAREN))
3965 && (attribute_takes_identifier_p (attr_name)
3966 || (c_dialect_objc ()
3967 && c_parser_peek_token (parser)->id_kind
3968 == C_ID_CLASSNAME)))
3970 tree arg1 = c_parser_peek_token (parser)->value;
3971 c_parser_consume_token (parser);
3972 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3973 attr_args = build_tree_list (NULL_TREE, arg1);
3974 else
3976 tree tree_list;
3977 c_parser_consume_token (parser);
3978 expr_list = c_parser_expr_list (parser, false, true,
3979 NULL, NULL, NULL, NULL);
3980 tree_list = build_tree_list_vec (expr_list);
3981 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3982 release_tree_vector (expr_list);
3985 else
3987 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3988 attr_args = NULL_TREE;
3989 else
3991 expr_list = c_parser_expr_list (parser, false, true,
3992 NULL, NULL, NULL, NULL);
3993 attr_args = build_tree_list_vec (expr_list);
3994 release_tree_vector (expr_list);
3997 attr = build_tree_list (attr_name, attr_args);
3998 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3999 c_parser_consume_token (parser);
4000 else
4002 parser->lex_untranslated_string = false;
4003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4004 "expected %<)%>");
4005 return attrs;
4007 attrs = chainon (attrs, attr);
4009 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4010 c_parser_consume_token (parser);
4011 else
4013 parser->lex_untranslated_string = false;
4014 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4015 "expected %<)%>");
4016 return attrs;
4018 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4019 c_parser_consume_token (parser);
4020 else
4022 parser->lex_untranslated_string = false;
4023 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4024 "expected %<)%>");
4025 return attrs;
4027 parser->lex_untranslated_string = false;
4030 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4031 c_finish_cilk_simd_fn_tokens (parser);
4032 return attrs;
4035 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4037 type-name:
4038 specifier-qualifier-list abstract-declarator[opt]
4041 static struct c_type_name *
4042 c_parser_type_name (c_parser *parser)
4044 struct c_declspecs *specs = build_null_declspecs ();
4045 struct c_declarator *declarator;
4046 struct c_type_name *ret;
4047 bool dummy = false;
4048 c_parser_declspecs (parser, specs, false, true, true, false, false,
4049 cla_prefer_type);
4050 if (!specs->declspecs_seen_p)
4052 c_parser_error (parser, "expected specifier-qualifier-list");
4053 return NULL;
4055 if (specs->type != error_mark_node)
4057 pending_xref_error ();
4058 finish_declspecs (specs);
4060 declarator = c_parser_declarator (parser,
4061 specs->typespec_kind != ctsk_none,
4062 C_DTR_ABSTRACT, &dummy);
4063 if (declarator == NULL)
4064 return NULL;
4065 ret = XOBNEW (&parser_obstack, struct c_type_name);
4066 ret->specs = specs;
4067 ret->declarator = declarator;
4068 return ret;
4071 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4073 initializer:
4074 assignment-expression
4075 { initializer-list }
4076 { initializer-list , }
4078 initializer-list:
4079 designation[opt] initializer
4080 initializer-list , designation[opt] initializer
4082 designation:
4083 designator-list =
4085 designator-list:
4086 designator
4087 designator-list designator
4089 designator:
4090 array-designator
4091 . identifier
4093 array-designator:
4094 [ constant-expression ]
4096 GNU extensions:
4098 initializer:
4101 designation:
4102 array-designator
4103 identifier :
4105 array-designator:
4106 [ constant-expression ... constant-expression ]
4108 Any expression without commas is accepted in the syntax for the
4109 constant-expressions, with non-constant expressions rejected later.
4111 This function is only used for top-level initializers; for nested
4112 ones, see c_parser_initval. */
4114 static struct c_expr
4115 c_parser_initializer (c_parser *parser)
4117 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4118 return c_parser_braced_init (parser, NULL_TREE, false);
4119 else
4121 struct c_expr ret;
4122 location_t loc = c_parser_peek_token (parser)->location;
4123 ret = c_parser_expr_no_commas (parser, NULL);
4124 if (TREE_CODE (ret.value) != STRING_CST
4125 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4126 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4127 return ret;
4131 /* Parse a braced initializer list. TYPE is the type specified for a
4132 compound literal, and NULL_TREE for other initializers and for
4133 nested braced lists. NESTED_P is true for nested braced lists,
4134 false for the list of a compound literal or the list that is the
4135 top-level initializer in a declaration. */
4137 static struct c_expr
4138 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4140 struct c_expr ret;
4141 struct obstack braced_init_obstack;
4142 location_t brace_loc = c_parser_peek_token (parser)->location;
4143 gcc_obstack_init (&braced_init_obstack);
4144 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4145 c_parser_consume_token (parser);
4146 if (nested_p)
4147 push_init_level (brace_loc, 0, &braced_init_obstack);
4148 else
4149 really_start_incremental_init (type);
4150 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4152 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4154 else
4156 /* Parse a non-empty initializer list, possibly with a trailing
4157 comma. */
4158 while (true)
4160 c_parser_initelt (parser, &braced_init_obstack);
4161 if (parser->error)
4162 break;
4163 if (c_parser_next_token_is (parser, CPP_COMMA))
4164 c_parser_consume_token (parser);
4165 else
4166 break;
4167 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4168 break;
4171 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4173 ret.value = error_mark_node;
4174 ret.original_code = ERROR_MARK;
4175 ret.original_type = NULL;
4176 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4177 pop_init_level (brace_loc, 0, &braced_init_obstack);
4178 obstack_free (&braced_init_obstack, NULL);
4179 return ret;
4181 c_parser_consume_token (parser);
4182 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4183 obstack_free (&braced_init_obstack, NULL);
4184 return ret;
4187 /* Parse a nested initializer, including designators. */
4189 static void
4190 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4192 /* Parse any designator or designator list. A single array
4193 designator may have the subsequent "=" omitted in GNU C, but a
4194 longer list or a structure member designator may not. */
4195 if (c_parser_next_token_is (parser, CPP_NAME)
4196 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4198 /* Old-style structure member designator. */
4199 set_init_label (c_parser_peek_token (parser)->location,
4200 c_parser_peek_token (parser)->value,
4201 braced_init_obstack);
4202 /* Use the colon as the error location. */
4203 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4204 "obsolete use of designated initializer with %<:%>");
4205 c_parser_consume_token (parser);
4206 c_parser_consume_token (parser);
4208 else
4210 /* des_seen is 0 if there have been no designators, 1 if there
4211 has been a single array designator and 2 otherwise. */
4212 int des_seen = 0;
4213 /* Location of a designator. */
4214 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4215 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4216 || c_parser_next_token_is (parser, CPP_DOT))
4218 int des_prev = des_seen;
4219 if (!des_seen)
4220 des_loc = c_parser_peek_token (parser)->location;
4221 if (des_seen < 2)
4222 des_seen++;
4223 if (c_parser_next_token_is (parser, CPP_DOT))
4225 des_seen = 2;
4226 c_parser_consume_token (parser);
4227 if (c_parser_next_token_is (parser, CPP_NAME))
4229 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4230 braced_init_obstack);
4231 c_parser_consume_token (parser);
4233 else
4235 struct c_expr init;
4236 init.value = error_mark_node;
4237 init.original_code = ERROR_MARK;
4238 init.original_type = NULL;
4239 c_parser_error (parser, "expected identifier");
4240 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4241 process_init_element (input_location, init, false,
4242 braced_init_obstack);
4243 return;
4246 else
4248 tree first, second;
4249 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4250 location_t array_index_loc = UNKNOWN_LOCATION;
4251 /* ??? Following the old parser, [ objc-receiver
4252 objc-message-args ] is accepted as an initializer,
4253 being distinguished from a designator by what follows
4254 the first assignment expression inside the square
4255 brackets, but after a first array designator a
4256 subsequent square bracket is for Objective-C taken to
4257 start an expression, using the obsolete form of
4258 designated initializer without '=', rather than
4259 possibly being a second level of designation: in LALR
4260 terms, the '[' is shifted rather than reducing
4261 designator to designator-list. */
4262 if (des_prev == 1 && c_dialect_objc ())
4264 des_seen = des_prev;
4265 break;
4267 if (des_prev == 0 && c_dialect_objc ())
4269 /* This might be an array designator or an
4270 Objective-C message expression. If the former,
4271 continue parsing here; if the latter, parse the
4272 remainder of the initializer given the starting
4273 primary-expression. ??? It might make sense to
4274 distinguish when des_prev == 1 as well; see
4275 previous comment. */
4276 tree rec, args;
4277 struct c_expr mexpr;
4278 c_parser_consume_token (parser);
4279 if (c_parser_peek_token (parser)->type == CPP_NAME
4280 && ((c_parser_peek_token (parser)->id_kind
4281 == C_ID_TYPENAME)
4282 || (c_parser_peek_token (parser)->id_kind
4283 == C_ID_CLASSNAME)))
4285 /* Type name receiver. */
4286 tree id = c_parser_peek_token (parser)->value;
4287 c_parser_consume_token (parser);
4288 rec = objc_get_class_reference (id);
4289 goto parse_message_args;
4291 first = c_parser_expr_no_commas (parser, NULL).value;
4292 mark_exp_read (first);
4293 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4294 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4295 goto array_desig_after_first;
4296 /* Expression receiver. So far only one part
4297 without commas has been parsed; there might be
4298 more of the expression. */
4299 rec = first;
4300 while (c_parser_next_token_is (parser, CPP_COMMA))
4302 struct c_expr next;
4303 location_t comma_loc, exp_loc;
4304 comma_loc = c_parser_peek_token (parser)->location;
4305 c_parser_consume_token (parser);
4306 exp_loc = c_parser_peek_token (parser)->location;
4307 next = c_parser_expr_no_commas (parser, NULL);
4308 next = convert_lvalue_to_rvalue (exp_loc, next,
4309 true, true);
4310 rec = build_compound_expr (comma_loc, rec, next.value);
4312 parse_message_args:
4313 /* Now parse the objc-message-args. */
4314 args = c_parser_objc_message_args (parser);
4315 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4316 "expected %<]%>");
4317 mexpr.value
4318 = objc_build_message_expr (rec, args);
4319 mexpr.original_code = ERROR_MARK;
4320 mexpr.original_type = NULL;
4321 /* Now parse and process the remainder of the
4322 initializer, starting with this message
4323 expression as a primary-expression. */
4324 c_parser_initval (parser, &mexpr, braced_init_obstack);
4325 return;
4327 c_parser_consume_token (parser);
4328 array_index_loc = c_parser_peek_token (parser)->location;
4329 first = c_parser_expr_no_commas (parser, NULL).value;
4330 mark_exp_read (first);
4331 array_desig_after_first:
4332 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4334 ellipsis_loc = c_parser_peek_token (parser)->location;
4335 c_parser_consume_token (parser);
4336 second = c_parser_expr_no_commas (parser, NULL).value;
4337 mark_exp_read (second);
4339 else
4340 second = NULL_TREE;
4341 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4343 c_parser_consume_token (parser);
4344 set_init_index (array_index_loc, first, second,
4345 braced_init_obstack);
4346 if (second)
4347 pedwarn (ellipsis_loc, OPT_Wpedantic,
4348 "ISO C forbids specifying range of elements to initialize");
4350 else
4351 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4352 "expected %<]%>");
4355 if (des_seen >= 1)
4357 if (c_parser_next_token_is (parser, CPP_EQ))
4359 pedwarn_c90 (des_loc, OPT_Wpedantic,
4360 "ISO C90 forbids specifying subobject "
4361 "to initialize");
4362 c_parser_consume_token (parser);
4364 else
4366 if (des_seen == 1)
4367 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4368 "obsolete use of designated initializer without %<=%>");
4369 else
4371 struct c_expr init;
4372 init.value = error_mark_node;
4373 init.original_code = ERROR_MARK;
4374 init.original_type = NULL;
4375 c_parser_error (parser, "expected %<=%>");
4376 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4377 process_init_element (input_location, init, false,
4378 braced_init_obstack);
4379 return;
4384 c_parser_initval (parser, NULL, braced_init_obstack);
4387 /* Parse a nested initializer; as c_parser_initializer but parses
4388 initializers within braced lists, after any designators have been
4389 applied. If AFTER is not NULL then it is an Objective-C message
4390 expression which is the primary-expression starting the
4391 initializer. */
4393 static void
4394 c_parser_initval (c_parser *parser, struct c_expr *after,
4395 struct obstack * braced_init_obstack)
4397 struct c_expr init;
4398 gcc_assert (!after || c_dialect_objc ());
4399 location_t loc = c_parser_peek_token (parser)->location;
4401 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4402 init = c_parser_braced_init (parser, NULL_TREE, true);
4403 else
4405 init = c_parser_expr_no_commas (parser, after);
4406 if (init.value != NULL_TREE
4407 && TREE_CODE (init.value) != STRING_CST
4408 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4409 init = convert_lvalue_to_rvalue (loc, init, true, true);
4411 process_init_element (loc, init, false, braced_init_obstack);
4414 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4415 C99 6.8.2).
4417 compound-statement:
4418 { block-item-list[opt] }
4419 { label-declarations block-item-list }
4421 block-item-list:
4422 block-item
4423 block-item-list block-item
4425 block-item:
4426 nested-declaration
4427 statement
4429 nested-declaration:
4430 declaration
4432 GNU extensions:
4434 compound-statement:
4435 { label-declarations block-item-list }
4437 nested-declaration:
4438 __extension__ nested-declaration
4439 nested-function-definition
4441 label-declarations:
4442 label-declaration
4443 label-declarations label-declaration
4445 label-declaration:
4446 __label__ identifier-list ;
4448 Allowing the mixing of declarations and code is new in C99. The
4449 GNU syntax also permits (not shown above) labels at the end of
4450 compound statements, which yield an error. We don't allow labels
4451 on declarations; this might seem like a natural extension, but
4452 there would be a conflict between attributes on the label and
4453 prefix attributes on the declaration. ??? The syntax follows the
4454 old parser in requiring something after label declarations.
4455 Although they are erroneous if the labels declared aren't defined,
4456 is it useful for the syntax to be this way?
4458 OpenACC:
4460 block-item:
4461 openacc-directive
4463 openacc-directive:
4464 update-directive
4466 OpenMP:
4468 block-item:
4469 openmp-directive
4471 openmp-directive:
4472 barrier-directive
4473 flush-directive
4474 taskwait-directive
4475 taskyield-directive
4476 cancel-directive
4477 cancellation-point-directive */
4479 static tree
4480 c_parser_compound_statement (c_parser *parser)
4482 tree stmt;
4483 location_t brace_loc;
4484 brace_loc = c_parser_peek_token (parser)->location;
4485 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4487 /* Ensure a scope is entered and left anyway to avoid confusion
4488 if we have just prepared to enter a function body. */
4489 stmt = c_begin_compound_stmt (true);
4490 c_end_compound_stmt (brace_loc, stmt, true);
4491 return error_mark_node;
4493 stmt = c_begin_compound_stmt (true);
4494 c_parser_compound_statement_nostart (parser);
4496 /* If the compound stmt contains array notations, then we expand them. */
4497 if (flag_cilkplus && contains_array_notation_expr (stmt))
4498 stmt = expand_array_notation_exprs (stmt);
4499 return c_end_compound_stmt (brace_loc, stmt, true);
4502 /* Parse a compound statement except for the opening brace. This is
4503 used for parsing both compound statements and statement expressions
4504 (which follow different paths to handling the opening). */
4506 static void
4507 c_parser_compound_statement_nostart (c_parser *parser)
4509 bool last_stmt = false;
4510 bool last_label = false;
4511 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4512 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4513 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4515 c_parser_consume_token (parser);
4516 return;
4518 mark_valid_location_for_stdc_pragma (true);
4519 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4521 /* Read zero or more forward-declarations for labels that nested
4522 functions can jump to. */
4523 mark_valid_location_for_stdc_pragma (false);
4524 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4526 label_loc = c_parser_peek_token (parser)->location;
4527 c_parser_consume_token (parser);
4528 /* Any identifiers, including those declared as type names,
4529 are OK here. */
4530 while (true)
4532 tree label;
4533 if (c_parser_next_token_is_not (parser, CPP_NAME))
4535 c_parser_error (parser, "expected identifier");
4536 break;
4538 label
4539 = declare_label (c_parser_peek_token (parser)->value);
4540 C_DECLARED_LABEL_FLAG (label) = 1;
4541 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4542 c_parser_consume_token (parser);
4543 if (c_parser_next_token_is (parser, CPP_COMMA))
4544 c_parser_consume_token (parser);
4545 else
4546 break;
4548 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4550 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4552 /* We must now have at least one statement, label or declaration. */
4553 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4555 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4556 c_parser_error (parser, "expected declaration or statement");
4557 c_parser_consume_token (parser);
4558 return;
4560 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4562 location_t loc = c_parser_peek_token (parser)->location;
4563 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4564 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4565 || (c_parser_next_token_is (parser, CPP_NAME)
4566 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4568 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4569 label_loc = c_parser_peek_2nd_token (parser)->location;
4570 else
4571 label_loc = c_parser_peek_token (parser)->location;
4572 last_label = true;
4573 last_stmt = false;
4574 mark_valid_location_for_stdc_pragma (false);
4575 c_parser_label (parser);
4577 else if (!last_label
4578 && c_parser_next_tokens_start_declaration (parser))
4580 last_label = false;
4581 mark_valid_location_for_stdc_pragma (false);
4582 c_parser_declaration_or_fndef (parser, true, true, true, true,
4583 true, NULL, vNULL);
4584 if (last_stmt)
4585 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4586 "ISO C90 forbids mixed declarations and code");
4587 last_stmt = false;
4589 else if (!last_label
4590 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4592 /* __extension__ can start a declaration, but is also an
4593 unary operator that can start an expression. Consume all
4594 but the last of a possible series of __extension__ to
4595 determine which. */
4596 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4597 && (c_parser_peek_2nd_token (parser)->keyword
4598 == RID_EXTENSION))
4599 c_parser_consume_token (parser);
4600 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4602 int ext;
4603 ext = disable_extension_diagnostics ();
4604 c_parser_consume_token (parser);
4605 last_label = false;
4606 mark_valid_location_for_stdc_pragma (false);
4607 c_parser_declaration_or_fndef (parser, true, true, true, true,
4608 true, NULL, vNULL);
4609 /* Following the old parser, __extension__ does not
4610 disable this diagnostic. */
4611 restore_extension_diagnostics (ext);
4612 if (last_stmt)
4613 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4614 "ISO C90 forbids mixed declarations and code");
4615 last_stmt = false;
4617 else
4618 goto statement;
4620 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4622 /* External pragmas, and some omp pragmas, are not associated
4623 with regular c code, and so are not to be considered statements
4624 syntactically. This ensures that the user doesn't put them
4625 places that would turn into syntax errors if the directive
4626 were ignored. */
4627 if (c_parser_pragma (parser, pragma_compound))
4628 last_label = false, last_stmt = true;
4630 else if (c_parser_next_token_is (parser, CPP_EOF))
4632 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4633 c_parser_error (parser, "expected declaration or statement");
4634 return;
4636 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4638 if (parser->in_if_block)
4640 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4641 error_at (loc, """expected %<}%> before %<else%>");
4642 return;
4644 else
4646 error_at (loc, "%<else%> without a previous %<if%>");
4647 c_parser_consume_token (parser);
4648 continue;
4651 else
4653 statement:
4654 last_label = false;
4655 last_stmt = true;
4656 mark_valid_location_for_stdc_pragma (false);
4657 c_parser_statement_after_labels (parser);
4660 parser->error = false;
4662 if (last_label)
4663 error_at (label_loc, "label at end of compound statement");
4664 c_parser_consume_token (parser);
4665 /* Restore the value we started with. */
4666 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4669 /* Parse a label (C90 6.6.1, C99 6.8.1).
4671 label:
4672 identifier : attributes[opt]
4673 case constant-expression :
4674 default :
4676 GNU extensions:
4678 label:
4679 case constant-expression ... constant-expression :
4681 The use of attributes on labels is a GNU extension. The syntax in
4682 GNU C accepts any expressions without commas, non-constant
4683 expressions being rejected later. */
4685 static void
4686 c_parser_label (c_parser *parser)
4688 location_t loc1 = c_parser_peek_token (parser)->location;
4689 tree label = NULL_TREE;
4690 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4692 tree exp1, exp2;
4693 c_parser_consume_token (parser);
4694 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4695 if (c_parser_next_token_is (parser, CPP_COLON))
4697 c_parser_consume_token (parser);
4698 label = do_case (loc1, exp1, NULL_TREE);
4700 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4702 c_parser_consume_token (parser);
4703 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4704 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4705 label = do_case (loc1, exp1, exp2);
4707 else
4708 c_parser_error (parser, "expected %<:%> or %<...%>");
4710 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4712 c_parser_consume_token (parser);
4713 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4714 label = do_case (loc1, NULL_TREE, NULL_TREE);
4716 else
4718 tree name = c_parser_peek_token (parser)->value;
4719 tree tlab;
4720 tree attrs;
4721 location_t loc2 = c_parser_peek_token (parser)->location;
4722 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4723 c_parser_consume_token (parser);
4724 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4725 c_parser_consume_token (parser);
4726 attrs = c_parser_attributes (parser);
4727 tlab = define_label (loc2, name);
4728 if (tlab)
4730 decl_attributes (&tlab, attrs, 0);
4731 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4734 if (label)
4736 if (c_parser_next_tokens_start_declaration (parser))
4738 error_at (c_parser_peek_token (parser)->location,
4739 "a label can only be part of a statement and "
4740 "a declaration is not a statement");
4741 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4742 /*static_assert_ok*/ true,
4743 /*empty_ok*/ true, /*nested*/ true,
4744 /*start_attr_ok*/ true, NULL,
4745 vNULL);
4750 /* Parse a statement (C90 6.6, C99 6.8).
4752 statement:
4753 labeled-statement
4754 compound-statement
4755 expression-statement
4756 selection-statement
4757 iteration-statement
4758 jump-statement
4760 labeled-statement:
4761 label statement
4763 expression-statement:
4764 expression[opt] ;
4766 selection-statement:
4767 if-statement
4768 switch-statement
4770 iteration-statement:
4771 while-statement
4772 do-statement
4773 for-statement
4775 jump-statement:
4776 goto identifier ;
4777 continue ;
4778 break ;
4779 return expression[opt] ;
4781 GNU extensions:
4783 statement:
4784 asm-statement
4786 jump-statement:
4787 goto * expression ;
4789 Objective-C:
4791 statement:
4792 objc-throw-statement
4793 objc-try-catch-statement
4794 objc-synchronized-statement
4796 objc-throw-statement:
4797 @throw expression ;
4798 @throw ;
4800 OpenACC:
4802 statement:
4803 openacc-construct
4805 openacc-construct:
4806 parallel-construct
4807 kernels-construct
4808 data-construct
4809 loop-construct
4811 parallel-construct:
4812 parallel-directive structured-block
4814 kernels-construct:
4815 kernels-directive structured-block
4817 data-construct:
4818 data-directive structured-block
4820 loop-construct:
4821 loop-directive structured-block
4823 OpenMP:
4825 statement:
4826 openmp-construct
4828 openmp-construct:
4829 parallel-construct
4830 for-construct
4831 simd-construct
4832 for-simd-construct
4833 sections-construct
4834 single-construct
4835 parallel-for-construct
4836 parallel-for-simd-construct
4837 parallel-sections-construct
4838 master-construct
4839 critical-construct
4840 atomic-construct
4841 ordered-construct
4843 parallel-construct:
4844 parallel-directive structured-block
4846 for-construct:
4847 for-directive iteration-statement
4849 simd-construct:
4850 simd-directive iteration-statements
4852 for-simd-construct:
4853 for-simd-directive iteration-statements
4855 sections-construct:
4856 sections-directive section-scope
4858 single-construct:
4859 single-directive structured-block
4861 parallel-for-construct:
4862 parallel-for-directive iteration-statement
4864 parallel-for-simd-construct:
4865 parallel-for-simd-directive iteration-statement
4867 parallel-sections-construct:
4868 parallel-sections-directive section-scope
4870 master-construct:
4871 master-directive structured-block
4873 critical-construct:
4874 critical-directive structured-block
4876 atomic-construct:
4877 atomic-directive expression-statement
4879 ordered-construct:
4880 ordered-directive structured-block
4882 Transactional Memory:
4884 statement:
4885 transaction-statement
4886 transaction-cancel-statement
4889 static void
4890 c_parser_statement (c_parser *parser)
4892 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4893 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4894 || (c_parser_next_token_is (parser, CPP_NAME)
4895 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4896 c_parser_label (parser);
4897 c_parser_statement_after_labels (parser);
4900 /* Parse a statement, other than a labeled statement. */
4902 static void
4903 c_parser_statement_after_labels (c_parser *parser)
4905 location_t loc = c_parser_peek_token (parser)->location;
4906 tree stmt = NULL_TREE;
4907 bool in_if_block = parser->in_if_block;
4908 parser->in_if_block = false;
4909 switch (c_parser_peek_token (parser)->type)
4911 case CPP_OPEN_BRACE:
4912 add_stmt (c_parser_compound_statement (parser));
4913 break;
4914 case CPP_KEYWORD:
4915 switch (c_parser_peek_token (parser)->keyword)
4917 case RID_IF:
4918 c_parser_if_statement (parser);
4919 break;
4920 case RID_SWITCH:
4921 c_parser_switch_statement (parser);
4922 break;
4923 case RID_WHILE:
4924 c_parser_while_statement (parser, false);
4925 break;
4926 case RID_DO:
4927 c_parser_do_statement (parser, false);
4928 break;
4929 case RID_FOR:
4930 c_parser_for_statement (parser, false);
4931 break;
4932 case RID_CILK_FOR:
4933 if (!flag_cilkplus)
4935 error_at (c_parser_peek_token (parser)->location,
4936 "-fcilkplus must be enabled to use %<_Cilk_for%>");
4937 c_parser_skip_to_end_of_block_or_statement (parser);
4939 else
4940 c_parser_cilk_for (parser, integer_zero_node);
4941 break;
4942 case RID_CILK_SYNC:
4943 c_parser_consume_token (parser);
4944 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4945 if (!flag_cilkplus)
4946 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4947 else
4948 add_stmt (build_cilk_sync ());
4949 break;
4950 case RID_GOTO:
4951 c_parser_consume_token (parser);
4952 if (c_parser_next_token_is (parser, CPP_NAME))
4954 stmt = c_finish_goto_label (loc,
4955 c_parser_peek_token (parser)->value);
4956 c_parser_consume_token (parser);
4958 else if (c_parser_next_token_is (parser, CPP_MULT))
4960 struct c_expr val;
4962 c_parser_consume_token (parser);
4963 val = c_parser_expression (parser);
4964 val = convert_lvalue_to_rvalue (loc, val, false, true);
4965 stmt = c_finish_goto_ptr (loc, val.value);
4967 else
4968 c_parser_error (parser, "expected identifier or %<*%>");
4969 goto expect_semicolon;
4970 case RID_CONTINUE:
4971 c_parser_consume_token (parser);
4972 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4973 goto expect_semicolon;
4974 case RID_BREAK:
4975 c_parser_consume_token (parser);
4976 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4977 goto expect_semicolon;
4978 case RID_RETURN:
4979 c_parser_consume_token (parser);
4980 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4982 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4983 c_parser_consume_token (parser);
4985 else
4987 location_t xloc = c_parser_peek_token (parser)->location;
4988 struct c_expr expr = c_parser_expression_conv (parser);
4989 mark_exp_read (expr.value);
4990 stmt = c_finish_return (xloc, expr.value, expr.original_type);
4991 goto expect_semicolon;
4993 break;
4994 case RID_ASM:
4995 stmt = c_parser_asm_statement (parser);
4996 break;
4997 case RID_TRANSACTION_ATOMIC:
4998 case RID_TRANSACTION_RELAXED:
4999 stmt = c_parser_transaction (parser,
5000 c_parser_peek_token (parser)->keyword);
5001 break;
5002 case RID_TRANSACTION_CANCEL:
5003 stmt = c_parser_transaction_cancel (parser);
5004 goto expect_semicolon;
5005 case RID_AT_THROW:
5006 gcc_assert (c_dialect_objc ());
5007 c_parser_consume_token (parser);
5008 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5010 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5011 c_parser_consume_token (parser);
5013 else
5015 struct c_expr expr = c_parser_expression (parser);
5016 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5017 expr.value = c_fully_fold (expr.value, false, NULL);
5018 stmt = objc_build_throw_stmt (loc, expr.value);
5019 goto expect_semicolon;
5021 break;
5022 case RID_AT_TRY:
5023 gcc_assert (c_dialect_objc ());
5024 c_parser_objc_try_catch_finally_statement (parser);
5025 break;
5026 case RID_AT_SYNCHRONIZED:
5027 gcc_assert (c_dialect_objc ());
5028 c_parser_objc_synchronized_statement (parser);
5029 break;
5030 default:
5031 goto expr_stmt;
5033 break;
5034 case CPP_SEMICOLON:
5035 c_parser_consume_token (parser);
5036 break;
5037 case CPP_CLOSE_PAREN:
5038 case CPP_CLOSE_SQUARE:
5039 /* Avoid infinite loop in error recovery:
5040 c_parser_skip_until_found stops at a closing nesting
5041 delimiter without consuming it, but here we need to consume
5042 it to proceed further. */
5043 c_parser_error (parser, "expected statement");
5044 c_parser_consume_token (parser);
5045 break;
5046 case CPP_PRAGMA:
5047 c_parser_pragma (parser, pragma_stmt);
5048 break;
5049 default:
5050 expr_stmt:
5051 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5052 expect_semicolon:
5053 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5054 break;
5056 /* Two cases cannot and do not have line numbers associated: If stmt
5057 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5058 cannot hold line numbers. But that's OK because the statement
5059 will either be changed to a MODIFY_EXPR during gimplification of
5060 the statement expr, or discarded. If stmt was compound, but
5061 without new variables, we will have skipped the creation of a
5062 BIND and will have a bare STATEMENT_LIST. But that's OK because
5063 (recursively) all of the component statements should already have
5064 line numbers assigned. ??? Can we discard no-op statements
5065 earlier? */
5066 if (CAN_HAVE_LOCATION_P (stmt)
5067 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5068 SET_EXPR_LOCATION (stmt, loc);
5070 parser->in_if_block = in_if_block;
5073 /* Parse the condition from an if, do, while or for statements. */
5075 static tree
5076 c_parser_condition (c_parser *parser)
5078 location_t loc = c_parser_peek_token (parser)->location;
5079 tree cond;
5080 cond = c_parser_expression_conv (parser).value;
5081 cond = c_objc_common_truthvalue_conversion (loc, cond);
5082 cond = c_fully_fold (cond, false, NULL);
5083 if (warn_sequence_point)
5084 verify_sequence_points (cond);
5085 return cond;
5088 /* Parse a parenthesized condition from an if, do or while statement.
5090 condition:
5091 ( expression )
5093 static tree
5094 c_parser_paren_condition (c_parser *parser)
5096 tree cond;
5097 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5098 return error_mark_node;
5099 cond = c_parser_condition (parser);
5100 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5101 return cond;
5104 /* Parse a statement which is a block in C99. */
5106 static tree
5107 c_parser_c99_block_statement (c_parser *parser)
5109 tree block = c_begin_compound_stmt (flag_isoc99);
5110 location_t loc = c_parser_peek_token (parser)->location;
5111 c_parser_statement (parser);
5112 return c_end_compound_stmt (loc, block, flag_isoc99);
5115 /* Parse the body of an if statement. This is just parsing a
5116 statement but (a) it is a block in C99, (b) we track whether the
5117 body is an if statement for the sake of -Wparentheses warnings, (c)
5118 we handle an empty body specially for the sake of -Wempty-body
5119 warnings, and (d) we call parser_compound_statement directly
5120 because c_parser_statement_after_labels resets
5121 parser->in_if_block. */
5123 static tree
5124 c_parser_if_body (c_parser *parser, bool *if_p)
5126 tree block = c_begin_compound_stmt (flag_isoc99);
5127 location_t body_loc = c_parser_peek_token (parser)->location;
5128 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5129 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5130 || (c_parser_next_token_is (parser, CPP_NAME)
5131 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5132 c_parser_label (parser);
5133 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5134 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5136 location_t loc = c_parser_peek_token (parser)->location;
5137 add_stmt (build_empty_stmt (loc));
5138 c_parser_consume_token (parser);
5139 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5140 warning_at (loc, OPT_Wempty_body,
5141 "suggest braces around empty body in an %<if%> statement");
5143 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5144 add_stmt (c_parser_compound_statement (parser));
5145 else
5146 c_parser_statement_after_labels (parser);
5147 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5150 /* Parse the else body of an if statement. This is just parsing a
5151 statement but (a) it is a block in C99, (b) we handle an empty body
5152 specially for the sake of -Wempty-body warnings. */
5154 static tree
5155 c_parser_else_body (c_parser *parser)
5157 location_t else_loc = c_parser_peek_token (parser)->location;
5158 tree block = c_begin_compound_stmt (flag_isoc99);
5159 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5160 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5161 || (c_parser_next_token_is (parser, CPP_NAME)
5162 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5163 c_parser_label (parser);
5164 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5166 location_t loc = c_parser_peek_token (parser)->location;
5167 warning_at (loc,
5168 OPT_Wempty_body,
5169 "suggest braces around empty body in an %<else%> statement");
5170 add_stmt (build_empty_stmt (loc));
5171 c_parser_consume_token (parser);
5173 else
5174 c_parser_statement_after_labels (parser);
5175 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5178 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5180 if-statement:
5181 if ( expression ) statement
5182 if ( expression ) statement else statement
5185 static void
5186 c_parser_if_statement (c_parser *parser)
5188 tree block;
5189 location_t loc;
5190 tree cond;
5191 bool first_if = false;
5192 tree first_body, second_body;
5193 bool in_if_block;
5194 tree if_stmt;
5196 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5197 c_parser_consume_token (parser);
5198 block = c_begin_compound_stmt (flag_isoc99);
5199 loc = c_parser_peek_token (parser)->location;
5200 cond = c_parser_paren_condition (parser);
5201 in_if_block = parser->in_if_block;
5202 parser->in_if_block = true;
5203 first_body = c_parser_if_body (parser, &first_if);
5204 parser->in_if_block = in_if_block;
5205 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5207 c_parser_consume_token (parser);
5208 second_body = c_parser_else_body (parser);
5210 else
5211 second_body = NULL_TREE;
5212 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5213 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5215 /* If the if statement contains array notations, then we expand them. */
5216 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5217 if_stmt = fix_conditional_array_notations (if_stmt);
5218 add_stmt (if_stmt);
5221 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5223 switch-statement:
5224 switch (expression) statement
5227 static void
5228 c_parser_switch_statement (c_parser *parser)
5230 struct c_expr ce;
5231 tree block, expr, body, save_break;
5232 location_t switch_loc = c_parser_peek_token (parser)->location;
5233 location_t switch_cond_loc;
5234 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5235 c_parser_consume_token (parser);
5236 block = c_begin_compound_stmt (flag_isoc99);
5237 bool explicit_cast_p = false;
5238 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5240 switch_cond_loc = c_parser_peek_token (parser)->location;
5241 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5242 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5243 explicit_cast_p = true;
5244 ce = c_parser_expression (parser);
5245 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5246 expr = ce.value;
5247 if (flag_cilkplus && contains_array_notation_expr (expr))
5249 error_at (switch_cond_loc,
5250 "array notations cannot be used as a condition for switch "
5251 "statement");
5252 expr = error_mark_node;
5254 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5256 else
5258 switch_cond_loc = UNKNOWN_LOCATION;
5259 expr = error_mark_node;
5261 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5262 save_break = c_break_label;
5263 c_break_label = NULL_TREE;
5264 body = c_parser_c99_block_statement (parser);
5265 c_finish_case (body, ce.original_type);
5266 if (c_break_label)
5268 location_t here = c_parser_peek_token (parser)->location;
5269 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5270 SET_EXPR_LOCATION (t, here);
5271 add_stmt (t);
5273 c_break_label = save_break;
5274 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5277 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5279 while-statement:
5280 while (expression) statement
5283 static void
5284 c_parser_while_statement (c_parser *parser, bool ivdep)
5286 tree block, cond, body, save_break, save_cont;
5287 location_t loc;
5288 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5289 c_parser_consume_token (parser);
5290 block = c_begin_compound_stmt (flag_isoc99);
5291 loc = c_parser_peek_token (parser)->location;
5292 cond = c_parser_paren_condition (parser);
5293 if (flag_cilkplus && contains_array_notation_expr (cond))
5295 error_at (loc, "array notations cannot be used as a condition for while "
5296 "statement");
5297 cond = error_mark_node;
5300 if (ivdep && cond != error_mark_node)
5301 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5302 build_int_cst (integer_type_node,
5303 annot_expr_ivdep_kind));
5304 save_break = c_break_label;
5305 c_break_label = NULL_TREE;
5306 save_cont = c_cont_label;
5307 c_cont_label = NULL_TREE;
5308 body = c_parser_c99_block_statement (parser);
5309 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5310 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5311 c_break_label = save_break;
5312 c_cont_label = save_cont;
5315 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5317 do-statement:
5318 do statement while ( expression ) ;
5321 static void
5322 c_parser_do_statement (c_parser *parser, bool ivdep)
5324 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5325 location_t loc;
5326 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5327 c_parser_consume_token (parser);
5328 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5329 warning_at (c_parser_peek_token (parser)->location,
5330 OPT_Wempty_body,
5331 "suggest braces around empty body in %<do%> statement");
5332 block = c_begin_compound_stmt (flag_isoc99);
5333 loc = c_parser_peek_token (parser)->location;
5334 save_break = c_break_label;
5335 c_break_label = NULL_TREE;
5336 save_cont = c_cont_label;
5337 c_cont_label = NULL_TREE;
5338 body = c_parser_c99_block_statement (parser);
5339 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5340 new_break = c_break_label;
5341 c_break_label = save_break;
5342 new_cont = c_cont_label;
5343 c_cont_label = save_cont;
5344 cond = c_parser_paren_condition (parser);
5345 if (flag_cilkplus && contains_array_notation_expr (cond))
5347 error_at (loc, "array notations cannot be used as a condition for a "
5348 "do-while statement");
5349 cond = error_mark_node;
5351 if (ivdep && cond != error_mark_node)
5352 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5353 build_int_cst (integer_type_node,
5354 annot_expr_ivdep_kind));
5355 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5356 c_parser_skip_to_end_of_block_or_statement (parser);
5357 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5358 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5361 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5363 for-statement:
5364 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5365 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5367 The form with a declaration is new in C99.
5369 ??? In accordance with the old parser, the declaration may be a
5370 nested function, which is then rejected in check_for_loop_decls,
5371 but does it make any sense for this to be included in the grammar?
5372 Note in particular that the nested function does not include a
5373 trailing ';', whereas the "declaration" production includes one.
5374 Also, can we reject bad declarations earlier and cheaper than
5375 check_for_loop_decls?
5377 In Objective-C, there are two additional variants:
5379 foreach-statement:
5380 for ( expression in expresssion ) statement
5381 for ( declaration in expression ) statement
5383 This is inconsistent with C, because the second variant is allowed
5384 even if c99 is not enabled.
5386 The rest of the comment documents these Objective-C foreach-statement.
5388 Here is the canonical example of the first variant:
5389 for (object in array) { do something with object }
5390 we call the first expression ("object") the "object_expression" and
5391 the second expression ("array") the "collection_expression".
5392 object_expression must be an lvalue of type "id" (a generic Objective-C
5393 object) because the loop works by assigning to object_expression the
5394 various objects from the collection_expression. collection_expression
5395 must evaluate to something of type "id" which responds to the method
5396 countByEnumeratingWithState:objects:count:.
5398 The canonical example of the second variant is:
5399 for (id object in array) { do something with object }
5400 which is completely equivalent to
5402 id object;
5403 for (object in array) { do something with object }
5405 Note that initizializing 'object' in some way (eg, "for ((object =
5406 xxx) in array) { do something with object }") is possibly
5407 technically valid, but completely pointless as 'object' will be
5408 assigned to something else as soon as the loop starts. We should
5409 most likely reject it (TODO).
5411 The beginning of the Objective-C foreach-statement looks exactly
5412 like the beginning of the for-statement, and we can tell it is a
5413 foreach-statement only because the initial declaration or
5414 expression is terminated by 'in' instead of ';'.
5417 static void
5418 c_parser_for_statement (c_parser *parser, bool ivdep)
5420 tree block, cond, incr, save_break, save_cont, body;
5421 /* The following are only used when parsing an ObjC foreach statement. */
5422 tree object_expression;
5423 /* Silence the bogus uninitialized warning. */
5424 tree collection_expression = NULL;
5425 location_t loc = c_parser_peek_token (parser)->location;
5426 location_t for_loc = c_parser_peek_token (parser)->location;
5427 bool is_foreach_statement = false;
5428 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5429 c_parser_consume_token (parser);
5430 /* Open a compound statement in Objective-C as well, just in case this is
5431 as foreach expression. */
5432 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5433 cond = error_mark_node;
5434 incr = error_mark_node;
5435 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5437 /* Parse the initialization declaration or expression. */
5438 object_expression = error_mark_node;
5439 parser->objc_could_be_foreach_context = c_dialect_objc ();
5440 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5442 parser->objc_could_be_foreach_context = false;
5443 c_parser_consume_token (parser);
5444 c_finish_expr_stmt (loc, NULL_TREE);
5446 else if (c_parser_next_tokens_start_declaration (parser))
5448 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5449 &object_expression, vNULL);
5450 parser->objc_could_be_foreach_context = false;
5452 if (c_parser_next_token_is_keyword (parser, RID_IN))
5454 c_parser_consume_token (parser);
5455 is_foreach_statement = true;
5456 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5457 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5459 else
5460 check_for_loop_decls (for_loc, flag_isoc99);
5462 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5464 /* __extension__ can start a declaration, but is also an
5465 unary operator that can start an expression. Consume all
5466 but the last of a possible series of __extension__ to
5467 determine which. */
5468 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5469 && (c_parser_peek_2nd_token (parser)->keyword
5470 == RID_EXTENSION))
5471 c_parser_consume_token (parser);
5472 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5474 int ext;
5475 ext = disable_extension_diagnostics ();
5476 c_parser_consume_token (parser);
5477 c_parser_declaration_or_fndef (parser, true, true, true, true,
5478 true, &object_expression, vNULL);
5479 parser->objc_could_be_foreach_context = false;
5481 restore_extension_diagnostics (ext);
5482 if (c_parser_next_token_is_keyword (parser, RID_IN))
5484 c_parser_consume_token (parser);
5485 is_foreach_statement = true;
5486 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5487 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5489 else
5490 check_for_loop_decls (for_loc, flag_isoc99);
5492 else
5493 goto init_expr;
5495 else
5497 init_expr:
5499 struct c_expr ce;
5500 tree init_expression;
5501 ce = c_parser_expression (parser);
5502 init_expression = ce.value;
5503 parser->objc_could_be_foreach_context = false;
5504 if (c_parser_next_token_is_keyword (parser, RID_IN))
5506 c_parser_consume_token (parser);
5507 is_foreach_statement = true;
5508 if (! lvalue_p (init_expression))
5509 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5510 object_expression = c_fully_fold (init_expression, false, NULL);
5512 else
5514 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5515 init_expression = ce.value;
5516 c_finish_expr_stmt (loc, init_expression);
5517 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5521 /* Parse the loop condition. In the case of a foreach
5522 statement, there is no loop condition. */
5523 gcc_assert (!parser->objc_could_be_foreach_context);
5524 if (!is_foreach_statement)
5526 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5528 if (ivdep)
5530 c_parser_error (parser, "missing loop condition in loop with "
5531 "%<GCC ivdep%> pragma");
5532 cond = error_mark_node;
5534 else
5536 c_parser_consume_token (parser);
5537 cond = NULL_TREE;
5540 else
5542 cond = c_parser_condition (parser);
5543 if (flag_cilkplus && contains_array_notation_expr (cond))
5545 error_at (loc, "array notations cannot be used in a "
5546 "condition for a for-loop");
5547 cond = error_mark_node;
5549 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5550 "expected %<;%>");
5552 if (ivdep && cond != error_mark_node)
5553 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5554 build_int_cst (integer_type_node,
5555 annot_expr_ivdep_kind));
5557 /* Parse the increment expression (the third expression in a
5558 for-statement). In the case of a foreach-statement, this is
5559 the expression that follows the 'in'. */
5560 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5562 if (is_foreach_statement)
5564 c_parser_error (parser, "missing collection in fast enumeration");
5565 collection_expression = error_mark_node;
5567 else
5568 incr = c_process_expr_stmt (loc, NULL_TREE);
5570 else
5572 if (is_foreach_statement)
5573 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5574 false, NULL);
5575 else
5577 struct c_expr ce = c_parser_expression (parser);
5578 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5579 incr = c_process_expr_stmt (loc, ce.value);
5582 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5584 save_break = c_break_label;
5585 c_break_label = NULL_TREE;
5586 save_cont = c_cont_label;
5587 c_cont_label = NULL_TREE;
5588 body = c_parser_c99_block_statement (parser);
5589 if (is_foreach_statement)
5590 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5591 else
5592 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5593 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5594 c_break_label = save_break;
5595 c_cont_label = save_cont;
5598 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5599 statement with inputs, outputs, clobbers, and volatile tag
5600 allowed.
5602 asm-statement:
5603 asm type-qualifier[opt] ( asm-argument ) ;
5604 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5606 asm-argument:
5607 asm-string-literal
5608 asm-string-literal : asm-operands[opt]
5609 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5610 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5612 asm-goto-argument:
5613 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5614 : asm-goto-operands
5616 Qualifiers other than volatile are accepted in the syntax but
5617 warned for. */
5619 static tree
5620 c_parser_asm_statement (c_parser *parser)
5622 tree quals, str, outputs, inputs, clobbers, labels, ret;
5623 bool simple, is_goto;
5624 location_t asm_loc = c_parser_peek_token (parser)->location;
5625 int section, nsections;
5627 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5628 c_parser_consume_token (parser);
5629 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5631 quals = c_parser_peek_token (parser)->value;
5632 c_parser_consume_token (parser);
5634 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5635 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5637 warning_at (c_parser_peek_token (parser)->location,
5639 "%E qualifier ignored on asm",
5640 c_parser_peek_token (parser)->value);
5641 quals = NULL_TREE;
5642 c_parser_consume_token (parser);
5644 else
5645 quals = NULL_TREE;
5647 is_goto = false;
5648 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5650 c_parser_consume_token (parser);
5651 is_goto = true;
5654 /* ??? Follow the C++ parser rather than using the
5655 lex_untranslated_string kludge. */
5656 parser->lex_untranslated_string = true;
5657 ret = NULL;
5659 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5660 goto error;
5662 str = c_parser_asm_string_literal (parser);
5663 if (str == NULL_TREE)
5664 goto error_close_paren;
5666 simple = true;
5667 outputs = NULL_TREE;
5668 inputs = NULL_TREE;
5669 clobbers = NULL_TREE;
5670 labels = NULL_TREE;
5672 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5673 goto done_asm;
5675 /* Parse each colon-delimited section of operands. */
5676 nsections = 3 + is_goto;
5677 for (section = 0; section < nsections; ++section)
5679 if (!c_parser_require (parser, CPP_COLON,
5680 is_goto
5681 ? "expected %<:%>"
5682 : "expected %<:%> or %<)%>"))
5683 goto error_close_paren;
5685 /* Once past any colon, we're no longer a simple asm. */
5686 simple = false;
5688 if ((!c_parser_next_token_is (parser, CPP_COLON)
5689 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5690 || section == 3)
5691 switch (section)
5693 case 0:
5694 /* For asm goto, we don't allow output operands, but reserve
5695 the slot for a future extension that does allow them. */
5696 if (!is_goto)
5697 outputs = c_parser_asm_operands (parser);
5698 break;
5699 case 1:
5700 inputs = c_parser_asm_operands (parser);
5701 break;
5702 case 2:
5703 clobbers = c_parser_asm_clobbers (parser);
5704 break;
5705 case 3:
5706 labels = c_parser_asm_goto_operands (parser);
5707 break;
5708 default:
5709 gcc_unreachable ();
5712 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5713 goto done_asm;
5716 done_asm:
5717 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5719 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5720 goto error;
5723 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5724 c_parser_skip_to_end_of_block_or_statement (parser);
5726 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5727 clobbers, labels, simple));
5729 error:
5730 parser->lex_untranslated_string = false;
5731 return ret;
5733 error_close_paren:
5734 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5735 goto error;
5738 /* Parse asm operands, a GNU extension.
5740 asm-operands:
5741 asm-operand
5742 asm-operands , asm-operand
5744 asm-operand:
5745 asm-string-literal ( expression )
5746 [ identifier ] asm-string-literal ( expression )
5749 static tree
5750 c_parser_asm_operands (c_parser *parser)
5752 tree list = NULL_TREE;
5753 while (true)
5755 tree name, str;
5756 struct c_expr expr;
5757 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5759 c_parser_consume_token (parser);
5760 if (c_parser_next_token_is (parser, CPP_NAME))
5762 tree id = c_parser_peek_token (parser)->value;
5763 c_parser_consume_token (parser);
5764 name = build_string (IDENTIFIER_LENGTH (id),
5765 IDENTIFIER_POINTER (id));
5767 else
5769 c_parser_error (parser, "expected identifier");
5770 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5771 return NULL_TREE;
5773 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5774 "expected %<]%>");
5776 else
5777 name = NULL_TREE;
5778 str = c_parser_asm_string_literal (parser);
5779 if (str == NULL_TREE)
5780 return NULL_TREE;
5781 parser->lex_untranslated_string = false;
5782 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5784 parser->lex_untranslated_string = true;
5785 return NULL_TREE;
5787 expr = c_parser_expression (parser);
5788 mark_exp_read (expr.value);
5789 parser->lex_untranslated_string = true;
5790 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5792 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5793 return NULL_TREE;
5795 list = chainon (list, build_tree_list (build_tree_list (name, str),
5796 expr.value));
5797 if (c_parser_next_token_is (parser, CPP_COMMA))
5798 c_parser_consume_token (parser);
5799 else
5800 break;
5802 return list;
5805 /* Parse asm clobbers, a GNU extension.
5807 asm-clobbers:
5808 asm-string-literal
5809 asm-clobbers , asm-string-literal
5812 static tree
5813 c_parser_asm_clobbers (c_parser *parser)
5815 tree list = NULL_TREE;
5816 while (true)
5818 tree str = c_parser_asm_string_literal (parser);
5819 if (str)
5820 list = tree_cons (NULL_TREE, str, list);
5821 else
5822 return NULL_TREE;
5823 if (c_parser_next_token_is (parser, CPP_COMMA))
5824 c_parser_consume_token (parser);
5825 else
5826 break;
5828 return list;
5831 /* Parse asm goto labels, a GNU extension.
5833 asm-goto-operands:
5834 identifier
5835 asm-goto-operands , identifier
5838 static tree
5839 c_parser_asm_goto_operands (c_parser *parser)
5841 tree list = NULL_TREE;
5842 while (true)
5844 tree name, label;
5846 if (c_parser_next_token_is (parser, CPP_NAME))
5848 c_token *tok = c_parser_peek_token (parser);
5849 name = tok->value;
5850 label = lookup_label_for_goto (tok->location, name);
5851 c_parser_consume_token (parser);
5852 TREE_USED (label) = 1;
5854 else
5856 c_parser_error (parser, "expected identifier");
5857 return NULL_TREE;
5860 name = build_string (IDENTIFIER_LENGTH (name),
5861 IDENTIFIER_POINTER (name));
5862 list = tree_cons (name, label, list);
5863 if (c_parser_next_token_is (parser, CPP_COMMA))
5864 c_parser_consume_token (parser);
5865 else
5866 return nreverse (list);
5870 /* Parse an expression other than a compound expression; that is, an
5871 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5872 NULL then it is an Objective-C message expression which is the
5873 primary-expression starting the expression as an initializer.
5875 assignment-expression:
5876 conditional-expression
5877 unary-expression assignment-operator assignment-expression
5879 assignment-operator: one of
5880 = *= /= %= += -= <<= >>= &= ^= |=
5882 In GNU C we accept any conditional expression on the LHS and
5883 diagnose the invalid lvalue rather than producing a syntax
5884 error. */
5886 static struct c_expr
5887 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5888 tree omp_atomic_lhs)
5890 struct c_expr lhs, rhs, ret;
5891 enum tree_code code;
5892 location_t op_location, exp_location;
5893 gcc_assert (!after || c_dialect_objc ());
5894 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5895 op_location = c_parser_peek_token (parser)->location;
5896 switch (c_parser_peek_token (parser)->type)
5898 case CPP_EQ:
5899 code = NOP_EXPR;
5900 break;
5901 case CPP_MULT_EQ:
5902 code = MULT_EXPR;
5903 break;
5904 case CPP_DIV_EQ:
5905 code = TRUNC_DIV_EXPR;
5906 break;
5907 case CPP_MOD_EQ:
5908 code = TRUNC_MOD_EXPR;
5909 break;
5910 case CPP_PLUS_EQ:
5911 code = PLUS_EXPR;
5912 break;
5913 case CPP_MINUS_EQ:
5914 code = MINUS_EXPR;
5915 break;
5916 case CPP_LSHIFT_EQ:
5917 code = LSHIFT_EXPR;
5918 break;
5919 case CPP_RSHIFT_EQ:
5920 code = RSHIFT_EXPR;
5921 break;
5922 case CPP_AND_EQ:
5923 code = BIT_AND_EXPR;
5924 break;
5925 case CPP_XOR_EQ:
5926 code = BIT_XOR_EXPR;
5927 break;
5928 case CPP_OR_EQ:
5929 code = BIT_IOR_EXPR;
5930 break;
5931 default:
5932 return lhs;
5934 c_parser_consume_token (parser);
5935 exp_location = c_parser_peek_token (parser)->location;
5936 rhs = c_parser_expr_no_commas (parser, NULL);
5937 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5939 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5940 code, exp_location, rhs.value,
5941 rhs.original_type);
5942 if (code == NOP_EXPR)
5943 ret.original_code = MODIFY_EXPR;
5944 else
5946 TREE_NO_WARNING (ret.value) = 1;
5947 ret.original_code = ERROR_MARK;
5949 ret.original_type = NULL;
5950 return ret;
5953 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5954 is not NULL then it is an Objective-C message expression which is
5955 the primary-expression starting the expression as an initializer.
5957 conditional-expression:
5958 logical-OR-expression
5959 logical-OR-expression ? expression : conditional-expression
5961 GNU extensions:
5963 conditional-expression:
5964 logical-OR-expression ? : conditional-expression
5967 static struct c_expr
5968 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5969 tree omp_atomic_lhs)
5971 struct c_expr cond, exp1, exp2, ret;
5972 location_t cond_loc, colon_loc, middle_loc;
5974 gcc_assert (!after || c_dialect_objc ());
5976 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5978 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5979 return cond;
5980 cond_loc = c_parser_peek_token (parser)->location;
5981 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5982 c_parser_consume_token (parser);
5983 if (c_parser_next_token_is (parser, CPP_COLON))
5985 tree eptype = NULL_TREE;
5987 middle_loc = c_parser_peek_token (parser)->location;
5988 pedwarn (middle_loc, OPT_Wpedantic,
5989 "ISO C forbids omitting the middle term of a ?: expression");
5990 warn_for_omitted_condop (middle_loc, cond.value);
5991 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5993 eptype = TREE_TYPE (cond.value);
5994 cond.value = TREE_OPERAND (cond.value, 0);
5996 /* Make sure first operand is calculated only once. */
5997 exp1.value = c_save_expr (default_conversion (cond.value));
5998 if (eptype)
5999 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6000 exp1.original_type = NULL;
6001 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6002 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6004 else
6006 cond.value
6007 = c_objc_common_truthvalue_conversion
6008 (cond_loc, default_conversion (cond.value));
6009 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6010 exp1 = c_parser_expression_conv (parser);
6011 mark_exp_read (exp1.value);
6012 c_inhibit_evaluation_warnings +=
6013 ((cond.value == truthvalue_true_node)
6014 - (cond.value == truthvalue_false_node));
6017 colon_loc = c_parser_peek_token (parser)->location;
6018 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6020 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6021 ret.value = error_mark_node;
6022 ret.original_code = ERROR_MARK;
6023 ret.original_type = NULL;
6024 return ret;
6027 location_t exp2_loc = c_parser_peek_token (parser)->location;
6028 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6029 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6031 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6032 ret.value = build_conditional_expr (colon_loc, cond.value,
6033 cond.original_code == C_MAYBE_CONST_EXPR,
6034 exp1.value, exp1.original_type,
6035 exp2.value, exp2.original_type);
6036 ret.original_code = ERROR_MARK;
6037 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6038 ret.original_type = NULL;
6039 else
6041 tree t1, t2;
6043 /* If both sides are enum type, the default conversion will have
6044 made the type of the result be an integer type. We want to
6045 remember the enum types we started with. */
6046 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6047 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6048 ret.original_type = ((t1 != error_mark_node
6049 && t2 != error_mark_node
6050 && (TYPE_MAIN_VARIANT (t1)
6051 == TYPE_MAIN_VARIANT (t2)))
6052 ? t1
6053 : NULL);
6055 return ret;
6058 /* Parse a binary expression; that is, a logical-OR-expression (C90
6059 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6060 an Objective-C message expression which is the primary-expression
6061 starting the expression as an initializer.
6063 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6064 when it should be the unfolded lhs. In a valid OpenMP source,
6065 one of the operands of the toplevel binary expression must be equal
6066 to it. In that case, just return a build2 created binary operation
6067 rather than result of parser_build_binary_op.
6069 multiplicative-expression:
6070 cast-expression
6071 multiplicative-expression * cast-expression
6072 multiplicative-expression / cast-expression
6073 multiplicative-expression % cast-expression
6075 additive-expression:
6076 multiplicative-expression
6077 additive-expression + multiplicative-expression
6078 additive-expression - multiplicative-expression
6080 shift-expression:
6081 additive-expression
6082 shift-expression << additive-expression
6083 shift-expression >> additive-expression
6085 relational-expression:
6086 shift-expression
6087 relational-expression < shift-expression
6088 relational-expression > shift-expression
6089 relational-expression <= shift-expression
6090 relational-expression >= shift-expression
6092 equality-expression:
6093 relational-expression
6094 equality-expression == relational-expression
6095 equality-expression != relational-expression
6097 AND-expression:
6098 equality-expression
6099 AND-expression & equality-expression
6101 exclusive-OR-expression:
6102 AND-expression
6103 exclusive-OR-expression ^ AND-expression
6105 inclusive-OR-expression:
6106 exclusive-OR-expression
6107 inclusive-OR-expression | exclusive-OR-expression
6109 logical-AND-expression:
6110 inclusive-OR-expression
6111 logical-AND-expression && inclusive-OR-expression
6113 logical-OR-expression:
6114 logical-AND-expression
6115 logical-OR-expression || logical-AND-expression
6118 static struct c_expr
6119 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6120 tree omp_atomic_lhs)
6122 /* A binary expression is parsed using operator-precedence parsing,
6123 with the operands being cast expressions. All the binary
6124 operators are left-associative. Thus a binary expression is of
6125 form:
6127 E0 op1 E1 op2 E2 ...
6129 which we represent on a stack. On the stack, the precedence
6130 levels are strictly increasing. When a new operator is
6131 encountered of higher precedence than that at the top of the
6132 stack, it is pushed; its LHS is the top expression, and its RHS
6133 is everything parsed until it is popped. When a new operator is
6134 encountered with precedence less than or equal to that at the top
6135 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6136 by the result of the operation until the operator at the top of
6137 the stack has lower precedence than the new operator or there is
6138 only one element on the stack; then the top expression is the LHS
6139 of the new operator. In the case of logical AND and OR
6140 expressions, we also need to adjust c_inhibit_evaluation_warnings
6141 as appropriate when the operators are pushed and popped. */
6143 struct {
6144 /* The expression at this stack level. */
6145 struct c_expr expr;
6146 /* The precedence of the operator on its left, PREC_NONE at the
6147 bottom of the stack. */
6148 enum c_parser_prec prec;
6149 /* The operation on its left. */
6150 enum tree_code op;
6151 /* The source location of this operation. */
6152 location_t loc;
6153 } stack[NUM_PRECS];
6154 int sp;
6155 /* Location of the binary operator. */
6156 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6157 #define POP \
6158 do { \
6159 switch (stack[sp].op) \
6161 case TRUTH_ANDIF_EXPR: \
6162 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6163 == truthvalue_false_node); \
6164 break; \
6165 case TRUTH_ORIF_EXPR: \
6166 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6167 == truthvalue_true_node); \
6168 break; \
6169 default: \
6170 break; \
6172 stack[sp - 1].expr \
6173 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6174 stack[sp - 1].expr, true, true); \
6175 stack[sp].expr \
6176 = convert_lvalue_to_rvalue (stack[sp].loc, \
6177 stack[sp].expr, true, true); \
6178 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6179 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6180 && ((1 << stack[sp].prec) \
6181 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6182 | PREC_ADD | PREC_MULT))) \
6183 && stack[sp].op != TRUNC_MOD_EXPR \
6184 && stack[0].expr.value != error_mark_node \
6185 && stack[1].expr.value != error_mark_node \
6186 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6187 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6188 stack[0].expr.value \
6189 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6190 stack[0].expr.value, stack[1].expr.value); \
6191 else \
6192 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6193 stack[sp].op, \
6194 stack[sp - 1].expr, \
6195 stack[sp].expr); \
6196 sp--; \
6197 } while (0)
6198 gcc_assert (!after || c_dialect_objc ());
6199 stack[0].loc = c_parser_peek_token (parser)->location;
6200 stack[0].expr = c_parser_cast_expression (parser, after);
6201 stack[0].prec = PREC_NONE;
6202 sp = 0;
6203 while (true)
6205 enum c_parser_prec oprec;
6206 enum tree_code ocode;
6207 if (parser->error)
6208 goto out;
6209 switch (c_parser_peek_token (parser)->type)
6211 case CPP_MULT:
6212 oprec = PREC_MULT;
6213 ocode = MULT_EXPR;
6214 break;
6215 case CPP_DIV:
6216 oprec = PREC_MULT;
6217 ocode = TRUNC_DIV_EXPR;
6218 break;
6219 case CPP_MOD:
6220 oprec = PREC_MULT;
6221 ocode = TRUNC_MOD_EXPR;
6222 break;
6223 case CPP_PLUS:
6224 oprec = PREC_ADD;
6225 ocode = PLUS_EXPR;
6226 break;
6227 case CPP_MINUS:
6228 oprec = PREC_ADD;
6229 ocode = MINUS_EXPR;
6230 break;
6231 case CPP_LSHIFT:
6232 oprec = PREC_SHIFT;
6233 ocode = LSHIFT_EXPR;
6234 break;
6235 case CPP_RSHIFT:
6236 oprec = PREC_SHIFT;
6237 ocode = RSHIFT_EXPR;
6238 break;
6239 case CPP_LESS:
6240 oprec = PREC_REL;
6241 ocode = LT_EXPR;
6242 break;
6243 case CPP_GREATER:
6244 oprec = PREC_REL;
6245 ocode = GT_EXPR;
6246 break;
6247 case CPP_LESS_EQ:
6248 oprec = PREC_REL;
6249 ocode = LE_EXPR;
6250 break;
6251 case CPP_GREATER_EQ:
6252 oprec = PREC_REL;
6253 ocode = GE_EXPR;
6254 break;
6255 case CPP_EQ_EQ:
6256 oprec = PREC_EQ;
6257 ocode = EQ_EXPR;
6258 break;
6259 case CPP_NOT_EQ:
6260 oprec = PREC_EQ;
6261 ocode = NE_EXPR;
6262 break;
6263 case CPP_AND:
6264 oprec = PREC_BITAND;
6265 ocode = BIT_AND_EXPR;
6266 break;
6267 case CPP_XOR:
6268 oprec = PREC_BITXOR;
6269 ocode = BIT_XOR_EXPR;
6270 break;
6271 case CPP_OR:
6272 oprec = PREC_BITOR;
6273 ocode = BIT_IOR_EXPR;
6274 break;
6275 case CPP_AND_AND:
6276 oprec = PREC_LOGAND;
6277 ocode = TRUTH_ANDIF_EXPR;
6278 break;
6279 case CPP_OR_OR:
6280 oprec = PREC_LOGOR;
6281 ocode = TRUTH_ORIF_EXPR;
6282 break;
6283 default:
6284 /* Not a binary operator, so end of the binary
6285 expression. */
6286 goto out;
6288 binary_loc = c_parser_peek_token (parser)->location;
6289 while (oprec <= stack[sp].prec)
6290 POP;
6291 c_parser_consume_token (parser);
6292 switch (ocode)
6294 case TRUTH_ANDIF_EXPR:
6295 stack[sp].expr
6296 = convert_lvalue_to_rvalue (stack[sp].loc,
6297 stack[sp].expr, true, true);
6298 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6299 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6300 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6301 == truthvalue_false_node);
6302 break;
6303 case TRUTH_ORIF_EXPR:
6304 stack[sp].expr
6305 = convert_lvalue_to_rvalue (stack[sp].loc,
6306 stack[sp].expr, true, true);
6307 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6308 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6309 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6310 == truthvalue_true_node);
6311 break;
6312 default:
6313 break;
6315 sp++;
6316 stack[sp].loc = binary_loc;
6317 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6318 stack[sp].prec = oprec;
6319 stack[sp].op = ocode;
6320 stack[sp].loc = binary_loc;
6322 out:
6323 while (sp > 0)
6324 POP;
6325 return stack[0].expr;
6326 #undef POP
6329 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6330 NULL then it is an Objective-C message expression which is the
6331 primary-expression starting the expression as an initializer.
6333 cast-expression:
6334 unary-expression
6335 ( type-name ) unary-expression
6338 static struct c_expr
6339 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6341 location_t cast_loc = c_parser_peek_token (parser)->location;
6342 gcc_assert (!after || c_dialect_objc ());
6343 if (after)
6344 return c_parser_postfix_expression_after_primary (parser,
6345 cast_loc, *after);
6346 /* If the expression begins with a parenthesized type name, it may
6347 be either a cast or a compound literal; we need to see whether
6348 the next character is '{' to tell the difference. If not, it is
6349 an unary expression. Full detection of unknown typenames here
6350 would require a 3-token lookahead. */
6351 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6352 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6354 struct c_type_name *type_name;
6355 struct c_expr ret;
6356 struct c_expr expr;
6357 c_parser_consume_token (parser);
6358 type_name = c_parser_type_name (parser);
6359 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6360 if (type_name == NULL)
6362 ret.value = error_mark_node;
6363 ret.original_code = ERROR_MARK;
6364 ret.original_type = NULL;
6365 return ret;
6368 /* Save casted types in the function's used types hash table. */
6369 used_types_insert (type_name->specs->type);
6371 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6372 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6373 cast_loc);
6375 location_t expr_loc = c_parser_peek_token (parser)->location;
6376 expr = c_parser_cast_expression (parser, NULL);
6377 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6379 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6380 ret.original_code = ERROR_MARK;
6381 ret.original_type = NULL;
6382 return ret;
6384 else
6385 return c_parser_unary_expression (parser);
6388 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6390 unary-expression:
6391 postfix-expression
6392 ++ unary-expression
6393 -- unary-expression
6394 unary-operator cast-expression
6395 sizeof unary-expression
6396 sizeof ( type-name )
6398 unary-operator: one of
6399 & * + - ~ !
6401 GNU extensions:
6403 unary-expression:
6404 __alignof__ unary-expression
6405 __alignof__ ( type-name )
6406 && identifier
6408 (C11 permits _Alignof with type names only.)
6410 unary-operator: one of
6411 __extension__ __real__ __imag__
6413 Transactional Memory:
6415 unary-expression:
6416 transaction-expression
6418 In addition, the GNU syntax treats ++ and -- as unary operators, so
6419 they may be applied to cast expressions with errors for non-lvalues
6420 given later. */
6422 static struct c_expr
6423 c_parser_unary_expression (c_parser *parser)
6425 int ext;
6426 struct c_expr ret, op;
6427 location_t op_loc = c_parser_peek_token (parser)->location;
6428 location_t exp_loc;
6429 ret.original_code = ERROR_MARK;
6430 ret.original_type = NULL;
6431 switch (c_parser_peek_token (parser)->type)
6433 case CPP_PLUS_PLUS:
6434 c_parser_consume_token (parser);
6435 exp_loc = c_parser_peek_token (parser)->location;
6436 op = c_parser_cast_expression (parser, NULL);
6438 /* If there is array notations in op, we expand them. */
6439 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6440 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6441 else
6443 op = default_function_array_read_conversion (exp_loc, op);
6444 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6446 case CPP_MINUS_MINUS:
6447 c_parser_consume_token (parser);
6448 exp_loc = c_parser_peek_token (parser)->location;
6449 op = c_parser_cast_expression (parser, NULL);
6451 /* If there is array notations in op, we expand them. */
6452 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6453 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6454 else
6456 op = default_function_array_read_conversion (exp_loc, op);
6457 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6459 case CPP_AND:
6460 c_parser_consume_token (parser);
6461 op = c_parser_cast_expression (parser, NULL);
6462 mark_exp_read (op.value);
6463 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6464 case CPP_MULT:
6465 c_parser_consume_token (parser);
6466 exp_loc = c_parser_peek_token (parser)->location;
6467 op = c_parser_cast_expression (parser, NULL);
6468 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6469 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6470 return ret;
6471 case CPP_PLUS:
6472 if (!c_dialect_objc () && !in_system_header_at (input_location))
6473 warning_at (op_loc,
6474 OPT_Wtraditional,
6475 "traditional C rejects the unary plus operator");
6476 c_parser_consume_token (parser);
6477 exp_loc = c_parser_peek_token (parser)->location;
6478 op = c_parser_cast_expression (parser, NULL);
6479 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6480 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6481 case CPP_MINUS:
6482 c_parser_consume_token (parser);
6483 exp_loc = c_parser_peek_token (parser)->location;
6484 op = c_parser_cast_expression (parser, NULL);
6485 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6486 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6487 case CPP_COMPL:
6488 c_parser_consume_token (parser);
6489 exp_loc = c_parser_peek_token (parser)->location;
6490 op = c_parser_cast_expression (parser, NULL);
6491 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6492 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6493 case CPP_NOT:
6494 c_parser_consume_token (parser);
6495 exp_loc = c_parser_peek_token (parser)->location;
6496 op = c_parser_cast_expression (parser, NULL);
6497 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6498 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6499 case CPP_AND_AND:
6500 /* Refer to the address of a label as a pointer. */
6501 c_parser_consume_token (parser);
6502 if (c_parser_next_token_is (parser, CPP_NAME))
6504 ret.value = finish_label_address_expr
6505 (c_parser_peek_token (parser)->value, op_loc);
6506 c_parser_consume_token (parser);
6508 else
6510 c_parser_error (parser, "expected identifier");
6511 ret.value = error_mark_node;
6513 return ret;
6514 case CPP_KEYWORD:
6515 switch (c_parser_peek_token (parser)->keyword)
6517 case RID_SIZEOF:
6518 return c_parser_sizeof_expression (parser);
6519 case RID_ALIGNOF:
6520 return c_parser_alignof_expression (parser);
6521 case RID_EXTENSION:
6522 c_parser_consume_token (parser);
6523 ext = disable_extension_diagnostics ();
6524 ret = c_parser_cast_expression (parser, NULL);
6525 restore_extension_diagnostics (ext);
6526 return ret;
6527 case RID_REALPART:
6528 c_parser_consume_token (parser);
6529 exp_loc = c_parser_peek_token (parser)->location;
6530 op = c_parser_cast_expression (parser, NULL);
6531 op = default_function_array_conversion (exp_loc, op);
6532 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6533 case RID_IMAGPART:
6534 c_parser_consume_token (parser);
6535 exp_loc = c_parser_peek_token (parser)->location;
6536 op = c_parser_cast_expression (parser, NULL);
6537 op = default_function_array_conversion (exp_loc, op);
6538 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6539 case RID_TRANSACTION_ATOMIC:
6540 case RID_TRANSACTION_RELAXED:
6541 return c_parser_transaction_expression (parser,
6542 c_parser_peek_token (parser)->keyword);
6543 default:
6544 return c_parser_postfix_expression (parser);
6546 default:
6547 return c_parser_postfix_expression (parser);
6551 /* Parse a sizeof expression. */
6553 static struct c_expr
6554 c_parser_sizeof_expression (c_parser *parser)
6556 struct c_expr expr;
6557 location_t expr_loc;
6558 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6559 c_parser_consume_token (parser);
6560 c_inhibit_evaluation_warnings++;
6561 in_sizeof++;
6562 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6563 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6565 /* Either sizeof ( type-name ) or sizeof unary-expression
6566 starting with a compound literal. */
6567 struct c_type_name *type_name;
6568 c_parser_consume_token (parser);
6569 expr_loc = c_parser_peek_token (parser)->location;
6570 type_name = c_parser_type_name (parser);
6571 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6572 if (type_name == NULL)
6574 struct c_expr ret;
6575 c_inhibit_evaluation_warnings--;
6576 in_sizeof--;
6577 ret.value = error_mark_node;
6578 ret.original_code = ERROR_MARK;
6579 ret.original_type = NULL;
6580 return ret;
6582 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6584 expr = c_parser_postfix_expression_after_paren_type (parser,
6585 type_name,
6586 expr_loc);
6587 goto sizeof_expr;
6589 /* sizeof ( type-name ). */
6590 c_inhibit_evaluation_warnings--;
6591 in_sizeof--;
6592 return c_expr_sizeof_type (expr_loc, type_name);
6594 else
6596 expr_loc = c_parser_peek_token (parser)->location;
6597 expr = c_parser_unary_expression (parser);
6598 sizeof_expr:
6599 c_inhibit_evaluation_warnings--;
6600 in_sizeof--;
6601 mark_exp_read (expr.value);
6602 if (TREE_CODE (expr.value) == COMPONENT_REF
6603 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6604 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6605 return c_expr_sizeof_expr (expr_loc, expr);
6609 /* Parse an alignof expression. */
6611 static struct c_expr
6612 c_parser_alignof_expression (c_parser *parser)
6614 struct c_expr expr;
6615 location_t loc = c_parser_peek_token (parser)->location;
6616 tree alignof_spelling = c_parser_peek_token (parser)->value;
6617 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6618 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6619 "_Alignof") == 0;
6620 /* A diagnostic is not required for the use of this identifier in
6621 the implementation namespace; only diagnose it for the C11
6622 spelling because of existing code using the other spellings. */
6623 if (is_c11_alignof)
6625 if (flag_isoc99)
6626 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6627 alignof_spelling);
6628 else
6629 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6630 alignof_spelling);
6632 c_parser_consume_token (parser);
6633 c_inhibit_evaluation_warnings++;
6634 in_alignof++;
6635 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6636 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6638 /* Either __alignof__ ( type-name ) or __alignof__
6639 unary-expression starting with a compound literal. */
6640 location_t loc;
6641 struct c_type_name *type_name;
6642 struct c_expr ret;
6643 c_parser_consume_token (parser);
6644 loc = c_parser_peek_token (parser)->location;
6645 type_name = c_parser_type_name (parser);
6646 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6647 if (type_name == NULL)
6649 struct c_expr ret;
6650 c_inhibit_evaluation_warnings--;
6651 in_alignof--;
6652 ret.value = error_mark_node;
6653 ret.original_code = ERROR_MARK;
6654 ret.original_type = NULL;
6655 return ret;
6657 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6659 expr = c_parser_postfix_expression_after_paren_type (parser,
6660 type_name,
6661 loc);
6662 goto alignof_expr;
6664 /* alignof ( type-name ). */
6665 c_inhibit_evaluation_warnings--;
6666 in_alignof--;
6667 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6668 NULL, NULL),
6669 false, is_c11_alignof, 1);
6670 ret.original_code = ERROR_MARK;
6671 ret.original_type = NULL;
6672 return ret;
6674 else
6676 struct c_expr ret;
6677 expr = c_parser_unary_expression (parser);
6678 alignof_expr:
6679 mark_exp_read (expr.value);
6680 c_inhibit_evaluation_warnings--;
6681 in_alignof--;
6682 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6683 alignof_spelling);
6684 ret.value = c_alignof_expr (loc, expr.value);
6685 ret.original_code = ERROR_MARK;
6686 ret.original_type = NULL;
6687 return ret;
6691 /* Helper function to read arguments of builtins which are interfaces
6692 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6693 others. The name of the builtin is passed using BNAME parameter.
6694 Function returns true if there were no errors while parsing and
6695 stores the arguments in CEXPR_LIST. */
6696 static bool
6697 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6698 vec<c_expr_t, va_gc> **ret_cexpr_list,
6699 bool choose_expr_p)
6701 location_t loc = c_parser_peek_token (parser)->location;
6702 vec<c_expr_t, va_gc> *cexpr_list;
6703 c_expr_t expr;
6704 bool saved_force_folding_builtin_constant_p;
6706 *ret_cexpr_list = NULL;
6707 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6709 error_at (loc, "cannot take address of %qs", bname);
6710 return false;
6713 c_parser_consume_token (parser);
6715 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6717 c_parser_consume_token (parser);
6718 return true;
6721 saved_force_folding_builtin_constant_p
6722 = force_folding_builtin_constant_p;
6723 force_folding_builtin_constant_p |= choose_expr_p;
6724 expr = c_parser_expr_no_commas (parser, NULL);
6725 force_folding_builtin_constant_p
6726 = saved_force_folding_builtin_constant_p;
6727 vec_alloc (cexpr_list, 1);
6728 vec_safe_push (cexpr_list, expr);
6729 while (c_parser_next_token_is (parser, CPP_COMMA))
6731 c_parser_consume_token (parser);
6732 expr = c_parser_expr_no_commas (parser, NULL);
6733 vec_safe_push (cexpr_list, expr);
6736 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6737 return false;
6739 *ret_cexpr_list = cexpr_list;
6740 return true;
6743 /* This represents a single generic-association. */
6745 struct c_generic_association
6747 /* The location of the starting token of the type. */
6748 location_t type_location;
6749 /* The association's type, or NULL_TREE for 'default'. */
6750 tree type;
6751 /* The association's expression. */
6752 struct c_expr expression;
6755 /* Parse a generic-selection. (C11 6.5.1.1).
6757 generic-selection:
6758 _Generic ( assignment-expression , generic-assoc-list )
6760 generic-assoc-list:
6761 generic-association
6762 generic-assoc-list , generic-association
6764 generic-association:
6765 type-name : assignment-expression
6766 default : assignment-expression
6769 static struct c_expr
6770 c_parser_generic_selection (c_parser *parser)
6772 vec<c_generic_association> associations = vNULL;
6773 struct c_expr selector, error_expr;
6774 tree selector_type;
6775 struct c_generic_association matched_assoc;
6776 bool match_found = false;
6777 location_t generic_loc, selector_loc;
6779 error_expr.original_code = ERROR_MARK;
6780 error_expr.original_type = NULL;
6781 error_expr.value = error_mark_node;
6782 matched_assoc.type_location = UNKNOWN_LOCATION;
6783 matched_assoc.type = NULL_TREE;
6784 matched_assoc.expression = error_expr;
6786 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6787 generic_loc = c_parser_peek_token (parser)->location;
6788 c_parser_consume_token (parser);
6789 if (flag_isoc99)
6790 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6791 "ISO C99 does not support %<_Generic%>");
6792 else
6793 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6794 "ISO C90 does not support %<_Generic%>");
6796 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6797 return error_expr;
6799 c_inhibit_evaluation_warnings++;
6800 selector_loc = c_parser_peek_token (parser)->location;
6801 selector = c_parser_expr_no_commas (parser, NULL);
6802 selector = default_function_array_conversion (selector_loc, selector);
6803 c_inhibit_evaluation_warnings--;
6805 if (selector.value == error_mark_node)
6807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6808 return selector;
6810 selector_type = TREE_TYPE (selector.value);
6811 /* In ISO C terms, rvalues (including the controlling expression of
6812 _Generic) do not have qualified types. */
6813 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6814 selector_type = TYPE_MAIN_VARIANT (selector_type);
6815 /* In ISO C terms, _Noreturn is not part of the type of expressions
6816 such as &abort, but in GCC it is represented internally as a type
6817 qualifier. */
6818 if (FUNCTION_POINTER_TYPE_P (selector_type)
6819 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6820 selector_type
6821 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6823 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6825 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6826 return error_expr;
6829 while (1)
6831 struct c_generic_association assoc, *iter;
6832 unsigned int ix;
6833 c_token *token = c_parser_peek_token (parser);
6835 assoc.type_location = token->location;
6836 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6838 c_parser_consume_token (parser);
6839 assoc.type = NULL_TREE;
6841 else
6843 struct c_type_name *type_name;
6845 type_name = c_parser_type_name (parser);
6846 if (type_name == NULL)
6848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6849 goto error_exit;
6851 assoc.type = groktypename (type_name, NULL, NULL);
6852 if (assoc.type == error_mark_node)
6854 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6855 goto error_exit;
6858 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6859 error_at (assoc.type_location,
6860 "%<_Generic%> association has function type");
6861 else if (!COMPLETE_TYPE_P (assoc.type))
6862 error_at (assoc.type_location,
6863 "%<_Generic%> association has incomplete type");
6865 if (variably_modified_type_p (assoc.type, NULL_TREE))
6866 error_at (assoc.type_location,
6867 "%<_Generic%> association has "
6868 "variable length type");
6871 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6873 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6874 goto error_exit;
6877 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6878 if (assoc.expression.value == error_mark_node)
6880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6881 goto error_exit;
6884 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6886 if (assoc.type == NULL_TREE)
6888 if (iter->type == NULL_TREE)
6890 error_at (assoc.type_location,
6891 "duplicate %<default%> case in %<_Generic%>");
6892 inform (iter->type_location, "original %<default%> is here");
6895 else if (iter->type != NULL_TREE)
6897 if (comptypes (assoc.type, iter->type))
6899 error_at (assoc.type_location,
6900 "%<_Generic%> specifies two compatible types");
6901 inform (iter->type_location, "compatible type is here");
6906 if (assoc.type == NULL_TREE)
6908 if (!match_found)
6910 matched_assoc = assoc;
6911 match_found = true;
6914 else if (comptypes (assoc.type, selector_type))
6916 if (!match_found || matched_assoc.type == NULL_TREE)
6918 matched_assoc = assoc;
6919 match_found = true;
6921 else
6923 error_at (assoc.type_location,
6924 "%<_Generic> selector matches multiple associations");
6925 inform (matched_assoc.type_location,
6926 "other match is here");
6930 associations.safe_push (assoc);
6932 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6933 break;
6934 c_parser_consume_token (parser);
6937 associations.release ();
6939 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6941 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6942 return error_expr;
6945 if (!match_found)
6947 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6948 "compatible with any association",
6949 selector_type);
6950 return error_expr;
6953 return matched_assoc.expression;
6955 error_exit:
6956 associations.release ();
6957 return error_expr;
6960 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6962 postfix-expression:
6963 primary-expression
6964 postfix-expression [ expression ]
6965 postfix-expression ( argument-expression-list[opt] )
6966 postfix-expression . identifier
6967 postfix-expression -> identifier
6968 postfix-expression ++
6969 postfix-expression --
6970 ( type-name ) { initializer-list }
6971 ( type-name ) { initializer-list , }
6973 argument-expression-list:
6974 argument-expression
6975 argument-expression-list , argument-expression
6977 primary-expression:
6978 identifier
6979 constant
6980 string-literal
6981 ( expression )
6982 generic-selection
6984 GNU extensions:
6986 primary-expression:
6987 __func__
6988 (treated as a keyword in GNU C)
6989 __FUNCTION__
6990 __PRETTY_FUNCTION__
6991 ( compound-statement )
6992 __builtin_va_arg ( assignment-expression , type-name )
6993 __builtin_offsetof ( type-name , offsetof-member-designator )
6994 __builtin_choose_expr ( assignment-expression ,
6995 assignment-expression ,
6996 assignment-expression )
6997 __builtin_types_compatible_p ( type-name , type-name )
6998 __builtin_complex ( assignment-expression , assignment-expression )
6999 __builtin_shuffle ( assignment-expression , assignment-expression )
7000 __builtin_shuffle ( assignment-expression ,
7001 assignment-expression ,
7002 assignment-expression, )
7004 offsetof-member-designator:
7005 identifier
7006 offsetof-member-designator . identifier
7007 offsetof-member-designator [ expression ]
7009 Objective-C:
7011 primary-expression:
7012 [ objc-receiver objc-message-args ]
7013 @selector ( objc-selector-arg )
7014 @protocol ( identifier )
7015 @encode ( type-name )
7016 objc-string-literal
7017 Classname . identifier
7020 static struct c_expr
7021 c_parser_postfix_expression (c_parser *parser)
7023 struct c_expr expr, e1;
7024 struct c_type_name *t1, *t2;
7025 location_t loc = c_parser_peek_token (parser)->location;;
7026 expr.original_code = ERROR_MARK;
7027 expr.original_type = NULL;
7028 switch (c_parser_peek_token (parser)->type)
7030 case CPP_NUMBER:
7031 expr.value = c_parser_peek_token (parser)->value;
7032 loc = c_parser_peek_token (parser)->location;
7033 c_parser_consume_token (parser);
7034 if (TREE_CODE (expr.value) == FIXED_CST
7035 && !targetm.fixed_point_supported_p ())
7037 error_at (loc, "fixed-point types not supported for this target");
7038 expr.value = error_mark_node;
7040 break;
7041 case CPP_CHAR:
7042 case CPP_CHAR16:
7043 case CPP_CHAR32:
7044 case CPP_WCHAR:
7045 expr.value = c_parser_peek_token (parser)->value;
7046 c_parser_consume_token (parser);
7047 break;
7048 case CPP_STRING:
7049 case CPP_STRING16:
7050 case CPP_STRING32:
7051 case CPP_WSTRING:
7052 case CPP_UTF8STRING:
7053 expr.value = c_parser_peek_token (parser)->value;
7054 expr.original_code = STRING_CST;
7055 c_parser_consume_token (parser);
7056 break;
7057 case CPP_OBJC_STRING:
7058 gcc_assert (c_dialect_objc ());
7059 expr.value
7060 = objc_build_string_object (c_parser_peek_token (parser)->value);
7061 c_parser_consume_token (parser);
7062 break;
7063 case CPP_NAME:
7064 switch (c_parser_peek_token (parser)->id_kind)
7066 case C_ID_ID:
7068 tree id = c_parser_peek_token (parser)->value;
7069 c_parser_consume_token (parser);
7070 expr.value = build_external_ref (loc, id,
7071 (c_parser_peek_token (parser)->type
7072 == CPP_OPEN_PAREN),
7073 &expr.original_type);
7074 break;
7076 case C_ID_CLASSNAME:
7078 /* Here we parse the Objective-C 2.0 Class.name dot
7079 syntax. */
7080 tree class_name = c_parser_peek_token (parser)->value;
7081 tree component;
7082 c_parser_consume_token (parser);
7083 gcc_assert (c_dialect_objc ());
7084 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7086 expr.value = error_mark_node;
7087 break;
7089 if (c_parser_next_token_is_not (parser, CPP_NAME))
7091 c_parser_error (parser, "expected identifier");
7092 expr.value = error_mark_node;
7093 break;
7095 component = c_parser_peek_token (parser)->value;
7096 c_parser_consume_token (parser);
7097 expr.value = objc_build_class_component_ref (class_name,
7098 component);
7099 break;
7101 default:
7102 c_parser_error (parser, "expected expression");
7103 expr.value = error_mark_node;
7104 break;
7106 break;
7107 case CPP_OPEN_PAREN:
7108 /* A parenthesized expression, statement expression or compound
7109 literal. */
7110 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7112 /* A statement expression. */
7113 tree stmt;
7114 location_t brace_loc;
7115 c_parser_consume_token (parser);
7116 brace_loc = c_parser_peek_token (parser)->location;
7117 c_parser_consume_token (parser);
7118 if (!building_stmt_list_p ())
7120 error_at (loc, "braced-group within expression allowed "
7121 "only inside a function");
7122 parser->error = true;
7123 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7124 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7125 expr.value = error_mark_node;
7126 break;
7128 stmt = c_begin_stmt_expr ();
7129 c_parser_compound_statement_nostart (parser);
7130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7131 "expected %<)%>");
7132 pedwarn (loc, OPT_Wpedantic,
7133 "ISO C forbids braced-groups within expressions");
7134 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7135 mark_exp_read (expr.value);
7137 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7139 /* A compound literal. ??? Can we actually get here rather
7140 than going directly to
7141 c_parser_postfix_expression_after_paren_type from
7142 elsewhere? */
7143 location_t loc;
7144 struct c_type_name *type_name;
7145 c_parser_consume_token (parser);
7146 loc = c_parser_peek_token (parser)->location;
7147 type_name = c_parser_type_name (parser);
7148 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7149 "expected %<)%>");
7150 if (type_name == NULL)
7152 expr.value = error_mark_node;
7154 else
7155 expr = c_parser_postfix_expression_after_paren_type (parser,
7156 type_name,
7157 loc);
7159 else
7161 /* A parenthesized expression. */
7162 c_parser_consume_token (parser);
7163 expr = c_parser_expression (parser);
7164 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7165 TREE_NO_WARNING (expr.value) = 1;
7166 if (expr.original_code != C_MAYBE_CONST_EXPR)
7167 expr.original_code = ERROR_MARK;
7168 /* Don't change EXPR.ORIGINAL_TYPE. */
7169 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7170 "expected %<)%>");
7172 break;
7173 case CPP_KEYWORD:
7174 switch (c_parser_peek_token (parser)->keyword)
7176 case RID_FUNCTION_NAME:
7177 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7178 "%<__FUNCTION__%> predefined identifier");
7179 expr.value = fname_decl (loc,
7180 c_parser_peek_token (parser)->keyword,
7181 c_parser_peek_token (parser)->value);
7182 c_parser_consume_token (parser);
7183 break;
7184 case RID_PRETTY_FUNCTION_NAME:
7185 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7186 "%<__PRETTY_FUNCTION__%> predefined identifier");
7187 expr.value = fname_decl (loc,
7188 c_parser_peek_token (parser)->keyword,
7189 c_parser_peek_token (parser)->value);
7190 c_parser_consume_token (parser);
7191 break;
7192 case RID_C99_FUNCTION_NAME:
7193 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7194 "%<__func__%> predefined identifier");
7195 expr.value = fname_decl (loc,
7196 c_parser_peek_token (parser)->keyword,
7197 c_parser_peek_token (parser)->value);
7198 c_parser_consume_token (parser);
7199 break;
7200 case RID_VA_ARG:
7201 c_parser_consume_token (parser);
7202 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7204 expr.value = error_mark_node;
7205 break;
7207 e1 = c_parser_expr_no_commas (parser, NULL);
7208 mark_exp_read (e1.value);
7209 e1.value = c_fully_fold (e1.value, false, NULL);
7210 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7212 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7213 expr.value = error_mark_node;
7214 break;
7216 loc = c_parser_peek_token (parser)->location;
7217 t1 = c_parser_type_name (parser);
7218 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7219 "expected %<)%>");
7220 if (t1 == NULL)
7222 expr.value = error_mark_node;
7224 else
7226 tree type_expr = NULL_TREE;
7227 expr.value = c_build_va_arg (loc, e1.value,
7228 groktypename (t1, &type_expr, NULL));
7229 if (type_expr)
7231 expr.value = build2 (C_MAYBE_CONST_EXPR,
7232 TREE_TYPE (expr.value), type_expr,
7233 expr.value);
7234 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7237 break;
7238 case RID_OFFSETOF:
7239 c_parser_consume_token (parser);
7240 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7242 expr.value = error_mark_node;
7243 break;
7245 t1 = c_parser_type_name (parser);
7246 if (t1 == NULL)
7247 parser->error = true;
7248 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7249 gcc_assert (parser->error);
7250 if (parser->error)
7252 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7253 expr.value = error_mark_node;
7254 break;
7258 tree type = groktypename (t1, NULL, NULL);
7259 tree offsetof_ref;
7260 if (type == error_mark_node)
7261 offsetof_ref = error_mark_node;
7262 else
7264 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7265 SET_EXPR_LOCATION (offsetof_ref, loc);
7267 /* Parse the second argument to __builtin_offsetof. We
7268 must have one identifier, and beyond that we want to
7269 accept sub structure and sub array references. */
7270 if (c_parser_next_token_is (parser, CPP_NAME))
7272 offsetof_ref = build_component_ref
7273 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7274 c_parser_consume_token (parser);
7275 while (c_parser_next_token_is (parser, CPP_DOT)
7276 || c_parser_next_token_is (parser,
7277 CPP_OPEN_SQUARE)
7278 || c_parser_next_token_is (parser,
7279 CPP_DEREF))
7281 if (c_parser_next_token_is (parser, CPP_DEREF))
7283 loc = c_parser_peek_token (parser)->location;
7284 offsetof_ref = build_array_ref (loc,
7285 offsetof_ref,
7286 integer_zero_node);
7287 goto do_dot;
7289 else if (c_parser_next_token_is (parser, CPP_DOT))
7291 do_dot:
7292 c_parser_consume_token (parser);
7293 if (c_parser_next_token_is_not (parser,
7294 CPP_NAME))
7296 c_parser_error (parser, "expected identifier");
7297 break;
7299 offsetof_ref = build_component_ref
7300 (loc, offsetof_ref,
7301 c_parser_peek_token (parser)->value);
7302 c_parser_consume_token (parser);
7304 else
7306 struct c_expr ce;
7307 tree idx;
7308 loc = c_parser_peek_token (parser)->location;
7309 c_parser_consume_token (parser);
7310 ce = c_parser_expression (parser);
7311 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7312 idx = ce.value;
7313 idx = c_fully_fold (idx, false, NULL);
7314 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7315 "expected %<]%>");
7316 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7320 else
7321 c_parser_error (parser, "expected identifier");
7322 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7323 "expected %<)%>");
7324 expr.value = fold_offsetof (offsetof_ref);
7326 break;
7327 case RID_CHOOSE_EXPR:
7329 vec<c_expr_t, va_gc> *cexpr_list;
7330 c_expr_t *e1_p, *e2_p, *e3_p;
7331 tree c;
7333 c_parser_consume_token (parser);
7334 if (!c_parser_get_builtin_args (parser,
7335 "__builtin_choose_expr",
7336 &cexpr_list, true))
7338 expr.value = error_mark_node;
7339 break;
7342 if (vec_safe_length (cexpr_list) != 3)
7344 error_at (loc, "wrong number of arguments to "
7345 "%<__builtin_choose_expr%>");
7346 expr.value = error_mark_node;
7347 break;
7350 e1_p = &(*cexpr_list)[0];
7351 e2_p = &(*cexpr_list)[1];
7352 e3_p = &(*cexpr_list)[2];
7354 c = e1_p->value;
7355 mark_exp_read (e2_p->value);
7356 mark_exp_read (e3_p->value);
7357 if (TREE_CODE (c) != INTEGER_CST
7358 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7359 error_at (loc,
7360 "first argument to %<__builtin_choose_expr%> not"
7361 " a constant");
7362 constant_expression_warning (c);
7363 expr = integer_zerop (c) ? *e3_p : *e2_p;
7364 break;
7366 case RID_TYPES_COMPATIBLE_P:
7367 c_parser_consume_token (parser);
7368 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7370 expr.value = error_mark_node;
7371 break;
7373 t1 = c_parser_type_name (parser);
7374 if (t1 == NULL)
7376 expr.value = error_mark_node;
7377 break;
7379 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7381 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7382 expr.value = error_mark_node;
7383 break;
7385 t2 = c_parser_type_name (parser);
7386 if (t2 == NULL)
7388 expr.value = error_mark_node;
7389 break;
7391 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7392 "expected %<)%>");
7394 tree e1, e2;
7395 e1 = groktypename (t1, NULL, NULL);
7396 e2 = groktypename (t2, NULL, NULL);
7397 if (e1 == error_mark_node || e2 == error_mark_node)
7399 expr.value = error_mark_node;
7400 break;
7403 e1 = TYPE_MAIN_VARIANT (e1);
7404 e2 = TYPE_MAIN_VARIANT (e2);
7406 expr.value
7407 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7409 break;
7410 case RID_BUILTIN_COMPLEX:
7412 vec<c_expr_t, va_gc> *cexpr_list;
7413 c_expr_t *e1_p, *e2_p;
7415 c_parser_consume_token (parser);
7416 if (!c_parser_get_builtin_args (parser,
7417 "__builtin_complex",
7418 &cexpr_list, false))
7420 expr.value = error_mark_node;
7421 break;
7424 if (vec_safe_length (cexpr_list) != 2)
7426 error_at (loc, "wrong number of arguments to "
7427 "%<__builtin_complex%>");
7428 expr.value = error_mark_node;
7429 break;
7432 e1_p = &(*cexpr_list)[0];
7433 e2_p = &(*cexpr_list)[1];
7435 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7436 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7437 e1_p->value = convert (TREE_TYPE (e1_p->value),
7438 TREE_OPERAND (e1_p->value, 0));
7439 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7440 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7441 e2_p->value = convert (TREE_TYPE (e2_p->value),
7442 TREE_OPERAND (e2_p->value, 0));
7443 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7444 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7445 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7446 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7448 error_at (loc, "%<__builtin_complex%> operand "
7449 "not of real binary floating-point type");
7450 expr.value = error_mark_node;
7451 break;
7453 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7454 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7456 error_at (loc,
7457 "%<__builtin_complex%> operands of different types");
7458 expr.value = error_mark_node;
7459 break;
7461 pedwarn_c90 (loc, OPT_Wpedantic,
7462 "ISO C90 does not support complex types");
7463 expr.value = build2 (COMPLEX_EXPR,
7464 build_complex_type
7465 (TYPE_MAIN_VARIANT
7466 (TREE_TYPE (e1_p->value))),
7467 e1_p->value, e2_p->value);
7468 break;
7470 case RID_BUILTIN_SHUFFLE:
7472 vec<c_expr_t, va_gc> *cexpr_list;
7473 unsigned int i;
7474 c_expr_t *p;
7476 c_parser_consume_token (parser);
7477 if (!c_parser_get_builtin_args (parser,
7478 "__builtin_shuffle",
7479 &cexpr_list, false))
7481 expr.value = error_mark_node;
7482 break;
7485 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7486 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7488 if (vec_safe_length (cexpr_list) == 2)
7489 expr.value =
7490 c_build_vec_perm_expr
7491 (loc, (*cexpr_list)[0].value,
7492 NULL_TREE, (*cexpr_list)[1].value);
7494 else if (vec_safe_length (cexpr_list) == 3)
7495 expr.value =
7496 c_build_vec_perm_expr
7497 (loc, (*cexpr_list)[0].value,
7498 (*cexpr_list)[1].value,
7499 (*cexpr_list)[2].value);
7500 else
7502 error_at (loc, "wrong number of arguments to "
7503 "%<__builtin_shuffle%>");
7504 expr.value = error_mark_node;
7506 break;
7508 case RID_AT_SELECTOR:
7509 gcc_assert (c_dialect_objc ());
7510 c_parser_consume_token (parser);
7511 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7513 expr.value = error_mark_node;
7514 break;
7517 tree sel = c_parser_objc_selector_arg (parser);
7518 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7519 "expected %<)%>");
7520 expr.value = objc_build_selector_expr (loc, sel);
7522 break;
7523 case RID_AT_PROTOCOL:
7524 gcc_assert (c_dialect_objc ());
7525 c_parser_consume_token (parser);
7526 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7528 expr.value = error_mark_node;
7529 break;
7531 if (c_parser_next_token_is_not (parser, CPP_NAME))
7533 c_parser_error (parser, "expected identifier");
7534 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7535 expr.value = error_mark_node;
7536 break;
7539 tree id = c_parser_peek_token (parser)->value;
7540 c_parser_consume_token (parser);
7541 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7542 "expected %<)%>");
7543 expr.value = objc_build_protocol_expr (id);
7545 break;
7546 case RID_AT_ENCODE:
7547 /* Extension to support C-structures in the archiver. */
7548 gcc_assert (c_dialect_objc ());
7549 c_parser_consume_token (parser);
7550 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7552 expr.value = error_mark_node;
7553 break;
7555 t1 = c_parser_type_name (parser);
7556 if (t1 == NULL)
7558 expr.value = error_mark_node;
7559 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7560 break;
7562 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7563 "expected %<)%>");
7565 tree type = groktypename (t1, NULL, NULL);
7566 expr.value = objc_build_encode_expr (type);
7568 break;
7569 case RID_GENERIC:
7570 expr = c_parser_generic_selection (parser);
7571 break;
7572 case RID_CILK_SPAWN:
7573 c_parser_consume_token (parser);
7574 if (!flag_cilkplus)
7576 error_at (loc, "-fcilkplus must be enabled to use "
7577 "%<_Cilk_spawn%>");
7578 expr = c_parser_postfix_expression (parser);
7579 expr.value = error_mark_node;
7581 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7583 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7584 "are not permitted");
7585 /* Now flush out all the _Cilk_spawns. */
7586 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7587 c_parser_consume_token (parser);
7588 expr = c_parser_postfix_expression (parser);
7590 else
7592 expr = c_parser_postfix_expression (parser);
7593 expr.value = build_cilk_spawn (loc, expr.value);
7595 break;
7596 default:
7597 c_parser_error (parser, "expected expression");
7598 expr.value = error_mark_node;
7599 break;
7601 break;
7602 case CPP_OPEN_SQUARE:
7603 if (c_dialect_objc ())
7605 tree receiver, args;
7606 c_parser_consume_token (parser);
7607 receiver = c_parser_objc_receiver (parser);
7608 args = c_parser_objc_message_args (parser);
7609 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7610 "expected %<]%>");
7611 expr.value = objc_build_message_expr (receiver, args);
7612 break;
7614 /* Else fall through to report error. */
7615 default:
7616 c_parser_error (parser, "expected expression");
7617 expr.value = error_mark_node;
7618 break;
7620 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7623 /* Parse a postfix expression after a parenthesized type name: the
7624 brace-enclosed initializer of a compound literal, possibly followed
7625 by some postfix operators. This is separate because it is not
7626 possible to tell until after the type name whether a cast
7627 expression has a cast or a compound literal, or whether the operand
7628 of sizeof is a parenthesized type name or starts with a compound
7629 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7630 location of the first token after the parentheses around the type
7631 name. */
7633 static struct c_expr
7634 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7635 struct c_type_name *type_name,
7636 location_t type_loc)
7638 tree type;
7639 struct c_expr init;
7640 bool non_const;
7641 struct c_expr expr;
7642 location_t start_loc;
7643 tree type_expr = NULL_TREE;
7644 bool type_expr_const = true;
7645 check_compound_literal_type (type_loc, type_name);
7646 start_init (NULL_TREE, NULL, 0);
7647 type = groktypename (type_name, &type_expr, &type_expr_const);
7648 start_loc = c_parser_peek_token (parser)->location;
7649 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7651 error_at (type_loc, "compound literal has variable size");
7652 type = error_mark_node;
7654 init = c_parser_braced_init (parser, type, false);
7655 finish_init ();
7656 maybe_warn_string_init (type_loc, type, init);
7658 if (type != error_mark_node
7659 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7660 && current_function_decl)
7662 error ("compound literal qualified by address-space qualifier");
7663 type = error_mark_node;
7666 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7667 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7668 ? CONSTRUCTOR_NON_CONST (init.value)
7669 : init.original_code == C_MAYBE_CONST_EXPR);
7670 non_const |= !type_expr_const;
7671 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7672 expr.original_code = ERROR_MARK;
7673 expr.original_type = NULL;
7674 if (type_expr)
7676 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7678 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7679 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7681 else
7683 gcc_assert (!non_const);
7684 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7685 type_expr, expr.value);
7688 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7691 /* Callback function for sizeof_pointer_memaccess_warning to compare
7692 types. */
7694 static bool
7695 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7697 return comptypes (type1, type2) == 1;
7700 /* Parse a postfix expression after the initial primary or compound
7701 literal; that is, parse a series of postfix operators.
7703 EXPR_LOC is the location of the primary expression. */
7705 static struct c_expr
7706 c_parser_postfix_expression_after_primary (c_parser *parser,
7707 location_t expr_loc,
7708 struct c_expr expr)
7710 struct c_expr orig_expr;
7711 tree ident, idx;
7712 location_t sizeof_arg_loc[3];
7713 tree sizeof_arg[3];
7714 unsigned int literal_zero_mask;
7715 unsigned int i;
7716 vec<tree, va_gc> *exprlist;
7717 vec<tree, va_gc> *origtypes = NULL;
7718 vec<location_t> arg_loc = vNULL;
7720 while (true)
7722 location_t op_loc = c_parser_peek_token (parser)->location;
7723 switch (c_parser_peek_token (parser)->type)
7725 case CPP_OPEN_SQUARE:
7726 /* Array reference. */
7727 c_parser_consume_token (parser);
7728 if (flag_cilkplus
7729 && c_parser_peek_token (parser)->type == CPP_COLON)
7730 /* If we are here, then we have something like this:
7731 Array [ : ]
7733 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7734 expr.value);
7735 else
7737 idx = c_parser_expression (parser).value;
7738 /* Here we have 3 options:
7739 1. Array [EXPR] -- Normal Array call.
7740 2. Array [EXPR : EXPR] -- Array notation without stride.
7741 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7743 For 1, we just handle it just like a normal array expression.
7744 For 2 and 3 we handle it like we handle array notations. The
7745 idx value we have above becomes the initial/start index.
7747 if (flag_cilkplus
7748 && c_parser_peek_token (parser)->type == CPP_COLON)
7749 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7750 expr.value);
7751 else
7753 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7754 "expected %<]%>");
7755 expr.value = build_array_ref (op_loc, expr.value, idx);
7758 expr.original_code = ERROR_MARK;
7759 expr.original_type = NULL;
7760 break;
7761 case CPP_OPEN_PAREN:
7762 /* Function call. */
7763 c_parser_consume_token (parser);
7764 for (i = 0; i < 3; i++)
7766 sizeof_arg[i] = NULL_TREE;
7767 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7769 literal_zero_mask = 0;
7770 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7771 exprlist = NULL;
7772 else
7773 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7774 sizeof_arg_loc, sizeof_arg,
7775 &arg_loc, &literal_zero_mask);
7776 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7777 "expected %<)%>");
7778 orig_expr = expr;
7779 mark_exp_read (expr.value);
7780 if (warn_sizeof_pointer_memaccess)
7781 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7782 expr.value, exprlist,
7783 sizeof_arg,
7784 sizeof_ptr_memacc_comptypes);
7785 if (warn_memset_transposed_args
7786 && TREE_CODE (expr.value) == FUNCTION_DECL
7787 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7788 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7789 && vec_safe_length (exprlist) == 3
7790 && integer_zerop ((*exprlist)[2])
7791 && (literal_zero_mask & (1 << 2)) != 0
7792 && (!integer_zerop ((*exprlist)[1])
7793 || (literal_zero_mask & (1 << 1)) == 0))
7794 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7795 "%<memset%> used with constant zero length parameter; "
7796 "this could be due to transposed parameters");
7798 expr.value
7799 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7800 exprlist, origtypes);
7801 expr.original_code = ERROR_MARK;
7802 if (TREE_CODE (expr.value) == INTEGER_CST
7803 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7804 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7805 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7806 expr.original_code = C_MAYBE_CONST_EXPR;
7807 expr.original_type = NULL;
7808 if (exprlist)
7810 release_tree_vector (exprlist);
7811 release_tree_vector (origtypes);
7813 arg_loc.release ();
7814 break;
7815 case CPP_DOT:
7816 /* Structure element reference. */
7817 c_parser_consume_token (parser);
7818 expr = default_function_array_conversion (expr_loc, expr);
7819 if (c_parser_next_token_is (parser, CPP_NAME))
7820 ident = c_parser_peek_token (parser)->value;
7821 else
7823 c_parser_error (parser, "expected identifier");
7824 expr.value = error_mark_node;
7825 expr.original_code = ERROR_MARK;
7826 expr.original_type = NULL;
7827 return expr;
7829 c_parser_consume_token (parser);
7830 expr.value = build_component_ref (op_loc, expr.value, ident);
7831 expr.original_code = ERROR_MARK;
7832 if (TREE_CODE (expr.value) != COMPONENT_REF)
7833 expr.original_type = NULL;
7834 else
7836 /* Remember the original type of a bitfield. */
7837 tree field = TREE_OPERAND (expr.value, 1);
7838 if (TREE_CODE (field) != FIELD_DECL)
7839 expr.original_type = NULL;
7840 else
7841 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7843 break;
7844 case CPP_DEREF:
7845 /* Structure element reference. */
7846 c_parser_consume_token (parser);
7847 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7848 if (c_parser_next_token_is (parser, CPP_NAME))
7849 ident = c_parser_peek_token (parser)->value;
7850 else
7852 c_parser_error (parser, "expected identifier");
7853 expr.value = error_mark_node;
7854 expr.original_code = ERROR_MARK;
7855 expr.original_type = NULL;
7856 return expr;
7858 c_parser_consume_token (parser);
7859 expr.value = build_component_ref (op_loc,
7860 build_indirect_ref (op_loc,
7861 expr.value,
7862 RO_ARROW),
7863 ident);
7864 expr.original_code = ERROR_MARK;
7865 if (TREE_CODE (expr.value) != COMPONENT_REF)
7866 expr.original_type = NULL;
7867 else
7869 /* Remember the original type of a bitfield. */
7870 tree field = TREE_OPERAND (expr.value, 1);
7871 if (TREE_CODE (field) != FIELD_DECL)
7872 expr.original_type = NULL;
7873 else
7874 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7876 break;
7877 case CPP_PLUS_PLUS:
7878 /* Postincrement. */
7879 c_parser_consume_token (parser);
7880 /* If the expressions have array notations, we expand them. */
7881 if (flag_cilkplus
7882 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7883 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7884 else
7886 expr = default_function_array_read_conversion (expr_loc, expr);
7887 expr.value = build_unary_op (op_loc,
7888 POSTINCREMENT_EXPR, expr.value, 0);
7890 expr.original_code = ERROR_MARK;
7891 expr.original_type = NULL;
7892 break;
7893 case CPP_MINUS_MINUS:
7894 /* Postdecrement. */
7895 c_parser_consume_token (parser);
7896 /* If the expressions have array notations, we expand them. */
7897 if (flag_cilkplus
7898 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7899 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7900 else
7902 expr = default_function_array_read_conversion (expr_loc, expr);
7903 expr.value = build_unary_op (op_loc,
7904 POSTDECREMENT_EXPR, expr.value, 0);
7906 expr.original_code = ERROR_MARK;
7907 expr.original_type = NULL;
7908 break;
7909 default:
7910 return expr;
7915 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7917 expression:
7918 assignment-expression
7919 expression , assignment-expression
7922 static struct c_expr
7923 c_parser_expression (c_parser *parser)
7925 location_t tloc = c_parser_peek_token (parser)->location;
7926 struct c_expr expr;
7927 expr = c_parser_expr_no_commas (parser, NULL);
7928 if (c_parser_next_token_is (parser, CPP_COMMA))
7929 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7930 while (c_parser_next_token_is (parser, CPP_COMMA))
7932 struct c_expr next;
7933 tree lhsval;
7934 location_t loc = c_parser_peek_token (parser)->location;
7935 location_t expr_loc;
7936 c_parser_consume_token (parser);
7937 expr_loc = c_parser_peek_token (parser)->location;
7938 lhsval = expr.value;
7939 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7940 lhsval = TREE_OPERAND (lhsval, 1);
7941 if (DECL_P (lhsval) || handled_component_p (lhsval))
7942 mark_exp_read (lhsval);
7943 next = c_parser_expr_no_commas (parser, NULL);
7944 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7945 expr.value = build_compound_expr (loc, expr.value, next.value);
7946 expr.original_code = COMPOUND_EXPR;
7947 expr.original_type = next.original_type;
7949 return expr;
7952 /* Parse an expression and convert functions or arrays to pointers and
7953 lvalues to rvalues. */
7955 static struct c_expr
7956 c_parser_expression_conv (c_parser *parser)
7958 struct c_expr expr;
7959 location_t loc = c_parser_peek_token (parser)->location;
7960 expr = c_parser_expression (parser);
7961 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7962 return expr;
7965 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
7966 argument is a literal zero alone and if so, set it in literal_zero_mask. */
7968 static inline void
7969 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
7970 unsigned int idx)
7972 if (idx >= HOST_BITS_PER_INT)
7973 return;
7975 c_token *tok = c_parser_peek_token (parser);
7976 switch (tok->type)
7978 case CPP_NUMBER:
7979 case CPP_CHAR:
7980 case CPP_WCHAR:
7981 case CPP_CHAR16:
7982 case CPP_CHAR32:
7983 /* If a parameter is literal zero alone, remember it
7984 for -Wmemset-transposed-args warning. */
7985 if (integer_zerop (tok->value)
7986 && !TREE_OVERFLOW (tok->value)
7987 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7988 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
7989 *literal_zero_mask |= 1U << idx;
7990 default:
7991 break;
7995 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7996 functions and arrays to pointers and lvalues to rvalues. If
7997 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
7998 locations of function arguments into this vector.
8000 nonempty-expr-list:
8001 assignment-expression
8002 nonempty-expr-list , assignment-expression
8005 static vec<tree, va_gc> *
8006 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8007 vec<tree, va_gc> **p_orig_types,
8008 location_t *sizeof_arg_loc, tree *sizeof_arg,
8009 vec<location_t> *locations,
8010 unsigned int *literal_zero_mask)
8012 vec<tree, va_gc> *ret;
8013 vec<tree, va_gc> *orig_types;
8014 struct c_expr expr;
8015 location_t loc = c_parser_peek_token (parser)->location;
8016 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8017 unsigned int idx = 0;
8019 ret = make_tree_vector ();
8020 if (p_orig_types == NULL)
8021 orig_types = NULL;
8022 else
8023 orig_types = make_tree_vector ();
8025 if (sizeof_arg != NULL
8026 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8027 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8028 if (literal_zero_mask)
8029 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8030 expr = c_parser_expr_no_commas (parser, NULL);
8031 if (convert_p)
8032 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8033 if (fold_p)
8034 expr.value = c_fully_fold (expr.value, false, NULL);
8035 ret->quick_push (expr.value);
8036 if (orig_types)
8037 orig_types->quick_push (expr.original_type);
8038 if (locations)
8039 locations->safe_push (loc);
8040 if (sizeof_arg != NULL
8041 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8042 && expr.original_code == SIZEOF_EXPR)
8044 sizeof_arg[0] = c_last_sizeof_arg;
8045 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8047 while (c_parser_next_token_is (parser, CPP_COMMA))
8049 c_parser_consume_token (parser);
8050 loc = c_parser_peek_token (parser)->location;
8051 if (sizeof_arg != NULL
8052 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8053 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8054 else
8055 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8056 if (literal_zero_mask)
8057 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8058 expr = c_parser_expr_no_commas (parser, NULL);
8059 if (convert_p)
8060 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8061 if (fold_p)
8062 expr.value = c_fully_fold (expr.value, false, NULL);
8063 vec_safe_push (ret, expr.value);
8064 if (orig_types)
8065 vec_safe_push (orig_types, expr.original_type);
8066 if (locations)
8067 locations->safe_push (loc);
8068 if (++idx < 3
8069 && sizeof_arg != NULL
8070 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8071 && expr.original_code == SIZEOF_EXPR)
8073 sizeof_arg[idx] = c_last_sizeof_arg;
8074 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8077 if (orig_types)
8078 *p_orig_types = orig_types;
8079 return ret;
8082 /* Parse Objective-C-specific constructs. */
8084 /* Parse an objc-class-definition.
8086 objc-class-definition:
8087 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8088 objc-class-instance-variables[opt] objc-methodprotolist @end
8089 @implementation identifier objc-superclass[opt]
8090 objc-class-instance-variables[opt]
8091 @interface identifier ( identifier ) objc-protocol-refs[opt]
8092 objc-methodprotolist @end
8093 @interface identifier ( ) objc-protocol-refs[opt]
8094 objc-methodprotolist @end
8095 @implementation identifier ( identifier )
8097 objc-superclass:
8098 : identifier
8100 "@interface identifier (" must start "@interface identifier (
8101 identifier ) ...": objc-methodprotolist in the first production may
8102 not start with a parenthesized identifier as a declarator of a data
8103 definition with no declaration specifiers if the objc-superclass,
8104 objc-protocol-refs and objc-class-instance-variables are omitted. */
8106 static void
8107 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8109 bool iface_p;
8110 tree id1;
8111 tree superclass;
8112 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8113 iface_p = true;
8114 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8115 iface_p = false;
8116 else
8117 gcc_unreachable ();
8119 c_parser_consume_token (parser);
8120 if (c_parser_next_token_is_not (parser, CPP_NAME))
8122 c_parser_error (parser, "expected identifier");
8123 return;
8125 id1 = c_parser_peek_token (parser)->value;
8126 c_parser_consume_token (parser);
8127 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8129 /* We have a category or class extension. */
8130 tree id2;
8131 tree proto = NULL_TREE;
8132 c_parser_consume_token (parser);
8133 if (c_parser_next_token_is_not (parser, CPP_NAME))
8135 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8137 /* We have a class extension. */
8138 id2 = NULL_TREE;
8140 else
8142 c_parser_error (parser, "expected identifier or %<)%>");
8143 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8144 return;
8147 else
8149 id2 = c_parser_peek_token (parser)->value;
8150 c_parser_consume_token (parser);
8152 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8153 if (!iface_p)
8155 objc_start_category_implementation (id1, id2);
8156 return;
8158 if (c_parser_next_token_is (parser, CPP_LESS))
8159 proto = c_parser_objc_protocol_refs (parser);
8160 objc_start_category_interface (id1, id2, proto, attributes);
8161 c_parser_objc_methodprotolist (parser);
8162 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8163 objc_finish_interface ();
8164 return;
8166 if (c_parser_next_token_is (parser, CPP_COLON))
8168 c_parser_consume_token (parser);
8169 if (c_parser_next_token_is_not (parser, CPP_NAME))
8171 c_parser_error (parser, "expected identifier");
8172 return;
8174 superclass = c_parser_peek_token (parser)->value;
8175 c_parser_consume_token (parser);
8177 else
8178 superclass = NULL_TREE;
8179 if (iface_p)
8181 tree proto = NULL_TREE;
8182 if (c_parser_next_token_is (parser, CPP_LESS))
8183 proto = c_parser_objc_protocol_refs (parser);
8184 objc_start_class_interface (id1, superclass, proto, attributes);
8186 else
8187 objc_start_class_implementation (id1, superclass);
8188 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8189 c_parser_objc_class_instance_variables (parser);
8190 if (iface_p)
8192 objc_continue_interface ();
8193 c_parser_objc_methodprotolist (parser);
8194 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8195 objc_finish_interface ();
8197 else
8199 objc_continue_implementation ();
8200 return;
8204 /* Parse objc-class-instance-variables.
8206 objc-class-instance-variables:
8207 { objc-instance-variable-decl-list[opt] }
8209 objc-instance-variable-decl-list:
8210 objc-visibility-spec
8211 objc-instance-variable-decl ;
8213 objc-instance-variable-decl-list objc-visibility-spec
8214 objc-instance-variable-decl-list objc-instance-variable-decl ;
8215 objc-instance-variable-decl-list ;
8217 objc-visibility-spec:
8218 @private
8219 @protected
8220 @public
8222 objc-instance-variable-decl:
8223 struct-declaration
8226 static void
8227 c_parser_objc_class_instance_variables (c_parser *parser)
8229 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8230 c_parser_consume_token (parser);
8231 while (c_parser_next_token_is_not (parser, CPP_EOF))
8233 tree decls;
8234 /* Parse any stray semicolon. */
8235 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8237 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8238 "extra semicolon");
8239 c_parser_consume_token (parser);
8240 continue;
8242 /* Stop if at the end of the instance variables. */
8243 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8245 c_parser_consume_token (parser);
8246 break;
8248 /* Parse any objc-visibility-spec. */
8249 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8251 c_parser_consume_token (parser);
8252 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8253 continue;
8255 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8257 c_parser_consume_token (parser);
8258 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8259 continue;
8261 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8263 c_parser_consume_token (parser);
8264 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8265 continue;
8267 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8269 c_parser_consume_token (parser);
8270 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8271 continue;
8273 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8275 c_parser_pragma (parser, pragma_external);
8276 continue;
8279 /* Parse some comma-separated declarations. */
8280 decls = c_parser_struct_declaration (parser);
8281 if (decls == NULL)
8283 /* There is a syntax error. We want to skip the offending
8284 tokens up to the next ';' (included) or '}'
8285 (excluded). */
8287 /* First, skip manually a ')' or ']'. This is because they
8288 reduce the nesting level, so c_parser_skip_until_found()
8289 wouldn't be able to skip past them. */
8290 c_token *token = c_parser_peek_token (parser);
8291 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8292 c_parser_consume_token (parser);
8294 /* Then, do the standard skipping. */
8295 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8297 /* We hopefully recovered. Start normal parsing again. */
8298 parser->error = false;
8299 continue;
8301 else
8303 /* Comma-separated instance variables are chained together
8304 in reverse order; add them one by one. */
8305 tree ivar = nreverse (decls);
8306 for (; ivar; ivar = DECL_CHAIN (ivar))
8307 objc_add_instance_variable (copy_node (ivar));
8309 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8313 /* Parse an objc-class-declaration.
8315 objc-class-declaration:
8316 @class identifier-list ;
8319 static void
8320 c_parser_objc_class_declaration (c_parser *parser)
8322 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8323 c_parser_consume_token (parser);
8324 /* Any identifiers, including those declared as type names, are OK
8325 here. */
8326 while (true)
8328 tree id;
8329 if (c_parser_next_token_is_not (parser, CPP_NAME))
8331 c_parser_error (parser, "expected identifier");
8332 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8333 parser->error = false;
8334 return;
8336 id = c_parser_peek_token (parser)->value;
8337 objc_declare_class (id);
8338 c_parser_consume_token (parser);
8339 if (c_parser_next_token_is (parser, CPP_COMMA))
8340 c_parser_consume_token (parser);
8341 else
8342 break;
8344 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8347 /* Parse an objc-alias-declaration.
8349 objc-alias-declaration:
8350 @compatibility_alias identifier identifier ;
8353 static void
8354 c_parser_objc_alias_declaration (c_parser *parser)
8356 tree id1, id2;
8357 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8358 c_parser_consume_token (parser);
8359 if (c_parser_next_token_is_not (parser, CPP_NAME))
8361 c_parser_error (parser, "expected identifier");
8362 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8363 return;
8365 id1 = c_parser_peek_token (parser)->value;
8366 c_parser_consume_token (parser);
8367 if (c_parser_next_token_is_not (parser, CPP_NAME))
8369 c_parser_error (parser, "expected identifier");
8370 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8371 return;
8373 id2 = c_parser_peek_token (parser)->value;
8374 c_parser_consume_token (parser);
8375 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8376 objc_declare_alias (id1, id2);
8379 /* Parse an objc-protocol-definition.
8381 objc-protocol-definition:
8382 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8383 @protocol identifier-list ;
8385 "@protocol identifier ;" should be resolved as "@protocol
8386 identifier-list ;": objc-methodprotolist may not start with a
8387 semicolon in the first alternative if objc-protocol-refs are
8388 omitted. */
8390 static void
8391 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8393 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8395 c_parser_consume_token (parser);
8396 if (c_parser_next_token_is_not (parser, CPP_NAME))
8398 c_parser_error (parser, "expected identifier");
8399 return;
8401 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8402 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8404 /* Any identifiers, including those declared as type names, are
8405 OK here. */
8406 while (true)
8408 tree id;
8409 if (c_parser_next_token_is_not (parser, CPP_NAME))
8411 c_parser_error (parser, "expected identifier");
8412 break;
8414 id = c_parser_peek_token (parser)->value;
8415 objc_declare_protocol (id, attributes);
8416 c_parser_consume_token (parser);
8417 if (c_parser_next_token_is (parser, CPP_COMMA))
8418 c_parser_consume_token (parser);
8419 else
8420 break;
8422 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8424 else
8426 tree id = c_parser_peek_token (parser)->value;
8427 tree proto = NULL_TREE;
8428 c_parser_consume_token (parser);
8429 if (c_parser_next_token_is (parser, CPP_LESS))
8430 proto = c_parser_objc_protocol_refs (parser);
8431 parser->objc_pq_context = true;
8432 objc_start_protocol (id, proto, attributes);
8433 c_parser_objc_methodprotolist (parser);
8434 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8435 parser->objc_pq_context = false;
8436 objc_finish_interface ();
8440 /* Parse an objc-method-type.
8442 objc-method-type:
8446 Return true if it is a class method (+) and false if it is
8447 an instance method (-).
8449 static inline bool
8450 c_parser_objc_method_type (c_parser *parser)
8452 switch (c_parser_peek_token (parser)->type)
8454 case CPP_PLUS:
8455 c_parser_consume_token (parser);
8456 return true;
8457 case CPP_MINUS:
8458 c_parser_consume_token (parser);
8459 return false;
8460 default:
8461 gcc_unreachable ();
8465 /* Parse an objc-method-definition.
8467 objc-method-definition:
8468 objc-method-type objc-method-decl ;[opt] compound-statement
8471 static void
8472 c_parser_objc_method_definition (c_parser *parser)
8474 bool is_class_method = c_parser_objc_method_type (parser);
8475 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8476 parser->objc_pq_context = true;
8477 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8478 &expr);
8479 if (decl == error_mark_node)
8480 return; /* Bail here. */
8482 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8484 c_parser_consume_token (parser);
8485 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8486 "extra semicolon in method definition specified");
8489 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8491 c_parser_error (parser, "expected %<{%>");
8492 return;
8495 parser->objc_pq_context = false;
8496 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8498 add_stmt (c_parser_compound_statement (parser));
8499 objc_finish_method_definition (current_function_decl);
8501 else
8503 /* This code is executed when we find a method definition
8504 outside of an @implementation context (or invalid for other
8505 reasons). Parse the method (to keep going) but do not emit
8506 any code.
8508 c_parser_compound_statement (parser);
8512 /* Parse an objc-methodprotolist.
8514 objc-methodprotolist:
8515 empty
8516 objc-methodprotolist objc-methodproto
8517 objc-methodprotolist declaration
8518 objc-methodprotolist ;
8519 @optional
8520 @required
8522 The declaration is a data definition, which may be missing
8523 declaration specifiers under the same rules and diagnostics as
8524 other data definitions outside functions, and the stray semicolon
8525 is diagnosed the same way as a stray semicolon outside a
8526 function. */
8528 static void
8529 c_parser_objc_methodprotolist (c_parser *parser)
8531 while (true)
8533 /* The list is terminated by @end. */
8534 switch (c_parser_peek_token (parser)->type)
8536 case CPP_SEMICOLON:
8537 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8538 "ISO C does not allow extra %<;%> outside of a function");
8539 c_parser_consume_token (parser);
8540 break;
8541 case CPP_PLUS:
8542 case CPP_MINUS:
8543 c_parser_objc_methodproto (parser);
8544 break;
8545 case CPP_PRAGMA:
8546 c_parser_pragma (parser, pragma_external);
8547 break;
8548 case CPP_EOF:
8549 return;
8550 default:
8551 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8552 return;
8553 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8554 c_parser_objc_at_property_declaration (parser);
8555 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8557 objc_set_method_opt (true);
8558 c_parser_consume_token (parser);
8560 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8562 objc_set_method_opt (false);
8563 c_parser_consume_token (parser);
8565 else
8566 c_parser_declaration_or_fndef (parser, false, false, true,
8567 false, true, NULL, vNULL);
8568 break;
8573 /* Parse an objc-methodproto.
8575 objc-methodproto:
8576 objc-method-type objc-method-decl ;
8579 static void
8580 c_parser_objc_methodproto (c_parser *parser)
8582 bool is_class_method = c_parser_objc_method_type (parser);
8583 tree decl, attributes = NULL_TREE;
8585 /* Remember protocol qualifiers in prototypes. */
8586 parser->objc_pq_context = true;
8587 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8588 NULL);
8589 /* Forget protocol qualifiers now. */
8590 parser->objc_pq_context = false;
8592 /* Do not allow the presence of attributes to hide an erroneous
8593 method implementation in the interface section. */
8594 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8596 c_parser_error (parser, "expected %<;%>");
8597 return;
8600 if (decl != error_mark_node)
8601 objc_add_method_declaration (is_class_method, decl, attributes);
8603 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8606 /* If we are at a position that method attributes may be present, check that
8607 there are not any parsed already (a syntax error) and then collect any
8608 specified at the current location. Finally, if new attributes were present,
8609 check that the next token is legal ( ';' for decls and '{' for defs). */
8611 static bool
8612 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8614 bool bad = false;
8615 if (*attributes)
8617 c_parser_error (parser,
8618 "method attributes must be specified at the end only");
8619 *attributes = NULL_TREE;
8620 bad = true;
8623 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8624 *attributes = c_parser_attributes (parser);
8626 /* If there were no attributes here, just report any earlier error. */
8627 if (*attributes == NULL_TREE || bad)
8628 return bad;
8630 /* If the attributes are followed by a ; or {, then just report any earlier
8631 error. */
8632 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8633 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8634 return bad;
8636 /* We've got attributes, but not at the end. */
8637 c_parser_error (parser,
8638 "expected %<;%> or %<{%> after method attribute definition");
8639 return true;
8642 /* Parse an objc-method-decl.
8644 objc-method-decl:
8645 ( objc-type-name ) objc-selector
8646 objc-selector
8647 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8648 objc-keyword-selector objc-optparmlist
8649 attributes
8651 objc-keyword-selector:
8652 objc-keyword-decl
8653 objc-keyword-selector objc-keyword-decl
8655 objc-keyword-decl:
8656 objc-selector : ( objc-type-name ) identifier
8657 objc-selector : identifier
8658 : ( objc-type-name ) identifier
8659 : identifier
8661 objc-optparmlist:
8662 objc-optparms objc-optellipsis
8664 objc-optparms:
8665 empty
8666 objc-opt-parms , parameter-declaration
8668 objc-optellipsis:
8669 empty
8670 , ...
8673 static tree
8674 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8675 tree *attributes, tree *expr)
8677 tree type = NULL_TREE;
8678 tree sel;
8679 tree parms = NULL_TREE;
8680 bool ellipsis = false;
8681 bool attr_err = false;
8683 *attributes = NULL_TREE;
8684 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8686 c_parser_consume_token (parser);
8687 type = c_parser_objc_type_name (parser);
8688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8690 sel = c_parser_objc_selector (parser);
8691 /* If there is no selector, or a colon follows, we have an
8692 objc-keyword-selector. If there is a selector, and a colon does
8693 not follow, that selector ends the objc-method-decl. */
8694 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8696 tree tsel = sel;
8697 tree list = NULL_TREE;
8698 while (true)
8700 tree atype = NULL_TREE, id, keyworddecl;
8701 tree param_attr = NULL_TREE;
8702 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8703 break;
8704 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8706 c_parser_consume_token (parser);
8707 atype = c_parser_objc_type_name (parser);
8708 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8709 "expected %<)%>");
8711 /* New ObjC allows attributes on method parameters. */
8712 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8713 param_attr = c_parser_attributes (parser);
8714 if (c_parser_next_token_is_not (parser, CPP_NAME))
8716 c_parser_error (parser, "expected identifier");
8717 return error_mark_node;
8719 id = c_parser_peek_token (parser)->value;
8720 c_parser_consume_token (parser);
8721 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8722 list = chainon (list, keyworddecl);
8723 tsel = c_parser_objc_selector (parser);
8724 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8725 break;
8728 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8730 /* Parse the optional parameter list. Optional Objective-C
8731 method parameters follow the C syntax, and may include '...'
8732 to denote a variable number of arguments. */
8733 parms = make_node (TREE_LIST);
8734 while (c_parser_next_token_is (parser, CPP_COMMA))
8736 struct c_parm *parm;
8737 c_parser_consume_token (parser);
8738 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8740 ellipsis = true;
8741 c_parser_consume_token (parser);
8742 attr_err |= c_parser_objc_maybe_method_attributes
8743 (parser, attributes) ;
8744 break;
8746 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8747 if (parm == NULL)
8748 break;
8749 parms = chainon (parms,
8750 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8752 sel = list;
8754 else
8755 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8757 if (sel == NULL)
8759 c_parser_error (parser, "objective-c method declaration is expected");
8760 return error_mark_node;
8763 if (attr_err)
8764 return error_mark_node;
8766 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8769 /* Parse an objc-type-name.
8771 objc-type-name:
8772 objc-type-qualifiers[opt] type-name
8773 objc-type-qualifiers[opt]
8775 objc-type-qualifiers:
8776 objc-type-qualifier
8777 objc-type-qualifiers objc-type-qualifier
8779 objc-type-qualifier: one of
8780 in out inout bycopy byref oneway
8783 static tree
8784 c_parser_objc_type_name (c_parser *parser)
8786 tree quals = NULL_TREE;
8787 struct c_type_name *type_name = NULL;
8788 tree type = NULL_TREE;
8789 while (true)
8791 c_token *token = c_parser_peek_token (parser);
8792 if (token->type == CPP_KEYWORD
8793 && (token->keyword == RID_IN
8794 || token->keyword == RID_OUT
8795 || token->keyword == RID_INOUT
8796 || token->keyword == RID_BYCOPY
8797 || token->keyword == RID_BYREF
8798 || token->keyword == RID_ONEWAY))
8800 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8801 c_parser_consume_token (parser);
8803 else
8804 break;
8806 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8807 type_name = c_parser_type_name (parser);
8808 if (type_name)
8809 type = groktypename (type_name, NULL, NULL);
8811 /* If the type is unknown, and error has already been produced and
8812 we need to recover from the error. In that case, use NULL_TREE
8813 for the type, as if no type had been specified; this will use the
8814 default type ('id') which is good for error recovery. */
8815 if (type == error_mark_node)
8816 type = NULL_TREE;
8818 return build_tree_list (quals, type);
8821 /* Parse objc-protocol-refs.
8823 objc-protocol-refs:
8824 < identifier-list >
8827 static tree
8828 c_parser_objc_protocol_refs (c_parser *parser)
8830 tree list = NULL_TREE;
8831 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8832 c_parser_consume_token (parser);
8833 /* Any identifiers, including those declared as type names, are OK
8834 here. */
8835 while (true)
8837 tree id;
8838 if (c_parser_next_token_is_not (parser, CPP_NAME))
8840 c_parser_error (parser, "expected identifier");
8841 break;
8843 id = c_parser_peek_token (parser)->value;
8844 list = chainon (list, build_tree_list (NULL_TREE, id));
8845 c_parser_consume_token (parser);
8846 if (c_parser_next_token_is (parser, CPP_COMMA))
8847 c_parser_consume_token (parser);
8848 else
8849 break;
8851 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8852 return list;
8855 /* Parse an objc-try-catch-finally-statement.
8857 objc-try-catch-finally-statement:
8858 @try compound-statement objc-catch-list[opt]
8859 @try compound-statement objc-catch-list[opt] @finally compound-statement
8861 objc-catch-list:
8862 @catch ( objc-catch-parameter-declaration ) compound-statement
8863 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8865 objc-catch-parameter-declaration:
8866 parameter-declaration
8867 '...'
8869 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8871 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8872 for C++. Keep them in sync. */
8874 static void
8875 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8877 location_t location;
8878 tree stmt;
8880 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8881 c_parser_consume_token (parser);
8882 location = c_parser_peek_token (parser)->location;
8883 objc_maybe_warn_exceptions (location);
8884 stmt = c_parser_compound_statement (parser);
8885 objc_begin_try_stmt (location, stmt);
8887 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8889 struct c_parm *parm;
8890 tree parameter_declaration = error_mark_node;
8891 bool seen_open_paren = false;
8893 c_parser_consume_token (parser);
8894 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8895 seen_open_paren = true;
8896 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8898 /* We have "@catch (...)" (where the '...' are literally
8899 what is in the code). Skip the '...'.
8900 parameter_declaration is set to NULL_TREE, and
8901 objc_being_catch_clauses() knows that that means
8902 '...'. */
8903 c_parser_consume_token (parser);
8904 parameter_declaration = NULL_TREE;
8906 else
8908 /* We have "@catch (NSException *exception)" or something
8909 like that. Parse the parameter declaration. */
8910 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8911 if (parm == NULL)
8912 parameter_declaration = error_mark_node;
8913 else
8914 parameter_declaration = grokparm (parm, NULL);
8916 if (seen_open_paren)
8917 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8918 else
8920 /* If there was no open parenthesis, we are recovering from
8921 an error, and we are trying to figure out what mistake
8922 the user has made. */
8924 /* If there is an immediate closing parenthesis, the user
8925 probably forgot the opening one (ie, they typed "@catch
8926 NSException *e)". Parse the closing parenthesis and keep
8927 going. */
8928 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8929 c_parser_consume_token (parser);
8931 /* If these is no immediate closing parenthesis, the user
8932 probably doesn't know that parenthesis are required at
8933 all (ie, they typed "@catch NSException *e"). So, just
8934 forget about the closing parenthesis and keep going. */
8936 objc_begin_catch_clause (parameter_declaration);
8937 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8938 c_parser_compound_statement_nostart (parser);
8939 objc_finish_catch_clause ();
8941 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8943 c_parser_consume_token (parser);
8944 location = c_parser_peek_token (parser)->location;
8945 stmt = c_parser_compound_statement (parser);
8946 objc_build_finally_clause (location, stmt);
8948 objc_finish_try_stmt ();
8951 /* Parse an objc-synchronized-statement.
8953 objc-synchronized-statement:
8954 @synchronized ( expression ) compound-statement
8957 static void
8958 c_parser_objc_synchronized_statement (c_parser *parser)
8960 location_t loc;
8961 tree expr, stmt;
8962 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8963 c_parser_consume_token (parser);
8964 loc = c_parser_peek_token (parser)->location;
8965 objc_maybe_warn_exceptions (loc);
8966 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8968 struct c_expr ce = c_parser_expression (parser);
8969 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8970 expr = ce.value;
8971 expr = c_fully_fold (expr, false, NULL);
8972 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8974 else
8975 expr = error_mark_node;
8976 stmt = c_parser_compound_statement (parser);
8977 objc_build_synchronized (loc, expr, stmt);
8980 /* Parse an objc-selector; return NULL_TREE without an error if the
8981 next token is not an objc-selector.
8983 objc-selector:
8984 identifier
8985 one of
8986 enum struct union if else while do for switch case default
8987 break continue return goto asm sizeof typeof __alignof
8988 unsigned long const short volatile signed restrict _Complex
8989 in out inout bycopy byref oneway int char float double void _Bool
8990 _Atomic
8992 ??? Why this selection of keywords but not, for example, storage
8993 class specifiers? */
8995 static tree
8996 c_parser_objc_selector (c_parser *parser)
8998 c_token *token = c_parser_peek_token (parser);
8999 tree value = token->value;
9000 if (token->type == CPP_NAME)
9002 c_parser_consume_token (parser);
9003 return value;
9005 if (token->type != CPP_KEYWORD)
9006 return NULL_TREE;
9007 switch (token->keyword)
9009 case RID_ENUM:
9010 case RID_STRUCT:
9011 case RID_UNION:
9012 case RID_IF:
9013 case RID_ELSE:
9014 case RID_WHILE:
9015 case RID_DO:
9016 case RID_FOR:
9017 case RID_SWITCH:
9018 case RID_CASE:
9019 case RID_DEFAULT:
9020 case RID_BREAK:
9021 case RID_CONTINUE:
9022 case RID_RETURN:
9023 case RID_GOTO:
9024 case RID_ASM:
9025 case RID_SIZEOF:
9026 case RID_TYPEOF:
9027 case RID_ALIGNOF:
9028 case RID_UNSIGNED:
9029 case RID_LONG:
9030 case RID_INT128:
9031 case RID_CONST:
9032 case RID_SHORT:
9033 case RID_VOLATILE:
9034 case RID_SIGNED:
9035 case RID_RESTRICT:
9036 case RID_COMPLEX:
9037 case RID_IN:
9038 case RID_OUT:
9039 case RID_INOUT:
9040 case RID_BYCOPY:
9041 case RID_BYREF:
9042 case RID_ONEWAY:
9043 case RID_INT:
9044 case RID_CHAR:
9045 case RID_FLOAT:
9046 case RID_DOUBLE:
9047 case RID_VOID:
9048 case RID_BOOL:
9049 case RID_ATOMIC:
9050 case RID_AUTO_TYPE:
9051 c_parser_consume_token (parser);
9052 return value;
9053 default:
9054 return NULL_TREE;
9058 /* Parse an objc-selector-arg.
9060 objc-selector-arg:
9061 objc-selector
9062 objc-keywordname-list
9064 objc-keywordname-list:
9065 objc-keywordname
9066 objc-keywordname-list objc-keywordname
9068 objc-keywordname:
9069 objc-selector :
9073 static tree
9074 c_parser_objc_selector_arg (c_parser *parser)
9076 tree sel = c_parser_objc_selector (parser);
9077 tree list = NULL_TREE;
9078 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9079 return sel;
9080 while (true)
9082 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9083 return list;
9084 list = chainon (list, build_tree_list (sel, NULL_TREE));
9085 sel = c_parser_objc_selector (parser);
9086 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9087 break;
9089 return list;
9092 /* Parse an objc-receiver.
9094 objc-receiver:
9095 expression
9096 class-name
9097 type-name
9100 static tree
9101 c_parser_objc_receiver (c_parser *parser)
9103 location_t loc = c_parser_peek_token (parser)->location;
9105 if (c_parser_peek_token (parser)->type == CPP_NAME
9106 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9107 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9109 tree id = c_parser_peek_token (parser)->value;
9110 c_parser_consume_token (parser);
9111 return objc_get_class_reference (id);
9113 struct c_expr ce = c_parser_expression (parser);
9114 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9115 return c_fully_fold (ce.value, false, NULL);
9118 /* Parse objc-message-args.
9120 objc-message-args:
9121 objc-selector
9122 objc-keywordarg-list
9124 objc-keywordarg-list:
9125 objc-keywordarg
9126 objc-keywordarg-list objc-keywordarg
9128 objc-keywordarg:
9129 objc-selector : objc-keywordexpr
9130 : objc-keywordexpr
9133 static tree
9134 c_parser_objc_message_args (c_parser *parser)
9136 tree sel = c_parser_objc_selector (parser);
9137 tree list = NULL_TREE;
9138 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9139 return sel;
9140 while (true)
9142 tree keywordexpr;
9143 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9144 return error_mark_node;
9145 keywordexpr = c_parser_objc_keywordexpr (parser);
9146 list = chainon (list, build_tree_list (sel, keywordexpr));
9147 sel = c_parser_objc_selector (parser);
9148 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9149 break;
9151 return list;
9154 /* Parse an objc-keywordexpr.
9156 objc-keywordexpr:
9157 nonempty-expr-list
9160 static tree
9161 c_parser_objc_keywordexpr (c_parser *parser)
9163 tree ret;
9164 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9165 NULL, NULL, NULL, NULL);
9166 if (vec_safe_length (expr_list) == 1)
9168 /* Just return the expression, remove a level of
9169 indirection. */
9170 ret = (*expr_list)[0];
9172 else
9174 /* We have a comma expression, we will collapse later. */
9175 ret = build_tree_list_vec (expr_list);
9177 release_tree_vector (expr_list);
9178 return ret;
9181 /* A check, needed in several places, that ObjC interface, implementation or
9182 method definitions are not prefixed by incorrect items. */
9183 static bool
9184 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9185 struct c_declspecs *specs)
9187 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9188 || specs->typespec_kind != ctsk_none)
9190 c_parser_error (parser,
9191 "no type or storage class may be specified here,");
9192 c_parser_skip_to_end_of_block_or_statement (parser);
9193 return true;
9195 return false;
9198 /* Parse an Objective-C @property declaration. The syntax is:
9200 objc-property-declaration:
9201 '@property' objc-property-attributes[opt] struct-declaration ;
9203 objc-property-attributes:
9204 '(' objc-property-attribute-list ')'
9206 objc-property-attribute-list:
9207 objc-property-attribute
9208 objc-property-attribute-list, objc-property-attribute
9210 objc-property-attribute
9211 'getter' = identifier
9212 'setter' = identifier
9213 'readonly'
9214 'readwrite'
9215 'assign'
9216 'retain'
9217 'copy'
9218 'nonatomic'
9220 For example:
9221 @property NSString *name;
9222 @property (readonly) id object;
9223 @property (retain, nonatomic, getter=getTheName) id name;
9224 @property int a, b, c;
9226 PS: This function is identical to cp_parser_objc_at_propery_declaration
9227 for C++. Keep them in sync. */
9228 static void
9229 c_parser_objc_at_property_declaration (c_parser *parser)
9231 /* The following variables hold the attributes of the properties as
9232 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9233 seen. When we see an attribute, we set them to 'true' (if they
9234 are boolean properties) or to the identifier (if they have an
9235 argument, ie, for getter and setter). Note that here we only
9236 parse the list of attributes, check the syntax and accumulate the
9237 attributes that we find. objc_add_property_declaration() will
9238 then process the information. */
9239 bool property_assign = false;
9240 bool property_copy = false;
9241 tree property_getter_ident = NULL_TREE;
9242 bool property_nonatomic = false;
9243 bool property_readonly = false;
9244 bool property_readwrite = false;
9245 bool property_retain = false;
9246 tree property_setter_ident = NULL_TREE;
9248 /* 'properties' is the list of properties that we read. Usually a
9249 single one, but maybe more (eg, in "@property int a, b, c;" there
9250 are three). */
9251 tree properties;
9252 location_t loc;
9254 loc = c_parser_peek_token (parser)->location;
9255 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9257 c_parser_consume_token (parser); /* Eat '@property'. */
9259 /* Parse the optional attribute list... */
9260 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9262 /* Eat the '(' */
9263 c_parser_consume_token (parser);
9265 /* Property attribute keywords are valid now. */
9266 parser->objc_property_attr_context = true;
9268 while (true)
9270 bool syntax_error = false;
9271 c_token *token = c_parser_peek_token (parser);
9272 enum rid keyword;
9274 if (token->type != CPP_KEYWORD)
9276 if (token->type == CPP_CLOSE_PAREN)
9277 c_parser_error (parser, "expected identifier");
9278 else
9280 c_parser_consume_token (parser);
9281 c_parser_error (parser, "unknown property attribute");
9283 break;
9285 keyword = token->keyword;
9286 c_parser_consume_token (parser);
9287 switch (keyword)
9289 case RID_ASSIGN: property_assign = true; break;
9290 case RID_COPY: property_copy = true; break;
9291 case RID_NONATOMIC: property_nonatomic = true; break;
9292 case RID_READONLY: property_readonly = true; break;
9293 case RID_READWRITE: property_readwrite = true; break;
9294 case RID_RETAIN: property_retain = true; break;
9296 case RID_GETTER:
9297 case RID_SETTER:
9298 if (c_parser_next_token_is_not (parser, CPP_EQ))
9300 if (keyword == RID_GETTER)
9301 c_parser_error (parser,
9302 "missing %<=%> (after %<getter%> attribute)");
9303 else
9304 c_parser_error (parser,
9305 "missing %<=%> (after %<setter%> attribute)");
9306 syntax_error = true;
9307 break;
9309 c_parser_consume_token (parser); /* eat the = */
9310 if (c_parser_next_token_is_not (parser, CPP_NAME))
9312 c_parser_error (parser, "expected identifier");
9313 syntax_error = true;
9314 break;
9316 if (keyword == RID_SETTER)
9318 if (property_setter_ident != NULL_TREE)
9319 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9320 else
9321 property_setter_ident = c_parser_peek_token (parser)->value;
9322 c_parser_consume_token (parser);
9323 if (c_parser_next_token_is_not (parser, CPP_COLON))
9324 c_parser_error (parser, "setter name must terminate with %<:%>");
9325 else
9326 c_parser_consume_token (parser);
9328 else
9330 if (property_getter_ident != NULL_TREE)
9331 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9332 else
9333 property_getter_ident = c_parser_peek_token (parser)->value;
9334 c_parser_consume_token (parser);
9336 break;
9337 default:
9338 c_parser_error (parser, "unknown property attribute");
9339 syntax_error = true;
9340 break;
9343 if (syntax_error)
9344 break;
9346 if (c_parser_next_token_is (parser, CPP_COMMA))
9347 c_parser_consume_token (parser);
9348 else
9349 break;
9351 parser->objc_property_attr_context = false;
9352 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9354 /* ... and the property declaration(s). */
9355 properties = c_parser_struct_declaration (parser);
9357 if (properties == error_mark_node)
9359 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9360 parser->error = false;
9361 return;
9364 if (properties == NULL_TREE)
9365 c_parser_error (parser, "expected identifier");
9366 else
9368 /* Comma-separated properties are chained together in
9369 reverse order; add them one by one. */
9370 properties = nreverse (properties);
9372 for (; properties; properties = TREE_CHAIN (properties))
9373 objc_add_property_declaration (loc, copy_node (properties),
9374 property_readonly, property_readwrite,
9375 property_assign, property_retain,
9376 property_copy, property_nonatomic,
9377 property_getter_ident, property_setter_ident);
9380 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9381 parser->error = false;
9384 /* Parse an Objective-C @synthesize declaration. The syntax is:
9386 objc-synthesize-declaration:
9387 @synthesize objc-synthesize-identifier-list ;
9389 objc-synthesize-identifier-list:
9390 objc-synthesize-identifier
9391 objc-synthesize-identifier-list, objc-synthesize-identifier
9393 objc-synthesize-identifier
9394 identifier
9395 identifier = identifier
9397 For example:
9398 @synthesize MyProperty;
9399 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9401 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9402 for C++. Keep them in sync.
9404 static void
9405 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9407 tree list = NULL_TREE;
9408 location_t loc;
9409 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9410 loc = c_parser_peek_token (parser)->location;
9412 c_parser_consume_token (parser);
9413 while (true)
9415 tree property, ivar;
9416 if (c_parser_next_token_is_not (parser, CPP_NAME))
9418 c_parser_error (parser, "expected identifier");
9419 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9420 /* Once we find the semicolon, we can resume normal parsing.
9421 We have to reset parser->error manually because
9422 c_parser_skip_until_found() won't reset it for us if the
9423 next token is precisely a semicolon. */
9424 parser->error = false;
9425 return;
9427 property = c_parser_peek_token (parser)->value;
9428 c_parser_consume_token (parser);
9429 if (c_parser_next_token_is (parser, CPP_EQ))
9431 c_parser_consume_token (parser);
9432 if (c_parser_next_token_is_not (parser, CPP_NAME))
9434 c_parser_error (parser, "expected identifier");
9435 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9436 parser->error = false;
9437 return;
9439 ivar = c_parser_peek_token (parser)->value;
9440 c_parser_consume_token (parser);
9442 else
9443 ivar = NULL_TREE;
9444 list = chainon (list, build_tree_list (ivar, property));
9445 if (c_parser_next_token_is (parser, CPP_COMMA))
9446 c_parser_consume_token (parser);
9447 else
9448 break;
9450 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9451 objc_add_synthesize_declaration (loc, list);
9454 /* Parse an Objective-C @dynamic declaration. The syntax is:
9456 objc-dynamic-declaration:
9457 @dynamic identifier-list ;
9459 For example:
9460 @dynamic MyProperty;
9461 @dynamic MyProperty, AnotherProperty;
9463 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9464 for C++. Keep them in sync.
9466 static void
9467 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9469 tree list = NULL_TREE;
9470 location_t loc;
9471 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9472 loc = c_parser_peek_token (parser)->location;
9474 c_parser_consume_token (parser);
9475 while (true)
9477 tree property;
9478 if (c_parser_next_token_is_not (parser, CPP_NAME))
9480 c_parser_error (parser, "expected identifier");
9481 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9482 parser->error = false;
9483 return;
9485 property = c_parser_peek_token (parser)->value;
9486 list = chainon (list, build_tree_list (NULL_TREE, property));
9487 c_parser_consume_token (parser);
9488 if (c_parser_next_token_is (parser, CPP_COMMA))
9489 c_parser_consume_token (parser);
9490 else
9491 break;
9493 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9494 objc_add_dynamic_declaration (loc, list);
9498 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9499 should be considered, statements. ALLOW_STMT is true if we're within
9500 the context of a function and such pragmas are to be allowed. Returns
9501 true if we actually parsed such a pragma. */
9503 static bool
9504 c_parser_pragma (c_parser *parser, enum pragma_context context)
9506 unsigned int id;
9508 id = c_parser_peek_token (parser)->pragma_kind;
9509 gcc_assert (id != PRAGMA_NONE);
9511 switch (id)
9513 case PRAGMA_OACC_UPDATE:
9514 if (context != pragma_compound)
9516 if (context == pragma_stmt)
9517 c_parser_error (parser, "%<#pragma acc update%> may only be "
9518 "used in compound statements");
9519 goto bad_stmt;
9521 c_parser_oacc_update (parser);
9522 return false;
9524 case PRAGMA_OMP_BARRIER:
9525 if (context != pragma_compound)
9527 if (context == pragma_stmt)
9528 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9529 "used in compound statements");
9530 goto bad_stmt;
9532 c_parser_omp_barrier (parser);
9533 return false;
9535 case PRAGMA_OMP_FLUSH:
9536 if (context != pragma_compound)
9538 if (context == pragma_stmt)
9539 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9540 "used in compound statements");
9541 goto bad_stmt;
9543 c_parser_omp_flush (parser);
9544 return false;
9546 case PRAGMA_OMP_TASKWAIT:
9547 if (context != pragma_compound)
9549 if (context == pragma_stmt)
9550 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9551 "used in compound statements");
9552 goto bad_stmt;
9554 c_parser_omp_taskwait (parser);
9555 return false;
9557 case PRAGMA_OMP_TASKYIELD:
9558 if (context != pragma_compound)
9560 if (context == pragma_stmt)
9561 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9562 "used in compound statements");
9563 goto bad_stmt;
9565 c_parser_omp_taskyield (parser);
9566 return false;
9568 case PRAGMA_OMP_CANCEL:
9569 if (context != pragma_compound)
9571 if (context == pragma_stmt)
9572 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9573 "used in compound statements");
9574 goto bad_stmt;
9576 c_parser_omp_cancel (parser);
9577 return false;
9579 case PRAGMA_OMP_CANCELLATION_POINT:
9580 if (context != pragma_compound)
9582 if (context == pragma_stmt)
9583 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9584 "only be used in compound statements");
9585 goto bad_stmt;
9587 c_parser_omp_cancellation_point (parser);
9588 return false;
9590 case PRAGMA_OMP_THREADPRIVATE:
9591 c_parser_omp_threadprivate (parser);
9592 return false;
9594 case PRAGMA_OMP_TARGET:
9595 return c_parser_omp_target (parser, context);
9597 case PRAGMA_OMP_END_DECLARE_TARGET:
9598 c_parser_omp_end_declare_target (parser);
9599 return false;
9601 case PRAGMA_OMP_SECTION:
9602 error_at (c_parser_peek_token (parser)->location,
9603 "%<#pragma omp section%> may only be used in "
9604 "%<#pragma omp sections%> construct");
9605 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9606 return false;
9608 case PRAGMA_OMP_DECLARE_REDUCTION:
9609 c_parser_omp_declare (parser, context);
9610 return false;
9611 case PRAGMA_IVDEP:
9612 c_parser_consume_pragma (parser);
9613 c_parser_skip_to_pragma_eol (parser);
9614 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9615 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9616 && !c_parser_next_token_is_keyword (parser, RID_DO))
9618 c_parser_error (parser, "for, while or do statement expected");
9619 return false;
9621 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9622 c_parser_for_statement (parser, true);
9623 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9624 c_parser_while_statement (parser, true);
9625 else
9626 c_parser_do_statement (parser, true);
9627 return false;
9629 case PRAGMA_GCC_PCH_PREPROCESS:
9630 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9631 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9632 return false;
9634 case PRAGMA_CILK_SIMD:
9635 if (!c_parser_cilk_verify_simd (parser, context))
9636 return false;
9637 c_parser_consume_pragma (parser);
9638 c_parser_cilk_simd (parser);
9639 return false;
9640 case PRAGMA_CILK_GRAINSIZE:
9641 if (!flag_cilkplus)
9643 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9644 " enabled");
9645 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9646 return false;
9648 if (context == pragma_external)
9650 error_at (c_parser_peek_token (parser)->location,
9651 "%<#pragma grainsize%> must be inside a function");
9652 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9653 return false;
9655 c_parser_cilk_grainsize (parser);
9656 return false;
9658 default:
9659 if (id < PRAGMA_FIRST_EXTERNAL)
9661 if (context != pragma_stmt && context != pragma_compound)
9663 bad_stmt:
9664 c_parser_error (parser, "expected declaration specifiers");
9665 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9666 return false;
9668 c_parser_omp_construct (parser);
9669 return true;
9671 break;
9674 c_parser_consume_pragma (parser);
9675 c_invoke_pragma_handler (id);
9677 /* Skip to EOL, but suppress any error message. Those will have been
9678 generated by the handler routine through calling error, as opposed
9679 to calling c_parser_error. */
9680 parser->error = true;
9681 c_parser_skip_to_pragma_eol (parser);
9683 return false;
9686 /* The interface the pragma parsers have to the lexer. */
9688 enum cpp_ttype
9689 pragma_lex (tree *value)
9691 c_token *tok = c_parser_peek_token (the_parser);
9692 enum cpp_ttype ret = tok->type;
9694 *value = tok->value;
9695 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9696 ret = CPP_EOF;
9697 else
9699 if (ret == CPP_KEYWORD)
9700 ret = CPP_NAME;
9701 c_parser_consume_token (the_parser);
9704 return ret;
9707 static void
9708 c_parser_pragma_pch_preprocess (c_parser *parser)
9710 tree name = NULL;
9712 c_parser_consume_pragma (parser);
9713 if (c_parser_next_token_is (parser, CPP_STRING))
9715 name = c_parser_peek_token (parser)->value;
9716 c_parser_consume_token (parser);
9718 else
9719 c_parser_error (parser, "expected string literal");
9720 c_parser_skip_to_pragma_eol (parser);
9722 if (name)
9723 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9726 /* OpenACC and OpenMP parsing routines. */
9728 /* Returns name of the next clause.
9729 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9730 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9731 returned and the token is consumed. */
9733 static pragma_omp_clause
9734 c_parser_omp_clause_name (c_parser *parser)
9736 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9738 if (c_parser_next_token_is_keyword (parser, RID_IF))
9739 result = PRAGMA_OMP_CLAUSE_IF;
9740 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9741 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9742 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9743 result = PRAGMA_OMP_CLAUSE_FOR;
9744 else if (c_parser_next_token_is (parser, CPP_NAME))
9746 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9748 switch (p[0])
9750 case 'a':
9751 if (!strcmp ("aligned", p))
9752 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9753 else if (!strcmp ("async", p))
9754 result = PRAGMA_OMP_CLAUSE_ASYNC;
9755 break;
9756 case 'c':
9757 if (!strcmp ("collapse", p))
9758 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9759 else if (!strcmp ("copy", p))
9760 result = PRAGMA_OMP_CLAUSE_COPY;
9761 else if (!strcmp ("copyin", p))
9762 result = PRAGMA_OMP_CLAUSE_COPYIN;
9763 else if (!strcmp ("copyout", p))
9764 result = PRAGMA_OMP_CLAUSE_COPYOUT;
9765 else if (!strcmp ("copyprivate", p))
9766 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9767 else if (!strcmp ("create", p))
9768 result = PRAGMA_OMP_CLAUSE_CREATE;
9769 break;
9770 case 'd':
9771 if (!strcmp ("delete", p))
9772 result = PRAGMA_OMP_CLAUSE_DELETE;
9773 else if (!strcmp ("depend", p))
9774 result = PRAGMA_OMP_CLAUSE_DEPEND;
9775 else if (!strcmp ("device", p))
9776 result = PRAGMA_OMP_CLAUSE_DEVICE;
9777 else if (!strcmp ("deviceptr", p))
9778 result = PRAGMA_OMP_CLAUSE_DEVICEPTR;
9779 else if (!strcmp ("dist_schedule", p))
9780 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9781 break;
9782 case 'f':
9783 if (!strcmp ("final", p))
9784 result = PRAGMA_OMP_CLAUSE_FINAL;
9785 else if (!strcmp ("firstprivate", p))
9786 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9787 else if (!strcmp ("from", p))
9788 result = PRAGMA_OMP_CLAUSE_FROM;
9789 break;
9790 case 'h':
9791 if (!strcmp ("host", p))
9792 result = PRAGMA_OMP_CLAUSE_SELF;
9793 break;
9794 case 'i':
9795 if (!strcmp ("inbranch", p))
9796 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9797 break;
9798 case 'l':
9799 if (!strcmp ("lastprivate", p))
9800 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9801 else if (!strcmp ("linear", p))
9802 result = PRAGMA_OMP_CLAUSE_LINEAR;
9803 break;
9804 case 'm':
9805 if (!strcmp ("map", p))
9806 result = PRAGMA_OMP_CLAUSE_MAP;
9807 else if (!strcmp ("mergeable", p))
9808 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9809 else if (flag_cilkplus && !strcmp ("mask", p))
9810 result = PRAGMA_CILK_CLAUSE_MASK;
9811 break;
9812 case 'n':
9813 if (!strcmp ("notinbranch", p))
9814 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9815 else if (!strcmp ("nowait", p))
9816 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9817 else if (!strcmp ("num_gangs", p))
9818 result = PRAGMA_OMP_CLAUSE_NUM_GANGS;
9819 else if (!strcmp ("num_teams", p))
9820 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9821 else if (!strcmp ("num_threads", p))
9822 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9823 else if (!strcmp ("num_workers", p))
9824 result = PRAGMA_OMP_CLAUSE_NUM_WORKERS;
9825 else if (flag_cilkplus && !strcmp ("nomask", p))
9826 result = PRAGMA_CILK_CLAUSE_NOMASK;
9827 break;
9828 case 'o':
9829 if (!strcmp ("ordered", p))
9830 result = PRAGMA_OMP_CLAUSE_ORDERED;
9831 break;
9832 case 'p':
9833 if (!strcmp ("parallel", p))
9834 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9835 else if (!strcmp ("present", p))
9836 result = PRAGMA_OMP_CLAUSE_PRESENT;
9837 else if (!strcmp ("present_or_copy", p)
9838 || !strcmp ("pcopy", p))
9839 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY;
9840 else if (!strcmp ("present_or_copyin", p)
9841 || !strcmp ("pcopyin", p))
9842 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN;
9843 else if (!strcmp ("present_or_copyout", p)
9844 || !strcmp ("pcopyout", p))
9845 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT;
9846 else if (!strcmp ("present_or_create", p)
9847 || !strcmp ("pcreate", p))
9848 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE;
9849 else if (!strcmp ("private", p))
9850 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9851 else if (!strcmp ("proc_bind", p))
9852 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9853 break;
9854 case 'r':
9855 if (!strcmp ("reduction", p))
9856 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9857 break;
9858 case 's':
9859 if (!strcmp ("safelen", p))
9860 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9861 else if (!strcmp ("schedule", p))
9862 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9863 else if (!strcmp ("sections", p))
9864 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9865 else if (!strcmp ("shared", p))
9866 result = PRAGMA_OMP_CLAUSE_SHARED;
9867 else if (!strcmp ("simdlen", p))
9868 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9869 else if (!strcmp ("self", p))
9870 result = PRAGMA_OMP_CLAUSE_SELF;
9871 break;
9872 case 't':
9873 if (!strcmp ("taskgroup", p))
9874 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9875 else if (!strcmp ("thread_limit", p))
9876 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9877 else if (!strcmp ("to", p))
9878 result = PRAGMA_OMP_CLAUSE_TO;
9879 break;
9880 case 'u':
9881 if (!strcmp ("uniform", p))
9882 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9883 else if (!strcmp ("untied", p))
9884 result = PRAGMA_OMP_CLAUSE_UNTIED;
9885 break;
9886 case 'v':
9887 if (!strcmp ("vector_length", p))
9888 result = PRAGMA_OMP_CLAUSE_VECTOR_LENGTH;
9889 else if (flag_cilkplus && !strcmp ("vectorlength", p))
9890 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9891 break;
9892 case 'w':
9893 if (!strcmp ("wait", p))
9894 result = PRAGMA_OMP_CLAUSE_WAIT;
9895 break;
9899 if (result != PRAGMA_OMP_CLAUSE_NONE)
9900 c_parser_consume_token (parser);
9902 return result;
9905 /* Validate that a clause of the given type does not already exist. */
9907 static void
9908 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9909 const char *name)
9911 tree c;
9913 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9914 if (OMP_CLAUSE_CODE (c) == code)
9916 location_t loc = OMP_CLAUSE_LOCATION (c);
9917 error_at (loc, "too many %qs clauses", name);
9918 break;
9922 /* OpenACC 2.0
9923 Parse wait clause or wait directive parameters. */
9925 static tree
9926 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
9928 vec<tree, va_gc> *args;
9929 tree t, args_tree;
9931 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9932 return list;
9934 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
9936 if (args->length () == 0)
9938 c_parser_error (parser, "expected integer expression before ')'");
9939 release_tree_vector (args);
9940 return list;
9943 args_tree = build_tree_list_vec (args);
9945 for (t = args_tree; t; t = TREE_CHAIN (t))
9947 tree targ = TREE_VALUE (t);
9949 if (targ != error_mark_node)
9951 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
9953 c_parser_error (parser, "expression must be integral");
9954 targ = error_mark_node;
9956 else
9958 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
9960 OMP_CLAUSE_DECL (c) = targ;
9961 OMP_CLAUSE_CHAIN (c) = list;
9962 list = c;
9967 release_tree_vector (args);
9968 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9969 return list;
9972 /* OpenACC 2.0, OpenMP 2.5:
9973 variable-list:
9974 identifier
9975 variable-list , identifier
9977 If KIND is nonzero, create the appropriate node and install the
9978 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9979 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9981 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9982 return the list created. */
9984 static tree
9985 c_parser_omp_variable_list (c_parser *parser,
9986 location_t clause_loc,
9987 enum omp_clause_code kind, tree list)
9989 if (c_parser_next_token_is_not (parser, CPP_NAME)
9990 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9991 c_parser_error (parser, "expected identifier");
9993 while (c_parser_next_token_is (parser, CPP_NAME)
9994 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9996 tree t = lookup_name (c_parser_peek_token (parser)->value);
9998 if (t == NULL_TREE)
10000 undeclared_variable (c_parser_peek_token (parser)->location,
10001 c_parser_peek_token (parser)->value);
10002 t = error_mark_node;
10005 c_parser_consume_token (parser);
10007 if (t == error_mark_node)
10009 else if (kind != 0)
10011 switch (kind)
10013 case OMP_CLAUSE_MAP:
10014 case OMP_CLAUSE_FROM:
10015 case OMP_CLAUSE_TO:
10016 case OMP_CLAUSE_DEPEND:
10017 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10019 tree low_bound = NULL_TREE, length = NULL_TREE;
10021 c_parser_consume_token (parser);
10022 if (!c_parser_next_token_is (parser, CPP_COLON))
10024 low_bound = c_parser_expression (parser).value;
10025 mark_exp_read (low_bound);
10027 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10028 length = integer_one_node;
10029 else
10031 /* Look for `:'. */
10032 if (!c_parser_require (parser, CPP_COLON,
10033 "expected %<:%>"))
10035 t = error_mark_node;
10036 break;
10038 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10040 length = c_parser_expression (parser).value;
10041 mark_exp_read (length);
10044 /* Look for the closing `]'. */
10045 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10046 "expected %<]%>"))
10048 t = error_mark_node;
10049 break;
10051 t = tree_cons (low_bound, length, t);
10053 break;
10054 default:
10055 break;
10058 if (t != error_mark_node)
10060 tree u = build_omp_clause (clause_loc, kind);
10061 OMP_CLAUSE_DECL (u) = t;
10062 OMP_CLAUSE_CHAIN (u) = list;
10063 list = u;
10066 else
10067 list = tree_cons (t, NULL_TREE, list);
10069 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10070 break;
10072 c_parser_consume_token (parser);
10075 return list;
10078 /* Similarly, but expect leading and trailing parenthesis. This is a very
10079 common case for OpenACC and OpenMP clauses. */
10081 static tree
10082 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10083 tree list)
10085 /* The clauses location. */
10086 location_t loc = c_parser_peek_token (parser)->location;
10088 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10090 list = c_parser_omp_variable_list (parser, loc, kind, list);
10091 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10093 return list;
10096 /* OpenACC 2.0:
10097 copy ( variable-list )
10098 copyin ( variable-list )
10099 copyout ( variable-list )
10100 create ( variable-list )
10101 delete ( variable-list )
10102 present ( variable-list )
10103 present_or_copy ( variable-list )
10104 pcopy ( variable-list )
10105 present_or_copyin ( variable-list )
10106 pcopyin ( variable-list )
10107 present_or_copyout ( variable-list )
10108 pcopyout ( variable-list )
10109 present_or_create ( variable-list )
10110 pcreate ( variable-list ) */
10112 static tree
10113 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10114 tree list)
10116 enum omp_clause_map_kind kind;
10117 switch (c_kind)
10119 default:
10120 gcc_unreachable ();
10121 case PRAGMA_OMP_CLAUSE_COPY:
10122 kind = OMP_CLAUSE_MAP_FORCE_TOFROM;
10123 break;
10124 case PRAGMA_OMP_CLAUSE_COPYIN:
10125 kind = OMP_CLAUSE_MAP_FORCE_TO;
10126 break;
10127 case PRAGMA_OMP_CLAUSE_COPYOUT:
10128 kind = OMP_CLAUSE_MAP_FORCE_FROM;
10129 break;
10130 case PRAGMA_OMP_CLAUSE_CREATE:
10131 kind = OMP_CLAUSE_MAP_FORCE_ALLOC;
10132 break;
10133 case PRAGMA_OMP_CLAUSE_DELETE:
10134 kind = OMP_CLAUSE_MAP_FORCE_DEALLOC;
10135 break;
10136 case PRAGMA_OMP_CLAUSE_DEVICE:
10137 kind = OMP_CLAUSE_MAP_FORCE_TO;
10138 break;
10139 case PRAGMA_OMP_CLAUSE_HOST:
10140 kind = OMP_CLAUSE_MAP_FORCE_FROM;
10141 break;
10142 case PRAGMA_OMP_CLAUSE_PRESENT:
10143 kind = OMP_CLAUSE_MAP_FORCE_PRESENT;
10144 break;
10145 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
10146 kind = OMP_CLAUSE_MAP_TOFROM;
10147 break;
10148 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
10149 kind = OMP_CLAUSE_MAP_TO;
10150 break;
10151 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
10152 kind = OMP_CLAUSE_MAP_FROM;
10153 break;
10154 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
10155 kind = OMP_CLAUSE_MAP_ALLOC;
10156 break;
10157 case PRAGMA_OMP_CLAUSE_SELF:
10158 kind = OMP_CLAUSE_MAP_FORCE_FROM;
10159 break;
10161 tree nl, c;
10162 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10164 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10165 OMP_CLAUSE_MAP_KIND (c) = kind;
10167 return nl;
10170 /* OpenACC 2.0:
10171 deviceptr ( variable-list ) */
10173 static tree
10174 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10176 location_t loc = c_parser_peek_token (parser)->location;
10177 tree vars, t;
10179 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10180 c_parser_oacc_data_clause), as for PRAGMA_OMP_CLAUSE_DEVICEPTR,
10181 variable-list must only allow for pointer variables. */
10182 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10183 for (t = vars; t && t; t = TREE_CHAIN (t))
10185 tree v = TREE_PURPOSE (t);
10187 /* FIXME diagnostics: Ideally we should keep individual
10188 locations for all the variables in the var list to make the
10189 following errors more precise. Perhaps
10190 c_parser_omp_var_list_parens() should construct a list of
10191 locations to go along with the var list. */
10193 if (TREE_CODE (v) != VAR_DECL)
10194 error_at (loc, "%qD is not a variable", v);
10195 else if (TREE_TYPE (v) == error_mark_node)
10197 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10198 error_at (loc, "%qD is not a pointer variable", v);
10200 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10201 OMP_CLAUSE_MAP_KIND (u) = OMP_CLAUSE_MAP_FORCE_DEVICEPTR;
10202 OMP_CLAUSE_DECL (u) = v;
10203 OMP_CLAUSE_CHAIN (u) = list;
10204 list = u;
10207 return list;
10210 /* OpenACC 2.0, OpenMP 3.0:
10211 collapse ( constant-expression ) */
10213 static tree
10214 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10216 tree c, num = error_mark_node;
10217 HOST_WIDE_INT n;
10218 location_t loc;
10220 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10222 loc = c_parser_peek_token (parser)->location;
10223 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10225 num = c_parser_expr_no_commas (parser, NULL).value;
10226 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10228 if (num == error_mark_node)
10229 return list;
10230 mark_exp_read (num);
10231 num = c_fully_fold (num, false, NULL);
10232 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10233 || !tree_fits_shwi_p (num)
10234 || (n = tree_to_shwi (num)) <= 0
10235 || (int) n != n)
10237 error_at (loc,
10238 "collapse argument needs positive constant integer expression");
10239 return list;
10241 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10242 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10243 OMP_CLAUSE_CHAIN (c) = list;
10244 return c;
10247 /* OpenMP 2.5:
10248 copyin ( variable-list ) */
10250 static tree
10251 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10253 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10256 /* OpenMP 2.5:
10257 copyprivate ( variable-list ) */
10259 static tree
10260 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10262 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10265 /* OpenMP 2.5:
10266 default ( shared | none ) */
10268 static tree
10269 c_parser_omp_clause_default (c_parser *parser, tree list)
10271 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10272 location_t loc = c_parser_peek_token (parser)->location;
10273 tree c;
10275 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10276 return list;
10277 if (c_parser_next_token_is (parser, CPP_NAME))
10279 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10281 switch (p[0])
10283 case 'n':
10284 if (strcmp ("none", p) != 0)
10285 goto invalid_kind;
10286 kind = OMP_CLAUSE_DEFAULT_NONE;
10287 break;
10289 case 's':
10290 if (strcmp ("shared", p) != 0)
10291 goto invalid_kind;
10292 kind = OMP_CLAUSE_DEFAULT_SHARED;
10293 break;
10295 default:
10296 goto invalid_kind;
10299 c_parser_consume_token (parser);
10301 else
10303 invalid_kind:
10304 c_parser_error (parser, "expected %<none%> or %<shared%>");
10306 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10308 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10309 return list;
10311 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10312 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10313 OMP_CLAUSE_CHAIN (c) = list;
10314 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10316 return c;
10319 /* OpenMP 2.5:
10320 firstprivate ( variable-list ) */
10322 static tree
10323 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10325 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10328 /* OpenMP 3.1:
10329 final ( expression ) */
10331 static tree
10332 c_parser_omp_clause_final (c_parser *parser, tree list)
10334 location_t loc = c_parser_peek_token (parser)->location;
10335 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10337 tree t = c_parser_paren_condition (parser);
10338 tree c;
10340 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10342 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10343 OMP_CLAUSE_FINAL_EXPR (c) = t;
10344 OMP_CLAUSE_CHAIN (c) = list;
10345 list = c;
10347 else
10348 c_parser_error (parser, "expected %<(%>");
10350 return list;
10353 /* OpenACC, OpenMP 2.5:
10354 if ( expression ) */
10356 static tree
10357 c_parser_omp_clause_if (c_parser *parser, tree list)
10359 location_t loc = c_parser_peek_token (parser)->location;
10360 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10362 tree t = c_parser_paren_condition (parser);
10363 tree c;
10365 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10367 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10368 OMP_CLAUSE_IF_EXPR (c) = t;
10369 OMP_CLAUSE_CHAIN (c) = list;
10370 list = c;
10372 else
10373 c_parser_error (parser, "expected %<(%>");
10375 return list;
10378 /* OpenMP 2.5:
10379 lastprivate ( variable-list ) */
10381 static tree
10382 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10384 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10387 /* OpenMP 3.1:
10388 mergeable */
10390 static tree
10391 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10393 tree c;
10395 /* FIXME: Should we allow duplicates? */
10396 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10398 c = build_omp_clause (c_parser_peek_token (parser)->location,
10399 OMP_CLAUSE_MERGEABLE);
10400 OMP_CLAUSE_CHAIN (c) = list;
10402 return c;
10405 /* OpenMP 2.5:
10406 nowait */
10408 static tree
10409 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10411 tree c;
10412 location_t loc = c_parser_peek_token (parser)->location;
10414 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10416 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10417 OMP_CLAUSE_CHAIN (c) = list;
10418 return c;
10421 /* OpenACC:
10422 num_gangs ( expression ) */
10424 static tree
10425 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
10427 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
10428 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10430 location_t expr_loc = c_parser_peek_token (parser)->location;
10431 tree c, t = c_parser_expression (parser).value;
10432 mark_exp_read (t);
10433 t = c_fully_fold (t, false, NULL);
10435 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10437 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10439 c_parser_error (parser, "expected integer expression");
10440 return list;
10443 /* Attempt to statically determine when the number isn't positive. */
10444 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10445 build_int_cst (TREE_TYPE (t), 0));
10446 if (CAN_HAVE_LOCATION_P (c))
10447 SET_EXPR_LOCATION (c, expr_loc);
10448 if (c == boolean_true_node)
10450 warning_at (expr_loc, 0,
10451 "%<num_gangs%> value must be positive");
10452 t = integer_one_node;
10455 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
10457 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
10458 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
10459 OMP_CLAUSE_CHAIN (c) = list;
10460 list = c;
10463 return list;
10466 /* OpenMP 2.5:
10467 num_threads ( expression ) */
10469 static tree
10470 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10472 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10473 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10475 location_t expr_loc = c_parser_peek_token (parser)->location;
10476 tree c, t = c_parser_expression (parser).value;
10477 mark_exp_read (t);
10478 t = c_fully_fold (t, false, NULL);
10480 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10482 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10484 c_parser_error (parser, "expected integer expression");
10485 return list;
10488 /* Attempt to statically determine when the number isn't positive. */
10489 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10490 build_int_cst (TREE_TYPE (t), 0));
10491 if (CAN_HAVE_LOCATION_P (c))
10492 SET_EXPR_LOCATION (c, expr_loc);
10493 if (c == boolean_true_node)
10495 warning_at (expr_loc, 0,
10496 "%<num_threads%> value must be positive");
10497 t = integer_one_node;
10500 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10502 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10503 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10504 OMP_CLAUSE_CHAIN (c) = list;
10505 list = c;
10508 return list;
10511 /* OpenACC:
10512 num_workers ( expression ) */
10514 static tree
10515 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
10517 location_t num_workers_loc = c_parser_peek_token (parser)->location;
10518 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10520 location_t expr_loc = c_parser_peek_token (parser)->location;
10521 tree c, t = c_parser_expression (parser).value;
10522 mark_exp_read (t);
10523 t = c_fully_fold (t, false, NULL);
10525 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10527 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10529 c_parser_error (parser, "expected integer expression");
10530 return list;
10533 /* Attempt to statically determine when the number isn't positive. */
10534 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10535 build_int_cst (TREE_TYPE (t), 0));
10536 if (CAN_HAVE_LOCATION_P (c))
10537 SET_EXPR_LOCATION (c, expr_loc);
10538 if (c == boolean_true_node)
10540 warning_at (expr_loc, 0,
10541 "%<num_workers%> value must be positive");
10542 t = integer_one_node;
10545 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
10547 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
10548 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
10549 OMP_CLAUSE_CHAIN (c) = list;
10550 list = c;
10553 return list;
10556 /* OpenACC:
10557 async [( int-expr )] */
10559 static tree
10560 c_parser_oacc_clause_async (c_parser *parser, tree list)
10562 tree c, t;
10563 location_t loc = c_parser_peek_token (parser)->location;
10565 /* TODO XXX: FIX -1 (acc_async_noval). */
10566 t = build_int_cst (integer_type_node, -1);
10568 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10570 c_parser_consume_token (parser);
10572 t = c_parser_expression (parser).value;
10573 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10574 c_parser_error (parser, "expected integer expression");
10575 else if (t == error_mark_node
10576 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10577 return list;
10579 else
10581 t = c_fully_fold (t, false, NULL);
10584 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
10586 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
10587 OMP_CLAUSE_ASYNC_EXPR (c) = t;
10588 OMP_CLAUSE_CHAIN (c) = list;
10589 list = c;
10591 return list;
10594 /* OpenACC:
10595 wait ( int-expr-list ) */
10597 static tree
10598 c_parser_oacc_clause_wait (c_parser *parser, tree list)
10600 location_t clause_loc = c_parser_peek_token (parser)->location;
10602 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10603 list = c_parser_oacc_wait_list (parser, clause_loc, list);
10605 return list;
10608 /* OpenMP 2.5:
10609 ordered */
10611 static tree
10612 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10614 tree c;
10616 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10618 c = build_omp_clause (c_parser_peek_token (parser)->location,
10619 OMP_CLAUSE_ORDERED);
10620 OMP_CLAUSE_CHAIN (c) = list;
10622 return c;
10625 /* OpenMP 2.5:
10626 private ( variable-list ) */
10628 static tree
10629 c_parser_omp_clause_private (c_parser *parser, tree list)
10631 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10634 /* OpenMP 2.5:
10635 reduction ( reduction-operator : variable-list )
10637 reduction-operator:
10638 One of: + * - & ^ | && ||
10640 OpenMP 3.1:
10642 reduction-operator:
10643 One of: + * - & ^ | && || max min
10645 OpenMP 4.0:
10647 reduction-operator:
10648 One of: + * - & ^ | && ||
10649 identifier */
10651 static tree
10652 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10654 location_t clause_loc = c_parser_peek_token (parser)->location;
10655 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10657 enum tree_code code = ERROR_MARK;
10658 tree reduc_id = NULL_TREE;
10660 switch (c_parser_peek_token (parser)->type)
10662 case CPP_PLUS:
10663 code = PLUS_EXPR;
10664 break;
10665 case CPP_MULT:
10666 code = MULT_EXPR;
10667 break;
10668 case CPP_MINUS:
10669 code = MINUS_EXPR;
10670 break;
10671 case CPP_AND:
10672 code = BIT_AND_EXPR;
10673 break;
10674 case CPP_XOR:
10675 code = BIT_XOR_EXPR;
10676 break;
10677 case CPP_OR:
10678 code = BIT_IOR_EXPR;
10679 break;
10680 case CPP_AND_AND:
10681 code = TRUTH_ANDIF_EXPR;
10682 break;
10683 case CPP_OR_OR:
10684 code = TRUTH_ORIF_EXPR;
10685 break;
10686 case CPP_NAME:
10688 const char *p
10689 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10690 if (strcmp (p, "min") == 0)
10692 code = MIN_EXPR;
10693 break;
10695 if (strcmp (p, "max") == 0)
10697 code = MAX_EXPR;
10698 break;
10700 reduc_id = c_parser_peek_token (parser)->value;
10701 break;
10703 default:
10704 c_parser_error (parser,
10705 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10706 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10707 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10708 return list;
10710 c_parser_consume_token (parser);
10711 reduc_id = c_omp_reduction_id (code, reduc_id);
10712 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10714 tree nl, c;
10716 nl = c_parser_omp_variable_list (parser, clause_loc,
10717 OMP_CLAUSE_REDUCTION, list);
10718 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10720 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10721 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10722 if (code == ERROR_MARK
10723 || !(INTEGRAL_TYPE_P (type)
10724 || TREE_CODE (type) == REAL_TYPE
10725 || TREE_CODE (type) == COMPLEX_TYPE))
10726 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10727 = c_omp_reduction_lookup (reduc_id,
10728 TYPE_MAIN_VARIANT (type));
10731 list = nl;
10733 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10735 return list;
10738 /* OpenMP 2.5:
10739 schedule ( schedule-kind )
10740 schedule ( schedule-kind , expression )
10742 schedule-kind:
10743 static | dynamic | guided | runtime | auto
10746 static tree
10747 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10749 tree c, t;
10750 location_t loc = c_parser_peek_token (parser)->location;
10752 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10753 return list;
10755 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10757 if (c_parser_next_token_is (parser, CPP_NAME))
10759 tree kind = c_parser_peek_token (parser)->value;
10760 const char *p = IDENTIFIER_POINTER (kind);
10762 switch (p[0])
10764 case 'd':
10765 if (strcmp ("dynamic", p) != 0)
10766 goto invalid_kind;
10767 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10768 break;
10770 case 'g':
10771 if (strcmp ("guided", p) != 0)
10772 goto invalid_kind;
10773 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10774 break;
10776 case 'r':
10777 if (strcmp ("runtime", p) != 0)
10778 goto invalid_kind;
10779 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10780 break;
10782 default:
10783 goto invalid_kind;
10786 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10787 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10788 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10789 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10790 else
10791 goto invalid_kind;
10793 c_parser_consume_token (parser);
10794 if (c_parser_next_token_is (parser, CPP_COMMA))
10796 location_t here;
10797 c_parser_consume_token (parser);
10799 here = c_parser_peek_token (parser)->location;
10800 t = c_parser_expr_no_commas (parser, NULL).value;
10801 mark_exp_read (t);
10802 t = c_fully_fold (t, false, NULL);
10804 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10805 error_at (here, "schedule %<runtime%> does not take "
10806 "a %<chunk_size%> parameter");
10807 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10808 error_at (here,
10809 "schedule %<auto%> does not take "
10810 "a %<chunk_size%> parameter");
10811 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10812 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10813 else
10814 c_parser_error (parser, "expected integer expression");
10816 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10818 else
10819 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10820 "expected %<,%> or %<)%>");
10822 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10823 OMP_CLAUSE_CHAIN (c) = list;
10824 return c;
10826 invalid_kind:
10827 c_parser_error (parser, "invalid schedule kind");
10828 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10829 return list;
10832 /* OpenMP 2.5:
10833 shared ( variable-list ) */
10835 static tree
10836 c_parser_omp_clause_shared (c_parser *parser, tree list)
10838 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10841 /* OpenMP 3.0:
10842 untied */
10844 static tree
10845 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10847 tree c;
10849 /* FIXME: Should we allow duplicates? */
10850 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10852 c = build_omp_clause (c_parser_peek_token (parser)->location,
10853 OMP_CLAUSE_UNTIED);
10854 OMP_CLAUSE_CHAIN (c) = list;
10856 return c;
10859 /* OpenACC:
10860 vector_length ( expression ) */
10862 static tree
10863 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
10865 location_t vector_length_loc = c_parser_peek_token (parser)->location;
10866 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10868 location_t expr_loc = c_parser_peek_token (parser)->location;
10869 tree c, t = c_parser_expression (parser).value;
10870 mark_exp_read (t);
10871 t = c_fully_fold (t, false, NULL);
10873 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10875 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10877 c_parser_error (parser, "expected integer expression");
10878 return list;
10881 /* Attempt to statically determine when the number isn't positive. */
10882 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10883 build_int_cst (TREE_TYPE (t), 0));
10884 if (CAN_HAVE_LOCATION_P (c))
10885 SET_EXPR_LOCATION (c, expr_loc);
10886 if (c == boolean_true_node)
10888 warning_at (expr_loc, 0,
10889 "%<vector_length%> value must be positive");
10890 t = integer_one_node;
10893 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
10895 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
10896 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
10897 OMP_CLAUSE_CHAIN (c) = list;
10898 list = c;
10901 return list;
10904 /* OpenMP 4.0:
10905 inbranch
10906 notinbranch */
10908 static tree
10909 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10910 enum omp_clause_code code, tree list)
10912 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10914 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10915 OMP_CLAUSE_CHAIN (c) = list;
10917 return c;
10920 /* OpenMP 4.0:
10921 parallel
10923 sections
10924 taskgroup */
10926 static tree
10927 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10928 enum omp_clause_code code, tree list)
10930 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10931 OMP_CLAUSE_CHAIN (c) = list;
10933 return c;
10936 /* OpenMP 4.0:
10937 num_teams ( expression ) */
10939 static tree
10940 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10942 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10943 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10945 location_t expr_loc = c_parser_peek_token (parser)->location;
10946 tree c, t = c_parser_expression (parser).value;
10947 mark_exp_read (t);
10948 t = c_fully_fold (t, false, NULL);
10950 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10952 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10954 c_parser_error (parser, "expected integer expression");
10955 return list;
10958 /* Attempt to statically determine when the number isn't positive. */
10959 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10960 build_int_cst (TREE_TYPE (t), 0));
10961 if (CAN_HAVE_LOCATION_P (c))
10962 SET_EXPR_LOCATION (c, expr_loc);
10963 if (c == boolean_true_node)
10965 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10966 t = integer_one_node;
10969 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10971 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10972 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10973 OMP_CLAUSE_CHAIN (c) = list;
10974 list = c;
10977 return list;
10980 /* OpenMP 4.0:
10981 thread_limit ( expression ) */
10983 static tree
10984 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10986 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
10987 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10989 location_t expr_loc = c_parser_peek_token (parser)->location;
10990 tree c, t = c_parser_expression (parser).value;
10991 mark_exp_read (t);
10992 t = c_fully_fold (t, false, NULL);
10994 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10996 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10998 c_parser_error (parser, "expected integer expression");
10999 return list;
11002 /* Attempt to statically determine when the number isn't positive. */
11003 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11004 build_int_cst (TREE_TYPE (t), 0));
11005 if (CAN_HAVE_LOCATION_P (c))
11006 SET_EXPR_LOCATION (c, expr_loc);
11007 if (c == boolean_true_node)
11009 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
11010 t = integer_one_node;
11013 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
11014 "thread_limit");
11016 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11017 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
11018 OMP_CLAUSE_CHAIN (c) = list;
11019 list = c;
11022 return list;
11025 /* OpenMP 4.0:
11026 aligned ( variable-list )
11027 aligned ( variable-list : constant-expression ) */
11029 static tree
11030 c_parser_omp_clause_aligned (c_parser *parser, tree list)
11032 location_t clause_loc = c_parser_peek_token (parser)->location;
11033 tree nl, c;
11035 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11036 return list;
11038 nl = c_parser_omp_variable_list (parser, clause_loc,
11039 OMP_CLAUSE_ALIGNED, list);
11041 if (c_parser_next_token_is (parser, CPP_COLON))
11043 c_parser_consume_token (parser);
11044 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
11045 mark_exp_read (alignment);
11046 alignment = c_fully_fold (alignment, false, NULL);
11047 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
11048 && TREE_CODE (alignment) != INTEGER_CST
11049 && tree_int_cst_sgn (alignment) != 1)
11051 error_at (clause_loc, "%<aligned%> clause alignment expression must "
11052 "be positive constant integer expression");
11053 alignment = NULL_TREE;
11056 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11057 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
11060 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11061 return nl;
11064 /* OpenMP 4.0:
11065 linear ( variable-list )
11066 linear ( variable-list : expression ) */
11068 static tree
11069 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
11071 location_t clause_loc = c_parser_peek_token (parser)->location;
11072 tree nl, c, step;
11074 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11075 return list;
11077 nl = c_parser_omp_variable_list (parser, clause_loc,
11078 OMP_CLAUSE_LINEAR, list);
11080 if (c_parser_next_token_is (parser, CPP_COLON))
11082 c_parser_consume_token (parser);
11083 step = c_parser_expression (parser).value;
11084 mark_exp_read (step);
11085 step = c_fully_fold (step, false, NULL);
11086 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
11088 sorry ("using parameters for %<linear%> step is not supported yet");
11089 step = integer_one_node;
11091 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
11093 error_at (clause_loc, "%<linear%> clause step expression must "
11094 "be integral");
11095 step = integer_one_node;
11099 else
11100 step = integer_one_node;
11102 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11104 OMP_CLAUSE_LINEAR_STEP (c) = step;
11107 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11108 return nl;
11111 /* OpenMP 4.0:
11112 safelen ( constant-expression ) */
11114 static tree
11115 c_parser_omp_clause_safelen (c_parser *parser, tree list)
11117 location_t clause_loc = c_parser_peek_token (parser)->location;
11118 tree c, t;
11120 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11121 return list;
11123 t = c_parser_expr_no_commas (parser, NULL).value;
11124 mark_exp_read (t);
11125 t = c_fully_fold (t, false, NULL);
11126 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11127 && TREE_CODE (t) != INTEGER_CST
11128 && tree_int_cst_sgn (t) != 1)
11130 error_at (clause_loc, "%<safelen%> clause expression must "
11131 "be positive constant integer expression");
11132 t = NULL_TREE;
11135 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11136 if (t == NULL_TREE || t == error_mark_node)
11137 return list;
11139 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
11141 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
11142 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
11143 OMP_CLAUSE_CHAIN (c) = list;
11144 return c;
11147 /* OpenMP 4.0:
11148 simdlen ( constant-expression ) */
11150 static tree
11151 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
11153 location_t clause_loc = c_parser_peek_token (parser)->location;
11154 tree c, t;
11156 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11157 return list;
11159 t = c_parser_expr_no_commas (parser, NULL).value;
11160 mark_exp_read (t);
11161 t = c_fully_fold (t, false, NULL);
11162 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11163 && TREE_CODE (t) != INTEGER_CST
11164 && tree_int_cst_sgn (t) != 1)
11166 error_at (clause_loc, "%<simdlen%> clause expression must "
11167 "be positive constant integer expression");
11168 t = NULL_TREE;
11171 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11172 if (t == NULL_TREE || t == error_mark_node)
11173 return list;
11175 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
11177 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
11178 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
11179 OMP_CLAUSE_CHAIN (c) = list;
11180 return c;
11183 /* OpenMP 4.0:
11184 depend ( depend-kind: variable-list )
11186 depend-kind:
11187 in | out | inout */
11189 static tree
11190 c_parser_omp_clause_depend (c_parser *parser, tree list)
11192 location_t clause_loc = c_parser_peek_token (parser)->location;
11193 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
11194 tree nl, c;
11196 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11197 return list;
11199 if (c_parser_next_token_is (parser, CPP_NAME))
11201 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11202 if (strcmp ("in", p) == 0)
11203 kind = OMP_CLAUSE_DEPEND_IN;
11204 else if (strcmp ("inout", p) == 0)
11205 kind = OMP_CLAUSE_DEPEND_INOUT;
11206 else if (strcmp ("out", p) == 0)
11207 kind = OMP_CLAUSE_DEPEND_OUT;
11208 else
11209 goto invalid_kind;
11211 else
11212 goto invalid_kind;
11214 c_parser_consume_token (parser);
11215 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11216 goto resync_fail;
11218 nl = c_parser_omp_variable_list (parser, clause_loc,
11219 OMP_CLAUSE_DEPEND, list);
11221 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11222 OMP_CLAUSE_DEPEND_KIND (c) = kind;
11224 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11225 return nl;
11227 invalid_kind:
11228 c_parser_error (parser, "invalid depend kind");
11229 resync_fail:
11230 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11231 return list;
11234 /* OpenMP 4.0:
11235 map ( map-kind: variable-list )
11236 map ( variable-list )
11238 map-kind:
11239 alloc | to | from | tofrom */
11241 static tree
11242 c_parser_omp_clause_map (c_parser *parser, tree list)
11244 location_t clause_loc = c_parser_peek_token (parser)->location;
11245 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
11246 tree nl, c;
11248 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11249 return list;
11251 if (c_parser_next_token_is (parser, CPP_NAME)
11252 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11254 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11255 if (strcmp ("alloc", p) == 0)
11256 kind = OMP_CLAUSE_MAP_ALLOC;
11257 else if (strcmp ("to", p) == 0)
11258 kind = OMP_CLAUSE_MAP_TO;
11259 else if (strcmp ("from", p) == 0)
11260 kind = OMP_CLAUSE_MAP_FROM;
11261 else if (strcmp ("tofrom", p) == 0)
11262 kind = OMP_CLAUSE_MAP_TOFROM;
11263 else
11265 c_parser_error (parser, "invalid map kind");
11266 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11267 "expected %<)%>");
11268 return list;
11270 c_parser_consume_token (parser);
11271 c_parser_consume_token (parser);
11274 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
11276 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11277 OMP_CLAUSE_MAP_KIND (c) = kind;
11279 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11280 return nl;
11283 /* OpenMP 4.0:
11284 device ( expression ) */
11286 static tree
11287 c_parser_omp_clause_device (c_parser *parser, tree list)
11289 location_t clause_loc = c_parser_peek_token (parser)->location;
11290 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11292 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
11293 mark_exp_read (t);
11294 t = c_fully_fold (t, false, NULL);
11296 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11298 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11300 c_parser_error (parser, "expected integer expression");
11301 return list;
11304 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
11306 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
11307 OMP_CLAUSE_DEVICE_ID (c) = t;
11308 OMP_CLAUSE_CHAIN (c) = list;
11309 list = c;
11312 return list;
11315 /* OpenMP 4.0:
11316 dist_schedule ( static )
11317 dist_schedule ( static , expression ) */
11319 static tree
11320 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
11322 tree c, t = NULL_TREE;
11323 location_t loc = c_parser_peek_token (parser)->location;
11325 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11326 return list;
11328 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
11330 c_parser_error (parser, "invalid dist_schedule kind");
11331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11332 "expected %<)%>");
11333 return list;
11336 c_parser_consume_token (parser);
11337 if (c_parser_next_token_is (parser, CPP_COMMA))
11339 c_parser_consume_token (parser);
11341 t = c_parser_expr_no_commas (parser, NULL).value;
11342 mark_exp_read (t);
11343 t = c_fully_fold (t, false, NULL);
11344 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11346 else
11347 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11348 "expected %<,%> or %<)%>");
11350 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11351 if (t == error_mark_node)
11352 return list;
11354 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
11355 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
11356 OMP_CLAUSE_CHAIN (c) = list;
11357 return c;
11360 /* OpenMP 4.0:
11361 proc_bind ( proc-bind-kind )
11363 proc-bind-kind:
11364 master | close | spread */
11366 static tree
11367 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
11369 location_t clause_loc = c_parser_peek_token (parser)->location;
11370 enum omp_clause_proc_bind_kind kind;
11371 tree c;
11373 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11374 return list;
11376 if (c_parser_next_token_is (parser, CPP_NAME))
11378 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11379 if (strcmp ("master", p) == 0)
11380 kind = OMP_CLAUSE_PROC_BIND_MASTER;
11381 else if (strcmp ("close", p) == 0)
11382 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
11383 else if (strcmp ("spread", p) == 0)
11384 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
11385 else
11386 goto invalid_kind;
11388 else
11389 goto invalid_kind;
11391 c_parser_consume_token (parser);
11392 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11393 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
11394 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
11395 OMP_CLAUSE_CHAIN (c) = list;
11396 return c;
11398 invalid_kind:
11399 c_parser_error (parser, "invalid proc_bind kind");
11400 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11401 return list;
11404 /* OpenMP 4.0:
11405 to ( variable-list ) */
11407 static tree
11408 c_parser_omp_clause_to (c_parser *parser, tree list)
11410 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11413 /* OpenMP 4.0:
11414 from ( variable-list ) */
11416 static tree
11417 c_parser_omp_clause_from (c_parser *parser, tree list)
11419 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11422 /* OpenMP 4.0:
11423 uniform ( variable-list ) */
11425 static tree
11426 c_parser_omp_clause_uniform (c_parser *parser, tree list)
11428 /* The clauses location. */
11429 location_t loc = c_parser_peek_token (parser)->location;
11431 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11433 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11434 list);
11435 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11437 return list;
11440 /* Parse all OpenACC clauses. The set clauses allowed by the directive
11441 is a bitmask in MASK. Return the list of clauses found. */
11443 static tree
11444 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
11445 const char *where, bool finish_p = true)
11447 tree clauses = NULL;
11448 bool first = true;
11450 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11452 location_t here;
11453 pragma_omp_clause c_kind;
11454 const char *c_name;
11455 tree prev = clauses;
11457 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11458 c_parser_consume_token (parser);
11460 here = c_parser_peek_token (parser)->location;
11461 c_kind = c_parser_omp_clause_name (parser);
11463 switch (c_kind)
11465 case PRAGMA_OMP_CLAUSE_ASYNC:
11466 clauses = c_parser_oacc_clause_async (parser, clauses);
11467 c_name = "async";
11468 break;
11469 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11470 clauses = c_parser_omp_clause_collapse (parser, clauses);
11471 c_name = "collapse";
11472 break;
11473 case PRAGMA_OMP_CLAUSE_COPY:
11474 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11475 c_name = "copy";
11476 break;
11477 case PRAGMA_OMP_CLAUSE_COPYIN:
11478 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11479 c_name = "copyin";
11480 break;
11481 case PRAGMA_OMP_CLAUSE_COPYOUT:
11482 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11483 c_name = "copyout";
11484 break;
11485 case PRAGMA_OMP_CLAUSE_CREATE:
11486 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11487 c_name = "create";
11488 break;
11489 case PRAGMA_OMP_CLAUSE_DELETE:
11490 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11491 c_name = "delete";
11492 break;
11493 case PRAGMA_OMP_CLAUSE_DEVICE:
11494 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11495 c_name = "device";
11496 break;
11497 case PRAGMA_OMP_CLAUSE_DEVICEPTR:
11498 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
11499 c_name = "deviceptr";
11500 break;
11501 case PRAGMA_OMP_CLAUSE_HOST:
11502 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11503 c_name = "host";
11504 break;
11505 case PRAGMA_OMP_CLAUSE_IF:
11506 clauses = c_parser_omp_clause_if (parser, clauses);
11507 c_name = "if";
11508 break;
11509 case PRAGMA_OMP_CLAUSE_NUM_GANGS:
11510 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
11511 c_name = "num_gangs";
11512 break;
11513 case PRAGMA_OMP_CLAUSE_NUM_WORKERS:
11514 clauses = c_parser_omp_clause_num_workers (parser, clauses);
11515 c_name = "num_workers";
11516 break;
11517 case PRAGMA_OMP_CLAUSE_PRESENT:
11518 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11519 c_name = "present";
11520 break;
11521 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
11522 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11523 c_name = "present_or_copy";
11524 break;
11525 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
11526 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11527 c_name = "present_or_copyin";
11528 break;
11529 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
11530 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11531 c_name = "present_or_copyout";
11532 break;
11533 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
11534 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11535 c_name = "present_or_create";
11536 break;
11537 case PRAGMA_OMP_CLAUSE_REDUCTION:
11538 clauses = c_parser_omp_clause_reduction (parser, clauses);
11539 c_name = "reduction";
11540 break;
11541 case PRAGMA_OMP_CLAUSE_SELF:
11542 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11543 c_name = "self";
11544 break;
11545 case PRAGMA_OMP_CLAUSE_VECTOR_LENGTH:
11546 clauses = c_parser_omp_clause_vector_length (parser, clauses);
11547 c_name = "vector_length";
11548 break;
11549 case PRAGMA_OMP_CLAUSE_WAIT:
11550 clauses = c_parser_oacc_clause_wait (parser, clauses);
11551 c_name = "wait";
11552 break;
11553 default:
11554 c_parser_error (parser, "expected clause");
11555 goto saw_error;
11558 first = false;
11560 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11562 /* Remove the invalid clause(s) from the list to avoid
11563 confusing the rest of the compiler. */
11564 clauses = prev;
11565 error_at (here, "%qs is not valid for %qs", c_name, where);
11569 saw_error:
11570 c_parser_skip_to_pragma_eol (parser);
11572 if (finish_p)
11573 return c_finish_omp_clauses (clauses);
11575 return clauses;
11578 /* Parse all OpenMP clauses. The set clauses allowed by the directive
11579 is a bitmask in MASK. Return the list of clauses found. */
11581 static tree
11582 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11583 const char *where, bool finish_p = true)
11585 tree clauses = NULL;
11586 bool first = true, cilk_simd_fn = false;
11588 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11590 location_t here;
11591 pragma_omp_clause c_kind;
11592 const char *c_name;
11593 tree prev = clauses;
11595 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11596 c_parser_consume_token (parser);
11598 here = c_parser_peek_token (parser)->location;
11599 c_kind = c_parser_omp_clause_name (parser);
11601 switch (c_kind)
11603 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11604 clauses = c_parser_omp_clause_collapse (parser, clauses);
11605 c_name = "collapse";
11606 break;
11607 case PRAGMA_OMP_CLAUSE_COPYIN:
11608 clauses = c_parser_omp_clause_copyin (parser, clauses);
11609 c_name = "copyin";
11610 break;
11611 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11612 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11613 c_name = "copyprivate";
11614 break;
11615 case PRAGMA_OMP_CLAUSE_DEFAULT:
11616 clauses = c_parser_omp_clause_default (parser, clauses);
11617 c_name = "default";
11618 break;
11619 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11620 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11621 c_name = "firstprivate";
11622 break;
11623 case PRAGMA_OMP_CLAUSE_FINAL:
11624 clauses = c_parser_omp_clause_final (parser, clauses);
11625 c_name = "final";
11626 break;
11627 case PRAGMA_OMP_CLAUSE_IF:
11628 clauses = c_parser_omp_clause_if (parser, clauses);
11629 c_name = "if";
11630 break;
11631 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11632 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11633 c_name = "lastprivate";
11634 break;
11635 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11636 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11637 c_name = "mergeable";
11638 break;
11639 case PRAGMA_OMP_CLAUSE_NOWAIT:
11640 clauses = c_parser_omp_clause_nowait (parser, clauses);
11641 c_name = "nowait";
11642 break;
11643 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11644 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11645 c_name = "num_threads";
11646 break;
11647 case PRAGMA_OMP_CLAUSE_ORDERED:
11648 clauses = c_parser_omp_clause_ordered (parser, clauses);
11649 c_name = "ordered";
11650 break;
11651 case PRAGMA_OMP_CLAUSE_PRIVATE:
11652 clauses = c_parser_omp_clause_private (parser, clauses);
11653 c_name = "private";
11654 break;
11655 case PRAGMA_OMP_CLAUSE_REDUCTION:
11656 clauses = c_parser_omp_clause_reduction (parser, clauses);
11657 c_name = "reduction";
11658 break;
11659 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11660 clauses = c_parser_omp_clause_schedule (parser, clauses);
11661 c_name = "schedule";
11662 break;
11663 case PRAGMA_OMP_CLAUSE_SHARED:
11664 clauses = c_parser_omp_clause_shared (parser, clauses);
11665 c_name = "shared";
11666 break;
11667 case PRAGMA_OMP_CLAUSE_UNTIED:
11668 clauses = c_parser_omp_clause_untied (parser, clauses);
11669 c_name = "untied";
11670 break;
11671 case PRAGMA_OMP_CLAUSE_INBRANCH:
11672 case PRAGMA_CILK_CLAUSE_MASK:
11673 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11674 clauses);
11675 c_name = "inbranch";
11676 break;
11677 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11678 case PRAGMA_CILK_CLAUSE_NOMASK:
11679 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11680 clauses);
11681 c_name = "notinbranch";
11682 break;
11683 case PRAGMA_OMP_CLAUSE_PARALLEL:
11684 clauses
11685 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11686 clauses);
11687 c_name = "parallel";
11688 if (!first)
11690 clause_not_first:
11691 error_at (here, "%qs must be the first clause of %qs",
11692 c_name, where);
11693 clauses = prev;
11695 break;
11696 case PRAGMA_OMP_CLAUSE_FOR:
11697 clauses
11698 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11699 clauses);
11700 c_name = "for";
11701 if (!first)
11702 goto clause_not_first;
11703 break;
11704 case PRAGMA_OMP_CLAUSE_SECTIONS:
11705 clauses
11706 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11707 clauses);
11708 c_name = "sections";
11709 if (!first)
11710 goto clause_not_first;
11711 break;
11712 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11713 clauses
11714 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11715 clauses);
11716 c_name = "taskgroup";
11717 if (!first)
11718 goto clause_not_first;
11719 break;
11720 case PRAGMA_OMP_CLAUSE_TO:
11721 clauses = c_parser_omp_clause_to (parser, clauses);
11722 c_name = "to";
11723 break;
11724 case PRAGMA_OMP_CLAUSE_FROM:
11725 clauses = c_parser_omp_clause_from (parser, clauses);
11726 c_name = "from";
11727 break;
11728 case PRAGMA_OMP_CLAUSE_UNIFORM:
11729 clauses = c_parser_omp_clause_uniform (parser, clauses);
11730 c_name = "uniform";
11731 break;
11732 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11733 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11734 c_name = "num_teams";
11735 break;
11736 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11737 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11738 c_name = "thread_limit";
11739 break;
11740 case PRAGMA_OMP_CLAUSE_ALIGNED:
11741 clauses = c_parser_omp_clause_aligned (parser, clauses);
11742 c_name = "aligned";
11743 break;
11744 case PRAGMA_OMP_CLAUSE_LINEAR:
11745 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11746 cilk_simd_fn = true;
11747 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11748 c_name = "linear";
11749 break;
11750 case PRAGMA_OMP_CLAUSE_DEPEND:
11751 clauses = c_parser_omp_clause_depend (parser, clauses);
11752 c_name = "depend";
11753 break;
11754 case PRAGMA_OMP_CLAUSE_MAP:
11755 clauses = c_parser_omp_clause_map (parser, clauses);
11756 c_name = "map";
11757 break;
11758 case PRAGMA_OMP_CLAUSE_DEVICE:
11759 clauses = c_parser_omp_clause_device (parser, clauses);
11760 c_name = "device";
11761 break;
11762 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11763 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11764 c_name = "dist_schedule";
11765 break;
11766 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11767 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11768 c_name = "proc_bind";
11769 break;
11770 case PRAGMA_OMP_CLAUSE_SAFELEN:
11771 clauses = c_parser_omp_clause_safelen (parser, clauses);
11772 c_name = "safelen";
11773 break;
11774 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11775 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11776 c_name = "simdlen";
11777 break;
11778 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11779 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11780 c_name = "simdlen";
11781 break;
11782 default:
11783 c_parser_error (parser, "expected clause");
11784 goto saw_error;
11787 first = false;
11789 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11791 /* Remove the invalid clause(s) from the list to avoid
11792 confusing the rest of the compiler. */
11793 clauses = prev;
11794 error_at (here, "%qs is not valid for %qs", c_name, where);
11798 saw_error:
11799 c_parser_skip_to_pragma_eol (parser);
11801 if (finish_p)
11802 return c_finish_omp_clauses (clauses);
11804 return clauses;
11807 /* OpenACC 2.0, OpenMP 2.5:
11808 structured-block:
11809 statement
11811 In practice, we're also interested in adding the statement to an
11812 outer node. So it is convenient if we work around the fact that
11813 c_parser_statement calls add_stmt. */
11815 static tree
11816 c_parser_omp_structured_block (c_parser *parser)
11818 tree stmt = push_stmt_list ();
11819 c_parser_statement (parser);
11820 return pop_stmt_list (stmt);
11823 /* OpenACC 2.0:
11824 # pragma acc data oacc-data-clause[optseq] new-line
11825 structured-block
11827 LOC is the location of the #pragma token.
11830 #define OACC_DATA_CLAUSE_MASK \
11831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
11832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
11833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
11834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
11835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
11836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
11837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
11838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
11839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
11840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
11841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) )
11843 static tree
11844 c_parser_oacc_data (location_t loc, c_parser *parser)
11846 tree stmt, clauses, block;
11848 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
11849 "#pragma acc data");
11851 block = c_begin_omp_parallel ();
11852 add_stmt (c_parser_omp_structured_block (parser));
11854 stmt = c_finish_oacc_data (loc, clauses, block);
11856 return stmt;
11859 /* OpenACC 2.0:
11860 # pragma acc kernels oacc-kernels-clause[optseq] new-line
11861 structured-block
11863 LOC is the location of the #pragma token.
11866 #define OACC_KERNELS_CLAUSE_MASK \
11867 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
11868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
11869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
11870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
11871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
11872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
11873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
11874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
11875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
11876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
11877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
11878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
11879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
11881 static tree
11882 c_parser_oacc_kernels (location_t loc, c_parser *parser, char *p_name)
11884 tree stmt, clauses = NULL_TREE, block;
11886 strcat (p_name, " kernels");
11888 if (c_parser_next_token_is (parser, CPP_NAME))
11890 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11891 if (strcmp (p, "loop") == 0)
11893 c_parser_consume_token (parser);
11894 block = c_begin_omp_parallel ();
11895 c_parser_oacc_loop (loc, parser, p_name);
11896 stmt = c_finish_oacc_kernels (loc, clauses, block);
11897 OACC_KERNELS_COMBINED (stmt) = 1;
11898 return stmt;
11902 clauses = c_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
11903 p_name);
11905 block = c_begin_omp_parallel ();
11906 add_stmt (c_parser_omp_structured_block (parser));
11908 stmt = c_finish_oacc_kernels (loc, clauses, block);
11910 return stmt;
11913 /* OpenACC 2.0:
11914 # pragma acc loop oacc-loop-clause[optseq] new-line
11915 structured-block
11917 LOC is the location of the #pragma token.
11920 #define OACC_LOOP_CLAUSE_MASK \
11921 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
11922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) )
11924 static tree
11925 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name)
11927 tree stmt, clauses, block;
11929 strcat (p_name, " loop");
11931 clauses = c_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK, p_name);
11933 block = c_begin_compound_stmt (true);
11934 stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
11935 block = c_end_compound_stmt (loc, block, true);
11936 add_stmt (block);
11938 return stmt;
11941 /* OpenACC 2.0:
11942 # pragma acc parallel oacc-parallel-clause[optseq] new-line
11943 structured-block
11945 LOC is the location of the #pragma token.
11948 #define OACC_PARALLEL_CLAUSE_MASK \
11949 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
11950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
11951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
11952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
11953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
11954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
11955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
11956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_GANGS) \
11957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_WORKERS) \
11958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
11959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
11960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
11961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
11962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
11963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_VECTOR_LENGTH) \
11965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
11967 static tree
11968 c_parser_oacc_parallel (location_t loc, c_parser *parser, char *p_name)
11970 tree stmt, clauses = NULL_TREE, block;
11972 strcat (p_name, " parallel");
11974 if (c_parser_next_token_is (parser, CPP_NAME))
11976 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11977 if (strcmp (p, "loop") == 0)
11979 c_parser_consume_token (parser);
11980 block = c_begin_omp_parallel ();
11981 c_parser_oacc_loop (loc, parser, p_name);
11982 stmt = c_finish_oacc_parallel (loc, clauses, block);
11983 OACC_PARALLEL_COMBINED (stmt) = 1;
11984 return stmt;
11988 clauses = c_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
11989 p_name);
11991 block = c_begin_omp_parallel ();
11992 add_stmt (c_parser_omp_structured_block (parser));
11994 stmt = c_finish_oacc_parallel (loc, clauses, block);
11996 return stmt;
11999 /* OpenACC 2.0:
12000 # pragma acc update oacc-update-clause[optseq] new-line
12003 #define OACC_UPDATE_CLAUSE_MASK \
12004 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
12005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HOST) \
12007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SELF) \
12009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
12011 static void
12012 c_parser_oacc_update (c_parser *parser)
12014 location_t loc = c_parser_peek_token (parser)->location;
12016 c_parser_consume_pragma (parser);
12018 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
12019 "#pragma acc update");
12020 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12022 error_at (loc,
12023 "%<#pragma acc update%> must contain at least one "
12024 "%<device%> or %<host/self%> clause");
12025 return;
12028 if (parser->error)
12029 return;
12031 tree stmt = make_node (OACC_UPDATE);
12032 TREE_TYPE (stmt) = void_type_node;
12033 OACC_UPDATE_CLAUSES (stmt) = clauses;
12034 SET_EXPR_LOCATION (stmt, loc);
12035 add_stmt (stmt);
12038 /* OpenACC 2.0:
12039 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
12041 LOC is the location of the #pragma token.
12044 #define OACC_WAIT_CLAUSE_MASK \
12045 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) )
12047 static tree
12048 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
12050 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
12052 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12053 list = c_parser_oacc_wait_list (parser, loc, list);
12055 strcpy (p_name, " wait");
12056 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
12057 stmt = c_finish_oacc_wait (loc, list, clauses);
12059 return stmt;
12062 /* OpenMP 2.5:
12063 # pragma omp atomic new-line
12064 expression-stmt
12066 expression-stmt:
12067 x binop= expr | x++ | ++x | x-- | --x
12068 binop:
12069 +, *, -, /, &, ^, |, <<, >>
12071 where x is an lvalue expression with scalar type.
12073 OpenMP 3.1:
12074 # pragma omp atomic new-line
12075 update-stmt
12077 # pragma omp atomic read new-line
12078 read-stmt
12080 # pragma omp atomic write new-line
12081 write-stmt
12083 # pragma omp atomic update new-line
12084 update-stmt
12086 # pragma omp atomic capture new-line
12087 capture-stmt
12089 # pragma omp atomic capture new-line
12090 capture-block
12092 read-stmt:
12093 v = x
12094 write-stmt:
12095 x = expr
12096 update-stmt:
12097 expression-stmt | x = x binop expr
12098 capture-stmt:
12099 v = expression-stmt
12100 capture-block:
12101 { v = x; update-stmt; } | { update-stmt; v = x; }
12103 OpenMP 4.0:
12104 update-stmt:
12105 expression-stmt | x = x binop expr | x = expr binop x
12106 capture-stmt:
12107 v = update-stmt
12108 capture-block:
12109 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
12111 where x and v are lvalue expressions with scalar type.
12113 LOC is the location of the #pragma token. */
12115 static void
12116 c_parser_omp_atomic (location_t loc, c_parser *parser)
12118 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
12119 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
12120 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
12121 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
12122 struct c_expr expr;
12123 location_t eloc;
12124 bool structured_block = false;
12125 bool swapped = false;
12126 bool seq_cst = false;
12128 if (c_parser_next_token_is (parser, CPP_NAME))
12130 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12131 if (!strcmp (p, "seq_cst"))
12133 seq_cst = true;
12134 c_parser_consume_token (parser);
12135 if (c_parser_next_token_is (parser, CPP_COMMA)
12136 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12137 c_parser_consume_token (parser);
12140 if (c_parser_next_token_is (parser, CPP_NAME))
12142 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12144 if (!strcmp (p, "read"))
12145 code = OMP_ATOMIC_READ;
12146 else if (!strcmp (p, "write"))
12147 code = NOP_EXPR;
12148 else if (!strcmp (p, "update"))
12149 code = OMP_ATOMIC;
12150 else if (!strcmp (p, "capture"))
12151 code = OMP_ATOMIC_CAPTURE_NEW;
12152 else
12153 p = NULL;
12154 if (p)
12155 c_parser_consume_token (parser);
12157 if (!seq_cst)
12159 if (c_parser_next_token_is (parser, CPP_COMMA)
12160 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12161 c_parser_consume_token (parser);
12163 if (c_parser_next_token_is (parser, CPP_NAME))
12165 const char *p
12166 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12167 if (!strcmp (p, "seq_cst"))
12169 seq_cst = true;
12170 c_parser_consume_token (parser);
12174 c_parser_skip_to_pragma_eol (parser);
12176 switch (code)
12178 case OMP_ATOMIC_READ:
12179 case NOP_EXPR: /* atomic write */
12180 v = c_parser_unary_expression (parser).value;
12181 v = c_fully_fold (v, false, NULL);
12182 if (v == error_mark_node)
12183 goto saw_error;
12184 loc = c_parser_peek_token (parser)->location;
12185 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12186 goto saw_error;
12187 if (code == NOP_EXPR)
12188 lhs = c_parser_expression (parser).value;
12189 else
12190 lhs = c_parser_unary_expression (parser).value;
12191 lhs = c_fully_fold (lhs, false, NULL);
12192 if (lhs == error_mark_node)
12193 goto saw_error;
12194 if (code == NOP_EXPR)
12196 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
12197 opcode. */
12198 code = OMP_ATOMIC;
12199 rhs = lhs;
12200 lhs = v;
12201 v = NULL_TREE;
12203 goto done;
12204 case OMP_ATOMIC_CAPTURE_NEW:
12205 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12207 c_parser_consume_token (parser);
12208 structured_block = true;
12210 else
12212 v = c_parser_unary_expression (parser).value;
12213 v = c_fully_fold (v, false, NULL);
12214 if (v == error_mark_node)
12215 goto saw_error;
12216 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12217 goto saw_error;
12219 break;
12220 default:
12221 break;
12224 /* For structured_block case we don't know yet whether
12225 old or new x should be captured. */
12226 restart:
12227 eloc = c_parser_peek_token (parser)->location;
12228 expr = c_parser_unary_expression (parser);
12229 lhs = expr.value;
12230 expr = default_function_array_conversion (eloc, expr);
12231 unfolded_lhs = expr.value;
12232 lhs = c_fully_fold (lhs, false, NULL);
12233 orig_lhs = lhs;
12234 switch (TREE_CODE (lhs))
12236 case ERROR_MARK:
12237 saw_error:
12238 c_parser_skip_to_end_of_block_or_statement (parser);
12239 if (structured_block)
12241 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12242 c_parser_consume_token (parser);
12243 else if (code == OMP_ATOMIC_CAPTURE_NEW)
12245 c_parser_skip_to_end_of_block_or_statement (parser);
12246 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12247 c_parser_consume_token (parser);
12250 return;
12252 case POSTINCREMENT_EXPR:
12253 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12254 code = OMP_ATOMIC_CAPTURE_OLD;
12255 /* FALLTHROUGH */
12256 case PREINCREMENT_EXPR:
12257 lhs = TREE_OPERAND (lhs, 0);
12258 unfolded_lhs = NULL_TREE;
12259 opcode = PLUS_EXPR;
12260 rhs = integer_one_node;
12261 break;
12263 case POSTDECREMENT_EXPR:
12264 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12265 code = OMP_ATOMIC_CAPTURE_OLD;
12266 /* FALLTHROUGH */
12267 case PREDECREMENT_EXPR:
12268 lhs = TREE_OPERAND (lhs, 0);
12269 unfolded_lhs = NULL_TREE;
12270 opcode = MINUS_EXPR;
12271 rhs = integer_one_node;
12272 break;
12274 case COMPOUND_EXPR:
12275 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
12276 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
12277 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
12278 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
12279 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
12280 (TREE_OPERAND (lhs, 1), 0), 0)))
12281 == BOOLEAN_TYPE)
12282 /* Undo effects of boolean_increment for post {in,de}crement. */
12283 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
12284 /* FALLTHRU */
12285 case MODIFY_EXPR:
12286 if (TREE_CODE (lhs) == MODIFY_EXPR
12287 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
12289 /* Undo effects of boolean_increment. */
12290 if (integer_onep (TREE_OPERAND (lhs, 1)))
12292 /* This is pre or post increment. */
12293 rhs = TREE_OPERAND (lhs, 1);
12294 lhs = TREE_OPERAND (lhs, 0);
12295 unfolded_lhs = NULL_TREE;
12296 opcode = NOP_EXPR;
12297 if (code == OMP_ATOMIC_CAPTURE_NEW
12298 && !structured_block
12299 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12300 code = OMP_ATOMIC_CAPTURE_OLD;
12301 break;
12303 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
12304 && TREE_OPERAND (lhs, 0)
12305 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
12307 /* This is pre or post decrement. */
12308 rhs = TREE_OPERAND (lhs, 1);
12309 lhs = TREE_OPERAND (lhs, 0);
12310 unfolded_lhs = NULL_TREE;
12311 opcode = NOP_EXPR;
12312 if (code == OMP_ATOMIC_CAPTURE_NEW
12313 && !structured_block
12314 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12315 code = OMP_ATOMIC_CAPTURE_OLD;
12316 break;
12319 /* FALLTHRU */
12320 default:
12321 switch (c_parser_peek_token (parser)->type)
12323 case CPP_MULT_EQ:
12324 opcode = MULT_EXPR;
12325 break;
12326 case CPP_DIV_EQ:
12327 opcode = TRUNC_DIV_EXPR;
12328 break;
12329 case CPP_PLUS_EQ:
12330 opcode = PLUS_EXPR;
12331 break;
12332 case CPP_MINUS_EQ:
12333 opcode = MINUS_EXPR;
12334 break;
12335 case CPP_LSHIFT_EQ:
12336 opcode = LSHIFT_EXPR;
12337 break;
12338 case CPP_RSHIFT_EQ:
12339 opcode = RSHIFT_EXPR;
12340 break;
12341 case CPP_AND_EQ:
12342 opcode = BIT_AND_EXPR;
12343 break;
12344 case CPP_OR_EQ:
12345 opcode = BIT_IOR_EXPR;
12346 break;
12347 case CPP_XOR_EQ:
12348 opcode = BIT_XOR_EXPR;
12349 break;
12350 case CPP_EQ:
12351 c_parser_consume_token (parser);
12352 eloc = c_parser_peek_token (parser)->location;
12353 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
12354 rhs1 = expr.value;
12355 switch (TREE_CODE (rhs1))
12357 case MULT_EXPR:
12358 case TRUNC_DIV_EXPR:
12359 case PLUS_EXPR:
12360 case MINUS_EXPR:
12361 case LSHIFT_EXPR:
12362 case RSHIFT_EXPR:
12363 case BIT_AND_EXPR:
12364 case BIT_IOR_EXPR:
12365 case BIT_XOR_EXPR:
12366 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
12368 opcode = TREE_CODE (rhs1);
12369 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12370 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12371 goto stmt_done;
12373 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
12375 opcode = TREE_CODE (rhs1);
12376 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12377 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12378 swapped = !commutative_tree_code (opcode);
12379 goto stmt_done;
12381 break;
12382 case ERROR_MARK:
12383 goto saw_error;
12384 default:
12385 break;
12387 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
12389 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12391 code = OMP_ATOMIC_CAPTURE_OLD;
12392 v = lhs;
12393 lhs = NULL_TREE;
12394 expr = default_function_array_read_conversion (eloc, expr);
12395 unfolded_lhs1 = expr.value;
12396 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
12397 rhs1 = NULL_TREE;
12398 c_parser_consume_token (parser);
12399 goto restart;
12401 if (structured_block)
12403 opcode = NOP_EXPR;
12404 expr = default_function_array_read_conversion (eloc, expr);
12405 rhs = c_fully_fold (expr.value, false, NULL);
12406 rhs1 = NULL_TREE;
12407 goto stmt_done;
12410 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
12411 goto saw_error;
12412 default:
12413 c_parser_error (parser,
12414 "invalid operator for %<#pragma omp atomic%>");
12415 goto saw_error;
12418 /* Arrange to pass the location of the assignment operator to
12419 c_finish_omp_atomic. */
12420 loc = c_parser_peek_token (parser)->location;
12421 c_parser_consume_token (parser);
12422 eloc = c_parser_peek_token (parser)->location;
12423 expr = c_parser_expression (parser);
12424 expr = default_function_array_read_conversion (eloc, expr);
12425 rhs = expr.value;
12426 rhs = c_fully_fold (rhs, false, NULL);
12427 break;
12429 stmt_done:
12430 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12432 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
12433 goto saw_error;
12434 v = c_parser_unary_expression (parser).value;
12435 v = c_fully_fold (v, false, NULL);
12436 if (v == error_mark_node)
12437 goto saw_error;
12438 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12439 goto saw_error;
12440 eloc = c_parser_peek_token (parser)->location;
12441 expr = c_parser_unary_expression (parser);
12442 lhs1 = expr.value;
12443 expr = default_function_array_read_conversion (eloc, expr);
12444 unfolded_lhs1 = expr.value;
12445 lhs1 = c_fully_fold (lhs1, false, NULL);
12446 if (lhs1 == error_mark_node)
12447 goto saw_error;
12449 if (structured_block)
12451 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12452 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
12454 done:
12455 if (unfolded_lhs && unfolded_lhs1
12456 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
12458 error ("%<#pragma omp atomic capture%> uses two different "
12459 "expressions for memory");
12460 stmt = error_mark_node;
12462 else
12463 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
12464 swapped, seq_cst);
12465 if (stmt != error_mark_node)
12466 add_stmt (stmt);
12468 if (!structured_block)
12469 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12473 /* OpenMP 2.5:
12474 # pragma omp barrier new-line
12477 static void
12478 c_parser_omp_barrier (c_parser *parser)
12480 location_t loc = c_parser_peek_token (parser)->location;
12481 c_parser_consume_pragma (parser);
12482 c_parser_skip_to_pragma_eol (parser);
12484 c_finish_omp_barrier (loc);
12487 /* OpenMP 2.5:
12488 # pragma omp critical [(name)] new-line
12489 structured-block
12491 LOC is the location of the #pragma itself. */
12493 static tree
12494 c_parser_omp_critical (location_t loc, c_parser *parser)
12496 tree stmt, name = NULL;
12498 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12500 c_parser_consume_token (parser);
12501 if (c_parser_next_token_is (parser, CPP_NAME))
12503 name = c_parser_peek_token (parser)->value;
12504 c_parser_consume_token (parser);
12505 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12507 else
12508 c_parser_error (parser, "expected identifier");
12510 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12511 c_parser_error (parser, "expected %<(%> or end of line");
12512 c_parser_skip_to_pragma_eol (parser);
12514 stmt = c_parser_omp_structured_block (parser);
12515 return c_finish_omp_critical (loc, stmt, name);
12518 /* OpenMP 2.5:
12519 # pragma omp flush flush-vars[opt] new-line
12521 flush-vars:
12522 ( variable-list ) */
12524 static void
12525 c_parser_omp_flush (c_parser *parser)
12527 location_t loc = c_parser_peek_token (parser)->location;
12528 c_parser_consume_pragma (parser);
12529 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12530 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12531 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12532 c_parser_error (parser, "expected %<(%> or end of line");
12533 c_parser_skip_to_pragma_eol (parser);
12535 c_finish_omp_flush (loc);
12538 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
12539 The real trick here is to determine the loop control variable early
12540 so that we can push a new decl if necessary to make it private.
12541 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
12542 respectively. */
12544 static tree
12545 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
12546 tree clauses, tree *cclauses)
12548 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
12549 tree declv, condv, incrv, initv, ret = NULL;
12550 bool fail = false, open_brace_parsed = false;
12551 int i, collapse = 1, nbraces = 0;
12552 location_t for_loc;
12553 vec<tree, va_gc> *for_block = make_tree_vector ();
12555 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
12556 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
12557 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
12559 gcc_assert (collapse >= 1);
12561 declv = make_tree_vec (collapse);
12562 initv = make_tree_vec (collapse);
12563 condv = make_tree_vec (collapse);
12564 incrv = make_tree_vec (collapse);
12566 if (code != CILK_FOR
12567 && !c_parser_next_token_is_keyword (parser, RID_FOR))
12569 c_parser_error (parser, "for statement expected");
12570 return NULL;
12572 if (code == CILK_FOR
12573 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
12575 c_parser_error (parser, "_Cilk_for statement expected");
12576 return NULL;
12578 for_loc = c_parser_peek_token (parser)->location;
12579 c_parser_consume_token (parser);
12581 for (i = 0; i < collapse; i++)
12583 int bracecount = 0;
12585 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12586 goto pop_scopes;
12588 /* Parse the initialization declaration or expression. */
12589 if (c_parser_next_tokens_start_declaration (parser))
12591 if (i > 0)
12592 vec_safe_push (for_block, c_begin_compound_stmt (true));
12593 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12594 NULL, vNULL);
12595 decl = check_for_loop_decls (for_loc, flag_isoc99);
12596 if (decl == NULL)
12597 goto error_init;
12598 if (DECL_INITIAL (decl) == error_mark_node)
12599 decl = error_mark_node;
12600 init = decl;
12602 else if (c_parser_next_token_is (parser, CPP_NAME)
12603 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
12605 struct c_expr decl_exp;
12606 struct c_expr init_exp;
12607 location_t init_loc;
12609 decl_exp = c_parser_postfix_expression (parser);
12610 decl = decl_exp.value;
12612 c_parser_require (parser, CPP_EQ, "expected %<=%>");
12614 init_loc = c_parser_peek_token (parser)->location;
12615 init_exp = c_parser_expr_no_commas (parser, NULL);
12616 init_exp = default_function_array_read_conversion (init_loc,
12617 init_exp);
12618 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
12619 NOP_EXPR, init_loc, init_exp.value,
12620 init_exp.original_type);
12621 init = c_process_expr_stmt (init_loc, init);
12623 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12625 else
12627 error_init:
12628 c_parser_error (parser,
12629 "expected iteration declaration or initialization");
12630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12631 "expected %<)%>");
12632 fail = true;
12633 goto parse_next;
12636 /* Parse the loop condition. */
12637 cond = NULL_TREE;
12638 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
12640 location_t cond_loc = c_parser_peek_token (parser)->location;
12641 struct c_expr cond_expr
12642 = c_parser_binary_expression (parser, NULL, NULL_TREE);
12644 cond = cond_expr.value;
12645 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
12646 cond = c_fully_fold (cond, false, NULL);
12647 switch (cond_expr.original_code)
12649 case GT_EXPR:
12650 case GE_EXPR:
12651 case LT_EXPR:
12652 case LE_EXPR:
12653 break;
12654 case NE_EXPR:
12655 if (code == CILK_SIMD || code == CILK_FOR)
12656 break;
12657 /* FALLTHRU. */
12658 default:
12659 /* Can't be cond = error_mark_node, because we want to preserve
12660 the location until c_finish_omp_for. */
12661 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
12662 break;
12664 protected_set_expr_location (cond, cond_loc);
12666 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12668 /* Parse the increment expression. */
12669 incr = NULL_TREE;
12670 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
12672 location_t incr_loc = c_parser_peek_token (parser)->location;
12674 incr = c_process_expr_stmt (incr_loc,
12675 c_parser_expression (parser).value);
12677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12679 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
12680 fail = true;
12681 else
12683 TREE_VEC_ELT (declv, i) = decl;
12684 TREE_VEC_ELT (initv, i) = init;
12685 TREE_VEC_ELT (condv, i) = cond;
12686 TREE_VEC_ELT (incrv, i) = incr;
12689 parse_next:
12690 if (i == collapse - 1)
12691 break;
12693 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
12694 in between the collapsed for loops to be still considered perfectly
12695 nested. Hopefully the final version clarifies this.
12696 For now handle (multiple) {'s and empty statements. */
12699 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12701 c_parser_consume_token (parser);
12702 break;
12704 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12706 c_parser_consume_token (parser);
12707 bracecount++;
12709 else if (bracecount
12710 && c_parser_next_token_is (parser, CPP_SEMICOLON))
12711 c_parser_consume_token (parser);
12712 else
12714 c_parser_error (parser, "not enough perfectly nested loops");
12715 if (bracecount)
12717 open_brace_parsed = true;
12718 bracecount--;
12720 fail = true;
12721 collapse = 0;
12722 break;
12725 while (1);
12727 nbraces += bracecount;
12730 save_break = c_break_label;
12731 if (code == CILK_SIMD)
12732 c_break_label = build_int_cst (size_type_node, 2);
12733 else
12734 c_break_label = size_one_node;
12735 save_cont = c_cont_label;
12736 c_cont_label = NULL_TREE;
12737 body = push_stmt_list ();
12739 if (open_brace_parsed)
12741 location_t here = c_parser_peek_token (parser)->location;
12742 stmt = c_begin_compound_stmt (true);
12743 c_parser_compound_statement_nostart (parser);
12744 add_stmt (c_end_compound_stmt (here, stmt, true));
12746 else
12747 add_stmt (c_parser_c99_block_statement (parser));
12748 if (c_cont_label)
12750 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
12751 SET_EXPR_LOCATION (t, loc);
12752 add_stmt (t);
12755 body = pop_stmt_list (body);
12756 c_break_label = save_break;
12757 c_cont_label = save_cont;
12759 while (nbraces)
12761 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12763 c_parser_consume_token (parser);
12764 nbraces--;
12766 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
12767 c_parser_consume_token (parser);
12768 else
12770 c_parser_error (parser, "collapsed loops not perfectly nested");
12771 while (nbraces)
12773 location_t here = c_parser_peek_token (parser)->location;
12774 stmt = c_begin_compound_stmt (true);
12775 add_stmt (body);
12776 c_parser_compound_statement_nostart (parser);
12777 body = c_end_compound_stmt (here, stmt, true);
12778 nbraces--;
12780 goto pop_scopes;
12784 /* Only bother calling c_finish_omp_for if we haven't already generated
12785 an error from the initialization parsing. */
12786 if (!fail)
12788 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
12789 incrv, body, NULL);
12790 if (stmt)
12792 if (cclauses != NULL
12793 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
12795 gcc_assert (code != OACC_LOOP);
12796 tree *c;
12797 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
12798 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
12799 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
12800 c = &OMP_CLAUSE_CHAIN (*c);
12801 else
12803 for (i = 0; i < collapse; i++)
12804 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
12805 break;
12806 if (i == collapse)
12807 c = &OMP_CLAUSE_CHAIN (*c);
12808 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
12810 error_at (loc,
12811 "iteration variable %qD should not be firstprivate",
12812 OMP_CLAUSE_DECL (*c));
12813 *c = OMP_CLAUSE_CHAIN (*c);
12815 else
12817 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
12818 change it to shared (decl) in
12819 OMP_PARALLEL_CLAUSES. */
12820 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
12821 OMP_CLAUSE_LASTPRIVATE);
12822 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
12823 if (code == OMP_SIMD)
12825 OMP_CLAUSE_CHAIN (l)
12826 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12827 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
12829 else
12831 OMP_CLAUSE_CHAIN (l) = clauses;
12832 clauses = l;
12834 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
12838 OMP_FOR_CLAUSES (stmt) = clauses;
12840 ret = stmt;
12842 pop_scopes:
12843 while (!for_block->is_empty ())
12845 /* FIXME diagnostics: LOC below should be the actual location of
12846 this particular for block. We need to build a list of
12847 locations to go along with FOR_BLOCK. */
12848 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
12849 add_stmt (stmt);
12851 release_tree_vector (for_block);
12852 return ret;
12855 /* Helper function for OpenMP parsing, split clauses and call
12856 finish_omp_clauses on each of the set of clauses afterwards. */
12858 static void
12859 omp_split_clauses (location_t loc, enum tree_code code,
12860 omp_clause_mask mask, tree clauses, tree *cclauses)
12862 int i;
12863 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
12864 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
12865 if (cclauses[i])
12866 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
12869 /* OpenMP 4.0:
12870 #pragma omp simd simd-clause[optseq] new-line
12871 for-loop
12873 LOC is the location of the #pragma token.
12876 #define OMP_SIMD_CLAUSE_MASK \
12877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
12878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12885 static tree
12886 c_parser_omp_simd (location_t loc, c_parser *parser,
12887 char *p_name, omp_clause_mask mask, tree *cclauses)
12889 tree block, clauses, ret;
12891 strcat (p_name, " simd");
12892 mask |= OMP_SIMD_CLAUSE_MASK;
12893 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
12895 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12896 if (cclauses)
12898 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
12899 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
12902 block = c_begin_compound_stmt (true);
12903 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
12904 block = c_end_compound_stmt (loc, block, true);
12905 add_stmt (block);
12907 return ret;
12910 /* OpenMP 2.5:
12911 #pragma omp for for-clause[optseq] new-line
12912 for-loop
12914 OpenMP 4.0:
12915 #pragma omp for simd for-simd-clause[optseq] new-line
12916 for-loop
12918 LOC is the location of the #pragma token.
12921 #define OMP_FOR_CLAUSE_MASK \
12922 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
12927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
12928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
12929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12931 static tree
12932 c_parser_omp_for (location_t loc, c_parser *parser,
12933 char *p_name, omp_clause_mask mask, tree *cclauses)
12935 tree block, clauses, ret;
12937 strcat (p_name, " for");
12938 mask |= OMP_FOR_CLAUSE_MASK;
12939 if (cclauses)
12940 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12942 if (c_parser_next_token_is (parser, CPP_NAME))
12944 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12946 if (strcmp (p, "simd") == 0)
12948 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12949 if (cclauses == NULL)
12950 cclauses = cclauses_buf;
12952 c_parser_consume_token (parser);
12953 if (!flag_openmp) /* flag_openmp_simd */
12954 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12955 block = c_begin_compound_stmt (true);
12956 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12957 block = c_end_compound_stmt (loc, block, true);
12958 if (ret == NULL_TREE)
12959 return ret;
12960 ret = make_node (OMP_FOR);
12961 TREE_TYPE (ret) = void_type_node;
12962 OMP_FOR_BODY (ret) = block;
12963 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12964 SET_EXPR_LOCATION (ret, loc);
12965 add_stmt (ret);
12966 return ret;
12969 if (!flag_openmp) /* flag_openmp_simd */
12971 c_parser_skip_to_pragma_eol (parser);
12972 return NULL_TREE;
12975 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12976 if (cclauses)
12978 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12979 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12982 block = c_begin_compound_stmt (true);
12983 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12984 block = c_end_compound_stmt (loc, block, true);
12985 add_stmt (block);
12987 return ret;
12990 /* OpenMP 2.5:
12991 # pragma omp master new-line
12992 structured-block
12994 LOC is the location of the #pragma token.
12997 static tree
12998 c_parser_omp_master (location_t loc, c_parser *parser)
13000 c_parser_skip_to_pragma_eol (parser);
13001 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
13004 /* OpenMP 2.5:
13005 # pragma omp ordered new-line
13006 structured-block
13008 LOC is the location of the #pragma itself.
13011 static tree
13012 c_parser_omp_ordered (location_t loc, c_parser *parser)
13014 c_parser_skip_to_pragma_eol (parser);
13015 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
13018 /* OpenMP 2.5:
13020 section-scope:
13021 { section-sequence }
13023 section-sequence:
13024 section-directive[opt] structured-block
13025 section-sequence section-directive structured-block
13027 SECTIONS_LOC is the location of the #pragma omp sections. */
13029 static tree
13030 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
13032 tree stmt, substmt;
13033 bool error_suppress = false;
13034 location_t loc;
13036 loc = c_parser_peek_token (parser)->location;
13037 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
13039 /* Avoid skipping until the end of the block. */
13040 parser->error = false;
13041 return NULL_TREE;
13044 stmt = push_stmt_list ();
13046 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
13048 substmt = c_parser_omp_structured_block (parser);
13049 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13050 SET_EXPR_LOCATION (substmt, loc);
13051 add_stmt (substmt);
13054 while (1)
13056 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13057 break;
13058 if (c_parser_next_token_is (parser, CPP_EOF))
13059 break;
13061 loc = c_parser_peek_token (parser)->location;
13062 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
13064 c_parser_consume_pragma (parser);
13065 c_parser_skip_to_pragma_eol (parser);
13066 error_suppress = false;
13068 else if (!error_suppress)
13070 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
13071 error_suppress = true;
13074 substmt = c_parser_omp_structured_block (parser);
13075 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13076 SET_EXPR_LOCATION (substmt, loc);
13077 add_stmt (substmt);
13079 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
13080 "expected %<#pragma omp section%> or %<}%>");
13082 substmt = pop_stmt_list (stmt);
13084 stmt = make_node (OMP_SECTIONS);
13085 SET_EXPR_LOCATION (stmt, sections_loc);
13086 TREE_TYPE (stmt) = void_type_node;
13087 OMP_SECTIONS_BODY (stmt) = substmt;
13089 return add_stmt (stmt);
13092 /* OpenMP 2.5:
13093 # pragma omp sections sections-clause[optseq] newline
13094 sections-scope
13096 LOC is the location of the #pragma token.
13099 #define OMP_SECTIONS_CLAUSE_MASK \
13100 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13106 static tree
13107 c_parser_omp_sections (location_t loc, c_parser *parser,
13108 char *p_name, omp_clause_mask mask, tree *cclauses)
13110 tree block, clauses, ret;
13112 strcat (p_name, " sections");
13113 mask |= OMP_SECTIONS_CLAUSE_MASK;
13114 if (cclauses)
13115 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13117 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13118 if (cclauses)
13120 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
13121 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
13124 block = c_begin_compound_stmt (true);
13125 ret = c_parser_omp_sections_scope (loc, parser);
13126 if (ret)
13127 OMP_SECTIONS_CLAUSES (ret) = clauses;
13128 block = c_end_compound_stmt (loc, block, true);
13129 add_stmt (block);
13131 return ret;
13134 /* OpenMP 2.5:
13135 # pragma omp parallel parallel-clause[optseq] new-line
13136 structured-block
13137 # pragma omp parallel for parallel-for-clause[optseq] new-line
13138 structured-block
13139 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
13140 structured-block
13142 OpenMP 4.0:
13143 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
13144 structured-block
13146 LOC is the location of the #pragma token.
13149 #define OMP_PARALLEL_CLAUSE_MASK \
13150 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
13156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
13158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
13160 static tree
13161 c_parser_omp_parallel (location_t loc, c_parser *parser,
13162 char *p_name, omp_clause_mask mask, tree *cclauses)
13164 tree stmt, clauses, block;
13166 strcat (p_name, " parallel");
13167 mask |= OMP_PARALLEL_CLAUSE_MASK;
13169 if (c_parser_next_token_is_keyword (parser, RID_FOR))
13171 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13172 if (cclauses == NULL)
13173 cclauses = cclauses_buf;
13175 c_parser_consume_token (parser);
13176 if (!flag_openmp) /* flag_openmp_simd */
13177 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13178 block = c_begin_omp_parallel ();
13179 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13180 stmt
13181 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13182 block);
13183 if (ret == NULL_TREE)
13184 return ret;
13185 OMP_PARALLEL_COMBINED (stmt) = 1;
13186 return stmt;
13188 else if (cclauses)
13190 error_at (loc, "expected %<for%> after %qs", p_name);
13191 c_parser_skip_to_pragma_eol (parser);
13192 return NULL_TREE;
13194 else if (!flag_openmp) /* flag_openmp_simd */
13196 c_parser_skip_to_pragma_eol (parser);
13197 return NULL_TREE;
13199 else if (c_parser_next_token_is (parser, CPP_NAME))
13201 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13202 if (strcmp (p, "sections") == 0)
13204 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13205 if (cclauses == NULL)
13206 cclauses = cclauses_buf;
13208 c_parser_consume_token (parser);
13209 block = c_begin_omp_parallel ();
13210 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
13211 stmt = c_finish_omp_parallel (loc,
13212 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13213 block);
13214 OMP_PARALLEL_COMBINED (stmt) = 1;
13215 return stmt;
13219 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13221 block = c_begin_omp_parallel ();
13222 c_parser_statement (parser);
13223 stmt = c_finish_omp_parallel (loc, clauses, block);
13225 return stmt;
13228 /* OpenMP 2.5:
13229 # pragma omp single single-clause[optseq] new-line
13230 structured-block
13232 LOC is the location of the #pragma.
13235 #define OMP_SINGLE_CLAUSE_MASK \
13236 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
13239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13241 static tree
13242 c_parser_omp_single (location_t loc, c_parser *parser)
13244 tree stmt = make_node (OMP_SINGLE);
13245 SET_EXPR_LOCATION (stmt, loc);
13246 TREE_TYPE (stmt) = void_type_node;
13248 OMP_SINGLE_CLAUSES (stmt)
13249 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
13250 "#pragma omp single");
13251 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
13253 return add_stmt (stmt);
13256 /* OpenMP 3.0:
13257 # pragma omp task task-clause[optseq] new-line
13259 LOC is the location of the #pragma.
13262 #define OMP_TASK_CLAUSE_MASK \
13263 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
13265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
13270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
13271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
13273 static tree
13274 c_parser_omp_task (location_t loc, c_parser *parser)
13276 tree clauses, block;
13278 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
13279 "#pragma omp task");
13281 block = c_begin_omp_task ();
13282 c_parser_statement (parser);
13283 return c_finish_omp_task (loc, clauses, block);
13286 /* OpenMP 3.0:
13287 # pragma omp taskwait new-line
13290 static void
13291 c_parser_omp_taskwait (c_parser *parser)
13293 location_t loc = c_parser_peek_token (parser)->location;
13294 c_parser_consume_pragma (parser);
13295 c_parser_skip_to_pragma_eol (parser);
13297 c_finish_omp_taskwait (loc);
13300 /* OpenMP 3.1:
13301 # pragma omp taskyield new-line
13304 static void
13305 c_parser_omp_taskyield (c_parser *parser)
13307 location_t loc = c_parser_peek_token (parser)->location;
13308 c_parser_consume_pragma (parser);
13309 c_parser_skip_to_pragma_eol (parser);
13311 c_finish_omp_taskyield (loc);
13314 /* OpenMP 4.0:
13315 # pragma omp taskgroup new-line
13318 static tree
13319 c_parser_omp_taskgroup (c_parser *parser)
13321 location_t loc = c_parser_peek_token (parser)->location;
13322 c_parser_skip_to_pragma_eol (parser);
13323 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
13326 /* OpenMP 4.0:
13327 # pragma omp cancel cancel-clause[optseq] new-line
13329 LOC is the location of the #pragma.
13332 #define OMP_CANCEL_CLAUSE_MASK \
13333 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
13337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13339 static void
13340 c_parser_omp_cancel (c_parser *parser)
13342 location_t loc = c_parser_peek_token (parser)->location;
13344 c_parser_consume_pragma (parser);
13345 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
13346 "#pragma omp cancel");
13348 c_finish_omp_cancel (loc, clauses);
13351 /* OpenMP 4.0:
13352 # pragma omp cancellation point cancelpt-clause[optseq] new-line
13354 LOC is the location of the #pragma.
13357 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
13358 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
13363 static void
13364 c_parser_omp_cancellation_point (c_parser *parser)
13366 location_t loc = c_parser_peek_token (parser)->location;
13367 tree clauses;
13368 bool point_seen = false;
13370 c_parser_consume_pragma (parser);
13371 if (c_parser_next_token_is (parser, CPP_NAME))
13373 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13374 if (strcmp (p, "point") == 0)
13376 c_parser_consume_token (parser);
13377 point_seen = true;
13380 if (!point_seen)
13382 c_parser_error (parser, "expected %<point%>");
13383 c_parser_skip_to_pragma_eol (parser);
13384 return;
13387 clauses
13388 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
13389 "#pragma omp cancellation point");
13391 c_finish_omp_cancellation_point (loc, clauses);
13394 /* OpenMP 4.0:
13395 #pragma omp distribute distribute-clause[optseq] new-line
13396 for-loop */
13398 #define OMP_DISTRIBUTE_CLAUSE_MASK \
13399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
13402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13404 static tree
13405 c_parser_omp_distribute (location_t loc, c_parser *parser,
13406 char *p_name, omp_clause_mask mask, tree *cclauses)
13408 tree clauses, block, ret;
13410 strcat (p_name, " distribute");
13411 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
13413 if (c_parser_next_token_is (parser, CPP_NAME))
13415 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13416 bool simd = false;
13417 bool parallel = false;
13419 if (strcmp (p, "simd") == 0)
13420 simd = true;
13421 else
13422 parallel = strcmp (p, "parallel") == 0;
13423 if (parallel || simd)
13425 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13426 if (cclauses == NULL)
13427 cclauses = cclauses_buf;
13428 c_parser_consume_token (parser);
13429 if (!flag_openmp) /* flag_openmp_simd */
13431 if (simd)
13432 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13433 else
13434 return c_parser_omp_parallel (loc, parser, p_name, mask,
13435 cclauses);
13437 block = c_begin_compound_stmt (true);
13438 if (simd)
13439 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13440 else
13441 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
13442 block = c_end_compound_stmt (loc, block, true);
13443 if (ret == NULL)
13444 return ret;
13445 ret = make_node (OMP_DISTRIBUTE);
13446 TREE_TYPE (ret) = void_type_node;
13447 OMP_FOR_BODY (ret) = block;
13448 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13449 SET_EXPR_LOCATION (ret, loc);
13450 add_stmt (ret);
13451 return ret;
13454 if (!flag_openmp) /* flag_openmp_simd */
13456 c_parser_skip_to_pragma_eol (parser);
13457 return NULL_TREE;
13460 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13461 if (cclauses)
13463 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
13464 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13467 block = c_begin_compound_stmt (true);
13468 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
13469 block = c_end_compound_stmt (loc, block, true);
13470 add_stmt (block);
13472 return ret;
13475 /* OpenMP 4.0:
13476 # pragma omp teams teams-clause[optseq] new-line
13477 structured-block */
13479 #define OMP_TEAMS_CLAUSE_MASK \
13480 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
13485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
13486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
13488 static tree
13489 c_parser_omp_teams (location_t loc, c_parser *parser,
13490 char *p_name, omp_clause_mask mask, tree *cclauses)
13492 tree clauses, block, ret;
13494 strcat (p_name, " teams");
13495 mask |= OMP_TEAMS_CLAUSE_MASK;
13497 if (c_parser_next_token_is (parser, CPP_NAME))
13499 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13500 if (strcmp (p, "distribute") == 0)
13502 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13503 if (cclauses == NULL)
13504 cclauses = cclauses_buf;
13506 c_parser_consume_token (parser);
13507 if (!flag_openmp) /* flag_openmp_simd */
13508 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13509 block = c_begin_compound_stmt (true);
13510 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13511 block = c_end_compound_stmt (loc, block, true);
13512 if (ret == NULL)
13513 return ret;
13514 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13515 ret = make_node (OMP_TEAMS);
13516 TREE_TYPE (ret) = void_type_node;
13517 OMP_TEAMS_CLAUSES (ret) = clauses;
13518 OMP_TEAMS_BODY (ret) = block;
13519 return add_stmt (ret);
13522 if (!flag_openmp) /* flag_openmp_simd */
13524 c_parser_skip_to_pragma_eol (parser);
13525 return NULL_TREE;
13528 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13529 if (cclauses)
13531 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
13532 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13535 tree stmt = make_node (OMP_TEAMS);
13536 TREE_TYPE (stmt) = void_type_node;
13537 OMP_TEAMS_CLAUSES (stmt) = clauses;
13538 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
13540 return add_stmt (stmt);
13543 /* OpenMP 4.0:
13544 # pragma omp target data target-data-clause[optseq] new-line
13545 structured-block */
13547 #define OMP_TARGET_DATA_CLAUSE_MASK \
13548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13552 static tree
13553 c_parser_omp_target_data (location_t loc, c_parser *parser)
13555 tree stmt = make_node (OMP_TARGET_DATA);
13556 TREE_TYPE (stmt) = void_type_node;
13558 OMP_TARGET_DATA_CLAUSES (stmt)
13559 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
13560 "#pragma omp target data");
13561 keep_next_level ();
13562 tree block = c_begin_compound_stmt (true);
13563 add_stmt (c_parser_omp_structured_block (parser));
13564 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13566 SET_EXPR_LOCATION (stmt, loc);
13567 return add_stmt (stmt);
13570 /* OpenMP 4.0:
13571 # pragma omp target update target-update-clause[optseq] new-line */
13573 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
13574 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
13575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
13576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13579 static bool
13580 c_parser_omp_target_update (location_t loc, c_parser *parser,
13581 enum pragma_context context)
13583 if (context == pragma_stmt)
13585 error_at (loc,
13586 "%<#pragma omp target update%> may only be "
13587 "used in compound statements");
13588 c_parser_skip_to_pragma_eol (parser);
13589 return false;
13592 tree clauses
13593 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
13594 "#pragma omp target update");
13595 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
13596 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
13598 error_at (loc,
13599 "%<#pragma omp target update must contain at least one "
13600 "%<from%> or %<to%> clauses");
13601 return false;
13604 tree stmt = make_node (OMP_TARGET_UPDATE);
13605 TREE_TYPE (stmt) = void_type_node;
13606 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
13607 SET_EXPR_LOCATION (stmt, loc);
13608 add_stmt (stmt);
13609 return false;
13612 /* OpenMP 4.0:
13613 # pragma omp target target-clause[optseq] new-line
13614 structured-block */
13616 #define OMP_TARGET_CLAUSE_MASK \
13617 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13621 static bool
13622 c_parser_omp_target (c_parser *parser, enum pragma_context context)
13624 location_t loc = c_parser_peek_token (parser)->location;
13625 c_parser_consume_pragma (parser);
13627 if (context != pragma_stmt && context != pragma_compound)
13629 c_parser_error (parser, "expected declaration specifiers");
13630 c_parser_skip_to_pragma_eol (parser);
13631 return false;
13634 if (c_parser_next_token_is (parser, CPP_NAME))
13636 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13638 if (strcmp (p, "teams") == 0)
13640 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
13641 char p_name[sizeof ("#pragma omp target teams distribute "
13642 "parallel for simd")];
13644 c_parser_consume_token (parser);
13645 strcpy (p_name, "#pragma omp target");
13646 if (!flag_openmp) /* flag_openmp_simd */
13648 tree stmt = c_parser_omp_teams (loc, parser, p_name,
13649 OMP_TARGET_CLAUSE_MASK,
13650 cclauses);
13651 return stmt != NULL_TREE;
13653 keep_next_level ();
13654 tree block = c_begin_compound_stmt (true);
13655 tree ret = c_parser_omp_teams (loc, parser, p_name,
13656 OMP_TARGET_CLAUSE_MASK, cclauses);
13657 block = c_end_compound_stmt (loc, block, true);
13658 if (ret == NULL_TREE)
13659 return false;
13660 tree stmt = make_node (OMP_TARGET);
13661 TREE_TYPE (stmt) = void_type_node;
13662 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
13663 OMP_TARGET_BODY (stmt) = block;
13664 add_stmt (stmt);
13665 return true;
13667 else if (!flag_openmp) /* flag_openmp_simd */
13669 c_parser_skip_to_pragma_eol (parser);
13670 return false;
13672 else if (strcmp (p, "data") == 0)
13674 c_parser_consume_token (parser);
13675 c_parser_omp_target_data (loc, parser);
13676 return true;
13678 else if (strcmp (p, "update") == 0)
13680 c_parser_consume_token (parser);
13681 return c_parser_omp_target_update (loc, parser, context);
13685 tree stmt = make_node (OMP_TARGET);
13686 TREE_TYPE (stmt) = void_type_node;
13688 OMP_TARGET_CLAUSES (stmt)
13689 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
13690 "#pragma omp target");
13691 keep_next_level ();
13692 tree block = c_begin_compound_stmt (true);
13693 add_stmt (c_parser_omp_structured_block (parser));
13694 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13696 SET_EXPR_LOCATION (stmt, loc);
13697 add_stmt (stmt);
13698 return true;
13701 /* OpenMP 4.0:
13702 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
13704 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
13705 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
13706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
13709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
13710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
13712 static void
13713 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
13715 vec<c_token> clauses = vNULL;
13716 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13718 c_token *token = c_parser_peek_token (parser);
13719 if (token->type == CPP_EOF)
13721 c_parser_skip_to_pragma_eol (parser);
13722 clauses.release ();
13723 return;
13725 clauses.safe_push (*token);
13726 c_parser_consume_token (parser);
13728 clauses.safe_push (*c_parser_peek_token (parser));
13729 c_parser_skip_to_pragma_eol (parser);
13731 while (c_parser_next_token_is (parser, CPP_PRAGMA))
13733 if (c_parser_peek_token (parser)->pragma_kind
13734 != PRAGMA_OMP_DECLARE_REDUCTION
13735 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
13736 || strcmp (IDENTIFIER_POINTER
13737 (c_parser_peek_2nd_token (parser)->value),
13738 "simd") != 0)
13740 c_parser_error (parser,
13741 "%<#pragma omp declare simd%> must be followed by "
13742 "function declaration or definition or another "
13743 "%<#pragma omp declare simd%>");
13744 clauses.release ();
13745 return;
13747 c_parser_consume_pragma (parser);
13748 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13750 c_token *token = c_parser_peek_token (parser);
13751 if (token->type == CPP_EOF)
13753 c_parser_skip_to_pragma_eol (parser);
13754 clauses.release ();
13755 return;
13757 clauses.safe_push (*token);
13758 c_parser_consume_token (parser);
13760 clauses.safe_push (*c_parser_peek_token (parser));
13761 c_parser_skip_to_pragma_eol (parser);
13764 /* Make sure nothing tries to read past the end of the tokens. */
13765 c_token eof_token;
13766 memset (&eof_token, 0, sizeof (eof_token));
13767 eof_token.type = CPP_EOF;
13768 clauses.safe_push (eof_token);
13769 clauses.safe_push (eof_token);
13771 switch (context)
13773 case pragma_external:
13774 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13775 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13777 int ext = disable_extension_diagnostics ();
13779 c_parser_consume_token (parser);
13780 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13781 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13782 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13783 NULL, clauses);
13784 restore_extension_diagnostics (ext);
13786 else
13787 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13788 NULL, clauses);
13789 break;
13790 case pragma_struct:
13791 case pragma_param:
13792 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13793 "function declaration or definition");
13794 break;
13795 case pragma_compound:
13796 case pragma_stmt:
13797 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13798 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13800 int ext = disable_extension_diagnostics ();
13802 c_parser_consume_token (parser);
13803 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13804 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13805 if (c_parser_next_tokens_start_declaration (parser))
13807 c_parser_declaration_or_fndef (parser, true, true, true, true,
13808 true, NULL, clauses);
13809 restore_extension_diagnostics (ext);
13810 break;
13812 restore_extension_diagnostics (ext);
13814 else if (c_parser_next_tokens_start_declaration (parser))
13816 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
13817 NULL, clauses);
13818 break;
13820 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13821 "function declaration or definition");
13822 break;
13823 default:
13824 gcc_unreachable ();
13826 clauses.release ();
13829 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
13830 and put that into "omp declare simd" attribute. */
13832 static void
13833 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
13834 vec<c_token> clauses)
13836 if (flag_cilkplus
13837 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13839 error ("%<#pragma omp declare simd%> cannot be used in the same "
13840 "function marked as a Cilk Plus SIMD-enabled function");
13841 vec_free (parser->cilk_simd_fn_tokens);
13842 return;
13845 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
13846 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
13847 has already processed the tokens. */
13848 if (clauses.exists () && clauses[0].type == CPP_EOF)
13849 return;
13850 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
13852 error ("%<#pragma omp declare simd%> not immediately followed by "
13853 "a function declaration or definition");
13854 clauses[0].type = CPP_EOF;
13855 return;
13857 if (clauses.exists () && clauses[0].type != CPP_NAME)
13859 error_at (DECL_SOURCE_LOCATION (fndecl),
13860 "%<#pragma omp declare simd%> not immediately followed by "
13861 "a single function declaration or definition");
13862 clauses[0].type = CPP_EOF;
13863 return;
13866 if (parms == NULL_TREE)
13867 parms = DECL_ARGUMENTS (fndecl);
13869 unsigned int tokens_avail = parser->tokens_avail;
13870 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13871 bool is_cilkplus_cilk_simd_fn = false;
13873 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13875 parser->tokens = parser->cilk_simd_fn_tokens->address ();
13876 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
13877 is_cilkplus_cilk_simd_fn = true;
13879 else
13881 parser->tokens = clauses.address ();
13882 parser->tokens_avail = clauses.length ();
13885 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
13886 while (parser->tokens_avail > 3)
13888 c_token *token = c_parser_peek_token (parser);
13889 if (!is_cilkplus_cilk_simd_fn)
13890 gcc_assert (token->type == CPP_NAME
13891 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
13892 else
13893 gcc_assert (token->type == CPP_NAME
13894 && is_cilkplus_vector_p (token->value));
13895 c_parser_consume_token (parser);
13896 parser->in_pragma = true;
13898 tree c = NULL_TREE;
13899 if (is_cilkplus_cilk_simd_fn)
13900 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
13901 "SIMD-enabled functions attribute");
13902 else
13903 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
13904 "#pragma omp declare simd");
13905 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
13906 if (c != NULL_TREE)
13907 c = tree_cons (NULL_TREE, c, NULL_TREE);
13908 if (is_cilkplus_cilk_simd_fn)
13910 tree k = build_tree_list (get_identifier ("cilk simd function"),
13911 NULL_TREE);
13912 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
13913 DECL_ATTRIBUTES (fndecl) = k;
13915 c = build_tree_list (get_identifier ("omp declare simd"), c);
13916 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
13917 DECL_ATTRIBUTES (fndecl) = c;
13920 parser->tokens = &parser->tokens_buf[0];
13921 parser->tokens_avail = tokens_avail;
13922 if (clauses.exists ())
13923 clauses[0].type = CPP_PRAGMA;
13925 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13926 vec_free (parser->cilk_simd_fn_tokens);
13930 /* OpenMP 4.0:
13931 # pragma omp declare target new-line
13932 declarations and definitions
13933 # pragma omp end declare target new-line */
13935 static void
13936 c_parser_omp_declare_target (c_parser *parser)
13938 c_parser_skip_to_pragma_eol (parser);
13939 current_omp_declare_target_attribute++;
13942 static void
13943 c_parser_omp_end_declare_target (c_parser *parser)
13945 location_t loc = c_parser_peek_token (parser)->location;
13946 c_parser_consume_pragma (parser);
13947 if (c_parser_next_token_is (parser, CPP_NAME)
13948 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13949 "declare") == 0)
13951 c_parser_consume_token (parser);
13952 if (c_parser_next_token_is (parser, CPP_NAME)
13953 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13954 "target") == 0)
13955 c_parser_consume_token (parser);
13956 else
13958 c_parser_error (parser, "expected %<target%>");
13959 c_parser_skip_to_pragma_eol (parser);
13960 return;
13963 else
13965 c_parser_error (parser, "expected %<declare%>");
13966 c_parser_skip_to_pragma_eol (parser);
13967 return;
13969 c_parser_skip_to_pragma_eol (parser);
13970 if (!current_omp_declare_target_attribute)
13971 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
13972 "%<#pragma omp declare target%>");
13973 else
13974 current_omp_declare_target_attribute--;
13978 /* OpenMP 4.0
13979 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13980 initializer-clause[opt] new-line
13982 initializer-clause:
13983 initializer (omp_priv = initializer)
13984 initializer (function-name (argument-list)) */
13986 static void
13987 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13989 unsigned int tokens_avail = 0, i;
13990 vec<tree> types = vNULL;
13991 vec<c_token> clauses = vNULL;
13992 enum tree_code reduc_code = ERROR_MARK;
13993 tree reduc_id = NULL_TREE;
13994 tree type;
13995 location_t rloc = c_parser_peek_token (parser)->location;
13997 if (context == pragma_struct || context == pragma_param)
13999 error ("%<#pragma omp declare reduction%> not at file or block scope");
14000 goto fail;
14003 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14004 goto fail;
14006 switch (c_parser_peek_token (parser)->type)
14008 case CPP_PLUS:
14009 reduc_code = PLUS_EXPR;
14010 break;
14011 case CPP_MULT:
14012 reduc_code = MULT_EXPR;
14013 break;
14014 case CPP_MINUS:
14015 reduc_code = MINUS_EXPR;
14016 break;
14017 case CPP_AND:
14018 reduc_code = BIT_AND_EXPR;
14019 break;
14020 case CPP_XOR:
14021 reduc_code = BIT_XOR_EXPR;
14022 break;
14023 case CPP_OR:
14024 reduc_code = BIT_IOR_EXPR;
14025 break;
14026 case CPP_AND_AND:
14027 reduc_code = TRUTH_ANDIF_EXPR;
14028 break;
14029 case CPP_OR_OR:
14030 reduc_code = TRUTH_ORIF_EXPR;
14031 break;
14032 case CPP_NAME:
14033 const char *p;
14034 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14035 if (strcmp (p, "min") == 0)
14037 reduc_code = MIN_EXPR;
14038 break;
14040 if (strcmp (p, "max") == 0)
14042 reduc_code = MAX_EXPR;
14043 break;
14045 reduc_id = c_parser_peek_token (parser)->value;
14046 break;
14047 default:
14048 c_parser_error (parser,
14049 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
14050 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
14051 goto fail;
14054 tree orig_reduc_id, reduc_decl;
14055 orig_reduc_id = reduc_id;
14056 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
14057 reduc_decl = c_omp_reduction_decl (reduc_id);
14058 c_parser_consume_token (parser);
14060 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14061 goto fail;
14063 while (true)
14065 location_t loc = c_parser_peek_token (parser)->location;
14066 struct c_type_name *ctype = c_parser_type_name (parser);
14067 if (ctype != NULL)
14069 type = groktypename (ctype, NULL, NULL);
14070 if (type == error_mark_node)
14072 else if ((INTEGRAL_TYPE_P (type)
14073 || TREE_CODE (type) == REAL_TYPE
14074 || TREE_CODE (type) == COMPLEX_TYPE)
14075 && orig_reduc_id == NULL_TREE)
14076 error_at (loc, "predeclared arithmetic type in "
14077 "%<#pragma omp declare reduction%>");
14078 else if (TREE_CODE (type) == FUNCTION_TYPE
14079 || TREE_CODE (type) == ARRAY_TYPE)
14080 error_at (loc, "function or array type in "
14081 "%<#pragma omp declare reduction%>");
14082 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
14083 error_at (loc, "const, volatile or restrict qualified type in "
14084 "%<#pragma omp declare reduction%>");
14085 else
14087 tree t;
14088 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
14089 if (comptypes (TREE_PURPOSE (t), type))
14091 error_at (loc, "redeclaration of %qs "
14092 "%<#pragma omp declare reduction%> for "
14093 "type %qT",
14094 IDENTIFIER_POINTER (reduc_id)
14095 + sizeof ("omp declare reduction ") - 1,
14096 type);
14097 location_t ploc
14098 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
14099 0));
14100 error_at (ploc, "previous %<#pragma omp declare "
14101 "reduction%>");
14102 break;
14104 if (t == NULL_TREE)
14105 types.safe_push (type);
14107 if (c_parser_next_token_is (parser, CPP_COMMA))
14108 c_parser_consume_token (parser);
14109 else
14110 break;
14112 else
14113 break;
14116 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
14117 || types.is_empty ())
14119 fail:
14120 clauses.release ();
14121 types.release ();
14122 while (true)
14124 c_token *token = c_parser_peek_token (parser);
14125 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
14126 break;
14127 c_parser_consume_token (parser);
14129 c_parser_skip_to_pragma_eol (parser);
14130 return;
14133 if (types.length () > 1)
14135 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14137 c_token *token = c_parser_peek_token (parser);
14138 if (token->type == CPP_EOF)
14139 goto fail;
14140 clauses.safe_push (*token);
14141 c_parser_consume_token (parser);
14143 clauses.safe_push (*c_parser_peek_token (parser));
14144 c_parser_skip_to_pragma_eol (parser);
14146 /* Make sure nothing tries to read past the end of the tokens. */
14147 c_token eof_token;
14148 memset (&eof_token, 0, sizeof (eof_token));
14149 eof_token.type = CPP_EOF;
14150 clauses.safe_push (eof_token);
14151 clauses.safe_push (eof_token);
14154 int errs = errorcount;
14155 FOR_EACH_VEC_ELT (types, i, type)
14157 tokens_avail = parser->tokens_avail;
14158 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14159 if (!clauses.is_empty ())
14161 parser->tokens = clauses.address ();
14162 parser->tokens_avail = clauses.length ();
14163 parser->in_pragma = true;
14166 bool nested = current_function_decl != NULL_TREE;
14167 if (nested)
14168 c_push_function_context ();
14169 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
14170 reduc_id, default_function_type);
14171 current_function_decl = fndecl;
14172 allocate_struct_function (fndecl, true);
14173 push_scope ();
14174 tree stmt = push_stmt_list ();
14175 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
14176 warn about these. */
14177 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
14178 get_identifier ("omp_out"), type);
14179 DECL_ARTIFICIAL (omp_out) = 1;
14180 DECL_CONTEXT (omp_out) = fndecl;
14181 pushdecl (omp_out);
14182 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
14183 get_identifier ("omp_in"), type);
14184 DECL_ARTIFICIAL (omp_in) = 1;
14185 DECL_CONTEXT (omp_in) = fndecl;
14186 pushdecl (omp_in);
14187 struct c_expr combiner = c_parser_expression (parser);
14188 struct c_expr initializer;
14189 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
14190 bool bad = false;
14191 initializer.value = error_mark_node;
14192 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14193 bad = true;
14194 else if (c_parser_next_token_is (parser, CPP_NAME)
14195 && strcmp (IDENTIFIER_POINTER
14196 (c_parser_peek_token (parser)->value),
14197 "initializer") == 0)
14199 c_parser_consume_token (parser);
14200 pop_scope ();
14201 push_scope ();
14202 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
14203 get_identifier ("omp_priv"), type);
14204 DECL_ARTIFICIAL (omp_priv) = 1;
14205 DECL_INITIAL (omp_priv) = error_mark_node;
14206 DECL_CONTEXT (omp_priv) = fndecl;
14207 pushdecl (omp_priv);
14208 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
14209 get_identifier ("omp_orig"), type);
14210 DECL_ARTIFICIAL (omp_orig) = 1;
14211 DECL_CONTEXT (omp_orig) = fndecl;
14212 pushdecl (omp_orig);
14213 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14214 bad = true;
14215 else if (!c_parser_next_token_is (parser, CPP_NAME))
14217 c_parser_error (parser, "expected %<omp_priv%> or "
14218 "function-name");
14219 bad = true;
14221 else if (strcmp (IDENTIFIER_POINTER
14222 (c_parser_peek_token (parser)->value),
14223 "omp_priv") != 0)
14225 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
14226 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14228 c_parser_error (parser, "expected function-name %<(%>");
14229 bad = true;
14231 else
14232 initializer = c_parser_postfix_expression (parser);
14233 if (initializer.value
14234 && TREE_CODE (initializer.value) == CALL_EXPR)
14236 int j;
14237 tree c = initializer.value;
14238 for (j = 0; j < call_expr_nargs (c); j++)
14239 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
14240 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
14241 break;
14242 if (j == call_expr_nargs (c))
14243 error ("one of the initializer call arguments should be "
14244 "%<&omp_priv%>");
14247 else
14249 c_parser_consume_token (parser);
14250 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14251 bad = true;
14252 else
14254 tree st = push_stmt_list ();
14255 start_init (omp_priv, NULL_TREE, 0);
14256 location_t loc = c_parser_peek_token (parser)->location;
14257 struct c_expr init = c_parser_initializer (parser);
14258 finish_init ();
14259 finish_decl (omp_priv, loc, init.value,
14260 init.original_type, NULL_TREE);
14261 pop_stmt_list (st);
14264 if (!bad
14265 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14266 bad = true;
14269 if (!bad)
14271 c_parser_skip_to_pragma_eol (parser);
14273 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
14274 DECL_INITIAL (reduc_decl));
14275 DECL_INITIAL (reduc_decl) = t;
14276 DECL_SOURCE_LOCATION (omp_out) = rloc;
14277 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
14278 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
14279 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
14280 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
14281 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
14282 if (omp_priv)
14284 DECL_SOURCE_LOCATION (omp_priv) = rloc;
14285 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
14286 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
14287 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
14288 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
14289 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14290 walk_tree (&DECL_INITIAL (omp_priv),
14291 c_check_omp_declare_reduction_r,
14292 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14296 pop_stmt_list (stmt);
14297 pop_scope ();
14298 if (cfun->language != NULL)
14300 ggc_free (cfun->language);
14301 cfun->language = NULL;
14303 set_cfun (NULL);
14304 current_function_decl = NULL_TREE;
14305 if (nested)
14306 c_pop_function_context ();
14308 if (!clauses.is_empty ())
14310 parser->tokens = &parser->tokens_buf[0];
14311 parser->tokens_avail = tokens_avail;
14313 if (bad)
14314 goto fail;
14315 if (errs != errorcount)
14316 break;
14319 clauses.release ();
14320 types.release ();
14324 /* OpenMP 4.0
14325 #pragma omp declare simd declare-simd-clauses[optseq] new-line
14326 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14327 initializer-clause[opt] new-line
14328 #pragma omp declare target new-line */
14330 static void
14331 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
14333 c_parser_consume_pragma (parser);
14334 if (c_parser_next_token_is (parser, CPP_NAME))
14336 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14337 if (strcmp (p, "simd") == 0)
14339 /* c_parser_consume_token (parser); done in
14340 c_parser_omp_declare_simd. */
14341 c_parser_omp_declare_simd (parser, context);
14342 return;
14344 if (strcmp (p, "reduction") == 0)
14346 c_parser_consume_token (parser);
14347 c_parser_omp_declare_reduction (parser, context);
14348 return;
14350 if (!flag_openmp) /* flag_openmp_simd */
14352 c_parser_skip_to_pragma_eol (parser);
14353 return;
14355 if (strcmp (p, "target") == 0)
14357 c_parser_consume_token (parser);
14358 c_parser_omp_declare_target (parser);
14359 return;
14363 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
14364 "or %<target%>");
14365 c_parser_skip_to_pragma_eol (parser);
14368 /* Main entry point to parsing most OpenMP pragmas. */
14370 static void
14371 c_parser_omp_construct (c_parser *parser)
14373 enum pragma_kind p_kind;
14374 location_t loc;
14375 tree stmt;
14376 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
14377 omp_clause_mask mask (0);
14379 loc = c_parser_peek_token (parser)->location;
14380 p_kind = c_parser_peek_token (parser)->pragma_kind;
14381 c_parser_consume_pragma (parser);
14383 switch (p_kind)
14385 case PRAGMA_OACC_DATA:
14386 stmt = c_parser_oacc_data (loc, parser);
14387 break;
14388 case PRAGMA_OACC_KERNELS:
14389 strcpy (p_name, "#pragma acc");
14390 stmt = c_parser_oacc_kernels (loc, parser, p_name);
14391 break;
14392 case PRAGMA_OACC_LOOP:
14393 strcpy (p_name, "#pragma acc");
14394 stmt = c_parser_oacc_loop (loc, parser, p_name);
14395 break;
14396 case PRAGMA_OACC_PARALLEL:
14397 strcpy (p_name, "#pragma acc");
14398 stmt = c_parser_oacc_parallel (loc, parser, p_name);
14399 break;
14400 case PRAGMA_OACC_WAIT:
14401 strcpy (p_name, "#pragma wait");
14402 stmt = c_parser_oacc_wait (loc, parser, p_name);
14403 break;
14404 case PRAGMA_OMP_ATOMIC:
14405 c_parser_omp_atomic (loc, parser);
14406 return;
14407 case PRAGMA_OMP_CRITICAL:
14408 stmt = c_parser_omp_critical (loc, parser);
14409 break;
14410 case PRAGMA_OMP_DISTRIBUTE:
14411 strcpy (p_name, "#pragma omp");
14412 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
14413 break;
14414 case PRAGMA_OMP_FOR:
14415 strcpy (p_name, "#pragma omp");
14416 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
14417 break;
14418 case PRAGMA_OMP_MASTER:
14419 stmt = c_parser_omp_master (loc, parser);
14420 break;
14421 case PRAGMA_OMP_ORDERED:
14422 stmt = c_parser_omp_ordered (loc, parser);
14423 break;
14424 case PRAGMA_OMP_PARALLEL:
14425 strcpy (p_name, "#pragma omp");
14426 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
14427 break;
14428 case PRAGMA_OMP_SECTIONS:
14429 strcpy (p_name, "#pragma omp");
14430 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
14431 break;
14432 case PRAGMA_OMP_SIMD:
14433 strcpy (p_name, "#pragma omp");
14434 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
14435 break;
14436 case PRAGMA_OMP_SINGLE:
14437 stmt = c_parser_omp_single (loc, parser);
14438 break;
14439 case PRAGMA_OMP_TASK:
14440 stmt = c_parser_omp_task (loc, parser);
14441 break;
14442 case PRAGMA_OMP_TASKGROUP:
14443 stmt = c_parser_omp_taskgroup (parser);
14444 break;
14445 case PRAGMA_OMP_TEAMS:
14446 strcpy (p_name, "#pragma omp");
14447 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
14448 break;
14449 default:
14450 gcc_unreachable ();
14453 if (stmt)
14454 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
14458 /* OpenMP 2.5:
14459 # pragma omp threadprivate (variable-list) */
14461 static void
14462 c_parser_omp_threadprivate (c_parser *parser)
14464 tree vars, t;
14465 location_t loc;
14467 c_parser_consume_pragma (parser);
14468 loc = c_parser_peek_token (parser)->location;
14469 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14471 /* Mark every variable in VARS to be assigned thread local storage. */
14472 for (t = vars; t; t = TREE_CHAIN (t))
14474 tree v = TREE_PURPOSE (t);
14476 /* FIXME diagnostics: Ideally we should keep individual
14477 locations for all the variables in the var list to make the
14478 following errors more precise. Perhaps
14479 c_parser_omp_var_list_parens() should construct a list of
14480 locations to go along with the var list. */
14482 /* If V had already been marked threadprivate, it doesn't matter
14483 whether it had been used prior to this point. */
14484 if (TREE_CODE (v) != VAR_DECL)
14485 error_at (loc, "%qD is not a variable", v);
14486 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
14487 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
14488 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
14489 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
14490 else if (TREE_TYPE (v) == error_mark_node)
14492 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
14493 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
14494 else
14496 if (! DECL_THREAD_LOCAL_P (v))
14498 set_decl_tls_model (v, decl_default_tls_model (v));
14499 /* If rtl has been already set for this var, call
14500 make_decl_rtl once again, so that encode_section_info
14501 has a chance to look at the new decl flags. */
14502 if (DECL_RTL_SET_P (v))
14503 make_decl_rtl (v);
14505 C_DECL_THREADPRIVATE_P (v) = 1;
14509 c_parser_skip_to_pragma_eol (parser);
14512 /* Cilk Plus <#pragma simd> parsing routines. */
14514 /* Helper function for c_parser_pragma. Perform some sanity checking
14515 for <#pragma simd> constructs. Returns FALSE if there was a
14516 problem. */
14518 static bool
14519 c_parser_cilk_verify_simd (c_parser *parser,
14520 enum pragma_context context)
14522 if (!flag_cilkplus)
14524 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
14525 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14526 return false;
14528 if (context == pragma_external)
14530 c_parser_error (parser,"pragma simd must be inside a function");
14531 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14532 return false;
14534 return true;
14537 /* Cilk Plus:
14538 This function is shared by SIMD-enabled functions and #pragma simd.
14539 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
14540 CLAUSES is unused. The main purpose of this function is to parse a
14541 vectorlength attribute or clause and check for parse errors.
14542 When IS_SIMD_FN is true then the function is merely caching the tokens
14543 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
14544 cache is cleared since there is no reason to continue.
14545 Syntax:
14546 vectorlength ( constant-expression ) */
14548 static tree
14549 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
14550 bool is_simd_fn)
14552 if (is_simd_fn)
14553 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
14554 else
14555 /* The vectorlength clause behaves exactly like OpenMP's safelen
14556 clause. Represent it in OpenMP terms. */
14557 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
14559 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14560 return clauses;
14562 location_t loc = c_parser_peek_token (parser)->location;
14563 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14564 expr = c_fully_fold (expr, false, NULL);
14566 /* If expr is an error_mark_node then the above function would have
14567 emitted an error. No reason to do it twice. */
14568 if (expr == error_mark_node)
14570 else if (!TREE_TYPE (expr)
14571 || !TREE_CONSTANT (expr)
14572 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14574 error_at (loc, "vectorlength must be an integer constant");
14575 else if (wi::exact_log2 (expr) == -1)
14576 error_at (loc, "vectorlength must be a power of 2");
14577 else
14579 if (is_simd_fn)
14581 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
14582 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
14583 OMP_CLAUSE_CHAIN (u) = clauses;
14584 clauses = u;
14586 else
14588 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
14589 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
14590 OMP_CLAUSE_CHAIN (u) = clauses;
14591 clauses = u;
14595 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14597 return clauses;
14600 /* Cilk Plus:
14601 linear ( simd-linear-variable-list )
14603 simd-linear-variable-list:
14604 simd-linear-variable
14605 simd-linear-variable-list , simd-linear-variable
14607 simd-linear-variable:
14608 id-expression
14609 id-expression : simd-linear-step
14611 simd-linear-step:
14612 conditional-expression */
14614 static tree
14615 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
14617 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14618 return clauses;
14620 location_t loc = c_parser_peek_token (parser)->location;
14622 if (c_parser_next_token_is_not (parser, CPP_NAME)
14623 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14624 c_parser_error (parser, "expected identifier");
14626 while (c_parser_next_token_is (parser, CPP_NAME)
14627 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14629 tree var = lookup_name (c_parser_peek_token (parser)->value);
14631 if (var == NULL)
14633 undeclared_variable (c_parser_peek_token (parser)->location,
14634 c_parser_peek_token (parser)->value);
14635 c_parser_consume_token (parser);
14637 else if (var == error_mark_node)
14638 c_parser_consume_token (parser);
14639 else
14641 tree step = integer_one_node;
14643 /* Parse the linear step if present. */
14644 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14646 c_parser_consume_token (parser);
14647 c_parser_consume_token (parser);
14649 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14650 expr = c_fully_fold (expr, false, NULL);
14652 if (TREE_TYPE (expr)
14653 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
14654 && (TREE_CONSTANT (expr)
14655 || DECL_P (expr)))
14656 step = expr;
14657 else
14658 c_parser_error (parser,
14659 "step size must be an integer constant "
14660 "expression or an integer variable");
14662 else
14663 c_parser_consume_token (parser);
14665 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
14666 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
14667 OMP_CLAUSE_DECL (u) = var;
14668 OMP_CLAUSE_LINEAR_STEP (u) = step;
14669 OMP_CLAUSE_CHAIN (u) = clauses;
14670 clauses = u;
14673 if (c_parser_next_token_is_not (parser, CPP_COMMA))
14674 break;
14676 c_parser_consume_token (parser);
14679 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14681 return clauses;
14684 /* Returns the name of the next clause. If the clause is not
14685 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
14686 not consumed. Otherwise, the appropriate pragma_simd_clause is
14687 returned and the token is consumed. */
14689 static pragma_omp_clause
14690 c_parser_cilk_clause_name (c_parser *parser)
14692 pragma_omp_clause result;
14693 c_token *token = c_parser_peek_token (parser);
14695 if (!token->value || token->type != CPP_NAME)
14696 return PRAGMA_CILK_CLAUSE_NONE;
14698 const char *p = IDENTIFIER_POINTER (token->value);
14700 if (!strcmp (p, "vectorlength"))
14701 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
14702 else if (!strcmp (p, "linear"))
14703 result = PRAGMA_CILK_CLAUSE_LINEAR;
14704 else if (!strcmp (p, "private"))
14705 result = PRAGMA_CILK_CLAUSE_PRIVATE;
14706 else if (!strcmp (p, "firstprivate"))
14707 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
14708 else if (!strcmp (p, "lastprivate"))
14709 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
14710 else if (!strcmp (p, "reduction"))
14711 result = PRAGMA_CILK_CLAUSE_REDUCTION;
14712 else
14713 return PRAGMA_CILK_CLAUSE_NONE;
14715 c_parser_consume_token (parser);
14716 return result;
14719 /* Parse all #<pragma simd> clauses. Return the list of clauses
14720 found. */
14722 static tree
14723 c_parser_cilk_all_clauses (c_parser *parser)
14725 tree clauses = NULL;
14727 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14729 pragma_omp_clause c_kind;
14731 c_kind = c_parser_cilk_clause_name (parser);
14733 switch (c_kind)
14735 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
14736 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
14737 break;
14738 case PRAGMA_CILK_CLAUSE_LINEAR:
14739 clauses = c_parser_cilk_clause_linear (parser, clauses);
14740 break;
14741 case PRAGMA_CILK_CLAUSE_PRIVATE:
14742 /* Use the OpenMP counterpart. */
14743 clauses = c_parser_omp_clause_private (parser, clauses);
14744 break;
14745 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
14746 /* Use the OpenMP counterpart. */
14747 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14748 break;
14749 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
14750 /* Use the OpenMP counterpart. */
14751 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14752 break;
14753 case PRAGMA_CILK_CLAUSE_REDUCTION:
14754 /* Use the OpenMP counterpart. */
14755 clauses = c_parser_omp_clause_reduction (parser, clauses);
14756 break;
14757 default:
14758 c_parser_error (parser, "expected %<#pragma simd%> clause");
14759 goto saw_error;
14763 saw_error:
14764 c_parser_skip_to_pragma_eol (parser);
14765 return c_finish_cilk_clauses (clauses);
14768 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
14769 Here is the correct syntax of this pragma:
14770 #pragma cilk grainsize = <EXP>
14773 static void
14774 c_parser_cilk_grainsize (c_parser *parser)
14776 extern tree convert_to_integer (tree, tree);
14778 /* consume the 'grainsize' keyword. */
14779 c_parser_consume_pragma (parser);
14781 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
14783 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
14784 if (g_expr.value == error_mark_node)
14786 c_parser_skip_to_pragma_eol (parser);
14787 return;
14789 tree grain = convert_to_integer (long_integer_type_node,
14790 c_fully_fold (g_expr.value, false,
14791 NULL));
14792 c_parser_skip_to_pragma_eol (parser);
14793 c_token *token = c_parser_peek_token (parser);
14794 if (token && token->type == CPP_KEYWORD
14795 && token->keyword == RID_CILK_FOR)
14797 if (grain == NULL_TREE || grain == error_mark_node)
14798 grain = integer_zero_node;
14799 c_parser_cilk_for (parser, grain);
14801 else
14802 warning (0, "%<#pragma cilk grainsize%> is not followed by "
14803 "%<_Cilk_for%>");
14805 else
14806 c_parser_skip_to_pragma_eol (parser);
14809 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
14811 static void
14812 c_parser_cilk_simd (c_parser *parser)
14814 tree clauses = c_parser_cilk_all_clauses (parser);
14815 tree block = c_begin_compound_stmt (true);
14816 location_t loc = c_parser_peek_token (parser)->location;
14817 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
14818 block = c_end_compound_stmt (loc, block, true);
14819 add_stmt (block);
14822 /* Create an artificial decl with TYPE and emit initialization of it with
14823 INIT. */
14825 static tree
14826 c_get_temp_regvar (tree type, tree init)
14828 location_t loc = EXPR_LOCATION (init);
14829 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
14830 DECL_ARTIFICIAL (decl) = 1;
14831 DECL_IGNORED_P (decl) = 1;
14832 pushdecl (decl);
14833 tree t = build2 (INIT_EXPR, type, decl, init);
14834 add_stmt (t);
14835 return decl;
14838 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
14839 GRAIN is the grain value passed in through pragma or 0. */
14841 static void
14842 c_parser_cilk_for (c_parser *parser, tree grain)
14844 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
14845 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
14846 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
14847 clauses = c_finish_omp_clauses (clauses);
14849 tree block = c_begin_compound_stmt (true);
14850 tree sb = push_stmt_list ();
14851 location_t loc = c_parser_peek_token (parser)->location;
14852 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
14853 sb = pop_stmt_list (sb);
14855 if (omp_for)
14857 tree omp_par = make_node (OMP_PARALLEL);
14858 TREE_TYPE (omp_par) = void_type_node;
14859 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
14860 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
14861 TREE_SIDE_EFFECTS (bind) = 1;
14862 BIND_EXPR_BODY (bind) = sb;
14863 OMP_PARALLEL_BODY (omp_par) = bind;
14864 if (OMP_FOR_PRE_BODY (omp_for))
14866 add_stmt (OMP_FOR_PRE_BODY (omp_for));
14867 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
14869 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
14870 tree decl = TREE_OPERAND (init, 0);
14871 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
14872 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
14873 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
14874 if (TREE_CODE (t) != INTEGER_CST)
14876 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
14877 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14878 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
14879 OMP_CLAUSE_CHAIN (c) = clauses;
14880 clauses = c;
14882 if (TREE_CODE (incr) == MODIFY_EXPR)
14884 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
14885 if (TREE_CODE (t) != INTEGER_CST)
14887 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
14888 = c_get_temp_regvar (TREE_TYPE (t), t);
14889 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14890 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
14891 OMP_CLAUSE_CHAIN (c) = clauses;
14892 clauses = c;
14895 t = TREE_OPERAND (init, 1);
14896 if (TREE_CODE (t) != INTEGER_CST)
14898 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
14899 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14900 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
14901 OMP_CLAUSE_CHAIN (c) = clauses;
14902 clauses = c;
14904 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
14905 OMP_CLAUSE_DECL (c) = decl;
14906 OMP_CLAUSE_CHAIN (c) = clauses;
14907 clauses = c;
14908 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
14909 OMP_CLAUSE_OPERAND (c, 0)
14910 = cilk_for_number_of_iterations (omp_for);
14911 OMP_CLAUSE_CHAIN (c) = clauses;
14912 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c);
14913 add_stmt (omp_par);
14916 block = c_end_compound_stmt (loc, block, true);
14917 add_stmt (block);
14921 /* Parse a transaction attribute (GCC Extension).
14923 transaction-attribute:
14924 attributes
14925 [ [ any-word ] ]
14927 The transactional memory language description is written for C++,
14928 and uses the C++0x attribute syntax. For compatibility, allow the
14929 bracket style for transactions in C as well. */
14931 static tree
14932 c_parser_transaction_attributes (c_parser *parser)
14934 tree attr_name, attr = NULL;
14936 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
14937 return c_parser_attributes (parser);
14939 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
14940 return NULL_TREE;
14941 c_parser_consume_token (parser);
14942 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
14943 goto error1;
14945 attr_name = c_parser_attribute_any_word (parser);
14946 if (attr_name)
14948 c_parser_consume_token (parser);
14949 attr = build_tree_list (attr_name, NULL_TREE);
14951 else
14952 c_parser_error (parser, "expected identifier");
14954 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14955 error1:
14956 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14957 return attr;
14960 /* Parse a __transaction_atomic or __transaction_relaxed statement
14961 (GCC Extension).
14963 transaction-statement:
14964 __transaction_atomic transaction-attribute[opt] compound-statement
14965 __transaction_relaxed compound-statement
14967 Note that the only valid attribute is: "outer".
14970 static tree
14971 c_parser_transaction (c_parser *parser, enum rid keyword)
14973 unsigned int old_in = parser->in_transaction;
14974 unsigned int this_in = 1, new_in;
14975 location_t loc = c_parser_peek_token (parser)->location;
14976 tree stmt, attrs;
14978 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
14979 || keyword == RID_TRANSACTION_RELAXED)
14980 && c_parser_next_token_is_keyword (parser, keyword));
14981 c_parser_consume_token (parser);
14983 if (keyword == RID_TRANSACTION_RELAXED)
14984 this_in |= TM_STMT_ATTR_RELAXED;
14985 else
14987 attrs = c_parser_transaction_attributes (parser);
14988 if (attrs)
14989 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
14992 /* Keep track if we're in the lexical scope of an outer transaction. */
14993 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
14995 parser->in_transaction = new_in;
14996 stmt = c_parser_compound_statement (parser);
14997 parser->in_transaction = old_in;
14999 if (flag_tm)
15000 stmt = c_finish_transaction (loc, stmt, this_in);
15001 else
15002 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15003 "%<__transaction_atomic%> without transactional memory support enabled"
15004 : "%<__transaction_relaxed %> "
15005 "without transactional memory support enabled"));
15007 return stmt;
15010 /* Parse a __transaction_atomic or __transaction_relaxed expression
15011 (GCC Extension).
15013 transaction-expression:
15014 __transaction_atomic ( expression )
15015 __transaction_relaxed ( expression )
15018 static struct c_expr
15019 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
15021 struct c_expr ret;
15022 unsigned int old_in = parser->in_transaction;
15023 unsigned int this_in = 1;
15024 location_t loc = c_parser_peek_token (parser)->location;
15025 tree attrs;
15027 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15028 || keyword == RID_TRANSACTION_RELAXED)
15029 && c_parser_next_token_is_keyword (parser, keyword));
15030 c_parser_consume_token (parser);
15032 if (keyword == RID_TRANSACTION_RELAXED)
15033 this_in |= TM_STMT_ATTR_RELAXED;
15034 else
15036 attrs = c_parser_transaction_attributes (parser);
15037 if (attrs)
15038 this_in |= parse_tm_stmt_attr (attrs, 0);
15041 parser->in_transaction = this_in;
15042 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
15044 tree expr = c_parser_expression (parser).value;
15045 ret.original_type = TREE_TYPE (expr);
15046 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
15047 if (this_in & TM_STMT_ATTR_RELAXED)
15048 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
15049 SET_EXPR_LOCATION (ret.value, loc);
15050 ret.original_code = TRANSACTION_EXPR;
15051 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15053 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
15054 goto error;
15057 else
15059 error:
15060 ret.value = error_mark_node;
15061 ret.original_code = ERROR_MARK;
15062 ret.original_type = NULL;
15064 parser->in_transaction = old_in;
15066 if (!flag_tm)
15067 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15068 "%<__transaction_atomic%> without transactional memory support enabled"
15069 : "%<__transaction_relaxed %> "
15070 "without transactional memory support enabled"));
15072 return ret;
15075 /* Parse a __transaction_cancel statement (GCC Extension).
15077 transaction-cancel-statement:
15078 __transaction_cancel transaction-attribute[opt] ;
15080 Note that the only valid attribute is "outer".
15083 static tree
15084 c_parser_transaction_cancel (c_parser *parser)
15086 location_t loc = c_parser_peek_token (parser)->location;
15087 tree attrs;
15088 bool is_outer = false;
15090 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
15091 c_parser_consume_token (parser);
15093 attrs = c_parser_transaction_attributes (parser);
15094 if (attrs)
15095 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
15097 if (!flag_tm)
15099 error_at (loc, "%<__transaction_cancel%> without "
15100 "transactional memory support enabled");
15101 goto ret_error;
15103 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
15105 error_at (loc, "%<__transaction_cancel%> within a "
15106 "%<__transaction_relaxed%>");
15107 goto ret_error;
15109 else if (is_outer)
15111 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
15112 && !is_tm_may_cancel_outer (current_function_decl))
15114 error_at (loc, "outer %<__transaction_cancel%> not "
15115 "within outer %<__transaction_atomic%>");
15116 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
15117 goto ret_error;
15120 else if (parser->in_transaction == 0)
15122 error_at (loc, "%<__transaction_cancel%> not within "
15123 "%<__transaction_atomic%>");
15124 goto ret_error;
15127 return add_stmt (build_tm_abort_call (loc, is_outer));
15129 ret_error:
15130 return build1 (NOP_EXPR, void_type_node, error_mark_node);
15133 /* Parse a single source file. */
15135 void
15136 c_parse_file (void)
15138 /* Use local storage to begin. If the first token is a pragma, parse it.
15139 If it is #pragma GCC pch_preprocess, then this will load a PCH file
15140 which will cause garbage collection. */
15141 c_parser tparser;
15143 memset (&tparser, 0, sizeof tparser);
15144 tparser.tokens = &tparser.tokens_buf[0];
15145 the_parser = &tparser;
15147 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
15148 c_parser_pragma_pch_preprocess (&tparser);
15150 the_parser = ggc_alloc<c_parser> ();
15151 *the_parser = tparser;
15152 if (tparser.tokens == &tparser.tokens_buf[0])
15153 the_parser->tokens = &the_parser->tokens_buf[0];
15155 /* Initialize EH, if we've been told to do so. */
15156 if (flag_exceptions)
15157 using_eh_for_cleanups ();
15159 c_parser_translation_unit (the_parser);
15160 the_parser = NULL;
15163 /* This function parses Cilk Plus array notation. The starting index is
15164 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
15165 return value of this function is a tree_node called VALUE_TREE of type
15166 ARRAY_NOTATION_REF. */
15168 static tree
15169 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
15170 tree array_value)
15172 c_token *token = NULL;
15173 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
15174 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
15175 tree array_type_domain = NULL_TREE;
15177 if (array_value == error_mark_node || initial_index == error_mark_node)
15179 /* No need to continue. If either of these 2 were true, then an error
15180 must be emitted already. Thus, no need to emit them twice. */
15181 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15182 return error_mark_node;
15185 array_type = TREE_TYPE (array_value);
15186 gcc_assert (array_type);
15187 if (TREE_CODE (array_type) != ARRAY_TYPE
15188 && TREE_CODE (array_type) != POINTER_TYPE)
15190 error_at (loc, "base of array section must be pointer or array type");
15191 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15192 return error_mark_node;
15194 type = TREE_TYPE (array_type);
15195 token = c_parser_peek_token (parser);
15197 if (token->type == CPP_EOF)
15199 c_parser_error (parser, "expected %<:%> or numeral");
15200 return value_tree;
15202 else if (token->type == CPP_COLON)
15204 if (!initial_index)
15206 /* If we are here, then we have a case like this A[:]. */
15207 c_parser_consume_token (parser);
15208 if (TREE_CODE (array_type) == POINTER_TYPE)
15210 error_at (loc, "start-index and length fields necessary for "
15211 "using array notations in pointers");
15212 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15213 return error_mark_node;
15215 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15217 error_at (loc, "array notations cannot be used with function "
15218 "type");
15219 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15220 return error_mark_node;
15222 array_type_domain = TYPE_DOMAIN (array_type);
15224 if (!array_type_domain)
15226 error_at (loc, "start-index and length fields necessary for "
15227 "using array notations in dimensionless arrays");
15228 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15229 return error_mark_node;
15232 start_index = TYPE_MINVAL (array_type_domain);
15233 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
15234 start_index);
15235 if (!TYPE_MAXVAL (array_type_domain)
15236 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
15238 error_at (loc, "start-index and length fields necessary for "
15239 "using array notations in variable-length arrays");
15240 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15241 return error_mark_node;
15243 end_index = TYPE_MAXVAL (array_type_domain);
15244 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
15245 end_index, integer_one_node);
15246 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
15247 stride = build_int_cst (integer_type_node, 1);
15248 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
15250 else if (initial_index != error_mark_node)
15252 /* If we are here, then there should be 2 possibilities:
15253 1. Array [EXPR : EXPR]
15254 2. Array [EXPR : EXPR : EXPR]
15256 start_index = initial_index;
15258 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15260 error_at (loc, "array notations cannot be used with function "
15261 "type");
15262 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15263 return error_mark_node;
15265 c_parser_consume_token (parser); /* consume the ':' */
15266 struct c_expr ce = c_parser_expression (parser);
15267 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15268 end_index = ce.value;
15269 if (!end_index || end_index == error_mark_node)
15271 c_parser_skip_to_end_of_block_or_statement (parser);
15272 return error_mark_node;
15274 if (c_parser_peek_token (parser)->type == CPP_COLON)
15276 c_parser_consume_token (parser);
15277 ce = c_parser_expression (parser);
15278 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15279 stride = ce.value;
15280 if (!stride || stride == error_mark_node)
15282 c_parser_skip_to_end_of_block_or_statement (parser);
15283 return error_mark_node;
15287 else
15288 c_parser_error (parser, "expected array notation expression");
15290 else
15291 c_parser_error (parser, "expected array notation expression");
15293 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15295 value_tree = build_array_notation_ref (loc, array_value, start_index,
15296 end_index, stride, type);
15297 if (value_tree != error_mark_node)
15298 SET_EXPR_LOCATION (value_tree, loc);
15299 return value_tree;
15302 #include "gt-c-c-parser.h"